File: getargs.c

package info (click to toggle)
libcgns 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,740 kB
  • ctags: 4,493
  • sloc: ansic: 46,717; fortran: 1,341; sh: 368; makefile: 259
file content (116 lines) | stat: -rw-r--r-- 2,740 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "getargs.h"

/*---------- usage --------------------------------------------------
 * display usage message and exit
 *-------------------------------------------------------------------*/

void print_usage (
#ifdef PROTOTYPE
    char **usgmsg, char *errmsg)
#else
    usgmsg, errmsg)
char **usgmsg, *errmsg;
#endif
{
    int n;

    if (NULL != errmsg)
        fprintf (stderr, "ERROR: %s\n", errmsg);
    for (n = 0; NULL != usgmsg[n]; n++)
        fprintf (stderr, "%s\n", usgmsg[n]);
    exit (NULL != errmsg);
}

/*---------- getargs ---------------------------------------------------
 * get option letter from argument vector or terminates on error
 * this is similar to getopt()
 *----------------------------------------------------------------------*/

int argind = 0;  /* index into argv array */
int argerr = 1;  /* error output flag */
char *argarg;    /* pointer to argument string */

int getargs (
#ifdef PROTOTYPE
    int argc, char **argv, char *ostr)
#else
    argc, argv, ostr)
int argc;
char **argv, *ostr;
#endif
{
    int argopt;
    char *oli;
    static char *place;
    static int nextarg;

    /* initialization */

    if (!argind)
        nextarg = 1;

    if (nextarg) {  /* update scanning pointer */
        if (argind >= argc || ++argind == argc) {
            argarg = NULL;
            return (-1);
        }
        if ('-' != argv[argind][0]) {
            argarg = argv[argind];
            return (0);
        }
        place = argarg = &argv[argind][1];
        if (!*place) {
            if (++argind == argc) {
                argarg = NULL;
                return (-1);
            }
            argarg = argv[argind];
            return (0);
        }
        nextarg = 0;
    }

    /* check for valid option */

    if ((argopt = *place++) == ':' || argopt == ';' ||
        (oli = strchr (ostr, argopt)) == NULL) {
        if (argerr) {
            fprintf (stderr, "invalid option - `%c'\n", argopt);
            exit (-1);
        }
        return (argopt);
    }

    /* don't need argument */

    if (*++oli != ':') {
        if (*place && *oli == ';') {    /* optional argument */
            argarg = place;
            nextarg = 1;
        }
        else {
            argarg = NULL;
            if (!*place)
                nextarg = 1;
        }
        return (argopt);
    }

    /* get argument */

    if (!*place) {
        if (++argind >= argc) {
            if (!argerr) return (':');
            fprintf (stderr, "missing argument for option `%c'\n", argopt);
            exit (1);
        }
        place = argv[argind];
    }
    argarg = place;
    nextarg = 1;
    return (argopt);
}