1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/* arg.c -- Argument list support
* Created: Sun Jan 7 13:39:29 1996 by faith@dict.org
* Copyright 1996-1997, 2002 Rickard E. Faith (faith@dict.org)
* Copyright 2002-2008 Aleksey Cheusov (vle@gmx.net)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* \section{Argument List Routines}
*
* \intro Argument lists are vectors of pointers to strings (e.g., the
* standard |char **argv|). These routines manage the efficient creation,
* manipulation, and deletion of these sorts of lists.
*
*/
#include "maaP.h"
typedef struct Arg {
#if MAA_MAGIC
unsigned magic;
#endif
int argc; /* Current count */
int argm; /* Maximum count */
char **argv; /* Vector */
mem_String object;
} *Arg;
static void _arg_check(arg_List arg, const char *function)
{
Arg a = (Arg)arg;
if (!a) err_internal(function, "arg is null");
#if MAA_MAGIC
if (a->magic != ARG_MAGIC)
err_internal(function,
"Magic match failed: 0x%08x (should be 0x%08x)",
a->magic,
ARG_MAGIC);
#endif
}
/* \doc Create an |arg_List| object. */
arg_List arg_create(void)
{
Arg a = xmalloc(sizeof(struct Arg));
#if MAA_MAGIC
a->magic = ARG_MAGIC;
#endif
a->argc = 0;
a->argm = 2;
a->argv = xmalloc(a->argm * sizeof(char **));
a->argv[0] = NULL;
a->object = mem_create_strings();
return a;
}
/* \doc Free all memory associated with |arg|, including the memory used
for the strings. */
void arg_destroy(arg_List arg)
{
Arg a = (Arg)arg;
_arg_check(a, __func__);
mem_destroy_strings(a->object);
xfree(a->argv);
#if MAA_MAGIC
a->magic = ARG_MAGIC_FREED;
#endif
xfree(a);
}
/* \doc Add |string| as the next item in |arg|. */
arg_List arg_add(arg_List arg, const char *string)
{
Arg a = (Arg)arg;
const char *new;
_arg_check(a, __func__);
new = mem_strcpy(a->object, string);
if (a->argm <= a->argc + 2)
a->argv = xrealloc(a->argv, sizeof(char **) * (a->argm *= 2));
a->argv[a->argc++] = (char *)__UNCONST(new); /* discard const */
a->argv[a->argc] = NULL;
return a;
}
/* \doc Add |length| characters of |string| as the next item in |arg|. A
terminating "NULL" is added to the copied |string|. */
arg_List arg_addn(arg_List arg, const char *string, int length)
{
Arg a = (Arg)arg;
const char *new;
_arg_check(a, __func__);
new = mem_strncpy(a->object, string, length);
if (a->argm <= a->argc + 2)
a->argv = xrealloc(a->argv, sizeof(char **) * (a->argm *= 2));
a->argv[a->argc++] = (char *)__UNCONST(new); /* discard const */
a->argv[a->argc] = NULL;
return a;
}
/* \doc Grow the next item of |arg| with |length| characters of |string|.
Several calls to |arg_grow| should be followed by a single call to
|arg_finish| without any intervening calls to other functions which
modify |arg|. */
void arg_grow(arg_List arg, const char *string, int length)
{
Arg a = (Arg)arg;
_arg_check(a, __func__);
mem_grow(a->object, string, length);
}
/* \doc Finish the growth of the next item in |arg| started by
|arg_grow|. */
arg_List arg_finish(arg_List arg)
{
Arg a = (Arg)arg;
const char *new;
_arg_check(a, __func__);
new = mem_finish(a->object);
if (a->argm <= a->argc + 2)
a->argv = xrealloc(a->argv, sizeof(char **) * (a->argm *= 2));
a->argv[a->argc++] = (char *)__UNCONST(new); /* discard const */
a->argv[a->argc] = NULL;
return a;
}
/* \doc Return |item| from |arg|. |arg| is 0-based. */
const char *arg_get(arg_List arg, int item) /* FIXME! inline? */
{
Arg a = (Arg)arg;
_arg_check(a, __func__);
if (item < 0 || item >= a->argc)
err_internal(__func__,
"Request for item %d in list containing %d items",
item,
a->argc);
return a->argv[ item ];
}
/* \doc Return the number of items in |arg|. */
int arg_count(arg_List arg) /* FIXME! inline? */
{
_arg_check(arg, __func__);
return ((Arg)arg)->argc;
}
/* \doc Get an |argc| and |argv| from |arg|. These are suitable for use in
calls to |exec|. The |argc|+1 item in |argv| is "NULL". */
void arg_get_vector(arg_List arg, int *argc, char ***argv)
{
Arg a = (Arg)arg;
_arg_check(a, __func__);
*argc = a->argc;
*argv = a->argv;
}
/* \doc Break up |string| into arguments, placing them as items in |arg|.
Items within single or double quotes may contain spaces. The quotes are
stripped as in shell argument processing.
Backslash followed by <char> outside quoted or
double-quote subtokens is treated just as <char> */
#include "arggram.c"
#if ACTIONS_COUNT != 3
#error Modify arg_argify function or change arggram.txt
#endif
#if CHARTYPES_COUNT != 6
#error Modify char2char_type function or change arggram.txt
#endif
static int char2char_type (int quoteStyle, char ch)
{
switch (ch){
case '"':
if (ARG_NO_QUOTE & quoteStyle)
return CHARTYPE_OTHER;
else
return CHARTYPE_DQ;
case '\'':
if (ARG_NO_QUOTE & quoteStyle)
return CHARTYPE_OTHER;
else
return CHARTYPE_SQ;
case '\\':
if (ARG_NO_ESCAPE & quoteStyle)
return CHARTYPE_OTHER;
else
return CHARTYPE_BS;
case ' ':
case '\t':
case '\r':
case '\v':
case '\n':
return CHARTYPE_SPACE;
case '\0':
return CHARTYPE_NULL;
default:
return CHARTYPE_OTHER;
}
}
arg_List arg_argify(const char *string, int quoteStyle)
{
Arg a = arg_create();
const char *last = NULL;
const char *pt = string;
char ch;
int ch_type;
int state = 0;
int curr_act = -1;
do {
ch = *pt;
ch_type = char2char_type (quoteStyle, ch);
curr_act = action [state] [ch_type];
/* fprintf (stderr, "%i -- %i(%c) / %i --> %i\n", state, ch_type, ch, curr_act, transition [state] [ch_type]);*/
state = transition [state] [ch_type];
switch (curr_act){
case ACTION_INCLUDE:
if (!last)
last = pt;
break;
case ACTION_SKIP:
if (last){
arg_grow (a, last, pt - last);
}
last = pt + 1;
break;
case ACTION_FINISH:
/* assert (last);*/
if (last){
arg_grow (a, last, pt - last);
arg_finish (a);
last = NULL;
}
break;
default:
abort ();
}
++pt;
}while (ch && state >= 0);
switch (state){
case -1:
/* Fine! Normal exit*/
break;
case -2:
/* Parsing error */
break;
default:
/* Oooops! */
err_internal(__func__, "arg.c:arg_argify is buggy!!!:");
}
return a;
}
|