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.55 2000/07/07 22:29:13 skimo Exp $ (Berkeley) $Date: 2000/07/07 22:29:13 $";
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(wp
, argc
, argv
)
67 int ch
, flagchk
, lflag
, secure
, startup
, readonly
, rval
, silent
;
68 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
->hq
);
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
->exfq
);
86 CIRCLEQ_INIT(&gp
->dcb_store
.textq
);
90 /* Set initial screen type and mode based on the program name. */
92 if (!strcmp(gp
->progname
, "ex") || !strcmp(gp
->progname
, "nex"))
95 /* Nview, view are readonly. */
96 if (!strcmp(gp
->progname
, "nview") ||
97 !strcmp(gp
->progname
, "view"))
100 /* Vi is the default. */
104 /* Convert old-style arguments into new-style ones. */
105 if (v_obsolete(gp
->progname
, argv
))
108 /* Parse the arguments. */
110 tag_f
= wsizearg
= NULL
;
111 lflag
= secure
= silent
= 0;
114 /* Set the file snapshot flag. */
115 F_SET(gp
, G_SNAPSHOT
);
118 while ((ch
= getopt(argc
, argv
, "c:D:eFlRrSsT:t:vw:")) != EOF
)
120 while ((ch
= getopt(argc
, argv
, "c:eFlRrSst:vw:")) != EOF
)
123 case 'c': /* Run the command. */
126 * We should support multiple -c options.
128 if (gp
->c_option
!= NULL
) {
129 v_estr(gp
->progname
, 0,
130 "only one -c command may be specified.");
133 gp
->c_option
= optarg
;
145 v_estr(gp
->progname
, 0,
146 "usage: -D requires s or w argument.");
151 case 'e': /* Ex mode. */
155 case 'F': /* No snapshot. */
156 F_CLR(gp
, G_SNAPSHOT
);
158 case 'l': /* Set lisp, showmatch options. */
161 case 'R': /* Readonly. */
164 case 'r': /* Recover. */
165 if (flagchk
== 't') {
166 v_estr(gp
->progname
, 0,
167 "only one of -r and -t may be specified.");
179 case 'T': /* Trace. */
180 (void)vtrace_init(optarg
);
184 if (flagchk
== 'r') {
185 v_estr(gp
->progname
, 0,
186 "only one of -r and -t may be specified.");
189 if (flagchk
== 't') {
190 v_estr(gp
->progname
, 0,
191 "only one tag file may be specified.");
197 case 'v': /* Vi mode. */
206 (void)gp
->scr_usage();
213 * -s option is only meaningful to ex.
215 * If not reading from a terminal, it's like -s was specified.
217 if (silent
&& !LF_ISSET(SC_EX
)) {
218 v_estr(gp
->progname
, 0, "-s option is only applicable to ex.");
221 if (LF_ISSET(SC_EX
) && F_ISSET(gp
, G_SCRIPTED
))
224 if (db_env_create(&gp
->env
, 0)) {
225 v_estr(gp
->progname
, 0, "Unable to create DB environment.");
228 gp
->env
->set_errfile(gp
->env
, stderr
);
229 gp
->env
->open(gp
->env
, NULL
, 0, 0);
233 * Build and initialize the first/current screen. This is a bit
234 * tricky. If an error is returned, we may or may not have a
235 * screen structure. If we have a screen structure, put it on a
236 * display queue so that the error messages get displayed.
239 * Everything we do until we go interactive is done in ex mode.
241 if (screen_init(gp
, NULL
, &sp
)) {
243 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
249 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
252 if (v_key_init(sp
)) /* Special key initialization. */
255 { int oargs
[5], *oargp
= oargs
;
256 if (lflag
) { /* Command-line options. */
258 *oargp
++ = O_SHOWMATCH
;
261 *oargp
++ = O_READONLY
;
264 *oargp
= -1; /* Options initialization. */
265 if (opts_init(sp
, oargs
))
268 if (wsizearg
!= NULL
) {
270 (void)snprintf(path
, sizeof(path
), "window=%s", wsizearg
);
271 a
.bp
= (CHAR_T
*)path
;
272 a
.len
= strlen(path
);
277 (void)opts_set(sp
, av
, NULL
);
279 if (silent
) { /* Ex batch mode option values. */
280 O_CLR(sp
, O_AUTOPRINT
);
282 O_CLR(sp
, O_VERBOSE
);
284 F_SET(sp
, SC_EX_SILENT
);
287 sp
->rows
= O_VAL(sp
, O_LINES
); /* Make ex formatting work. */
288 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
290 if (!silent
&& startup
) { /* Read EXINIT, exrc files. */
293 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
301 * List recovery files if -r specified without file arguments.
302 * Note, options must be initialized and startup information
303 * read before doing this.
305 if (flagchk
== 'r' && argv
[0] == NULL
) {
315 * Initialize the default ^D, ^U scrolling value here, after the
316 * user has had every opportunity to set the window option.
318 * It's historic practice that changing the value of the window
319 * option did not alter the default scrolling value, only giving
320 * a count to ^D/^U did that.
322 sp
->defscroll
= (O_VAL(sp
, O_WINDOW
) + 1) / 2;
325 * If we don't have a command-line option, switch into the right
326 * editor now, so that we position default files correctly, and
327 * so that any tags file file-already-locked messages are in the
328 * vi screen, not the ex screen.
331 * If we have a command-line option, the error message can end
332 * up in the wrong place, but I think that the combination is
335 if (gp
->c_option
== NULL
) {
336 F_CLR(sp
, SC_EX
| SC_VI
);
337 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
));
340 /* Open a tag file if specified. */
341 if (tag_f
!= NULL
&& ex_tag_first(sp
, tag_f
))
345 * Append any remaining arguments as file names. Files are recovery
346 * files if -r specified. If the tag option or ex startup commands
347 * loaded a file, then any file arguments are going to come after it.
350 if (sp
->frp
!= NULL
) {
351 /* Cheat -- we know we have an extra argv slot. */
353 *--argv
, char *, strlen(sp
->frp
->name
) + 1);
355 v_estr(gp
->progname
, errno
, NULL
);
358 (void)strcpy(*argv
, sp
->frp
->name
);
360 sp
->argv
= sp
->cargv
= argv
;
361 F_SET(sp
, SC_ARGNOFREE
);
363 F_SET(sp
, SC_ARGRECOVER
);
367 * If the ex startup commands and or/the tag option haven't already
368 * created a file, create one. If no command-line files were given,
369 * use a temporary file.
371 if (sp
->frp
== NULL
) {
372 if (sp
->argv
== NULL
) {
373 if ((frp
= file_add(sp
, NULL
)) == NULL
)
376 if ((frp
= file_add(sp
, (CHAR_T
*)sp
->argv
[0])) == NULL
)
378 if (F_ISSET(sp
, SC_ARGRECOVER
))
379 F_SET(frp
, FR_RECOVER
);
382 if (file_init(sp
, frp
, NULL
, 0))
384 if (EXCMD_RUNNING(wp
)) {
386 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
395 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
396 * was forced to initialize the screen during startup. We'd like to
397 * wait for a single character from the user, but we can't because
398 * we're not in raw mode. We can't switch to raw mode because the
399 * vi initialization will switch to xterm's alternate screen, causing
400 * us to lose the messages we're pausing to make sure the user read.
401 * So, wait for a complete line.
403 if (F_ISSET(sp
, SC_SCR_EX
)) {
404 p
= msg_cmsg(sp
, CMSG_CONT_R
, &len
);
405 (void)write(STDOUT_FILENO
, p
, len
);
407 if (v_event_get(sp
, &ev
, 0, 0))
409 if (ev
.e_event
== E_INTERRUPT
||
410 ev
.e_event
== E_CHARACTER
&&
411 (ev
.e_value
== K_CR
|| ev
.e_value
== K_NL
))
413 (void)gp
->scr_bell(sp
);
417 /* Switch into the right editor, regardless. */
418 F_CLR(sp
, SC_EX
| SC_VI
);
419 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
) | SC_STATUS_CNT
);
422 * Main edit loop. Vi handles split screens itself, we only return
423 * here when switching editor modes or restarting the screen.
426 if (F_ISSET(sp
, SC_EX
) ? ex(&sp
) : vi(&sp
))
438 * Convert historic arguments into something getopt(3) will like.
441 v_obsolete(name
, argv
)
448 * Translate old style arguments into something getopt will like.
449 * Make sure it's not text space memory, because ex modifies the
451 * Change "+" into "-c$".
452 * Change "+<anything else>" into "-c<anything else>".
453 * Change "-" into "-s"
454 * The c, T, t and w options take arguments so they can't be
457 * Stop if we find "--" as an argument, the user may want to edit
458 * a file named "+foo".
460 while (*++argv
&& strcmp(argv
[0], "--"))
461 if (argv
[0][0] == '+') {
462 if (argv
[0][1] == '\0') {
463 MALLOC_NOMSG(NULL
, argv
[0], char *, 4);
466 (void)strcpy(argv
[0], "-c$");
469 len
= strlen(argv
[0]);
470 MALLOC_NOMSG(NULL
, argv
[0], char *, len
+ 2);
475 (void)strcpy(argv
[0] + 2, p
+ 1);
477 } else if (argv
[0][0] == '-')
478 if (argv
[0][1] == '\0') {
479 MALLOC_NOMSG(NULL
, argv
[0], char *, 3);
480 if (argv
[0] == NULL
) {
481 nomem
: v_estr(name
, errno
, NULL
);
484 (void)strcpy(argv
[0], "-s");
486 if ((argv
[0][1] == 'c' || argv
[0][1] == 'T' ||
487 argv
[0][1] == 't' || argv
[0][1] == 'w') &&
501 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
502 v_estr(gp
->progname
, errno
, _PATH_TTY
);
506 (void)printf("process %lu waiting, enter <CR> to continue: ",
508 (void)fflush(stdout
);
511 if (read(fd
, &ch
, 1) != 1) {
515 } while (ch
!= '\n' && ch
!= '\r');
521 v_estr(name
, eno
, msg
)
525 (void)fprintf(stderr
, "%s", name
);
527 (void)fprintf(stderr
, ": %s", msg
);
529 (void)fprintf(stderr
, ": %s", strerror(errno
));
530 (void)fprintf(stderr
, "\n");