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
|
/* arg.c -- Argument list support
* Created: Sun Jan 7 13:39:29 1996 by faith@acm.org
* Revised: Fri Aug 15 07:55:05 1997 by faith@acm.org
* Copyright 1996, 1997 Rickard E. Faith (faith@acm.org)
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: arg.c,v 1.9 1997/08/15 12:10:53 faith Exp $
*
* \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
int 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\n" );
#if MAA_MAGIC
if (a->magic != ARG_MAGIC)
err_internal( function,
"Magic match failed: 0x%08x (should be 0x%08x)\n",
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, __FUNCTION__ );
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, __FUNCTION__ );
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 *)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, __FUNCTION__ );
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 *)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, __FUNCTION__ );
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, __FUNCTION__ );
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 *)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, __FUNCTION__ );
if (item < 0 || item >= a->argc)
err_internal( __FUNCTION__,
"Request for item %d in list containing %d items\n",
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, __FUNCTION__ );
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, __FUNCTION__ );
*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. In this version, backslash is
"not" a special quoting character. */
arg_List arg_argify( const char *string, int quoteStyle )
{
Arg a = arg_create();
const char *last;
const char *pt = string;
int len;
int quote = 0;
if (!string)
err_internal( __FUNCTION__, "Cannot argify NULL pointer\n" );
while (*pt && (*pt == ' '
|| *pt == '\t'
|| *pt == '\n'
|| *pt == '\r'
|| *pt == '\v'
|| *pt == '\f'))
++pt;
for (last = pt, len = 0; *pt; ++pt, ++len) {
switch (*pt) {
case ' ':
case '\t':
case '\n':
case '\r':
case '\v':
case '\f':
if (!quote) {
while (pt[1] == ' '
|| pt[1] == '\t'
|| pt[1] == '\n'
|| pt[1] == '\r'
|| pt[1] == '\v'
|| pt[1] == '\f')
++pt;
arg_grow( a, last, len );
arg_finish( a );
last = pt + 1;
len = -1;
}
break;
case '"':
case '\'':
if (!(quoteStyle & ARG_NO_QUOTE)) {
if (quote == *pt) {
arg_grow( a, last, len );
quote = 0;
/* last = 0; */
len = -1;
} else if (!quote) {
arg_grow( a, last, len );
quote = *pt;
last = pt + 1;
len = -1;
}
}
break;
case '\\':
if (!(quoteStyle & ARG_NO_ESCAPE)) {
if (pt[1]) {
arg_grow( a, last, len );
arg_grow( a, ++pt, 1 );
last = pt + 1;
len = -1;
}
}
break;
}
}
if (*last
&& *last != ' '
&& *last != '\t'
&& *last != '\n'
&& *last != '\r'
&& *last != '\v'
&& *last != '\f') {
arg_grow( a, last, len );
arg_finish( a );
}
return a;
}
|