2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char copyright[] =
14 "@(#) Copyright (c) 1992, 1993, 1994\n\
15 The Regents of the University of California. All rights reserved.\n\
16 @(#) Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
17 Keith Bostic. All rights reserved.\n";
21 static const char sccsid[] = "@(#)main.c 10.48 (Berkeley) 10/11/96";
24 #include <sys/types.h>
25 #include <sys/queue.h>
29 #include <bitstring.h>
40 #include "pathnames.h"
42 static void attach __P((GS *));
43 static void v_estr __P((char *, int, char *));
44 static int v_obsolete __P((char *, char *[]));
48 * Main editor routine.
50 * PUBLIC: int editor __P((GS *, int, char *[]));
53 editor(gp, argc, argv)
66 int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
70 char *tag_f, *wsizearg, path[256];
72 /* Initialize the busy routine, if not defined by the screen. */
73 if (gp->scr_busy == NULL)
74 gp->scr_busy = vs_busy;
75 /* Initialize the message routine, if not defined by the screen. */
76 if (gp->scr_msg == NULL)
79 /* Common global structure initialization. */
80 CIRCLEQ_INIT(&gp->dq);
81 CIRCLEQ_INIT(&gp->hq);
83 LIST_INSERT_HEAD(&gp->ecq, &gp->excmd, q);
84 gp->noprint = DEFAULT_NOPRINT;
86 /* Structures shared by screens so stored in the GS structure. */
87 CIRCLEQ_INIT(&gp->frefq);
88 CIRCLEQ_INIT(&gp->dcb_store.textq);
92 /* Set initial screen type and mode based on the program name. */
94 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
97 /* Nview, view are readonly. */
98 if (!strcmp(gp->progname, "nview") ||
99 !strcmp(gp->progname, "view"))
102 /* Vi is the default. */
106 /* Convert old-style arguments into new-style ones. */
107 if (v_obsolete(gp->progname, argv))
110 /* Parse the arguments. */
112 tag_f = wsizearg = NULL;
113 lflag = secure = silent = 0;
116 /* Set the file snapshot flag. */
117 F_SET(gp, G_SNAPSHOT);
121 while ((ch = getopt(argc, argv, "c:D:eFGlRrSsT:t:vw:")) != EOF)
123 while ((ch = getopt(argc, argv, "c:eFGlRrSst:vw:")) != EOF)
127 while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != EOF)
129 while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != EOF)
133 case 'c': /* Run the command. */
136 * We should support multiple -c options.
138 if (gp->c_option != NULL) {
139 v_estr(gp->progname, 0,
140 "only one -c command may be specified.");
143 gp->c_option = optarg;
155 v_estr(gp->progname, 0,
156 "usage: -D requires s or w argument.");
161 case 'e': /* Ex mode. */
165 case 'F': /* No snapshot. */
166 F_CLR(gp, G_SNAPSHOT);
169 case 'G': /* gtags mode. */
173 case 'l': /* Set lisp, showmatch options. */
176 case 'R': /* Readonly. */
179 case 'r': /* Recover. */
180 if (flagchk == 't') {
181 v_estr(gp->progname, 0,
182 "only one of -r and -t may be specified.");
194 case 'T': /* Trace. */
195 if ((gp->tracefp = fopen(optarg, "w")) == NULL) {
196 v_estr(gp->progname, errno, optarg);
199 (void)fprintf(gp->tracefp,
200 "\n===\ntrace: open %s\n", optarg);
204 if (flagchk == 'r') {
205 v_estr(gp->progname, 0,
206 "only one of -r and -t may be specified.");
209 if (flagchk == 't') {
210 v_estr(gp->progname, 0,
211 "only one tag file may be specified.");
217 case 'v': /* Vi mode. */
226 (void)gp->scr_usage();
233 * -s option is only meaningful to ex.
235 * If not reading from a terminal, it's like -s was specified.
237 if (silent && !LF_ISSET(SC_EX)) {
238 v_estr(gp->progname, 0, "-s option is only applicable to ex.");
241 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
245 * Build and initialize the first/current screen. This is a bit
246 * tricky. If an error is returned, we may or may not have a
247 * screen structure. If we have a screen structure, put it on a
248 * display queue so that the error messages get displayed.
251 * Everything we do until we go interactive is done in ex mode.
253 if (screen_init(gp, NULL, &sp)) {
255 CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
259 CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
261 if (v_key_init(sp)) /* Special key initialization. */
264 { int oargs[5], *oargp = oargs;
265 if (lflag) { /* Command-line options. */
267 *oargp++ = O_SHOWMATCH;
270 *oargp++ = O_READONLY;
273 *oargp++ = O_GTAGSMODE;
277 *oargp = -1; /* Options initialization. */
278 if (opts_init(sp, oargs))
281 if (wsizearg != NULL) {
283 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
284 a.bp = (CHAR_T *)path;
285 a.len = strlen(path);
290 (void)opts_set(sp, av, NULL);
292 if (silent) { /* Ex batch mode option values. */
293 O_CLR(sp, O_AUTOPRINT);
295 O_CLR(sp, O_VERBOSE);
297 F_SET(sp, SC_EX_SILENT);
300 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
301 sp->cols = O_VAL(sp, O_COLUMNS);
303 if (!silent && startup) { /* Read EXINIT, exrc files. */
306 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
314 * List recovery files if -r specified without file arguments.
315 * Note, options must be initialized and startup information
316 * read before doing this.
318 if (flagchk == 'r' && argv[0] == NULL) {
328 * Initialize the default ^D, ^U scrolling value here, after the
329 * user has had every opportunity to set the window option.
331 * It's historic practice that changing the value of the window
332 * option did not alter the default scrolling value, only giving
333 * a count to ^D/^U did that.
335 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
338 * If we don't have a command-line option, switch into the right
339 * editor now, so that we position default files correctly, and
340 * so that any tags file file-already-locked messages are in the
341 * vi screen, not the ex screen.
344 * If we have a command-line option, the error message can end
345 * up in the wrong place, but I think that the combination is
348 if (gp->c_option == NULL) {
349 F_CLR(sp, SC_EX | SC_VI);
350 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
353 /* Open a tag file if specified. */
354 if (tag_f != NULL && ex_tag_first(sp, tag_f))
358 * Append any remaining arguments as file names. Files are recovery
359 * files if -r specified. If the tag option or ex startup commands
360 * loaded a file, then any file arguments are going to come after it.
363 if (sp->frp != NULL) {
364 /* Cheat -- we know we have an extra argv slot. */
366 *--argv, char *, strlen(sp->frp->name) + 1);
368 v_estr(gp->progname, errno, NULL);
371 (void)strcpy(*argv, sp->frp->name);
373 sp->argv = sp->cargv = argv;
374 F_SET(sp, SC_ARGNOFREE);
376 F_SET(sp, SC_ARGRECOVER);
380 * If the ex startup commands and or/the tag option haven't already
381 * created a file, create one. If no command-line files were given,
382 * use a temporary file.
384 if (sp->frp == NULL) {
385 if (sp->argv == NULL) {
386 if ((frp = file_add(sp, NULL)) == NULL)
389 if ((frp = file_add(sp, (CHAR_T *)sp->argv[0])) == NULL)
391 if (F_ISSET(sp, SC_ARGRECOVER))
392 F_SET(frp, FR_RECOVER);
395 if (file_init(sp, frp, NULL, 0))
397 if (EXCMD_RUNNING(gp)) {
399 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
408 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
409 * was forced to initialize the screen during startup. We'd like to
410 * wait for a single character from the user, but we can't because
411 * we're not in raw mode. We can't switch to raw mode because the
412 * vi initialization will switch to xterm's alternate screen, causing
413 * us to lose the messages we're pausing to make sure the user read.
414 * So, wait for a complete line.
416 if (F_ISSET(sp, SC_SCR_EX)) {
417 p = msg_cmsg(sp, CMSG_CONT_R, &len);
418 (void)write(STDOUT_FILENO, p, len);
420 if (v_event_get(sp, &ev, 0, 0))
422 if (ev.e_event == E_INTERRUPT ||
423 ev.e_event == E_CHARACTER &&
424 (ev.e_value == K_CR || ev.e_value == K_NL))
426 (void)gp->scr_bell(sp);
430 /* Switch into the right editor, regardless. */
431 F_CLR(sp, SC_EX | SC_VI);
432 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
435 * Main edit loop. Vi handles split screens itself, we only return
436 * here when switching editor modes or restarting the screen.
439 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
446 /* Clean out the global structure. */
454 * End the program, discarding screens and most of the global area.
456 * PUBLIC: void v_end __P((GS *));
465 /* If there are any remaining screens, kill them off. */
466 if (gp->ccl_sp != NULL) {
467 (void)file_end(gp->ccl_sp, NULL, 1);
468 (void)screen_end(gp->ccl_sp);
470 while ((sp = gp->dq.cqh_first) != (void *)&gp->dq)
471 (void)screen_end(sp);
472 while ((sp = gp->hq.cqh_first) != (void *)&gp->hq)
473 (void)screen_end(sp);
475 #ifdef HAVE_PERL_INTERP
479 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
482 while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) {
483 CIRCLEQ_REMOVE(&gp->frefq, frp, q);
484 if (frp->name != NULL)
486 if (frp->tname != NULL)
492 /* Free key input queue. */
493 if (gp->i_event != NULL)
496 /* Free cut buffers. */
499 /* Free map sequences. */
502 /* Free default buffer storage. */
503 (void)text_lfree(&gp->dcb_store.textq);
505 /* Close message catalogs. */
509 /* Ring the bell if scheduled. */
510 if (F_ISSET(gp, G_BELLSCHED))
511 (void)fprintf(stderr, "\07"); /* \a */
514 * Flush any remaining messages. If a message is here, it's almost
515 * certainly the message about the event that killed us (although
516 * it's possible that the user is sourcing a file that exits from the
519 while ((mp = gp->msgq.lh_first) != NULL) {
520 (void)fprintf(stderr, "%s%.*s",
521 mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
523 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
529 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
530 /* Free any temporary space. */
531 if (gp->tmp_bp != NULL)
535 /* Close debugging file descriptor. */
536 if (gp->tracefp != NULL)
537 (void)fclose(gp->tracefp);
544 * Convert historic arguments into something getopt(3) will like.
547 v_obsolete(name, argv)
554 * Translate old style arguments into something getopt will like.
555 * Make sure it's not text space memory, because ex modifies the
557 * Change "+" into "-c$".
558 * Change "+<anything else>" into "-c<anything else>".
559 * Change "-" into "-s"
560 * The c, T, t and w options take arguments so they can't be
563 * Stop if we find "--" as an argument, the user may want to edit
564 * a file named "+foo".
566 while (*++argv && strcmp(argv[0], "--"))
567 if (argv[0][0] == '+') {
568 if (argv[0][1] == '\0') {
569 MALLOC_NOMSG(NULL, argv[0], char *, 4);
572 (void)strcpy(argv[0], "-c$");
575 len = strlen(argv[0]);
576 MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
581 (void)strcpy(argv[0] + 2, p + 1);
583 } else if (argv[0][0] == '-')
584 if (argv[0][1] == '\0') {
585 MALLOC_NOMSG(NULL, argv[0], char *, 3);
586 if (argv[0] == NULL) {
587 nomem: v_estr(name, errno, NULL);
590 (void)strcpy(argv[0], "-s");
592 if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
593 argv[0][1] == 't' || argv[0][1] == 'w') &&
607 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
608 v_estr(gp->progname, errno, _PATH_TTY);
612 (void)printf("process %lu waiting, enter <CR> to continue: ",
614 (void)fflush(stdout);
617 if (read(fd, &ch, 1) != 1) {
621 } while (ch != '\n' && ch != '\r');
627 v_estr(name, eno, msg)
631 (void)fprintf(stderr, "%s", name);
633 (void)fprintf(stderr, ": %s", msg);
635 (void)fprintf(stderr, ": %s", strerror(errno));
636 (void)fprintf(stderr, "\n");