]> git.sur5r.net Git - cc65/blob - src/common/cmdline.c
Fix macro output in the listing
[cc65] / src / common / cmdline.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 cmdline.c                                 */
4 /*                                                                           */
5 /*                 Helper functions for command line parsing                 */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 #include "abend.h"
39 #include "cmdline.h"
40
41
42
43 /*****************************************************************************/
44 /*                                   Data                                    */
45 /*****************************************************************************/
46
47
48
49 /* Program name - is set after call to InitCmdLine */
50 const char* ProgName;
51
52 /* The program argument vector */
53 static char** ArgVec     = 0;
54 static unsigned ArgCount = 0;
55
56
57
58 /*****************************************************************************/
59 /*                                   Code                                    */
60 /*****************************************************************************/
61
62
63
64 void InitCmdLine (unsigned aArgCount, char* aArgVec[], const char* aProgName)
65 /* Initialize command line parsing. aArgVec is the argument array terminated by
66  * a NULL pointer (as usual), ArgCount is the number of valid arguments in the
67  * array. Both arguments are remembered in static storage.
68  */
69 {
70     /* Remember the argument vector */
71     ArgCount = aArgCount;
72     ArgVec   = aArgVec;
73
74     /* Get the program name from argv[0] but strip a path */
75     if (ArgVec[0] == 0) {
76         /* Use the default name given */
77         ProgName = aProgName;
78     } else {
79         /* Strip a path */
80         ProgName = strchr (ArgVec[0], '\0');
81         while (ProgName > ArgVec[0]) {
82             --ProgName;
83             if (*ProgName == '/' || *ProgName == '\\') {
84                 ++ProgName;
85                 break;
86             }
87         }
88         if (ProgName[0] == '\0') {
89             /* Use the default */
90             ProgName = aProgName;
91         }
92     }
93 }
94
95
96
97 void UnknownOption (const char* Opt)
98 /* Print an error about an unknown option. */
99 {
100     AbEnd ("Unknown option: %s\n", Opt);
101 }
102
103
104
105 void NeedArg (const char* Opt)
106 /* Print an error about a missing option argument and exit. */
107 {
108     AbEnd ("Option requires an argument: %s\n", Opt);
109 }
110
111
112
113 void InvDef (const char* Def)
114 /* Print an error about an invalid definition and die */
115 {
116     AbEnd ("Invalid definition: `%s'\n", Def);
117 }
118
119
120
121 const char* GetArg (int* ArgNum, unsigned Len)
122 /* Get an argument for a short option. The argument may be appended to the
123  * option itself or may be separate. Len is the length of the option string.
124  */
125 {
126     const char* Arg = ArgVec[*ArgNum];
127     if (Arg[Len] != '\0') {
128         /* Argument appended */
129         return Arg + Len;
130     } else {
131         /* Separate argument */
132         Arg = ArgVec[*ArgNum + 1];
133         if (Arg == 0) {
134             /* End of arguments */
135             NeedArg (ArgVec[*ArgNum]);
136         }
137         ++(*ArgNum);
138         return Arg;
139     }
140 }
141
142
143
144 void LongOption (int* ArgNum, const LongOpt* OptTab, unsigned OptCount)
145 /* Handle a long command line option */
146 {
147     /* Get the option and the argument (which may be zero) */
148     const char* Opt = ArgVec[*ArgNum];
149
150     /* Search the table for a match */
151     while (OptCount) {
152         if (strcmp (Opt, OptTab->Option) == 0) {
153             /* Found, call the function */
154             if (OptTab->ArgCount > 0) {
155                 OptTab->Func (Opt, ArgVec[++(*ArgNum)]);
156             } else {
157                 OptTab->Func (Opt, 0);
158             }
159             /* Done */
160             return;
161         }
162
163         /* Next table entry */
164         --OptCount;
165         ++OptTab;
166     }
167
168     /* Invalid option */
169     UnknownOption (Opt);
170 }
171
172
173