1a5f0fb15SPaul Saab /* 2*a15691bfSXin LI * Copyright (C) 1984-2015 Mark Nudelman 3a5f0fb15SPaul Saab * 4a5f0fb15SPaul Saab * You may distribute under the terms of either the GNU General Public 5a5f0fb15SPaul Saab * License or the Less License, as specified in the README file. 6a5f0fb15SPaul Saab * 796e55cc7SXin LI * For more information, see the README file. 8a5f0fb15SPaul Saab */ 9a5f0fb15SPaul Saab 10a5f0fb15SPaul Saab 11a5f0fb15SPaul Saab /* 12a5f0fb15SPaul Saab * Process command line options. 13a5f0fb15SPaul Saab * 14a5f0fb15SPaul Saab * Each option is a single letter which controls a program variable. 15a5f0fb15SPaul Saab * The options have defaults which may be changed via 16a5f0fb15SPaul Saab * the command line option, toggled via the "-" command, 17a5f0fb15SPaul Saab * or queried via the "_" command. 18a5f0fb15SPaul Saab */ 19a5f0fb15SPaul Saab 20a5f0fb15SPaul Saab #include "less.h" 21a5f0fb15SPaul Saab #include "option.h" 22a5f0fb15SPaul Saab 23000ba3e8STim J. Robbins static struct loption *pendopt; 24a5f0fb15SPaul Saab public int plusoption = FALSE; 25a5f0fb15SPaul Saab 26a5f0fb15SPaul Saab static char *optstring(); 27a5f0fb15SPaul Saab static int flip_triple(); 28a5f0fb15SPaul Saab 29a5f0fb15SPaul Saab extern int screen_trashed; 307f074f9cSXin LI extern int less_is_more; 317f074f9cSXin LI extern int quit_at_eof; 32a5f0fb15SPaul Saab extern char *every_first_cmd; 337bd2567cSXin LI extern int opt_use_backslash; 34a5f0fb15SPaul Saab 35a5f0fb15SPaul Saab /* 3633096f16SXin LI * Return a printable description of an option. 3733096f16SXin LI */ 3833096f16SXin LI static char * 3933096f16SXin LI opt_desc(o) 4033096f16SXin LI struct loption *o; 4133096f16SXin LI { 4233096f16SXin LI static char buf[OPTNAME_MAX + 10]; 4333096f16SXin LI if (o->oletter == OLETTER_NONE) 4433096f16SXin LI SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname); 4533096f16SXin LI else 4633096f16SXin LI SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname); 4733096f16SXin LI return (buf); 4833096f16SXin LI } 4933096f16SXin LI 5033096f16SXin LI /* 5133096f16SXin LI * Return a string suitable for printing as the "name" of an option. 5233096f16SXin LI * For example, if the option letter is 'x', just return "-x". 5333096f16SXin LI */ 5433096f16SXin LI public char * 5533096f16SXin LI propt(c) 5633096f16SXin LI int c; 5733096f16SXin LI { 5833096f16SXin LI static char buf[8]; 5933096f16SXin LI 6033096f16SXin LI sprintf(buf, "-%s", prchar(c)); 6133096f16SXin LI return (buf); 6233096f16SXin LI } 6333096f16SXin LI 6433096f16SXin LI /* 65a5f0fb15SPaul Saab * Scan an argument (either from the command line or from the 66a5f0fb15SPaul Saab * LESS environment variable) and process it. 67a5f0fb15SPaul Saab */ 68a5f0fb15SPaul Saab public void 69a5f0fb15SPaul Saab scan_option(s) 70a5f0fb15SPaul Saab char *s; 71a5f0fb15SPaul Saab { 72000ba3e8STim J. Robbins register struct loption *o; 73a5f0fb15SPaul Saab register int optc; 74a5f0fb15SPaul Saab char *optname; 75a5f0fb15SPaul Saab char *printopt; 76a5f0fb15SPaul Saab char *str; 77a5f0fb15SPaul Saab int set_default; 78a5f0fb15SPaul Saab int lc; 79a5f0fb15SPaul Saab int err; 80a5f0fb15SPaul Saab PARG parg; 81a5f0fb15SPaul Saab 82a5f0fb15SPaul Saab if (s == NULL) 83a5f0fb15SPaul Saab return; 84a5f0fb15SPaul Saab 85a5f0fb15SPaul Saab /* 86a5f0fb15SPaul Saab * If we have a pending option which requires an argument, 87a5f0fb15SPaul Saab * handle it now. 88a5f0fb15SPaul Saab * This happens if the previous option was, for example, "-P" 89a5f0fb15SPaul Saab * without a following string. In that case, the current 90a5f0fb15SPaul Saab * option is simply the argument for the previous option. 91a5f0fb15SPaul Saab */ 92a5f0fb15SPaul Saab if (pendopt != NULL) 93a5f0fb15SPaul Saab { 94a5f0fb15SPaul Saab switch (pendopt->otype & OTYPE) 95a5f0fb15SPaul Saab { 96a5f0fb15SPaul Saab case STRING: 97a5f0fb15SPaul Saab (*pendopt->ofunc)(INIT, s); 98a5f0fb15SPaul Saab break; 99a5f0fb15SPaul Saab case NUMBER: 10033096f16SXin LI printopt = opt_desc(pendopt); 101a5f0fb15SPaul Saab *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL); 102a5f0fb15SPaul Saab break; 103a5f0fb15SPaul Saab } 104a5f0fb15SPaul Saab pendopt = NULL; 105a5f0fb15SPaul Saab return; 106a5f0fb15SPaul Saab } 107a5f0fb15SPaul Saab 108a5f0fb15SPaul Saab set_default = FALSE; 109a5f0fb15SPaul Saab optname = NULL; 110a5f0fb15SPaul Saab 111a5f0fb15SPaul Saab while (*s != '\0') 112a5f0fb15SPaul Saab { 113a5f0fb15SPaul Saab /* 114a5f0fb15SPaul Saab * Check some special cases first. 115a5f0fb15SPaul Saab */ 116a5f0fb15SPaul Saab switch (optc = *s++) 117a5f0fb15SPaul Saab { 118a5f0fb15SPaul Saab case ' ': 119a5f0fb15SPaul Saab case '\t': 120a5f0fb15SPaul Saab case END_OPTION_STRING: 121a5f0fb15SPaul Saab continue; 122a5f0fb15SPaul Saab case '-': 123a5f0fb15SPaul Saab /* 124a5f0fb15SPaul Saab * "--" indicates an option name instead of a letter. 125a5f0fb15SPaul Saab */ 126a5f0fb15SPaul Saab if (*s == '-') 127a5f0fb15SPaul Saab { 128a5f0fb15SPaul Saab optname = ++s; 129a5f0fb15SPaul Saab break; 130a5f0fb15SPaul Saab } 131a5f0fb15SPaul Saab /* 132a5f0fb15SPaul Saab * "-+" means set these options back to their defaults. 133a5f0fb15SPaul Saab * (They may have been set otherwise by previous 134a5f0fb15SPaul Saab * options.) 135a5f0fb15SPaul Saab */ 136a5f0fb15SPaul Saab set_default = (*s == '+'); 137a5f0fb15SPaul Saab if (set_default) 138a5f0fb15SPaul Saab s++; 139a5f0fb15SPaul Saab continue; 140a5f0fb15SPaul Saab case '+': 141a5f0fb15SPaul Saab /* 142a5f0fb15SPaul Saab * An option prefixed by a "+" is ungotten, so 143a5f0fb15SPaul Saab * that it is interpreted as less commands 144a5f0fb15SPaul Saab * processed at the start of the first input file. 145a5f0fb15SPaul Saab * "++" means process the commands at the start of 146a5f0fb15SPaul Saab * EVERY input file. 147a5f0fb15SPaul Saab */ 148a5f0fb15SPaul Saab plusoption = TRUE; 149000ba3e8STim J. Robbins s = optstring(s, &str, propt('+'), NULL); 1504cc5fc9aSXin LI if (s == NULL) 1514cc5fc9aSXin LI return; 152c9346414SPaul Saab if (*str == '+') 15321fa6541SXin LI every_first_cmd = save(str+1); 154a5f0fb15SPaul Saab else 155*a15691bfSXin LI { 156*a15691bfSXin LI ungetcc(CHAR_END_COMMAND); 157c9346414SPaul Saab ungetsc(str); 158*a15691bfSXin LI } 1594cc5fc9aSXin LI free(str); 160a5f0fb15SPaul Saab continue; 161a5f0fb15SPaul Saab case '0': case '1': case '2': case '3': case '4': 162a5f0fb15SPaul Saab case '5': case '6': case '7': case '8': case '9': 163a5f0fb15SPaul Saab /* 164a5f0fb15SPaul Saab * Special "more" compatibility form "-<number>" 165a5f0fb15SPaul Saab * instead of -z<number> to set the scrolling 166a5f0fb15SPaul Saab * window size. 167a5f0fb15SPaul Saab */ 168a5f0fb15SPaul Saab s--; 169a5f0fb15SPaul Saab optc = 'z'; 170a5f0fb15SPaul Saab break; 1717f074f9cSXin LI case 'n': 1727f074f9cSXin LI if (less_is_more) 1737f074f9cSXin LI optc = 'z'; 1747f074f9cSXin LI break; 175a5f0fb15SPaul Saab } 176a5f0fb15SPaul Saab 177a5f0fb15SPaul Saab /* 178a5f0fb15SPaul Saab * Not a special case. 179a5f0fb15SPaul Saab * Look up the option letter in the option table. 180a5f0fb15SPaul Saab */ 181a5f0fb15SPaul Saab err = 0; 182a5f0fb15SPaul Saab if (optname == NULL) 183a5f0fb15SPaul Saab { 184a5f0fb15SPaul Saab printopt = propt(optc); 1856dcb072bSXin LI lc = ASCII_IS_LOWER(optc); 186a5f0fb15SPaul Saab o = findopt(optc); 187a5f0fb15SPaul Saab } else 188a5f0fb15SPaul Saab { 189a5f0fb15SPaul Saab printopt = optname; 1906dcb072bSXin LI lc = ASCII_IS_LOWER(optname[0]); 191a5f0fb15SPaul Saab o = findopt_name(&optname, NULL, &err); 192a5f0fb15SPaul Saab s = optname; 193a5f0fb15SPaul Saab optname = NULL; 194a5f0fb15SPaul Saab if (*s == '\0' || *s == ' ') 195a5f0fb15SPaul Saab { 196a5f0fb15SPaul Saab /* 197a5f0fb15SPaul Saab * The option name matches exactly. 198a5f0fb15SPaul Saab */ 199a5f0fb15SPaul Saab ; 200a5f0fb15SPaul Saab } else if (*s == '=') 201a5f0fb15SPaul Saab { 202a5f0fb15SPaul Saab /* 203a5f0fb15SPaul Saab * The option name is followed by "=value". 204a5f0fb15SPaul Saab */ 205a5f0fb15SPaul Saab if (o != NULL && 206a5f0fb15SPaul Saab (o->otype & OTYPE) != STRING && 207a5f0fb15SPaul Saab (o->otype & OTYPE) != NUMBER) 208a5f0fb15SPaul Saab { 209a5f0fb15SPaul Saab parg.p_string = printopt; 210a5f0fb15SPaul Saab error("The %s option should not be followed by =", 211a5f0fb15SPaul Saab &parg); 2124cc5fc9aSXin LI return; 213a5f0fb15SPaul Saab } 214a5f0fb15SPaul Saab s++; 215a5f0fb15SPaul Saab } else 216a5f0fb15SPaul Saab { 217a5f0fb15SPaul Saab /* 218a5f0fb15SPaul Saab * The specified name is longer than the 219a5f0fb15SPaul Saab * real option name. 220a5f0fb15SPaul Saab */ 221a5f0fb15SPaul Saab o = NULL; 222a5f0fb15SPaul Saab } 223a5f0fb15SPaul Saab } 224a5f0fb15SPaul Saab if (o == NULL) 225a5f0fb15SPaul Saab { 226a5f0fb15SPaul Saab parg.p_string = printopt; 227a5f0fb15SPaul Saab if (err == OPT_AMBIG) 228a5f0fb15SPaul Saab error("%s is an ambiguous abbreviation (\"less --help\" for help)", 229a5f0fb15SPaul Saab &parg); 230a5f0fb15SPaul Saab else 231a5f0fb15SPaul Saab error("There is no %s option (\"less --help\" for help)", 232a5f0fb15SPaul Saab &parg); 2334cc5fc9aSXin LI return; 234a5f0fb15SPaul Saab } 235a5f0fb15SPaul Saab 236a5f0fb15SPaul Saab str = NULL; 237a5f0fb15SPaul Saab switch (o->otype & OTYPE) 238a5f0fb15SPaul Saab { 239a5f0fb15SPaul Saab case BOOL: 240a5f0fb15SPaul Saab if (set_default) 241a5f0fb15SPaul Saab *(o->ovar) = o->odefault; 242a5f0fb15SPaul Saab else 243a5f0fb15SPaul Saab *(o->ovar) = ! o->odefault; 244a5f0fb15SPaul Saab break; 245a5f0fb15SPaul Saab case TRIPLE: 246a5f0fb15SPaul Saab if (set_default) 247a5f0fb15SPaul Saab *(o->ovar) = o->odefault; 248a5f0fb15SPaul Saab else 249a5f0fb15SPaul Saab *(o->ovar) = flip_triple(o->odefault, lc); 250a5f0fb15SPaul Saab break; 251a5f0fb15SPaul Saab case STRING: 252a5f0fb15SPaul Saab if (*s == '\0') 253a5f0fb15SPaul Saab { 254a5f0fb15SPaul Saab /* 255a5f0fb15SPaul Saab * Set pendopt and return. 256a5f0fb15SPaul Saab * We will get the string next time 257a5f0fb15SPaul Saab * scan_option is called. 258a5f0fb15SPaul Saab */ 259a5f0fb15SPaul Saab pendopt = o; 260a5f0fb15SPaul Saab return; 261a5f0fb15SPaul Saab } 262a5f0fb15SPaul Saab /* 263a5f0fb15SPaul Saab * Don't do anything here. 264a5f0fb15SPaul Saab * All processing of STRING options is done by 265a5f0fb15SPaul Saab * the handling function. 266a5f0fb15SPaul Saab */ 267000ba3e8STim J. Robbins while (*s == ' ') 268000ba3e8STim J. Robbins s++; 269000ba3e8STim J. Robbins s = optstring(s, &str, printopt, o->odesc[1]); 2704cc5fc9aSXin LI if (s == NULL) 2714cc5fc9aSXin LI return; 272a5f0fb15SPaul Saab break; 273a5f0fb15SPaul Saab case NUMBER: 274a5f0fb15SPaul Saab if (*s == '\0') 275a5f0fb15SPaul Saab { 276a5f0fb15SPaul Saab pendopt = o; 277a5f0fb15SPaul Saab return; 278a5f0fb15SPaul Saab } 279a5f0fb15SPaul Saab *(o->ovar) = getnum(&s, printopt, (int*)NULL); 280a5f0fb15SPaul Saab break; 281a5f0fb15SPaul Saab } 282a5f0fb15SPaul Saab /* 283a5f0fb15SPaul Saab * If the option has a handling function, call it. 284a5f0fb15SPaul Saab */ 285a5f0fb15SPaul Saab if (o->ofunc != NULL) 286a5f0fb15SPaul Saab (*o->ofunc)(INIT, str); 2874cc5fc9aSXin LI if (str != NULL) 2884cc5fc9aSXin LI free(str); 289a5f0fb15SPaul Saab } 290a5f0fb15SPaul Saab } 291a5f0fb15SPaul Saab 292a5f0fb15SPaul Saab /* 293a5f0fb15SPaul Saab * Toggle command line flags from within the program. 294a5f0fb15SPaul Saab * Used by the "-" and "_" commands. 295a5f0fb15SPaul Saab * how_toggle may be: 296a5f0fb15SPaul Saab * OPT_NO_TOGGLE just report the current setting, without changing it. 297a5f0fb15SPaul Saab * OPT_TOGGLE invert the current setting 298a5f0fb15SPaul Saab * OPT_UNSET set to the default value 299a5f0fb15SPaul Saab * OPT_SET set to the inverse of the default value 300a5f0fb15SPaul Saab */ 301a5f0fb15SPaul Saab public void 30233096f16SXin LI toggle_option(o, lower, s, how_toggle) 30333096f16SXin LI struct loption *o; 30433096f16SXin LI int lower; 305a5f0fb15SPaul Saab char *s; 306a5f0fb15SPaul Saab int how_toggle; 307a5f0fb15SPaul Saab { 308a5f0fb15SPaul Saab register int num; 309a5f0fb15SPaul Saab int no_prompt; 310a5f0fb15SPaul Saab int err; 311a5f0fb15SPaul Saab PARG parg; 312a5f0fb15SPaul Saab 313a5f0fb15SPaul Saab no_prompt = (how_toggle & OPT_NO_PROMPT); 314a5f0fb15SPaul Saab how_toggle &= ~OPT_NO_PROMPT; 315a5f0fb15SPaul Saab 316a5f0fb15SPaul Saab if (o == NULL) 317a5f0fb15SPaul Saab { 31833096f16SXin LI error("No such option", NULL_PARG); 319a5f0fb15SPaul Saab return; 320a5f0fb15SPaul Saab } 321a5f0fb15SPaul Saab 322a5f0fb15SPaul Saab if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE)) 323a5f0fb15SPaul Saab { 32433096f16SXin LI parg.p_string = opt_desc(o); 325a5f0fb15SPaul Saab error("Cannot change the %s option", &parg); 326a5f0fb15SPaul Saab return; 327a5f0fb15SPaul Saab } 328a5f0fb15SPaul Saab 329a5f0fb15SPaul Saab if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY)) 330a5f0fb15SPaul Saab { 33133096f16SXin LI parg.p_string = opt_desc(o); 332a5f0fb15SPaul Saab error("Cannot query the %s option", &parg); 333a5f0fb15SPaul Saab return; 334a5f0fb15SPaul Saab } 335a5f0fb15SPaul Saab 336a5f0fb15SPaul Saab /* 337a5f0fb15SPaul Saab * Check for something which appears to be a do_toggle 338a5f0fb15SPaul Saab * (because the "-" command was used), but really is not. 339a5f0fb15SPaul Saab * This could be a string option with no string, or 340a5f0fb15SPaul Saab * a number option with no number. 341a5f0fb15SPaul Saab */ 342a5f0fb15SPaul Saab switch (o->otype & OTYPE) 343a5f0fb15SPaul Saab { 344a5f0fb15SPaul Saab case STRING: 345a5f0fb15SPaul Saab case NUMBER: 346a5f0fb15SPaul Saab if (how_toggle == OPT_TOGGLE && *s == '\0') 347a5f0fb15SPaul Saab how_toggle = OPT_NO_TOGGLE; 348a5f0fb15SPaul Saab break; 349a5f0fb15SPaul Saab } 350a5f0fb15SPaul Saab 351a5f0fb15SPaul Saab #if HILITE_SEARCH 352a5f0fb15SPaul Saab if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT)) 353a5f0fb15SPaul Saab repaint_hilite(0); 354a5f0fb15SPaul Saab #endif 355a5f0fb15SPaul Saab 356a5f0fb15SPaul Saab /* 357a5f0fb15SPaul Saab * Now actually toggle (change) the variable. 358a5f0fb15SPaul Saab */ 359a5f0fb15SPaul Saab if (how_toggle != OPT_NO_TOGGLE) 360a5f0fb15SPaul Saab { 361a5f0fb15SPaul Saab switch (o->otype & OTYPE) 362a5f0fb15SPaul Saab { 363a5f0fb15SPaul Saab case BOOL: 364a5f0fb15SPaul Saab /* 365a5f0fb15SPaul Saab * Boolean. 366a5f0fb15SPaul Saab */ 367a5f0fb15SPaul Saab switch (how_toggle) 368a5f0fb15SPaul Saab { 369a5f0fb15SPaul Saab case OPT_TOGGLE: 370a5f0fb15SPaul Saab *(o->ovar) = ! *(o->ovar); 371a5f0fb15SPaul Saab break; 372a5f0fb15SPaul Saab case OPT_UNSET: 373a5f0fb15SPaul Saab *(o->ovar) = o->odefault; 374a5f0fb15SPaul Saab break; 375a5f0fb15SPaul Saab case OPT_SET: 376a5f0fb15SPaul Saab *(o->ovar) = ! o->odefault; 377a5f0fb15SPaul Saab break; 378a5f0fb15SPaul Saab } 379a5f0fb15SPaul Saab break; 380a5f0fb15SPaul Saab case TRIPLE: 381a5f0fb15SPaul Saab /* 382a5f0fb15SPaul Saab * Triple: 383a5f0fb15SPaul Saab * If user gave the lower case letter, then switch 384a5f0fb15SPaul Saab * to 1 unless already 1, in which case make it 0. 385a5f0fb15SPaul Saab * If user gave the upper case letter, then switch 386a5f0fb15SPaul Saab * to 2 unless already 2, in which case make it 0. 387a5f0fb15SPaul Saab */ 388a5f0fb15SPaul Saab switch (how_toggle) 389a5f0fb15SPaul Saab { 390a5f0fb15SPaul Saab case OPT_TOGGLE: 39133096f16SXin LI *(o->ovar) = flip_triple(*(o->ovar), lower); 392a5f0fb15SPaul Saab break; 393a5f0fb15SPaul Saab case OPT_UNSET: 394a5f0fb15SPaul Saab *(o->ovar) = o->odefault; 395a5f0fb15SPaul Saab break; 396a5f0fb15SPaul Saab case OPT_SET: 39733096f16SXin LI *(o->ovar) = flip_triple(o->odefault, lower); 398a5f0fb15SPaul Saab break; 399a5f0fb15SPaul Saab } 400a5f0fb15SPaul Saab break; 401a5f0fb15SPaul Saab case STRING: 402a5f0fb15SPaul Saab /* 403a5f0fb15SPaul Saab * String: don't do anything here. 404a5f0fb15SPaul Saab * The handling function will do everything. 405a5f0fb15SPaul Saab */ 406a5f0fb15SPaul Saab switch (how_toggle) 407a5f0fb15SPaul Saab { 408a5f0fb15SPaul Saab case OPT_SET: 409a5f0fb15SPaul Saab case OPT_UNSET: 410a5f0fb15SPaul Saab error("Cannot use \"-+\" or \"--\" for a string option", 411a5f0fb15SPaul Saab NULL_PARG); 412a5f0fb15SPaul Saab return; 413a5f0fb15SPaul Saab } 414a5f0fb15SPaul Saab break; 415a5f0fb15SPaul Saab case NUMBER: 416a5f0fb15SPaul Saab /* 417a5f0fb15SPaul Saab * Number: set the variable to the given number. 418a5f0fb15SPaul Saab */ 419a5f0fb15SPaul Saab switch (how_toggle) 420a5f0fb15SPaul Saab { 421a5f0fb15SPaul Saab case OPT_TOGGLE: 422000ba3e8STim J. Robbins num = getnum(&s, NULL, &err); 423a5f0fb15SPaul Saab if (!err) 424a5f0fb15SPaul Saab *(o->ovar) = num; 425a5f0fb15SPaul Saab break; 426a5f0fb15SPaul Saab case OPT_UNSET: 427a5f0fb15SPaul Saab *(o->ovar) = o->odefault; 428a5f0fb15SPaul Saab break; 429a5f0fb15SPaul Saab case OPT_SET: 430a5f0fb15SPaul Saab error("Can't use \"-!\" for a numeric option", 431a5f0fb15SPaul Saab NULL_PARG); 432a5f0fb15SPaul Saab return; 433a5f0fb15SPaul Saab } 434a5f0fb15SPaul Saab break; 435a5f0fb15SPaul Saab } 436a5f0fb15SPaul Saab } 437a5f0fb15SPaul Saab 438a5f0fb15SPaul Saab /* 439a5f0fb15SPaul Saab * Call the handling function for any special action 440a5f0fb15SPaul Saab * specific to this option. 441a5f0fb15SPaul Saab */ 442a5f0fb15SPaul Saab if (o->ofunc != NULL) 443a5f0fb15SPaul Saab (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s); 444a5f0fb15SPaul Saab 445a5f0fb15SPaul Saab #if HILITE_SEARCH 446a5f0fb15SPaul Saab if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT)) 447a5f0fb15SPaul Saab chg_hilite(); 448a5f0fb15SPaul Saab #endif 449a5f0fb15SPaul Saab 450a5f0fb15SPaul Saab if (!no_prompt) 451a5f0fb15SPaul Saab { 452a5f0fb15SPaul Saab /* 453a5f0fb15SPaul Saab * Print a message describing the new setting. 454a5f0fb15SPaul Saab */ 455a5f0fb15SPaul Saab switch (o->otype & OTYPE) 456a5f0fb15SPaul Saab { 457a5f0fb15SPaul Saab case BOOL: 458a5f0fb15SPaul Saab case TRIPLE: 459a5f0fb15SPaul Saab /* 460a5f0fb15SPaul Saab * Print the odesc message. 461a5f0fb15SPaul Saab */ 462a5f0fb15SPaul Saab error(o->odesc[*(o->ovar)], NULL_PARG); 463a5f0fb15SPaul Saab break; 464a5f0fb15SPaul Saab case NUMBER: 465a5f0fb15SPaul Saab /* 466a5f0fb15SPaul Saab * The message is in odesc[1] and has a %d for 467a5f0fb15SPaul Saab * the value of the variable. 468a5f0fb15SPaul Saab */ 469a5f0fb15SPaul Saab parg.p_int = *(o->ovar); 470a5f0fb15SPaul Saab error(o->odesc[1], &parg); 471a5f0fb15SPaul Saab break; 472a5f0fb15SPaul Saab case STRING: 473a5f0fb15SPaul Saab /* 474a5f0fb15SPaul Saab * Message was already printed by the handling function. 475a5f0fb15SPaul Saab */ 476a5f0fb15SPaul Saab break; 477a5f0fb15SPaul Saab } 478a5f0fb15SPaul Saab } 479a5f0fb15SPaul Saab 480a5f0fb15SPaul Saab if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT)) 481a5f0fb15SPaul Saab screen_trashed = TRUE; 482a5f0fb15SPaul Saab } 483a5f0fb15SPaul Saab 484a5f0fb15SPaul Saab /* 485a5f0fb15SPaul Saab * "Toggle" a triple-valued option. 486a5f0fb15SPaul Saab */ 487a5f0fb15SPaul Saab static int 488a5f0fb15SPaul Saab flip_triple(val, lc) 489a5f0fb15SPaul Saab int val; 490a5f0fb15SPaul Saab int lc; 491a5f0fb15SPaul Saab { 492a5f0fb15SPaul Saab if (lc) 493a5f0fb15SPaul Saab return ((val == OPT_ON) ? OPT_OFF : OPT_ON); 494a5f0fb15SPaul Saab else 495a5f0fb15SPaul Saab return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS); 496a5f0fb15SPaul Saab } 497a5f0fb15SPaul Saab 498a5f0fb15SPaul Saab /* 49933096f16SXin LI * Determine if an option takes a parameter. 500a5f0fb15SPaul Saab */ 501a5f0fb15SPaul Saab public int 50233096f16SXin LI opt_has_param(o) 50333096f16SXin LI struct loption *o; 504a5f0fb15SPaul Saab { 505a5f0fb15SPaul Saab if (o == NULL) 50633096f16SXin LI return (0); 50733096f16SXin LI if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) 50833096f16SXin LI return (0); 50933096f16SXin LI return (1); 510a5f0fb15SPaul Saab } 511a5f0fb15SPaul Saab 512a5f0fb15SPaul Saab /* 513a5f0fb15SPaul Saab * Return the prompt to be used for a given option letter. 514a5f0fb15SPaul Saab * Only string and number valued options have prompts. 515a5f0fb15SPaul Saab */ 516a5f0fb15SPaul Saab public char * 51733096f16SXin LI opt_prompt(o) 51833096f16SXin LI struct loption *o; 519a5f0fb15SPaul Saab { 520a5f0fb15SPaul Saab if (o == NULL || (o->otype & (STRING|NUMBER)) == 0) 52133096f16SXin LI return ("?"); 522a5f0fb15SPaul Saab return (o->odesc[0]); 523a5f0fb15SPaul Saab } 524a5f0fb15SPaul Saab 525a5f0fb15SPaul Saab /* 526a5f0fb15SPaul Saab * Return whether or not there is a string option pending; 527a5f0fb15SPaul Saab * that is, if the previous option was a string-valued option letter 528a5f0fb15SPaul Saab * (like -P) without a following string. 529a5f0fb15SPaul Saab * In that case, the current option is taken to be the string for 530a5f0fb15SPaul Saab * the previous option. 531a5f0fb15SPaul Saab */ 532a5f0fb15SPaul Saab public int 533a5f0fb15SPaul Saab isoptpending() 534a5f0fb15SPaul Saab { 535a5f0fb15SPaul Saab return (pendopt != NULL); 536a5f0fb15SPaul Saab } 537a5f0fb15SPaul Saab 538a5f0fb15SPaul Saab /* 539a5f0fb15SPaul Saab * Print error message about missing string. 540a5f0fb15SPaul Saab */ 541a5f0fb15SPaul Saab static void 542a5f0fb15SPaul Saab nostring(printopt) 543a5f0fb15SPaul Saab char *printopt; 544a5f0fb15SPaul Saab { 545a5f0fb15SPaul Saab PARG parg; 546a5f0fb15SPaul Saab parg.p_string = printopt; 547a5f0fb15SPaul Saab error("Value is required after %s", &parg); 548a5f0fb15SPaul Saab } 549a5f0fb15SPaul Saab 550a5f0fb15SPaul Saab /* 551a5f0fb15SPaul Saab * Print error message if a STRING type option is not followed by a string. 552a5f0fb15SPaul Saab */ 553a5f0fb15SPaul Saab public void 554a5f0fb15SPaul Saab nopendopt() 555a5f0fb15SPaul Saab { 55633096f16SXin LI nostring(opt_desc(pendopt)); 557a5f0fb15SPaul Saab } 558a5f0fb15SPaul Saab 559a5f0fb15SPaul Saab /* 560a5f0fb15SPaul Saab * Scan to end of string or to an END_OPTION_STRING character. 561a5f0fb15SPaul Saab * In the latter case, replace the char with a null char. 562a5f0fb15SPaul Saab * Return a pointer to the remainder of the string, if any. 563a5f0fb15SPaul Saab */ 564a5f0fb15SPaul Saab static char * 565000ba3e8STim J. Robbins optstring(s, p_str, printopt, validchars) 566a5f0fb15SPaul Saab char *s; 567000ba3e8STim J. Robbins char **p_str; 568a5f0fb15SPaul Saab char *printopt; 569c9346414SPaul Saab char *validchars; 570a5f0fb15SPaul Saab { 571a5f0fb15SPaul Saab register char *p; 5724cc5fc9aSXin LI register char *out; 573a5f0fb15SPaul Saab 574a5f0fb15SPaul Saab if (*s == '\0') 575a5f0fb15SPaul Saab { 576a5f0fb15SPaul Saab nostring(printopt); 5774cc5fc9aSXin LI return (NULL); 578a5f0fb15SPaul Saab } 5794cc5fc9aSXin LI /* Alloc could be more than needed, but not worth trimming. */ 5804cc5fc9aSXin LI *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char)); 5814cc5fc9aSXin LI out = *p_str; 5824cc5fc9aSXin LI 583a5f0fb15SPaul Saab for (p = s; *p != '\0'; p++) 584000ba3e8STim J. Robbins { 5857bd2567cSXin LI if (opt_use_backslash && *p == '\\' && p[1] != '\0') 5864cc5fc9aSXin LI { 5874cc5fc9aSXin LI /* Take next char literally. */ 5884cc5fc9aSXin LI ++p; 5894cc5fc9aSXin LI } else 5904cc5fc9aSXin LI { 591c9346414SPaul Saab if (*p == END_OPTION_STRING || 592c9346414SPaul Saab (validchars != NULL && strchr(validchars, *p) == NULL)) 5934cc5fc9aSXin LI /* End of option string. */ 594c9346414SPaul Saab break; 595c9346414SPaul Saab } 5964cc5fc9aSXin LI *out++ = *p; 597000ba3e8STim J. Robbins } 5984cc5fc9aSXin LI *out = '\0'; 599a5f0fb15SPaul Saab return (p); 600a5f0fb15SPaul Saab } 601a5f0fb15SPaul Saab 602a5f0fb15SPaul Saab /* 6037f074f9cSXin LI */ 6047f074f9cSXin LI static int 6057f074f9cSXin LI num_error(printopt, errp) 6067f074f9cSXin LI char *printopt; 6077f074f9cSXin LI int *errp; 6087f074f9cSXin LI { 6097f074f9cSXin LI PARG parg; 6107f074f9cSXin LI 6117f074f9cSXin LI if (errp != NULL) 6127f074f9cSXin LI { 6137f074f9cSXin LI *errp = TRUE; 6147f074f9cSXin LI return (-1); 6157f074f9cSXin LI } 6167f074f9cSXin LI if (printopt != NULL) 6177f074f9cSXin LI { 6187f074f9cSXin LI parg.p_string = printopt; 6197f074f9cSXin LI error("Number is required after %s", &parg); 6207f074f9cSXin LI } 6217f074f9cSXin LI return (-1); 6227f074f9cSXin LI } 6237f074f9cSXin LI 6247f074f9cSXin LI /* 625a5f0fb15SPaul Saab * Translate a string into a number. 626a5f0fb15SPaul Saab * Like atoi(), but takes a pointer to a char *, and updates 627a5f0fb15SPaul Saab * the char * to point after the translated number. 628a5f0fb15SPaul Saab */ 629a5f0fb15SPaul Saab public int 630a5f0fb15SPaul Saab getnum(sp, printopt, errp) 631a5f0fb15SPaul Saab char **sp; 632a5f0fb15SPaul Saab char *printopt; 633a5f0fb15SPaul Saab int *errp; 634a5f0fb15SPaul Saab { 635a5f0fb15SPaul Saab register char *s; 636a5f0fb15SPaul Saab register int n; 637a5f0fb15SPaul Saab register int neg; 638a5f0fb15SPaul Saab 639a5f0fb15SPaul Saab s = skipsp(*sp); 640a5f0fb15SPaul Saab neg = FALSE; 641a5f0fb15SPaul Saab if (*s == '-') 642a5f0fb15SPaul Saab { 643a5f0fb15SPaul Saab neg = TRUE; 644a5f0fb15SPaul Saab s++; 645a5f0fb15SPaul Saab } 646a5f0fb15SPaul Saab if (*s < '0' || *s > '9') 6477f074f9cSXin LI return (num_error(printopt, errp)); 648a5f0fb15SPaul Saab 649a5f0fb15SPaul Saab n = 0; 650a5f0fb15SPaul Saab while (*s >= '0' && *s <= '9') 651a5f0fb15SPaul Saab n = 10 * n + *s++ - '0'; 652a5f0fb15SPaul Saab *sp = s; 653a5f0fb15SPaul Saab if (errp != NULL) 654a5f0fb15SPaul Saab *errp = FALSE; 655a5f0fb15SPaul Saab if (neg) 656a5f0fb15SPaul Saab n = -n; 657a5f0fb15SPaul Saab return (n); 658a5f0fb15SPaul Saab } 6597f074f9cSXin LI 6607f074f9cSXin LI /* 6617f074f9cSXin LI * Translate a string into a fraction, represented by the part of a 6627f074f9cSXin LI * number which would follow a decimal point. 6637f074f9cSXin LI * The value of the fraction is returned as parts per NUM_FRAC_DENOM. 6647f074f9cSXin LI * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM. 6657f074f9cSXin LI */ 6667f074f9cSXin LI public long 6677f074f9cSXin LI getfraction(sp, printopt, errp) 6687f074f9cSXin LI char **sp; 6697f074f9cSXin LI char *printopt; 6707f074f9cSXin LI int *errp; 6717f074f9cSXin LI { 6727f074f9cSXin LI register char *s; 6737f074f9cSXin LI long frac = 0; 6747f074f9cSXin LI int fraclen = 0; 6757f074f9cSXin LI 6767f074f9cSXin LI s = skipsp(*sp); 6777f074f9cSXin LI if (*s < '0' || *s > '9') 6787f074f9cSXin LI return (num_error(printopt, errp)); 6797f074f9cSXin LI 6807f074f9cSXin LI for ( ; *s >= '0' && *s <= '9'; s++) 6817f074f9cSXin LI { 6827f074f9cSXin LI frac = (frac * 10) + (*s - '0'); 6837f074f9cSXin LI fraclen++; 6847f074f9cSXin LI } 6857f074f9cSXin LI if (fraclen > NUM_LOG_FRAC_DENOM) 6867f074f9cSXin LI while (fraclen-- > NUM_LOG_FRAC_DENOM) 6877f074f9cSXin LI frac /= 10; 6887f074f9cSXin LI else 6897f074f9cSXin LI while (fraclen++ < NUM_LOG_FRAC_DENOM) 6907f074f9cSXin LI frac *= 10; 6917f074f9cSXin LI *sp = s; 6927f074f9cSXin LI if (errp != NULL) 6937f074f9cSXin LI *errp = FALSE; 6947f074f9cSXin LI return (frac); 6957f074f9cSXin LI } 6967f074f9cSXin LI 6977f074f9cSXin LI 6987f074f9cSXin LI /* 6997f074f9cSXin LI * Get the value of the -e flag. 7007f074f9cSXin LI */ 7017f074f9cSXin LI public int 7027f074f9cSXin LI get_quit_at_eof() 7037f074f9cSXin LI { 7047f074f9cSXin LI if (!less_is_more) 7057f074f9cSXin LI return quit_at_eof; 7067f074f9cSXin LI /* When less_is_more is set, the -e flag semantics are different. */ 707*a15691bfSXin LI return quit_at_eof ? OPT_ONPLUS : OPT_ON; 7087f074f9cSXin LI } 709