home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1708 / main.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  5KB  |  308 lines

  1. /* main.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 main() function of vi */
  12.  
  13. #include "config.h"
  14. #include <signal.h>
  15. #include <setjmp.h>
  16. #include "vi.h"
  17.  
  18. extern        trapint(); /* defined below */
  19. extern char    *getenv();
  20. jmp_buf        jmpenv;
  21.  
  22. /*---------------------------------------------------------------------*/
  23.  
  24. void main(argc, argv)
  25.     int    argc;
  26.     char    *argv[];
  27. {
  28.     int    i;
  29.     char    *cmd = (char *)0;
  30.     char    *tag = (char *)0;
  31.     char    *str;
  32. #if    MSDOS || TOS
  33.     char firstarg[256];
  34. #else
  35.     char *firstarg;
  36. #endif
  37.  
  38. #ifndef NO_ARGV0
  39.     /* set mode to MODE_VI or MODE_EX depending on program name */
  40.     switch (argv[0][strlen(argv[0]) - 1])
  41.     {
  42.       case 'x':            /* "ex" */
  43.         mode = MODE_EX;
  44.         break;
  45.  
  46.       case 'w':            /* "view" */
  47.         mode = MODE_VI;
  48.         *o_readonly = TRUE;
  49.         break;
  50. #ifndef NO_EXTENSIONS
  51.       case 't':            /* "edit" or "input" */
  52.         mode = MODE_VI;
  53.         *o_inputmode = TRUE;
  54.         break;
  55. #endif
  56.       default:            /* "vi" or "elvis" */
  57.         mode = MODE_VI;
  58.     }
  59. #endif
  60.  
  61.     /* start curses */
  62.     initscr();
  63.     cbreak();
  64.     noecho();
  65.     scrollok(stdscr, TRUE);
  66.  
  67. #ifndef DEBUG
  68. #ifdef    SIGQUIT
  69.     /* normally, we ignore SIGQUIT.  SIGINT is trapped later */
  70.     signal(SIGQUIT, SIG_IGN);
  71. #endif
  72. #endif
  73.  
  74.     /* initialize the options */
  75.     initopts();
  76.  
  77.     /* map the arrow keys.  The KU,KD,KL,and KR variables correspond to
  78.      * the :ku=: (etc.) termcap capabilities.  The variables are defined
  79.      * as part of the curses package.
  80.      */
  81.     if (has_KU) mapkey(has_KU, "k",    WHEN_VICMD|WHEN_INMV, "<Up>");
  82.     if (has_KD) mapkey(has_KD, "j",    WHEN_VICMD|WHEN_INMV, "<Down>");
  83.     if (has_KL) mapkey(has_KL, "h",    WHEN_VICMD|WHEN_INMV, "<Left>");
  84.     if (has_KR) mapkey(has_KR, "l",    WHEN_VICMD|WHEN_INMV, "<Right>");
  85.     if (has_HM) mapkey(has_HM, "^",    WHEN_VICMD|WHEN_INMV, "<Home>");
  86.     if (has_EN) mapkey(has_EN, "$",    WHEN_VICMD|WHEN_INMV, "<End>");
  87.     if (has_PU) mapkey(has_PU, "\002", WHEN_VICMD|WHEN_INMV, "<PgUp>");
  88.     if (has_PD) mapkey(has_PD, "\006", WHEN_VICMD|WHEN_INMV, "<PgDn>");
  89. #if    MSDOS
  90.     if (*o_pcbios)
  91.     {
  92.         mapkey("#R", "i", WHEN_VICMD|WHEN_INMV,    "<Insrt>");
  93.         mapkey("#S", "x", WHEN_VICMD|WHEN_INMV,    "<Del>");
  94.         mapkey("#s", "B", WHEN_VICMD|WHEN_INMV,    "^<left>");
  95.         mapkey("#t", "W", WHEN_VICMD|WHEN_INMV,    "^<right>");
  96.     }
  97. #else
  98.     if (ERASEKEY != '\177')
  99.     {
  100.         mapkey("\177", "x", WHEN_VICMD|WHEN_INMV, "<Del>");
  101.     }
  102. #endif
  103.  
  104.     /* process any flags */
  105.     for (i = 1; i < argc && *argv[i] == '-'; i++)
  106.     {
  107.         switch (argv[i][1])
  108.         {
  109.           case 'R':    /* readonly */
  110.             *o_readonly = TRUE;
  111.             break;
  112.  
  113.           case 'r':    /* recover */
  114.             msg("Use the `virec` command to recover lost files\n");
  115.             refresh();
  116.             endwin();
  117.             exit(0);
  118.             break;
  119.  
  120.           case 't':    /* tag */
  121.             if (argv[i][2])
  122.             {
  123.                 tag = argv[i] + 2;
  124.             }
  125.             else
  126.             {
  127.                 i++;
  128.                 tag = argv[i];
  129.             }
  130.             break;
  131.  
  132.           case 'v':    /* vi mode */
  133.             mode = MODE_VI;
  134.             break;
  135.  
  136.           case 'e':    /* ex mode */
  137.             mode = MODE_EX;
  138.             break;
  139. #ifndef NO_EXTENSIONS
  140.           case 'i':    /* input mode */
  141.             *o_inputmode = TRUE;
  142.             break;
  143. #endif
  144.           default:
  145.             msg("Ignoring unknown flag \"%s\"", argv[i]);
  146.         }
  147.     }
  148.  
  149.     /* if we were given an initial ex command, save it... */
  150.     if (i < argc && *argv[i] == '+')
  151.     {
  152.         if (argv[i][1])
  153.         {
  154.             cmd = argv[i++] + 1;
  155.         }
  156.         else
  157.         {
  158.             cmd = "$"; /* "vi + file" means start at EOF */
  159.             i++;
  160.         }
  161.     }
  162.  
  163.     /* the remaining args are file names. */
  164.     nargs = argc - i;
  165.     if (nargs > 0)
  166.     {
  167. #if ! ( MSDOS || TOS )
  168.         firstarg = argv[i];
  169. #endif
  170.         strcpy(args, argv[i]);
  171.         while (++i < argc && strlen(args) + 1 + strlen(argv[i]) < sizeof args)
  172.         {
  173.             strcat(args, " ");
  174.             strcat(args, argv[i]);
  175.         }
  176.     }
  177. #if ! ( MSDOS || TOS )
  178.     else
  179.     {
  180.         firstarg = "";
  181.     }
  182. #endif
  183.     argno = 0;
  184.  
  185. #if MSDOS || TOS
  186.     if (nargs > 0)
  187.     {
  188.         strcpy(args, wildcard(args));
  189.         nargs = 1;
  190.         for (i = 0; args[i]; i++)
  191.         {
  192.             if (args[i] == ' ')
  193.             {
  194.                 nargs++;
  195.             }
  196.         }
  197.         for (i = 0; args[i] && args[i] != ' '; i++)
  198.         {
  199.             firstarg[i] = args[i];
  200.         }
  201.         firstarg[i] = '\0';
  202.     }
  203.     else
  204.     {
  205.         firstarg[0] = '\0';
  206.     }
  207. #endif
  208.  
  209.     /* perform the .exrc files and EXINIT environment variable */
  210. #ifdef SYSEXRC
  211.     doexrc(SYSEXRC);
  212. #endif
  213. #ifdef HMEXRC
  214.     str = getenv("HOME");
  215.     if (str)
  216.     {
  217.         sprintf(tmpblk.c, "%s%c%s", str, SLASH, HMEXRC);
  218.         doexrc(tmpblk.c);
  219.     }
  220. #endif
  221.     doexrc(EXRC);
  222. #ifdef EXINIT
  223.     str = getenv(EXINIT);
  224.     if (str)
  225.     {
  226.         doexcmd(str);
  227.     }
  228. #endif
  229.  
  230.     /* search for a tag now, if desired */
  231.     blkinit();
  232.     if (tag)
  233.     {
  234.         cmd_tag(MARK_FIRST, MARK_FIRST, CMD_TAG, 0, tag);
  235.     }
  236.  
  237.     /* if no tag, or tag failed, then start with first arg */
  238.     if (tmpfd < 0 && tmpstart(firstarg) == 0 && *origname)
  239.     {
  240.         ChangeText
  241.         {
  242.         }
  243.         clrflag(file, MODIFIED);
  244.     }
  245.     
  246.     /* now we do the immediate ex command that we noticed before */
  247.     if (cmd)
  248.     {
  249.         doexcmd(cmd);
  250.     }
  251.  
  252.     /* repeatedly call ex() or vi() (depending on the mode) until the
  253.      * mode is set to MODE_QUIT
  254.      */
  255.     while (mode != MODE_QUIT)
  256.     {
  257.         if (setjmp(jmpenv))
  258.         {
  259.             /* Maybe we just aborted a change? */
  260.             abortdo();
  261.         }
  262. #if    TURBOC
  263.         signal(SIGINT, (void(*)()) trapint);
  264. #else
  265.         signal(SIGINT, trapint);
  266. #endif
  267.  
  268.         switch (mode)
  269.         {
  270.           case MODE_VI:
  271.             vi();
  272.             break;
  273.  
  274.           case MODE_EX:
  275.             ex();
  276.             break;
  277. #ifdef DEBUG
  278.           default:
  279.             msg("mode = %d?", mode);
  280.             mode = MODE_QUIT;
  281. #endif
  282.         }
  283.     }
  284.  
  285.     /* free up the cut buffers */
  286.     cutend();
  287.  
  288.     /* end curses */
  289. #ifndef    NO_CURSORSHAPE    
  290.     if (has_CQ)
  291.         do_CQ();
  292. #endif
  293.     refresh();
  294.     endwin();
  295.  
  296.     exit(0);
  297.     /*NOTREACHED*/
  298. }
  299.  
  300.  
  301. /*ARGSUSED*/
  302. trapint(signo)
  303.     int    signo;
  304. {
  305.     resume_curses(FALSE);
  306.     longjmp(jmpenv, 1);
  307. }
  308.