OLD | NEW |
1 /* Create and destroy argument vectors (argv's) | 1 /* Create and destroy argument vectors (argv's) |
2 Copyright (C) 1992, 2001, 2010 Free Software Foundation, Inc. | 2 Copyright (C) 1992, 2001, 2010, 2012 Free Software Foundation, Inc. |
3 Written by Fred Fish @ Cygnus Support | 3 Written by Fred Fish @ Cygnus Support |
4 | 4 |
5 This file is part of the libiberty library. | 5 This file is part of the libiberty library. |
6 Libiberty is free software; you can redistribute it and/or | 6 Libiberty is free software; you can redistribute it and/or |
7 modify it under the terms of the GNU Library General Public | 7 modify it under the terms of the GNU Library General Public |
8 License as published by the Free Software Foundation; either | 8 License as published by the Free Software Foundation; either |
9 version 2 of the License, or (at your option) any later version. | 9 version 2 of the License, or (at your option) any later version. |
10 | 10 |
11 Libiberty is distributed in the hope that it will be useful, | 11 Libiberty is distributed in the hope that it will be useful, |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 dupargv (char **argv) | 65 dupargv (char **argv) |
66 { | 66 { |
67 int argc; | 67 int argc; |
68 char **copy; | 68 char **copy; |
69 | 69 |
70 if (argv == NULL) | 70 if (argv == NULL) |
71 return NULL; | 71 return NULL; |
72 | 72 |
73 /* the vector */ | 73 /* the vector */ |
74 for (argc = 0; argv[argc] != NULL; argc++); | 74 for (argc = 0; argv[argc] != NULL; argc++); |
75 copy = (char **) malloc ((argc + 1) * sizeof (char *)); | 75 copy = (char **) xmalloc ((argc + 1) * sizeof (char *)); |
76 if (copy == NULL) | 76 |
77 return NULL; | |
78 | |
79 /* the strings */ | 77 /* the strings */ |
80 for (argc = 0; argv[argc] != NULL; argc++) | 78 for (argc = 0; argv[argc] != NULL; argc++) |
81 { | 79 { |
82 int len = strlen (argv[argc]); | 80 int len = strlen (argv[argc]); |
83 copy[argc] = (char *) malloc (len + 1); | 81 copy[argc] = (char *) xmalloc (len + 1); |
84 if (copy[argc] == NULL) | |
85 » { | |
86 » freeargv (copy); | |
87 » return NULL; | |
88 » } | |
89 strcpy (copy[argc], argv[argc]); | 82 strcpy (copy[argc], argv[argc]); |
90 } | 83 } |
91 copy[argc] = NULL; | 84 copy[argc] = NULL; |
92 return copy; | 85 return copy; |
93 } | 86 } |
94 | 87 |
95 /* | 88 /* |
96 | 89 |
97 @deftypefn Extension void freeargv (char **@var{vector}) | 90 @deftypefn Extension void freeargv (char **@var{vector}) |
98 | 91 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 @deftypefn Extension char** buildargv (char *@var{sp}) | 135 @deftypefn Extension char** buildargv (char *@var{sp}) |
143 | 136 |
144 Given a pointer to a string, parse the string extracting fields | 137 Given a pointer to a string, parse the string extracting fields |
145 separated by whitespace and optionally enclosed within either single | 138 separated by whitespace and optionally enclosed within either single |
146 or double quotes (which are stripped off), and build a vector of | 139 or double quotes (which are stripped off), and build a vector of |
147 pointers to copies of the string for each field. The input string | 140 pointers to copies of the string for each field. The input string |
148 remains unchanged. The last element of the vector is followed by a | 141 remains unchanged. The last element of the vector is followed by a |
149 @code{NULL} element. | 142 @code{NULL} element. |
150 | 143 |
151 All of the memory for the pointer array and copies of the string | 144 All of the memory for the pointer array and copies of the string |
152 is obtained from @code{malloc}. All of the memory can be returned to the | 145 is obtained from @code{xmalloc}. All of the memory can be returned to the |
153 system with the single function call @code{freeargv}, which takes the | 146 system with the single function call @code{freeargv}, which takes the |
154 returned result of @code{buildargv}, as it's argument. | 147 returned result of @code{buildargv}, as it's argument. |
155 | 148 |
156 Returns a pointer to the argument vector if successful. Returns | 149 Returns a pointer to the argument vector if successful. Returns |
157 @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient | 150 @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient |
158 memory to complete building the argument vector. | 151 memory to complete building the argument vector. |
159 | 152 |
160 If the input is a null string (as opposed to a @code{NULL} pointer), | 153 If the input is a null string (as opposed to a @code{NULL} pointer), |
161 then buildarg returns an argument vector that has one arg, a null | 154 then buildarg returns an argument vector that has one arg, a null |
162 string. | 155 string. |
(...skipping 21 matching lines...) Expand all Loading... |
184 int squote = 0; | 177 int squote = 0; |
185 int dquote = 0; | 178 int dquote = 0; |
186 int bsquote = 0; | 179 int bsquote = 0; |
187 int argc = 0; | 180 int argc = 0; |
188 int maxargc = 0; | 181 int maxargc = 0; |
189 char **argv = NULL; | 182 char **argv = NULL; |
190 char **nargv; | 183 char **nargv; |
191 | 184 |
192 if (input != NULL) | 185 if (input != NULL) |
193 { | 186 { |
194 copybuf = (char *) alloca (strlen (input) + 1); | 187 copybuf = (char *) xmalloc (strlen (input) + 1); |
195 /* Is a do{}while to always execute the loop once. Always return an | 188 /* Is a do{}while to always execute the loop once. Always return an |
196 argv, even for null strings. See NOTES above, test case below. */ | 189 argv, even for null strings. See NOTES above, test case below. */ |
197 do | 190 do |
198 { | 191 { |
199 /* Pick off argv[argc] */ | 192 /* Pick off argv[argc] */ |
200 consume_whitespace (&input); | 193 consume_whitespace (&input); |
201 | 194 |
202 if ((maxargc == 0) || (argc >= (maxargc - 1))) | 195 if ((maxargc == 0) || (argc >= (maxargc - 1))) |
203 { | 196 { |
204 /* argv needs initialization, or expansion */ | 197 /* argv needs initialization, or expansion */ |
205 if (argv == NULL) | 198 if (argv == NULL) |
206 { | 199 { |
207 maxargc = INITIAL_MAXARGC; | 200 maxargc = INITIAL_MAXARGC; |
208 » » nargv = (char **) malloc (maxargc * sizeof (char *)); | 201 » » nargv = (char **) xmalloc (maxargc * sizeof (char *)); |
209 } | 202 } |
210 else | 203 else |
211 { | 204 { |
212 maxargc *= 2; | 205 maxargc *= 2; |
213 » » nargv = (char **) realloc (argv, maxargc * sizeof (char *)); | 206 » » nargv = (char **) xrealloc (argv, maxargc * sizeof (char *)); |
214 » » } | |
215 » if (nargv == NULL) | |
216 » » { | |
217 » » if (argv != NULL) | |
218 » » { | |
219 » » freeargv (argv); | |
220 » » argv = NULL; | |
221 » » } | |
222 » » break; | |
223 } | 207 } |
224 argv = nargv; | 208 argv = nargv; |
225 argv[argc] = NULL; | 209 argv[argc] = NULL; |
226 } | 210 } |
227 /* Begin scanning arg */ | 211 /* Begin scanning arg */ |
228 arg = copybuf; | 212 arg = copybuf; |
229 while (*input != EOS) | 213 while (*input != EOS) |
230 { | 214 { |
231 if (ISSPACE (*input) && !squote && !dquote && !bsquote) | 215 if (ISSPACE (*input) && !squote && !dquote && !bsquote) |
232 { | 216 { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 261 } |
278 else | 262 else |
279 { | 263 { |
280 *arg++ = *input; | 264 *arg++ = *input; |
281 } | 265 } |
282 } | 266 } |
283 input++; | 267 input++; |
284 } | 268 } |
285 } | 269 } |
286 *arg = EOS; | 270 *arg = EOS; |
287 » argv[argc] = strdup (copybuf); | 271 » argv[argc] = xstrdup (copybuf); |
288 » if (argv[argc] == NULL) | |
289 » { | |
290 » freeargv (argv); | |
291 » argv = NULL; | |
292 » break; | |
293 » } | |
294 argc++; | 272 argc++; |
295 argv[argc] = NULL; | 273 argv[argc] = NULL; |
296 | 274 |
297 consume_whitespace (&input); | 275 consume_whitespace (&input); |
298 } | 276 } |
299 while (*input != EOS); | 277 while (*input != EOS); |
| 278 |
| 279 free (copybuf); |
300 } | 280 } |
301 return (argv); | 281 return (argv); |
302 } | 282 } |
303 | 283 |
304 /* | 284 /* |
305 | 285 |
306 @deftypefn Extension int writeargv (const char **@var{argv}, FILE *@var{file}) | 286 @deftypefn Extension int writeargv (const char **@var{argv}, FILE *@var{file}) |
307 | 287 |
308 Write each member of ARGV, handling all necessary quoting, to the file | 288 Write each member of ARGV, handling all necessary quoting, to the file |
309 named by FILE, separated by whitespace. Return 0 on success, non-zero | 289 named by FILE, separated by whitespace. Return 0 on success, non-zero |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 if (only_whitespace (buffer)) | 428 if (only_whitespace (buffer)) |
449 { | 429 { |
450 file_argv = (char **) xmalloc (sizeof (char *)); | 430 file_argv = (char **) xmalloc (sizeof (char *)); |
451 file_argv[0] = NULL; | 431 file_argv[0] = NULL; |
452 } | 432 } |
453 else | 433 else |
454 /* Parse the string. */ | 434 /* Parse the string. */ |
455 file_argv = buildargv (buffer); | 435 file_argv = buildargv (buffer); |
456 /* If *ARGVP is not already dynamically allocated, copy it. */ | 436 /* If *ARGVP is not already dynamically allocated, copy it. */ |
457 if (!argv_dynamic) | 437 if (!argv_dynamic) |
458 » { | 438 » *argvp = dupargv (*argvp); |
459 » *argvp = dupargv (*argvp); | |
460 » if (!*argvp) | |
461 » { | |
462 » fputs ("\nout of memory\n", stderr); | |
463 » xexit (1); | |
464 » } | |
465 » } | |
466 /* Count the number of arguments. */ | 439 /* Count the number of arguments. */ |
467 file_argc = 0; | 440 file_argc = 0; |
468 while (file_argv[file_argc]) | 441 while (file_argv[file_argc]) |
469 ++file_argc; | 442 ++file_argc; |
470 /* Now, insert FILE_ARGV into ARGV. The "+1" below handles the | 443 /* Now, insert FILE_ARGV into ARGV. The "+1" below handles the |
471 NULL terminator at the end of ARGV. */ | 444 NULL terminator at the end of ARGV. */ |
472 *argvp = ((char **) | 445 *argvp = ((char **) |
473 xrealloc (*argvp, | 446 xrealloc (*argvp, |
474 (*argcp + file_argc + 1) * sizeof (char *))); | 447 (*argcp + file_argc + 1) * sizeof (char *))); |
475 memmove (*argvp + i + file_argc, *argvp + i + 1, | 448 memmove (*argvp + i + file_argc, *argvp + i + 1, |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 } | 531 } |
559 printf ("\n"); | 532 printf ("\n"); |
560 } | 533 } |
561 freeargv (argv); | 534 freeargv (argv); |
562 } | 535 } |
563 | 536 |
564 return 0; | 537 return 0; |
565 } | 538 } |
566 | 539 |
567 #endif /* MAIN */ | 540 #endif /* MAIN */ |
OLD | NEW |