1/*
2 * Generic Options Support Header File
3 *
4 * Copyright (c) 2001  Stanislav Karchebny <berk@madfire.net>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of other contributors
15 *    may be used to endorse or promote products derived from this
16 *    software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30#include <util.h>
31#include <ctype.h>
32
33#include "tasm-options.h"
34
35
36#ifdef __DEBUG__
37#define DEBUG(x) fprintf ## x ;
38#else
39#define DEBUG(x)
40#endif
41
42
43/* Options Parser */
44int
45parse_cmdline(int argc, char **argv, opt_option *options, size_t nopts,
46              void (*print_error) (const char *fmt, ...))
47{
48    int errors = 0, warnings = 0;
49    size_t i;
50    int got_it;
51
52    DEBUG((stderr, "parse_cmdline: entered\n"));
53
54  fail:
55    while (--argc) {
56        argv++;
57
58        if (argv[0][0] == '/' && argv[0][1]) {        /* opt */
59            got_it = 0;
60            for (i = 0; i < nopts; i++) {
61                char *cmd = &argv[0][1];
62                size_t len = strlen(options[i].opt);
63                if (yasm__strncasecmp(cmd, options[i].opt, len) == 0) {
64                    char *param;
65
66                    param = &argv[0][1+len];
67                    if (options[i].takes_param) {
68                        if (param[0] == '\0') {
69                            print_error(
70                                _("option `-%c' needs an argument!"),
71                                options[i].opt);
72                            errors++;
73                            goto fail;
74                        } else {
75                            argc--;
76                            argv++;
77                        }
78                    } else
79                        param = NULL;
80
81                    if (!options[i].handler(cmd, param, options[i].extra))
82                        got_it = 1;
83                    break;
84                }
85            }
86            if (!got_it) {
87                print_error(_("warning: unrecognized option `%s'"),
88                            argv[0]);
89                warnings++;
90            }
91        } else {    /* not an option, then it should be a file or something */
92
93            if (not_an_option_handler(argv[0]))
94                errors++;
95        }
96    }
97
98    DEBUG((stderr, "parse_cmdline: finished\n"));
99    return errors;
100}
101
102void
103help_msg(const char *msg, const char *tail, opt_option *options, size_t nopts)
104{
105    char optbuf[100], optopt[100];
106    size_t i;
107
108    printf("%s", gettext(msg));
109
110    for (i = 0; i < nopts; i++) {
111        optbuf[0] = 0;
112        optopt[0] = 0;
113
114        if (options[i].takes_param) {
115            if (options[i].opt)
116                sprintf(optbuf, "/%s <%s>", options[i].opt,
117                        options[i].param_desc ? options[i].
118                        param_desc : _("param"));
119        } else {
120            if (options[i].opt)
121                sprintf(optbuf, "/%s", options[i].opt);
122        }
123
124        printf("    %-22s  %s\n", optbuf, gettext(options[i].description));
125    }
126
127    printf("%s", gettext(tail));
128}
129