Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / less / dist / optfunc.c
blobbc6fe590d64520506eb34f3a75456863486dc321
1 /* $NetBSD: optfunc.c,v 1.3 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * Handling functions for command line options.
16 * Most options are handled by the generic code in option.c.
17 * But all string options, and a few non-string options, require
18 * special handling specific to the particular option.
19 * This special processing is done by the "handling functions" in this file.
21 * Each handling function is passed a "type" and, if it is a string
22 * option, the string which should be "assigned" to the option.
23 * The type may be one of:
24 * INIT The option is being initialized from the command line.
25 * TOGGLE The option is being changed from within the program.
26 * QUERY The setting of the option is merely being queried.
29 #include "less.h"
30 #include "option.h"
32 extern int nbufs;
33 extern int bufspace;
34 extern int pr_type;
35 extern int plusoption;
36 extern int swindow;
37 extern int sc_width;
38 extern int sc_height;
39 extern int secure;
40 extern int dohelp;
41 extern int any_display;
42 extern char openquote;
43 extern char closequote;
44 extern char *prproto[];
45 extern char *eqproto;
46 extern char *hproto;
47 extern char *wproto;
48 extern IFILE curr_ifile;
49 extern char version[];
50 extern int jump_sline;
51 extern int jump_sline_fraction;
52 extern int shift_count;
53 extern int shift_count_fraction;
54 extern int less_is_more;
55 #if LOGFILE
56 extern char *namelogfile;
57 extern int force_logfile;
58 extern int logfile;
59 #endif
60 #if TAGS
61 public char *tagoption = NULL;
62 extern char *tags;
63 #endif
64 #if MSDOS_COMPILER
65 extern int nm_fg_color, nm_bg_color;
66 extern int bo_fg_color, bo_bg_color;
67 extern int ul_fg_color, ul_bg_color;
68 extern int so_fg_color, so_bg_color;
69 extern int bl_fg_color, bl_bg_color;
70 #endif
73 #if LOGFILE
75 * Handler for -o option.
77 public void
78 opt_o(type, s)
79 int type;
80 char *s;
82 PARG parg;
84 if (secure)
86 error("log file support is not available", NULL_PARG);
87 return;
89 switch (type)
91 case INIT:
92 namelogfile = s;
93 break;
94 case TOGGLE:
95 if (ch_getflags() & CH_CANSEEK)
97 error("Input is not a pipe", NULL_PARG);
98 return;
100 if (logfile >= 0)
102 error("Log file is already in use", NULL_PARG);
103 return;
105 s = skipsp(s);
106 namelogfile = lglob(s);
107 use_logfile(namelogfile);
108 sync_logfile();
109 break;
110 case QUERY:
111 if (logfile < 0)
112 error("No log file", NULL_PARG);
113 else
115 parg.p_string = namelogfile;
116 error("Log file \"%s\"", &parg);
118 break;
123 * Handler for -O option.
125 public void
126 opt__O(type, s)
127 int type;
128 char *s;
130 force_logfile = TRUE;
131 opt_o(type, s);
133 #endif
136 * Handlers for -j option.
138 public void
139 opt_j(type, s)
140 int type;
141 char *s;
143 PARG parg;
144 char buf[16];
145 int len;
146 int err;
148 switch (type)
150 case INIT:
151 case TOGGLE:
152 if (*s == '.')
154 s++;
155 jump_sline_fraction = getfraction(&s, "j", &err);
156 if (err)
157 error("Invalid line fraction", NULL_PARG);
158 else
159 calc_jump_sline();
160 } else
162 int sline = getnum(&s, "j", &err);
163 if (err)
164 error("Invalid line number", NULL_PARG);
165 else
167 jump_sline = sline;
168 jump_sline_fraction = -1;
171 break;
172 case QUERY:
173 if (jump_sline_fraction < 0)
175 parg.p_int = jump_sline;
176 error("Position target at screen line %d", &parg);
177 } else
180 sprintf(buf, ".%06d", jump_sline_fraction);
181 len = strlen(buf);
182 while (len > 2 && buf[len-1] == '0')
183 len--;
184 buf[len] = '\0';
185 parg.p_string = buf;
186 error("Position target at screen position %s", &parg);
188 break;
192 public void
193 calc_jump_sline()
195 if (jump_sline_fraction < 0)
196 return;
197 jump_sline = sc_height * jump_sline_fraction / NUM_FRAC_DENOM;
201 * Handlers for -# option.
203 public void
204 opt_shift(type, s)
205 int type;
206 char *s;
208 PARG parg;
209 char buf[16];
210 int len;
211 int err;
213 switch (type)
215 case INIT:
216 case TOGGLE:
217 if (*s == '.')
219 s++;
220 shift_count_fraction = getfraction(&s, "#", &err);
221 if (err)
222 error("Invalid column fraction", NULL_PARG);
223 else
224 calc_shift_count();
225 } else
227 int hs = getnum(&s, "#", &err);
228 if (err)
229 error("Invalid column number", NULL_PARG);
230 else
232 shift_count = hs;
233 shift_count_fraction = -1;
236 break;
237 case QUERY:
238 if (shift_count_fraction < 0)
240 parg.p_int = shift_count;
241 error("Horizontal shift %d columns", &parg);
242 } else
245 sprintf(buf, ".%06d", shift_count_fraction);
246 len = strlen(buf);
247 while (len > 2 && buf[len-1] == '0')
248 len--;
249 buf[len] = '\0';
250 parg.p_string = buf;
251 error("Horizontal shift %s of screen width", &parg);
253 break;
256 public void
257 calc_shift_count()
259 if (shift_count_fraction < 0)
260 return;
261 shift_count = sc_width * shift_count_fraction / NUM_FRAC_DENOM;
264 #if USERFILE
265 public void
266 opt_k(type, s)
267 int type;
268 char *s;
270 PARG parg;
272 switch (type)
274 case INIT:
275 if (lesskey(s, 0))
277 parg.p_string = s;
278 error("Cannot use lesskey file \"%s\"", &parg);
280 break;
283 #endif
285 #if TAGS
287 * Handler for -t option.
289 public void
290 opt_t(type, s)
291 int type;
292 char *s;
294 IFILE save_ifile;
295 POSITION pos;
297 switch (type)
299 case INIT:
300 tagoption = s;
301 /* Do the rest in main() */
302 break;
303 case TOGGLE:
304 if (secure)
306 error("tags support is not available", NULL_PARG);
307 break;
309 findtag(skipsp(s));
310 save_ifile = save_curr_ifile();
312 * Try to open the file containing the tag
313 * and search for the tag in that file.
315 if (edit_tagfile() || (pos = tagsearch()) == NULL_POSITION)
317 /* Failed: reopen the old file. */
318 reedit_ifile(save_ifile);
319 break;
321 unsave_ifile(save_ifile);
322 jump_loc(pos, jump_sline);
323 break;
328 * Handler for -T option.
330 public void
331 opt__T(type, s)
332 int type;
333 char *s;
335 PARG parg;
337 switch (type)
339 case INIT:
340 tags = s;
341 break;
342 case TOGGLE:
343 s = skipsp(s);
344 tags = lglob(s);
345 break;
346 case QUERY:
347 parg.p_string = tags;
348 error("Tags file \"%s\"", &parg);
349 break;
352 #endif
355 * Handler for -p option.
357 public void
358 opt_p(type, s)
359 int type;
360 register char *s;
362 switch (type)
364 case INIT:
366 * Unget a search command for the specified string.
367 * {{ This won't work if the "/" command is
368 * changed or invalidated by a .lesskey file. }}
370 plusoption = TRUE;
371 ungetsc(s);
373 * In "more" mode, the -p argument is a command,
374 * not a search string, so we don't need a slash.
376 if (!less_is_more)
377 ungetsc("/");
378 break;
383 * Handler for -P option.
385 public void
386 opt__P(type, s)
387 int type;
388 register char *s;
390 register char **proto;
391 PARG parg;
393 switch (type)
395 case INIT:
396 case TOGGLE:
398 * Figure out which prototype string should be changed.
400 switch (*s)
402 case 's': proto = &prproto[PR_SHORT]; s++; break;
403 case 'm': proto = &prproto[PR_MEDIUM]; s++; break;
404 case 'M': proto = &prproto[PR_LONG]; s++; break;
405 case '=': proto = &eqproto; s++; break;
406 case 'h': proto = &hproto; s++; break;
407 case 'w': proto = &wproto; s++; break;
408 default: proto = &prproto[PR_SHORT]; break;
410 free(*proto);
411 *proto = save(s);
412 break;
413 case QUERY:
414 parg.p_string = prproto[pr_type];
415 error("%s", &parg);
416 break;
421 * Handler for the -b option.
423 /*ARGSUSED*/
424 public void
425 opt_b(type, s)
426 int type;
427 char *s;
429 switch (type)
431 case INIT:
432 case TOGGLE:
434 * Set the new number of buffers.
436 ch_setbufspace(bufspace);
437 break;
438 case QUERY:
439 break;
444 * Handler for the -i option.
446 /*ARGSUSED*/
447 public void
448 opt_i(type, s)
449 int type;
450 char *s;
452 switch (type)
454 case TOGGLE:
455 chg_caseless();
456 break;
457 case QUERY:
458 case INIT:
459 break;
464 * Handler for the -V option.
466 /*ARGSUSED*/
467 public void
468 opt__V(type, s)
469 int type;
470 char *s;
472 switch (type)
474 case TOGGLE:
475 case QUERY:
476 dispversion();
477 break;
478 case INIT:
480 * Force output to stdout per GNU standard for --version output.
482 any_display = 1;
483 putstr("less ");
484 putstr(version);
485 putstr(" (");
486 #if HAVE_GNU_REGEX
487 putstr("GNU ");
488 #endif
489 #if HAVE_POSIX_REGCOMP
490 putstr("POSIX ");
491 #endif
492 #if HAVE_PCRE
493 putstr("PCRE ");
494 #endif
495 #if HAVE_RE_COMP
496 putstr("BSD ");
497 #endif
498 #if HAVE_REGCMP
499 putstr("V8 ");
500 #endif
501 #if HAVE_V8_REGCOMP
502 putstr("Spencer V8 ");
503 #endif
504 #if !HAVE_GNU_REGEX && !HAVE_POSIX_REGCOMP && !HAVE_PCRE && !HAVE_RE_COMP && !HAVE_REGCMP && !HAVE_V8_REGCOMP
505 putstr("no ");
506 #endif
507 putstr("regular expressions)\n");
508 putstr("Copyright (C) 1984-2012 Mark Nudelman\n\n");
509 putstr("less comes with NO WARRANTY, to the extent permitted by law.\n");
510 putstr("For information about the terms of redistribution,\n");
511 putstr("see the file named README in the less distribution.\n");
512 putstr("Homepage: http://www.greenwoodsoftware.com/less\n");
513 quit(QUIT_OK);
514 break;
518 #if MSDOS_COMPILER
520 * Parse an MSDOS color descriptor.
522 static void
523 colordesc(s, fg_color, bg_color)
524 char *s;
525 int *fg_color;
526 int *bg_color;
528 int fg, bg;
529 int err;
531 fg = getnum(&s, "D", &err);
532 if (err)
534 error("Missing fg color in -D", NULL_PARG);
535 return;
537 if (*s != '.')
538 bg = nm_bg_color;
539 else
541 s++;
542 bg = getnum(&s, "D", &err);
543 if (err)
545 error("Missing bg color in -D", NULL_PARG);
546 return;
549 if (*s != '\0')
550 error("Extra characters at end of -D option", NULL_PARG);
551 *fg_color = fg;
552 *bg_color = bg;
556 * Handler for the -D option.
558 /*ARGSUSED*/
559 public void
560 opt_D(type, s)
561 int type;
562 char *s;
564 switch (type)
566 case INIT:
567 case TOGGLE:
568 switch (*s++)
570 case 'n':
571 colordesc(s, &nm_fg_color, &nm_bg_color);
572 break;
573 case 'd':
574 colordesc(s, &bo_fg_color, &bo_bg_color);
575 break;
576 case 'u':
577 colordesc(s, &ul_fg_color, &ul_bg_color);
578 break;
579 case 'k':
580 colordesc(s, &bl_fg_color, &bl_bg_color);
581 break;
582 case 's':
583 colordesc(s, &so_fg_color, &so_bg_color);
584 break;
585 default:
586 error("-D must be followed by n, d, u, k or s", NULL_PARG);
587 break;
589 if (type == TOGGLE)
591 at_enter(AT_STANDOUT);
592 at_exit();
594 break;
595 case QUERY:
596 break;
599 #endif
602 * Handler for the -x option.
604 public void
605 opt_x(type, s)
606 int type;
607 register char *s;
609 extern int tabstops[];
610 extern int ntabstops;
611 extern int tabdefault;
612 char msg[60+(4*TABSTOP_MAX)];
613 int i;
614 PARG p;
616 switch (type)
618 case INIT:
619 case TOGGLE:
620 /* Start at 1 because tabstops[0] is always zero. */
621 for (i = 1; i < TABSTOP_MAX; )
623 int n = 0;
624 s = skipsp(s);
625 while (*s >= '0' && *s <= '9')
626 n = (10 * n) + (*s++ - '0');
627 if (n > tabstops[i-1])
628 tabstops[i++] = n;
629 s = skipsp(s);
630 if (*s++ != ',')
631 break;
633 if (i < 2)
634 return;
635 ntabstops = i;
636 tabdefault = tabstops[ntabstops-1] - tabstops[ntabstops-2];
637 break;
638 case QUERY:
639 strcpy(msg, "Tab stops ");
640 if (ntabstops > 2)
642 for (i = 1; i < ntabstops; i++)
644 if (i > 1)
645 strcat(msg, ",");
646 sprintf(msg+strlen(msg), "%d", tabstops[i]);
648 sprintf(msg+strlen(msg), " and then ");
650 sprintf(msg+strlen(msg), "every %d spaces",
651 tabdefault);
652 p.p_string = msg;
653 error("%s", &p);
654 break;
660 * Handler for the -" option.
662 public void
663 opt_quote(type, s)
664 int type;
665 register char *s;
667 char buf[3];
668 PARG parg;
670 switch (type)
672 case INIT:
673 case TOGGLE:
674 if (s[0] == '\0')
676 openquote = closequote = '\0';
677 break;
679 if (s[1] != '\0' && s[2] != '\0')
681 error("-\" must be followed by 1 or 2 chars", NULL_PARG);
682 return;
684 openquote = s[0];
685 if (s[1] == '\0')
686 closequote = openquote;
687 else
688 closequote = s[1];
689 break;
690 case QUERY:
691 buf[0] = openquote;
692 buf[1] = closequote;
693 buf[2] = '\0';
694 parg.p_string = buf;
695 error("quotes %s", &parg);
696 break;
701 * "-?" means display a help message.
702 * If from the command line, exit immediately.
704 /*ARGSUSED*/
705 public void
706 opt_query(type, s)
707 int type;
708 char *s;
710 switch (type)
712 case QUERY:
713 case TOGGLE:
714 error("Use \"h\" for help", NULL_PARG);
715 break;
716 case INIT:
717 dohelp = 1;
722 * Get the "screen window" size.
724 public int
725 get_swindow()
727 if (swindow > 0)
728 return (swindow);
729 return (sc_height + swindow);