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 "%Z% Copyright (c) 1992, 1993, 1994\n\
15 The Regents of the University of California. All rights reserved.\n\
16 %Z% Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
17 Keith Bostic. All rights reserved.\n";
21 static const char sccsid
[] = "$Id: main.c,v 10.50 1996/12/17 14:51:28 bostic Exp $ (Berkeley) $Date: 1996/12/17 14:51:28 $";
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
;
67 char *tag_f
, *wsizearg
, path
[256];
69 /* Initialize the busy routine, if not defined by the screen. */
70 if (gp
->scr_busy
== NULL
)
71 gp
->scr_busy
= vs_busy
;
72 /* Initialize the message routine, if not defined by the screen. */
73 if (gp
->scr_msg
== NULL
)
76 /* Common global structure initialization. */
77 CIRCLEQ_INIT(&gp
->dq
);
78 CIRCLEQ_INIT(&gp
->hq
);
80 LIST_INSERT_HEAD(&gp
->ecq
, &gp
->excmd
, q
);
81 gp
->noprint
= DEFAULT_NOPRINT
;
83 /* Structures shared by screens so stored in the GS structure. */
84 CIRCLEQ_INIT(&gp
->frefq
);
85 CIRCLEQ_INIT(&gp
->dcb_store
.textq
);
89 /* Set initial screen type and mode based on the program name. */
91 if (!strcmp(gp
->progname
, "ex") || !strcmp(gp
->progname
, "nex"))
94 /* Nview, view are readonly. */
95 if (!strcmp(gp
->progname
, "nview") ||
96 !strcmp(gp
->progname
, "view"))
99 /* Vi is the default. */
103 /* Convert old-style arguments into new-style ones. */
104 if (v_obsolete(gp
->progname
, argv
))
107 /* Parse the arguments. */
109 tag_f
= wsizearg
= NULL
;
110 lflag
= secure
= silent
= 0;
113 /* Set the file snapshot flag. */
114 F_SET(gp
, G_SNAPSHOT
);
117 while ((ch
= getopt(argc
, argv
, "c:D:eFlRrSsT:t:vw:")) != EOF
)
119 while ((ch
= getopt(argc
, argv
, "c:eFlRrSst:vw:")) != EOF
)
122 case 'c': /* Run the command. */
125 * We should support multiple -c options.
127 if (gp
->c_option
!= NULL
) {
128 v_estr(gp
->progname
, 0,
129 "only one -c command may be specified.");
132 gp
->c_option
= optarg
;
144 v_estr(gp
->progname
, 0,
145 "usage: -D requires s or w argument.");
150 case 'e': /* Ex mode. */
154 case 'F': /* No snapshot. */
155 F_CLR(gp
, G_SNAPSHOT
);
157 case 'l': /* Set lisp, showmatch options. */
160 case 'R': /* Readonly. */
163 case 'r': /* Recover. */
164 if (flagchk
== 't') {
165 v_estr(gp
->progname
, 0,
166 "only one of -r and -t may be specified.");
178 case 'T': /* Trace. */
179 (void)vtrace_init(optarg
);
183 if (flagchk
== 'r') {
184 v_estr(gp
->progname
, 0,
185 "only one of -r and -t may be specified.");
188 if (flagchk
== 't') {
189 v_estr(gp
->progname
, 0,
190 "only one tag file may be specified.");
196 case 'v': /* Vi mode. */
205 (void)gp
->scr_usage();
212 * -s option is only meaningful to ex.
214 * If not reading from a terminal, it's like -s was specified.
216 if (silent
&& !LF_ISSET(SC_EX
)) {
217 v_estr(gp
->progname
, 0, "-s option is only applicable to ex.");
220 if (LF_ISSET(SC_EX
) && F_ISSET(gp
, G_SCRIPTED
))
224 * Build and initialize the first/current screen. This is a bit
225 * tricky. If an error is returned, we may or may not have a
226 * screen structure. If we have a screen structure, put it on a
227 * display queue so that the error messages get displayed.
230 * Everything we do until we go interactive is done in ex mode.
232 if (screen_init(gp
, NULL
, &sp
)) {
234 CIRCLEQ_INSERT_HEAD(&gp
->dq
, sp
, q
);
238 CIRCLEQ_INSERT_HEAD(&gp
->dq
, sp
, q
);
240 if (v_key_init(sp
)) /* Special key initialization. */
243 { int oargs
[5], *oargp
= oargs
;
244 if (lflag
) { /* Command-line options. */
246 *oargp
++ = O_SHOWMATCH
;
249 *oargp
++ = O_READONLY
;
252 *oargp
= -1; /* Options initialization. */
253 if (opts_init(sp
, oargs
))
256 if (wsizearg
!= NULL
) {
258 (void)snprintf(path
, sizeof(path
), "window=%s", wsizearg
);
259 a
.bp
= (CHAR_T
*)path
;
260 a
.len
= strlen(path
);
265 (void)opts_set(sp
, av
, NULL
);
267 if (silent
) { /* Ex batch mode option values. */
268 O_CLR(sp
, O_AUTOPRINT
);
270 O_CLR(sp
, O_VERBOSE
);
272 F_SET(sp
, SC_EX_SILENT
);
275 sp
->rows
= O_VAL(sp
, O_LINES
); /* Make ex formatting work. */
276 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
278 if (!silent
&& startup
) { /* Read EXINIT, exrc files. */
281 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
289 * List recovery files if -r specified without file arguments.
290 * Note, options must be initialized and startup information
291 * read before doing this.
293 if (flagchk
== 'r' && argv
[0] == NULL
) {
303 * Initialize the default ^D, ^U scrolling value here, after the
304 * user has had every opportunity to set the window option.
306 * It's historic practice that changing the value of the window
307 * option did not alter the default scrolling value, only giving
308 * a count to ^D/^U did that.
310 sp
->defscroll
= (O_VAL(sp
, O_WINDOW
) + 1) / 2;
313 * If we don't have a command-line option, switch into the right
314 * editor now, so that we position default files correctly, and
315 * so that any tags file file-already-locked messages are in the
316 * vi screen, not the ex screen.
319 * If we have a command-line option, the error message can end
320 * up in the wrong place, but I think that the combination is
323 if (gp
->c_option
== NULL
) {
324 F_CLR(sp
, SC_EX
| SC_VI
);
325 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
));
328 /* Open a tag file if specified. */
329 if (tag_f
!= NULL
&& ex_tag_first(sp
, tag_f
))
333 * Append any remaining arguments as file names. Files are recovery
334 * files if -r specified. If the tag option or ex startup commands
335 * loaded a file, then any file arguments are going to come after it.
338 if (sp
->frp
!= NULL
) {
339 /* Cheat -- we know we have an extra argv slot. */
341 *--argv
, char *, strlen(sp
->frp
->name
) + 1);
343 v_estr(gp
->progname
, errno
, NULL
);
346 (void)strcpy(*argv
, sp
->frp
->name
);
348 sp
->argv
= sp
->cargv
= argv
;
349 F_SET(sp
, SC_ARGNOFREE
);
351 F_SET(sp
, SC_ARGRECOVER
);
355 * If the ex startup commands and or/the tag option haven't already
356 * created a file, create one. If no command-line files were given,
357 * use a temporary file.
359 if (sp
->frp
== NULL
) {
360 if (sp
->argv
== NULL
) {
361 if ((frp
= file_add(sp
, NULL
)) == NULL
)
364 if ((frp
= file_add(sp
, (CHAR_T
*)sp
->argv
[0])) == NULL
)
366 if (F_ISSET(sp
, SC_ARGRECOVER
))
367 F_SET(frp
, FR_RECOVER
);
370 if (file_init(sp
, frp
, NULL
, 0))
372 if (EXCMD_RUNNING(gp
)) {
374 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
383 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
384 * was forced to initialize the screen during startup. We'd like to
385 * wait for a single character from the user, but we can't because
386 * we're not in raw mode. We can't switch to raw mode because the
387 * vi initialization will switch to xterm's alternate screen, causing
388 * us to lose the messages we're pausing to make sure the user read.
389 * So, wait for a complete line.
391 if (F_ISSET(sp
, SC_SCR_EX
)) {
392 p
= msg_cmsg(sp
, CMSG_CONT_R
, &len
);
393 (void)write(STDOUT_FILENO
, p
, len
);
395 if (v_event_get(sp
, &ev
, 0, 0))
397 if (ev
.e_event
== E_INTERRUPT
||
398 ev
.e_event
== E_CHARACTER
&&
399 (ev
.e_value
== K_CR
|| ev
.e_value
== K_NL
))
401 (void)gp
->scr_bell(sp
);
405 /* Switch into the right editor, regardless. */
406 F_CLR(sp
, SC_EX
| SC_VI
);
407 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
) | SC_STATUS_CNT
);
410 * Main edit loop. Vi handles split screens itself, we only return
411 * here when switching editor modes or restarting the screen.
414 if (F_ISSET(sp
, SC_EX
) ? ex(&sp
) : vi(&sp
))
421 /* Clean out the global structure. */
429 * End the program, discarding screens and most of the global area.
431 * PUBLIC: void v_end __P((GS *));
440 /* If there are any remaining screens, kill them off. */
441 if (gp
->ccl_sp
!= NULL
) {
442 (void)file_end(gp
->ccl_sp
, NULL
, 1);
443 (void)screen_end(gp
->ccl_sp
);
445 while ((sp
= gp
->dq
.cqh_first
) != (void *)&gp
->dq
)
446 (void)screen_end(sp
);
447 while ((sp
= gp
->hq
.cqh_first
) != (void *)&gp
->hq
)
448 (void)screen_end(sp
);
450 #ifdef HAVE_PERL_INTERP
454 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
457 while ((frp
= gp
->frefq
.cqh_first
) != (FREF
*)&gp
->frefq
) {
458 CIRCLEQ_REMOVE(&gp
->frefq
, frp
, q
);
459 if (frp
->name
!= NULL
)
461 if (frp
->tname
!= NULL
)
467 /* Free key input queue. */
468 if (gp
->i_event
!= NULL
)
471 /* Free cut buffers. */
474 /* Free map sequences. */
477 /* Free default buffer storage. */
478 (void)text_lfree(&gp
->dcb_store
.textq
);
480 /* Close message catalogs. */
484 /* Ring the bell if scheduled. */
485 if (F_ISSET(gp
, G_BELLSCHED
))
486 (void)fprintf(stderr
, "\07"); /* \a */
489 * Flush any remaining messages. If a message is here, it's almost
490 * certainly the message about the event that killed us (although
491 * it's possible that the user is sourcing a file that exits from the
494 while ((mp
= gp
->msgq
.lh_first
) != NULL
) {
495 (void)fprintf(stderr
, "%s%.*s",
496 mp
->mtype
== M_ERR
? "ex/vi: " : "", (int)mp
->len
, mp
->buf
);
498 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
504 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
505 /* Free any temporary space. */
506 if (gp
->tmp_bp
!= NULL
)
510 /* Close tracing file descriptor. */
518 * Convert historic arguments into something getopt(3) will like.
521 v_obsolete(name
, argv
)
528 * Translate old style arguments into something getopt will like.
529 * Make sure it's not text space memory, because ex modifies the
531 * Change "+" into "-c$".
532 * Change "+<anything else>" into "-c<anything else>".
533 * Change "-" into "-s"
534 * The c, T, t and w options take arguments so they can't be
537 * Stop if we find "--" as an argument, the user may want to edit
538 * a file named "+foo".
540 while (*++argv
&& strcmp(argv
[0], "--"))
541 if (argv
[0][0] == '+') {
542 if (argv
[0][1] == '\0') {
543 MALLOC_NOMSG(NULL
, argv
[0], char *, 4);
546 (void)strcpy(argv
[0], "-c$");
549 len
= strlen(argv
[0]);
550 MALLOC_NOMSG(NULL
, argv
[0], char *, len
+ 2);
555 (void)strcpy(argv
[0] + 2, p
+ 1);
557 } else if (argv
[0][0] == '-')
558 if (argv
[0][1] == '\0') {
559 MALLOC_NOMSG(NULL
, argv
[0], char *, 3);
560 if (argv
[0] == NULL
) {
561 nomem
: v_estr(name
, errno
, NULL
);
564 (void)strcpy(argv
[0], "-s");
566 if ((argv
[0][1] == 'c' || argv
[0][1] == 'T' ||
567 argv
[0][1] == 't' || argv
[0][1] == 'w') &&
581 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
582 v_estr(gp
->progname
, errno
, _PATH_TTY
);
586 (void)printf("process %lu waiting, enter <CR> to continue: ",
588 (void)fflush(stdout
);
591 if (read(fd
, &ch
, 1) != 1) {
595 } while (ch
!= '\n' && ch
!= '\r');
601 v_estr(name
, eno
, msg
)
605 (void)fprintf(stderr
, "%s", name
);
607 (void)fprintf(stderr
, ": %s", msg
);
609 (void)fprintf(stderr
, ": %s", strerror(errno
));
610 (void)fprintf(stderr
, "\n");