home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / rvi / part3 / rv_linecmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-09  |  5.8 KB  |  325 lines

  1. #include "rv.h"
  2. #include <ctype.h>
  3.  
  4. boolean    ed_undo;    /* Set TRUE if last mod was direct ed cmd */
  5.  
  6. extern boolean did_botprint;   /* used by botprint() */
  7.  
  8. static struct cm_cmdlist {  /* Commands */
  9.     char cm_cmd[12];
  10.     INT  cm_index;
  11. } cmdlist[] = {
  12.     "E",    3,
  13.     "Q",    2,
  14.     "edit", 3,
  15.     "file", 5,
  16.     "next", 4,
  17.     "quit",    2,
  18.     "se",    1,
  19.     "set",    1,
  20.     "sh",    8,
  21.     "shell",8,
  22.     "w",    6,
  23.     "wq",    7,
  24.     "write", 6,
  25.     "xit",    7,
  26.     "z",    7,
  27. };
  28.  
  29. static struct op_oplist {   /* Options */
  30.     char op_option[12];
  31.     INT  op_index;
  32. } oplist[] = {
  33.     "ai",        1,
  34.     "all",        2,
  35.     "autoindent",     1,
  36.     "debug",    3,
  37.     "fortran",    4,
  38.     "list",        5,
  39.     "scroll",    6,
  40.     "shiftwidth",    7,
  41.     "sw",        7,
  42.     "tabstops",    8,
  43.     "timeout",    10,
  44.     "to",        10,
  45.     "ts",        8,
  46.     "wrapscan",    9,
  47.     "ws",        9,
  48. };
  49.  
  50.  
  51. void
  52. rv_linecmd(cmd)
  53. /*
  54.  * Execute command line
  55.  */
  56. char *cmd;
  57. {
  58.     register char *s, *s2;
  59.     INT    i, j;
  60.     boolean    flag;
  61.     char    buf[12];
  62.     
  63.     if ((s2 = cmd) == NULL || *s2 == '\0')
  64.         return;
  65.     while (isalnum(*s2))
  66.         ++s2;
  67.     j = *s2;
  68.     *s2 = '\0';
  69.     i = binsearch(cmd, (char *) cmdlist, sizeof(struct cm_cmdlist),
  70.         sizeof(cmdlist) / sizeof(struct cm_cmdlist));
  71.     if (i >= 0)
  72.         i = cmdlist[i].cm_index;
  73.     *s2 = j;
  74.  
  75.     xmit_curline();
  76.     switch(i) {
  77. case 1:
  78.     /*
  79.      * Set
  80.      */
  81.     if (*s2 == '\0')
  82.         s2 = "all";
  83.     for (;;) {
  84.         while (isspace(*s2) && *s2 != '\0')
  85.             ++s2;
  86.         if (*s2 == '\0')
  87.             break;
  88.         s = s2;
  89.         while (isalnum(*s2))
  90.             ++s2;
  91.         if (*s2 != '\0')
  92.             *s2++ = '\0';
  93.         if (*s == 'n' && *(s+1) == 'o') {
  94.             s += 2;
  95.             flag = FALSE;
  96.         } else
  97.             flag = TRUE;
  98.         i = binsearch(s, (char *) oplist, sizeof(struct op_oplist),
  99.             sizeof(oplist) / sizeof(struct op_oplist));
  100.         if (i >= 0)
  101.             i = oplist[i].op_index;
  102.  
  103.         switch (i) {
  104.  
  105.         case -2:
  106.             botprint(TRUE, "\"%s\" is not unique\n", s);
  107.             break;
  108.  
  109.         case -1:
  110.             botprint(TRUE, "\"%s\": No such option - 'set all' gives all option values\n", s);
  111.             return;
  112.  
  113.         case 1: /* autoindent */
  114.             set_autoindent = flag;
  115.             break;
  116.         
  117.         case 2: /* all */
  118.             botprint(FALSE, "%sautoindent\n", set_autoindent ?
  119.                 "" : "no");
  120.             botprint(FALSE, "debug=%d\n", set_debug);
  121.             botprint(FALSE, "%sfortran\n", set_fortran ? "" :
  122.                 "no");
  123.             botprint(FALSE, "%slist", set_list ? "" : "no");
  124.             botprint(FALSE, "scroll=%d\n", set_scroll);
  125.             botprint(FALSE, "shiftwidth=%d\n", set_shiftwidth);
  126.             botprint(FALSE, "tabstops=%d\n", set_tabstops);
  127.             botprint(FALSE, "%stimeout", set_timeout ? "" : "no");
  128.             botprint(FALSE, "%swrapscan", set_wrapscan ? "" : "no");
  129.             break;
  130.  
  131.         case 3: /* debug */
  132.             set_debug = atoi(s2);
  133.             while (isdigit(*s2))
  134.                 ++s2;
  135.             break;
  136.  
  137.         case 4:
  138.             set_fortran = flag;
  139.             break;
  140.  
  141.         case 5: set_list = flag;
  142.             fetch_window(screen.sc_lineno-NUM_WINDOW_LINES/4-
  143.                 LINES-2+1, TRUE);
  144.             break;
  145.  
  146.         case 6: /* scroll */
  147.             set_scroll = atoi(s2);
  148.             while (isdigit(*s2))
  149.                 ++s2;
  150.             if (set_scroll <= 0)
  151.                 set_scroll = 1;
  152.             break;
  153.  
  154.         case 7: /* shiftwidth */
  155.             set_shiftwidth = atoi(s2);
  156.             while (isdigit(*s2))
  157.                 ++s2;
  158.             if (set_shiftwidth <= 0)
  159.                 set_shiftwidth = 1;
  160.             else if (set_shiftwidth > 40)
  161.                 set_shiftwidth = 40;
  162.             break;
  163.  
  164.         case 8: /* tabstops */
  165.             set_tabstops = atoi(s2);
  166.             while (isdigit(*s2))
  167.                 ++s2;
  168.             if (set_tabstops <= 0)
  169.                 set_tabstops = 1;
  170.             else if (set_tabstops > 40)
  171.                 set_tabstops = 40;
  172.             break;
  173.  
  174.         case 9: /* wrapscan */
  175.             set_wrapscan = flag;
  176.             break;
  177.  
  178.         case 10: /* timeout */
  179.             set_timeout = flag;
  180. #ifdef USG
  181.             keypad(stdscr, flag ? 1 : 2);
  182. #endif
  183.             break;
  184.  
  185.         default:
  186.             botprint(FALSE, "That option is not yet implemented\n");
  187.             break;
  188.         }
  189.     }
  190.     break;
  191.  
  192.  
  193. case 2: /* quit */
  194.     if (file.fi_modified && *s2 != '!') {
  195.         botprint(TRUE, "No write since last change (:q! overrides)");
  196.         return;
  197.     }
  198.     Quit();
  199.     break;
  200.  
  201. case 3: /* edit */
  202.     if (file.fi_modified && *s2 != '!') {
  203.         botprint(TRUE, "No write since last change (:edit! overrides)");
  204.         return;
  205.     }
  206.     if (*s2 == '!')
  207.         ++s2;
  208.     while (isspace(*s2))
  209.         ++s2;
  210.     edit(s2);
  211.     break;
  212.  
  213. case 4: /* next */
  214.     if (file.fi_modified && *s2 != '!') {
  215.         botprint(TRUE, "No write since last change (:next! overrides)");
  216.         return;
  217.     }
  218.     if (*s2 == '!')
  219.         ++s2;
  220.     while (isspace(*s2))
  221.         ++s2;
  222.     if (*s2 == '\0' && *nextfile == '\0') {
  223.         botprint(TRUE, "No more files to edit");
  224.         return;
  225.     }
  226.     edit(s2);
  227.     break;
  228.  
  229. case 5: /* file */
  230.     while (isspace(*s2))
  231.         ++s2;
  232.     if (*s2 == '\0') {
  233.         sizemsg();
  234.         return;
  235.     }
  236.     xmit_ed("f %s\n", s2);
  237.     strncpy(file.fi_name, s2, 126);
  238.     xmit_sync();
  239.     (void) recv_sync(TRUE);
  240.     sizemsg();
  241.     break;
  242.  
  243. case 6: /* write */
  244.     i = 0;
  245.     if (*s2 == '!') {
  246.         ++s2;
  247.         i = 1;
  248.     }
  249.     while (isspace(*s2))
  250.         ++s2;
  251.     if (*s2 == '\0' && file.fi_name[0] == '\0') {
  252.         botprint(TRUE, "No current filename");
  253.         return;
  254.     }
  255.     if (i)
  256.         xmit_ed("!rm -f %s\n", *s2 ? s2 : file.fi_name);
  257.     if (*s2)
  258.         xmit_ed("w %s\n", s2);
  259.     else {
  260.         xmit_ed("w\n");
  261.         s2 = file.fi_name;
  262.     }
  263.     xmit_sync();
  264.     botprint(FALSE, "\"%s\"", s2);
  265.     hitcr_continue();
  266.     refresh();
  267.     if (recv_sync(TRUE)) {
  268.         botprint(FALSE, "\"%s\" %d lines", s2, file.fi_numlines);
  269.         if (strcmp(s2, file.fi_name) == 0)
  270.             file.fi_modified = FALSE;
  271.     }
  272.     else
  273.         hitcr_continue();
  274.     break;
  275.  
  276. case 7: /* wq */
  277.     if (file.fi_modified)
  278.         rv_linecmd("w");
  279.     if (!file.fi_modified)
  280.         rv_linecmd("q");
  281.     break;
  282.  
  283. case 8: /* shell */
  284.     rv_shell("sh -i");
  285.     break;
  286.     
  287. default:
  288.     if (*s2 == '!') {  /* Shell escape */
  289.         if (strcmp(++s2, "sh") == 0)
  290.             s2 = "sh -i";
  291.         else if (strcmp(s2, "csh") == 0)
  292.             s2 = "csh -i";
  293.         rv_shell(s2);
  294.         return;
  295.     }
  296.     did_botprint = TRUE;
  297.     i = cmd[strlen(cmd)-1];
  298.     if (i == 'a' || i == 'c' || i == 'i' || i == 'H' || i == 'X'
  299.              || i == 'P') {
  300.         botprint(FALSE, "That command is reserved");
  301.         return;
  302.     }
  303.     toss_undo();
  304.     if (i == 'w') {
  305.         ed_undo = FALSE;
  306.         file.fi_modified = FALSE;
  307.     } else
  308.         ed_undo = TRUE;
  309.     xmit_ed("%d\n", screen.sc_lineno);
  310.     xmit_sync();
  311.     xmit_ed("%s\n", cmd);
  312.     xmit_sync();
  313.     xmit_ed(".=\n");
  314.     (void) recv_sync(FALSE);
  315.     (void) recv_sync(2);
  316.     (void) fgets(buf, 10, file.fi_fpin);
  317.     if ((i = atoi(buf)) <= 0)
  318.         i = screen.sc_lineno;
  319.     hitcr_continue();
  320.     fetch_window(i-NUM_WINDOW_LINES/4-LINES/2+1, TRUE);
  321.     move_abs_cursor(i, COL_FIRST_NONWHITE);
  322.     break;
  323.     }
  324. }
  325.