~ubuntu-branches/ubuntu/vivid/imaze/vivid

« back to all changes in this revision

Viewing changes to source/argv.c

  • Committer: Bazaar Package Importer
  • Author(s): Hans Freitag
  • Date: 2002-11-28 13:24:12 UTC
  • Revision ID: james.westby@ubuntu.com-20021128132412-lw82xl9oq1j36g8b
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** - - -  iMaze  - - -
 
3
**
 
4
** Copyright (c) 1993-2001 by Hans-Ulrich Kiel & Joerg Czeranski
 
5
** All rights reserved.
 
6
**
 
7
** Redistribution and use in source and binary forms, with or without
 
8
** modification, are permitted provided that the following conditions are
 
9
** met:
 
10
**
 
11
** 1. Redistributions of source code must retain the above copyright
 
12
**    notice, this list of conditions and the following disclaimer.
 
13
** 2. Redistributions in binary form must reproduce the above copyright
 
14
**    notice, this list of conditions and the following disclaimer in the
 
15
**    documentation and/or other materials provided with the distribution.
 
16
** 3. The name of the authors may not be used to endorse or promote
 
17
**    products derived from this software without specific prior written
 
18
**    permission.
 
19
** 4. The name ``iMaze'' may not be used for products derived from this
 
20
**    software unless a prefix or a suffix is added to the name.
 
21
**
 
22
** THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 
23
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
24
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
25
** DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
 
26
** INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
27
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
28
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
29
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
30
** STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 
31
** IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
32
** POSSIBILITY OF SUCH DAMAGE.
 
33
**
 
34
**
 
35
** File: argv.c
 
36
**
 
37
** Comment:
 
38
**  Parses command line parameters
 
39
*/
 
40
 
 
41
#include <stdio.h>
 
42
#include <stdlib.h>
 
43
#include <string.h>
 
44
 
 
45
#include "argv.h"
 
46
#include "speicher.h"
 
47
 
 
48
static char sccsid[] = "@(#)argv.c      3.9 12/3/01";
 
49
 
 
50
 
 
51
#define OPT_LENGTH 20
 
52
 
 
53
 
 
54
static int want_help = 0;
 
55
static struct arg_option all_opts[] =
 
56
{
 
57
        { Arg_Simple, "h", &want_help, "help" },
 
58
        { Arg_Include, NULL, argv_opts },
 
59
        { Arg_End }
 
60
};
 
61
 
 
62
 
 
63
char *program_name = "(unknown)";
 
64
 
 
65
 
 
66
/* prints enough spaces, help (if not NULL) and a newline */
 
67
static void print_help(int opt_length, char *help)
 
68
{
 
69
        char help_format[20];
 
70
 
 
71
        if (help == NULL)
 
72
                fprintf(stderr, "\n");
 
73
        else if (opt_length < OPT_LENGTH)
 
74
        {
 
75
                sprintf(help_format, "%%%ds%%s\n", OPT_LENGTH - opt_length);
 
76
                fprintf(stderr, help_format, " ", help);
 
77
        }
 
78
        else
 
79
                fprintf(stderr, " %s\n", help);
 
80
}
 
81
 
 
82
 
 
83
static void print_opts(struct arg_option *opts)
 
84
{
 
85
        char *param_name;
 
86
 
 
87
        for (;;)
 
88
        {
 
89
                switch (opts->type)
 
90
                {
 
91
                case Arg_End:
 
92
                        return;
 
93
 
 
94
                case Arg_Include:
 
95
                        print_opts(opts->data);
 
96
                        break;
 
97
 
 
98
                case Arg_Simple:
 
99
                        fprintf(stderr, "\t-%s", opts->name);
 
100
                        print_help(2 + strlen(opts->name), opts->help);
 
101
                        break;
 
102
 
 
103
                case Arg_String:
 
104
                case Arg_Callback:
 
105
                        param_name =
 
106
                                opts->param_name == NULL ? "(parameter)" :
 
107
                                        opts->param_name;
 
108
                        fprintf(stderr, "\t-%s %s", opts->name, param_name);
 
109
                        print_help(2 + strlen(opts->name) + 1
 
110
                                + strlen(param_name), opts->help);
 
111
                        break;
 
112
 
 
113
                default:
 
114
                        abort();
 
115
                }
 
116
 
 
117
                opts++;
 
118
        }
 
119
}
 
120
 
 
121
 
 
122
static struct arg_option *get_arg_end(void)
 
123
{
 
124
        struct arg_option *opt;
 
125
 
 
126
        for (opt = argv_opts; opt->type != Arg_End; opt++);
 
127
        return opt;
 
128
}
 
129
 
 
130
 
 
131
static void usage(void)
 
132
{
 
133
        struct arg_option *opt;
 
134
 
 
135
        opt = get_arg_end();
 
136
 
 
137
        fprintf(stderr, "usage: %s [options]", program_name);
 
138
        if (opt->data != NULL)
 
139
                fprintf(stderr, " %s",
 
140
                        opt->param_name == NULL ? "(parameter)" :
 
141
                                opt->param_name);
 
142
        fprintf(stderr, "\n\tvalid options:\n");
 
143
        print_opts(all_opts);
 
144
        exit(1);
 
145
}
 
146
 
 
147
 
 
148
static struct arg_option *match_option(struct arg_option *opts, char *name)
 
149
{
 
150
        for (;;)
 
151
        {
 
152
                switch (opts->type)
 
153
                {
 
154
                case Arg_End:
 
155
                        return NULL;
 
156
 
 
157
                case Arg_Include:
 
158
                        {
 
159
                                struct arg_option *result;
 
160
 
 
161
                                result = match_option(opts->data, name);
 
162
                                if (result != NULL)
 
163
                                        return result;
 
164
                        }
 
165
                        break;
 
166
 
 
167
                case Arg_Simple:
 
168
                case Arg_String:
 
169
                case Arg_Callback:
 
170
                        if (!strcmp(opts->name, name))
 
171
                                return opts;
 
172
                        break;
 
173
 
 
174
                default:
 
175
                        abort();
 
176
                }
 
177
 
 
178
                opts++;
 
179
        }
 
180
}
 
181
 
 
182
 
 
183
/* up to here local part                       */
 
184
/***********************************************/
 
185
/* from here on global part                    */
 
186
 
 
187
 
 
188
void process_args(int *argc, char **argv)
 
189
{
 
190
        int arg_i, ofs;
 
191
        char *str;
 
192
        struct arg_option *opt;
 
193
 
 
194
        if (argv[0] != NULL)
 
195
        {
 
196
                program_name = strrchr(argv[0], '/');
 
197
                if (program_name == NULL || program_name[1] == 0)
 
198
                        program_name = argv[0];
 
199
                else
 
200
                        program_name++;
 
201
        }
 
202
 
 
203
        arg_i = 1;
 
204
        ofs = 0;
 
205
 
 
206
        while (arg_i < *argc)
 
207
        {
 
208
                char name[2];
 
209
 
 
210
                if (want_help)
 
211
                        usage();
 
212
 
 
213
                if (ofs == 0)
 
214
                {
 
215
                        if (argv[arg_i][0] != '-')
 
216
                                break;
 
217
 
 
218
                        if (!strcmp(argv[arg_i], "-") ||
 
219
                                !strcmp(argv[arg_i], "--"))
 
220
                        {
 
221
                                arg_i++;
 
222
                                break;
 
223
                        }
 
224
 
 
225
                        ofs = 1;
 
226
                }
 
227
 
 
228
                if (argv[arg_i][ofs] == 0)
 
229
                {
 
230
                        arg_i++;
 
231
                        ofs = 0;
 
232
                        continue;
 
233
                }
 
234
 
 
235
                /* only single char options for now */
 
236
                name[0] = argv[arg_i][ofs];
 
237
                name[1] = 0;
 
238
                opt = match_option(all_opts, name);
 
239
                if (opt == NULL)
 
240
                {
 
241
                        fprintf(stderr, "%s: unknown option -%s\n\n",
 
242
                                program_name, name);
 
243
                        usage();
 
244
                }
 
245
 
 
246
                switch (opt->type)
 
247
                {
 
248
                case Arg_Simple:
 
249
                        ofs++;
 
250
                        *(int *)opt->data = 1;
 
251
                        break; /* success */
 
252
 
 
253
                case Arg_String:
 
254
                case Arg_Callback:
 
255
                        if (argv[arg_i][ofs + 1] == 0 && arg_i + 1 >= *argc)
 
256
                        {
 
257
                                fprintf(stderr,
 
258
                                        "%s: parameter missing for -%s\n\n",
 
259
                                        program_name, opt->name);
 
260
                                usage();
 
261
                        }
 
262
 
 
263
                        ofs++;
 
264
                        if (argv[arg_i][ofs] == 0)
 
265
                        {
 
266
                                arg_i++;
 
267
                                ofs = 0;
 
268
                        }
 
269
 
 
270
                        str = argv[arg_i] + ofs;
 
271
                        arg_i++;
 
272
                        ofs = 0;
 
273
 
 
274
                        if (opt->type == Arg_String)
 
275
                                *(char **)opt->data = str;
 
276
                        else /* Arg_Callback */
 
277
                        {
 
278
                                int (*f)();
 
279
 
 
280
                                f = (int (*)())opt->data;
 
281
                                if (f(str))
 
282
                                {
 
283
                                        fprintf(stderr,
 
284
                                                "%s: bad parameter for -%s: %s\n\n",
 
285
                                                program_name, opt->name, str);
 
286
                                        usage();
 
287
                                }
 
288
                        }
 
289
 
 
290
                        break; /* success */
 
291
 
 
292
                default:
 
293
                        abort();
 
294
                }
 
295
        }
 
296
 
 
297
        /* there can be no partial string left */
 
298
        if (ofs != 0)
 
299
                abort();
 
300
 
 
301
        opt = get_arg_end();
 
302
        if (opt->data == NULL)
 
303
        {
 
304
                if (arg_i < *argc)
 
305
                {
 
306
                        fprintf(stderr, "%s: too many parameters\n\n",
 
307
                                program_name);
 
308
                        usage();
 
309
                }
 
310
        }
 
311
        else
 
312
        {
 
313
                if (arg_i >= *argc)
 
314
                {
 
315
                        fprintf(stderr, "%s: parameter missing\n\n",
 
316
                                program_name);
 
317
                        usage();
 
318
                }
 
319
                else if (arg_i + 1 < *argc)
 
320
                {
 
321
                        fprintf(stderr, "%s: too many parameters\n\n",
 
322
                                program_name);
 
323
                        usage();
 
324
                }
 
325
 
 
326
                *(char **)opt->data = argv[arg_i++];
 
327
        }
 
328
 
 
329
        *argc = *argc < 1 ? 0 : 1;
 
330
        argv[*argc] = NULL;
 
331
}