File: argsconstruct.c

package info (click to toggle)
yodl 4.04.00-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,720 kB
  • sloc: ansic: 7,803; perl: 683; cpp: 570; sh: 411; xml: 190; makefile: 164
file content (71 lines) | stat: -rw-r--r-- 2,031 bytes parent folder | download | duplicates (5)
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
#include "args.ih"

void args_construct(int argc, char **argv,
                    char *options,
                    LongOption const *longOption)
{
    char *cp;
    size_t nopt;
    struct option *long_option;
    size_t nlong = 0;

    if (!longOption)
        long_option = new_calloc(1, sizeof(struct option));
    else
    {
        while (longOption[nlong++].d_name)
            ;

        long_option = new_memory(nlong, sizeof(struct option));

        for (nopt = 0; nopt < nlong; nopt++)
        {
            long_option[nopt].name      = longOption[nopt].d_name;
            long_option[nopt].has_arg   = longOption[nopt].d_type;
            long_option[nopt].flag      = 0;
            long_option[nopt].val       = longOption[nopt].d_value;
        }
    }

    memset(&args, 0, sizeof(Args));
    string_construct(&args.d_option, 0);

    args.d_argv = argv;
    args.d_argc = argc;

    if ((cp = getenv("home")) || (cp = getenv("HOME")))     /* set home */
        args.d_home = new_str(cp);

    args.d_programName = basename(argv[0]);         /*  set programname */
    args.d_initial_dir = new_getcwd();              /* set initial dir. */

    args.d_ok = true;
    nopt = 0;

    while (true)
    {
        int optchar = getopt_long(args.d_argc, args.d_argv,
                                  options, long_option, NULL);

        switch (optchar)
        {
            default:
                string_addchar(&args.d_option, optchar);
                new_size(&args.d_optarg, nopt + 1, nopt, sizeof(char *));
                args.d_optarg[nopt++] = optarg ? new_str(optarg) : 0;
            // FALLING THROUGH
            case 'l':
            continue;                   /* at `while(true)' */

            case '?':
                args.d_ok = false;      /* stop processing at failure       */
            break;

            case EOF:
                args.d_optind = optind;
            break;
        }
        break;                          /* leave `while(true)'  */
    }
    free(long_option);
}