libutil: add O_NOCTTY back to old pty open code
[minix.git] / commands / elvis / main.c
blob0198b8be4761dd2597f161f6123bb3a43d36486f
1 /* main.c */
3 /* Author:
4 * Steve Kirkendall
5 * 14407 SW Teal Blvd. #C
6 * Beaverton, OR 97005
7 * kirkenda@cs.pdx.edu
8 */
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 */
15 #if __BORLANDC__
16 #include "borland.h"
17 #endif
19 #include "config.h"
20 #include <setjmp.h>
21 #include "vi.h"
23 extern trapint(); /* defined below */
24 extern char *getenv();
25 jmp_buf jmpenv;
27 #ifndef NO_DIGRAPH
28 static init_digraphs();
29 #endif
31 /*---------------------------------------------------------------------*/
33 #if AMIGA
34 # include "amiwild.c"
35 main (argc, argv)
36 #else
37 # if VMS
38 # include "vmswild.c"
39 main (argc, argv)
40 # else
41 int main(argc, argv)
42 # endif
43 #endif
44 int argc;
45 char *argv[];
47 int i;
48 char *cmd = (char *)0;
49 char *err = (char *)0;
50 char *str;
51 char *tag = (char *)0;
53 /* set mode to MODE_VI or MODE_EX depending on program name */
54 switch (argv[0][strlen(argv[0]) - 1])
56 case 'x': /* "ex" */
57 mode = MODE_EX;
58 break;
60 case 'w': /* "view" */
61 mode = MODE_VI;
62 *o_readonly = TRUE;
63 break;
64 #ifndef NO_EXTENSIONS
65 case 't': /* "edit" or "input" */
66 mode = MODE_VI;
67 *o_inputmode = TRUE;
68 break;
69 #endif
70 default: /* "vi" or "elvis" */
71 mode = MODE_VI;
74 #ifndef DEBUG
75 # ifdef SIGQUIT
76 /* normally, we ignore SIGQUIT. SIGINT is trapped later */
77 signal(SIGQUIT, SIG_IGN);
78 # endif
79 #endif
81 /* temporarily ignore SIGINT */
82 signal(SIGINT, SIG_IGN);
84 /* start curses */
85 initscr();
86 cbreak();
87 noecho();
88 scrollok(stdscr, TRUE);
90 /* arrange for deadly signals to be caught */
91 # ifdef SIGHUP
92 signal(SIGHUP, (void(*)()) deathtrap);
93 # endif
94 # ifndef DEBUG
95 # ifdef SIGILL
96 signal(SIGILL, (void(*)()) deathtrap);
97 # endif
98 # ifdef SIGBUS
99 signal(SIGBUS, (void(*)()) deathtrap);
100 # endif
101 # ifdef SIGSEGV
102 signal(SIGSEGV, (void(*)()) deathtrap);
103 # endif
104 # ifdef SIGSYS
105 signal(SIGSYS, (void(*)()) deathtrap);
106 # endif
107 # endif /* !DEBUG */
108 # ifdef SIGPIPE
109 signal(SIGPIPE, (void(*)()) deathtrap);
110 # endif
111 # ifdef SIGTERM
112 signal(SIGTERM, (void(*)()) deathtrap);
113 # endif
114 # ifdef SIGUSR1
115 signal(SIGUSR1, (void(*)()) deathtrap);
116 # endif
117 # ifdef SIGUSR2
118 signal(SIGUSR2, (void(*)()) deathtrap);
119 # endif
121 /* initialize the options - must be done after initscr(), so that
122 * we can alter LINES and COLS if necessary.
124 initopts();
126 /* map the arrow keys. The KU,KD,KL,and KR variables correspond to
127 * the :ku=: (etc.) termcap capabilities. The variables are defined
128 * as part of the curses package.
130 if (has_KU) mapkey(has_KU, "k", WHEN_VICMD|WHEN_INMV, "<Up>");
131 if (has_KD) mapkey(has_KD, "j", WHEN_VICMD|WHEN_INMV, "<Down>");
132 if (has_KL) mapkey(has_KL, "h", WHEN_VICMD|WHEN_INMV, "<Left>");
133 if (has_KR) mapkey(has_KR, "l", WHEN_VICMD|WHEN_INMV, "<Right>");
134 if (has_HM) mapkey(has_HM, "^", WHEN_VICMD|WHEN_INMV, "<Home>");
135 if (has_EN) mapkey(has_EN, "$", WHEN_VICMD|WHEN_INMV, "<End>");
136 if (has_PU) mapkey(has_PU, "\002", WHEN_VICMD|WHEN_INMV, "<PageUp>");
137 if (has_PD) mapkey(has_PD, "\006", WHEN_VICMD|WHEN_INMV, "<PageDn>");
138 if (has_KI) mapkey(has_KI, "i", WHEN_VICMD|WHEN_INMV, "<Insert>");
139 #if MSDOS
140 # if RAINBOW
141 if (!strcmp("rainbow", o_term))
143 mapkey("\033[1~", "/", WHEN_VICMD, "<Find>");
144 mapkey("\033[3~", "x", WHEN_VICMD|WHEN_INMV, "<Remove>");
145 mapkey("\033[4~", "v", WHEN_VICMD|WHEN_INMV, "<Select>");
146 mapkey("\033[17~", ":sh\n", WHEN_VICMD, "<Intrpt>");
147 mapkey("\033[19~", ":q\n", WHEN_VICMD, "<Cancel>");
148 mapkey("\033[21~", "ZZ", WHEN_VICMD, "<Exit>");
149 mapkey("\033[26~", "V", WHEN_VICMD|WHEN_INMV, "<AddlOp>");
150 mapkey("\033[28~", "\\", WHEN_VICMD|WHEN_INMV, "<Help>");
151 mapkey("\033[29~", "K", WHEN_VICMD|WHEN_INMV, "<Do>");
153 else
154 # endif /* RAINBOW */
156 mapkey("#S", "x", WHEN_VICMD|WHEN_INMV, "<Delete>");
157 mapkey("#s", "B", WHEN_VICMD|WHEN_INMV, "^<Left>");
158 mapkey("#t", "W", WHEN_VICMD|WHEN_INMV, "^<Right>");
160 #else /* not MSDOS */
161 # if COHERENT
162 mapkey("\033[P", "x", WHEN_VICMD|WHEN_INMV, "<Del>");
163 # else
164 #if AMIGA
165 mapkey("\233?~", "\\", WHEN_VICMD|WHEN_INMV, "<Help>");
166 #endif
168 if (ERASEKEY != '\177')
170 mapkey("\177", "x", WHEN_VICMD|WHEN_INMV, "<Del>");
172 # endif
173 #endif
175 #ifndef NO_DIGRAPH
176 init_digraphs();
177 #endif /* NO_DIGRAPH */
179 /* process any flags */
180 for (i = 1; i < argc && *argv[i] == '-'; i++)
182 switch (argv[i][1])
184 case 'R': /* readonly */
185 *o_readonly = TRUE;
186 break;
188 case 'L':
189 case 'r': /* recover */
190 msg("Use the `elvrec` program to recover lost files");
191 endmsgs();
192 refresh();
193 endwin();
194 exit(0);
195 break;
197 case 't': /* tag */
198 if (argv[i][2])
200 tag = argv[i] + 2;
202 else
204 tag = argv[++i];
206 break;
208 case 'v': /* vi mode */
209 mode = MODE_VI;
210 break;
212 case 'e': /* ex mode */
213 mode = MODE_EX;
214 break;
215 #ifndef NO_EXTENSIONS
216 case 'i': /* input mode */
217 *o_inputmode = TRUE;
218 break;
219 #endif
220 #ifndef NO_ERRLIST
221 case 'm': /* use "errlist" as the errlist */
222 if (argv[i][2])
224 err = argv[i] + 2;
226 else if (i + 1 < argc)
228 err = argv[++i];
230 else
232 err = "";
234 break;
235 #endif
236 #ifndef CRUNCH
237 case 'c': /* run the following command, later */
238 if (argv[i][2])
240 cmd = argv[i] + 2;
242 else
244 cmd = argv[++i];
246 break;
248 case 'w': /* set the window size */
249 if (argv[i][2])
251 *o_window = atoi(argv[i] + 2);
252 wset = TRUE;
254 else
256 *o_window = atoi(argv[++i]);
257 wset = TRUE;
259 break;
260 #endif
261 default:
262 msg("Ignoring unknown flag \"%s\"", argv[i]);
266 /* if we were given an initial ex command, save it... */
267 if (i < argc && *argv[i] == '+')
269 if (argv[i][1])
271 cmd = argv[i++] + 1;
273 else
275 cmd = "$"; /* "vi + file" means start at EOF */
276 i++;
280 /* the remaining args are file names. */
281 if (i < argc)
283 strcpy(args, argv[i]);
284 while (++i < argc && strlen(args) + 1 + strlen(argv[i]) < sizeof args)
286 strcat(args, " ");
287 strcat(args, argv[i]);
289 #if MSDOS || TOS
290 /* expand wildcard characters, if necessary */
291 if (strchr(args, '*') || strchr(args, '?'))
293 strcpy(args, wildcard(args));
295 #endif
296 strcpy(tmpblk.c, args);
297 cmd_args(MARK_UNSET, MARK_UNSET, CMD_ARGS, TRUE, tmpblk.c);
299 else
301 /* empty args list */
302 args[0] = '\0';
303 nargs = 1;
304 argno = -1;
307 /* perform the .exrc files and EXINIT environment variable */
308 #ifdef SYSEXRC
309 doexrc(SYSEXRC);
310 #endif
311 #ifdef HMEXRC
312 str = getenv("HOME");
313 if (str && *str)
315 strcpy(tmpblk.c, str);
316 str = tmpblk.c + strlen(tmpblk.c);
317 #if !VMS
318 # if AMIGA /* Don't SLASH a device. "Elvis:.exrc" */
319 if (str[-1] != COLON && str[-1] != SLASH)
320 # else
321 if (str[-1] != SLASH)
322 # endif
324 *str++ = SLASH;
326 #endif
327 strcpy(str, HMEXRC);
328 doexrc(tmpblk.c);
330 #endif
331 #ifndef CRUNCH
332 if (*o_exrc)
333 #endif
335 doexrc(EXRC);
337 #ifdef EXINIT
338 str = getenv(EXINIT);
339 if (str)
341 exstring(str, strlen(str), ctrl('V'));
343 #endif
345 /* search for a tag (or an error) now, if desired */
346 blkinit();
347 if (tag)
349 cmd_tag(MARK_FIRST, MARK_FIRST, CMD_TAG, 0, tag);
351 #ifndef NO_ERRLIST
352 else if (err)
354 cmd_errlist(MARK_FIRST, MARK_FIRST, CMD_ERRLIST, 0, err);
356 #endif
358 /* if no tag/err, or tag failed, then start with first arg */
359 if (tmpfd < 0)
361 /* start with first arg */
362 cmd_next(MARK_UNSET, MARK_UNSET, CMD_NEXT, FALSE, "");
364 /* pretend to do something, just to force a recoverable
365 * version of the file out to disk
367 ChangeText
370 clrflag(file, MODIFIED);
373 /* now we do the immediate ex command that we noticed before */
374 if (cmd)
376 doexcmd(cmd);
379 /* repeatedly call ex() or vi() (depending on the mode) until the
380 * mode is set to MODE_QUIT
382 while (mode != MODE_QUIT)
384 if (setjmp(jmpenv))
386 /* Maybe we just aborted a change? */
387 abortdo();
389 signal(SIGINT, (void(*)()) trapint);
391 switch (mode)
393 case MODE_VI:
394 vi();
395 break;
397 case MODE_EX:
398 ex();
399 break;
400 #ifdef DEBUG
401 default:
402 msg("mode = %d?", mode);
403 mode = MODE_QUIT;
404 #endif
408 /* free up the cut buffers */
409 cutend();
411 /* end curses */
412 #ifndef NO_CURSORSHAPE
413 if (has_CQ)
414 do_CQ();
415 #endif
416 endmsgs();
417 move(LINES - 1, 0);
418 clrtoeol();
419 refresh();
420 endwin();
422 exit(0);
423 /*NOTREACHED*/
427 /*ARGSUSED*/
428 int trapint(signo)
429 int signo;
431 beep();
432 resume_curses(FALSE);
433 abortdo();
434 #if OSK
435 sigmask(-1);
436 #endif
437 signal(signo, (void (*)())trapint);
438 doingglobal = FALSE;
440 longjmp(jmpenv, 1);
442 return 0;
447 #ifndef NO_DIGRAPH
449 /* This stuff us used to build the default digraphs table. */
450 static char digtable[][4] =
452 # ifdef CS_IBMPC
453 "C,\200", "u\"\1", "e'\2", "a^\3",
454 "a\"\4", "a`\5", "a@\6", "c,\7",
455 "e^\10", "e\"\211", "e`\12", "i\"\13",
456 "i^\14", "i`\15", "A\"\16", "A@\17",
457 "E'\20", "ae\21", "AE\22", "o^\23",
458 "o\"\24", "o`\25", "u^\26", "u`\27",
459 "y\"\30", "O\"\31", "U\"\32", "a'\240",
460 "i'!", "o'\"", "u'#", "n~$",
461 "N~%", "a-&", "o-'", "~?(",
462 "~!-", "\"<.", "\">/",
463 # ifdef CS_SPECIAL
464 "2/+", "4/,", "^+;", "^q<",
465 "^c=", "^r>", "^t?", "pp]",
466 "^^^", "oo_", "*a`", "*ba",
467 "*pc", "*Sd", "*se", "*uf",
468 "*tg", "*Ph", "*Ti", "*Oj",
469 "*dk", "*Hl", "*hm", "*En",
470 "*No", "eqp", "pmq", "ger",
471 "les", "*It", "*iu", "*/v",
472 "*=w", "sq{", "^n|", "^2}",
473 "^3~", "^_\377",
474 # endif /* CS_SPECIAL */
475 # endif /* CS_IBMPC */
476 # ifdef CS_LATIN1
477 "~!!", "a-*", "\">+", "o-:",
478 "\"<>", "~??",
480 "A`@", "A'A", "A^B", "A~C",
481 "A\"D", "A@E", "AEF", "C,G",
482 "E`H", "E'I", "E^J", "E\"K",
483 "I`L", "I'M", "I^N", "I\"O",
484 "-DP", "N~Q", "O`R", "O'S",
485 "O^T", "O~U", "O\"V", "O/X",
486 "U`Y", "U'Z", "U^[", "U\"\\",
487 "Y'_",
489 "a``", "a'a", "a^b", "a~c",
490 "a\"d", "a@e", "aef", "c,g",
491 "e`h", "e'i", "e^j", "e\"k",
492 "i`l", "i'm", "i^n", "i\"o",
493 "-dp", "n~q", "o`r", "o's",
494 "o^t", "o~u", "o\"v", "o/x",
495 "u`y", "u'z", "u^{", "u\"|",
496 "y'~",
497 # endif /* CS_LATIN1 */
501 static init_digraphs()
503 int i;
505 for (i = 0; *digtable[i]; i++)
507 do_digraph(FALSE, digtable[i]);
509 do_digraph(FALSE, (char *)0);
510 return 0;
512 #endif /* NO_DIGRAPH */