home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / edit / elvis14s / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-08  |  8.0 KB  |  425 lines

  1. /* main.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    14407 SW Teal Blvd. #C
  6.  *    Beaverton, OR 97005
  7.  *    kirkenda@cs.pdx.edu
  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. #ifndef NO_DIGRAPH
  23. static init_digraphs();
  24. #endif
  25.  
  26. /*---------------------------------------------------------------------*/
  27.  
  28. void main(argc, argv)
  29.     int    argc;
  30.     char    *argv[];
  31. {
  32.     int    i;
  33.     char    *cmd = (char *)0;
  34.     char    *tag = (char *)0;
  35.     char    *err = (char *)0;
  36.     char    *str;
  37. #if MSDOS || TOS
  38.     char firstarg[256];
  39. #else
  40.     char *firstarg;
  41. #endif
  42.  
  43.     /* set mode to MODE_VI or MODE_EX depending on program name */
  44.     switch (argv[0][strlen(argv[0]) - 1])
  45.     {
  46.       case 'x':            /* "ex" */
  47.         mode = MODE_EX;
  48.         break;
  49.  
  50.       case 'w':            /* "view" */
  51.         mode = MODE_VI;
  52.         *o_readonly = TRUE;
  53.         break;
  54. #ifndef NO_EXTENSIONS
  55.       case 't':            /* "edit" or "input" */
  56.         mode = MODE_VI;
  57.         *o_inputmode = TRUE;
  58.         break;
  59. #endif
  60.       default:            /* "vi" or "elvis" */
  61.         mode = MODE_VI;
  62.     }
  63.  
  64. #ifndef DEBUG
  65. # ifdef    SIGQUIT
  66.     /* normally, we ignore SIGQUIT.  SIGINT is trapped later */
  67.     signal(SIGQUIT, SIG_IGN);
  68. # endif
  69. #endif
  70.  
  71.     /* temporarily ignore SIGINT */
  72.     signal(SIGINT, SIG_IGN);
  73.  
  74.     /* start curses */
  75.     initscr();
  76.     cbreak();
  77.     noecho();
  78.     scrollok(stdscr, TRUE);
  79.  
  80.     /* initialize the options */
  81.     initopts();
  82.  
  83.     /* map the arrow keys.  The KU,KD,KL,and KR variables correspond to
  84.      * the :ku=: (etc.) termcap capabilities.  The variables are defined
  85.      * as part of the curses package.
  86.      */
  87.     if (has_KU) mapkey(has_KU, "k",    WHEN_VICMD|WHEN_INMV, "<Up>");
  88.     if (has_KD) mapkey(has_KD, "j",    WHEN_VICMD|WHEN_INMV, "<Down>");
  89.     if (has_KL) mapkey(has_KL, "h",    WHEN_VICMD|WHEN_INMV, "<Left>");
  90.     if (has_KR) mapkey(has_KR, "l",    WHEN_VICMD|WHEN_INMV, "<Right>");
  91.     if (has_HM) mapkey(has_HM, "^",    WHEN_VICMD|WHEN_INMV, "<Home>");
  92.     if (has_EN) mapkey(has_EN, "$",    WHEN_VICMD|WHEN_INMV, "<End>");
  93.     if (has_PU) mapkey(has_PU, "\002", WHEN_VICMD|WHEN_INMV, "<PgUp>");
  94.     if (has_PD) mapkey(has_PD, "\006", WHEN_VICMD|WHEN_INMV, "<PgDn>");
  95. #if MSDOS
  96.     if (*o_pcbios)
  97.     {
  98.         mapkey("#R", "i", WHEN_VICMD|WHEN_INMV,    "<Insrt>");
  99.         mapkey("#S", "x", WHEN_VICMD|WHEN_INMV,    "<Del>");
  100.         mapkey("#s", "B", WHEN_VICMD|WHEN_INMV,    "^<left>");
  101.         mapkey("#t", "W", WHEN_VICMD|WHEN_INMV,    "^<right>");
  102.     }
  103. #else
  104.     if (ERASEKEY != '\177')
  105.     {
  106.         mapkey("\177", "x", WHEN_VICMD|WHEN_INMV, "<Del>");
  107.     }
  108. #endif
  109.  
  110. #ifndef NO_DIGRAPH
  111.     init_digraphs();
  112. #endif /* NO_DIGRAPH */
  113.  
  114.     /* process any flags */
  115.     for (i = 1; i < argc && *argv[i] == '-'; i++)
  116.     {
  117.         switch (argv[i][1])
  118.         {
  119.           case 'R':    /* readonly */
  120.             *o_readonly = TRUE;
  121.             break;
  122.  
  123.           case 'r':    /* recover */
  124.             msg("Use the `virec` program to recover lost files");
  125.             endmsgs();
  126.             refresh();
  127.             endwin();
  128.             exit(0);
  129.             break;
  130.  
  131.           case 't':    /* tag */
  132.             if (argv[i][2])
  133.             {
  134.                 tag = argv[i] + 2;
  135.             }
  136.             else
  137.             {
  138.                 i++;
  139.                 tag = argv[i];
  140.             }
  141.             break;
  142.  
  143.           case 'v':    /* vi mode */
  144.             mode = MODE_VI;
  145.             break;
  146.  
  147.           case 'e':    /* ex mode */
  148.             mode = MODE_EX;
  149.             break;
  150. #ifndef NO_EXTENSIONS
  151.           case 'i':    /* input mode */
  152.             *o_inputmode = TRUE;
  153.             break;
  154. #endif
  155. #ifndef NO_ERRLIST
  156.           case 'm':    /* use "errlist" as the errlist */
  157.             if (argv[i][2])
  158.             {
  159.                 err = argv[i] + 2;
  160.             }
  161.             else if (i + 1 < argc)
  162.             {
  163.                 i++;
  164.                 err = argv[i];
  165.             }
  166.             else
  167.             {
  168.                 err = "";
  169.             }
  170.             break;
  171. #endif
  172.           default:
  173.             msg("Ignoring unknown flag \"%s\"", argv[i]);
  174.         }
  175.     }
  176.  
  177.     /* if we were given an initial ex command, save it... */
  178.     if (i < argc && *argv[i] == '+')
  179.     {
  180.         if (argv[i][1])
  181.         {
  182.             cmd = argv[i++] + 1;
  183.         }
  184.         else
  185.         {
  186.             cmd = "$"; /* "vi + file" means start at EOF */
  187.             i++;
  188.         }
  189.     }
  190.  
  191.     /* the remaining args are file names. */
  192.     nargs = argc - i;
  193.     if (nargs > 0)
  194.     {
  195. #if ! ( MSDOS || TOS )
  196.         firstarg = argv[i];
  197. #endif
  198.         strcpy(args, argv[i]);
  199.         while (++i < argc && strlen(args) + 1 + strlen(argv[i]) < sizeof args)
  200.         {
  201.             strcat(args, " ");
  202.             strcat(args, argv[i]);
  203.         }
  204.     }
  205. #if ! ( MSDOS || TOS )
  206.     else
  207.     {
  208.         firstarg = "";
  209.     }
  210. #endif
  211.     argno = 0;
  212.  
  213. #if MSDOS || TOS
  214.     if (nargs > 0)
  215.     {
  216. #if ! SETARGV
  217.         strcpy(args, wildcard(args));
  218. #endif
  219.         nargs = 1;
  220.         for (i = 0; args[i]; i++)
  221.         {
  222.             if (args[i] == ' ')
  223.             {
  224.                 nargs++;
  225.             }
  226.         }
  227.         for (i = 0; args[i] && args[i] != ' '; i++)
  228.         {
  229.             firstarg[i] = args[i];
  230.         }
  231.         firstarg[i] = '\0';
  232.     }
  233.     else
  234.     {
  235.         firstarg[0] = '\0';
  236.     }
  237. #endif
  238.  
  239.     /* perform the .exrc files and EXINIT environment variable */
  240. #ifdef SYSEXRC
  241.     doexrc(SYSEXRC);
  242. #endif
  243. #ifdef HMEXRC
  244.     str = getenv("HOME");
  245.     if (str)
  246.     {
  247.         sprintf(tmpblk.c, "%s%c%s", str, SLASH, HMEXRC);
  248.         doexrc(tmpblk.c);
  249.     }
  250. #endif
  251.     doexrc(EXRC);
  252. #ifdef EXINIT
  253.     str = getenv(EXINIT);
  254.     if (str)
  255.     {
  256.         exstring(str, strlen(str));
  257.     }
  258. #endif
  259.  
  260.     /* search for a tag (or an error) now, if desired */
  261.     blkinit();
  262.     if (tag)
  263.     {
  264.         cmd_tag(MARK_FIRST, MARK_FIRST, CMD_TAG, 0, tag);
  265.     }
  266. #ifndef NO_ERRLIST
  267.     else if (err)
  268.     {
  269.         cmd_errlist(MARK_FIRST, MARK_FIRST, CMD_ERRLIST, 0, err);
  270.     }
  271. #endif
  272.  
  273.     /* if no tag/err, or tag failed, then start with first arg */
  274.     if (tmpfd < 0 && tmpstart(firstarg) == 0 && *origname)
  275.     {
  276.         ChangeText
  277.         {
  278.         }
  279.         clrflag(file, MODIFIED);
  280.     }
  281.  
  282.     /* now we do the immediate ex command that we noticed before */
  283.     if (cmd)
  284.     {
  285.         doexcmd(cmd);
  286.     }
  287.  
  288.     /* repeatedly call ex() or vi() (depending on the mode) until the
  289.      * mode is set to MODE_QUIT
  290.      */
  291.     while (mode != MODE_QUIT)
  292.     {
  293.         if (setjmp(jmpenv))
  294.         {
  295.             /* Maybe we just aborted a change? */
  296.             abortdo();
  297.         }
  298. #if TURBOC
  299.         signal(SIGINT, (void(*)()) trapint);
  300. #else
  301.         signal(SIGINT, trapint);
  302. #endif
  303.  
  304.         switch (mode)
  305.         {
  306.           case MODE_VI:
  307.             vi();
  308.             break;
  309.  
  310.           case MODE_EX:
  311.             ex();
  312.             break;
  313. #ifdef DEBUG
  314.           default:
  315.             msg("mode = %d?", mode);
  316.             mode = MODE_QUIT;
  317. #endif
  318.         }
  319.     }
  320.  
  321.     /* free up the cut buffers */
  322.     cutend();
  323.  
  324.     /* end curses */
  325. #ifndef    NO_CURSORSHAPE
  326.     if (has_CQ)
  327.         do_CQ();
  328. #endif
  329.     endmsgs();
  330.     move(LINES - 1, 0);
  331.     clrtoeol();
  332.     refresh();
  333.     endwin();
  334.  
  335.     exit(0);
  336.     /*NOTREACHED*/
  337. }
  338.  
  339.  
  340. /*ARGSUSED*/
  341. int trapint(signo)
  342.     int    signo;
  343. {
  344.     resume_curses(FALSE);
  345.     abortdo();
  346. #if OSK
  347.     sigmask(-1);
  348. #endif
  349. #if TURBOC
  350.     signal(signo, (void (*)())trapint);
  351. #else
  352.     signal(signo, trapint);
  353. #endif
  354.     longjmp(jmpenv, 1);
  355.  
  356.     return 0;
  357. }
  358.  
  359.  
  360. #ifndef NO_DIGRAPH
  361.  
  362. /* This stuff us used to build the default digraphs table. */
  363. static char    digtable[][4] =
  364. {
  365. # if CS_IBMPC
  366.     "C,\200",    "u\"\1",    "e'\2",        "a^\3",
  367.     "a\"\4",    "a`\5",        "a@\6",        "c,\7",
  368.     "e^\10",    "e\"\211",    "e`\12",    "i\"\13",
  369.     "i^\14",    "i`\15",    "A\"\16",    "A@\17",
  370.     "E'\20",    "ae\21",    "AE\22",    "o^\23",
  371.     "o\"\24",    "o`\25",    "u^\26",    "u`\27",
  372.     "y\"\30",    "O\"\31",    "U\"\32",    "a'\240",
  373.     "i'!",        "o'\"",        "u'#",        "n~$",
  374.     "N~%",        "a-&",        "o-'",        "~?(",
  375.     "~!-",        "\"<.",        "\">/",
  376. #  if CS_SPECIAL
  377.     "2/+",        "4/,",        "^+;",        "^q<",
  378.     "^c=",        "^r>",        "^t?",        "pp]",
  379.     "^^^",        "oo_",        "*a`",        "*ba",
  380.     "*pc",        "*Sd",        "*se",        "*uf",
  381.     "*tg",        "*Ph",        "*Ti",        "*Oj",
  382.     "*dk",        "*Hl",        "*hm",        "*En",
  383.     "*No",        "eqp",        "pmq",        "ger",
  384.     "les",        "*It",        "*iu",        "*/v",
  385.     "*=w",        "sq{",        "^n|",        "^2}",
  386.     "^3~",        "^_\377",
  387. #  endif /* CS_SPECIAL */
  388. # endif /* CS_IBMPC */
  389. # if CS_LATIN1
  390.     "~!!",        "a-*",        "\">+",        "o-:",
  391.     "\"<>",        "~??",
  392.  
  393.     "A`@",        "A'A",        "A^B",        "A~C",
  394.     "A\"D",        "A@E",        "AEF",        "C,G",
  395.     "E`H",        "E'I",        "E^J",        "E\"K",
  396.     "I`L",        "I'M",        "I^N",        "I\"O",
  397.     "-DP",        "N~Q",        "O`R",        "O'S",
  398.     "O^T",        "O~U",        "O\"V",        "O/X",
  399.     "U`Y",        "U'Z",        "U^[",        "U\"\\",
  400.     "Y'_",
  401.  
  402.     "a``",        "a'a",        "a^b",        "a~c",
  403.     "a\"d",        "a@e",        "aef",        "c,g",
  404.     "e`h",        "e'i",        "e^j",        "e\"k",
  405.     "i`l",        "i'm",        "i^n",        "i\"o",
  406.     "-dp",        "n~q",        "o`r",        "o's",
  407.     "o^t",        "o~u",        "o\"v",        "o/x",
  408.     "u`y",        "u'z",        "u^{",        "u\"|",
  409.     "y'~",
  410. # endif /* CS_LATIN1 */
  411.     ""
  412. };
  413.  
  414. static init_digraphs()
  415. {
  416.     int    i;
  417.  
  418.     for (i = 0; *digtable[i]; i++)
  419.     {
  420.         do_digraph(FALSE, digtable[i]);
  421.     }
  422.     do_digraph(FALSE, (char *)0);
  423. }
  424. #endif /* NO_DIGRAPH */
  425.