]>
git.ipfire.org Git - thirdparty/bash.git/blob - stringlib.c
d232837dd9f492e48204afbc8d7860604b097b5c
1 /* stringlib.c - Miscellaneous string functions. */
4 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #include "bashtypes.h"
26 #if defined (HAVE_UNISTD_H)
37 #if defined (EXTENDED_GLOB)
38 # include <glob/fnmatch.h>
41 /* **************************************************************** */
43 /* Functions to manage arrays of strings */
45 /* **************************************************************** */
47 /* Cons up a new array of words. The words are taken from LIST,
48 which is a WORD_LIST *. If COPY is true, everything is malloc'ed,
49 so you should free everything in this array when you are done.
50 The array is NULL terminated. If IP is non-null, it gets the
51 number of words in the returned array. STARTING_INDEX says where
52 to start filling in the returned array; it can be used to reserve
53 space at the beginning of the array. */
55 word_list_to_argv (list
, copy
, starting_index
, ip
)
57 int copy
, starting_index
, *ip
;
62 count
= list_length (list
);
63 array
= (char **)xmalloc ((1 + count
+ starting_index
) * sizeof (char *));
65 for (count
= 0; count
< starting_index
; count
++)
66 array
[count
] = (char *)NULL
;
67 for (count
= starting_index
; list
; count
++, list
= list
->next
)
68 array
[count
] = copy
? savestring (list
->word
->word
) : list
->word
->word
;
69 array
[count
] = (char *)NULL
;
76 /* Convert an array of strings into the form used internally by the shell.
77 COPY means to copy the values in ARRAY into the returned list rather
78 than allocate new storage. STARTING_INDEX says where in ARRAY to begin. */
80 argv_to_word_list (array
, copy
, starting_index
)
82 int copy
, starting_index
;
88 if (array
== 0 || array
[0] == 0)
89 return (WORD_LIST
*)NULL
;
91 for (count
= 0; array
[count
]; count
++)
94 for (i
= starting_index
, list
= (WORD_LIST
*)NULL
; i
< count
; i
++)
96 w
= make_bare_word (copy
? "" : array
[i
]);
102 list
= make_word_list (w
, list
);
104 return (REVERSE_LIST(list
, WORD_LIST
*));
107 /* Find STRING in ALIST, a list of string key/int value pairs. If FLAGS
108 is 1, STRING is treated as a pattern and matched using fnmatch. */
110 find_string_in_alist (string
, alist
, flags
)
112 STRING_INT_ALIST
*alist
;
118 for (i
= r
= 0; alist
[i
].word
; i
++)
120 #if defined (EXTENDED_GLOB)
122 r
= fnmatch (alist
[i
].word
, string
, FNM_EXTMATCH
) != FNM_NOMATCH
;
125 r
= STREQ (string
, alist
[i
].word
);
128 return (alist
[i
].token
);
133 /* **************************************************************** */
135 /* String Management Functions */
137 /* **************************************************************** */
139 /* Replace occurrences of PAT with REP in STRING. If GLOBAL is non-zero,
140 replace all occurrences, otherwise replace only the first.
141 This returns a new string; the caller should free it. */
143 strsub (string
, pat
, rep
, global
)
144 char *string
, *pat
, *rep
;
147 int patlen
, replen
, templen
, tempsize
, repl
, i
;
150 patlen
= strlen (pat
);
151 replen
= strlen (rep
);
152 for (temp
= (char *)NULL
, i
= templen
= tempsize
= 0, repl
= 1; string
[i
]; )
154 if (repl
&& STREQN (string
+ i
, pat
, patlen
))
156 RESIZE_MALLOCED_BUFFER (temp
, templen
, replen
, tempsize
, (replen
* 2));
159 temp
[templen
++] = *r
++;
166 RESIZE_MALLOCED_BUFFER (temp
, templen
, 1, tempsize
, 16);
167 temp
[templen
++] = string
[i
++];
174 /* Replace all instances of C in STRING with TEXT. TEXT may be empty or
175 NULL. If DO_GLOB is non-zero, we quote the replacement text for
176 globbing. Backslash may be used to quote C. */
178 strcreplace (string
, c
, text
, do_glob
)
184 char *ret
, *p
, *r
, *t
;
185 int len
, rlen
, ind
, tlen
;
188 rlen
= len
+ strlen (string
) + 2;
189 ret
= xmalloc (rlen
);
191 for (p
= string
, r
= ret
; p
&& *p
; )
198 if (do_glob
&& (glob_pattern_p (text
) || strchr (text
, '\\')))
200 t
= quote_globbing_chars (text
);
202 RESIZE_MALLOCED_BUFFER (ret
, ind
, tlen
, rlen
, rlen
);
203 r
= ret
+ ind
; /* in case reallocated */
210 RESIZE_MALLOCED_BUFFER (ret
, ind
, len
, rlen
, rlen
);
211 r
= ret
+ ind
; /* in case reallocated */
220 if (*p
== '\\' && p
[1] == c
)
223 RESIZE_MALLOCED_BUFFER (ret
, ind
, 2, rlen
, rlen
);
224 r
= ret
+ ind
; /* in case reallocated */
232 #ifdef INCLUDE_UNUSED
233 /* Remove all leading whitespace from STRING. This includes
234 newlines. STRING should be terminated with a zero. */
236 strip_leading (string
)
239 char *start
= string
;
241 while (*string
&& (whitespace (*string
) || *string
== '\n'))
246 int len
= strlen (string
);
247 FASTCOPY (string
, start
, len
);
253 /* Remove all trailing whitespace from STRING. This includes
254 newlines. If NEWLINES_ONLY is non-zero, only trailing newlines
255 are removed. STRING should be terminated with a zero. */
257 strip_trailing (string
, len
, newlines_only
)
264 if ((newlines_only
&& string
[len
] == '\n') ||
265 (!newlines_only
&& whitespace (string
[len
])))
270 string
[len
+ 1] = '\0';
273 /* A wrapper for bcopy that can be prototyped in general.h */