2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char copyright
[] =
10 "%Z% Copyright (c) 1992, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
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 $";
18 #include <sys/param.h>
38 #include "pathnames.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. */
58 static int reenter
; /* STATIC: Re-entrancy check. */
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. */
73 /* Set screen type and mode based on the program name. */
75 if ((myname
= strrchr(*argv
, '/')) == NULL
)
79 if (!strcmp(myname
, "ex") || !strcmp(myname
, "nex"))
82 /* View is readonly. */
83 if (!strcmp(myname
, "view"))
88 /* Convert old-style arguments into new-style ones. */
91 /* Parse the arguments. */
93 excmdarg
= rec_f
= tag_f
= trace_f
= wsizearg
= NULL
;
96 while ((ch
= getopt(argc
, argv
, "c:eFlRr:sT:t:vw:x:")) != EOF
)
98 case 'c': /* Run the command. */
101 case 'e': /* Ex mode. */
105 case 'F': /* No snapshot. */
109 if (flagchk
!= '\0' && flagchk
!= 'l')
111 "only one of -%c and -l may be specified.",
115 case 'R': /* Readonly. */
118 case 'r': /* Recover. */
121 "only one recovery file may be specified.");
124 "only one of -%c and -r may be specified.",
131 errx(1, "-s only applicable to ex.");
134 case 'T': /* Trace. */
140 "only one tag file may be specified.");
143 "only one of -%c and -t may be specified.",
148 case 'v': /* Vi mode. */
156 if (!strcmp(optarg
, "aw")) {
157 F_CLR(sp
, S_SCREENS
);
164 usage(LF_ISSET(S_EX
));
169 /* Build and initialize the GS structure. */
170 __global_list
= gp
= gs_init();
173 F_SET(gp
, G_SNAPSHOT
);
175 /* Build and initialize the first/current screen. */
176 if (screen_init(NULL
, &sp
, flags
))
178 CIRCLEQ_INSERT_HEAD(&__global_list
->dq
, sp
, q
);
180 if (trace_f
!= NULL
) {
182 if ((gp
->tracefp
= fopen(optarg
, "w")) == NULL
)
183 err(1, "%s", optarg
);
184 (void)fprintf(gp
->tracefp
, "\n===\ntrace: open %s\n", optarg
);
186 msgq(sp
, M_ERR
, "-T support not compiled into this version.");
190 if (set_window_size(sp
, 0, 0)) /* Set the window size. */
193 if (opts_init(sp
)) /* Options initialization. */
196 O_SET(sp
, O_READONLY
);
198 O_CLR(sp
, O_AUTOPRINT
);
200 O_CLR(sp
, O_VERBOSE
);
202 F_SET(sp
, S_EXSILENT
);
204 if (wsizearg
!= 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
))
214 "Unable to set command line window option");
217 /* Keymaps, special keys, must follow option initializations. */
222 if (digraph_init(sp
)) /* Digraph initialization. */
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.
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
);
242 (void)ex_icmd(sp
, NULL
, p
, strlen(p
));
245 else if ((p
= getenv("HOME")) != NULL
&& *p
) {
247 sizeof(path
), "%s/%s", p
, _PATH_NEXRC
);
248 if (!stat(path
, &sb
))
249 (void)ex_cfile(sp
, NULL
, 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. */
265 /* Use a tag file or recovery file if specified. */
266 if (tag_f
!= NULL
&& ex_tagfirst(sp
, tag_f
))
268 else if (rec_f
!= NULL
&& rcv_read(sp
, rec_f
))
271 /* Append any remaining arguments as file names. */
273 for (; *argv
!= NULL
; ++argv
)
274 if (file_add(sp
, NULL
, *argv
, 0) == NULL
)
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
)
285 if (file_init(sp
, frp
, NULL
, 0))
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
);
301 (void)sigaction(SIGHUP
, &act
, NULL
);
302 act
.sa_handler
= h_term
;
303 sigemptyset(&act
.sa_mask
);
305 (void)sigaction(SIGTERM
, &act
, NULL
);
306 act
.sa_handler
= h_winch
;
307 sigemptyset(&act
.sa_mask
);
309 (void)sigaction(SIGWINCH
, &act
, NULL
);
315 act
.sa_handler
= SIG_IGN
;
316 sigemptyset(&act
.sa_mask
);
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))
329 } else if (IN_VI_MODE(sp
)) {
330 if (term_push(sp
, "\n", 1, 0, 0))
332 if (term_push(sp
, excmdarg
, strlen(excmdarg
), 0, 0))
334 if (term_push(sp
, ":", 1, 0, 0))
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.");
345 if (sp
->s_edit(sp
, sp
->ep
))
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
);
362 * The screen type may have changed -- reinitialize the
363 * functions in case it has.
365 switch (F_ISSET(sp
, S_SCREENS
)) {
367 if (sex_screen_init(sp
))
371 if (svi_screen_init(sp
))
375 if (xaw_screen_init(sp
))
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.
392 err1
: gp
->msgq
.lh_first
= sp
->msgq
.lh_first
;
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
))
407 * Build and initialize the GS structure.
415 if ((gp
= calloc(1, sizeof(GS
))) == 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
)
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
);
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
))
444 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) == -1)
445 err(1, "%s", _PATH_TTY
);
446 if (tcgetattr(fd
, &gp
->original_termios
))
457 * End the GS structure.
466 /* Reset anything that needs resetting. */
467 if (gp
->flags
& G_SETMODE
) /* O_MESG */
468 if ((tty
= ttyname(STDERR_FILENO
)) == NULL
)
470 else if (chmod(tty
, gp
->origmode
) < 0)
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.
498 F_SET(__global_list
, G_SIGHUP
);
501 * If we're asleep, just die.
504 * This isn't right if the windows are independent.
506 if (F_ISSET(__global_list
, G_SLEEPING
))
518 F_SET(__global_list
, G_SIGTERM
);
521 * If we're asleep, just die.
524 * This isn't right if the windows are independent.
526 if (F_ISSET(__global_list
, G_SLEEPING
))
538 F_SET(__global_list
, G_SIGWINCH
);
549 * Translate old style arguments into something getopt will like.
550 * Make sure it's not text space memory, because ex changes the
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
)
562 (void)strcpy(argv
[0], "-c$");
565 len
= strlen(argv
[0]);
566 if ((argv
[0] = malloc(len
+ 2)) == NULL
)
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
)
576 } else if (argv
[0][1] == '\0') {
577 if ((argv
[0] = malloc(3)) == NULL
)
579 (void)strcpy(argv
[0], "-s");
589 "usage: ex [-eFlRsv] [-c command] [-r file] [-t tag] [-w size] [-x aw]"
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
);