1 /*****************************************************************************/
5 /* Helper functions for command line parsing */
9 /* (C) 2000-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 /* Program name - is set after call to InitCmdLine */
58 /* The program argument vector */
60 unsigned ArgCount = 0;
62 /* Struct to pass the command line */
64 char** Vec; /* The argument vector */
65 unsigned Count; /* Actual number of arguments */
66 unsigned Size; /* Number of argument allocated */
71 /*****************************************************************************/
72 /* Helper functions */
73 /*****************************************************************************/
77 static void NewCmdLine (CmdLine* L)
78 /* Initialize a CmdLine struct */
80 /* Initialize the struct */
83 L->Vec = xmalloc (L->Size * sizeof (L->Vec[0]));
88 static void AddArg (CmdLine* L, char* Arg)
89 /* Add one argument to the list */
91 if (L->Size <= L->Count) {
92 /* No space left, reallocate */
93 unsigned NewSize = L->Size * 2;
94 char** NewVec = xmalloc (NewSize * sizeof (L->Vec[0]));
95 memcpy (NewVec, L->Vec, L->Count * sizeof (L->Vec[0]));
101 /* We have space left, add a copy of the argument */
102 L->Vec[L->Count++] = Arg;
107 static void ExpandFile (CmdLine* L, const char* Name)
108 /* Add the contents of a file to the command line. Each line is a separate
109 * argument with leading and trailing whitespace removed.
114 /* Try to open the file for reading */
115 FILE* F = fopen (Name, "r");
117 AbEnd ("Cannot open \"%s\": %s", Name, strerror (errno));
120 /* File is open, read all lines */
121 while (fgets (Buf, sizeof (Buf), F) != 0) {
123 /* Get a pointer to the buffer */
126 /* Skip trailing whitespace (this will also kill the newline that is
127 * appended by fgets().
129 unsigned Len = strlen (Buf);
130 while (Len > 0 && IsSpace (Buf [Len-1])) {
135 /* Skip leading spaces */
136 while (IsSpace (*B)) {
140 /* Skip empty lines to work around problems with some editors */
145 /* Add anything not empty to the command line */
146 AddArg (L, xstrdup (B));
150 /* Close the file, ignore errors here since we had the file open for
158 /*****************************************************************************/
160 /*****************************************************************************/
164 void InitCmdLine (int* aArgCount, char** aArgVec[], const char* aProgName)
165 /* Initialize command line parsing. aArgVec is the argument array terminated by
166 * a NULL pointer (as usual), ArgCount is the number of valid arguments in the
167 * array. Both arguments are remembered in static storage.
173 /* Get the program name from argv[0] but strip a path */
174 if (*(aArgVec)[0] == 0) {
175 /* Use the default name given */
176 ProgName = aProgName;
179 ProgName = FindName ((*aArgVec)[0]);
180 if (ProgName[0] == '\0') {
181 /* Use the default */
182 ProgName = aProgName;
186 /* Make a CmdLine struct */
189 /* Walk over the parameters and add them to the CmdLine struct. Add a
190 * special handling for arguments preceeded by the '@' sign - these are
191 * actually files containing arguments.
193 for (I = 0; I < *aArgCount; ++I) {
195 /* Get the next argument */
196 char* Arg = (*aArgVec)[I];
198 /* Is this a file argument? */
199 if (Arg && Arg[0] == '@') {
201 /* Expand the file */
202 ExpandFile (&L, Arg+1);
206 /* No file, just add a copy */
212 /* Store the new argument list in a safe place... */
216 /* ...and pass back the changed data also */
217 *aArgCount = L.Count;
223 void UnknownOption (const char* Opt)
224 /* Print an error about an unknown option and die. */
226 AbEnd ("Unknown option: %s", Opt);
231 void NeedArg (const char* Opt)
232 /* Print an error about a missing option argument and exit. */
234 AbEnd ("Option requires an argument: %s", Opt);
239 void InvArg (const char* Opt, const char* Arg)
240 /* Print an error about an invalid option argument and exit. */
242 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
247 void InvDef (const char* Def)
248 /* Print an error about an invalid definition and die */
250 AbEnd ("Invalid definition: `%s'", Def);
255 const char* GetArg (unsigned* ArgNum, unsigned Len)
256 /* Get an argument for a short option. The argument may be appended to the
257 * option itself or may be separate. Len is the length of the option string.
260 const char* Arg = ArgVec[*ArgNum];
261 if (Arg[Len] != '\0') {
262 /* Argument appended */
265 /* Separate argument */
266 Arg = ArgVec[*ArgNum + 1];
268 /* End of arguments */
269 NeedArg (ArgVec[*ArgNum]);
278 void LongOption (unsigned* ArgNum, const LongOpt* OptTab, unsigned OptCount)
279 /* Handle a long command line option */
281 /* Get the option and the argument (which may be zero) */
282 const char* Opt = ArgVec[*ArgNum];
284 /* Search the table for a match */
286 if (strcmp (Opt, OptTab->Option) == 0) {
287 /* Found, call the function */
288 if (OptTab->ArgCount > 0) {
289 /* We need an argument, check if we have one */
290 const char* Arg = ArgVec[++(*ArgNum)];
294 OptTab->Func (Opt, Arg);
296 OptTab->Func (Opt, 0);
302 /* Next table entry */