~ubuntu-branches/ubuntu/saucy/fdclone/saucy-proposed

« back to all changes in this revision

Viewing changes to evalopt.c

  • Committer: Package Import Robot
  • Author(s): Elías Alejandro Año Mendoza
  • Date: 2013-03-31 20:19:19 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20130331201919-vd2f75ssuh1sdctw
Tags: 3.01-1
* New upstream release
* Updated Standard-Version to 3.9.4
* Bump debhelper to 9
* debian/rules enabled hardening build flags

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      evalopt.c
 
3
 *
 
4
 *      option arguments parser
 
5
 */
 
6
 
 
7
#include "headers.h"
 
8
#include "evalopt.h"
 
9
 
 
10
static CONST opt_t *NEAR getoption __P_((int, CONST opt_t *));
 
11
 
 
12
 
 
13
static CONST opt_t *NEAR getoption(c, optlist)
 
14
int c;
 
15
CONST opt_t *optlist;
 
16
{
 
17
        int n;
 
18
 
 
19
        if (!optlist) return(NULL);
 
20
        for (n = 0; optlist[n].opt; n++)
 
21
                if (c == optlist[n].opt) return(&(optlist[n]));
 
22
 
 
23
        return(NULL);
 
24
}
 
25
 
 
26
VOID initopt(optlist)
 
27
CONST opt_t *optlist;
 
28
{
 
29
        int n;
 
30
 
 
31
        if (!optlist) return;
 
32
        for (n = 0; optlist[n].opt; n++)
 
33
                if (optlist[n].var) *(optlist[n].var) = 0;
 
34
}
 
35
 
 
36
VOID optusage(arg0, args, optlist)
 
37
CONST char *arg0, *args;
 
38
CONST opt_t *optlist;
 
39
{
 
40
        int n;
 
41
 
 
42
        fprintf(stderr, "Usage: %s", arg0);
 
43
        n = 0;
 
44
        if (optlist) while (optlist[n].opt) n++;
 
45
        if (n > 0) {
 
46
                fputs(" [-", stderr);
 
47
                for (n = 0; optlist[n].opt; n++) {
 
48
                        fputc(optlist[n].opt, stderr);
 
49
 
 
50
                        if (!(optlist[n].argval)) continue;
 
51
                        fprintf(stderr, " <%s>", optlist[n].argval);
 
52
                        if (optlist[n + 1].opt) fputs(" [-", stderr);
 
53
                }
 
54
                fputc(']', stderr);
 
55
        }
 
56
        fprintf(stderr, " %s\n", args);
 
57
}
 
58
 
 
59
int evalopt(argc, argv, optlist)
 
60
int argc;
 
61
char *CONST *argv;
 
62
CONST opt_t *optlist;
 
63
{
 
64
        CONST char *cp;
 
65
        CONST opt_t *optp;
 
66
        int i, n, val;
 
67
 
 
68
        for (n = 1; n < argc; n++) {
 
69
                if (argv[n][0] != '-' || !argv[n][1]) break;
 
70
                if (argv[n][1] == '=' && !argv[n][2]) {
 
71
                        n++;
 
72
                        break;
 
73
                }
 
74
 
 
75
                for (i = 1; argv[n][i]; i++) {
 
76
                        if (!(optp = getoption(argv[n][i], optlist)))
 
77
                                return(-1);
 
78
 
 
79
                        if (!(optp -> argval)) {
 
80
                                if (optp -> var) *(optp -> var) = optp -> val;
 
81
                        }
 
82
                        else {
 
83
                                cp = &(argv[n][++i]);
 
84
                                if (!*cp) cp = argv[++n];
 
85
                                if (!cp || (val = atoi(cp)) <= 0) return(-1);
 
86
                                if (optp -> var) *(optp -> var) = val;
 
87
                                break;
 
88
                        }
 
89
                }
 
90
        }
 
91
 
 
92
        return(n);
 
93
}