s_resize -> s_rabs, add s_rrel
[nvi.git] / common / main.c
blob6fe6e3452c225e8f1dcb65ed4e7792e164218d7f
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char copyright[] =
10 "%Z% Copyright (c) 1992, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
12 #endif /* not lint */
14 #ifndef lint
15 static char sccsid[] = "$Id: main.c,v 8.50 1993/12/02 10:26:24 bostic Exp $ (Berkeley) $Date: 1993/12/02 10:26:24 $";
16 #endif /* not lint */
18 #include <sys/param.h>
19 #include <sys/stat.h>
21 #include <ctype.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <termios.h>
28 #include <unistd.h>
30 #ifdef __STDC__
31 #include <stdarg.h>
32 #else
33 #include <varargs.h>
34 #endif
36 #include "vi.h"
37 #include "excmd.h"
38 #include "pathnames.h"
39 #include "tag.h"
41 static GS *gs_init __P((void));
42 static void gs_end __P((GS *));
43 static void h_hup __P((int));
44 static void h_term __P((int));
45 static void h_winch __P((int));
46 static void obsolete __P((char *[]));
47 static void usage __P((int));
49 GS *__global_list; /* GLOBAL: List of screens. */
51 int
52 main(argc, argv)
53 int argc;
54 char *argv[];
56 extern int optind;
57 extern char *optarg;
58 static int reenter; /* STATIC: Re-entrancy check. */
59 struct sigaction act;
60 struct stat sb;
61 GS *gp;
62 FREF *frp;
63 SCR *sp;
64 u_int flags;
65 int ch, eval, flagchk, readonly, silent, snapshot;
66 char *excmdarg, *myname, *p, *rec_f, *tag_f, *trace_f, *wsizearg;
67 char path[MAXPATHLEN];
69 /* Stop if indirecting through a NULL pointer. */
70 if (reenter++)
71 abort();
73 /* Set screen type and mode based on the program name. */
74 readonly = 0;
75 if ((myname = strrchr(*argv, '/')) == NULL)
76 myname = *argv;
77 else
78 ++myname;
79 if (!strcmp(myname, "ex") || !strcmp(myname, "nex"))
80 LF_INIT(S_EX);
81 else {
82 /* View is readonly. */
83 if (!strcmp(myname, "view"))
84 readonly = 1;
85 LF_INIT(S_VI_CURSES);
88 /* Convert old-style arguments into new-style ones. */
89 obsolete(argv);
91 /* Parse the arguments. */
92 flagchk = '\0';
93 excmdarg = rec_f = tag_f = trace_f = wsizearg = NULL;
94 silent = 0;
95 snapshot = 1;
96 while ((ch = getopt(argc, argv, "c:eFlRr:sT:t:vw:x:")) != EOF)
97 switch (ch) {
98 case 'c': /* Run the command. */
99 excmdarg = optarg;
100 break;
101 case 'e': /* Ex mode. */
102 LF_CLR(S_SCREENS);
103 LF_SET(S_EX);
104 break;
105 case 'F': /* No snapshot. */
106 snapshot = 0;
107 break;
108 case 'l':
109 if (flagchk != '\0' && flagchk != 'l')
110 errx(1,
111 "only one of -%c and -l may be specified.",
112 flagchk);
113 flagchk = 'l';
114 break;
115 case 'R': /* Readonly. */
116 readonly = 1;
117 break;
118 case 'r': /* Recover. */
119 if (flagchk == 'r')
120 errx(1,
121 "only one recovery file may be specified.");
122 if (flagchk != '\0')
123 errx(1,
124 "only one of -%c and -r may be specified.",
125 flagchk);
126 flagchk = 'r';
127 rec_f = optarg;
128 break;
129 case 's':
130 if (!LF_ISSET(S_EX))
131 errx(1, "-s only applicable to ex.");
132 silent = 1;
133 break;
134 case 'T': /* Trace. */
135 trace_f = optarg;
136 break;
137 case 't': /* Tag. */
138 if (flagchk == 't')
139 errx(1,
140 "only one tag file may be specified.");
141 if (flagchk != '\0')
142 errx(1,
143 "only one of -%c and -t may be specified.",
144 flagchk);
145 flagchk = 't';
146 tag_f = optarg;
147 break;
148 case 'v': /* Vi mode. */
149 LF_CLR(S_SCREENS);
150 LF_SET(S_VI_CURSES);
151 break;
152 case 'w':
153 wsizearg = optarg;
154 break;
155 case 'x':
156 if (!strcmp(optarg, "aw")) {
157 F_CLR(sp, S_SCREENS);
158 F_SET(sp, S_VI_XAW);
159 break;
161 /* FALLTHROUGH */
162 case '?':
163 default:
164 usage(LF_ISSET(S_EX));
166 argc -= optind;
167 argv += optind;
169 /* Build and initialize the GS structure. */
170 __global_list = gp = gs_init();
172 if (snapshot)
173 F_SET(gp, G_SNAPSHOT);
175 /* Build and initialize the first/current screen. */
176 if (screen_init(NULL, &sp, flags))
177 goto err1;
178 CIRCLEQ_INSERT_HEAD(&__global_list->dq, sp, q);
180 if (trace_f != NULL) {
181 #ifdef DEBUG
182 if ((gp->tracefp = fopen(optarg, "w")) == NULL)
183 err(1, "%s", optarg);
184 (void)fprintf(gp->tracefp, "\n===\ntrace: open %s\n", optarg);
185 #else
186 msgq(sp, M_ERR, "-T support not compiled into this version.");
187 #endif
190 if (set_window_size(sp, 0, 0)) /* Set the window size. */
191 goto err1;
193 if (opts_init(sp)) /* Options initialization. */
194 goto err1;
195 if (readonly)
196 O_SET(sp, O_READONLY);
197 if (silent) {
198 O_CLR(sp, O_AUTOPRINT);
199 O_CLR(sp, O_PROMPT);
200 O_CLR(sp, O_VERBOSE);
201 O_CLR(sp, O_WARN);
202 F_SET(sp, S_EXSILENT);
204 if (wsizearg != NULL) {
205 ARGS *av[2], a;
206 a.bp = path;
207 av[0] = &a;
208 av[1] = NULL;
209 if (strtol(optarg, &p, 10) < 0 || *p)
210 errx(1, "illegal window size -- %s", optarg);
211 (void)snprintf(path, sizeof(path), "window=%s", optarg);
212 if (opts_set(sp, av))
213 msgq(sp, M_ERR,
214 "Unable to set command line window option");
217 /* Keymaps, special keys, must follow option initializations. */
218 if (term_init(sp))
219 goto err1;
221 #ifdef DIGRAPHS
222 if (digraph_init(sp)) /* Digraph initialization. */
223 goto err1;
224 #endif
227 * Source the system, environment, ~user and local .exrc values.
228 * If the environment exists, vi historically doesn't check ~user.
229 * This is done before the file is read in because things in the
230 * .exrc information can set, for example, the recovery directory.
232 if (!silent) {
233 if (!stat(_PATH_SYSEXRC, &sb))
234 (void)ex_cfile(sp, NULL, _PATH_SYSEXRC);
236 /* Source the EXINIT environment variable. */
237 if ((p = getenv("EXINIT")) != NULL)
238 if ((p = strdup(p)) == NULL) {
239 msgq(sp, M_SYSERR, NULL);
240 goto err1;
241 } else {
242 (void)ex_icmd(sp, NULL, p, strlen(p));
243 free(p);
245 else if ((p = getenv("HOME")) != NULL && *p) {
246 (void)snprintf(path,
247 sizeof(path), "%s/%s", p, _PATH_NEXRC);
248 if (!stat(path, &sb))
249 (void)ex_cfile(sp, NULL, path);
250 else {
251 (void)snprintf(path,
252 sizeof(path), "%s/%s", p, _PATH_EXRC);
253 if (!stat(path, &sb))
254 (void)ex_cfile(sp, NULL, path);
257 if (O_ISSET(sp, O_EXRC) && !stat(_PATH_EXRC, &sb))
258 (void)ex_cfile(sp, NULL, _PATH_EXRC);
261 /* List recovery files if -l specified. */
262 if (flagchk == 'l')
263 exit(rcv_list(sp));
265 /* Use a tag file or recovery file if specified. */
266 if (tag_f != NULL && ex_tagfirst(sp, tag_f))
267 goto err1;
268 else if (rec_f != NULL && rcv_read(sp, rec_f))
269 goto err1;
271 /* Append any remaining arguments as file names. */
272 if (*argv != NULL)
273 for (; *argv != NULL; ++argv)
274 if (file_add(sp, NULL, *argv, 0) == NULL)
275 goto err1;
278 * If no recovery or tag file, get an EXF structure.
279 * If no argv file, use a temporary file.
281 if (tag_f == NULL && rec_f == NULL) {
282 if ((frp = file_first(sp, 1)) == NULL &&
283 (frp = file_add(sp, NULL, NULL, 0)) == NULL)
284 goto err1;
285 if (file_init(sp, frp, NULL, 0))
286 goto err1;
290 * Initialize the signals. Use sigaction(2), not signal(3), because
291 * we don't want to always restart system calls on 4BSD systems. It
292 * would be nice in some cases to restart system calls, but SA_RESTART
293 * is a 4BSD extension so we can't use it.
295 * SIGWINCH, SIGHUP, SIGTERM:
296 * Catch and set a global bit.
298 act.sa_handler = h_hup;
299 sigemptyset(&act.sa_mask);
300 act.sa_flags = 0;
301 (void)sigaction(SIGHUP, &act, NULL);
302 act.sa_handler = h_term;
303 sigemptyset(&act.sa_mask);
304 act.sa_flags = 0;
305 (void)sigaction(SIGTERM, &act, NULL);
306 act.sa_handler = h_winch;
307 sigemptyset(&act.sa_mask);
308 act.sa_flags = 0;
309 (void)sigaction(SIGWINCH, &act, NULL);
312 * SIGQUIT:
313 * Always ignore.
315 act.sa_handler = SIG_IGN;
316 sigemptyset(&act.sa_mask);
317 act.sa_flags = 0;
318 (void)sigaction(SIGQUIT, &act, NULL);
321 * If there's an initial command, push it on the command stack.
322 * Historically, it was always an ex command, not vi in vi mode
323 * or ex in ex mode. So, make it look like an ex command to vi.
325 if (excmdarg != NULL)
326 if (IN_EX_MODE(sp)) {
327 if (term_push(sp, excmdarg, strlen(excmdarg), 0, 0))
328 goto err1;
329 } else if (IN_VI_MODE(sp)) {
330 if (term_push(sp, "\n", 1, 0, 0))
331 goto err1;
332 if (term_push(sp, excmdarg, strlen(excmdarg), 0, 0))
333 goto err1;
334 if (term_push(sp, ":", 1, 0, 0))
335 goto err1;
338 /* Vi reads from the terminal. */
339 if (!F_ISSET(gp, G_ISFROMTTY) && !F_ISSET(sp, S_EX)) {
340 msgq(sp, M_ERR, "Vi's standard input must be a terminal.");
341 goto err1;
344 for (;;) {
345 if (sp->s_edit(sp, sp->ep))
346 goto err2;
349 * Edit the next screen on the display queue, or, move
350 * a screen from the hidden queue to the display queue.
352 if ((sp = __global_list->dq.cqh_first) ==
353 (void *)&__global_list->dq)
354 if ((sp = __global_list->hq.cqh_first) !=
355 (void *)&__global_list->hq) {
356 CIRCLEQ_REMOVE(&sp->gp->hq, sp, q);
357 CIRCLEQ_INSERT_TAIL(&sp->gp->dq, sp, q);
358 } else
359 break;
362 * The screen type may have changed -- reinitialize the
363 * functions in case it has.
365 switch (F_ISSET(sp, S_SCREENS)) {
366 case S_EX:
367 if (sex_screen_init(sp))
368 return (1);
369 break;
370 case S_VI_CURSES:
371 if (svi_screen_init(sp))
372 return (1);
373 break;
374 case S_VI_XAW:
375 if (xaw_screen_init(sp))
376 return (1);
377 break;
378 default:
379 abort();
384 * Two error paths. The first means that something failed before
385 * we called a screen routine. Swap the message pointers between
386 * the SCR and the GS, so messages get displayed. The second is
387 * something failed in a screen. NOTE: sp may be GONE when the
388 * screen returns, so only the gp can be trusted.
390 eval = 0;
391 if (0) {
392 err1: gp->msgq.lh_first = sp->msgq.lh_first;
393 err2: eval = 1;
396 gs_end(gp);
398 /* Make absolutely sure that the modes are restored correctly. */
399 if (F_ISSET(gp, G_ISFROMTTY) &&
400 tcsetattr(STDIN_FILENO, TCSADRAIN, &gp->original_termios))
401 err(1, "tcsetattr");
402 exit(eval);
406 * gs_init --
407 * Build and initialize the GS structure.
409 static GS *
410 gs_init()
412 GS *gp;
413 int fd;
415 if ((gp = calloc(1, sizeof(GS))) == NULL)
416 err(1, NULL);
418 CIRCLEQ_INIT(&gp->dq);
419 CIRCLEQ_INIT(&gp->hq);
420 LIST_INIT(&gp->msgq);
422 /* Structures shared by screens so stored in the GS structure. */
423 if ((gp->tty = calloc(1, sizeof(IBUF))) == NULL)
424 err(1, NULL);
426 LIST_INIT(&gp->cutq);
427 LIST_INIT(&gp->seqq);
429 /* Set a flag if we're reading from the tty. */
430 if (isatty(STDIN_FILENO))
431 F_SET(gp, G_ISFROMTTY);
434 * XXX
435 * Set a flag and don't do terminal sets/resets if the input isn't
436 * from a tty. Under all circumstances put reasonable things into
437 * the original_termios field, as some routines (seq.c:seq_save()
438 * and term.c:term_init()) want values for special characters.
440 if (F_ISSET(gp, G_ISFROMTTY)) {
441 if (tcgetattr(STDIN_FILENO, &gp->original_termios))
442 err(1, "tcgetattr");
443 } else {
444 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) == -1)
445 err(1, "%s", _PATH_TTY);
446 if (tcgetattr(fd, &gp->original_termios))
447 err(1, "tcgetattr");
448 (void)close(fd);
451 return (gp);
456 * gs_end --
457 * End the GS structure.
459 static void
460 gs_end(gp)
461 GS *gp;
463 MSG *mp;
464 char *tty;
466 /* Reset anything that needs resetting. */
467 if (gp->flags & G_SETMODE) /* O_MESG */
468 if ((tty = ttyname(STDERR_FILENO)) == NULL)
469 warn("ttyname");
470 else if (chmod(tty, gp->origmode) < 0)
471 warn("%s", tty);
473 /* Ring the bell if scheduled. */
474 if (F_ISSET(gp, G_BELLSCHED))
475 (void)fprintf(stderr, "\07"); /* \a */
477 /* Flush any remaining messages. */
478 for (mp = gp->msgq.lh_first;
479 mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
480 (void)fprintf(stderr, "%.*s\n", (int)mp->len, mp->mbuf);
482 FREE(gp->special_key, MAX_FAST_KEY);
485 * DON'T FREE THE GLOBAL STRUCTURE -- WE DIDN'T TURN
486 * OFF SIGNALS/TIMERS, SO IT MAY STILL BE REFERENCED.
491 * h_hup --
492 * Handle SIGHUP.
494 static void
495 h_hup(signo)
496 int signo;
498 F_SET(__global_list, G_SIGHUP);
501 * If we're asleep, just die.
503 * XXX
504 * This isn't right if the windows are independent.
506 if (F_ISSET(__global_list, G_SLEEPING))
507 rcv_hup();
511 * h_term --
512 * Handle SIGTERM.
514 static void
515 h_term(signo)
516 int signo;
518 F_SET(__global_list, G_SIGTERM);
521 * If we're asleep, just die.
523 * XXX
524 * This isn't right if the windows are independent.
526 if (F_ISSET(__global_list, G_SLEEPING))
527 rcv_term();
531 * h_winch --
532 * Handle SIGWINCH.
534 static void
535 h_winch(signo)
536 int signo;
538 F_SET(__global_list, G_SIGWINCH);
541 static void
542 obsolete(argv)
543 char *argv[];
545 size_t len;
546 char *p, *myname;
549 * Translate old style arguments into something getopt will like.
550 * Make sure it's not text space memory, because ex changes the
551 * strings.
552 * Change "+" into "-c$".
553 * Change "+<anything else>" into "-c<anything else>".
554 * Change "-" into "-s"
555 * Change "-r" into "-l"
557 for (myname = argv[0]; *++argv;)
558 if (argv[0][0] == '+') {
559 if (argv[0][1] == '\0') {
560 if ((argv[0] = malloc(4)) == NULL)
561 err(1, NULL);
562 (void)strcpy(argv[0], "-c$");
563 } else {
564 p = argv[0];
565 len = strlen(argv[0]);
566 if ((argv[0] = malloc(len + 2)) == NULL)
567 err(1, NULL);
568 argv[0][0] = '-';
569 argv[0][1] = 'c';
570 (void)strcpy(argv[0] + 2, p + 1);
572 } else if (argv[0][0] == '-') {
573 if (argv[0][1] == 'r') {
574 if (argv[0][2] == '\0' && argv[1] == NULL)
575 argv[0][1] = 'l';
576 } else if (argv[0][1] == '\0') {
577 if ((argv[0] = malloc(3)) == NULL)
578 err(1, NULL);
579 (void)strcpy(argv[0], "-s");
584 static void
585 usage(is_ex)
586 int is_ex;
588 #define EX_USAGE \
589 "usage: ex [-eFlRsv] [-c command] [-r file] [-t tag] [-w size] [-x aw]"
590 #define VI_USAGE \
591 "usage: vi [-eFlRv] [-c command] [-r file] [-t tag] [-w size] [-x aw]"
593 (void)fprintf(stderr, "%s\n", is_ex ? EX_USAGE : VI_USAGE);
594 exit(1);