File: example.c

package info (click to toggle)
shhopt 1.1.7-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 140 kB
  • sloc: ansic: 351; makefile: 168
file content (137 lines) | stat: -rw-r--r-- 4,405 bytes parent folder | download | duplicates (6)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* $Id: example.c,v 1.7 2002/03/02 19:56:18 sverrehu Exp $ */
/*------------------------------------------------------------------------
 |  FILE            example.c
 |
 |  DESCRIPTION     Sample source code using the shhopt-library to parse
 |                  command line options.
 |
 |  WRITTEN BY      Sverre H. Huseby <shh@thathost.com>
 +----------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>  /* needed in strToDouble only */

#include "shhopt.h"

/*-----------------------------------------------------------------------+
|  PRIVATE DATA                                                          |
+-----------------------------------------------------------------------*/

/* called by -N or --number-func */
static void
numberFunc(int i)
{
    printf("integer function called, i=%d\n", i);
}

/* called by -S or --string-func */
static void
stringFunc(char *s)
{
    printf("string function called, s=\"%s\"\n", s);
}

/* called by -F or --flag-func */
static void
flagFunc(void)
{
    printf("flag function called\n");
}

/* shhopt does not support double arguments, so we write our own
 * handler for this. The trick is to create a function taking a string
 * argument, and convert this string to a double. The following two
 * functions handles this. */

/* convert a string to a double value, with error checking */
static double
strToDouble(char *s)
{
    double d;
    char *e;

    d = strtod(s, &e);
    if (*e) {
	fprintf(stderr, "invalid number `%s'\n", s);
	exit(1);
    }
    if (errno == ERANGE) {
	fprintf(stderr, "number `%s' out of range\n", s);
	exit(1);
    }
    return d;
}

/* called by -D or --double-func */
static void
doubleFunc(char *s)
{
    double d;

    d = strToDouble(s);
    printf("double function called, d=\"%g\"\n", d);
}

static void
usage(void)
{
    printf(
      "usage: example [option ...] [other-arguments ...]\n"
      "\n"
      "  -n, --number=NUMBER       set value of variable `number'\n"
      "  -N, --number-func=NUMBER  call function with given integer argument\n"
      "  -D, --double-func=NUMBER  call function with given double argument\n"
      "  -s, --string=STRING       set value of variable `string'\n"
      "  -S, --string-func=STRING  call function with given string argument\n"
      "  -f, --flag                set value of variable `flag' to 1\n"
      "  -F, --flag-func           call a function with no argument\n"
      "\n"
      "A double-dash (`--') indicates that the rest of the line is not to\n"
      "be scanned for options.\n"
    );
    exit(1);
}

/*-----------------------------------------------------------------------+
|  PUBLIC FUNCTIONS                                                      |
+-----------------------------------------------------------------------*/

int
main(int argc, char *argv[])
{
    int  q;                 /* counter */
    int  number = 0;        /* set to anything by -n or --number */
    int  flag = 0;          /* set to 1 by -f or --flag */
    char *string = "none";  /* set to anything by -s or --string */
    optStruct opt[] = {
      /* short long              type        var/func       special       */
        { 'h', "help",           OPT_FLAG,   usage,         OPT_CALLFUNC },
        { 'n', "number",         OPT_INT,    &number,       0            },
        { 'N', "number-func",    OPT_INT,    numberFunc,    OPT_CALLFUNC },
        { 'D', "double-func",    OPT_STRING, doubleFunc,    OPT_CALLFUNC },
        { 's', "string",         OPT_STRING, &string,       0            },
        { 'S', "string-func",    OPT_STRING, stringFunc,    OPT_CALLFUNC },
        { 'f', "flag",           OPT_FLAG,   &flag,         0            },
        { 'F', "flag-func",      OPT_FLAG,   flagFunc,      OPT_CALLFUNC },
        { 0, 0, OPT_END, 0, 0 }  /* no more options */
    };

    /* if you link with the shhmsg-library, you may wish to set the
     * the error handling function by calling
     * optSetFatalFunc(msgFatal); */

    /* parse all options */
    optParseOptions(&argc, argv, opt, 0);

    /* display result */
    printf("number=%d, flag=%d, string=\"%s\"\n", number, flag, string);

    /* what's left in argv, are any non-optinons. */
    printf("remaining arguments: ");
    for (q = 1; q < argc; q++)
        printf("\"%s\" ", argv[q]);
    printf("\n");

    return 0;
}