summaryrefslogtreecommitdiff
path: root/src/opt.c
blob: 8efbf52f23c0fee8fe9516a74d78e83782a8d6b3 (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
/*
 *	libHX/opt.c
 *	Copyright Jan Engelhardt, 2002-2011
 *
 *	This file is part of libHX. libHX is free software; you can
 *	redistribute it and/or modify it under the terms of the GNU Lesser
 *	General Public License as published by the Free Software Foundation;
 *	either version 2.1 or (at your option) any later version.
 */
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libHX/ctype_helper.h>
#include <libHX/deque.h>
#include <libHX/map.h>
#include <libHX/misc.h>
#include <libHX/option.h>
#include <libHX/string.h>
#include "internal.h"

/* Definitions */
#define C_OPEN  '('
#define C_CLOSE ')'
#define NTYPE_S(con, tpx) NTYPE((con), tpx, strtol)
#define NTYPE_U(con, tpx) NTYPE((con), tpx, strtoul)

#define NTYPE(con, tpx, func) case (con): { \
	tpx *p, v = (func)(cbi->data, NULL, 0); \
	if ((p = opt->ptr) != NULL) { \
		if (opt->type & HXOPT_NOT) \
			v = ~v; \
		switch (opt->type & HXOPT_LOPMASK2) { \
			case 0:         *p  = v; break; \
			case HXOPT_OR:  *p |= v; break; \
			case HXOPT_AND: *p &= v; break; \
			case HXOPT_XOR: *p ^= v; break; \
			default: \
				fprintf(stderr, "libHX-opt: illegal " \
				        "combination of logical op mask\n"); \
				break; \
		} \
	} \
	cbi->data_long = v; \
	break; \
}

#define SCREEN_WIDTH 80 /* fine, popt also has it hardcoded */

enum {

	W_NONE    = 0,
	W_SPACE   = 1 << 0,
	W_BRACKET = 1 << 1,
	W_ALT     = 1 << 2,
	W_EQUAL   = 1 << 3,

	HXOPT_LOPMASK2 = HXOPT_OR | HXOPT_AND | HXOPT_XOR,
	HXOPT_LOPMASK  = HXOPT_LOPMASK2 | HXOPT_NOT,
	HXOPT_TYPEMASK = 0x1F, /* 5 bits */
};

/**
 * HX_getopt_error - internal option parser error codes
 * %HXOPT_E_LONG_UNKNOWN:	unknown long option
 * %HXOPT_E_LONG_TAKESVOID:	long option was used with an arg (--long=arg)
 * %HXOPT_E_LONG_MISSING:	long option requires an argument
 * %HXOPT_E_SHORT_UNKNOWN:	unknown short option
 * %HXOPT_E_SHORT_MISSING:	short option requires an argument
 * %HXOPT_E_AMBIG_PREFIX:	used abbreviated long option but had multiple results
 */
enum {
	HXOPT_E_LONG_UNKNOWN = 1,
	HXOPT_E_LONG_TAKESVOID,
	HXOPT_E_LONG_MISSING,
	HXOPT_E_SHORT_UNKNOWN,
	HXOPT_E_SHORT_MISSING,
	HXOPT_E_AMBIG_PREFIX,
};

/**
 * HX_getopt_state - internal option parser states
 * %HXOPT_S_NORMAL:	base state, options accepted
 * %HXOPT_S_SHORT:	a short option has been seen
 * %HXOPT_S_TWOLONG:	a long option has been seen
 * %HXOPT_S_LONG:	a long option and its argument have been seen
 * %HXOPT_S_TERMINATED:	options closed, all remaining args are to be copied
 */
enum HX_getopt_state {
	HXOPT_S_NORMAL = 0,
	HXOPT_S_SHORT,
	HXOPT_S_TWOLONG,
	HXOPT_S_LONG,
	HXOPT_S_TERMINATED,
};

/**
 * %HXOPT_I_ASSIGN:	call do_assign
 * %HXOPT_I_ADVARG:	advance to next argument in @opt
 * %HXOPT_I_ADVARG2:	advance by two arguments in @opt
 * %HXOPT_I_ADVCHAR:	advance to next character in @cur
 * %HXOPT_I_ERROR:	HXoption error
 */
enum {
	HXOPT_I_ASSIGN  = 1 << 3,
	HXOPT_I_ADVARG  = 1 << 4,
	HXOPT_I_ADVARG2 = 1 << 5,
	HXOPT_I_ADVCHAR = 1 << 6,
	HXOPT_I_ERROR   = 1 << (sizeof(int) * CHAR_BIT - 2),

	HXOPT_I_MASK    = HXOPT_I_ADVARG | HXOPT_I_ADVARG2 | HXOPT_I_ADVCHAR |
	                  HXOPT_I_ASSIGN | HXOPT_I_ERROR,
};

/**
 * struct HX_getopt_vars - option parser working variable set
 * @arg0:	saved program name
 * @remaining:	list of extracted non-options
 * @cbi:	callback info
 * @flags:	flags setting the behavior for HX_getopt
 */
struct HX_getopt_vars {
	const char *arg0;
	struct HXdeque *remaining;
	struct HXoptcb cbi;
	unsigned int flags;
};

struct HXoption HXopt_ambig_prefix;

static bool posix_me_harder(void)
{
	const char *s;
	char *end;
	int res;

	s = getenv("POSIXLY_CORRECT");
	if (s == NULL || *s == '\0')
		return false;
	res = strtol(s, &end, 0);
	if (end != s)
		/* number */
		return res;
	return true; /* non-empty string */
}

static void do_assign(struct HXoptcb *cbi, const char *arg0)
{
	const struct HXoption *opt = cbi->current;

	switch (opt->type & HXOPT_TYPEMASK) {
	case HXTYPE_NONE: {
		if (opt->ptr != NULL) {
			int *p = opt->ptr;
			if (opt->type & HXOPT_INC)      ++*p;
			else if (opt->type & HXOPT_DEC) --*p;
			else                            *p = 1;
		}
		cbi->data_long = 1;
		break;
	}
	case HXTYPE_VAL:
		*static_cast(int *, opt->ptr) = cbi->data_long = opt->val;
		break;
	case HXTYPE_SVAL:
		*static_cast(const char **, opt->ptr) = cbi->data = opt->uptr;
		break;
	case HXTYPE_BOOL: {
		int *p;
		if ((p = opt->ptr) != NULL)
			*p = strcasecmp(cbi->data, "yes") == 0 ||
			     strcasecmp(cbi->data, "on") == 0 ||
			     strcasecmp(cbi->data, "true") == 0 ||
			     (HX_isdigit(*cbi->data) &&
			     strtoul(cbi->data, NULL, 0) != 0);
		break;
	}
	case HXTYPE_BYTE:
		*static_cast(unsigned char *, opt->ptr) = *cbi->data;
		break;

	NTYPE_U(HXTYPE_UCHAR,  unsigned char);
	NTYPE_S(HXTYPE_CHAR,   char);
	NTYPE_U(HXTYPE_USHORT, unsigned short);
	NTYPE_S(HXTYPE_SHORT,  short);
	NTYPE_U(HXTYPE_UINT,   unsigned int);
	NTYPE_S(HXTYPE_INT,    int);
	NTYPE_U(HXTYPE_ULONG,  unsigned long);
	NTYPE_S(HXTYPE_LONG,   long);
	NTYPE_U(HXTYPE_UINT8,  uint8_t);
	NTYPE_S(HXTYPE_INT8,   int8_t);
	NTYPE_U(HXTYPE_UINT16, uint16_t);
	NTYPE_S(HXTYPE_INT16,  int16_t);
	NTYPE_U(HXTYPE_UINT32, uint32_t);
	NTYPE_S(HXTYPE_INT32,  int32_t);
#ifndef _MSC_VER
	NTYPE(HXTYPE_ULLONG,   unsigned long long, strtoull);
	NTYPE(HXTYPE_LLONG,    long long, strtoll);
	NTYPE(HXTYPE_UINT64,   uint64_t, strtoull);
	NTYPE(HXTYPE_INT64,    int64_t, strtoll);
#endif
	NTYPE(HXTYPE_SIZE_T,   size_t, strtoull);
	case HXTYPE_FLOAT:
		cbi->data_dbl = strtod(cbi->data, NULL);
		if (opt->ptr != NULL)
			*static_cast(float *, opt->ptr) = cbi->data_dbl;
		break;
	case HXTYPE_DOUBLE:
		cbi->data_dbl = strtod(cbi->data, NULL);
		if (opt->ptr != NULL)
			*static_cast(double *, opt->ptr) = cbi->data_dbl;
		break;
	case HXTYPE_STRING:
		if (opt->ptr != NULL)
			*static_cast(char **, opt->ptr) = HX_strdup(cbi->data);
		break;
	case HXTYPE_STRDQ:
		HXdeque_push(opt->ptr, HX_strdup(cbi->data));
		break;
	case HXTYPE_MCSTR:
		if (opt->ptr != NULL)
			HXmc_strcpy(opt->ptr, cbi->data);
		break;
	case HXTYPE_XHELP:
		cbi->data = arg0;
		break;
	default:
		fprintf(stderr, "libHX-opt: illegal type %d\n",
		        opt->type & HXOPT_TYPEMASK);
		break;
	} /* switch */
	if (opt->cb != NULL)
		opt->cb(cbi);
}

static __inline__ const struct HXoption *
lookup_short(const struct HXoption *table, char opt)
{
	for (; table->type != HXTYPE_XSNTMARK; ++table)
		if (table->sh == opt)
			return table;
	return NULL;
}

static const struct HXoption *
lookup_long_pfx(const struct HXoption *table, const char *key)
{
	const struct HXoption *cand = NULL;
	size_t klen = strlen(key);

	for (; table->type != HXTYPE_XSNTMARK; ++table) {
		if (table->ln == NULL)
			continue;
		if (strncmp(table->ln, key, klen) != 0)
			continue;
		/* Prefix match */
		if (table->ln[klen] == '\0')
			return table; /* Exact match */
		if (cand != NULL)
			return &HXopt_ambig_prefix;
		cand = table;
	}
	return cand;
}

static __inline__ const struct HXoption *
lookup_long(const struct HXoption *table, const char *key)
{
	for (; table->type != HXTYPE_XSNTMARK; ++table)
		if (table->ln != NULL && strcmp(table->ln, key) == 0)
			return table;
	return NULL;
}

static __inline__ bool takes_void(unsigned int t)
{
	t &= HXOPT_TYPEMASK;
	return t == HXTYPE_NONE || t == HXTYPE_VAL || t == HXTYPE_SVAL ||
	       t == HXTYPE_XSNTMARK || t == HXTYPE_XHELP;
}

static void opt_to_text(const struct HXoption *opt, char *buf, size_t len,
    unsigned int flags)
{
	const char *alt, *htyp = (opt->htyp != NULL) ? opt->htyp : "ARG";
	size_t i = 0;
	char equ;

	if (flags & W_SPACE)   buf[i++] = ' ';
	if (flags & W_BRACKET) buf[i++] = '['; /* ] */
	if (flags & W_ALT) {
		alt = "|";
		equ = (flags & W_EQUAL) ? '=' : ' ';
	} else {
		alt = ", ";
		equ = '=';
	}

	if (opt->ln == NULL) {
		buf[i++] = '-';
		buf[i++] = opt->sh;
		if (!takes_void(opt->type))
			i += snprintf(buf + i, len - i, " %s", htyp);
	} else if (opt->sh == '\0') {
		if (takes_void(opt->type))
			i += snprintf(buf + i, len - i,
			     "--%s", opt->ln);
		else
			i += snprintf(buf + i, len - i,
			     "--%s=%s", opt->ln, htyp);
	} else {
		if (takes_void(opt->type))
			i += snprintf(buf + i, len - i, "-%c%s--%s",
			     opt->sh, alt, opt->ln);
		else
			i += snprintf(buf + i, len - i, "-%c%s--%s%c%s",
			     opt->sh, alt, opt->ln, equ, htyp);
	}

	if (flags & W_BRACKET)
		buf[i++] = ']';
	buf[i] = '\0';
}

static void print_indent(const char *msg, unsigned int ind, FILE *fp)
{
	size_t rest = SCREEN_WIDTH - ind;
	char *p;

	while (true) {
		if (strlen(msg) < rest) {
			fprintf(fp, "%s", msg);
			break;
		}
		if ((p = HX_strbchr(msg, msg + rest, ' ')) == NULL) {
			fprintf(fp, "%s", msg);
			break;
		}
		fprintf(fp, "%.*s\n%*s", static_cast(unsigned int, p - msg),
		        msg, ind, "");
		msg  = p + 1;
		rest = SCREEN_WIDTH - ind;
	}
	fprintf(fp, "\n");
}

/**
 * HXparse_dequote_int - shell-style argument unescape
 * @o:		input/output string
 * @end:	terminating characters
 *
 * Unescapes a quoted argument, in-place.
 * Returns a pointer to one position after the termination character.
 */
static char *HXparse_dequote_int(char *o, const char *end)
{
	char *i, quot = '\0';
	for (i = o; *i != '\0'; ) {
		if (quot == '\0') {
			switch (*i) {
				case '"':
				case '\'':
					quot = *i++;
					continue;
				case '\\':
					if (*++i != '\0')
						*o++ = *i++;
					continue;
			}
			if (end != NULL && strchr(end, *i) != NULL) {
				*o = '\0';
				return i + 1;
			}
			*o++ = *i++;
			continue;
		}
		if (*i == quot) {
			quot = 0;
			++i;
			continue;
		} else if (*i == '\\') {
			if (*++i != '\0')
				*o++ = *i++;
			continue;
		}
		*o++ = *i++;
	}
	*o = '\0';
	return NULL;
}

/**
 * HXparse_dequote_fmt
 * @s:		Input string
 * @end:	Terminating characters. May be %NULL.
 * @pptr:	Return pointer
 *
 * Dequote a string @s until @end, and return an allocated string that will
 * contain the result, or %NULL on error. @*pptr will then point to the
 * terminating character.
 * Nested %() are honored.
 *
 * (This function is used from format.c. It is here in opt.c to call
 * HXparse_dequote_int.)
 */
hxmc_t *HXparse_dequote_fmt(const char *s, const char *end, const char **pptr)
{
	unsigned int level = 0; /* nesting */
	const char *i;
	char quot = '\0';
	hxmc_t *tmp;

	/* Search for end */
	for (i = s; *i != '\0'; ) {
		if (quot == '\0') {
			switch (*i) {
				case '"':
				case '\'':
					quot = *i++;
					continue;
				case '\\':
					if (i[1] != '\0')
						i += 2;
					continue;
				case C_OPEN:
					++level;
					++i;
					continue;
			}
			if (level == 0 && end != NULL &&
			    strchr(end, *i) != NULL)
				break;
			if (i[0] == C_CLOSE && level > 0)
				--level;
			++i;
			continue;
		}
		if (*i == quot) {
			quot = 0;
			++i;
			continue;
		} else if (*i == '\\') {
			if (*++i != '\0')
				++i;
			continue;
		}
		++i;
	}

	if (pptr != NULL)
		*pptr = i;
	tmp = HXmc_meminit(s, i - s);
	if (tmp == NULL)
		return NULL;
	HXparse_dequote_int(tmp, NULL);
	return tmp;
}

static int HX_getopt_error(int err, const char *key, unsigned int flags)
{
	switch (err) {
	case HXOPT_E_LONG_UNKNOWN:
		if (!(flags & HXOPT_QUIET))
			fprintf(stderr, "Unknown option: %s\n", key);
		return HXOPT_I_ERROR | HXOPT_ERR_UNKN;
	case HXOPT_E_LONG_TAKESVOID:
		if (!(flags & HXOPT_QUIET))
			fprintf(stderr, "Option %s does not take "
			        "any argument\n", key);
		return HXOPT_I_ERROR | HXOPT_ERR_VOID;
	case HXOPT_E_LONG_MISSING:
		if (!(flags & HXOPT_QUIET))
			fprintf(stderr, "Option %s requires an "
			        "argument\n", key);
		return HXOPT_I_ERROR | HXOPT_ERR_MIS;
	case HXOPT_E_SHORT_UNKNOWN:
		if (!(flags & HXOPT_QUIET))
			fprintf(stderr, "Unknown option: -%c\n", *key);
		return HXOPT_I_ERROR | HXOPT_ERR_UNKN;
	case HXOPT_E_SHORT_MISSING:
		if (!(flags & HXOPT_QUIET))
			fprintf(stderr, "Option -%c requires an "
			        "argument\n", *key);
		return HXOPT_I_ERROR | HXOPT_ERR_MIS;
	case HXOPT_E_AMBIG_PREFIX:
		if (!(flags & HXOPT_QUIET))
			fprintf(stderr, "Option %s is ambiguous\n", key);
		return HXOPT_I_ERROR | HXOPT_ERR_AMBIG;
	}
	return HXOPT_I_ERROR;
}

static int HX_getopt_twolong(const char *const *opt,
    struct HX_getopt_vars *par)
{
	const char *key = opt[0], *value = opt[1];

	par->cbi.current = lookup_long_pfx(par->cbi.table, key + 2);
	if (par->cbi.current == &HXopt_ambig_prefix)
		return HX_getopt_error(HXOPT_E_AMBIG_PREFIX, key, par->flags);
	if (par->cbi.current == NULL) {
		if (par->flags & HXOPT_PTHRU) {
			char *tmp = HX_strdup(key);
			if (tmp == NULL)
				return -errno;
			if (HXdeque_push(par->remaining, tmp) == NULL) {
				free(tmp);
				return -errno;
			}
			return HXOPT_S_NORMAL | HXOPT_I_ADVARG;
		}
		return HX_getopt_error(HXOPT_E_LONG_UNKNOWN, key, par->flags);
	}

	par->cbi.flags = HXOPTCB_BY_LONG;
	if (takes_void(par->cbi.current->type)) {
		par->cbi.data = NULL;
		return HXOPT_S_NORMAL | HXOPT_I_ASSIGN | HXOPT_I_ADVARG;
	} else if (par->cbi.current->type & HXOPT_OPTIONAL) {
		/* Rule: take arg if next thing is not-null, not-option. */
		if (value == NULL || *value != '-' ||
		    (value[0] == '-' && value[1] == '\0')) {
			/* --file -, --file bla */
			par->cbi.data = value;
			return HXOPT_S_NORMAL | HXOPT_I_ASSIGN | HXOPT_I_ADVARG2;
		} else {
			/* --file --another --file -- endofoptions */
			par->cbi.data = NULL;
			return HXOPT_S_NORMAL | HXOPT_I_ASSIGN | HXOPT_I_ADVARG;
		}
	} else {
		if (value == NULL)
			return HX_getopt_error(HXOPT_E_LONG_MISSING, key, par->flags);
		par->cbi.data = value;
		return HXOPT_S_NORMAL | HXOPT_I_ASSIGN | HXOPT_I_ADVARG2;
	}
}

static int HX_getopt_long(const char *cur, struct HX_getopt_vars *par)
{
	int ret;
	char *key, *value;

	key = HX_strdup(cur);
	if (key == NULL)
		return -errno;

	value = strchr(key, '=');
	*value++ = '\0';
	par->cbi.current = lookup_long_pfx(par->cbi.table, key + 2);
	if (par->cbi.current == &HXopt_ambig_prefix) {
		ret = HX_getopt_error(HXOPT_E_AMBIG_PREFIX, key, par->flags);
		free(key);
		return ret;
	}
	if (par->cbi.current == NULL) {
		if (par->flags & HXOPT_PTHRU) {
			/* Undo nuke of '=' and reuse alloc */
			value[-1] = '=';
			if (HXdeque_push(par->remaining, key) == NULL) {
				free(key);
				return -errno;
			}
			return HXOPT_S_NORMAL | HXOPT_I_ADVARG;
		}
		ret = HX_getopt_error(HXOPT_E_LONG_UNKNOWN, key, par->flags);
		free(key);
		return ret;
	}
	/*
	 * @value is always non-NULL when entering
	 * %HXOPT_S_LONG, so no need to check for !takes_void.
	 */
	if (takes_void(par->cbi.current->type)) {
		ret = HX_getopt_error(HXOPT_E_LONG_TAKESVOID, key, par->flags);
		free(key);
		return ret;
	}

	par->cbi.flags    = HXOPTCB_BY_LONG;
	par->cbi.data     = value;
	/* Not possible to use %HXOPT_I_ASSIGN due to transience of @key. */
	do_assign(&par->cbi, par->arg0);
	free(key);
	return HXOPT_S_NORMAL | HXOPT_I_ADVARG;
}

static int HX_getopt_short(const char *const *opt, const char *cur,
    struct HX_getopt_vars *par)
{
	char op = *cur;

	if (op == '\0')
		return HXOPT_S_NORMAL | HXOPT_I_ADVARG;

	par->cbi.current = lookup_short(par->cbi.table, op);
	if (par->cbi.current == NULL) {
		if (par->flags & HXOPT_PTHRU) {
			/*
			 * @cur-1 is always valid: it is either the previous
			 * char, or it is '-'.
			 */
			char *buf = HX_strdup(cur - 1);
			if (buf != NULL)
				*buf = '-';
			if (HXdeque_push(par->remaining, buf) == NULL) {
				free(buf);
				return -errno;
			}
			return HXOPT_S_NORMAL | HXOPT_I_ADVARG;
		}
		return HX_getopt_error(HXOPT_E_SHORT_UNKNOWN, &op, par->flags);
	}

	par->cbi.flags = HXOPTCB_BY_SHORT;
	if (takes_void(par->cbi.current->type)) {
		/* -A */
		par->cbi.data = NULL;
		return HXOPT_S_SHORT | HXOPT_I_ASSIGN | HXOPT_I_ADVCHAR;
	} else if (cur[1] != '\0') {
		/* -Avalue */
		par->cbi.data = cur + 1;
		return HXOPT_S_NORMAL | HXOPT_I_ASSIGN | HXOPT_I_ADVARG;
	}

	cur = *++opt;
	if (par->cbi.current->type & HXOPT_OPTIONAL) {
		if (cur == NULL || *cur != '-' ||
		    (cur[0] == '-' && cur[1] == '\0')) {
			/* -f - -f bla */
			par->cbi.data = cur;
			return HXOPT_S_NORMAL | HXOPT_I_ASSIGN | HXOPT_I_ADVARG2;
		} else {
			/* -f -a-file --another --file -- endofoptions */
			par->cbi.data = NULL;
			return HXOPT_S_NORMAL | HXOPT_I_ASSIGN | HXOPT_I_ADVARG;
		}
	} else {
		/* -A value */
		if (cur == NULL)
			return HX_getopt_error(HXOPT_E_SHORT_MISSING, &op, par->flags);
		par->cbi.data = cur;
		return HXOPT_S_NORMAL | HXOPT_I_ASSIGN | HXOPT_I_ADVARG2;
	}
}

static int HX_getopt_term(const char *cur, const struct HX_getopt_vars *par)
{
	char *tmp = HX_strdup(cur);
	if (tmp == NULL)
		return -errno;
	if (HXdeque_push(par->remaining, tmp) == NULL) {
		free(tmp);
		return -errno;
	}
	return HXOPT_S_TERMINATED | HXOPT_I_ADVARG;
}

static int HX_getopt_normal(const char *cur, const struct HX_getopt_vars *par)
{
	if (cur[0] == '-' && cur[1] == '\0') {
		/* Note to popt developers: A single dash is NOT an option! */
		HXdeque_push(par->remaining, HX_strdup(cur));
		return HXOPT_S_NORMAL | HXOPT_I_ADVARG;
	}
	if (cur[0] == '-' && cur[1] == '-' && cur[2] == '\0') {
		/*
		 * Double dash. If passthrough is on, "--" must be copied into
		 * @remaining. This is done in the next round.
		 */
		if (!(par->flags & HXOPT_PTHRU))
			return HXOPT_S_TERMINATED | HXOPT_I_ADVARG;
		return HXOPT_S_TERMINATED;
	}
	if (cur[0] == '-' && cur[1] == '-') { /* long option */
		if (strchr(cur + 2, '=') == NULL)
			return HXOPT_S_TWOLONG;
		/* Single argument long option: --long=arg */
		return HXOPT_S_LONG;
	}
	if (cur[0] == '-')
		/* Short option(s) - one or more(!) */
		return HXOPT_S_SHORT | HXOPT_I_ADVCHAR;
	if (par->flags & HXOPT_RQ_ORDER)
		/* POSIX: first non-option implies option termination */
		return HXOPT_S_TERMINATED;
	cur = HX_strdup(cur);
	if (cur == NULL || HXdeque_push(par->remaining, cur) == NULL)
		return -errno;
	return HXOPT_S_NORMAL | HXOPT_I_ADVARG;
}

EXPORT_SYMBOL int HX_getopt(const struct HXoption *table, int *argc,
    const char ***argv, unsigned int flags)
{
	struct HX_getopt_vars ps;
	const char **opt = *argv;
	int state = HXOPT_S_NORMAL;
	int ret = HXOPT_ERR_SUCCESS;
	unsigned int argk;
	const char *cur;

	memset(&ps, 0, sizeof(ps));
	ps.remaining = HXdeque_init();
	if (ps.remaining == NULL)
		goto out;
	ps.flags = flags;
	ps.arg0  = **argv;
	ps.cbi.table = table;

	if (*opt != NULL) {
		/* put argv[0] back */
		char *arg = HX_strdup(*opt++);
		if (arg == NULL)
			goto out_errno;
		if (HXdeque_push(ps.remaining, arg) == NULL) {
			free(arg);
			goto out_errno;
		}
	}

	if (posix_me_harder())
		ps.flags |= HXOPT_RQ_ORDER;

	for (cur = *opt; cur != NULL; ) {
		if (state == HXOPT_S_TWOLONG)
			state = HX_getopt_twolong(opt, &ps);
		else if (state == HXOPT_S_LONG)
			state = HX_getopt_long(cur, &ps);
		else if (state == HXOPT_S_SHORT)
			state = HX_getopt_short(opt, cur, &ps);
		else if (state == HXOPT_S_TERMINATED)
			state = HX_getopt_term(cur, &ps);
		else if (state == HXOPT_S_NORMAL)
			state = HX_getopt_normal(cur, &ps);

		if (state < 0) {
			ret = state;
			break;
		}
		if (state & HXOPT_I_ERROR) {
			ret = state & ~HXOPT_I_ERROR;
			break;
		}
		if (state & HXOPT_I_ASSIGN)
			do_assign(&ps.cbi, ps.arg0);
		if (state & HXOPT_I_ADVARG)
			cur = *++opt;
		else if (state & HXOPT_I_ADVARG2)
			cur = *(opt += 2);
		else if (state & HXOPT_I_ADVCHAR)
			++cur;
		state &= ~HXOPT_I_MASK;
	}

 out:
	if (ret == HXOPT_ERR_SUCCESS) {
		const char **nvec = reinterpret_cast(const char **,
		                    HXdeque_to_vec(ps.remaining, &argk));
		if (nvec == NULL)
			goto out_errno;
		if (ps.flags & HXOPT_DESTROY_OLD)
			/*
			 * Only the "true, original" argv is stored on the
			 * stack - the argv that HX_getopt() produces is on
			 * the heap, so the %HXOPT_DESTROY_OLD flag should be
			 * passed when you use passthrough chaining, i.e. all
			 * but the first call to HX_getopt() should have this
			 * set.
			 */
			HX_zvecfree(const_cast2(char **, *argv));

		*argv = nvec;
		if (argc != NULL)
			*argc = argk;
	} else if (ret < 0) {
		if (!(ps.flags & HXOPT_QUIET))
			fprintf(stderr, "%s: %s\n", __func__, strerror(errno));
	} else {
		ps.cbi.data = ps.arg0;
		if (ps.flags & HXOPT_HELPONERR)
			HX_getopt_help(&ps.cbi, stderr);
		else if (ps.flags & HXOPT_USAGEONERR)
			HX_getopt_usage(&ps.cbi, stderr);
	}

	HXdeque_free(ps.remaining);
	return ret;

 out_errno:
	ret = -errno;
	goto out;
}

EXPORT_SYMBOL void HX_getopt_help(const struct HXoptcb *cbi, FILE *nfp)
{
	FILE *fp = (nfp == NULL) ? stderr : nfp;
	const struct HXoption *travp;
	char tmp[84] = {'\0'};
	unsigned int tw = 0;

	HX_getopt_usage(cbi, nfp);

	/* Find maximum indent */
	for (travp = cbi->table; travp->type != HXTYPE_XSNTMARK; ++travp) {
		size_t tl;

		opt_to_text(travp, tmp, sizeof(tmp), W_EQUAL);
		if ((tl = strlen(tmp)) > tw)
			tw = tl;
	}

	/* Print table */
	for (travp = cbi->table; travp->type != HXTYPE_XSNTMARK; ++travp) {
		opt_to_text(travp, tmp, sizeof(tmp), W_NONE);
		fprintf(fp, "  %-*s    ", static_cast(int, tw), tmp);
		if (travp->help == NULL)
			fprintf(fp, "\n");
		else
			print_indent(travp->help, tw + 6, fp);
	}
}

EXPORT_SYMBOL void HX_getopt_help_cb(const struct HXoptcb *cbi)
{
	HX_getopt_help(cbi, stdout);
	exit(EXIT_SUCCESS);
}

EXPORT_SYMBOL void HX_getopt_usage(const struct HXoptcb *cbi, FILE *nfp)
{
	size_t wd, tw = 0;
	FILE *fp = (nfp == NULL) ? stderr : nfp;
	const struct HXoption *travp;
	char tmp[84] = {};
	/* Program name now expected in .data */
	const char *arg0 = cbi->data;

	if (arg0 == NULL || *arg0 == '\0')
		arg0 = "($0)";

	wd = sizeof("Usage:") + strlen(arg0);
	fprintf(fp, "Usage: %s", arg0);

	/* Short-only flags */
	if (wd + 5 > SCREEN_WIDTH) {
		/* 5 is the minimum size for a new starting option, " [-X]" */
		fprintf(fp, "\n     ");
		wd = 6;
	}
	for (travp = cbi->table; travp->type != HXTYPE_XSNTMARK; ++travp) {
		if (!(travp->ln == NULL && travp->sh != '\0' &&
		    takes_void(travp->type)))
			continue;
		if (*tmp == '\0') {
			snprintf(tmp, sizeof(tmp), " [-"); /* ] */
			tw = 3;
		}
		tmp[tw++] = travp->sh;
		if (wd + tw + 1 > SCREEN_WIDTH) {
			tmp[tw++] = /* [ */ ']';
			tmp[tw]   = '\0';
			fprintf(fp, "%s\n      ", tmp);
			wd   = 6;
			*tmp = '\0';
		}
	}
	if (*tmp != '\0') {
		tmp[tw++] = ']';
		tmp[tw]   = '\0';
		wd += fprintf(fp, "%s", tmp);
	}

	/* Any other args */
	for (travp = cbi->table; travp->type != HXTYPE_XSNTMARK; ++travp) {
		if (travp->ln == NULL && travp->sh != '\0' &&
		    takes_void(travp->type))
			continue;

		opt_to_text(travp, tmp, sizeof(tmp),
		            W_SPACE | W_BRACKET | W_ALT);
		if (wd + strlen(tmp) > SCREEN_WIDTH) {
			fprintf(fp, "\n      ");
			wd = 6;
		}
		wd += fprintf(fp, "%s", tmp);
	}

	fprintf(fp, "\n");
}

EXPORT_SYMBOL void HX_getopt_usage_cb(const struct HXoptcb *cbi)
{
	HX_getopt_usage(cbi, stdout);
	exit(EXIT_SUCCESS);
}

static void HX_shconf_break(void *ptr, char *line,
    void (*cb)(void *, const char *, const char *))
{
	char *lp = line, *key, *val;
	HX_chomp(line);

	while (lp != NULL) {
		while (HX_isspace(*lp) || *lp == ';')
			++lp;
		/* Next entry if comment, empty line or no value */
		if (*lp == '#' || *lp == '\0')
			return;
		if (!HX_isalpha(*lp) && *lp != '_')
			/* Variables ought to start with [A-Z_] */
			return;
		key = lp;
		while (HX_isalnum(*lp) || *lp == '_')
			++lp;
		if (*lp != '=')
			/* Variable name contained something not in [A-Z0-9_] */
			return;
		*lp++ = '\0';
		val = lp;

		/* Handle escape codes and quotes, and assign to TAB entry */
		lp = HXparse_dequote_int(val, "\t\n ;");
		(*cb)(ptr, key, val);
	}
}

static void HX_shconf_assign(void *table, const char *key, const char *value)
{
	struct HXoptcb cbi = {
		.table = table,
		.flags = HXOPTCB_BY_LONG,
		.data  = value,
	};

	if ((cbi.current = lookup_long(table, key)) == NULL)
		return;
	do_assign(&cbi, NULL);
}

EXPORT_SYMBOL int HX_shconfig(const char *file, const struct HXoption *table)
{
	hxmc_t *ln = NULL;
	FILE *fp;

	if ((fp = fopen(file, "r")) == NULL)
		return -errno;

	while (HX_getl(&ln, fp) != NULL)
		HX_shconf_break(const_cast(void *,
			static_cast(const void *, table)), ln,
			HX_shconf_assign);

	HXmc_free(ln);
	fclose(fp);
	return 1;
}

static void HX_shconf_assignmp(void *map, const char *key, const char *value)
{
	HXmap_add(map, key, value);
}

EXPORT_SYMBOL struct HXmap *HX_shconfig_map(const char *file)
{
	struct HXmap *map;
	hxmc_t *ln = NULL;
	FILE *fp;

	map = HXmap_init(HXMAPT_DEFAULT, HXMAP_SCKEY | HXMAP_SCDATA);
	if (map == NULL)
		return NULL;

	if ((fp = fopen(file, "r")) == NULL) {
		int saved_errno = errno;
		HXmap_free(map);
		errno = saved_errno;
		return NULL;
	}

	while (HX_getl(&ln, fp) != NULL)
		HX_shconf_break(map, ln, HX_shconf_assignmp);

	HXmc_free(ln);
	fclose(fp);
	return map;
}

EXPORT_SYMBOL int HX_shconfig_pv(const char **path, const char *file,
    const struct HXoption *table, unsigned int flags)
{
	char buf[MAXFNLEN];
	int ret = 0;

	for (; *path != NULL; ++path) {
		int v;
		snprintf(buf, sizeof(buf), "%s/%s", *path, file);
		v = HX_shconfig(buf, table);
		if (v > 0) {
			++ret;
			if (flags & SHCONF_ONE)
				break;
		}
	}

	return ret;
}

EXPORT_SYMBOL void HX_shconfig_free(const struct HXoption *table)
{
	for (; table->ln != NULL; ++table) {
		char **ptr = table->ptr;
		if (table->type == HXTYPE_STRING &&
		    ptr != NULL && *ptr != NULL)
			free(*ptr);
	}
}