~m-grant-prg/swoc/stretch

« back to all changes in this revision

Viewing changes to src/prg/c/src/cli-prg/cmdlineargs.c

  • Committer: Mark Grant
  • Date: 2019-11-11 10:07:41 UTC
  • mfrom: (1.1.4)
  • Revision ID: m.grant.prg@gmail.com-20191111100741-jox2t0boj7bsvd8k
Merge new upstream development release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 ************************************************************************
44
44
 */
45
45
 
 
46
#include <getopt.h>
 
47
#include <stdarg.h>
46
48
#include <stdio.h>
47
49
#include <stdlib.h>
48
 
#include <stdarg.h>
49
50
#include <string.h>
50
 
#include <getopt.h>
51
51
 
 
52
#include "cmdlineargs.h"
52
53
#include "internal.h"
53
 
#include "cmdlineargs.h"
54
 
 
55
54
 
56
55
/**
57
56
 * Process command line arguments using getopt_long.
58
57
 * @param argc The standard CLA argc.
59
58
 * @param argv The standard CLA argv.
60
 
 * @param block_flag Struct for the block flag.
61
 
 * @param lock_flag Struct for the lock flag.
62
 
 * @param release_flag Struct for the release flag.
63
 
 * @param reset_flag Struct for the reset flag.
64
 
 * @param status_flag Struct for the status flag.
65
 
 * @param unblock_flag Struct for the unblock flag.
66
 
 * @param wait_flag Struct for the wait flag.
 
59
 * @param ... Variable number of flag structs.
67
60
 * @return 0 on success, on failure standard EX_USAGE (64) command line  usage
68
61
 * error.
69
62
 */
80
73
        int c;
81
74
        int x;
82
75
 
83
 
        struct option long_options[] = {
84
 
                {"block",       no_argument,            NULL,   'b'},
85
 
                {"help",        no_argument,            NULL,   'h'},
86
 
                {"lock",        no_argument,            NULL,   'l'},
87
 
                {"release",     no_argument,            NULL,   'r'},
88
 
                {"reset",       no_argument,            NULL,   'i'},
89
 
                {"status",      no_argument,            NULL,   's'},
90
 
                {"unblock",     no_argument,            NULL,   'u'},
91
 
                {"version",     no_argument,            NULL,   'V'},
92
 
                {"wait",        optional_argument,      NULL,   'w'},
93
 
                {NULL,          0,                      NULL,   0}
94
 
        };
 
76
        struct option long_options[]
 
77
                = { { "block", no_argument, NULL, 'b' },
 
78
                    { "help", no_argument, NULL, 'h' },
 
79
                    { "lock", no_argument, NULL, 'l' },
 
80
                    { "release", no_argument, NULL, 'r' },
 
81
                    { "reset", no_argument, NULL, 'i' },
 
82
                    { "status", no_argument, NULL, 's' },
 
83
                    { "unblock", no_argument, NULL, 'u' },
 
84
                    { "version", no_argument, NULL, 'V' },
 
85
                    { "wait", optional_argument, NULL, 'w' },
 
86
                    { NULL, 0, NULL, 0 } };
95
87
 
96
88
        va_start(ap, argv);
97
89
        block_flag = va_arg(ap, struct cla *);
103
95
        wait_flag = va_arg(ap, struct cla *);
104
96
        va_end(ap);
105
97
 
106
 
        while ((c = getopt_long(argc, argv, "bhilrsuVw::",
107
 
                long_options, &option_index)) != -1) {
108
 
 
 
98
        while ((c = getopt_long(argc, argv, "bhilrsuVw::", long_options,
 
99
                                &option_index))
 
100
               != -1) {
109
101
                switch (c) {
110
102
                case 'b':
111
103
                        if (lock_flag->is_set || release_flag->is_set
112
104
                            || reset_flag->is_set || status_flag->is_set
113
105
                            || unblock_flag->is_set || wait_flag->is_set) {
114
106
                                fprintf(stderr, "Options b, i, l, r, s, u and "
115
 
                                        "w are mutually exclusive.\n");
 
107
                                                "w are mutually exclusive.\n");
116
108
                                return 64;
117
109
                        }
118
110
                        block_flag->is_set = 1;
122
114
                            || release_flag->is_set || status_flag->is_set
123
115
                            || unblock_flag->is_set || wait_flag->is_set) {
124
116
                                fprintf(stderr, "Options b, i, l, r, s, u and "
125
 
                                        "w are mutually exclusive.\n");
 
117
                                                "w are mutually exclusive.\n");
126
118
                                return 64;
127
119
                        }
128
120
                        reset_flag->is_set = 1;
130
122
                case 'h':
131
123
                        printf("%s %s", argv[0], " - Help option.\n");
132
124
                        printf("\tLong and short options can be mixed on the "
133
 
                                "command line but if an option takes an "
134
 
                                "optional argument it is best to enter "
135
 
                                "-o\"argument\" or --option=argument.\n");
 
125
                               "command line but if an option takes an "
 
126
                               "optional argument it is best to enter "
 
127
                               "-o\"argument\" or --option=argument.\n");
136
128
                        printf("-b | --block\tBlock client on server.\n");
137
129
                        printf("-i | --reset\tSet locks to 0 and unblock "
138
130
                               "client on server.\n");
139
131
                        printf("-l | --lock\tSet client lock on server.\n");
140
132
                        printf("-r | --release\tRelease client lock on "
141
 
                                "server.\n");
 
133
                               "server.\n");
142
134
                        printf("-u | --unblock\tUnblock client on server.\n");
143
135
                        printf("-s | --status\tShow the locking status.\n");
144
136
                        printf("-V | --version\tDisplay version "
145
 
                                "information.\n");
 
137
                               "information.\n");
146
138
                        printf("-w[NumLocks] | --wait[=NumLocks]\tWait until "
147
 
                                "this client has NumLocks or fewer locks.\n");
 
139
                               "this client has NumLocks or fewer locks.\n");
148
140
                        exit(0);
149
141
                        break;
150
142
                case 'l':
152
144
                            || reset_flag->is_set || status_flag->is_set
153
145
                            || unblock_flag->is_set || wait_flag->is_set) {
154
146
                                fprintf(stderr, "Options b, i, l, r, s, u and "
155
 
                                        "w are mutually exclusive.\n");
 
147
                                                "w are mutually exclusive.\n");
156
148
                                return 64;
157
149
                        }
158
150
                        lock_flag->is_set = 1;
163
155
                            || reset_flag->is_set || status_flag->is_set
164
156
                            || unblock_flag->is_set || wait_flag->is_set) {
165
157
                                fprintf(stderr, "Options b, i, l, r, s, u and "
166
 
                                        "w are mutually exclusive.\n");
 
158
                                                "w are mutually exclusive.\n");
167
159
                                return 64;
168
160
                        }
169
161
                        release_flag->is_set = 1;
173
165
                            || release_flag->is_set || reset_flag->is_set
174
166
                            || status_flag->is_set || wait_flag->is_set) {
175
167
                                fprintf(stderr, "Options b, i, l, r, s, u and "
176
 
                                        "w are mutually exclusive.\n");
 
168
                                                "w are mutually exclusive.\n");
177
169
                                return 64;
178
170
                        }
179
171
                        unblock_flag->is_set = 1;
183
175
                            || release_flag->is_set || reset_flag->is_set
184
176
                            || unblock_flag->is_set || wait_flag->is_set) {
185
177
                                fprintf(stderr, "Options b, i, l, r, s, u and "
186
 
                                        "w are mutually exclusive.\n");
 
178
                                                "w are mutually exclusive.\n");
187
179
                                return 64;
188
180
                        }
189
181
                        status_flag->is_set = 1;
190
182
                        break;
191
183
                case 'V':
192
184
                        printf("%s %s %s %s", argv[0], "Source version -",
193
 
                                swocclient_get_src_version(), "\n");
 
185
                               swocclient_get_src_version(), "\n");
194
186
                        printf("%s %s %s %s", argv[0], "Package version -",
195
 
                                swocclient_get_pkg_version(), "\n");
 
187
                               swocclient_get_pkg_version(), "\n");
196
188
                        exit(0);
197
189
                        break;
198
190
                case 'w':
200
192
                            || release_flag->is_set || reset_flag->is_set
201
193
                            || status_flag->is_set || unblock_flag->is_set) {
202
194
                                fprintf(stderr, "Options b, i, l, r, s, u and "
203
 
                                        "w are mutually exclusive.\n");
 
195
                                                "w are mutually exclusive.\n");
204
196
                                return 64;
205
197
                        }
206
198
                        wait_flag->is_set = 1;
231
223
              || reset_flag->is_set || status_flag->is_set
232
224
              || unblock_flag->is_set || wait_flag->is_set)) {
233
225
                fprintf(stderr, "Either b, i, l, r, s, u or w must be "
234
 
                               "specified.\n");
 
226
                                "specified.\n");
235
227
                return 64;
236
228
        }
237
229
        return 0;
245
237
        int e = 0;
246
238
        if (ARG_BUF > strlen(srcarg))
247
239
                strcpy(flagarg, srcarg);
248
 
        else
249
 
        {
 
240
        else {
250
241
                fprintf(stderr, "Option argument '%s' too long.\n", srcarg);
251
242
                e = 64;
252
243
        }