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.63 2001/11/01 15:24:43 skimo Exp $ (Berkeley) $Date: 2001/11/01 15:24:43 $";
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 v_estr(gp
->progname
, 0,
145 "-F option no longer supported.");
147 case 'l': /* Set lisp, showmatch options. */
150 case 'R': /* Readonly. */
153 case 'r': /* Recover. */
154 if (flagchk
== 't') {
155 v_estr(gp
->progname
, 0,
156 "only one of -r and -t may be specified.");
168 case 'T': /* Trace. */
169 (void)vtrace_init(optarg
);
173 if (flagchk
== 'r') {
174 v_estr(gp
->progname
, 0,
175 "only one of -r and -t may be specified.");
178 if (flagchk
== 't') {
179 v_estr(gp
->progname
, 0,
180 "only one tag file may be specified.");
186 case 'v': /* Vi mode. */
195 (void)gp
->scr_usage();
202 * -s option is only meaningful to ex.
204 * If not reading from a terminal, it's like -s was specified.
206 if (silent
&& !LF_ISSET(SC_EX
)) {
207 v_estr(gp
->progname
, 0, "-s option is only applicable to ex.");
210 if (LF_ISSET(SC_EX
) && F_ISSET(gp
, G_SCRIPTED
))
214 * Build and initialize the first/current screen. This is a bit
215 * tricky. If an error is returned, we may or may not have a
216 * screen structure. If we have a screen structure, put it on a
217 * display queue so that the error messages get displayed.
220 * Everything we do until we go interactive is done in ex mode.
222 if (screen_init(gp
, NULL
, &sp
)) {
224 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
230 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
233 if (v_key_init(sp
)) /* Special key initialization. */
236 { int oargs
[5], *oargp
= oargs
;
237 if (lflag
) { /* Command-line options. */
239 *oargp
++ = O_SHOWMATCH
;
242 *oargp
++ = O_READONLY
;
245 *oargp
= -1; /* Options initialization. */
246 if (opts_init(sp
, oargs
))
249 if (wsizearg
!= NULL
) {
251 (void)snprintf(path
, sizeof(path
), "window=%s", wsizearg
);
252 a
.bp
= (CHAR_T
*)path
;
253 a
.len
= strlen(path
);
258 (void)opts_set(sp
, av
, NULL
);
260 if (silent
) { /* Ex batch mode option values. */
261 O_CLR(sp
, O_AUTOPRINT
);
263 O_CLR(sp
, O_VERBOSE
);
265 F_SET(sp
, SC_EX_SILENT
);
268 sp
->rows
= O_VAL(sp
, O_LINES
); /* Make ex formatting work. */
269 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
271 if (!silent
&& startup
) { /* Read EXINIT, exrc files. */
274 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
282 * List recovery files if -r specified without file arguments.
283 * Note, options must be initialized and startup information
284 * read before doing this.
286 if (flagchk
== 'r' && argv
[0] == NULL
) {
296 * Initialize the default ^D, ^U scrolling value here, after the
297 * user has had every opportunity to set the window option.
299 * It's historic practice that changing the value of the window
300 * option did not alter the default scrolling value, only giving
301 * a count to ^D/^U did that.
303 sp
->defscroll
= (O_VAL(sp
, O_WINDOW
) + 1) / 2;
306 * If we don't have a command-line option, switch into the right
307 * editor now, so that we position default files correctly, and
308 * so that any tags file file-already-locked messages are in the
309 * vi screen, not the ex screen.
312 * If we have a command-line option, the error message can end
313 * up in the wrong place, but I think that the combination is
316 if (gp
->c_option
== NULL
) {
317 F_CLR(sp
, SC_EX
| SC_VI
);
318 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
));
321 /* Open a tag file if specified. */
323 CHAR2INT(sp
, tag_f
, strlen(tag_f
) + 1, w
, wlen
);
324 if (ex_tag_first(sp
, w
))
329 * Append any remaining arguments as file names. Files are recovery
330 * files if -r specified. If the tag option or ex startup commands
331 * loaded a file, then any file arguments are going to come after it.
334 if (sp
->frp
!= NULL
) {
335 /* Cheat -- we know we have an extra argv slot. */
337 *--argv
, char *, strlen(sp
->frp
->name
) + 1);
339 v_estr(gp
->progname
, errno
, NULL
);
342 (void)strcpy(*argv
, sp
->frp
->name
);
344 sp
->argv
= sp
->cargv
= argv
;
345 F_SET(sp
, SC_ARGNOFREE
);
347 F_SET(sp
, SC_ARGRECOVER
);
351 * If the ex startup commands and or/the tag option haven't already
352 * created a file, create one. If no command-line files were given,
353 * use a temporary file.
355 if (sp
->frp
== NULL
) {
356 if (sp
->argv
== NULL
) {
357 if ((frp
= file_add(sp
, NULL
)) == NULL
)
360 if ((frp
= file_add(sp
, sp
->argv
[0])) == NULL
)
362 if (F_ISSET(sp
, SC_ARGRECOVER
))
363 F_SET(frp
, FR_RECOVER
);
366 if (file_init(sp
, frp
, NULL
, 0))
368 if (EXCMD_RUNNING(wp
)) {
370 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
379 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
380 * was forced to initialize the screen during startup. We'd like to
381 * wait for a single character from the user, but we can't because
382 * we're not in raw mode. We can't switch to raw mode because the
383 * vi initialization will switch to xterm's alternate screen, causing
384 * us to lose the messages we're pausing to make sure the user read.
385 * So, wait for a complete line.
387 if (F_ISSET(sp
, SC_SCR_EX
)) {
388 p
= msg_cmsg(sp
, CMSG_CONT_R
, &len
);
389 (void)write(STDOUT_FILENO
, p
, len
);
391 if (v_event_get(sp
, &ev
, 0, 0))
393 if (ev
.e_event
== E_INTERRUPT
||
394 (ev
.e_event
== E_CHARACTER
&&
395 (ev
.e_value
== K_CR
|| ev
.e_value
== K_NL
)))
397 (void)gp
->scr_bell(sp
);
401 /* Switch into the right editor, regardless. */
402 F_CLR(sp
, SC_EX
| SC_VI
);
403 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
) | SC_STATUS_CNT
);
406 * Main edit loop. Vi handles split screens itself, we only return
407 * here when switching editor modes or restarting the screen.
410 if (F_ISSET(sp
, SC_EX
) ? ex(&sp
) : vi(&sp
))
422 * Convert historic arguments into something getopt(3) will like.
425 v_obsolete(char *name
, char **argv
)
431 * Translate old style arguments into something getopt will like.
432 * Make sure it's not text space memory, because ex modifies the
434 * Change "+" into "-c$".
435 * Change "+<anything else>" into "-c<anything else>".
436 * Change "-" into "-s"
437 * The c, T, t and w options take arguments so they can't be
440 * Stop if we find "--" as an argument, the user may want to edit
441 * a file named "+foo".
443 while (*++argv
&& strcmp(argv
[0], "--"))
444 if (argv
[0][0] == '+') {
445 if (argv
[0][1] == '\0') {
446 MALLOC_NOMSG(NULL
, argv
[0], char *, 4);
449 (void)strcpy(argv
[0], "-c$");
452 len
= strlen(argv
[0]);
453 MALLOC_NOMSG(NULL
, argv
[0], char *, len
+ 2);
458 (void)strcpy(argv
[0] + 2, p
+ 1);
460 } else if (argv
[0][0] == '-') {
461 if (argv
[0][1] == '\0') {
462 MALLOC_NOMSG(NULL
, argv
[0], char *, 3);
463 if (argv
[0] == NULL
) {
464 nomem
: v_estr(name
, errno
, NULL
);
467 (void)strcpy(argv
[0], "-s");
469 if ((argv
[0][1] == 'c' || argv
[0][1] == 'T' ||
470 argv
[0][1] == 't' || argv
[0][1] == 'w') &&
484 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
485 v_estr(gp
->progname
, errno
, _PATH_TTY
);
489 (void)printf("process %lu waiting, enter <CR> to continue: ",
491 (void)fflush(stdout
);
494 if (read(fd
, &ch
, 1) != 1) {
498 } while (ch
!= '\n' && ch
!= '\r');
504 v_estr(char *name
, int eno
, char *msg
)
506 (void)fprintf(stderr
, "%s", name
);
508 (void)fprintf(stderr
, ": %s", msg
);
510 (void)fprintf(stderr
, ": %s", strerror(errno
));
511 (void)fprintf(stderr
, "\n");