1899
|
1 /*
|
|
2 convenient routines for argument list handling
|
|
3
|
|
4 Copyright (C) Andrew Tridgell 2002
|
|
5
|
|
6 This program is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2 of the License, or
|
|
9 (at your option) any later version.
|
|
10
|
|
11 This program is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with this program; if not, write to the Free Software
|
|
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19 */
|
|
20
|
|
21 #include "ccache.h"
|
|
22
|
|
23 ARGS *args_init(int init_argc, char **init_args)
|
|
24 {
|
|
25 ARGS *args;
|
|
26 int i;
|
|
27 args = (ARGS *)x_malloc(sizeof(ARGS));
|
|
28 args->argc = 0;
|
|
29 args->argv = (char **)x_malloc(sizeof(char *));
|
|
30 args->argv[0] = NULL;
|
|
31 for (i=0;i<init_argc;i++) {
|
|
32 args_add(args, init_args[i]);
|
|
33 }
|
|
34 return args;
|
|
35 }
|
|
36
|
|
37
|
|
38 void args_add(ARGS *args, const char *s)
|
|
39 {
|
|
40 args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char *));
|
|
41 args->argv[args->argc] = x_strdup(s);
|
|
42 args->argc++;
|
|
43 args->argv[args->argc] = NULL;
|
|
44 }
|
|
45
|
|
46 /* pop the last element off the args list */
|
|
47 void args_pop(ARGS *args, int n)
|
|
48 {
|
|
49 while (n--) {
|
|
50 args->argc--;
|
|
51 free(args->argv[args->argc]);
|
|
52 args->argv[args->argc] = NULL;
|
|
53 }
|
|
54 }
|
|
55
|
|
56 /* remove the first element of the argument list */
|
|
57 void args_remove_first(ARGS *args)
|
|
58 {
|
|
59 free(args->argv[0]);
|
|
60 memmove(&args->argv[0],
|
|
61 &args->argv[1],
|
|
62 args->argc * sizeof(args->argv[0]));
|
|
63 args->argc--;
|
|
64 }
|
|
65
|
|
66 /* add an argument into the front of the argument list */
|
|
67 void args_add_prefix(ARGS *args, const char *s)
|
|
68 {
|
|
69 args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char *));
|
|
70 memmove(&args->argv[1], &args->argv[0],
|
|
71 (args->argc+1) * sizeof(args->argv[0]));
|
|
72 args->argv[0] = x_strdup(s);
|
|
73 args->argc++;
|
|
74 }
|
|
75
|
|
76 /* strip any arguments beginning with the specified prefix */
|
|
77 void args_strip(ARGS *args, const char *prefix)
|
|
78 {
|
|
79 int i;
|
|
80 for (i=0; i<args->argc; ) {
|
|
81 if (strncmp(args->argv[i], prefix, strlen(prefix)) == 0) {
|
|
82 free(args->argv[i]);
|
|
83 memmove(&args->argv[i],
|
|
84 &args->argv[i+1],
|
|
85 args->argc * sizeof(args->argv[i]));
|
|
86 args->argc--;
|
|
87 } else {
|
|
88 i++;
|
|
89 }
|
|
90 }
|
|
91 }
|