145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Generic Options Support Header File
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Copyright (c) 2001  Stanislav Karchebny <berk@madfire.net>
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met:
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer.
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    notice, this list of conditions and the following disclaimer in the
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    documentation and/or other materials provided with the distribution.
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 3. Neither the name of the author nor the names of other contributors
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    may be used to endorse or promote products derived from this
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *    software without specific prior written permission.
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE.
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <util.h>
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <ctype.h>
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "tasm-options.h"
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef __DEBUG__
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define DEBUG(x) fprintf ## x ;
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#else
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define DEBUG(x)
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* Options Parser */
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgint
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgparse_cmdline(int argc, char **argv, opt_option *options, size_t nopts,
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org              void (*print_error) (const char *fmt, ...))
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int errors = 0, warnings = 0;
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    size_t i;
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    int got_it;
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    DEBUG((stderr, "parse_cmdline: entered\n"));
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org  fail:
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    while (--argc) {
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        argv++;
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (argv[0][0] == '/' && argv[0][1]) {        /* opt */
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            got_it = 0;
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            for (i = 0; i < nopts; i++) {
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                char *cmd = &argv[0][1];
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                size_t len = strlen(options[i].opt);
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                if (yasm__strncasecmp(cmd, options[i].opt, len) == 0) {
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    char *param;
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    param = &argv[0][1+len];
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    if (options[i].takes_param) {
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        if (param[0] == '\0') {
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                            print_error(
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                                _("option `-%c' needs an argument!"),
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                                options[i].opt);
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                            errors++;
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                            goto fail;
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        } else {
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                            argc--;
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                            argv++;
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        }
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    } else
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        param = NULL;
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    if (!options[i].handler(cmd, param, options[i].extra))
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        got_it = 1;
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                    break;
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                }
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            }
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            if (!got_it) {
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                print_error(_("warning: unrecognized option `%s'"),
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                            argv[0]);
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                warnings++;
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            }
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        } else {    /* not an option, then it should be a file or something */
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            if (not_an_option_handler(argv[0]))
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                errors++;
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    DEBUG((stderr, "parse_cmdline: finished\n"));
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return errors;
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orghelp_msg(const char *msg, const char *tail, opt_option *options, size_t nopts)
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    char optbuf[100], optopt[100];
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    size_t i;
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    printf("%s", gettext(msg));
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    for (i = 0; i < nopts; i++) {
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        optbuf[0] = 0;
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        optopt[0] = 0;
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        if (options[i].takes_param) {
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            if (options[i].opt)
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                sprintf(optbuf, "/%s <%s>", options[i].opt,
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        options[i].param_desc ? options[i].
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                        param_desc : _("param"));
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        } else {
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org            if (options[i].opt)
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org                sprintf(optbuf, "/%s", options[i].opt);
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        }
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org        printf("    %-22s  %s\n", optbuf, gettext(options[i].description));
12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    }
12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    printf("%s", gettext(tail));
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
129