home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 036 / less232.zip / OPTFUNC.C < prev    next >
C/C++ Source or Header  |  1994-09-22  |  9KB  |  503 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994  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 ispipe;
  49. extern int cbufs;
  50. extern int pr_type;
  51. extern int nohelp;
  52. extern int plusoption;
  53. extern int swindow;
  54. extern int sc_height;
  55. extern int any_display;
  56. extern char *prproto[];
  57. extern char *eqproto;
  58. extern IFILE curr_ifile;
  59. #if LOGFILE
  60. extern char *namelogfile;
  61. extern int force_logfile;
  62. extern int logfile;
  63. extern char *glob();
  64. #endif
  65. #if TAGS
  66. public char *tagoption = NULL;
  67. extern char *tagfile;
  68. extern char *tags;
  69. extern int jump_sline;
  70. #endif
  71. #if MSOFTC
  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.     switch (type)
  92.     {
  93.     case INIT:
  94.         namelogfile = s;
  95.         break;
  96.     case TOGGLE:
  97.         if (!ispipe)
  98.         {
  99.             error("Input is not a pipe", NULL_PARG);
  100.             return;
  101.         }
  102.         if (logfile >= 0)
  103.         {
  104.             error("Log file is already in use", NULL_PARG);
  105.             return;
  106.         }
  107.         s = skipsp(s);
  108.         namelogfile = glob(s);
  109.         if (namelogfile == NULL)
  110.             namelogfile = save(s);
  111.         use_logfile(namelogfile);
  112.         sync_logfile();
  113.         break;
  114.     case QUERY:
  115.         if (logfile < 0)
  116.             error("No log file", NULL_PARG);
  117.         else
  118.         {
  119.             parg.p_string = namelogfile;
  120.             error("Log file \"%s\"", &parg);
  121.         }
  122.         break;
  123.     }
  124. }
  125.  
  126. /*
  127.  * Handler for -O option.
  128.  */
  129.     public void
  130. opt__O(type, s)
  131.     int type;
  132.     char *s;
  133. {
  134.     force_logfile = 1;
  135.     opt_o(type, s);
  136. }
  137.  
  138. /*
  139.  * Handlers for obsolete -l and -L options.
  140.  */
  141.     public void
  142. opt_l(type, s)
  143.     int type;
  144.     char *s;
  145. {
  146.     int err;
  147.     char *t;
  148.     
  149.     switch (type)
  150.     {
  151.     case INIT:
  152.         t = s;
  153.         (void) getnum(&t, 'l', &err);
  154.         if (err || *t != '\0')
  155.         {
  156.             error("The -l option is obsolete.  Use -o", NULL_PARG);
  157.             return;
  158.         }
  159.         plusoption = 1;
  160.         ungetsc(s);
  161.         break;
  162.     case QUERY:
  163.         error("Line number is required after -l", NULL_PARG);
  164.         break;
  165.     }
  166. }
  167.  
  168.     /*ARGSUSED*/
  169.     public void
  170. opt__L(type, s)
  171.     int type;
  172.     char *s;
  173. {
  174.     error("The -L option is obsolete.  Use -O", NULL_PARG);
  175. }
  176. #endif
  177.  
  178. #if USERFILE
  179.     public void
  180. opt_k(type, s)
  181.     int type;
  182.     char *s;
  183. {
  184.     PARG parg;
  185.  
  186.     switch (type)
  187.     {
  188.     case INIT:
  189.         if (add_cmdtable(s))
  190.         {
  191.             parg.p_string = s;
  192.             error("Cannot use lesskey file \"%s\"", &parg);
  193.         }
  194.         break;
  195.     case QUERY:
  196.     case TOGGLE:
  197.         error("Cannot query the -k flag", NULL_PARG);
  198.         break;
  199.     }
  200. }
  201. #endif
  202.  
  203. #if TAGS
  204. /*
  205.  * Handler for -t option.
  206.  */
  207.     public void
  208. opt_t(type, s)
  209.     int type;
  210.     char *s;
  211. {
  212.     IFILE save_ifile;
  213.     POSITION pos;
  214.  
  215.     switch (type)
  216.     {
  217.     case INIT:
  218.         tagoption = s;
  219.         /* Do the rest in main() */
  220.         break;
  221.     case TOGGLE:
  222.         findtag(skipsp(s));
  223.         if (tagfile == NULL)
  224.             break;
  225.         save_ifile = curr_ifile;
  226.         if (edit(tagfile))
  227.             break;
  228.         if ((pos = tagsearch()) == NULL_POSITION)
  229.         {
  230.             if (edit_ifile(save_ifile))
  231.                 quit(-1);
  232.             break;
  233.         }
  234.         jump_loc(pos, jump_sline);
  235.         break;
  236.     case QUERY:
  237.         error("Tag is required after -t", NULL_PARG);
  238.         break;
  239.     }
  240. }
  241.  
  242. /*
  243.  * Handler for -T option.
  244.  */
  245.     public void
  246. opt__T(type, s)
  247.     int type;
  248.     char *s;
  249. {
  250.     PARG parg;
  251.  
  252.     switch (type)
  253.     {
  254.     case INIT:
  255.         tags = s;
  256.         break;
  257.     case TOGGLE:
  258.         s = skipsp(s);
  259.         tags = glob(s);
  260.         if (tags == NULL)
  261.             tags = save(s);
  262.         break;
  263.     case QUERY:
  264.         parg.p_string = tags;
  265.         error("Tags file \"%s\"", &parg);
  266.         break;
  267.     }
  268. }
  269. #endif
  270.  
  271. /*
  272.  * Handler for -p option.
  273.  */
  274.     public void
  275. opt_p(type, s)
  276.     int type;
  277.     register char *s;
  278. {
  279.     switch (type)
  280.     {
  281.     case INIT:
  282.         /*
  283.          * Unget a search command for the specified string.
  284.          * {{ This won't work if the "/" command is
  285.          *    changed or invalidated by a .lesskey file. }}
  286.          */
  287.         plusoption = 1;
  288.         ungetsc(s);
  289.         ungetsc("/");
  290.         break;
  291.     case QUERY:
  292.         error("Pattern is required after -p", NULL_PARG);
  293.         break;
  294.     }
  295. }
  296.  
  297. /*
  298.  * Handler for -P option.
  299.  */
  300.     public void
  301. opt__P(type, s)
  302.     int type;
  303.     register char *s;
  304. {
  305.     register char **proto;
  306.     PARG parg;
  307.  
  308.     switch (type)
  309.     {
  310.     case INIT:
  311.     case TOGGLE:
  312.         /*
  313.          * Figure out which prototype string should be changed.
  314.          */
  315.         switch (*s)
  316.         {
  317.         case 'm':  proto = &prproto[PR_MEDIUM];    s++;    break;
  318.         case 'M':  proto = &prproto[PR_LONG];    s++;    break;
  319.         case '=':  proto = &eqproto;        s++;    break;
  320.         default:   proto = &prproto[PR_SHORT];        break;
  321.         }
  322.         free(*proto);
  323.         *proto = save(s);
  324.         break;
  325.     case QUERY:
  326.         parg.p_string = prproto[pr_type];
  327.         error("%s", &parg);
  328.         break;
  329.     }
  330. }
  331.  
  332. /*
  333.  * Handler for the -b option.
  334.  */
  335.     /*ARGSUSED*/
  336.     public void
  337. opt_b(type, s)
  338.     int type;
  339.     char *s;
  340. {
  341.     switch (type)
  342.     {
  343.     case TOGGLE:
  344.     case QUERY:
  345.         /*
  346.          * Allocate the new number of buffers.
  347.          */
  348.         cbufs = ch_nbuf(cbufs);
  349.         break;
  350.     case INIT:
  351.         break;
  352.     }
  353. }
  354.  
  355. /*
  356.  * Handler for the -i option.
  357.  */
  358.     /*ARGSUSED*/
  359.     public void
  360. opt_i(type, s)
  361.     int type;
  362.     char *s;
  363. {
  364.     switch (type)
  365.     {
  366.     case TOGGLE:
  367.         chg_caseless();
  368.         break;
  369.     case QUERY:
  370.     case INIT:
  371.         break;
  372.     }
  373. }
  374.  
  375. #if MSOFTC
  376. /*
  377.  *
  378.  */
  379.        static void
  380. colordesc(s, fg_color, bg_color)
  381.     char *s;
  382.     int *fg_color;
  383.     int *bg_color;
  384. {
  385.     int fg, bg;
  386.     int err;
  387.     
  388.     fg = getnum(&s, 'D', &err);
  389.     if (err)
  390.     {
  391.         error("Missing fg color in -D", NULL_PARG);
  392.         return;
  393.     }
  394.     if (*s != '.')
  395.         bg = 0;
  396.     else
  397.     {
  398.         s++;
  399.         bg = getnum(&s, 'D', &err);
  400.         if (err)
  401.         {
  402.             error("Missing fg color in -D", NULL_PARG);
  403.             return;
  404.         }
  405.     }
  406.     *fg_color = fg;
  407.     *bg_color = bg;
  408. }
  409.  
  410. /*
  411.  * Handler for the -D option.
  412.  */
  413.     /*ARGSUSED*/
  414.     public void
  415. opt_D(type, s)
  416.     int type;
  417.     char *s;
  418. {
  419.     switch (type)
  420.     {
  421.     case INIT:
  422.     case TOGGLE:
  423.         switch (*s++)
  424.         {
  425.         case 'n':
  426.             colordesc(s, &nm_fg_color, &nm_bg_color);
  427.             break;
  428.         case 'd':
  429.             colordesc(s, &bo_fg_color, &bo_bg_color);
  430.             break;
  431.         case 'u':
  432.             colordesc(s, &ul_fg_color, &ul_bg_color);
  433.             break;
  434.         case 'k':
  435.             colordesc(s, &bl_fg_color, &bl_bg_color);
  436.             break;
  437.         case 's':
  438.             colordesc(s, &so_fg_color, &so_bg_color);
  439.             break;
  440.         default:
  441.             error("-D must be followed by n, d, u, k or s", NULL_PARG);
  442.             break;
  443.         }
  444.         if (type == TOGGLE)
  445.         {
  446.             so_enter();
  447.             so_exit();
  448.         }
  449.         break;
  450.     case QUERY:
  451.         break;
  452.     }
  453. }
  454. #endif
  455.  
  456. /*
  457.  * "-?" means display a help message.
  458.  * If from the command line, exit immediately.
  459.  */
  460.     /*ARGSUSED*/
  461.     public void
  462. opt_query(type, s)
  463.     int type;
  464.     char *s;
  465. {
  466.     if (nohelp)
  467.         return;
  468.     switch (type)
  469.     {
  470.     case QUERY:
  471.     case TOGGLE:
  472.         error("Use \"h\" for help", NULL_PARG);
  473.         break;
  474.     case INIT:
  475.         /*
  476.          * This is "less -?".
  477.          * It rather ungracefully grabs control, 
  478.          * does the initializations normally done in main,
  479.          * shows the help file and exits.
  480.          */
  481.         raw_mode(1);
  482.         get_term();
  483.         open_getchr();
  484.         init();
  485.         any_display = 1;
  486.         help(1);
  487.         quit(0);
  488.         /*NOTREACHED*/
  489.     }
  490. }
  491.  
  492. /*
  493.  * Get the "screen window" size.
  494.  */
  495.     public int
  496. get_swindow()
  497. {
  498.     if (swindow > 0)
  499.         return (swindow);
  500.     return (sc_height + swindow);
  501. }
  502.  
  503.