home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / less_332.lzh / less_332 / optfunc.c < prev    next >
C/C++ Source or Header  |  1998-03-03  |  9KB  |  533 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994,1995,1996  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Handling functions for command line options.
  30.  *
  31.  * Most options are handled by the generic code in option.c.
  32.  * But all string options, and a few non-string options, require
  33.  * special handling specific to the particular option.
  34.  * This special processing is done by the "handling functions" in this file.
  35.  *
  36.  * Each handling function is passed a "type" and, if it is a string
  37.  * option, the string which should be "assigned" to the option.
  38.  * The type may be one of:
  39.  *    INIT    The option is being initialized from the command line.
  40.  *    TOGGLE    The option is being changed from within the program.
  41.  *    QUERY    The setting of the option is merely being queried.
  42.  */
  43.  
  44. #include "less.h"
  45. #include "option.h"
  46.  
  47. extern int nbufs;
  48. extern int cbufs;
  49. extern int pr_type;
  50. extern int plusoption;
  51. extern int swindow;
  52. extern int sc_height;
  53. extern int secure;
  54. extern int dohelp;
  55. extern char openquote;
  56. extern char closequote;
  57. extern char *prproto[];
  58. extern char *eqproto;
  59. extern char *hproto;
  60. extern IFILE curr_ifile;
  61. #if LOGFILE
  62. extern char *namelogfile;
  63. extern int force_logfile;
  64. extern int logfile;
  65. #endif
  66. #if TAGS
  67. public char *tagoption = NULL;
  68. extern char *tags;
  69. extern int jump_sline;
  70. #endif
  71. #if MSDOS_COMPILER
  72. extern int nm_fg_color, nm_bg_color;
  73. extern int bo_fg_color, bo_bg_color;
  74. extern int ul_fg_color, ul_bg_color;
  75. extern int so_fg_color, so_bg_color;
  76. extern int bl_fg_color, bl_bg_color;
  77. #endif
  78.  
  79.  
  80. #if LOGFILE
  81. /*
  82.  * Handler for -o option.
  83.  */
  84.     public void
  85. opt_o(type, s)
  86.     int type;
  87.     char *s;
  88. {
  89.     PARG parg;
  90.  
  91.     if (secure)
  92.     {
  93.         error("log file support is not available", NULL_PARG);
  94.         return;
  95.     }
  96.     switch (type)
  97.     {
  98.     case INIT:
  99.         namelogfile = s;
  100.         break;
  101.     case TOGGLE:
  102.         if (ch_getflags() & CH_CANSEEK)
  103.         {
  104.             error("Input is not a pipe", NULL_PARG);
  105.             return;
  106.         }
  107.         if (logfile >= 0)
  108.         {
  109.             error("Log file is already in use", NULL_PARG);
  110.             return;
  111.         }
  112.         s = skipsp(s);
  113.         namelogfile = lglob(s);
  114.         use_logfile(namelogfile);
  115.         sync_logfile();
  116.         break;
  117.     case QUERY:
  118.         if (logfile < 0)
  119.             error("No log file", NULL_PARG);
  120.         else
  121.         {
  122.             parg.p_string = unquote_file(namelogfile);
  123.             error("Log file \"%s\"", &parg);
  124.             free(parg.p_string);
  125.         }
  126.         break;
  127.     }
  128. }
  129.  
  130. /*
  131.  * Handler for -O option.
  132.  */
  133.     public void
  134. opt__O(type, s)
  135.     int type;
  136.     char *s;
  137. {
  138.     force_logfile = TRUE;
  139.     opt_o(type, s);
  140. }
  141. #endif
  142.  
  143. /*
  144.  * Handlers for -l option.
  145.  */
  146.     public void
  147. opt_l(type, s)
  148.     int type;
  149.     char *s;
  150. {
  151.     int err;
  152.     int n;
  153.     char *t;
  154.     
  155.     switch (type)
  156.     {
  157.     case INIT:
  158.         t = s;
  159.         n = getnum(&t, 'l', &err);
  160.         if (err || n <= 0)
  161.         {
  162.             error("Line number is required after -l", NULL_PARG);
  163.             return;
  164.         }
  165.         plusoption = TRUE;
  166.         ungetsc(s);
  167.         break;
  168.     }
  169. }
  170.  
  171. #if USERFILE
  172.     public void
  173. opt_k(type, s)
  174.     int type;
  175.     char *s;
  176. {
  177.     PARG parg;
  178.  
  179.     switch (type)
  180.     {
  181.     case INIT:
  182.         if (lesskey(s))
  183.         {
  184.             parg.p_string = unquote_file(s);
  185.             error("Cannot use lesskey file \"%s\"", &parg);
  186.             free(parg.p_string);
  187.         }
  188.         break;
  189.     }
  190. }
  191. #endif
  192.  
  193. #if TAGS
  194. /*
  195.  * Handler for -t option.
  196.  */
  197.     public void
  198. opt_t(type, s)
  199.     int type;
  200.     char *s;
  201. {
  202.     IFILE save_ifile;
  203.     POSITION pos;
  204.  
  205.     switch (type)
  206.     {
  207.     case INIT:
  208.         tagoption = s;
  209.         /* Do the rest in main() */
  210.         break;
  211.     case TOGGLE:
  212.         if (secure)
  213.         {
  214.             error("tags support is not available", NULL_PARG);
  215.             break;
  216.         }
  217.         findtag(skipsp(s));
  218.         save_ifile = save_curr_ifile();
  219.         if (edit_tagfile())
  220.             break;
  221.         if ((pos = tagsearch()) == NULL_POSITION)
  222.         {
  223.             reedit_ifile(save_ifile);
  224.             break;
  225.         }
  226.         unsave_ifile(save_ifile);
  227.         jump_loc(pos, jump_sline);
  228.         break;
  229.     }
  230. }
  231.  
  232. /*
  233.  * Handler for -T option.
  234.  */
  235.     public void
  236. opt__T(type, s)
  237.     int type;
  238.     char *s;
  239. {
  240.     PARG parg;
  241.  
  242.     switch (type)
  243.     {
  244.     case INIT:
  245.         tags = s;
  246.         break;
  247.     case TOGGLE:
  248.         s = skipsp(s);
  249.         tags = lglob(s);
  250.         break;
  251.     case QUERY:
  252.         parg.p_string = unquote_file(tags);
  253.         error("Tags file \"%s\"", &parg);
  254.         free(parg.p_string);
  255.         break;
  256.     }
  257. }
  258. #endif
  259.  
  260. /*
  261.  * Handler for -p option.
  262.  */
  263.     public void
  264. opt_p(type, s)
  265.     int type;
  266.     register char *s;
  267. {
  268.     switch (type)
  269.     {
  270.     case INIT:
  271.         /*
  272.          * Unget a search command for the specified string.
  273.          * {{ This won't work if the "/" command is
  274.          *    changed or invalidated by a .lesskey file. }}
  275.          */
  276.         plusoption = TRUE;
  277.         ungetsc(s);
  278.         ungetsc("/");
  279.         break;
  280.     }
  281. }
  282.  
  283. /*
  284.  * Handler for -P option.
  285.  */
  286.     public void
  287. opt__P(type, s)
  288.     int type;
  289.     register char *s;
  290. {
  291.     register char **proto;
  292.     PARG parg;
  293.  
  294.     switch (type)
  295.     {
  296.     case INIT:
  297.     case TOGGLE:
  298.         /*
  299.          * Figure out which prototype string should be changed.
  300.          */
  301.         switch (*s)
  302.         {
  303.         case 's':  proto = &prproto[PR_SHORT];    s++;    break;
  304.         case 'm':  proto = &prproto[PR_MEDIUM];    s++;    break;
  305.         case 'M':  proto = &prproto[PR_LONG];    s++;    break;
  306.         case '=':  proto = &eqproto;        s++;    break;
  307.         case 'h':  proto = &hproto;        s++;    break;
  308.         default:   proto = &prproto[PR_SHORT];        break;
  309.         }
  310.         free(*proto);
  311.         *proto = save(s);
  312.         break;
  313.     case QUERY:
  314.         parg.p_string = prproto[pr_type];
  315.         error("%s", &parg);
  316.         break;
  317.     }
  318. }
  319.  
  320. /*
  321.  * Handler for the -b option.
  322.  */
  323.     /*ARGSUSED*/
  324.     public void
  325. opt_b(type, s)
  326.     int type;
  327.     char *s;
  328. {
  329.     switch (type)
  330.     {
  331.     case TOGGLE:
  332.     case QUERY:
  333.         /*
  334.          * Allocate the new number of buffers.
  335.          */
  336.         cbufs = ch_nbuf(cbufs);
  337.         break;
  338.     case INIT:
  339.         break;
  340.     }
  341. }
  342.  
  343. /*
  344.  * Handler for the -i option.
  345.  */
  346.     /*ARGSUSED*/
  347.     public void
  348. opt_i(type, s)
  349.     int type;
  350.     char *s;
  351. {
  352.     switch (type)
  353.     {
  354.     case TOGGLE:
  355.         chg_caseless();
  356.         break;
  357.     case QUERY:
  358.     case INIT:
  359.         break;
  360.     }
  361. }
  362.  
  363. /*
  364.  * Handler for the -V option.
  365.  */
  366.     /*ARGSUSED*/
  367.     public void
  368. opt__V(type, s)
  369.     int type;
  370.     char *s;
  371. {
  372.     switch (type)
  373.     {
  374.     case TOGGLE:
  375.     case QUERY:
  376.     case INIT:
  377.         dispversion();
  378.         if (type == INIT)
  379.             quit(QUIT_OK);
  380.         break;
  381.     }
  382. }
  383.  
  384. #if MSDOS_COMPILER
  385. /*
  386.  *
  387.  */
  388.        static void
  389. colordesc(s, fg_color, bg_color)
  390.     char *s;
  391.     int *fg_color;
  392.     int *bg_color;
  393. {
  394.     int fg, bg;
  395.     int err;
  396.     
  397.     fg = getnum(&s, 'D', &err);
  398.     if (err)
  399.     {
  400.         error("Missing fg color in -D", NULL_PARG);
  401.         return;
  402.     }
  403.     if (*s != '.')
  404.         bg = 0;
  405.     else
  406.     {
  407.         s++;
  408.         bg = getnum(&s, 'D', &err);
  409.         if (err)
  410.         {
  411.             error("Missing fg color in -D", NULL_PARG);
  412.             return;
  413.         }
  414.     }
  415.     *fg_color = fg;
  416.     *bg_color = bg;
  417. }
  418.  
  419. /*
  420.  * Handler for the -D option.
  421.  */
  422.     /*ARGSUSED*/
  423.     public void
  424. opt_D(type, s)
  425.     int type;
  426.     char *s;
  427. {
  428.     switch (type)
  429.     {
  430.     case INIT:
  431.     case TOGGLE:
  432.         switch (*s++)
  433.         {
  434.         case 'n':
  435.             colordesc(s, &nm_fg_color, &nm_bg_color);
  436.             break;
  437.         case 'd':
  438.             colordesc(s, &bo_fg_color, &bo_bg_color);
  439.             break;
  440.         case 'u':
  441.             colordesc(s, &ul_fg_color, &ul_bg_color);
  442.             break;
  443.         case 'k':
  444.             colordesc(s, &bl_fg_color, &bl_bg_color);
  445.             break;
  446.         case 's':
  447.             colordesc(s, &so_fg_color, &so_bg_color);
  448.             break;
  449.         default:
  450.             error("-D must be followed by n, d, u, k or s", NULL_PARG);
  451.             break;
  452.         }
  453.         if (type == TOGGLE)
  454.         {
  455.             so_enter();
  456.             so_exit();
  457.         }
  458.         break;
  459.     case QUERY:
  460.         break;
  461.     }
  462. }
  463. #endif
  464.  
  465. /*
  466.  * Handler for the -" option.
  467.  */
  468.     public void
  469. opt_quote(type, s)
  470.     int type;
  471.     register char *s;
  472. {
  473.     char buf[3];
  474.     PARG parg;
  475.  
  476.     switch (type)
  477.     {
  478.     case INIT:
  479.     case TOGGLE:
  480.         if (s[1] != '\0' && s[2] != '\0')
  481.         {
  482.             error("-\" must be followed by 1 or 2 chars", NULL_PARG);
  483.             return;
  484.         }
  485.         openquote = s[0];
  486.         if (s[1] == '\0')
  487.             closequote = openquote;
  488.         else
  489.             closequote = s[1];
  490.         break;
  491.     case QUERY:
  492.         buf[0] = openquote;
  493.         buf[1] = closequote;
  494.         buf[2] = '\0';
  495.         parg.p_string = buf;
  496.         error("quotes %s", &parg);
  497.         break;
  498.     }
  499. }
  500.  
  501. /*
  502.  * "-?" means display a help message.
  503.  * If from the command line, exit immediately.
  504.  */
  505.     /*ARGSUSED*/
  506.     public void
  507. opt_query(type, s)
  508.     int type;
  509.     char *s;
  510. {
  511.     switch (type)
  512.     {
  513.     case QUERY:
  514.     case TOGGLE:
  515.         error("Use \"h\" for help", NULL_PARG);
  516.         break;
  517.     case INIT:
  518.         dohelp = 1;
  519.     }
  520. }
  521.  
  522. /*
  523.  * Get the "screen window" size.
  524.  */
  525.     public int
  526. get_swindow()
  527. {
  528.     if (swindow > 0)
  529.         return (swindow);
  530.     return (sc_height + swindow);
  531. }
  532.  
  533.