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.56 2000/07/14 14:29:16 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:16 $";
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];
74 /* Initialize the busy routine, if not defined by the screen. */
75 if (gp
->scr_busy
== NULL
)
76 gp
->scr_busy
= vs_busy
;
77 /* Initialize the message routine, if not defined by the screen. */
78 if (gp
->scr_msg
== NULL
)
81 /* Common global structure initialization. */
82 CIRCLEQ_INIT(&gp
->hq
);
83 gp
->noprint
= DEFAULT_NOPRINT
;
85 /* Structures shared by screens so stored in the GS structure. */
86 CIRCLEQ_INIT(&gp
->frefq
);
87 CIRCLEQ_INIT(&gp
->exfq
);
88 CIRCLEQ_INIT(&gp
->dcb_store
.textq
);
92 /* Set initial screen type and mode based on the program name. */
94 if (!strcmp(gp
->progname
, "ex") || !strcmp(gp
->progname
, "nex"))
97 /* Nview, view are readonly. */
98 if (!strcmp(gp
->progname
, "nview") ||
99 !strcmp(gp
->progname
, "view"))
102 /* Vi is the default. */
106 /* Convert old-style arguments into new-style ones. */
107 if (v_obsolete(gp
->progname
, argv
))
110 /* Parse the arguments. */
112 tag_f
= wsizearg
= NULL
;
113 lflag
= secure
= silent
= 0;
116 /* Set the file snapshot flag. */
117 F_SET(gp
, G_SNAPSHOT
);
120 while ((ch
= getopt(argc
, argv
, "c:D:eFlRrSsT:t:vw:")) != EOF
)
122 while ((ch
= getopt(argc
, argv
, "c:eFlRrSst:vw:")) != EOF
)
125 case 'c': /* Run the command. */
128 * We should support multiple -c options.
130 if (gp
->c_option
!= NULL
) {
131 v_estr(gp
->progname
, 0,
132 "only one -c command may be specified.");
135 gp
->c_option
= optarg
;
147 v_estr(gp
->progname
, 0,
148 "usage: -D requires s or w argument.");
153 case 'e': /* Ex mode. */
157 case 'F': /* No snapshot. */
158 F_CLR(gp
, G_SNAPSHOT
);
160 case 'l': /* Set lisp, showmatch options. */
163 case 'R': /* Readonly. */
166 case 'r': /* Recover. */
167 if (flagchk
== 't') {
168 v_estr(gp
->progname
, 0,
169 "only one of -r and -t may be specified.");
181 case 'T': /* Trace. */
182 (void)vtrace_init(optarg
);
186 if (flagchk
== 'r') {
187 v_estr(gp
->progname
, 0,
188 "only one of -r and -t may be specified.");
191 if (flagchk
== 't') {
192 v_estr(gp
->progname
, 0,
193 "only one tag file may be specified.");
199 case 'v': /* Vi mode. */
208 (void)gp
->scr_usage();
215 * -s option is only meaningful to ex.
217 * If not reading from a terminal, it's like -s was specified.
219 if (silent
&& !LF_ISSET(SC_EX
)) {
220 v_estr(gp
->progname
, 0, "-s option is only applicable to ex.");
223 if (LF_ISSET(SC_EX
) && F_ISSET(gp
, G_SCRIPTED
))
226 if (db_env_create(&gp
->env
, 0)) {
227 v_estr(gp
->progname
, 0, "Unable to create DB environment.");
230 gp
->env
->set_errfile(gp
->env
, stderr
);
231 gp
->env
->open(gp
->env
, NULL
, 0, 0);
235 * Build and initialize the first/current screen. This is a bit
236 * tricky. If an error is returned, we may or may not have a
237 * screen structure. If we have a screen structure, put it on a
238 * display queue so that the error messages get displayed.
241 * Everything we do until we go interactive is done in ex mode.
243 if (screen_init(gp
, NULL
, &sp
)) {
245 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
251 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
254 if (v_key_init(sp
)) /* Special key initialization. */
257 { int oargs
[5], *oargp
= oargs
;
258 if (lflag
) { /* Command-line options. */
260 *oargp
++ = O_SHOWMATCH
;
263 *oargp
++ = O_READONLY
;
266 *oargp
= -1; /* Options initialization. */
267 if (opts_init(sp
, oargs
))
270 if (wsizearg
!= NULL
) {
272 (void)snprintf(path
, sizeof(path
), "window=%s", wsizearg
);
273 a
.bp
= (CHAR_T
*)path
;
274 a
.len
= strlen(path
);
279 (void)opts_set(sp
, av
, NULL
);
281 if (silent
) { /* Ex batch mode option values. */
282 O_CLR(sp
, O_AUTOPRINT
);
284 O_CLR(sp
, O_VERBOSE
);
286 F_SET(sp
, SC_EX_SILENT
);
289 sp
->rows
= O_VAL(sp
, O_LINES
); /* Make ex formatting work. */
290 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
292 if (!silent
&& startup
) { /* Read EXINIT, exrc files. */
295 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
303 * List recovery files if -r specified without file arguments.
304 * Note, options must be initialized and startup information
305 * read before doing this.
307 if (flagchk
== 'r' && argv
[0] == NULL
) {
317 * Initialize the default ^D, ^U scrolling value here, after the
318 * user has had every opportunity to set the window option.
320 * It's historic practice that changing the value of the window
321 * option did not alter the default scrolling value, only giving
322 * a count to ^D/^U did that.
324 sp
->defscroll
= (O_VAL(sp
, O_WINDOW
) + 1) / 2;
327 * If we don't have a command-line option, switch into the right
328 * editor now, so that we position default files correctly, and
329 * so that any tags file file-already-locked messages are in the
330 * vi screen, not the ex screen.
333 * If we have a command-line option, the error message can end
334 * up in the wrong place, but I think that the combination is
337 if (gp
->c_option
== NULL
) {
338 F_CLR(sp
, SC_EX
| SC_VI
);
339 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
));
342 /* Open a tag file if specified. */
344 CHAR2INT(sp
, tag_f
, strlen(tag_f
) + 1, w
, wlen
);
345 if (ex_tag_first(sp
, w
))
350 * Append any remaining arguments as file names. Files are recovery
351 * files if -r specified. If the tag option or ex startup commands
352 * loaded a file, then any file arguments are going to come after it.
355 if (sp
->frp
!= NULL
) {
356 /* Cheat -- we know we have an extra argv slot. */
358 *--argv
, char *, strlen(sp
->frp
->name
) + 1);
360 v_estr(gp
->progname
, errno
, NULL
);
363 (void)strcpy(*argv
, sp
->frp
->name
);
365 sp
->argv
= sp
->cargv
= argv
;
366 F_SET(sp
, SC_ARGNOFREE
);
368 F_SET(sp
, SC_ARGRECOVER
);
372 * If the ex startup commands and or/the tag option haven't already
373 * created a file, create one. If no command-line files were given,
374 * use a temporary file.
376 if (sp
->frp
== NULL
) {
377 if (sp
->argv
== NULL
) {
378 if ((frp
= file_add(sp
, NULL
)) == NULL
)
381 if ((frp
= file_add(sp
, sp
->argv
[0])) == NULL
)
383 if (F_ISSET(sp
, SC_ARGRECOVER
))
384 F_SET(frp
, FR_RECOVER
);
387 if (file_init(sp
, frp
, NULL
, 0))
389 if (EXCMD_RUNNING(wp
)) {
391 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
400 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
401 * was forced to initialize the screen during startup. We'd like to
402 * wait for a single character from the user, but we can't because
403 * we're not in raw mode. We can't switch to raw mode because the
404 * vi initialization will switch to xterm's alternate screen, causing
405 * us to lose the messages we're pausing to make sure the user read.
406 * So, wait for a complete line.
408 if (F_ISSET(sp
, SC_SCR_EX
)) {
409 p
= msg_cmsg(sp
, CMSG_CONT_R
, &len
);
410 (void)write(STDOUT_FILENO
, p
, len
);
412 if (v_event_get(sp
, &ev
, 0, 0))
414 if (ev
.e_event
== E_INTERRUPT
||
415 ev
.e_event
== E_CHARACTER
&&
416 (ev
.e_value
== K_CR
|| ev
.e_value
== K_NL
))
418 (void)gp
->scr_bell(sp
);
422 /* Switch into the right editor, regardless. */
423 F_CLR(sp
, SC_EX
| SC_VI
);
424 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
) | SC_STATUS_CNT
);
427 * Main edit loop. Vi handles split screens itself, we only return
428 * here when switching editor modes or restarting the screen.
431 if (F_ISSET(sp
, SC_EX
) ? ex(&sp
) : vi(&sp
))
443 * Convert historic arguments into something getopt(3) will like.
446 v_obsolete(name
, argv
)
453 * Translate old style arguments into something getopt will like.
454 * Make sure it's not text space memory, because ex modifies the
456 * Change "+" into "-c$".
457 * Change "+<anything else>" into "-c<anything else>".
458 * Change "-" into "-s"
459 * The c, T, t and w options take arguments so they can't be
462 * Stop if we find "--" as an argument, the user may want to edit
463 * a file named "+foo".
465 while (*++argv
&& strcmp(argv
[0], "--"))
466 if (argv
[0][0] == '+') {
467 if (argv
[0][1] == '\0') {
468 MALLOC_NOMSG(NULL
, argv
[0], char *, 4);
471 (void)strcpy(argv
[0], "-c$");
474 len
= strlen(argv
[0]);
475 MALLOC_NOMSG(NULL
, argv
[0], char *, len
+ 2);
480 (void)strcpy(argv
[0] + 2, p
+ 1);
482 } else if (argv
[0][0] == '-')
483 if (argv
[0][1] == '\0') {
484 MALLOC_NOMSG(NULL
, argv
[0], char *, 3);
485 if (argv
[0] == NULL
) {
486 nomem
: v_estr(name
, errno
, NULL
);
489 (void)strcpy(argv
[0], "-s");
491 if ((argv
[0][1] == 'c' || argv
[0][1] == 'T' ||
492 argv
[0][1] == 't' || argv
[0][1] == 'w') &&
506 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
507 v_estr(gp
->progname
, errno
, _PATH_TTY
);
511 (void)printf("process %lu waiting, enter <CR> to continue: ",
513 (void)fflush(stdout
);
516 if (read(fd
, &ch
, 1) != 1) {
520 } while (ch
!= '\n' && ch
!= '\r');
526 v_estr(name
, eno
, msg
)
530 (void)fprintf(stderr
, "%s", name
);
532 (void)fprintf(stderr
, ": %s", msg
);
534 (void)fprintf(stderr
, ": %s", strerror(errno
));
535 (void)fprintf(stderr
, "\n");