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