home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / LESS177.ZIP / src / optfunc.c < prev    next >
C/C++ Source or Header  |  1992-07-18  |  6KB  |  377 lines

  1. /*
  2.  * Handling functions for command line options.
  3.  *
  4.  * Most options are handled by the generic code in option.c.
  5.  * But all string options, and a few non-string options, require
  6.  * special handling specific to the particular option.
  7.  * This special processing is done by the "handling functions" in this file.
  8.  *
  9.  * Each handling function is passed a "type" and, if it is a string
  10.  * option, the string which should be "assigned" to the option.
  11.  * The type may be one of:
  12.  *    INIT    The option is being initialized from the command line.
  13.  *    TOGGLE    The option is being changed from within the program.
  14.  *    QUERY    The setting of the option is merely being queried.
  15.  */
  16.  
  17. #include "less.h"
  18. #include "option.h"
  19.  
  20. extern int nbufs;
  21. extern int ispipe;
  22. extern int cbufs;
  23. extern int pr_type;
  24. extern int nohelp;
  25. extern int plusoption;
  26. extern char *prproto[];
  27. extern char *eqproto;
  28. extern IFILE curr_ifile;
  29. #if LOGFILE
  30. extern char *namelogfile;
  31. extern int force_logfile;
  32. extern int logfile;
  33. extern char *glob();
  34. #endif
  35. #if TAGS
  36. public int tagoption = 0;
  37. extern char *tagfile;
  38. extern char *tagpattern;
  39. extern char *tags;
  40. #endif
  41. #if __MSDOS__
  42. public char *window_box = NULL;
  43. extern int  directvideo;
  44. extern int  output_mode;
  45. #endif
  46.  
  47.  
  48. #if LOGFILE
  49. /*
  50.  * Handler for -o option.
  51.  */
  52.     public void
  53. opt_o(type, s)
  54.     int type;
  55.     char *s;
  56. {
  57.     PARG parg;
  58.  
  59.     switch (type)
  60.     {
  61.     case INIT:
  62.         namelogfile = s;
  63.         break;
  64.     case TOGGLE:
  65.         if (!ispipe)
  66.         {
  67.             error("Input is not a pipe", NULL_PARG);
  68.             return;
  69.         }
  70.         if (logfile >= 0)
  71.         {
  72.             error("Log file is already in use", NULL_PARG);
  73.             return;
  74.         }
  75.         s = skipsp(s);
  76.         namelogfile = glob(s);
  77.         if (namelogfile == NULL)
  78.             namelogfile = save(s);
  79.         use_logfile(s);
  80.         sync_logfile();
  81.         break;
  82.     case QUERY:
  83.         if (logfile < 0)
  84.             error("No log file", NULL_PARG);
  85.         else
  86.         {
  87.             parg.p_string = namelogfile;
  88.             error("Log file \"%s\"", &parg);
  89.         }
  90.         break;
  91.     }
  92. }
  93.  
  94. /*
  95.  * Handler for -O option.
  96.  */
  97.     public void
  98. opt__O(type, s)
  99.     int type;
  100.     char *s;
  101. {
  102.     force_logfile = 1;
  103.     opt_o(type, s);
  104. }
  105.  
  106. /*
  107.  * Handlers for obsolete -l and -L options.
  108.  */
  109.     public void
  110. opt_l(type, s)
  111.     int type;
  112.     char *s;
  113. {
  114.     error("The -l option is obsolete.  Use -o", NULL_PARG);
  115. }
  116.  
  117.     public void
  118. opt__L(type, s)
  119.     int type;
  120.     char *s;
  121. {
  122.     error("The -L option is obsolete.  Use -O", NULL_PARG);
  123. }
  124. #endif
  125.  
  126. #if USERFILE
  127.     public void
  128. opt_k(type, s)
  129.     int type;
  130.     char *s;
  131. {
  132.     PARG parg;
  133.  
  134.     switch (type)
  135.     {
  136.     case INIT:
  137.         if (add_cmdtable(s))
  138.         {
  139.             parg.p_string = s;
  140.             error("Cannot use lesskey file \"%s\"", &parg);
  141.         }
  142.         break;
  143.     case QUERY:
  144.     case TOGGLE:
  145.         error("Cannot query the -k flag", NULL_PARG);
  146.         break;
  147.     }
  148. }
  149. #endif
  150.  
  151. #if TAGS
  152. /*
  153.  * Handler for -t option.
  154.  */
  155.     public void
  156. opt_t(type, s)
  157.     int type;
  158.     char *s;
  159. {
  160.     char *curr_filename;
  161.  
  162.     switch (type)
  163.     {
  164.     case INIT:
  165.         tagoption = 1;
  166.         findtag(s);
  167.         break;
  168.     case TOGGLE:
  169.         findtag(skipsp(s));
  170.         if (tagfile != NULL)
  171.         {
  172.             curr_filename = get_filename(curr_ifile);
  173.             if (edit(tagfile, 0) == 0)
  174.                 if (tagsearch())
  175.                     (void) edit(curr_filename, 0);
  176.         }
  177.         break;
  178.     case QUERY:
  179.         error("Tag is required after -t", NULL_PARG);
  180.         break;
  181.     }
  182. }
  183.  
  184. /*
  185.  * Handler for -T option.
  186.  */
  187.     public void
  188. opt__T(type, s)
  189.     int type;
  190.     char *s;
  191. {
  192.     PARG parg;
  193.  
  194.     switch (type)
  195.     {
  196.     case INIT:
  197.         tags = s;
  198.         break;
  199.     case TOGGLE:
  200.         s = skipsp(s);
  201.         tags = glob(s);
  202.         if (tags == NULL)
  203.             tags = save(s);
  204.         break;
  205.     case QUERY:
  206.         parg.p_string = tags;
  207.         error("Tags file \"%s\"", &parg);
  208.         break;
  209.     }
  210. }
  211. #endif
  212.  
  213. /*
  214.  * Handler for -p option.
  215.  */
  216.     public void
  217. opt_p(type, s)
  218.     int type;
  219.     register char *s;
  220. {
  221.     switch (type)
  222.     {
  223.     case INIT:
  224.         /*
  225.          * Unget a search command for the specified string.
  226.          * {{ This won't work if the "/" command is
  227.          *    changed or invalidated by a .lesskey file. }}
  228.          */
  229.         plusoption = 1;
  230.         ungetsc(s);
  231.         ungetsc("/");
  232.         break;
  233.     case QUERY:
  234.         error("Pattern is required after -p", NULL_PARG);
  235.         break;
  236.     }
  237. }
  238.  
  239. /*
  240.  * Handler for -P option.
  241.  */
  242.     public void
  243. opt__P(type, s)
  244.     int type;
  245.     register char *s;
  246. {
  247.     register char **proto;
  248.     PARG parg;
  249.  
  250.     switch (type)
  251.     {
  252.     case INIT:
  253.     case TOGGLE:
  254.         /*
  255.          * Figure out which prototype string should be changed.
  256.          */
  257.         switch (*s)
  258.         {
  259.         case 'm':  proto = &prproto[PR_MEDIUM];    s++;    break;
  260.         case 'M':  proto = &prproto[PR_LONG];    s++;    break;
  261.         case '=':  proto = &eqproto;        s++;    break;
  262.         default:   proto = &prproto[pr_type];        break;
  263.         }
  264.         free(*proto);
  265.         *proto = save(s);
  266.         break;
  267.     case QUERY:
  268.         parg.p_string = prproto[pr_type];
  269.         error("%s", &parg);
  270.         break;
  271.     }
  272. }
  273.  
  274. /*
  275.  * Handler for the -b option.
  276.  */
  277.     /*ARGSUSED*/
  278.     public void
  279. opt_b(type, s)
  280.     int type;
  281.     char *s;
  282. {
  283.     switch (type)
  284.     {
  285.     case TOGGLE:
  286.     case QUERY:
  287.         /*
  288.          * Allocate the new number of buffers.
  289.          */
  290.         cbufs = ch_nbuf(cbufs);
  291.         break;
  292.     case INIT:
  293.         break;
  294.     }
  295. }
  296.  
  297. #if __MSDOS__
  298. /*
  299.  * Handler for -v option. (use BIOS or direct video)
  300.  */
  301.     public void
  302. opt_v(type, s)
  303.     int type;
  304.     register char *s;
  305. {
  306.     switch (type)
  307.     {
  308.     case INIT:
  309.     case TOGGLE:
  310.         if (output_mode == 2)
  311.             directvideo = 1;
  312.         else
  313.             directvideo = 0;
  314.         break;
  315.     case QUERY:
  316.         break;
  317.     }
  318. }
  319.  
  320. /*
  321.  * Handler for -W option. (set/modify window boundaries)
  322.  */
  323.     public void
  324. opt_W(type, s)
  325.     int type;
  326.     register char *s;
  327. {
  328.     PARG parg;
  329.  
  330.     switch (type)
  331.     {
  332.     case INIT:
  333.         window_box = save(s);
  334.         break;        /* get_term will take care of actually setting window */
  335. #ifdef MOVE_WINDOW
  336.     case TOGGLE:
  337.         if (window_box != NULL)
  338.             free(window_box);
  339.         window_box = save(s);
  340.         reset_window();
  341.         break;
  342. #endif
  343.     case QUERY:
  344.         parg.p_string = window_box;
  345.         error("%s", &parg);
  346.         break;
  347.     }
  348. }
  349. #endif
  350.  
  351. /*
  352.  * "-?" means display a help message.
  353.  * If from the command line, exit immediately.
  354.  */
  355.     /*ARGSUSED*/
  356.     public void
  357. opt_query(type, s)
  358.     int type;
  359.     char *s;
  360. {
  361.     if (nohelp)
  362.         return;
  363.     switch (type)
  364.     {
  365.     case QUERY:
  366.     case TOGGLE:
  367.         error("Use \"h\" for help", NULL_PARG);
  368.         break;
  369.     case INIT:
  370.         raw_mode(1);
  371.         init();
  372.         help();
  373.         quit(0);
  374.         /*NOTREACHED*/
  375.     }
  376. }
  377.