3 * Copyright (c) 1999-2001, Darren Hiebert
5 * This source code is released for free distribution under the terms of the
6 * GNU General Public License.
8 * This module contains functions for reading command line arguments.
14 #include "general.h" /* must always come first */
24 * FUNCTION DEFINITIONS
27 static char *nextStringArg (const char** const next
)
32 Assert (*next
!= NULL
);
33 for (start
= *next
; isspace ((int) *start
) ; ++start
)
42 for (end
= start
; *end
!= '\0' && ! isspace ((int) *end
) ; ++end
)
46 result
= xMalloc (length
+ 1, char);
47 strncpy (result
, start
, length
);
48 result
[length
] = '\0';
54 static char* nextStringLine (const char** const next
)
60 Assert (*next
!= NULL
);
61 for (end
= *next
; *end
!= '\n' && *end
!= '\0' ; ++end
)
66 result
= xMalloc (length
+ 1, char);
67 strncpy (result
, *next
, length
);
68 result
[length
] = '\0';
76 static char* nextString (const Arguments
* const current
, const char** const next
)
79 if (current
->lineMode
)
80 result
= nextStringLine (next
);
82 result
= nextStringArg (next
);
86 static char* nextFileArg (FILE* const fp
)
92 vString
* vs
= vStringNew ();
104 } while (c
!= EOF
&& ! isspace (c
));
105 vStringTerminate (vs
);
106 Assert (vStringLength (vs
) > 0);
107 result
= xMalloc (vStringLength (vs
) + 1, char);
108 strcpy (result
, vStringValue (vs
));
115 static char* nextFileLine (FILE* const fp
)
121 vString
* vs
= vStringNew ();
125 while (c
!= EOF
&& c
!= '\n')
130 vStringTerminate (vs
);
131 if (vStringLength (vs
) > 0)
133 result
= xMalloc (vStringLength (vs
) + 1, char);
134 strcpy (result
, vStringValue (vs
));
141 static char* nextFileString (const Arguments
* const current
, FILE* const fp
)
144 if (current
->lineMode
)
145 result
= nextFileLine (fp
);
147 result
= nextFileArg (fp
);
151 extern Arguments
* argNewFromString (const char* const string
)
153 Arguments
* result
= xMalloc (1, Arguments
);
154 memset (result
, 0, sizeof (Arguments
));
155 result
->type
= ARG_STRING
;
156 result
->u
.stringArgs
.string
= string
;
157 result
->u
.stringArgs
.item
= string
;
158 result
->u
.stringArgs
.next
= string
;
159 result
->item
= nextString (result
, &result
->u
.stringArgs
.next
);
163 extern Arguments
* argNewFromArgv (char* const* const argv
)
165 Arguments
* result
= xMalloc (1, Arguments
);
166 memset (result
, 0, sizeof (Arguments
));
167 result
->type
= ARG_ARGV
;
168 result
->u
.argvArgs
.argv
= argv
;
169 result
->u
.argvArgs
.item
= result
->u
.argvArgs
.argv
;
170 result
->item
= *result
->u
.argvArgs
.item
;
174 extern Arguments
* argNewFromFile (FILE* const fp
)
176 Arguments
* result
= xMalloc (1, Arguments
);
177 memset (result
, 0, sizeof (Arguments
));
178 result
->type
= ARG_FILE
;
179 result
->u
.fileArgs
.fp
= fp
;
180 result
->item
= nextFileString (result
, result
->u
.fileArgs
.fp
);
184 extern Arguments
* argNewFromLineFile (FILE* const fp
)
186 Arguments
* result
= xMalloc (1, Arguments
);
187 memset (result
, 0, sizeof (Arguments
));
188 result
->type
= ARG_FILE
;
189 result
->lineMode
= TRUE
;
190 result
->u
.fileArgs
.fp
= fp
;
191 result
->item
= nextFileString (result
, result
->u
.fileArgs
.fp
);
195 extern char *argItem (const Arguments
* const current
)
197 Assert (current
!= NULL
);
198 Assert (! argOff (current
));
199 return current
->item
;
202 extern boolean
argOff (const Arguments
* const current
)
204 Assert (current
!= NULL
);
205 return (boolean
) (current
->item
== NULL
);
208 extern void argSetWordMode (Arguments
* const current
)
210 Assert (current
!= NULL
);
211 current
->lineMode
= FALSE
;
214 extern void argSetLineMode (Arguments
* const current
)
216 Assert (current
!= NULL
);
217 current
->lineMode
= TRUE
;
220 extern void argForth (Arguments
* const current
)
222 Assert (current
!= NULL
);
223 Assert (! argOff (current
));
224 switch (current
->type
)
227 if (current
->item
!= NULL
)
228 eFree (current
->item
);
229 current
->u
.stringArgs
.item
= current
->u
.stringArgs
.next
;
230 current
->item
= nextString (current
, ¤t
->u
.stringArgs
.next
);
233 ++current
->u
.argvArgs
.item
;
234 current
->item
= *current
->u
.argvArgs
.item
;
237 if (current
->item
!= NULL
)
238 eFree (current
->item
);
239 current
->item
= nextFileString (current
, current
->u
.fileArgs
.fp
);
242 Assert ("Invalid argument type" == NULL
);
247 extern void argDelete (Arguments
* const current
)
249 Assert (current
!= NULL
);
250 if (current
->type
== ARG_STRING
&& current
->item
!= NULL
)
251 eFree (current
->item
);
252 memset (current
, 0, sizeof (Arguments
));
256 /* vi:set tabstop=8 shiftwidth=4: */