Fix building with makefile.win32 from Windows command prompt, not MSYS
[geany-mirror.git] / tagmanager / args.c
blobcf9b1620c3641dfe9c5ae9e9a78e1d8de477abd5
1 /*
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.
9 */
12 * INCLUDE FILES
14 #include "general.h" /* must always come first */
16 #include <stdio.h>
17 #include <string.h>
18 #include <ctype.h>
20 #include "args.h"
21 #include "main.h"
24 * FUNCTION DEFINITIONS
27 static char *nextStringArg (const char** const next)
29 char* result = NULL;
30 const char* start;
32 Assert (*next != NULL);
33 for (start = *next ; isspace ((int) *start) ; ++start)
35 if (*start == '\0')
36 *next = start;
37 else
39 size_t length;
40 const char* end;
42 for (end = start ; *end != '\0' && ! isspace ((int) *end) ; ++end)
44 length = end - start;
45 Assert (length > 0);
46 result = xMalloc (length + 1, char);
47 strncpy (result, start, length);
48 result [length] = '\0';
49 *next = end;
51 return result;
54 static char* nextStringLine (const char** const next)
56 char* result = NULL;
57 size_t length;
58 const char* end;
60 Assert (*next != NULL);
61 for (end = *next ; *end != '\n' && *end != '\0' ; ++end)
63 length = end - *next;
64 if (length > 0)
66 result = xMalloc (length + 1, char);
67 strncpy (result, *next, length);
68 result [length] = '\0';
70 if (*end == '\n')
71 ++end;
72 *next = end;
73 return result;
76 static char* nextString (const Arguments* const current, const char** const next)
78 char* result;
79 if (current->lineMode)
80 result = nextStringLine (next);
81 else
82 result = nextStringArg (next);
83 return result;
86 static char* nextFileArg (FILE* const fp)
88 char* result = NULL;
89 Assert (fp != NULL);
90 if (! feof (fp))
92 vString* vs = vStringNew ();
93 int c;
95 c = fgetc (fp);
96 while (isspace (c));
98 if (c != EOF)
102 vStringPut (vs, c);
103 c = fgetc (fp);
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));
110 vStringDelete (vs);
112 return result;
115 static char* nextFileLine (FILE* const fp)
117 char* result = NULL;
118 Assert (fp != NULL);
119 if (! feof (fp))
121 vString* vs = vStringNew ();
122 int c;
124 c = fgetc (fp);
125 while (c != EOF && c != '\n')
127 vStringPut (vs, c);
128 c = fgetc (fp);
130 vStringTerminate (vs);
131 if (vStringLength (vs) > 0)
133 result = xMalloc (vStringLength (vs) + 1, char);
134 strcpy (result, vStringValue (vs));
136 vStringDelete (vs);
138 return result;
141 static char* nextFileString (const Arguments* const current, FILE* const fp)
143 char* result;
144 if (current->lineMode)
145 result = nextFileLine (fp);
146 else
147 result = nextFileArg (fp);
148 return result;
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);
160 return result;
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;
171 return result;
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);
181 return result;
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);
192 return result;
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)
226 case ARG_STRING:
227 if (current->item != NULL)
228 eFree (current->item);
229 current->u.stringArgs.item = current->u.stringArgs.next;
230 current->item = nextString (current, &current->u.stringArgs.next);
231 break;
232 case ARG_ARGV:
233 ++current->u.argvArgs.item;
234 current->item = *current->u.argvArgs.item;
235 break;
236 case ARG_FILE:
237 if (current->item != NULL)
238 eFree (current->item);
239 current->item = nextFileString (current, current->u.fileArgs.fp);
240 break;
241 default:
242 Assert ("Invalid argument type" == NULL);
243 break;
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));
253 eFree (current);
256 /* vi:set tabstop=8 shiftwidth=4: */