=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/less/option.c,v retrieving revision 1.6 retrieving revision 1.7 diff -c -r1.6 -r1.7 *** src/usr.bin/less/option.c 2003/04/14 15:09:57 1.6 --- src/usr.bin/less/option.c 2011/09/16 18:12:09 1.7 *************** *** 1,5 **** /* ! * Copyright (C) 1984-2002 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. --- 1,5 ---- /* ! * Copyright (C) 1984-2011 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. *************** *** 24,36 **** static struct loption *pendopt; public int plusoption = FALSE; - static char *propt(); static char *optstring(); static int flip_triple(); extern int screen_trashed; extern char *every_first_cmd; /* * Scan an argument (either from the command line or from the * LESS environment variable) and process it. --- 24,66 ---- static struct loption *pendopt; public int plusoption = FALSE; static char *optstring(); static int flip_triple(); extern int screen_trashed; + extern int less_is_more; + extern int quit_at_eof; extern char *every_first_cmd; + /* + * Return a printable description of an option. + */ + static char * + opt_desc(o) + 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 char * + propt(c) + int c; + { + static char buf[8]; + + snprintf(buf, sizeof(buf), "-%s", prchar(c)); + return (buf); + } + /* * Scan an argument (either from the command line or from the * LESS environment variable) and process it. *************** *** 67,73 **** (*pendopt->ofunc)(INIT, s); break; case NUMBER: ! printopt = propt(pendopt->oletter); *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL); break; } --- 97,103 ---- (*pendopt->ofunc)(INIT, s); break; case NUMBER: ! printopt = opt_desc(pendopt); *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL); break; } *************** *** 134,139 **** --- 164,173 ---- s--; optc = 'z'; break; + case 'n': + if (less_is_more) + optc = 'z'; + break; } /* *************** *** 144,157 **** if (optname == NULL) { printopt = propt(optc); ! lc = SIMPLE_IS_LOWER(optc); o = findopt(optc); } #if GNU_OPTIONS else { printopt = optname; ! lc = SIMPLE_IS_LOWER(optname[0]); o = findopt_name(&optname, NULL, &err); s = optname; optname = NULL; --- 178,191 ---- if (optname == NULL) { printopt = propt(optc); ! lc = ASCII_IS_LOWER(optc); o = findopt(optc); } #if GNU_OPTIONS else { printopt = optname; ! lc = ASCII_IS_LOWER(optname[0]); o = findopt_name(&optname, NULL, &err); s = optname; optname = NULL; *************** *** 260,271 **** * OPT_SET set to the inverse of the default value */ public void ! toggle_option(c, s, how_toggle) ! int c; char *s; int how_toggle; { - register struct loption *o; register int num; int no_prompt; int err; --- 294,305 ---- * OPT_SET set to the inverse of the default value */ public void ! toggle_option(o, lower, s, how_toggle) ! struct loption *o; ! int lower; char *s; int how_toggle; { register int num; int no_prompt; int err; *************** *** 274,300 **** no_prompt = (how_toggle & OPT_NO_PROMPT); how_toggle &= ~OPT_NO_PROMPT; - /* - * Look up the option letter in the option table. - */ - o = findopt(c); if (o == NULL) { ! parg.p_string = propt(c); ! error("There is no %s option", &parg); return; } if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE)) { ! parg.p_string = propt(c); error("Cannot change the %s option", &parg); return; ! } if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY)) { ! parg.p_string = propt(c); error("Cannot query the %s option", &parg); return; } --- 308,329 ---- 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; } *************** *** 354,368 **** switch (how_toggle) { case OPT_TOGGLE: ! *(o->ovar) = flip_triple(*(o->ovar), ! islower(c)); break; case OPT_UNSET: *(o->ovar) = o->odefault; break; case OPT_SET: ! *(o->ovar) = flip_triple(o->odefault, ! islower(c)); break; } break; --- 383,395 ---- 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; *************** *** 464,496 **** } /* ! * Return a string suitable for printing as the "name" of an option. ! * For example, if the option letter is 'x', just return "-x". */ - static char * - propt(c) - int c; - { - static char buf[8]; - - snprintf(buf, sizeof(buf), "-%s", prchar(c)); - return (buf); - } - - /* - * Determine if an option is a single character option (BOOL or TRIPLE), - * or if it a multi-character option (NUMBER). - */ public int ! single_char_option(c) ! int c; { - register struct loption *o; - - o = findopt(c); if (o == NULL) ! return (TRUE); ! return ((o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) != 0); } /* --- 491,507 ---- } /* ! * Determine if an option takes a parameter. */ public int ! opt_has_param(o) ! struct loption *o; { if (o == NULL) ! return (0); ! if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) ! return (0); ! return (1); } /* *************** *** 498,511 **** * Only string and number valued options have prompts. */ public char * ! opt_prompt(c) ! int c; { - register struct loption *o; - - o = findopt(c); if (o == NULL || (o->otype & (STRING|NUMBER)) == 0) ! return (NULL); return (o->odesc[0]); } --- 509,519 ---- * Only string and number valued options have prompts. */ public char * ! opt_prompt(o) ! struct loption *o; { if (o == NULL || (o->otype & (STRING|NUMBER)) == 0) ! return ("?"); return (o->odesc[0]); } *************** *** 540,546 **** public void nopendopt() { ! nostring(propt(pendopt->oletter)); } /* --- 548,554 ---- public void nopendopt() { ! nostring(opt_desc(pendopt)); } /* *************** *** 589,594 **** --- 597,626 ---- } /* + */ + static int + num_error(printopt, errp) + char *printopt; + int *errp; + { + PARG parg; + + if (errp != NULL) + { + *errp = TRUE; + return (-1); + } + if (printopt != NULL) + { + parg.p_string = printopt; + error("Number is required after %s", &parg); + } + quit(QUIT_ERROR); + /* NOTREACHED */ + 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. *************** *** 602,608 **** register char *s; register int n; register int neg; - PARG parg; s = skipsp(*sp); neg = FALSE; --- 634,639 ---- *************** *** 612,630 **** s++; } if (*s < '0' || *s > '9') ! { ! if (errp != NULL) ! { ! *errp = TRUE; ! return (-1); ! } ! if (printopt != NULL) ! { ! parg.p_string = printopt; ! error("Number is required after %s", &parg); ! } ! quit(QUIT_ERROR); ! } n = 0; while (*s >= '0' && *s <= '9') --- 643,649 ---- s++; } if (*s < '0' || *s > '9') ! return (num_error(printopt, errp)); n = 0; while (*s >= '0' && *s <= '9') *************** *** 635,638 **** --- 654,707 ---- if (neg) n = -n; return (n); + } + + /* + * 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(sp, printopt, errp) + char **sp; + char *printopt; + int *errp; + { + register char *s; + long frac = 0; + int fraclen = 0; + + s = skipsp(*sp); + if (*s < '0' || *s > '9') + return (num_error(printopt, errp)); + + for ( ; *s >= '0' && *s <= '9'; s++) + { + frac = (frac * 10) + (*s - '0'); + fraclen++; + } + if (fraclen > NUM_LOG_FRAC_DENOM) + while (fraclen-- > NUM_LOG_FRAC_DENOM) + frac /= 10; + else + while (fraclen++ < NUM_LOG_FRAC_DENOM) + frac *= 10; + *sp = s; + if (errp != NULL) + *errp = FALSE; + return (frac); + } + + + /* + * Get the value of the -e flag. + */ + public int + get_quit_at_eof() + { + 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_ON : OPT_ONPLUS; }