5 * 14407 SW Teal Blvd. #C
11 /* This file contains the main() function of vi */
13 /* HACK! bcc needs to disable use of precompiled headers for this file,
14 or else command line args will not be passed to elvis */
24 extern trapint(); /* defined below */
25 extern char *getenv();
29 static init_digraphs();
32 /*---------------------------------------------------------------------*/
44 char *cmd
= (char *)0;
45 char *err
= (char *)0;
47 char *tag
= (char *)0;
49 /* set mode to MODE_VI or MODE_EX depending on program name */
50 switch (argv
[0][strlen(argv
[0]) - 1])
56 case 'w': /* "view" */
61 case 't': /* "edit" or "input" */
66 default: /* "vi" or "elvis" */
72 /* normally, we ignore SIGQUIT. SIGINT is trapped later */
73 signal(SIGQUIT
, SIG_IGN
);
77 /* temporarily ignore SIGINT */
78 signal(SIGINT
, SIG_IGN
);
84 scrollok(stdscr
, TRUE
);
86 /* arrange for deadly signals to be caught */
88 signal(SIGHUP
, (void(*)()) deathtrap
);
92 signal(SIGILL
, (void(*)()) deathtrap
);
95 signal(SIGBUS
, (void(*)()) deathtrap
);
98 signal(SIGSEGV
, (void(*)()) deathtrap
);
101 signal(SIGSYS
, (void(*)()) deathtrap
);
105 signal(SIGPIPE
, (void(*)()) deathtrap
);
108 signal(SIGTERM
, (void(*)()) deathtrap
);
111 signal(SIGUSR1
, (void(*)()) deathtrap
);
114 signal(SIGUSR2
, (void(*)()) deathtrap
);
117 /* initialize the options - must be done after initscr(), so that
118 * we can alter LINES and COLS if necessary.
122 /* map the arrow keys. The KU,KD,KL,and KR variables correspond to
123 * the :ku=: (etc.) termcap capabilities. The variables are defined
124 * as part of the curses package.
126 if (has_KU
) mapkey(has_KU
, "k", WHEN_VICMD
|WHEN_INMV
, "<Up>");
127 if (has_KD
) mapkey(has_KD
, "j", WHEN_VICMD
|WHEN_INMV
, "<Down>");
128 if (has_KL
) mapkey(has_KL
, "h", WHEN_VICMD
|WHEN_INMV
, "<Left>");
129 if (has_KR
) mapkey(has_KR
, "l", WHEN_VICMD
|WHEN_INMV
, "<Right>");
130 if (has_HM
) mapkey(has_HM
, "^", WHEN_VICMD
|WHEN_INMV
, "<Home>");
131 if (has_EN
) mapkey(has_EN
, "$", WHEN_VICMD
|WHEN_INMV
, "<End>");
132 if (has_PU
) mapkey(has_PU
, "\002", WHEN_VICMD
|WHEN_INMV
, "<PageUp>");
133 if (has_PD
) mapkey(has_PD
, "\006", WHEN_VICMD
|WHEN_INMV
, "<PageDn>");
134 if (has_KI
) mapkey(has_KI
, "i", WHEN_VICMD
|WHEN_INMV
, "<Insert>");
137 if (!strcmp("rainbow", o_term
))
139 mapkey("\033[1~", "/", WHEN_VICMD
, "<Find>");
140 mapkey("\033[3~", "x", WHEN_VICMD
|WHEN_INMV
, "<Remove>");
141 mapkey("\033[4~", "v", WHEN_VICMD
|WHEN_INMV
, "<Select>");
142 mapkey("\033[17~", ":sh\n", WHEN_VICMD
, "<Intrpt>");
143 mapkey("\033[19~", ":q\n", WHEN_VICMD
, "<Cancel>");
144 mapkey("\033[21~", "ZZ", WHEN_VICMD
, "<Exit>");
145 mapkey("\033[26~", "V", WHEN_VICMD
|WHEN_INMV
, "<AddlOp>");
146 mapkey("\033[28~", "\\", WHEN_VICMD
|WHEN_INMV
, "<Help>");
147 mapkey("\033[29~", "K", WHEN_VICMD
|WHEN_INMV
, "<Do>");
150 # endif /* RAINBOW */
152 mapkey("#S", "x", WHEN_VICMD
|WHEN_INMV
, "<Delete>");
153 mapkey("#s", "B", WHEN_VICMD
|WHEN_INMV
, "^<Left>");
154 mapkey("#t", "W", WHEN_VICMD
|WHEN_INMV
, "^<Right>");
156 #else /* not MSDOS */
158 mapkey("\033[P", "x", WHEN_VICMD
|WHEN_INMV
, "<Del>");
160 if (ERASEKEY
!= '\177')
162 mapkey("\177", "x", WHEN_VICMD
|WHEN_INMV
, "<Del>");
169 #endif /* NO_DIGRAPH */
171 /* process any flags */
172 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++)
176 case 'R': /* readonly */
183 case 'r': /* recover */
184 msg("Use the `elvrec` program to recover lost files");
202 case 'v': /* vi mode */
206 case 'e': /* ex mode */
209 #ifndef NO_EXTENSIONS
210 case 'i': /* input mode */
215 case 'm': /* use "errlist" as the errlist */
220 else if (i
+ 1 < argc
)
231 case 'c': /* run the following command, later */
243 msg("Ignoring unknown flag \"%s\"", argv
[i
]);
247 /* if we were given an initial ex command, save it... */
248 if (i
< argc
&& *argv
[i
] == '+')
256 cmd
= "$"; /* "vi + file" means start at EOF */
261 /* the remaining args are file names. */
264 strcpy(args
, argv
[i
]);
265 while (++i
< argc
&& strlen(args
) + 1 + strlen(argv
[i
]) < sizeof args
)
268 strcat(args
, argv
[i
]);
271 /* expand wildcard characters, if necessary */
272 if (strchr(args
, '*') || strchr(args
, '?'))
274 strcpy(args
, wildcard(args
));
277 strcpy(tmpblk
.c
, args
);
278 cmd_args(MARK_UNSET
, MARK_UNSET
, CMD_ARGS
, TRUE
, tmpblk
.c
);
282 /* empty args list */
288 /* perform the .exrc files and EXINIT environment variable */
293 str
= getenv("HOME");
296 strcpy(tmpblk
.c
, str
);
297 str
= tmpblk
.c
+ strlen(tmpblk
.c
);
299 if (str
[-1] != SLASH
)
315 str
= getenv(EXINIT
);
318 exstring(str
, strlen(str
));
322 /* search for a tag (or an error) now, if desired */
326 cmd_tag(MARK_FIRST
, MARK_FIRST
, CMD_TAG
, 0, tag
);
331 cmd_errlist(MARK_FIRST
, MARK_FIRST
, CMD_ERRLIST
, 0, err
);
335 /* if no tag/err, or tag failed, then start with first arg */
338 /* start with first arg */
339 cmd_next(MARK_UNSET
, MARK_UNSET
, CMD_NEXT
, FALSE
, "");
341 /* pretend to do something, just to force a recoverable
342 * version of the file out to disk
347 clrflag(file
, MODIFIED
);
350 /* now we do the immediate ex command that we noticed before */
356 /* repeatedly call ex() or vi() (depending on the mode) until the
357 * mode is set to MODE_QUIT
359 while (mode
!= MODE_QUIT
)
363 /* Maybe we just aborted a change? */
366 signal(SIGINT
, (void(*)()) trapint
);
379 msg("mode = %d?", mode
);
385 /* free up the cut buffers */
389 #ifndef NO_CURSORSHAPE
408 resume_curses(FALSE
);
413 #if TURBOC || __GNUC__
414 signal(signo
, (void (*)())trapint
);
416 signal(signo
, trapint
);
429 /* This stuff us used to build the default digraphs table. */
430 static char digtable
[][4] =
433 "C,\200", "u\"\1", "e'\2", "a^\3",
434 "a\"\4", "a`\5", "a@\6", "c,\7",
435 "e^\10", "e\"\211", "e`\12", "i\"\13",
436 "i^\14", "i`\15", "A\"\16", "A@\17",
437 "E'\20", "ae\21", "AE\22", "o^\23",
438 "o\"\24", "o`\25", "u^\26", "u`\27",
439 "y\"\30", "O\"\31", "U\"\32", "a'\240",
440 "i'!", "o'\"", "u'#", "n~$",
441 "N~%", "a-&", "o-'", "~?(",
442 "~!-", "\"<.", "\">/",
444 "2/+", "4/,", "^+;", "^q<",
445 "^c=", "^r>", "^t?", "pp]",
446 "^^^", "oo_", "*a`", "*ba",
447 "*pc", "*Sd", "*se", "*uf",
448 "*tg", "*Ph", "*Ti", "*Oj",
449 "*dk", "*Hl", "*hm", "*En",
450 "*No", "eqp", "pmq", "ger",
451 "les", "*It", "*iu", "*/v",
452 "*=w", "sq{", "^n|", "^2}",
454 # endif /* CS_SPECIAL */
455 # endif /* CS_IBMPC */
457 "~!!", "a-*", "\">+", "o-:",
460 "A`@", "A'A", "A^B", "A~C",
461 "A\"D", "A@E", "AEF", "C,G",
462 "E`H", "E'I", "E^J", "E\"K",
463 "I`L", "I'M", "I^N", "I\"O",
464 "-DP", "N~Q", "O`R", "O'S",
465 "O^T", "O~U", "O\"V", "O/X",
466 "U`Y", "U'Z", "U^[", "U\"\\",
469 "a``", "a'a", "a^b", "a~c",
470 "a\"d", "a@e", "aef", "c,g",
471 "e`h", "e'i", "e^j", "e\"k",
472 "i`l", "i'm", "i^n", "i\"o",
473 "-dp", "n~q", "o`r", "o's",
474 "o^t", "o~u", "o\"v", "o/x",
475 "u`y", "u'z", "u^{", "u\"|",
477 # endif /* CS_LATIN1 */
481 static init_digraphs()
485 for (i
= 0; *digtable
[i
]; i
++)
487 do_digraph(FALSE
, digtable
[i
]);
489 do_digraph(FALSE
, (char *)0);
491 #endif /* NO_DIGRAPH */