home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / main < prev    next >
Encoding:
Text File  |  1990-04-14  |  3.5 KB  |  217 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 <signal.h>
  14. #include <setjmp.h>
  15. #include "vi.h"
  16.  
  17. extern        trapint(); /* defined below */
  18. extern char    *getenv();
  19. jmp_buf        jmpenv;
  20.  
  21. /*---------------------------------------------------------------------*/
  22.  
  23. main(argc, argv)
  24.     int    argc;
  25.     char    *argv[];
  26. {
  27.     int    i;
  28.     char    *cmd = (char *)0;
  29.     char    *tag = (char *)0;
  30.     char    *str;
  31.  
  32.     /* set mode to MODE_VI or MODE_EX depending on program name */
  33.     switch (argv[0][strlen(argv[0]) - 1])
  34.     {
  35.       case 'x':
  36.         mode = MODE_EX;
  37.         break;
  38.  
  39.       case 'w':
  40.         mode = MODE_VI;
  41.         *o_readonly = TRUE;
  42.         break;
  43.  
  44.       default:
  45.         mode = MODE_VI;
  46.     }
  47.  
  48.     /* start curses */
  49.     initscr();
  50.     cbreak();
  51.     noecho();
  52.     scrollok(stdscr, TRUE);
  53.  
  54. #ifndef DEBUG
  55.     /* normally, we ignore SIGQUIT.  SIGINT is trapped later */
  56. #endif
  57.  
  58.     /* map the arrow keys.  The KU,KD,KL,and KR variables correspond to
  59.      * the :ku=: (etc.) termcap capabilities.  The variables are defined
  60.      * as part of the curses package.
  61.      */
  62.     if (KU) mapkey(KU, "k", WHEN_VICMD);
  63.     if (KD) mapkey(KD, "j", WHEN_VICMD);
  64.     if (KL) mapkey(KL, "h", WHEN_VICMD);
  65.     if (KR) mapkey(KR, "l", WHEN_VICMD);
  66.  
  67.     /* initialize the options */
  68.     initopts();
  69.  
  70.     /* process any flags */
  71.     for (i = 1; i < argc && *argv[i] == '-'; i++)
  72.     {
  73.         switch (argv[i][1])
  74.         {
  75.           case 'R':    /* readonly */
  76.             *o_readonly = TRUE;
  77.             break;
  78.  
  79.           case 'r':    /* recover */
  80.             addstr("Use the `virecover` command to recover lost files\n");
  81.             refresh();
  82.             endwin();
  83.             exit(0);
  84.             break;
  85.  
  86.           case 't':    /* tag */
  87.             if (argv[i][2])
  88.             {
  89.                 tag = argv[i] + 2;
  90.             }
  91.             else
  92.             {
  93.                 i++;
  94.                 tag = argv[i];
  95.             }
  96.             break;
  97.  
  98.           case 'v':    /* vi mode */
  99.             mode = MODE_VI;
  100.             break;
  101.  
  102.           case 'e':    /* ex mode */
  103.             mode = MODE_EX;
  104.             break;
  105.         }
  106.     }
  107.  
  108.     /* if we were given an initial ex command, save it... */
  109.     if (i < argc && *argv[i] == '+')
  110.     {
  111.         if (argv[i][1])
  112.         {
  113.             cmd = argv[i++] + 1;
  114.         }
  115.         else
  116.         {
  117.             cmd = "$"; /* "vi + file" means start at EOF */
  118.             i++;
  119.         }
  120.     }
  121.  
  122.     /* the remaining args are file names. */
  123.     nargs = argc - i;
  124.     if (nargs > 0)
  125.     {
  126.         strcpy(args, argv[i]);
  127.         while (++i < argc)
  128.         {
  129.             strcat(args, " ");
  130.             strcat(args, argv[i]);
  131.         }
  132.         argno = 1;
  133.     }
  134.     argno = 0;
  135.  
  136.     /* search for a tag now, if desired */
  137.     blkinit();
  138.     if (tag)
  139.     {
  140.         cmd_tag(MARK_FIRST, MARK_FIRST, CMD_TAG, 0, tag);
  141.     }
  142.  
  143.     /* if no tag, or tag failed, then start with first arg */
  144.     if (tmpfd < 0 && tmpstart(argv[i - nargs]) == 0 && *origname)
  145.     {
  146.         ChangeText
  147.         {
  148.         }
  149.         clrflag(file, MODIFIED);
  150.     }
  151.  
  152.     /* perform the .exrc files and EXINIT environment variable */
  153. #ifdef ARC
  154.     str = "$.Library";
  155. #else
  156.     str = getenv("HOME");
  157. #endif /* ARC */
  158.     if (str)
  159.     {
  160.         sprintf(tmpblk.c, "%s%c%s", str, SLASH, HMEXRC);
  161.         doexrc(tmpblk.c);
  162.     }
  163.     doexrc(EXRC);
  164.     str = getenv("EXINIT");
  165.     if (str)
  166.     {
  167.         doexcmd(str);
  168.     }
  169.  
  170.     /* now we do the immediate ex command that we noticed before */
  171.     if (cmd)
  172.     {
  173.         doexcmd(cmd);
  174.     }
  175.  
  176.     /* repeatedly call ex() or vi() (depending on the mode) until the
  177.      * mode is set to MODE_QUIT
  178.      */
  179.     while (mode != MODE_QUIT)
  180.     {
  181.           setjmp(jmpenv);
  182. #ifndef ARC
  183.         signal(SIGINT, trapint);
  184. #endif
  185.  
  186.         switch (mode)
  187.         {
  188.           case MODE_VI:
  189.             vi();
  190.             break;
  191.  
  192.           case MODE_EX:
  193.             ex();
  194.             break;
  195. #ifdef DEBUG
  196.           default:
  197.             msg("mode = %d?", mode);
  198.             mode = MODE_QUIT;
  199. #endif
  200.         }
  201.     }
  202.  
  203.     /* free up the cut buffers */
  204.     cutend();
  205.  
  206.     /* end curses */
  207.     endwin();
  208. }
  209.  
  210.  
  211. trapint(signo)
  212.     int    signo;
  213. {
  214.     resume_curses(FALSE);
  215.     longjmp(jmpenv, 1);
  216. }
  217.