forked from gwsw/less
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.c
782 lines (733 loc) · 16.1 KB
/
option.c
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
/*
* Copyright (C) 1984-2024 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* Process command line options.
*
* Each option is a single letter which controls a program variable.
* The options have defaults which may be changed via
* the command line option, toggled via the "-" command,
* or queried via the "_" command.
*/
#include "less.h"
#include "option.h"
static struct loption *pendopt;
public lbool plusoption = FALSE;
static constant char *optstring(constant char *s, char **p_str, constant char *printopt, constant char *validchars);
static int flip_triple(int val, int lc);
extern int less_is_more;
extern int quit_at_eof;
extern char *every_first_cmd;
extern int opt_use_backslash;
/*
* Return a printable description of an option.
*/
static constant char * opt_desc(struct loption *o)
{
static char buf[OPTNAME_MAX + 10];
if (o->oletter == OLETTER_NONE)
SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname);
else
SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname);
return (buf);
}
/*
* Return a string suitable for printing as the "name" of an option.
* For example, if the option letter is 'x', just return "-x".
*/
public constant char * propt(char c)
{
static char buf[MAX_PRCHAR_LEN+2];
sprintf(buf, "-%s", prchar((LWCHAR) c));
return (buf);
}
/*
* Scan an argument (either from the command line or from the
* LESS environment variable) and process it.
*/
public void scan_option(constant char *s)
{
struct loption *o;
char optc;
constant char *optname;
constant char *printopt;
char *str;
lbool set_default;
int lc;
lbool ambig;
PARG parg;
if (s == NULL)
return;
/*
* If we have a pending option which requires an argument,
* handle it now.
* This happens if the previous option was, for example, "-P"
* without a following string. In that case, the current
* option is simply the argument for the previous option.
*/
if (pendopt != NULL)
{
if (!(pendopt->otype & UNSUPPORTED))
{
switch (pendopt->otype & OTYPE)
{
case STRING:
(*pendopt->ofunc)(INIT, s);
break;
case NUMBER:
printopt = opt_desc(pendopt);
*(pendopt->ovar) = getnumc(&s, printopt, NULL);
break;
}
}
pendopt = NULL;
return;
}
set_default = FALSE;
optname = NULL;
while (*s != '\0')
{
/*
* Check some special cases first.
*/
switch (optc = *s++)
{
case ' ':
case '\t':
case END_OPTION_STRING:
continue;
case '-':
/*
* "--" indicates an option name instead of a letter.
*/
if (*s == '-')
optname = ++s;
/*
* "-+" or "--+" means set these options back to their defaults.
* (They may have been set otherwise by previous options.)
*/
set_default = (*s == '+');
if (set_default)
s++;
if (optname != NULL)
{
optname = s;
break;
}
continue;
case '+':
/*
* An option prefixed by a "+" is ungotten, so
* that it is interpreted as less commands
* processed at the start of the first input file.
* "++" means process the commands at the start of
* EVERY input file.
*/
plusoption = TRUE;
s = optstring(s, &str, propt('+'), NULL);
if (s == NULL)
return;
if (*str == '+')
{
if (every_first_cmd != NULL)
free(every_first_cmd);
every_first_cmd = save(str+1);
} else
{
ungetsc(str);
ungetcc_end_command();
}
free(str);
continue;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
/*
* Special "more" compatibility form "-<number>"
* instead of -z<number> to set the scrolling
* window size.
*/
s--;
optc = 'z';
break;
case 'n':
if (less_is_more)
optc = 'z';
break;
}
/*
* Not a special case.
* Look up the option letter in the option table.
*/
ambig = FALSE;
if (optname == NULL)
{
printopt = propt(optc);
lc = ASCII_IS_LOWER(optc);
o = findopt(optc);
} else
{
printopt = optname;
lc = ASCII_IS_LOWER(optname[0]);
o = findopt_name(&optname, NULL, &ambig);
s = optname;
optname = NULL;
if (*s == '\0' || *s == ' ')
{
/*
* The option name matches exactly.
*/
;
} else if (*s == '=')
{
/*
* The option name is followed by "=value".
*/
if (o != NULL &&
(o->otype & OTYPE) != STRING &&
(o->otype & OTYPE) != NUMBER)
{
parg.p_string = printopt;
error("The %s option should not be followed by =",
&parg);
return;
}
s++;
} else
{
/*
* The specified name is longer than the
* real option name.
*/
o = NULL;
}
}
if (o == NULL)
{
parg.p_string = printopt;
if (ambig)
error("%s is an ambiguous abbreviation (\"less --help\" for help)",
&parg);
else
error("There is no %s option (\"less --help\" for help)",
&parg);
return;
}
str = NULL;
switch (o->otype & OTYPE)
{
case BOOL:
if (o->otype & UNSUPPORTED)
break;
if (o->ovar != NULL)
{
if (set_default)
*(o->ovar) = o->odefault;
else
*(o->ovar) = ! o->odefault;
}
break;
case TRIPLE:
if (o->otype & UNSUPPORTED)
break;
if (o->ovar != NULL)
{
if (set_default)
*(o->ovar) = o->odefault;
else
*(o->ovar) = flip_triple(o->odefault, lc);
}
break;
case STRING:
if (*s == '\0')
{
/*
* Set pendopt and return.
* We will get the string next time
* scan_option is called.
*/
pendopt = o;
return;
}
/*
* Don't do anything here.
* All processing of STRING options is done by
* the handling function.
*/
while (*s == ' ')
s++;
s = optstring(s, &str, printopt, o->odesc[1]);
if (s == NULL)
return;
break;
case NUMBER:
if (*s == '\0')
{
pendopt = o;
return;
}
if (o->otype & UNSUPPORTED)
break;
*(o->ovar) = getnumc(&s, printopt, NULL);
break;
}
/*
* If the option has a handling function, call it.
*/
if (o->ofunc != NULL && !(o->otype & UNSUPPORTED))
(*o->ofunc)(INIT, str);
if (str != NULL)
free(str);
}
}
/*
* Toggle command line flags from within the program.
* Used by the "-" and "_" commands.
* how_toggle may be:
* OPT_NO_TOGGLE just report the current setting, without changing it.
* OPT_TOGGLE invert the current setting
* OPT_UNSET set to the default value
* OPT_SET set to the inverse of the default value
*/
public void toggle_option(struct loption *o, int lower, constant char *s, int how_toggle)
{
int num;
int no_prompt;
lbool err;
PARG parg;
no_prompt = (how_toggle & OPT_NO_PROMPT);
how_toggle &= ~OPT_NO_PROMPT;
if (o == NULL)
{
error("No such option", NULL_PARG);
return;
}
if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
{
parg.p_string = opt_desc(o);
error("Cannot change the %s option", &parg);
return;
}
if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
{
parg.p_string = opt_desc(o);
error("Cannot query the %s option", &parg);
return;
}
/*
* Check for something which appears to be a do_toggle
* (because the "-" command was used), but really is not.
* This could be a string option with no string, or
* a number option with no number.
*/
switch (o->otype & OTYPE)
{
case STRING:
case NUMBER:
if (how_toggle == OPT_TOGGLE && *s == '\0')
how_toggle = OPT_NO_TOGGLE;
break;
}
#if HILITE_SEARCH
if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
repaint_hilite(FALSE);
#endif
/*
* Now actually toggle (change) the variable.
*/
if (how_toggle != OPT_NO_TOGGLE)
{
switch (o->otype & OTYPE)
{
case BOOL:
/*
* Boolean.
*/
if (o->ovar != NULL)
{
switch (how_toggle)
{
case OPT_TOGGLE:
*(o->ovar) = ! *(o->ovar);
break;
case OPT_UNSET:
*(o->ovar) = o->odefault;
break;
case OPT_SET:
*(o->ovar) = ! o->odefault;
break;
}
}
break;
case TRIPLE:
/*
* Triple:
* If user gave the lower case letter, then switch
* to 1 unless already 1, in which case make it 0.
* If user gave the upper case letter, then switch
* to 2 unless already 2, in which case make it 0.
*/
if (o->ovar != NULL)
{
switch (how_toggle)
{
case OPT_TOGGLE:
*(o->ovar) = flip_triple(*(o->ovar), lower);
break;
case OPT_UNSET:
*(o->ovar) = o->odefault;
break;
case OPT_SET:
*(o->ovar) = flip_triple(o->odefault, lower);
break;
}
}
break;
case STRING:
/*
* String: don't do anything here.
* The handling function will do everything.
*/
switch (how_toggle)
{
case OPT_SET:
case OPT_UNSET:
error("Cannot use \"-+\" or \"--\" for a string option",
NULL_PARG);
return;
}
break;
case NUMBER:
/*
* Number: set the variable to the given number.
*/
switch (how_toggle)
{
case OPT_TOGGLE:
num = getnumc(&s, NULL, &err);
if (!err)
*(o->ovar) = num;
break;
case OPT_UNSET:
*(o->ovar) = o->odefault;
break;
case OPT_SET:
error("Can't use \"-!\" for a numeric option",
NULL_PARG);
return;
}
break;
}
}
/*
* Call the handling function for any special action
* specific to this option.
*/
if (o->ofunc != NULL)
(*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
#if HILITE_SEARCH
if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
chg_hilite();
#endif
if (!no_prompt)
{
/*
* Print a message describing the new setting.
*/
switch (o->otype & OTYPE)
{
case BOOL:
case TRIPLE:
/*
* Print the odesc message.
*/
if (o->ovar != NULL)
error(o->odesc[*(o->ovar)], NULL_PARG);
break;
case NUMBER:
/*
* The message is in odesc[1] and has a %d for
* the value of the variable.
*/
parg.p_int = *(o->ovar);
error(o->odesc[1], &parg);
break;
case STRING:
/*
* Message was already printed by the handling function.
*/
break;
}
}
if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
screen_trashed();
}
/*
* "Toggle" a triple-valued option.
*/
static int flip_triple(int val, int lc)
{
if (lc)
return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
else
return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
}
/*
* Determine if an option takes a parameter.
*/
public int opt_has_param(struct loption *o)
{
if (o == NULL)
return (0);
if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE))
return (0);
return (1);
}
/*
* Return the prompt to be used for a given option letter.
* Only string and number valued options have prompts.
*/
public constant char * opt_prompt(struct loption *o)
{
if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
return ("?");
return (o->odesc[0]);
}
/*
* If the specified option can be toggled, return NULL.
* Otherwise return an appropriate error message.
*/
public constant char * opt_toggle_disallowed(int c)
{
switch (c)
{
case 'o':
if (ch_getflags() & CH_CANSEEK)
return "Input is not a pipe";
break;
}
return NULL;
}
/*
* Return whether or not there is a string option pending;
* that is, if the previous option was a string-valued option letter
* (like -P) without a following string.
* In that case, the current option is taken to be the string for
* the previous option.
*/
public lbool isoptpending(void)
{
return (pendopt != NULL);
}
/*
* Print error message about missing string.
*/
static void nostring(constant char *printopt)
{
PARG parg;
parg.p_string = printopt;
error("Value is required after %s", &parg);
}
/*
* Print error message if a STRING type option is not followed by a string.
*/
public void nopendopt(void)
{
nostring(opt_desc(pendopt));
}
/*
* Scan to end of string or to an END_OPTION_STRING character.
* In the latter case, replace the char with a null char.
* Return a pointer to the remainder of the string, if any.
* validchars is of the form "[-][.]d[,]".
* "-" means an optional leading "-" is allowed
* "." means an optional leading "." is allowed (after any "-")
* "d" indicates a string of one or more digits (0-9)
* "," indicates a comma-separated list of digit strings is allowed
* "s" means a space char terminates the argument
*/
static constant char * optstring(constant char *s, char **p_str, constant char *printopt, constant char *validchars)
{
constant char *p;
char *out;
if (*s == '\0')
{
nostring(printopt);
return (NULL);
}
/* Alloc could be more than needed, but not worth trimming. */
*p_str = (char *) ecalloc(strlen(s)+1, sizeof(char));
out = *p_str;
for (p = s; *p != '\0'; p++)
{
if (opt_use_backslash && *p == '\\' && p[1] != '\0')
{
/* Take next char literally. */
++p;
} else
{
if (validchars != NULL)
{
if (validchars[0] == 's')
{
if (*p == ' ')
break;
} else if (*p == '-')
{
if (validchars[0] != '-')
break;
++validchars;
} else if (*p == '.')
{
if (validchars[0] == '-')
++validchars;
if (validchars[0] != '.')
break;
++validchars;
} else if (*p == ',')
{
if (validchars[0] == '\0' || validchars[1] != ',')
break;
} else if (*p >= '0' && *p <= '9')
{
while (validchars[0] == '-' || validchars[0] == '.')
++validchars;
if (validchars[0] != 'd')
break;
} else
break;
}
if (*p == END_OPTION_STRING)
/* End of option string. */
break;
}
*out++ = *p;
}
*out = '\0';
return (p);
}
/*
*/
static int num_error(constant char *printopt, lbool *errp, lbool overflow)
{
PARG parg;
if (errp != NULL)
{
*errp = TRUE;
return (-1);
}
if (printopt != NULL)
{
parg.p_string = printopt;
error((overflow
? "Number too large in '%s'"
: "Number is required after %s"),
&parg);
}
return (-1);
}
/*
* Translate a string into a number.
* Like atoi(), but takes a pointer to a char *, and updates
* the char * to point after the translated number.
*/
public int getnumc(constant char **sp, constant char *printopt, lbool *errp)
{
constant char *s = *sp;
int n;
lbool neg;
s = skipspc(s);
neg = FALSE;
if (*s == '-')
{
neg = TRUE;
s++;
}
if (*s < '0' || *s > '9')
return (num_error(printopt, errp, FALSE));
n = lstrtoic(s, sp, 10);
if (n < 0)
return (num_error(printopt, errp, TRUE));
if (errp != NULL)
*errp = FALSE;
if (neg)
n = -n;
return (n);
}
public int getnum(char **sp, constant char *printopt, lbool *errp)
{
constant char *cs = *sp;
int r = getnumc(&cs, printopt, errp);
*sp = (char *) cs;
return r;
}
/*
* Translate a string into a fraction, represented by the part of a
* number which would follow a decimal point.
* The value of the fraction is returned as parts per NUM_FRAC_DENOM.
* That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
*/
public long getfraction(constant char **sp, constant char *printopt, lbool *errp)
{
constant char *s;
long frac = 0;
int fraclen = 0;
s = skipspc(*sp);
if (*s < '0' || *s > '9')
return (num_error(printopt, errp, FALSE));
for ( ; *s >= '0' && *s <= '9'; s++)
{
if (NUM_LOG_FRAC_DENOM <= fraclen)
continue;
frac = (frac * 10) + (*s - '0');
fraclen++;
}
while (fraclen++ < NUM_LOG_FRAC_DENOM)
frac *= 10;
*sp = s;
if (errp != NULL)
*errp = FALSE;
return (frac);
}
/*
* Set the UNSUPPORTED bit in every option listed
* in the LESS_UNSUPPORT environment variable.
*/
public void init_unsupport(void)
{
constant char *s = lgetenv("LESS_UNSUPPORT");
if (isnullenv(s))
return;
for (;;)
{
struct loption *opt;
s = skipspc(s);
if (*s == '\0') break;
if (*s == '-' && *++s == '\0') break;
if (*s == '-') /* long option name */
{
++s;
opt = findopt_name(&s, NULL, NULL);
} else /* short (single-char) option */
{
opt = findopt(*s);
if (opt != NULL) ++s;
}
if (opt != NULL)
opt->otype |= UNSUPPORTED;
}
}
/*
* Get the value of the -e flag.
*/
public int get_quit_at_eof(void)
{
if (!less_is_more)
return quit_at_eof;
/* When less_is_more is set, the -e flag semantics are different. */
return quit_at_eof ? OPT_ONPLUS : OPT_ON;
}