summaryrefslogtreecommitdiff
path: root/security/nss/cmd/cmdlib/cmdline.c
blob: 84ad35be30e43b63273278a0c0c9c6ad375828e4 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape security libraries.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */

#include <string.h>
#include <ctype.h>

#include "cmdutil.h"

static int s_indent_size = 4;

void
CMD_SetIndentSize(int size) 
{
    s_indent_size = size;
}

#if 0
static void
indent(PRFileDesc *out, int level)
{
    int i, j;
    for (i=0; i<level; i++)
	for (j=0; j<s_indent_size; j++)	
	    PR_fprintf(out, " ");
}
#endif

struct cmdPrintStateStr {
    PRFileDesc *file;
    int width;
    int indent;
    int linepos;
};

static void
init_print_ps(cmdPrintState *ps, PRFileDesc *outfile, int width, int indent)
{
    ps->file = (outfile) ? outfile : PR_STDOUT;
    ps->width = (width > 0) ? width : 80;
    ps->indent = (indent > 0) ? indent : 0;
    ps->linepos = 0;
}

static void
print_ps_indent(cmdPrintState *ps)
{
    int j;
    if (ps->linepos != 0) {
	PR_fprintf(ps->file, "\n");
	ps->linepos = 0;
    }
    for (j=0; j<=ps->indent; j++) PR_fprintf(ps->file, " ");
    ps->linepos = ps->indent;
}

static void
print_ps_to_indent(cmdPrintState *ps)
{
    if (ps->linepos > ps->indent)
	PR_fprintf(ps->file, "\n");
    while (ps->linepos <= ps->indent) {
	PR_fprintf(ps->file, " ");
	ps->linepos++;
    }
}

static void
nprintbuf(cmdPrintState *ps, char *buf, int start, int len)
{
    int j;
    for (j=start; j<start + len; j++) {
	if (buf[j] == '\n') {
	    PR_fprintf(ps->file, "\n");
	    ps->linepos = 0;
	    print_ps_indent(ps);
	} else {
	    PR_fprintf(ps->file, "%c", buf[j]);
	    ps->linepos++;
	}
    }
}

static void 
nprintf(cmdPrintState *ps, char *msg, ...)
{
    char buf[256];
    int i, len, grouplen;
    PRBool openquote, openbracket, openparen, openangle, itsaword;
    va_list args;
    va_start(args, msg);
    vsprintf(buf, msg, args);
    len = strlen(buf);
    /* print_ps_indent(ps); */
    if (len < ps->width - ps->linepos) {
	nprintbuf(ps, buf, 0, len + 1);
	return;
    }
    /* group in this order: " [ ( < word > ) ] " */
    i=0;
    openquote=openbracket=openparen=openangle=itsaword=PR_FALSE;
    while (i<len) {
	grouplen = 0;
	if (buf[i] == '\"') { openquote = PR_TRUE; grouplen = 1; }
	else if (buf[i] == '[') { openbracket = PR_TRUE; grouplen = 1; }
	else if (buf[i] == '(') { openparen = PR_TRUE; grouplen = 1; }
	else if (buf[i] == '<') { openangle = PR_TRUE; grouplen = 1; }
	else itsaword = PR_TRUE;
	while (grouplen < len && buf[i+grouplen] != '\0' &&
	       ((openquote && buf[i+grouplen] != '\"') ||
	        (openbracket && buf[i+grouplen] != ']') ||
	        (openparen && buf[i+grouplen] != ')') ||
	        (openangle && buf[i+grouplen] != '>') ||
	        (itsaword && !isspace(buf[i+grouplen]))))
	    grouplen++;
	grouplen++; /* grab the terminator (whitespace for word) */
	if (!itsaword && isspace(buf[i+grouplen])) grouplen++;
	if (grouplen < ps->width - ps->linepos) {
	    nprintbuf(ps, buf, i, grouplen);
	} else if (grouplen < ps->width - ps->indent) {
	    print_ps_indent(ps);
	    nprintbuf(ps, buf, i, grouplen);
	} else {
	    /* it's just too darn long.  what to do? */
	}
	i += grouplen;
	openquote=openbracket=openparen=openangle=itsaword=PR_FALSE;
    }
    va_end(args);
}

void 
CMD_PrintUsageString(cmdPrintState *ps, char *str)
{
    nprintf(ps, "%s", str);
}

/* void because it exits with Usage() if failure */
static void
command_line_okay(cmdCommand *cmd, char *progName)
{
    int i, c = -1;
    /* user asked for help.  hope somebody gives it to them. */
    if (cmd->opt[0].on) return;
    /* check that the command got all of its needed options */
    for (i=0; i<cmd->ncmd; i++) {
	if (cmd->cmd[i].on) {
	    if (c > 0) {
		fprintf(stderr, 
		        "%s: only one command can be given at a time.\n",
		        progName);
		CMD_Usage(progName, cmd);
	    } else {
		c = i;
	    }
	}
    }
    if (cmd->cmd[c].argUse == CMDArgReq && cmd->cmd[c].arg == NULL) {
	/* where's the arg when you need it... */
	fprintf(stderr, "%s: command --%s requires an argument.\n",
	                 progName, cmd->cmd[c].s);
	fprintf(stderr, "type \"%s --%s --help\" for help.\n",
	                 progName, cmd->cmd[c].s);
	CMD_Usage(progName, cmd);
    }
    for (i=0; i<cmd->nopt; i++) {
	if (cmd->cmd[c].req & CMDBIT(i)) {
	    /* command requires this option */
	    if (!cmd->opt[i].on) {
		/* but it ain't there */
		fprintf(stderr, "%s: command --%s requires option --%s.\n",
		                 progName, cmd->cmd[c].s, cmd->opt[i].s);
	    } else {
		/* okay, its there, but does it have an arg? */
		if (cmd->opt[i].argUse == CMDArgReq && !cmd->opt[i].arg) {
		    fprintf(stderr, "%s: option --%s requires an argument.\n",
		                     progName, cmd->opt[i].s);
		}
	    }
	} else if (cmd->cmd[c].opt & CMDBIT(i)) {
	   /* this option is optional */
	    if (cmd->opt[i].on) {
		/* okay, its there, but does it have an arg? */
		if (cmd->opt[i].argUse == CMDArgReq && !cmd->opt[i].arg) {
		    fprintf(stderr, "%s: option --%s requires an argument.\n",
		                     progName, cmd->opt[i].s);
		}
	    }
	} else {
	   /* command knows nothing about it */
	   if (cmd->opt[i].on) {
		/* so why the h--- is it on? */
		fprintf(stderr, "%s: option --%s not used with command --%s.\n",
		                 progName, cmd->opt[i].s, cmd->cmd[c].s);
	   }
	}
    }
}

static char *
get_arg(char *curopt, char **nextopt, int argc, int *index)
{
    char *str;
    if (curopt) {
	str = curopt;
    } else {
	if (*index + 1 >= argc) return NULL;
	/* not really an argument but another flag */
	if (nextopt[*index+1][0] == '-') return NULL;
	str = nextopt[++(*index)];
    }
    /* parse the option */
    return strdup(str);
}

int
CMD_ParseCommandLine(int argc, char **argv, char *progName, cmdCommand *cmd)
{
    int i, j, k;
    int cmdToRun = -1;
    char *flag;
    i=1;
    if (argc <= 1) return -2; /* gross hack for cmdless things like atob */
    do {
	flag = argv[i];
	if (strlen(flag) < 2) /* huh? */
	    return -1;
	if (flag[0] != '-')
	    return -1;
	/* ignore everything after lone "--" (app-specific weirdness there) */
	if (strcmp(flag, "--") == 0)
	    return cmdToRun;
	/* single hyphen means short alias (single-char) */
	if (flag[1] != '-') {
	    j=1;
	    /* collect a set of opts, ex. -abc */
	    while (flag[j] != '\0') {
		PRBool found = PR_FALSE;
		/* walk the command set looking for match */
		for (k=0; k<cmd->ncmd; k++) {
		    if (flag[j] == cmd->cmd[k].c) {
			/* done - only take one command at a time */
			if (j > 1) return -1;
			cmd->cmd[k].on = found = PR_TRUE;
			cmdToRun = k;
			if (cmd->cmd[k].argUse != CMDNoArg)
			    cmd->cmd[k].arg = get_arg(NULL, argv, argc, &i);
			goto next_flag;
		    }
		}
		/* wasn't found in commands, try options */
		for (k=0; k<cmd->nopt; k++) {
		    if (flag[j] == cmd->opt[k].c) {
			/* collect this option and keep going */
			cmd->opt[k].on = found = PR_TRUE;
			if (flag[j+1] == '\0') {
			    if (cmd->opt[k].argUse != CMDNoArg)
				cmd->opt[k].arg = get_arg(NULL, argv, argc, &i);
			    goto next_flag;
			}
		    }
		}
		j++;
		if (!found) return -1;
	    }
	} else { /* long alias, ex. --list */
	    char *fl = NULL, *arg = NULL;
	    PRBool hyphened = PR_FALSE;
	    fl = &flag[2];
	    arg = strchr(fl, '=');
	    if (arg) {
		*arg++ = '\0';
	    } else {
		arg = strchr(fl, '-');
		if (arg) {
		    hyphened = PR_TRUE; /* watch this, see below */
		    *arg++ = '\0';
		}
	    }
	    for (k=0; k<cmd->ncmd; k++) {
		if (strcmp(fl, cmd->cmd[k].s) == 0) {
		    cmd->cmd[k].on = PR_TRUE;
		    cmdToRun = k;
		    if (cmd->cmd[k].argUse != CMDNoArg || hyphened) {
			cmd->cmd[k].arg = get_arg(arg, argv, argc, &i);
		    }
		    if (arg) arg[-1] = '=';
		    goto next_flag;
		}
	    }
	    for (k=0; k<cmd->nopt; k++) {
		if (strcmp(fl, cmd->opt[k].s) == 0) {
		    cmd->opt[k].on = PR_TRUE;
		    if (cmd->opt[k].argUse != CMDNoArg || hyphened) {
			cmd->opt[k].arg = get_arg(arg, argv, argc, &i);
		    }
		    if (arg) arg[-1] = '=';
		    goto next_flag;
		}
	    }
	    return -1;
	}
next_flag:
	i++;
    } while (i < argc);
    command_line_okay(cmd, progName);
    return cmdToRun;
}

void
CMD_LongUsage(char *progName, cmdCommand *cmd, cmdUsageCallback usage)
{
    int i, j;
    PRBool oneCommand = PR_FALSE;
    cmdPrintState ps;
    init_print_ps(&ps, PR_STDERR, 80, 0);
    nprintf(&ps, "\n%s:   ", progName);
    /* prints app-specific header */
    ps.indent = strlen(progName) + 4;
    usage(&ps, 0, PR_FALSE, PR_TRUE, PR_FALSE);
    for (i=0; i<cmd->ncmd; i++) if (cmd->cmd[i].on) oneCommand = PR_TRUE;
    for (i=0; i<cmd->ncmd; i++) {
	if ((oneCommand  && cmd->cmd[i].on) || !oneCommand) {
	    ps.indent = 0;
	    print_ps_indent(&ps);
	    if (cmd->cmd[i].c != 0) {
		nprintf(&ps, "-%c, ", cmd->cmd[i].c);
		nprintf(&ps, "--%-16s ", cmd->cmd[i].s);
	    } else {
		nprintf(&ps, "--%-20s ", cmd->cmd[i].s);
	    }
	    ps.indent += 20;
	    usage(&ps, i, PR_TRUE, PR_FALSE, PR_FALSE);
	    for (j=0; j<cmd->nopt; j++) {
		if (cmd->cmd[i].req & CMDBIT(j)) {
		    ps.indent = 0;
		    print_ps_indent(&ps);
		    nprintf(&ps, "%3s* ", "");
		    if (cmd->opt[j].c != 0) {
			nprintf(&ps, "-%c, ", cmd->opt[j].c);
			nprintf(&ps, "--%-16s  ", cmd->opt[j].s);
		    } else {
			nprintf(&ps, "--%-20s  ", cmd->opt[j].s);
		    }
		    ps.indent += 29;
		    usage(&ps, j, PR_FALSE, PR_FALSE, PR_FALSE);
		}
	    }
	    for (j=0; j<cmd->nopt; j++) {
		if (cmd->cmd[i].opt & CMDBIT(j)) {
		    ps.indent = 0;
		    print_ps_indent(&ps);
		    nprintf(&ps, "%5s", "");
		    if (cmd->opt[j].c != 0) {
			nprintf(&ps, "-%c, ", cmd->opt[j].c);
			nprintf(&ps, "--%-16s  ", cmd->opt[j].s);
		    } else {
			nprintf(&ps, "--%-20s  ", cmd->opt[j].s);
		    }
		    ps.indent += 29;
		    usage(&ps, j, PR_FALSE, PR_FALSE, PR_FALSE);
		}
	    }
	}
	nprintf(&ps, "\n");
    }
    ps.indent = 0;
    nprintf(&ps, "\n* - required flag for command\n\n");
    /* prints app-specific footer */
    usage(&ps, 0, PR_FALSE, PR_FALSE, PR_TRUE);
    /*nprintf(&ps, "\n\n");*/
    exit(1);
}

void
CMD_Usage(char *progName, cmdCommand *cmd)
{
    int i, j, inc;
    PRBool first;
    cmdPrintState ps;
    init_print_ps(&ps, PR_STDERR, 80, 0);
    nprintf(&ps, "%s", progName);
    ps.indent = strlen(progName) + 1;
    print_ps_to_indent(&ps);
    for (i=0; i<cmd->ncmd; i++) {
	if (cmd->cmd[i].c != 0) {
	    nprintf(&ps, "-%c", cmd->cmd[i].c);
	    inc = 4;
	} else {
	    nprintf(&ps, "--%s", cmd->cmd[i].s);
	    inc = 4 + strlen(cmd->cmd[i].s);
	}
	first = PR_TRUE;
	ps.indent += inc;
	print_ps_to_indent(&ps);
	for (j=0; j<cmd->nopt; j++) {
	    if (cmd->cmd[i].req & CMDBIT(j)) {
		if (cmd->opt[j].c != 0 && cmd->opt[j].argUse == CMDNoArg) {
		    if (first) {
			nprintf(&ps, "-");
			first = !first;
		    }
		    nprintf(&ps, "%c", cmd->opt[j].c);
		}
	    }
	}
	for (j=0; j<cmd->nopt; j++) {
	    if (cmd->cmd[i].req & CMDBIT(j)) {
		if (cmd->opt[j].c != 0)
		    nprintf(&ps, "-%c ", cmd->opt[j].c);
		else
		    nprintf(&ps, "--%s ", cmd->opt[j].s);
		if (cmd->opt[j].argUse != CMDNoArg)
		    nprintf(&ps, "%s ", cmd->opt[j].s);
	    }
	}
	first = PR_TRUE;
	for (j=0; j<cmd->nopt; j++) {
	    if (cmd->cmd[i].opt & CMDBIT(j)) {
		if (cmd->opt[j].c != 0 && cmd->opt[j].argUse == CMDNoArg) {
		    if (first) {
			nprintf(&ps, "[-");
			first = !first;
		    }
		    nprintf(&ps, "%c", cmd->opt[j].c);
		}
	    }
	}
	if (!first) nprintf(&ps, "] ");
	for (j=0; j<cmd->nopt; j++) {
	    if (cmd->cmd[i].opt & CMDBIT(j) && 
	         cmd->opt[j].argUse != CMDNoArg) {
		if (cmd->opt[j].c != 0)
		    nprintf(&ps, "[-%c %s] ", cmd->opt[j].c, cmd->opt[j].s);
		else
		    nprintf(&ps, "[--%s %s] ", cmd->opt[j].s, cmd->opt[j].s);
	    }
	}
	ps.indent -= inc;
	print_ps_indent(&ps);
    }
    ps.indent = 0;
    nprintf(&ps, "\n");
    exit(1);
}