Debian Codegraph @

terps/tads/tads2/argize.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1998 by Christopher Nebel. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name:
9 argize.c
10
11 Function:
12 Break a string into argc/argv-style arguments.
13
14 Notes:
15 none
16
17 Modified:
18 <2>07/04/98 CNebel - Fixed so that args wouldn't pick up trailing
19 whitespace.
20 <1>07/04/98 CNebel - Created based on older Mac-specific argize source
21 */
22
23 #include "argize.h"
24
25 #include <stddef.h>
26 #include <ctype.h>
27
28
29 /*
30 * Return what argc would be if this string were fed to argize.
31 */
32 int countargs(const char *cmdline)
33 {
34 const char *p = cmdline;
35 int state = 0;
36 int argc = 0;
37
38 while (*p) {
39 switch (state) {
40 case 0: /* skip leading white space */
41 while (isspace((unsigned char)*p)) p++;
42 if (!*p) break; /* If there's nothing left, get out. */
43 state = 1;
44 /* fall through */;
45
46 case 1: /* begin arg */
47 argc++;
48 state = 2;
49 /* fall through */
50
51 case 2: /* collect chars for current arg */
52 if (isspace((unsigned char)*p))
53 p++, state = 0; /* Terminate this arg */
54 else if (*p == '"')
55 p++, state = 3; /* Open quote. */
56 else if (*p == '\\')
57 p++, state = 4; /* Escape sequence. */
58 else
59 p++; /* Just keep going. */
60
61 break;
62
63 case 3: /* collect chars inside quotes. */
64 switch (*p) {
65 case '"' : p++; state = 2; break; /* Close quote */
66 case '\\': p++; state = 5; break; /* Escape sequence. */
67 default: p++; break;
68 }
69 break;
70
71 case 4: /* escaped character outside quotes. */
72 p++; state = 2;
73 break;
74
75 case 5: /* escaped character inside quotes. */
76 p++; state = 3;
77 break;
78 }
79 }
80
81 return argc;
82 }
83
84
85 /*
86 * Break a string <cmdline> into argc/argv arguments, removing quotes in
87 * the process. Returns 0 on success, 1 if there were too many
88 * arguments.
89 */
90 int argize(char *cmdline, int *const argc, char *argv[], const size_t argvlen)
91 {
92 char *p, *q;
93 int state;
94
95 p = q = cmdline;
96 *argc = 0;
97 state = 0;
98
99 while (*p) {
100 switch (state) {
101 case 0: /* skip leading white space */
102 while (isspace((unsigned char)*p)) p++, q++;
103 if (!*p) break; /* If there's nothing left, get out. */
104 state = 1;
105 /* fall through */;
106
107 case 1: /* begin arg */
108 if ((size_t)*argc < argvlen)
109 argv[(*argc)++] = q;
110 else
111 return 1;
112 state = 2;
113 /* fall through */
114
115 case 2: /* collect chars for current arg */
116 if (isspace((unsigned char)*p))
117 *q++ = *p++ = '\0', state = 0; /* Terminate this arg */
118 else if (*p == '"')
119 p++, state = 3; /* Open quote. */
120 else if (*p == '\\')
121 p++, state = 4; /* Escape sequence. */
122 else
123 *q++ = *p++; /* Just keep going. */
124
125 break;
126
127 case 3: /* collect chars inside quotes. */
128 switch (*p) {
129 case '"' : p++; state = 2; break; /* Close quote.*/
130 case '\\': p++; state = 5; break; /* Escape sequence. */
131 default: *q++ = *p++; break;
132 }
133 break;
134
135 case 4: /* escaped character outside quotes. */
136 *q++ = *p++; state = 2;
137 break;
138
139 case 5: /* escaped character inside quotes. */
140 *q++ = *p++; state = 3;
141 break;
142 }
143 }
144 *q = '\0';
145 return 0;
146 }