home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / opts < prev    next >
Encoding:
Text File  |  1990-03-18  |  7.9 KB  |  388 lines

  1. /* opts.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    16820 SW Tallac Way
  6.  *    Beaverton, OR 97006
  7.  *    kirkenda@jove.cs.pdx.edu, or ...uunet!tektronix!psueea!jove!kirkenda
  8.  */
  9.  
  10.  
  11. /* This file contains the code that manages the run-time options -- The 
  12.  * values that can be modified via the "set" command.
  13.  */
  14.  
  15. #include "vi.h"
  16. #ifndef NULL
  17. #define NULL (char *)0
  18. #endif
  19. extern char    *getenv();
  20.  
  21. /* These are the default values of all options */
  22. char    o_autoindent[1] =    {FALSE};
  23. char    o_autowrite[1] =     {FALSE};
  24. #ifndef SET_NOCHARATTR
  25. char    o_charattr[1] =        {FALSE};
  26. #endif
  27. char    o_columns[3] =        {80, 32, 255};
  28. char    o_exrefresh[1] =    {TRUE};
  29. char    o_ignorecase[1] =    {FALSE};
  30. #ifdef M_SYSV
  31. char    o_keytime[3] =        {0, 0, 2};
  32. #else
  33. char    o_keytime[3] =        {2, 0, 2};
  34. #endif
  35. char    o_keywordprg[80] =    KEYWORDPRG;
  36. char    o_lines[3] =        {25, 2, 50};    /* More lines? Enlarge kbuf */
  37. char    o_magic[1] =        {TRUE};
  38. char    o_paragraphs[30] =    "PPppPApa";
  39. char    o_readonly[1] =        {FALSE};
  40. char    o_report[3] =        {5, 1, 127};
  41. char    o_scroll[3] =        {12, 1, 127};
  42. char    o_sections[30] =    "SEseSHsh";
  43. char    o_shell[60] =        "/bin/sh";
  44. char    o_shiftwidth[3] =    {8, 1, 255};
  45. char    o_sidescroll[3] =    {8, 1, 40};
  46. char    o_tabstop[3] =        {8, 1, 40};
  47. char    o_term[30] =        "?";
  48. char    o_vbell[1] =        {TRUE};
  49. char    o_wrapmargin[3] =    {0, 0, 255};
  50. char    o_wrapscan[1] =        {TRUE};
  51. #ifdef ARC
  52. char     o_colourb[3] =        {0, 0, 7};
  53. char    o_colourf[3] =        {2, 0, 7};
  54. #endif
  55.  
  56.  
  57. /* The following describes the names & types of all options */
  58. #define BOOL    0
  59. #define    NUM    1
  60. #define    STR    2
  61. #define SET    0x01
  62. #define CANSET    0x02
  63. struct
  64. {
  65.     char    *name;    /* name of an option */
  66.     char    *nm;    /* short name of an option */
  67.     char    type;    /* type of an option */
  68.     char    flags;    /* boolean: has this option been set? */
  69.     char    *value;    /* value */
  70. }
  71.     opts[] =
  72. {
  73.     /* name            type    flags    value */
  74.     { "autoindent",    "ai",    BOOL,    CANSET,    o_autoindent    },
  75.     { "autowrite",    "aw",    BOOL,    CANSET,    o_autowrite    },
  76. #ifndef SET_NOCHARATTR
  77.     { "charattr",    "ca",    BOOL,    CANSET,    o_charattr    },
  78. #endif
  79.     { "columns",    "co",    NUM,    CANSET,    o_columns    },
  80. #ifdef ARC
  81.     { "colourbak",    "cb",    NUM,    CANSET,    o_colourb    },
  82.     { "colourfor",    "cf",    NUM,    CANSET,    o_colourf    },
  83. #endif
  84.     { "exrefresh",    "er",    BOOL,    CANSET,    o_exrefresh    },
  85.     { "ignorecase",    "ic",    BOOL,    CANSET,    o_ignorecase    },
  86.     { "keytime",    "kt",    NUM,    CANSET,    o_keytime    },
  87.     { "keywordprg",    "kp",    STR,    CANSET,    o_keywordprg    },
  88.     { "lines",    "li",    NUM,    SET,    o_lines        },
  89.     { "magic",    "mg",    BOOL,    CANSET,    o_magic        },
  90.     { "paragraphs",    "pa",    STR,    CANSET,    o_paragraphs    },
  91.     { "readonly",    "ro",    BOOL,    CANSET,    o_readonly    },
  92.     { "report",    "re",    NUM,    CANSET,    o_report    },
  93.     { "scroll",    "sc",    NUM,    CANSET,    o_scroll    },
  94.     { "sections",    "se",    STR,    CANSET,    o_sections    },
  95.     { "shell",    "sh",    STR,    CANSET,    o_shell        },
  96.     { "shiftwidth",    "sw",    NUM,    CANSET,    o_shiftwidth    },
  97.     { "sidescroll",    "ss",    NUM,    CANSET,    o_sidescroll    },
  98.     { "tabstop",    "ts",    NUM,    CANSET,    o_tabstop    },
  99.     { "term",    "te",    STR,    SET,    o_term        },
  100.     { "vbell",    "vb",    BOOL,    CANSET,    o_vbell        },
  101.     { "wrapmargin",    "wm",    NUM,    CANSET,    o_wrapmargin    },
  102.     { "wrapscan",    "ws",    BOOL,    CANSET,    o_wrapscan    },
  103.     { NULL, NULL, 0, CANSET, NULL }
  104. };
  105.  
  106.  
  107. /* This function initializes certain options from environment variables, etc. */
  108. initopts()
  109. {
  110.     char    *val;
  111.     int    i;
  112.  
  113.     /* set some stuff from environment variables */
  114.     if (val = getenv("SHELL")) /* yes, ASSIGNMENT! */
  115.     {
  116.         strcpy(o_shell, val);
  117.     }
  118.     if (val = getenv("TERM")) /* yes, ASSIGNMENT! */
  119.     {
  120.         strcpy(o_term, val);
  121.     }
  122.  
  123.     /* set some stuff according to the screen size */
  124.     *o_lines = LINES;
  125.     *o_columns = COLS;
  126.     *o_scroll = LINES / 2 - 1;
  127.  
  128.     /* disable the vbell option if we don't know how to do a vbell */
  129.     if (!VB)
  130.     {
  131.         for (i = 0; opts[i].value != o_vbell; i++)
  132.         {
  133.         }
  134.         opts[i].flags &= ~CANSET;
  135.         *o_vbell = FALSE;
  136.     }
  137. }
  138.  
  139. /* This function lists the current values of all options */
  140. dumpopts(all)
  141.     int    all;    /* boolean: dump all options, or just set ones? */
  142. {
  143.     int    i;
  144.     int    col;
  145.  
  146.     for (i = col = 0; opts[i].name; i++)
  147.     {
  148.         /* if not set and not all, ignore this option */
  149.         if (!all && !(opts[i].flags & SET))
  150.         {
  151.             continue;
  152.         }
  153.  
  154.         /* align this option in one of the columns */
  155.         if (col > 52)
  156.         {
  157.             addch('\n');
  158.             col = 0;
  159.         }
  160.         else if (col > 26)
  161.         {
  162.             while (col < 52)
  163.             {
  164.                 qaddch(' ');
  165.                 col++;
  166.             }
  167.         }
  168.         else if (col > 0)
  169.         {
  170.             while (col < 26)
  171.             {
  172.                 qaddch(' ');
  173.                 col++;
  174.             }
  175.         }
  176.  
  177.         switch (opts[i].type)
  178.         {
  179.           case BOOL:
  180.             if (!*opts[i].value)
  181.             {
  182.                 qaddch('n');
  183.                 qaddch('o');
  184.                 col += 2;
  185.             }
  186.             addstr(opts[i].name);
  187.             col += strlen(opts[i].name);
  188.             break;
  189.  
  190.           case NUM:
  191.             wprintw(stdscr, "%s=%-3d", opts[i].name, *opts[i].value & 0xff);
  192.             col += 4 + strlen(opts[i].name);
  193.             break;
  194.  
  195.           case STR:
  196.             wprintw(stdscr, "%s=\"%s\"", opts[i].name, opts[i].value);
  197.             col += 3 + strlen(opts[i].name) + strlen(opts[i].value);
  198.             break;
  199.         }
  200.         exrefresh();
  201.     }
  202.     if (col > 0)
  203.     {
  204.         addch('\n');
  205.         exrefresh();
  206.     }
  207. }
  208.  
  209. /* This function saves the current configuarion of options to a file */
  210. saveopts(fd)
  211.     int    fd;    /* file descriptor to write to */
  212. {
  213.     int    i;
  214.  
  215.     /* HACK! refresh the screen so we can borrow its buffer */
  216.     refresh();
  217.  
  218.     /* write each set options */
  219.     for (i = 0; opts[i].name; i++)
  220.     {
  221.         /* if unset or unsettable, ignore this option */
  222.         if (!(opts[i].flags & SET) || !(opts[i].flags & CANSET))
  223.         {
  224.             continue;
  225.         }
  226.  
  227.         qaddstr("set ");
  228.         switch (opts[i].type)
  229.         {
  230.           case BOOL:
  231.             if (!*opts[i].value)
  232.             {
  233.                 qaddch('n');
  234.                 qaddch('o');
  235.             }
  236.             addstr(opts[i].name);
  237.             break;
  238.  
  239.           case NUM:
  240.             wprintw(stdscr, "%s=%-3d", opts[i].name, *opts[i].value & 0xff);
  241.             break;
  242.  
  243.           case STR:
  244.             wprintw(stdscr, "%s=\"%s\"", opts[i].name, opts[i].value);
  245.             break;
  246.         }
  247.         qaddch('\n');
  248.     }
  249.  
  250.     /* write the buffered stuff to the file */
  251.     if (stdscr != kbuf)
  252.     {
  253.         write(fd, kbuf, (int)(stdscr - kbuf));
  254.         stdscr = kbuf;
  255.     }
  256. }
  257.  
  258.  
  259. /* This function changes the values of one or more options. */
  260. setopts(assignments)
  261.     char    *assignments;    /* a string containing option assignments */
  262. {
  263.     char    *name;        /* name of variable in assignments */
  264.     char    *value;        /* value of the variable */
  265.     char    *scan;        /* used for moving through strings */
  266.     int    i, j;
  267.  
  268.     /* for each assignment... */
  269.     for (name = assignments; *name; )
  270.     {
  271.         /* skip whitespace */
  272.         if (*name == ' ' || *name == '\t')
  273.         {
  274.             name++;
  275.             continue;
  276.         }
  277.  
  278.         /* find the value, if any */
  279.         for (scan = name; *scan >= 'a' && *scan <= 'z'; scan++)
  280.         {
  281.         }
  282.         if (*scan == '=')
  283.         {
  284.             *scan++ = '\0';
  285.             if (*scan == '"')
  286.             {
  287.                 value = ++scan;
  288.                 while (*scan && *scan != '"')
  289.                 {
  290.                     scan++;
  291.                 }
  292.                 if (*scan)
  293.                 {
  294.                     *scan++ = '\0';
  295.                 }
  296.             }
  297.             else
  298.             {
  299.                 value = scan;
  300.                 while (*scan && *scan != ' ' && *scan != '\t')
  301.                 {
  302.                     scan++;
  303.                 }
  304.                 if (*scan)
  305.                 {
  306.                     *scan++ = '\0';
  307.                 }
  308.             }
  309.         }
  310.         else
  311.         {
  312.             if (*scan)
  313.             {
  314.                 *scan++ = '\0';
  315.             }
  316.             value = NULL;
  317.             if (name[0] == 'n' && name[1] == 'o')
  318.             {
  319.                 name += 2;
  320.             }
  321.         }
  322.  
  323.         /* find the variable */
  324.         for (i = 0;
  325.              opts[i].name && strcmp(opts[i].name, name) && strcmp(opts[i].nm, name);
  326.              i++)
  327.         {
  328.         }
  329.  
  330.         /* change the variable */
  331.         if (!opts[i].name)
  332.         {
  333.             msg("invalid option name \"%s\"", name);
  334.         }
  335.         else if (!(opts[i].flags & CANSET))
  336.         {
  337.             msg("option \"%s\" can't be altered", name);
  338.         }
  339.         else if (value)
  340.         {
  341.             switch (opts[i].type)
  342.             {
  343.               case BOOL:
  344.                 msg("option \"[no]%s\" is boolean", name);
  345.                 break;
  346.  
  347.               case NUM:
  348.                 j = atoi(value);
  349.                 if (j == 0 && *value != '0')
  350.                 {
  351.                     msg("option \"%s\" must have a numeric value", name);
  352.                 }
  353.                 else if (j < opts[i].value[1] || j > (opts[i].value[2] & 0xff))
  354.                 {
  355.                     msg("option \"%s\" must have a value between %d and %d",
  356.                         name, opts[i].value[1], opts[i].value[2] & 0xff);
  357.                 }
  358.                 else
  359.                 {
  360.                     *opts[i].value = atoi(value);
  361.                     opts[i].flags |= SET;
  362.                 }
  363.                 break;
  364.  
  365.               case STR:
  366.                 strcpy(opts[i].value, value);
  367.                 opts[i].flags |= SET;
  368.                 break;
  369.             }
  370.         }
  371.         else /* valid option, no value */
  372.         {
  373.             if (opts[i].type == BOOL)
  374.             {
  375.                 *opts[i].value = (name[-1] != 'o');
  376.                 opts[i].flags |= SET;
  377.             }
  378.             else
  379.             {
  380.                 msg("option \"%s\" must be given a value", name);
  381.             }
  382.         }
  383.  
  384.         /* move on to the next option */
  385.         name = scan;
  386.     }
  387. }
  388.