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.62 2001/11/01 10:28:25 skimo Exp $ (Berkeley) $Date: 2001/11/01 10:28:25 $";
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((WIN *, int, char *[]));
53 editor(WIN
*wp
, int argc
, char **argv
)
64 int ch
, flagchk
, lflag
, secure
, startup
, readonly
, rval
, silent
;
65 char *tag_f
, *wsizearg
, path
[256];
71 /* Initialize the busy routine, if not defined by the screen. */
72 if (gp
->scr_busy
== NULL
)
73 gp
->scr_busy
= vs_busy
;
74 /* Initialize the message routine, if not defined by the screen. */
75 if (wp
->scr_msg
== NULL
)
78 /* Set initial screen type and mode based on the program name. */
80 if (!strcmp(gp
->progname
, "ex") || !strcmp(gp
->progname
, "nex"))
83 /* Nview, view are readonly. */
84 if (!strcmp(gp
->progname
, "nview") ||
85 !strcmp(gp
->progname
, "view"))
88 /* Vi is the default. */
92 /* Convert old-style arguments into new-style ones. */
93 if (v_obsolete(gp
->progname
, argv
))
96 /* Parse the arguments. */
98 tag_f
= wsizearg
= NULL
;
99 lflag
= secure
= silent
= 0;
102 /* Set the file snapshot flag. */
103 F_SET(gp
, G_SNAPSHOT
);
106 while ((ch
= getopt(argc
, argv
, "c:D:eFlRrSsT:t:vw:")) != EOF
)
108 while ((ch
= getopt(argc
, argv
, "c:eFlRrSst:vw:")) != EOF
)
111 case 'c': /* Run the command. */
114 * We should support multiple -c options.
116 if (gp
->c_option
!= NULL
) {
117 v_estr(gp
->progname
, 0,
118 "only one -c command may be specified.");
121 gp
->c_option
= optarg
;
133 v_estr(gp
->progname
, 0,
134 "usage: -D requires s or w argument.");
139 case 'e': /* Ex mode. */
143 case 'F': /* No snapshot. */
144 F_CLR(gp
, G_SNAPSHOT
);
146 case 'l': /* Set lisp, showmatch options. */
149 case 'R': /* Readonly. */
152 case 'r': /* Recover. */
153 if (flagchk
== 't') {
154 v_estr(gp
->progname
, 0,
155 "only one of -r and -t may be specified.");
167 case 'T': /* Trace. */
168 (void)vtrace_init(optarg
);
172 if (flagchk
== 'r') {
173 v_estr(gp
->progname
, 0,
174 "only one of -r and -t may be specified.");
177 if (flagchk
== 't') {
178 v_estr(gp
->progname
, 0,
179 "only one tag file may be specified.");
185 case 'v': /* Vi mode. */
194 (void)gp
->scr_usage();
201 * -s option is only meaningful to ex.
203 * If not reading from a terminal, it's like -s was specified.
205 if (silent
&& !LF_ISSET(SC_EX
)) {
206 v_estr(gp
->progname
, 0, "-s option is only applicable to ex.");
209 if (LF_ISSET(SC_EX
) && F_ISSET(gp
, G_SCRIPTED
))
213 * Build and initialize the first/current screen. This is a bit
214 * tricky. If an error is returned, we may or may not have a
215 * screen structure. If we have a screen structure, put it on a
216 * display queue so that the error messages get displayed.
219 * Everything we do until we go interactive is done in ex mode.
221 if (screen_init(gp
, NULL
, &sp
)) {
223 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
229 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
232 if (v_key_init(sp
)) /* Special key initialization. */
235 { int oargs
[5], *oargp
= oargs
;
236 if (lflag
) { /* Command-line options. */
238 *oargp
++ = O_SHOWMATCH
;
241 *oargp
++ = O_READONLY
;
244 *oargp
= -1; /* Options initialization. */
245 if (opts_init(sp
, oargs
))
248 if (wsizearg
!= NULL
) {
250 (void)snprintf(path
, sizeof(path
), "window=%s", wsizearg
);
251 a
.bp
= (CHAR_T
*)path
;
252 a
.len
= strlen(path
);
257 (void)opts_set(sp
, av
, NULL
);
259 if (silent
) { /* Ex batch mode option values. */
260 O_CLR(sp
, O_AUTOPRINT
);
262 O_CLR(sp
, O_VERBOSE
);
264 F_SET(sp
, SC_EX_SILENT
);
267 sp
->rows
= O_VAL(sp
, O_LINES
); /* Make ex formatting work. */
268 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
270 if (!silent
&& startup
) { /* Read EXINIT, exrc files. */
273 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
281 * List recovery files if -r specified without file arguments.
282 * Note, options must be initialized and startup information
283 * read before doing this.
285 if (flagchk
== 'r' && argv
[0] == NULL
) {
295 * Initialize the default ^D, ^U scrolling value here, after the
296 * user has had every opportunity to set the window option.
298 * It's historic practice that changing the value of the window
299 * option did not alter the default scrolling value, only giving
300 * a count to ^D/^U did that.
302 sp
->defscroll
= (O_VAL(sp
, O_WINDOW
) + 1) / 2;
305 * If we don't have a command-line option, switch into the right
306 * editor now, so that we position default files correctly, and
307 * so that any tags file file-already-locked messages are in the
308 * vi screen, not the ex screen.
311 * If we have a command-line option, the error message can end
312 * up in the wrong place, but I think that the combination is
315 if (gp
->c_option
== NULL
) {
316 F_CLR(sp
, SC_EX
| SC_VI
);
317 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
));
320 /* Open a tag file if specified. */
322 CHAR2INT(sp
, tag_f
, strlen(tag_f
) + 1, w
, wlen
);
323 if (ex_tag_first(sp
, w
))
328 * Append any remaining arguments as file names. Files are recovery
329 * files if -r specified. If the tag option or ex startup commands
330 * loaded a file, then any file arguments are going to come after it.
333 if (sp
->frp
!= NULL
) {
334 /* Cheat -- we know we have an extra argv slot. */
336 *--argv
, char *, strlen(sp
->frp
->name
) + 1);
338 v_estr(gp
->progname
, errno
, NULL
);
341 (void)strcpy(*argv
, sp
->frp
->name
);
343 sp
->argv
= sp
->cargv
= argv
;
344 F_SET(sp
, SC_ARGNOFREE
);
346 F_SET(sp
, SC_ARGRECOVER
);
350 * If the ex startup commands and or/the tag option haven't already
351 * created a file, create one. If no command-line files were given,
352 * use a temporary file.
354 if (sp
->frp
== NULL
) {
355 if (sp
->argv
== NULL
) {
356 if ((frp
= file_add(sp
, NULL
)) == NULL
)
359 if ((frp
= file_add(sp
, sp
->argv
[0])) == NULL
)
361 if (F_ISSET(sp
, SC_ARGRECOVER
))
362 F_SET(frp
, FR_RECOVER
);
365 if (file_init(sp
, frp
, NULL
, 0))
367 if (EXCMD_RUNNING(wp
)) {
369 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
378 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
379 * was forced to initialize the screen during startup. We'd like to
380 * wait for a single character from the user, but we can't because
381 * we're not in raw mode. We can't switch to raw mode because the
382 * vi initialization will switch to xterm's alternate screen, causing
383 * us to lose the messages we're pausing to make sure the user read.
384 * So, wait for a complete line.
386 if (F_ISSET(sp
, SC_SCR_EX
)) {
387 p
= msg_cmsg(sp
, CMSG_CONT_R
, &len
);
388 (void)write(STDOUT_FILENO
, p
, len
);
390 if (v_event_get(sp
, &ev
, 0, 0))
392 if (ev
.e_event
== E_INTERRUPT
||
393 (ev
.e_event
== E_CHARACTER
&&
394 (ev
.e_value
== K_CR
|| ev
.e_value
== K_NL
)))
396 (void)gp
->scr_bell(sp
);
400 /* Switch into the right editor, regardless. */
401 F_CLR(sp
, SC_EX
| SC_VI
);
402 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
) | SC_STATUS_CNT
);
405 * Main edit loop. Vi handles split screens itself, we only return
406 * here when switching editor modes or restarting the screen.
409 if (F_ISSET(sp
, SC_EX
) ? ex(&sp
) : vi(&sp
))
421 * Convert historic arguments into something getopt(3) will like.
424 v_obsolete(char *name
, char **argv
)
430 * Translate old style arguments into something getopt will like.
431 * Make sure it's not text space memory, because ex modifies the
433 * Change "+" into "-c$".
434 * Change "+<anything else>" into "-c<anything else>".
435 * Change "-" into "-s"
436 * The c, T, t and w options take arguments so they can't be
439 * Stop if we find "--" as an argument, the user may want to edit
440 * a file named "+foo".
442 while (*++argv
&& strcmp(argv
[0], "--"))
443 if (argv
[0][0] == '+') {
444 if (argv
[0][1] == '\0') {
445 MALLOC_NOMSG(NULL
, argv
[0], char *, 4);
448 (void)strcpy(argv
[0], "-c$");
451 len
= strlen(argv
[0]);
452 MALLOC_NOMSG(NULL
, argv
[0], char *, len
+ 2);
457 (void)strcpy(argv
[0] + 2, p
+ 1);
459 } else if (argv
[0][0] == '-') {
460 if (argv
[0][1] == '\0') {
461 MALLOC_NOMSG(NULL
, argv
[0], char *, 3);
462 if (argv
[0] == NULL
) {
463 nomem
: v_estr(name
, errno
, NULL
);
466 (void)strcpy(argv
[0], "-s");
468 if ((argv
[0][1] == 'c' || argv
[0][1] == 'T' ||
469 argv
[0][1] == 't' || argv
[0][1] == 'w') &&
483 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
484 v_estr(gp
->progname
, errno
, _PATH_TTY
);
488 (void)printf("process %lu waiting, enter <CR> to continue: ",
490 (void)fflush(stdout
);
493 if (read(fd
, &ch
, 1) != 1) {
497 } while (ch
!= '\n' && ch
!= '\r');
503 v_estr(char *name
, int eno
, char *msg
)
505 (void)fprintf(stderr
, "%s", name
);
507 (void)fprintf(stderr
, ": %s", msg
);
509 (void)fprintf(stderr
, ": %s", strerror(errno
));
510 (void)fprintf(stderr
, "\n");