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 */
23 extern trapint(); /* defined below */
24 extern char *getenv();
28 static init_digraphs();
31 /*---------------------------------------------------------------------*/
48 char *cmd
= (char *)0;
49 char *err
= (char *)0;
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])
60 case 'w': /* "view" */
65 case 't': /* "edit" or "input" */
70 default: /* "vi" or "elvis" */
76 /* normally, we ignore SIGQUIT. SIGINT is trapped later */
77 signal(SIGQUIT
, SIG_IGN
);
81 /* temporarily ignore SIGINT */
82 signal(SIGINT
, SIG_IGN
);
88 scrollok(stdscr
, TRUE
);
90 /* arrange for deadly signals to be caught */
92 signal(SIGHUP
, (void(*)()) deathtrap
);
96 signal(SIGILL
, (void(*)()) deathtrap
);
99 signal(SIGBUS
, (void(*)()) deathtrap
);
102 signal(SIGSEGV
, (void(*)()) deathtrap
);
105 signal(SIGSYS
, (void(*)()) deathtrap
);
109 signal(SIGPIPE
, (void(*)()) deathtrap
);
112 signal(SIGTERM
, (void(*)()) deathtrap
);
115 signal(SIGUSR1
, (void(*)()) deathtrap
);
118 signal(SIGUSR2
, (void(*)()) deathtrap
);
121 /* initialize the options - must be done after initscr(), so that
122 * we can alter LINES and COLS if necessary.
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>");
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>");
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 */
162 mapkey("\033[P", "x", WHEN_VICMD
|WHEN_INMV
, "<Del>");
165 mapkey("\233?~", "\\", WHEN_VICMD
|WHEN_INMV
, "<Help>");
168 if (ERASEKEY
!= '\177')
170 mapkey("\177", "x", WHEN_VICMD
|WHEN_INMV
, "<Del>");
177 #endif /* NO_DIGRAPH */
179 /* process any flags */
180 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++)
184 case 'R': /* readonly */
189 case 'r': /* recover */
190 msg("Use the `elvrec` program to recover lost files");
208 case 'v': /* vi mode */
212 case 'e': /* ex mode */
215 #ifndef NO_EXTENSIONS
216 case 'i': /* input mode */
221 case 'm': /* use "errlist" as the errlist */
226 else if (i
+ 1 < argc
)
237 case 'c': /* run the following command, later */
248 case 'w': /* set the window size */
251 *o_window
= atoi(argv
[i
] + 2);
256 *o_window
= atoi(argv
[++i
]);
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
] == '+')
275 cmd
= "$"; /* "vi + file" means start at EOF */
280 /* the remaining args are file names. */
283 strcpy(args
, argv
[i
]);
284 while (++i
< argc
&& strlen(args
) + 1 + strlen(argv
[i
]) < sizeof args
)
287 strcat(args
, argv
[i
]);
290 /* expand wildcard characters, if necessary */
291 if (strchr(args
, '*') || strchr(args
, '?'))
293 strcpy(args
, wildcard(args
));
296 strcpy(tmpblk
.c
, args
);
297 cmd_args(MARK_UNSET
, MARK_UNSET
, CMD_ARGS
, TRUE
, tmpblk
.c
);
301 /* empty args list */
307 /* perform the .exrc files and EXINIT environment variable */
312 str
= getenv("HOME");
315 strcpy(tmpblk
.c
, str
);
316 str
= tmpblk
.c
+ strlen(tmpblk
.c
);
318 # if AMIGA /* Don't SLASH a device. "Elvis:.exrc" */
319 if (str
[-1] != COLON
&& str
[-1] != SLASH
)
321 if (str
[-1] != SLASH
)
338 str
= getenv(EXINIT
);
341 exstring(str
, strlen(str
), ctrl('V'));
345 /* search for a tag (or an error) now, if desired */
349 cmd_tag(MARK_FIRST
, MARK_FIRST
, CMD_TAG
, 0, tag
);
354 cmd_errlist(MARK_FIRST
, MARK_FIRST
, CMD_ERRLIST
, 0, err
);
358 /* if no tag/err, or tag failed, then start with first arg */
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
370 clrflag(file
, MODIFIED
);
373 /* now we do the immediate ex command that we noticed before */
379 /* repeatedly call ex() or vi() (depending on the mode) until the
380 * mode is set to MODE_QUIT
382 while (mode
!= MODE_QUIT
)
386 /* Maybe we just aborted a change? */
389 signal(SIGINT
, (void(*)()) trapint
);
402 msg("mode = %d?", mode
);
408 /* free up the cut buffers */
412 #ifndef NO_CURSORSHAPE
432 resume_curses(FALSE
);
437 signal(signo
, (void (*)())trapint
);
449 /* This stuff us used to build the default digraphs table. */
450 static char digtable
[][4] =
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 "~!-", "\"<.", "\">/",
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}",
474 # endif /* CS_SPECIAL */
475 # endif /* CS_IBMPC */
477 "~!!", "a-*", "\">+", "o-:",
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\"\\",
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\"|",
497 # endif /* CS_LATIN1 */
501 static init_digraphs()
505 for (i
= 0; *digtable
[i
]; i
++)
507 do_digraph(FALSE
, digtable
[i
]);
509 do_digraph(FALSE
, (char *)0);
512 #endif /* NO_DIGRAPH */