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[] = "$Id: main.c,v 11.0 2012/10/17 06:34:37 zy Exp $";
24 #include <sys/types.h>
25 #include <sys/queue.h>
28 #include <bitstring.h>
39 #include "pathnames.h"
41 static void attach __P((GS *));
42 static void v_estr __P((char *, int, char *));
43 static int v_obsolete __P((char *, char *[]));
47 * Main editor routine.
49 * PUBLIC: int editor __P((GS *, int, char *[]));
65 int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
66 char *tag_f, *wsizearg, path[256];
70 /* Initialize the busy routine, if not defined by the screen. */
71 if (gp->scr_busy == NULL)
72 gp->scr_busy = vs_busy;
73 /* Initialize the message routine, if not defined by the screen. */
74 if (gp->scr_msg == NULL)
76 gp->catd = (nl_catd)-1;
78 /* Common global structure initialization. */
82 SLIST_INSERT_HEAD(gp->ecq, &gp->excmd, q);
83 gp->noprint = DEFAULT_NOPRINT;
85 /* Structures shared by screens so stored in the GS structure. */
86 TAILQ_INIT(gp->frefq);
87 TAILQ_INIT(gp->dcb_store.textq);
91 /* Set initial screen type and mode based on the program name. */
93 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
96 /* Nview, view are readonly. */
97 if (!strcmp(gp->progname, "nview") ||
98 !strcmp(gp->progname, "view"))
101 /* Vi is the default. */
105 /* Convert old-style arguments into new-style ones. */
106 if (v_obsolete(gp->progname, argv))
109 /* Parse the arguments. */
111 tag_f = wsizearg = NULL;
112 lflag = secure = silent = 0;
115 /* Set the file snapshot flag. */
116 F_SET(gp, G_SNAPSHOT);
119 while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != EOF)
121 while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != EOF)
124 case 'c': /* Run the command. */
127 * We should support multiple -c options.
129 if (gp->c_option != NULL) {
130 v_estr(gp->progname, 0,
131 "only one -c command may be specified.");
134 gp->c_option = optarg;
146 v_estr(gp->progname, 0,
147 "usage: -D requires s or w argument.");
152 case 'e': /* Ex mode. */
156 case 'F': /* No snapshot. */
157 F_CLR(gp, G_SNAPSHOT);
159 case 'l': /* Set lisp, showmatch options. */
162 case 'R': /* Readonly. */
165 case 'r': /* Recover. */
166 if (flagchk == 't') {
167 v_estr(gp->progname, 0,
168 "only one of -r and -t may be specified.");
180 case 'T': /* Trace. */
181 if ((gp->tracefp = fopen(optarg, "w")) == NULL) {
182 v_estr(gp->progname, errno, optarg);
185 (void)fprintf(gp->tracefp,
186 "\n===\ntrace: open %s\n", optarg);
190 if (flagchk == 'r') {
191 v_estr(gp->progname, 0,
192 "only one of -r and -t may be specified.");
195 if (flagchk == 't') {
196 v_estr(gp->progname, 0,
197 "only one tag file may be specified.");
203 case 'v': /* Vi mode. */
212 (void)gp->scr_usage();
219 * -s option is only meaningful to ex.
221 * If not reading from a terminal, it's like -s was specified.
223 if (silent && !LF_ISSET(SC_EX)) {
224 v_estr(gp->progname, 0, "-s option is only applicable to ex.");
227 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
231 * Build and initialize the first/current screen. This is a bit
232 * tricky. If an error is returned, we may or may not have a
233 * screen structure. If we have a screen structure, put it on a
234 * display queue so that the error messages get displayed.
237 * Everything we do until we go interactive is done in ex mode.
239 if (screen_init(gp, NULL, &sp)) {
241 TAILQ_INSERT_HEAD(gp->dq, sp, q);
245 TAILQ_INSERT_HEAD(gp->dq, sp, q);
247 if (v_key_init(sp)) /* Special key initialization. */
250 { int oargs[5], *oargp = oargs;
251 if (lflag) { /* Command-line options. */
253 *oargp++ = O_SHOWMATCH;
256 *oargp++ = O_READONLY;
259 *oargp = -1; /* Options initialization. */
260 if (opts_init(sp, oargs))
263 if (wsizearg != NULL) {
265 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
266 a.bp = (CHAR_T *)path;
267 a.len = strlen(path);
272 (void)opts_set(sp, av, NULL);
274 if (silent) { /* Ex batch mode option values. */
275 O_CLR(sp, O_AUTOPRINT);
277 O_CLR(sp, O_VERBOSE);
279 F_SET(sp, SC_EX_SILENT);
282 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
283 sp->cols = O_VAL(sp, O_COLUMNS);
285 if (!silent && startup) { /* Read EXINIT, exrc files. */
288 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
296 * List recovery files if -r specified without file arguments.
297 * Note, options must be initialized and startup information
298 * read before doing this.
300 if (flagchk == 'r' && argv[0] == NULL) {
310 * Initialize the default ^D, ^U scrolling value here, after the
311 * user has had every opportunity to set the window option.
313 * It's historic practice that changing the value of the window
314 * option did not alter the default scrolling value, only giving
315 * a count to ^D/^U did that.
317 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
320 * If we don't have a command-line option, switch into the right
321 * editor now, so that we position default files correctly, and
322 * so that any tags file file-already-locked messages are in the
323 * vi screen, not the ex screen.
326 * If we have a command-line option, the error message can end
327 * up in the wrong place, but I think that the combination is
330 if (gp->c_option == NULL) {
331 F_CLR(sp, SC_EX | SC_VI);
332 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
335 /* Open a tag file if specified. */
337 CHAR2INT(sp, tag_f, strlen(tag_f) + 1, w, wlen);
338 if (ex_tag_first(sp, w))
343 * Append any remaining arguments as file names. Files are recovery
344 * files if -r specified. If the tag option or ex startup commands
345 * loaded a file, then any file arguments are going to come after it.
348 if (sp->frp != NULL) {
349 /* Cheat -- we know we have an extra argv slot. */
350 *--argv = strdup(sp->frp->name);
352 v_estr(gp->progname, errno, NULL);
356 sp->argv = sp->cargv = argv;
357 F_SET(sp, SC_ARGNOFREE);
359 F_SET(sp, SC_ARGRECOVER);
363 * If the ex startup commands and or/the tag option haven't already
364 * created a file, create one. If no command-line files were given,
365 * use a temporary file.
367 if (sp->frp == NULL) {
368 if (sp->argv == NULL) {
369 if ((frp = file_add(sp, NULL)) == NULL)
372 if ((frp = file_add(sp, sp->argv[0])) == NULL)
374 if (F_ISSET(sp, SC_ARGRECOVER))
375 F_SET(frp, FR_RECOVER);
378 if (file_init(sp, frp, NULL, 0))
380 if (EXCMD_RUNNING(gp)) {
382 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
391 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
392 * was forced to initialize the screen during startup. We'd like to
393 * wait for a single character from the user, but we can't because
394 * we're not in raw mode. We can't switch to raw mode because the
395 * vi initialization will switch to xterm's alternate screen, causing
396 * us to lose the messages we're pausing to make sure the user read.
397 * So, wait for a complete line.
399 if (F_ISSET(sp, SC_SCR_EX)) {
400 p = msg_cmsg(sp, CMSG_CONT_R, &len);
401 (void)write(STDOUT_FILENO, p, len);
403 if (v_event_get(sp, &ev, 0, 0))
405 if (ev.e_event == E_INTERRUPT ||
406 (ev.e_event == E_CHARACTER &&
407 (ev.e_value == K_CR || ev.e_value == K_NL)))
409 (void)gp->scr_bell(sp);
413 /* Switch into the right editor, regardless. */
414 F_CLR(sp, SC_EX | SC_VI);
415 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
418 * Main edit loop. Vi handles split screens itself, we only return
419 * here when switching editor modes or restarting the screen.
422 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
429 /* Clean out the global structure. */
437 * End the program, discarding screens and most of the global area.
439 * PUBLIC: void v_end __P((GS *));
448 /* If there are any remaining screens, kill them off. */
449 if (gp->ccl_sp != NULL) {
450 (void)file_end(gp->ccl_sp, NULL, 1);
451 (void)screen_end(gp->ccl_sp);
453 while ((sp = TAILQ_FIRST(gp->dq)) != NULL)
454 (void)screen_end(sp);
455 while ((sp = TAILQ_FIRST(gp->hq)) != NULL)
456 (void)screen_end(sp);
458 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
461 while ((frp = TAILQ_FIRST(gp->frefq)) != NULL) {
462 TAILQ_REMOVE(gp->frefq, frp, q);
463 if (frp->name != NULL)
465 if (frp->tname != NULL)
471 /* Free key input queue. */
472 if (gp->i_event != NULL)
475 /* Free cut buffers. */
478 /* Free map sequences. */
481 /* Free default buffer storage. */
482 (void)text_lfree(gp->dcb_store.textq);
484 /* Close message catalogs. */
488 /* Ring the bell if scheduled. */
489 if (F_ISSET(gp, G_BELLSCHED))
490 (void)fprintf(stderr, "\07"); /* \a */
493 * Flush any remaining messages. If a message is here, it's almost
494 * certainly the message about the event that killed us (although
495 * it's possible that the user is sourcing a file that exits from the
498 while ((mp = SLIST_FIRST(gp->msgq)) != NULL) {
499 (void)fprintf(stderr, "%s%.*s",
500 mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
501 SLIST_REMOVE_HEAD(gp->msgq, q);
502 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
508 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
509 /* Free any temporary space. */
510 if (gp->tmp_bp != NULL)
514 /* Close debugging file descriptor. */
515 if (gp->tracefp != NULL)
516 (void)fclose(gp->tracefp);
523 * Convert historic arguments into something getopt(3) will like.
534 * Translate old style arguments into something getopt will like.
535 * Make sure it's not text space memory, because ex modifies the
537 * Change "+" into "-c$".
538 * Change "+<anything else>" into "-c<anything else>".
539 * Change "-" into "-s"
540 * The c, T, t and w options take arguments so they can't be
543 * Stop if we find "--" as an argument, the user may want to edit
544 * a file named "+foo".
546 while (*++argv && strcmp(argv[0], "--"))
547 if (argv[0][0] == '+') {
548 if (argv[0][1] == '\0') {
549 argv[0] = strdup("-c$");
554 len = strlen(argv[0]);
555 argv[0] = malloc(len + 2);
560 (void)strlcpy(argv[0] + 2, p + 1, len);
562 } else if (argv[0][0] == '-')
563 if (argv[0][1] == '\0') {
564 argv[0] = strdup("-s");
565 if (argv[0] == NULL) {
566 nomem: v_estr(name, errno, NULL);
570 if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
571 argv[0][1] == 't' || argv[0][1] == 'w') &&
584 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
585 v_estr(gp->progname, errno, _PATH_TTY);
589 (void)printf("process %lu waiting, enter <CR> to continue: ",
591 (void)fflush(stdout);
594 if (read(fd, &ch, 1) != 1) {
598 } while (ch != '\n' && ch != '\r');
609 (void)fprintf(stderr, "%s", name);
611 (void)fprintf(stderr, ": %s", msg);
613 (void)fprintf(stderr, ": %s", strerror(errno));
614 (void)fprintf(stderr, "\n");