wordsmithing, print out the full path for local .exrc files that
[nvi.git] / common / main.c
blobef2bb1e7a8f474193aca45682929f9c60025fba6
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.57 1993/12/22 17:57:42 bostic Exp $ (Berkeley) $Date: 1993/12/22 17:57:42 $";
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 int exrc_isok __P((SCR *, char *, int));
42 static void gs_end __P((GS *));
43 static GS *gs_init __P((void));
44 static void h_hup __P((int));
45 static void h_term __P((int));
46 static void h_winch __P((int));
47 static void obsolete __P((char *[]));
48 static void usage __P((int));
50 GS *__global_list; /* GLOBAL: List of screens. */
52 int
53 main(argc, argv)
54 int argc;
55 char *argv[];
57 extern int optind;
58 extern char *optarg;
59 static int reenter; /* STATIC: Re-entrancy check. */
60 struct sigaction act;
61 struct stat sb;
62 GS *gp;
63 FREF *frp;
64 SCR *sp;
65 u_int flags, saved_vi_mode;
66 int ch, eval, flagchk, readonly, silent, snapshot;
67 char *excmdarg, *myname, *p, *rec_f, *tag_f, *trace_f, *wsizearg;
68 char path[MAXPATHLEN];
70 /* Stop if indirecting through a NULL pointer. */
71 if (reenter++)
72 abort();
74 /* Set screen type and mode based on the program name. */
75 readonly = 0;
76 if ((myname = strrchr(*argv, '/')) == NULL)
77 myname = *argv;
78 else
79 ++myname;
80 if (!strcmp(myname, "ex") || !strcmp(myname, "nex"))
81 LF_INIT(S_EX);
82 else {
83 /* View is readonly. */
84 if (!strcmp(myname, "view"))
85 readonly = 1;
86 LF_INIT(S_VI_CURSES);
88 saved_vi_mode = S_VI_CURSES;
90 /* Convert old-style arguments into new-style ones. */
91 obsolete(argv);
93 /* Parse the arguments. */
94 flagchk = '\0';
95 excmdarg = rec_f = tag_f = trace_f = wsizearg = NULL;
96 silent = 0;
97 snapshot = 1;
98 while ((ch = getopt(argc, argv, "c:eFlRr:sT:t:vw:x:")) != EOF)
99 switch (ch) {
100 case 'c': /* Run the command. */
101 excmdarg = optarg;
102 break;
103 case 'e': /* Ex mode. */
104 LF_CLR(S_SCREENS);
105 LF_SET(S_EX);
106 break;
107 case 'F': /* No snapshot. */
108 snapshot = 0;
109 break;
110 case 'l':
111 if (flagchk != '\0' && flagchk != 'l')
112 errx(1,
113 "only one of -%c and -l may be specified.",
114 flagchk);
115 flagchk = 'l';
116 break;
117 case 'R': /* Readonly. */
118 readonly = 1;
119 break;
120 case 'r': /* Recover. */
121 if (flagchk == 'r')
122 errx(1,
123 "only one recovery file may be specified.");
124 if (flagchk != '\0')
125 errx(1,
126 "only one of -%c and -r may be specified.",
127 flagchk);
128 flagchk = 'r';
129 rec_f = optarg;
130 break;
131 case 's':
132 if (!LF_ISSET(S_EX))
133 errx(1, "-s only applicable to ex.");
134 silent = 1;
135 break;
136 case 'T': /* Trace. */
137 trace_f = optarg;
138 break;
139 case 't': /* Tag. */
140 if (flagchk == 't')
141 errx(1,
142 "only one tag file may be specified.");
143 if (flagchk != '\0')
144 errx(1,
145 "only one of -%c and -t may be specified.",
146 flagchk);
147 flagchk = 't';
148 tag_f = optarg;
149 break;
150 case 'v': /* Vi mode. */
151 LF_CLR(S_SCREENS);
152 LF_SET(S_VI_CURSES);
153 break;
154 case 'w':
155 wsizearg = optarg;
156 break;
157 case 'x':
158 if (!strcmp(optarg, "aw")) {
159 LF_CLR(S_SCREENS);
160 LF_SET(S_VI_XAW);
161 saved_vi_mode = S_VI_XAW;
162 break;
164 /* FALLTHROUGH */
165 case '?':
166 default:
167 usage(LF_ISSET(S_EX));
169 argc -= optind;
170 argv += optind;
172 /* Build and initialize the GS structure. */
173 __global_list = gp = gs_init();
175 if (snapshot)
176 F_SET(gp, G_SNAPSHOT);
178 /* Build and initialize the first/current screen. */
179 if (screen_init(NULL, &sp, flags))
180 goto err1;
181 sp->saved_vi_mode = saved_vi_mode;
182 CIRCLEQ_INSERT_HEAD(&__global_list->dq, sp, q);
184 if (trace_f != NULL) {
185 #ifdef DEBUG
186 if ((gp->tracefp = fopen(optarg, "w")) == NULL)
187 err(1, "%s", optarg);
188 (void)fprintf(gp->tracefp, "\n===\ntrace: open %s\n", optarg);
189 #else
190 msgq(sp, M_ERR, "-T support not compiled into this version.");
191 #endif
194 if (set_window_size(sp, 0, 0)) /* Set the window size. */
195 goto err1;
197 if (opts_init(sp)) /* Options initialization. */
198 goto err1;
199 if (readonly)
200 O_SET(sp, O_READONLY);
201 if (silent) {
202 O_CLR(sp, O_AUTOPRINT);
203 O_CLR(sp, O_PROMPT);
204 O_CLR(sp, O_VERBOSE);
205 O_CLR(sp, O_WARN);
206 F_SET(sp, S_EXSILENT);
208 if (wsizearg != NULL) {
209 ARGS *av[2], a, b;
210 if (strtol(optarg, &p, 10) < 0 || *p)
211 errx(1, "illegal window size -- %s", optarg);
212 (void)snprintf(path, sizeof(path), "window=%s", optarg);
213 a.bp = path;
214 a.len = strlen(path);
215 b.bp = NULL;
216 b.len = 0;
217 av[0] = &a;
218 av[1] = &b;
219 if (opts_set(sp, av))
220 msgq(sp, M_ERR,
221 "Unable to set command line window option");
224 /* Keymaps, special keys, must follow option initializations. */
225 if (term_init(sp))
226 goto err1;
228 #ifdef DIGRAPHS
229 if (digraph_init(sp)) /* Digraph initialization. */
230 goto err1;
231 #endif
234 * Source the system, environment, ~user and local .exrc values.
235 * If the environment exists, vi historically doesn't check ~user.
236 * This is done before the file is read in because things in the
237 * .exrc information can set, for example, the recovery directory.
239 if (!silent) {
240 if (exrc_isok(sp, _PATH_SYSEXRC, 1))
241 (void)ex_cfile(sp, NULL, _PATH_SYSEXRC);
243 /* Source the EXINIT environment variable. */
244 if ((p = getenv("EXINIT")) != NULL)
245 if ((p = strdup(p)) == NULL) {
246 msgq(sp, M_SYSERR, NULL);
247 goto err1;
248 } else {
249 (void)ex_icmd(sp, NULL, p, strlen(p));
250 free(p);
252 else if ((p = getenv("HOME")) != NULL && *p) {
253 (void)snprintf(path,
254 sizeof(path), "%s/%s", p, _PATH_NEXRC);
255 if (exrc_isok(sp, path, 0))
256 (void)ex_cfile(sp, NULL, path);
257 else {
258 (void)snprintf(path,
259 sizeof(path), "%s/%s", p, _PATH_EXRC);
260 if (exrc_isok(sp, path, 0))
261 (void)ex_cfile(sp, NULL, path);
265 * !!!
266 * According to O'Reilly ("Learning the VI Editor", Fifth Ed.,
267 * May 1992, page 106), System V release 3.2 and later, has an
268 * option "[no]exrc", causing vi to not "read .exrc files in
269 * the current directory unless you first set the exrc option
270 * in your home directory's .exrc file". Yeah, right. Did
271 * someone actually believe that users would change their home
272 * .exrc file based on whether or not they wanted to source the
273 * current local .exrc? Or that users would want ALL the local
274 * .exrc files on some systems, and none of them on others?
275 * I think not.
277 * Apply the same tests to local .exrc files that are applied
278 * to any other .exrc file.
280 if (exrc_isok(sp, _PATH_EXRC, 0))
281 (void)ex_cfile(sp, NULL, _PATH_EXRC);
284 /* List recovery files if -l specified. */
285 if (flagchk == 'l')
286 exit(rcv_list(sp));
288 /* Use a tag file or recovery file if specified. */
289 if (tag_f != NULL && ex_tagfirst(sp, tag_f))
290 goto err1;
291 else if (rec_f != NULL && rcv_read(sp, rec_f))
292 goto err1;
294 /* Append any remaining arguments as file names. */
295 if (*argv != NULL)
296 for (; *argv != NULL; ++argv)
297 if (file_add(sp, NULL, *argv, 0) == NULL)
298 goto err1;
301 * If no recovery or tag file, get an EXF structure.
302 * If no argv file, use a temporary file.
304 if (tag_f == NULL && rec_f == NULL) {
305 if ((frp = file_first(sp)) == NULL &&
306 (frp = file_add(sp, NULL, NULL, 1)) == NULL)
307 goto err1;
308 if (file_init(sp, frp, NULL, 0))
309 goto err1;
312 /* Set up the argument pointer. */
313 sp->a_frp = sp->frp;
316 * Initialize the signals. Use sigaction(2), not signal(3), because
317 * we don't want to always restart system calls on 4BSD systems. It
318 * would be nice in some cases to restart system calls, but SA_RESTART
319 * is a 4BSD extension so we can't use it.
321 * SIGWINCH, SIGHUP, SIGTERM:
322 * Catch and set a global bit.
324 act.sa_handler = h_hup;
325 sigemptyset(&act.sa_mask);
326 act.sa_flags = 0;
327 (void)sigaction(SIGHUP, &act, NULL);
328 act.sa_handler = h_term;
329 sigemptyset(&act.sa_mask);
330 act.sa_flags = 0;
331 (void)sigaction(SIGTERM, &act, NULL);
332 act.sa_handler = h_winch;
333 sigemptyset(&act.sa_mask);
334 act.sa_flags = 0;
335 (void)sigaction(SIGWINCH, &act, NULL);
338 * SIGQUIT:
339 * Always ignore.
341 act.sa_handler = SIG_IGN;
342 sigemptyset(&act.sa_mask);
343 act.sa_flags = 0;
344 (void)sigaction(SIGQUIT, &act, NULL);
347 * If there's an initial command, push it on the command stack.
348 * Historically, it was always an ex command, not vi in vi mode
349 * or ex in ex mode. So, make it look like an ex command to vi.
351 if (excmdarg != NULL)
352 if (IN_EX_MODE(sp)) {
353 if (term_push(sp, excmdarg, strlen(excmdarg), 0, 0))
354 goto err1;
355 } else if (IN_VI_MODE(sp)) {
356 if (term_push(sp, "\n", 1, 0, 0))
357 goto err1;
358 if (term_push(sp, excmdarg, strlen(excmdarg), 0, 0))
359 goto err1;
360 if (term_push(sp, ":", 1, 0, 0))
361 goto err1;
364 /* Vi reads from the terminal. */
365 if (!F_ISSET(gp, G_ISFROMTTY) && !F_ISSET(sp, S_EX)) {
366 msgq(sp, M_ERR, "Vi's standard input must be a terminal.");
367 goto err1;
370 for (;;) {
371 if (sp->s_edit(sp, sp->ep))
372 goto err2;
375 * Edit the next screen on the display queue, or, move
376 * a screen from the hidden queue to the display queue.
378 if ((sp = __global_list->dq.cqh_first) ==
379 (void *)&__global_list->dq)
380 if ((sp = __global_list->hq.cqh_first) !=
381 (void *)&__global_list->hq) {
382 CIRCLEQ_REMOVE(&sp->gp->hq, sp, q);
383 CIRCLEQ_INSERT_TAIL(&sp->gp->dq, sp, q);
384 } else
385 break;
388 * The screen type may have changed -- reinitialize the
389 * functions in case it has.
391 switch (F_ISSET(sp, S_SCREENS)) {
392 case S_EX:
393 if (sex_screen_init(sp))
394 goto err2;
395 break;
396 case S_VI_CURSES:
397 if (svi_screen_init(sp))
398 goto err2;
399 break;
400 case S_VI_XAW:
401 if (xaw_screen_init(sp))
402 goto err2;
403 break;
404 default:
405 abort();
410 * Two error paths. The first means that something failed before
411 * we called a screen routine. Swap the message pointers between
412 * the SCR and the GS, so messages get displayed. The second is
413 * something failed in a screen. NOTE: sp may be GONE when the
414 * screen returns, so only the gp can be trusted.
416 eval = 0;
417 if (0) {
418 err1: gp->msgq.lh_first = sp->msgq.lh_first;
419 err2: eval = 1;
422 gs_end(gp);
424 /* Make absolutely sure that the modes are restored correctly. */
425 if (F_ISSET(gp, G_ISFROMTTY) &&
426 tcsetattr(STDIN_FILENO, TCSADRAIN, &gp->original_termios))
427 err(1, "tcsetattr");
428 exit(eval);
432 * gs_init --
433 * Build and initialize the GS structure.
435 static GS *
436 gs_init()
438 GS *gp;
439 int fd;
441 CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
442 if (gp == NULL)
443 err(1, NULL);
445 CIRCLEQ_INIT(&gp->dq);
446 CIRCLEQ_INIT(&gp->hq);
447 LIST_INIT(&gp->msgq);
449 /* Structures shared by screens so stored in the GS structure. */
450 CALLOC_NOMSG(NULL, gp->tty, IBUF *, 1, sizeof(IBUF));
451 if (gp->tty == NULL)
452 err(1, NULL);
454 LIST_INIT(&gp->cutq);
455 LIST_INIT(&gp->seqq);
457 /* Set a flag if we're reading from the tty. */
458 if (isatty(STDIN_FILENO))
459 F_SET(gp, G_ISFROMTTY);
462 * XXX
463 * Set a flag and don't do terminal sets/resets if the input isn't
464 * from a tty. Under all circumstances put reasonable things into
465 * the original_termios field, as some routines (seq.c:seq_save()
466 * and term.c:term_init()) want values for special characters.
468 if (F_ISSET(gp, G_ISFROMTTY)) {
469 if (tcgetattr(STDIN_FILENO, &gp->original_termios))
470 err(1, "tcgetattr");
471 } else {
472 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) == -1)
473 err(1, "%s", _PATH_TTY);
474 if (tcgetattr(fd, &gp->original_termios))
475 err(1, "tcgetattr");
476 (void)close(fd);
479 return (gp);
484 * gs_end --
485 * End the GS structure.
487 static void
488 gs_end(gp)
489 GS *gp;
491 MSG *mp;
492 SCR *sp;
493 char *tty;
495 /* Reset anything that needs resetting. */
496 if (gp->flags & G_SETMODE) /* O_MESG */
497 if ((tty = ttyname(STDERR_FILENO)) == NULL)
498 warn("ttyname");
499 else if (chmod(tty, gp->origmode) < 0)
500 warn("%s", tty);
502 /* Ring the bell if scheduled. */
503 if (F_ISSET(gp, G_BELLSCHED))
504 (void)fprintf(stderr, "\07"); /* \a */
506 /* If there are any remaining screens, flush their messages. */
507 for (sp = __global_list->dq.cqh_first;
508 sp != (void *)&__global_list->dq; sp = sp->q.cqe_next)
509 for (mp = sp->msgq.lh_first;
510 mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
511 (void)fprintf(stderr, "%.*s\n", (int)mp->len, mp->mbuf);
512 for (sp = __global_list->hq.cqh_first;
513 sp != (void *)&__global_list->hq; sp = sp->q.cqe_next)
514 for (mp = sp->msgq.lh_first;
515 mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
516 (void)fprintf(stderr, "%.*s\n", (int)mp->len, mp->mbuf);
517 /* Flush messages on the global queue. */
518 for (mp = gp->msgq.lh_first;
519 mp != NULL && !(F_ISSET(mp, M_EMPTY)); mp = mp->q.le_next)
520 (void)fprintf(stderr, "%.*s\n", (int)mp->len, mp->mbuf);
522 FREE(gp->special_key, MAX_FAST_KEY);
525 * DON'T FREE THE GLOBAL STRUCTURE -- WE DIDN'T TURN
526 * OFF SIGNALS/TIMERS, SO IT MAY STILL BE REFERENCED.
531 * h_hup --
532 * Handle SIGHUP.
534 static void
535 h_hup(signo)
536 int signo;
538 F_SET(__global_list, G_SIGHUP);
541 * If we're asleep, just die.
543 * XXX
544 * This isn't right if the windows are independent.
546 if (F_ISSET(__global_list, G_SLEEPING))
547 rcv_hup();
551 * h_term --
552 * Handle SIGTERM.
554 static void
555 h_term(signo)
556 int signo;
558 F_SET(__global_list, G_SIGTERM);
561 * If we're asleep, just die.
563 * XXX
564 * This isn't right if the windows are independent.
566 if (F_ISSET(__global_list, G_SLEEPING))
567 rcv_term();
571 * h_winch --
572 * Handle SIGWINCH.
574 static void
575 h_winch(signo)
576 int signo;
578 F_SET(__global_list, G_SIGWINCH);
582 * exrc_isok --
583 * Check a .exrc for source-ability.
585 static int
586 exrc_isok(sp, path, rootok)
587 SCR *sp;
588 char *path;
589 int rootok;
591 struct stat sb;
592 uid_t uid;
593 char *emsg, buf[MAXPATHLEN];
595 /* Check for the file's existence. */
596 if (stat(path, &sb))
597 return (0);
600 * !!!
601 * Historically, vi did not read the .exrc files if they were owned
602 * by someone other than the user, unless the undocumented option
603 * sourceany was set. We don't support the sourceany option. We
604 * check that the user (or root, for system files) owns the file and
605 * require that it not be writeable by anyone other than the owner.
608 /* Owned by the user or root. */
609 uid = getuid();
610 if (rootok) {
611 if (sb.st_uid != uid && sb.st_uid != 0) {
612 emsg = "not owned by you or root";
613 goto err;
615 } else
616 if (sb.st_uid != uid) {
617 emsg = "not owned by you";
618 goto err;
621 /* Not writeable by anyone but the owner. */
622 if (sb.st_mode & (S_IWGRP | S_IWOTH)) {
623 emsg = "writeable by a user other than the owner";
624 err: if (strchr(path, '/') == NULL &&
625 getcwd(buf, sizeof(buf)) != NULL)
626 msgq(sp, M_ERR,
627 "%s/%s: not sourced: %s.", buf, path, emsg);
628 else
629 msgq(sp, M_ERR,
630 "%s: not sourced: %s.", path, emsg);
631 return (0);
633 return (1);
636 static void
637 obsolete(argv)
638 char *argv[];
640 size_t len;
641 char *p, *myname;
644 * Translate old style arguments into something getopt will like.
645 * Make sure it's not text space memory, because ex changes the
646 * strings.
647 * Change "+" into "-c$".
648 * Change "+<anything else>" into "-c<anything else>".
649 * Change "-" into "-s"
650 * Change "-r" into "-l"
652 for (myname = argv[0]; *++argv;)
653 if (argv[0][0] == '+') {
654 if (argv[0][1] == '\0') {
655 MALLOC_NOMSG(NULL, argv[0], char *, 4);
656 if (argv[0] == NULL)
657 err(1, NULL);
658 (void)strcpy(argv[0], "-c$");
659 } else {
660 p = argv[0];
661 len = strlen(argv[0]);
662 MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
663 if (argv[0] == NULL)
664 err(1, NULL);
665 argv[0][0] = '-';
666 argv[0][1] = 'c';
667 (void)strcpy(argv[0] + 2, p + 1);
669 } else if (argv[0][0] == '-') {
670 if (argv[0][1] == 'r') {
671 if (argv[0][2] == '\0' && argv[1] == NULL)
672 argv[0][1] = 'l';
673 } else if (argv[0][1] == '\0') {
674 MALLOC_NOMSG(NULL, argv[0], char *, 3);
675 if (argv[0] == NULL)
676 err(1, NULL);
677 (void)strcpy(argv[0], "-s");
682 static void
683 usage(is_ex)
684 int is_ex;
686 #define EX_USAGE \
687 "usage: ex [-eFlRsv] [-c command] [-r file] [-t tag] [-w size] [-x aw]"
688 #define VI_USAGE \
689 "usage: vi [-eFlRv] [-c command] [-r file] [-t tag] [-w size] [-x aw]"
691 (void)fprintf(stderr, "%s\n", is_ex ? EX_USAGE : VI_USAGE);
692 exit(1);