unix/fiss

lib/libutil/arg.h in master
Repositories | Summary | Log | Files | LICENSE

arg.h (1491B) download


 1#pragma once
 2
 3#include <utf.h>
 4
 5/* command line */
 6extern char* argv0;
 7
 8#define ARGBEGIN                                               \
 9	for ((argv0 ? 0 : (argv0 = *argv)), argv++, argc--;        \
10	     argv[0] && argv[0][0] == '-' && argv[0][1];           \
11	     argc--, argv++) {                                     \
12		char *_args, *_argt;                                   \
13		Rune  _argc;                                           \
14		_args = &argv[0][1];                                   \
15		if (_args[0] == '-' && _args[1] == 0) {                \
16			argc--;                                            \
17			argv++;                                            \
18			break;                                             \
19		}                                                      \
20		_argc = 0;                                             \
21		while (*_args && (_args += chartorune(&_argc, _args))) \
22			switch (_argc)
23#define ARGEND   \
24	SET(_argt);  \
25	USED(_argt); \
26	USED(_argc); \
27	USED(_args); \
28	}            \
29	USED(argv);  \
30	USED(argc);
31
32#define ARGF() (_argt = _args, _args = "",                    \
33	            (*_argt ? _argt : argv[1] ? (argc--, *++argv) \
34	                                      : 0))
35#define EARGF(x) (_argt = _args, _args = "",                    \
36	              (*_argt ? _argt : argv[1] ? (argc--, *++argv) \
37	                                        : ((x), abort(), (char*) 0)))
38
39#define ARGC() _argc
40
41#define SHIFT(by) (argc -= (by), argv += (by))