This file is indexed.

/usr/share/nickle/parse-args.5c is in nickle 2.77-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
namespace ParseArgs {
    import File;
    import String;

    public typedef union {
	&bool arg_flag;
	&string arg_string;
	&int arg_int;
	&real arg_real;
	(void(string)) arg_lambda;
    } arg_var;

    public typedef struct {
	arg_var var;
        int abbr;
	string name;
	string expr_name;
	string desc;
    } arg;

    public typedef struct {
	arg_var var;
	void optional;
	string name;
    } posn_arg;

    public typedef struct {
	string prog_name;
	arg[*] args;
	posn_arg[*] posn_args;
	&int unknown;
    } argdesc;

    string arg_string(arg a)
	/*
	 * Given an arg desc, produce
	 * a printable representation
	 */
    {
	string stdarg() {
	    if (is_uninit(&a.name))
		return a.expr_name;
	    string s = "";
	    if (!is_uninit(&a.abbr))
		s += sprintf("-%c,", a.abbr);
	    s += sprintf("--%s", a.name);
	    return s;
	}
	union switch (a.var) {
	case arg_flag:
	    return stdarg();
	default:
	    return sprintf("%s <%s>", stdarg(), a.expr_name);
	}
	abort("internal error: unknown arg type");
    }

    public string basename(string path) 
	/*
	 * Return the filename part of 'path'
	 */
    {
	int p = rindex(path, "/");
	if (p == -1)
	    return path;
	return substr(path, p + 1, length(path) - p - 1);
    }

    /*
     * The argument description was defective, with
     * a reason string.
     */
    public exception bad_argd(string reason);

    /*
     * Print a usage message on the given file.
     * XXX Valid only after parseargs() has been called.
     */
    public void(file f) usage;

    public void parseargs(&argdesc argd, &(string[*]) argv) 
	/*
	 * Parse arguments in 'argv' according to 'argd'
	 */
    {
	/*
	 * validate that any argd.posn_args
	 * consists of mandatory followed by optional args
	 */
	if (!is_uninit(&argd.posn_args))
	    for (int i = 1; i < dim(argd.posn_args); i++)
		if (!is_uninit(&argd.posn_args[i - 1].optional) &&
		    is_uninit(&argd.posn_args[i].optional))
		    raise bad_argd("fixed after optional posn arg");

	/* argument that is current focus */
	int argind = 0;

	/* save program basename as needed */
	if (is_uninit(&argd.prog_name)) {
	    argd.prog_name = basename(argv[0]);
	    argind++;
	}

	/* build hashes for flags */
	arg[int] arg_abbr_hash = {};
	arg[string] arg_name_hash = {};
	if (!is_uninit(&argd.args)) {
	    for (int i = 0; i < dim(argd.args); i++) {
		&arg d = &argd.args[i];
		if (!is_uninit(&d.abbr))
		    arg_abbr_hash[d.abbr] = d;
		if (!is_uninit(&d.name))
		    arg_name_hash[d.name] = d;
	    }
	}

	void usagef(file f)
	    /*
	     * Print a description of program usage on f
	     */
	{
	    fprintf(f, "%s: usage: %s", argd.prog_name, argd.prog_name);
	    if (!is_uninit(&argd.args)) {
		fprintf(f, " <options>");
		for (int i = 0; i < dim(argd.args); i++)
		    if (is_uninit(&argd.args[i].name))
			fprintf(f, " %s", arg_string(argd.args[i]));
	    }
	    if (!is_uninit(&argd.posn_args)) {
		for (int i = 0; i < dim(argd.posn_args); i++)
		    if (is_uninit(&argd.posn_args[i].optional))
			fprintf(f, " %s", argd.posn_args[i].name);
		    else
			fprintf(f, " [%s]", argd.posn_args[i].name);
	    }
	    if (!is_uninit(&&argd.unknown))
		fprintf(f, " [--] ...");
	    fprintf(f, "\n");
	    if (!is_uninit(&argd.args)) {
		for (int i = 0; i < dim(argd.args); i++)
		    if (is_uninit(&argd.args[i].name))
			fprintf(f, "    %s   %s\n",
				argd.args[i].expr_name,
				argd.args[i].desc);
		for (int i = 0; i < dim(argd.args); i++)
		    if (!is_uninit(&argd.args[i].name))
			fprintf(f, "    %s   %s\n",
				arg_string(argd.args[i]),
				argd.args[i].desc);
	    }
	    exit(1);
	}
	usage = usagef;

	void save_value(&arg_var a, string value)
	    /*
	     * convert and store the value
	     * associated with an argument.
	     * return number of args consumed
	     */
	{
	    union switch(a) {
	    case arg_string var:
		var = value;
		break;
	    case arg_int var:
		var = string_to_integer(value);
		break;
	    case arg_real var:
		var = string_to_real(value);
		break;
	    case arg_lambda var:
		var(value);
		break;
	    default:
		abort("save_value called on wrong var type");
	    }
	}

	void process_flag(&arg a)
	    /*
	     * Process a flag
	     */
	{
	    argind++;
	    union switch(a.var) {
	    case arg_flag var:
		var = true;
		return;
	    default:
		if (argind >= dim(argv)) {
		    fprintf(stderr, "%s: missing value for --%s\n",
			    argd.prog_name, a.name);
		    usagef(stderr);
		}
		save_value(&a.var, argv[argind]);
		argind++;
	    }
	}

	/* handle the help case first */
	if (argind < dim(argv) &&
	    (argv[argind] == "--help" || argv[argind] == "--usage"))
	    usagef(stdout);
	
	/* now walk the arguments */
	while(argind < dim(argv)) {
	    string a = argv[argind];

	    /* we're done on a "user stop" */
	    if (a == "--") {
		if (is_uninit(&&argd.unknown)) {
		    fprintf(stderr,
			    "%s: don't know what to do with arg \"%s\"\n",
			    argd.prog_name,
			    a);
			usagef(stderr);
		}
		break;
	    }

	    /* process abbreviated (old-style) flags (maybe grouped) */
	    if (a[0] == '-' && a[1] != '-') {
		for (int i = 1; i < length(a); i++) {
		    if (!hash_test(arg_abbr_hash, a[i])) {
			fprintf(stderr, "%s: unknown flag char '-%c'\n",
				argd.prog_name,
				a[i]);
			usagef(stderr);
		    }
		    process_flag(&arg_abbr_hash[a[i]]);
		}
		continue;
	    }

	    /* process a long flag */
	    if (a[0] == '-' && a[1] == '-') {
		string argname = substr(a, 2, length(a) - 2);
		if (!hash_test(arg_name_hash, argname)) {
			fprintf(stderr, "%s: unknown flag \"--%s\"\n",
				argd.prog_name,
				argname);
			usagef(stderr);
		}
		process_flag(&arg_name_hash[argname]);
		continue;
	    }

	    /* maybe something will suck up remaining args  */
	    if (!is_uninit(&argd.posn_args) ||
		!is_uninit(&&argd.unknown))
		break;

	    fprintf(stderr, "%s: extra argument \"%s\"\n",
		    argd.prog_name,
		    argv[argind]);
	    usagef(stderr);
	}

	/* process any positional arguments */
	if (!is_uninit(&argd.posn_args)) {
	    for (int i = 0; i < dim(argd.posn_args); i++) {
		if (argind >= dim(argv) ||
		    argv[argind] == "--") {
		    if (!is_uninit(&argd.posn_args[i].optional))
			break;
		    fprintf(stderr, "%s: missing required argument %s\n",
			    argd.prog_name,
			    argd.posn_args[i].name);
		    usagef(stderr);
		}
		save_value(&argd.posn_args[i].var, argv[argind]);
		argind++;
	    }
	}

	/* process any unknown arguments */
	if (argind < dim(argv)) {
	    if (argv[argind] == "--")
		argind++;
	    if (!is_uninit(&&argd.unknown)) {
		argd.unknown = argind;
		return;
	    }
	    if (argind < dim(argv)) {
	        fprintf(stderr, "%s: unknown or extra argument \"%s\"\n",
			argd.prog_name,
			argv[argind]);
		usagef(stderr);
	    }
	}
    }
}