Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
313 dpurdie 1
/* -*- mode: c; tabs: 4; -*- ************************************************
2
* Module name   : longopt.c
3
* Module type   : CMDFILE source file
4
* Environment(s): n/a
5
*
6
* Version   Who     Date        Description
7
            APY     12/99       Created
8
                    05/10/04    -e/E exclusive check/error
9
*
10
* $Source: /cvsroot/device/DEVL/UTILS/CMDFILE/longopt.c,v $
11
* $Revision: 1.7 $ $Date: 2004/11/05 07:51:39 $ $State: Exp $
12
* $Author: ayoung $ $Locker:  $
13
*.........................................................................*/
14
 
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include <ctype.h>
18
#include "getopt.h"
19
#include "longopt.h"
20
#include "cmdfile.h"
21
 
22
#define VALID_OPTIONS           "nNokreEvtwWmd"
23
#define HAS_ARGUMENT            "krmdW"
24
#define OPTIONAL_ARGUMENT       "k"
25
 
26
static struct option const long_options[] =
27
{
28
    { "help",       no_argument, 0,     'h' },
29
    { "version",    no_argument, 0,     'v' },
30
    { 0, 0, 0, 0 }
31
};
32
 
33
 
34
/* Process long options --help and --version, but only if argc == 2.
35
   Be careful not to gobble up `--'.  */
36
 
37
void
38
parse_long_options( argc, argv, command_name, version_string, usage )
39
    int argc;
40
    char **argv;
41
    const char *command_name;
42
    const char *version_string;
43
    void (*usage)();
44
{
45
    int c;
46
    int saved_opterr;
47
    int saved_optind;
48
 
49
    saved_opterr = opterr;
50
    saved_optind = optind;
51
 
52
/* Don't print an error message for unrecognized options.  */
53
    opterr = 0;
54
 
55
    if (argc == 2 &&
56
            (c = getoptl(argc, argv, "+", long_options, 0)) != EOF)
57
    {
58
        switch (c)
59
        {
60
        case 'h':
61
            (*usage)(0);
62
 
63
        case 'v':
64
            printf("%s - %s\n", command_name, version_string);
65
            exit(0);
66
 
67
        default:
68
            /* Don't process any other long-named options.  */
69
            break;
70
        }
71
    }
72
 
73
/* Restore previous value */
74
    opterr = saved_opterr;
75
 
76
/* Restore optind in case it has advanced past a leading `--'.  */
77
    optind = saved_optind;
78
}
79
 
80
 
81
int
82
parse_short_argument( const register char *arg )
83
{
84
    int argument = FALSE;
85
    register int i;
86
 
87
    /* If it appears that we are handling options, then make sure that
88
    all of the options specified are actually valid.  Otherwise, the
89
    string should just be echoed. */
90
 
91
    for (i = 0; arg[i]; i++)
92
    {
93
        if (arg[i] == 'o')
94
            break;                              /* must be last */
95
 
96
        if (argument)
97
            argument = FALSE;                   /* argument to previous */
98
 
99
        else
100
        {
101
            if (strrchr (VALID_OPTIONS, arg[i]) == 0)
102
            {                                   /* not within valid list */
103
                if (vflg > 1)
104
                    printf( "invalid character '%c' in option list  -- ignored\n", arg[i] );
105
                return (-1);
106
            }
107
 
108
            if (strrchr (HAS_ARGUMENT, arg[i]))
109
            {                                   /* argument expected */
110
                if (arg[i+1] == '\0')
111
                {
112
                    if (!strrchr (OPTIONAL_ARGUMENT, arg[i]))
113
                    {
114
                        if (vflg > 1)
115
                            printf( "option '%c' missing argument  -- ignored\n", arg[i] );
116
                        return (-1);
117
                    }
118
                }
119
                argument = TRUE;
120
            }
121
        }
122
    }
123
 
124
    /* All of the options are valid options ... handle them. */
125
    while (*arg)
126
    {
127
        if (*arg == 'o')
128
        {
129
            arg++;                              /* skip 'o */
130
 
131
            if (strlen(arg) == 0)
132
                fatalerr("Missing -o");
133
 
134
            if (output != stdout)               /* shouldn't happen */
135
                fatalerr("Second -o encountered \"%s\"", arg);
136
 
137
            oflg = arg;
138
            if (*oflg == '\0')
139
                fatalerr("Missing output file name");
140
            break;
141
        }
142
        else if (*arg == 'n')                   /* no newline */
143
            nflg = 0;
144
        else if (*arg == 'N')                         
145
            nlflg = 1;                          /* newline was seperator */
146
 
147
        else if (*arg == 'k')
148
        {
149
            if (arg[1] == '1')
150
                arg++, kflg++;                  /* lf/cr kludge */
151
            else if (arg[1] == '2')
152
                arg++, k2flg++;                 /* MRI kludge */
153
            else
154
            {
155
                kflg = 1;
156
            }
157
        }
158
        else if (*arg == 'r')                   /* inline realpath */
159
        {
160
            if (rflg == -1)                     /* remove default (if any) */
161
                rtags[ rflg = 0 ] = '\0';
162
            if (arg[1])
163
                rtags[ rflg++ ] = arg[1], arg++;
164
        }
165
        else if (*arg == 'e') {
166
            if (eflg != 1)
167
                fatalerr("-E and -e are exclusive");
168
            eflg = 2;
169
        }
170
        else if (*arg == 'E') {
171
            if (eflg != 1)
172
                fatalerr("-E and -e are exclusive");
173
            eflg = 0;
174
        }
175
        else if (*arg == 'v')
176
            vflg++;
177
        else if (*arg == 'c')
178
            cflg++;
179
        else if (*arg == 'M')
180
            Mflg++;
181
        else if (*arg == 't')
182
            tflg++;
183
 
184
        else if (*arg == 'w')
185
            wflg++;
186
 
187
        else if (*arg == 'W')                   /* whitespace handling */
188
        {
189
            switch(arg[1]) {
190
            case '0':                           /* method 0: nothing */
191
            case '-':
192
                whitespace = WS_IGNORE;
193
                break;
194
            case '1':                           /* method 1: escape */
195
            case 'e':
196
                whitespace = WS_ESCAPE;
197
                break;
198
            case '2':                           /* method 2: quote path */
199
            case 'q':
200
                whitespace = WS_QUOTE;
201
                break;
202
        /*  case '3':                   */      /* method 3: junk */
203
        /*  case 'j':                   */
204
        /*      whitespace = WS_JUNK;   */
205
        /*      break;                  */
206
        /*  case '4':                   */      /* method 4: URL */
207
        /*  case 'u':                   */
208
        /*      whitespace = WS_URL;    */
209
        /*      break;                  */
210
            default:
211
                fatalerr( "Unknown -W specification '%c'", arg[1] );
212
                break;
213
            }
214
            arg++;
215
        }
216
 
217
        else if (*arg == 'm' && arg[1])
218
            mflg = arg[1], arg++;
219
        else if (*arg == 'd' && arg[1])
220
        {
221
            dflg[0] = arg[1];                   /* known pairs */
222
            if      (dflg[0] == '(') dflg[1] = ')';
223
            else if (dflg[0] == '{') dflg[1] = '}';
224
            else if (dflg[0] == '[') dflg[1] = ']';
225
            else if (dflg[0] == '<') dflg[1] = '>';
226
            else dflg[1] = arg[1];              /* single */
227
            arg++;
228
        }
229
        else
230
        {
231
            return (-1);                        /* unknown, exit arg loop */
232
        }
233
        arg++;
234
    }
235
 
236
    return (1);
237
}
238