Debian Codegraph @

libinetutils/argcv.c

raw
1 /* argcv.c - simple functions for parsing input based on whitespace
2 Copyright (C) 2001-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils 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 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 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, see `http://www.gnu.org/licenses/'. */
18
19 #include <config.h>
20
21 #include "argcv.h"
22 #include <ctype.h>
23 #include <progname.h>
24
25 /*
26 * takes a string and splits it into several strings, breaking at ' '
27 * command is the string to split
28 * the number of strings is placed into argc
29 * the split strings are put into argv
30 * returns 0 on success, nonzero on failure
31 */
32
33 #define isws(c) ((c)==' '||(c)=='\t')
34 #define isdelim(c,delim) ((c)=='"'||strchr(delim,(c))!=NULL)
35
36 static int
37 argcv_scan (int len, const char *command, const char *delim,
38 int *start, int *end, int *save)
39 {
40 int i = *save;
41
42 /* Skip initial whitespace */
43 while (i < len && isws (command[i]))
44 i++;
45 *start = i;
46
47 switch (command[i])
48 {
49 case '"':
50 case '\'':
51 while (++i < len && command[i] != command[*start])
52 ;
53 if (i < len) /* found matching quote */
54 break;
55 default:
56 if (isdelim (command[i], delim))
57 break;
58 /* Skip until next whitespace character or end of line */
59 while (++i < len && !(isws (command[i]) || isdelim (command[i], delim)))
60 ;
61 i--;
62 break;
63 }
64
65 *end = i;
66 *save = i + 1;
67 return *save;
68 }
69
70 int
71 argcv_get (const char *command, const char *delim, int *argc, char ***argv)
72 {
73 int len = strlen (command);
74 int i = 0;
75 int start, end, save;
76
77 *argc = 0;
78 *argv = NULL;
79
80 while (len > 0 && isspace ((int) command[len - 1]))
81 len--;
82 if (len < 1)
83 return 1;
84
85 /* Count number of arguments */
86 *argc = 1;
87 save = 0;
88 while (argcv_scan (len, command, delim, &start, &end, &save) < len)
89 (*argc)++;
90
91 *argv = calloc ((*argc + 1), sizeof (char *));
92
93 i = 0;
94 save = 0;
95 for (i = 0; i < *argc; i++)
96 {
97 int n;
98 argcv_scan (len, command, delim, &start, &end, &save);
99
100 if (command[start] == '"' && command[end] == '"')
101 {
102 start++;
103 end--;
104 }
105 else if (command[start] == '\'' && command[end] == '\'')
106 {
107 start++;
108 end--;
109 }
110 n = end - start + 1;
111 (*argv)[i] = calloc (n + 1, sizeof (char));
112 if ((*argv)[i] == NULL)
113 return 1;
114 memcpy ((*argv)[i], &command[start], n);
115 (*argv)[i][n] = 0;
116 }
117 (*argv)[i] = NULL;
118 return 0;
119 }
120
121 /*
122 * frees all elements of an argv array
123 * argc is the number of elements
124 * argv is the array
125 */
126 int
127 argcv_free (int argc, char **argv)
128 {
129 while (--argc >= 0)
130 free (argv[argc]);
131 free (argv);
132 return 1;
133 }
134
135 /* Take a argv an make string separated by ' '. */
136
137 int
138 argcv_string (int argc, char **argv, char **pstring)
139 {
140 int i;
141 size_t len;
142 char *buffer;
143
144 /* No need. */
145 if (pstring == NULL)
146 return 1;
147
148 buffer = malloc (1);
149 if (buffer == NULL)
150 return 1;
151 *buffer = '\0';
152
153 for (len = i = 0; i < argc; i++)
154 {
155 len += strlen (argv[i]) + 2;
156 buffer = realloc (buffer, len);
157 if (buffer == NULL)
158 return 1;
159 if (i != 0)
160 strcat (buffer, " ");
161 strcat (buffer, argv[i]);
162 }
163
164 /* Strip off trailing space. */
165 if (*buffer != '\0')
166 {
167 while (buffer[strlen (buffer) - 1] == ' ')
168 {
169 buffer[strlen (buffer) - 1] = '\0';
170 }
171 }
172 if (pstring)
173 *pstring = buffer;
174 return 0;
175 }
176
177 #if 0
178 char *command = "set prompt=\"& \"";
179
180 int
181 main (int argc, char **argv)
182 {
183 int i, argc;
184 char **argv;
185 set_program_name (argv[0]);
186 argcv_get (command, "=", &argc, &argv);
187 printf ("%d args:\n", argc);
188 for (i = 0; i < argc; i++)
189 printf ("%s\n", argv[i]);
190 }
191 #endif