1 /* $NetBSD: main.c,v 1.6 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 static const char copyright
[] =
17 "%Z% Copyright (c) 1992, 1993, 1994\n\
18 The Regents of the University of California. All rights reserved.\n\
19 %Z% Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
20 Keith Bostic. All rights reserved.\n";
23 __RCSID("$NetBSD: main.c,v 1.6 2014/01/26 21:43:45 christos Exp $");
26 #include <sys/cdefs.h>
29 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 ";
32 __RCSID("$NetBSD: main.c,v 1.6 2014/01/26 21:43:45 christos Exp $");
35 #include <sys/types.h>
36 #include <sys/queue.h>
40 #include <bitstring.h>
51 #include "pathnames.h"
53 static void v_estr
__P((const char *, int, const char *));
54 static int v_obsolete
__P((char *, char *[]));
58 * Main editor routine.
60 * PUBLIC: int editor __P((WIN *, int, char *[]));
63 editor(WIN
*wp
, int argc
, char **argv
)
74 int ch
, flagchk
, lflag
, secure
, startup
, readonly
, rval
, silent
;
78 char *tag_f
, *wsizearg
, path
[256];
84 /* Initialize the busy routine, if not defined by the screen. */
85 if (gp
->scr_busy
== NULL
)
86 gp
->scr_busy
= vs_busy
;
87 /* Initialize the message routine, if not defined by the screen. */
88 if (wp
->scr_msg
== NULL
)
91 /* Set initial screen type and mode based on the program name. */
93 if (!strcmp(gp
->progname
, "ex") || !strcmp(gp
->progname
, "nex"))
96 /* Nview, view are readonly. */
97 if (!strcmp(gp
->progname
, "nview") ||
98 !strcmp(gp
->progname
, "view"))
101 /* Vi is the default. */
105 /* Convert old-style arguments into new-style ones. */
106 if (v_obsolete(gp
->progname
, argv
))
109 /* Parse the arguments. */
111 tag_f
= wsizearg
= NULL
;
112 lflag
= secure
= silent
= 0;
115 /* Set the file snapshot flag. */
116 F_SET(gp
, G_SNAPSHOT
);
118 while ((ch
= getopt(argc
, argv
, "c:"
126 "lRrSsT:t:vw:")) != EOF
)
128 case 'c': /* Run the command. */
131 * We should support multiple -c options.
133 if (gp
->c_option
!= NULL
) {
134 v_estr(gp
->progname
, 0,
135 "only one -c command may be specified.");
138 gp
->c_option
= optarg
;
150 v_estr(gp
->progname
, 0,
151 "usage: -D requires s or w argument.");
156 case 'e': /* Ex mode. */
160 case 'F': /* No snapshot. */
161 v_estr(gp
->progname
, 0,
162 "-F option no longer supported.");
164 case 'l': /* Set lisp, showmatch options. */
168 case 'G': /* gtags mode. */
172 case 'R': /* Readonly. */
175 case 'r': /* Recover. */
176 if (flagchk
== 't') {
177 v_estr(gp
->progname
, 0,
178 "only one of -r and -t may be specified.");
190 case 'T': /* Trace. */
191 (void)vtrace_init(optarg
);
195 if (flagchk
== 'r') {
196 v_estr(gp
->progname
, 0,
197 "only one of -r and -t may be specified.");
200 if (flagchk
== 't') {
201 v_estr(gp
->progname
, 0,
202 "only one tag file may be specified.");
208 case 'v': /* Vi mode. */
217 (void)gp
->scr_usage();
224 * -s option is only meaningful to ex.
226 * If not reading from a terminal, it's like -s was specified.
228 if (silent
&& !LF_ISSET(SC_EX
)) {
229 v_estr(gp
->progname
, 0, "-s option is only applicable to ex.");
232 if (LF_ISSET(SC_EX
) && F_ISSET(gp
, G_SCRIPTED
))
236 * Build and initialize the first/current screen. This is a bit
237 * tricky. If an error is returned, we may or may not have a
238 * screen structure. If we have a screen structure, put it on a
239 * display queue so that the error messages get displayed.
242 * Everything we do until we go interactive is done in ex mode.
244 if (screen_init(gp
, NULL
, &sp
)) {
246 TAILQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
252 TAILQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
255 if (v_key_init(sp
)) /* Special key initialization. */
258 { int oargs
[5], *oargp
= oargs
;
259 if (lflag
) { /* Command-line options. */
261 *oargp
++ = O_SHOWMATCH
;
264 *oargp
++ = O_READONLY
;
267 *oargp
++ = O_GTAGSMODE
;
271 *oargp
= -1; /* Options initialization. */
272 if (opts_init(sp
, oargs
))
275 if (wsizearg
!= NULL
) {
277 (void)snprintf(path
, sizeof(path
), "window=%s", wsizearg
);
278 a
.bp
= (CHAR_T
*)path
;
279 a
.len
= strlen(path
);
284 (void)opts_set(sp
, av
, NULL
);
286 if (silent
) { /* Ex batch mode option values. */
287 O_CLR(sp
, O_AUTOPRINT
);
289 O_CLR(sp
, O_VERBOSE
);
291 F_SET(sp
, SC_EX_SILENT
);
294 sp
->rows
= O_VAL(sp
, O_LINES
); /* Make ex formatting work. */
295 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
297 if (!silent
&& startup
) { /* Read EXINIT, exrc files. */
300 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
308 * List recovery files if -r specified without file arguments.
309 * Note, options must be initialized and startup information
310 * read before doing this.
312 if (flagchk
== 'r' && argv
[0] == NULL
) {
322 * Initialize the default ^D, ^U scrolling value here, after the
323 * user has had every opportunity to set the window option.
325 * It's historic practice that changing the value of the window
326 * option did not alter the default scrolling value, only giving
327 * a count to ^D/^U did that.
329 sp
->defscroll
= (O_VAL(sp
, O_WINDOW
) + 1) / 2;
332 * If we don't have a command-line option, switch into the right
333 * editor now, so that we position default files correctly, and
334 * so that any tags file file-already-locked messages are in the
335 * vi screen, not the ex screen.
338 * If we have a command-line option, the error message can end
339 * up in the wrong place, but I think that the combination is
342 if (gp
->c_option
== NULL
) {
343 F_CLR(sp
, SC_EX
| SC_VI
);
344 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
));
347 /* Open a tag file if specified. */
349 CHAR2INT(sp
, tag_f
, strlen(tag_f
) + 1, w
, wlen
);
350 if (ex_tag_first(sp
, w
))
355 * Append any remaining arguments as file names. Files are recovery
356 * files if -r specified. If the tag option or ex startup commands
357 * loaded a file, then any file arguments are going to come after it.
360 if (sp
->frp
!= NULL
) {
361 /* Cheat -- we know we have an extra argv slot. */
363 *--argv
, char *, strlen(sp
->frp
->name
) + 1);
365 v_estr(gp
->progname
, errno
, NULL
);
368 (void)strcpy(*argv
, sp
->frp
->name
);
370 sp
->argv
= sp
->cargv
= argv
;
371 F_SET(sp
, SC_ARGNOFREE
);
373 F_SET(sp
, SC_ARGRECOVER
);
377 * If the ex startup commands and or/the tag option haven't already
378 * created a file, create one. If no command-line files were given,
379 * use a temporary file.
381 if (sp
->frp
== NULL
) {
382 if (sp
->argv
== NULL
) {
383 if ((frp
= file_add(sp
, NULL
)) == NULL
)
386 if ((frp
= file_add(sp
, sp
->argv
[0])) == NULL
)
388 if (F_ISSET(sp
, SC_ARGRECOVER
))
389 F_SET(frp
, FR_RECOVER
);
392 if (file_init(sp
, frp
, NULL
, 0))
394 if (EXCMD_RUNNING(wp
)) {
396 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
405 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
406 * was forced to initialize the screen during startup. We'd like to
407 * wait for a single character from the user, but we can't because
408 * we're not in raw mode. We can't switch to raw mode because the
409 * vi initialization will switch to xterm's alternate screen, causing
410 * us to lose the messages we're pausing to make sure the user read.
411 * So, wait for a complete line.
413 if (F_ISSET(sp
, SC_SCR_EX
)) {
414 p
= msg_cmsg(sp
, CMSG_CONT_R
, &len
);
415 (void)write(STDOUT_FILENO
, p
, len
);
417 if (v_event_get(sp
, &ev
, 0, 0))
419 if (ev
.e_event
== E_INTERRUPT
||
420 (ev
.e_event
== E_CHARACTER
&&
421 (ev
.e_value
== K_CR
|| ev
.e_value
== K_NL
)))
423 (void)gp
->scr_bell(sp
);
427 /* Switch into the right editor, regardless. */
428 F_CLR(sp
, SC_EX
| SC_VI
);
429 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
) | SC_STATUS_CNT
);
432 * Main edit loop. Vi handles split screens itself, we only return
433 * here when switching editor modes or restarting the screen.
436 if (F_ISSET(sp
, SC_EX
) ? ex(&sp
) : vi(&sp
))
448 * Convert historic arguments into something getopt(3) will like.
451 v_obsolete(char *name
, char **argv
)
457 * Translate old style arguments into something getopt will like.
458 * Make sure it's not text space memory, because ex modifies the
460 * Change "+" into "-c$".
461 * Change "+<anything else>" into "-c<anything else>".
462 * Change "-" into "-s"
463 * The c, T, t and w options take arguments so they can't be
466 * Stop if we find "--" as an argument, the user may want to edit
467 * a file named "+foo".
469 while (*++argv
&& strcmp(argv
[0], "--"))
470 if (argv
[0][0] == '+') {
471 if (argv
[0][1] == '\0') {
472 MALLOC_NOMSG(NULL
, argv
[0], char *, 4);
475 (void)strcpy(argv
[0], "-c$");
478 len
= strlen(argv
[0]);
479 MALLOC_NOMSG(NULL
, argv
[0], char *, len
+ 2);
484 (void)strcpy(argv
[0] + 2, p
+ 1);
486 } else if (argv
[0][0] == '-') {
487 if (argv
[0][1] == '\0') {
488 MALLOC_NOMSG(NULL
, argv
[0], char *, 3);
489 if (argv
[0] == NULL
) {
490 nomem
: v_estr(name
, errno
, NULL
);
493 (void)strcpy(argv
[0], "-s");
495 if ((argv
[0][1] == 'c' || argv
[0][1] == 'T' ||
496 argv
[0][1] == 't' || argv
[0][1] == 'w') &&
510 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
511 v_estr(gp
->progname
, errno
, _PATH_TTY
);
515 (void)printf("process %lu waiting, enter <CR> to continue: ",
517 (void)fflush(stdout
);
520 if (read(fd
, &ch
, 1) != 1) {
524 } while (ch
!= '\n' && ch
!= '\r');
530 v_estr(const char *name
, int eno
, const char *msg
)
532 (void)fprintf(stderr
, "%s", name
);
534 (void)fprintf(stderr
, ": %s", msg
);
536 (void)fprintf(stderr
, ": %s", strerror(errno
));
537 (void)fprintf(stderr
, "\n");