1 /* $NetBSD: main.c,v 1.5 2013/12/01 02:34:54 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.
14 static const char copyright
[] =
15 "%Z% Copyright (c) 1992, 1993, 1994\n\
16 The Regents of the University of California. All rights reserved.\n\
17 %Z% Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
18 Keith Bostic. All rights reserved.\n";
22 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 ";
25 #include <sys/types.h>
26 #include <sys/queue.h>
30 #include <bitstring.h>
41 #include "pathnames.h"
43 static void v_estr
__P((const char *, int, const 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
;
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 (wp
->scr_msg
== NULL
)
81 /* Set initial screen type and mode based on the program name. */
83 if (!strcmp(gp
->progname
, "ex") || !strcmp(gp
->progname
, "nex"))
86 /* Nview, view are readonly. */
87 if (!strcmp(gp
->progname
, "nview") ||
88 !strcmp(gp
->progname
, "view"))
91 /* Vi is the default. */
95 /* Convert old-style arguments into new-style ones. */
96 if (v_obsolete(gp
->progname
, argv
))
99 /* Parse the arguments. */
101 tag_f
= wsizearg
= NULL
;
102 lflag
= secure
= silent
= 0;
105 /* Set the file snapshot flag. */
106 F_SET(gp
, G_SNAPSHOT
);
108 while ((ch
= getopt(argc
, argv
, "c:"
116 "lRrSsT:t:vw:")) != EOF
)
118 case 'c': /* Run the command. */
121 * We should support multiple -c options.
123 if (gp
->c_option
!= NULL
) {
124 v_estr(gp
->progname
, 0,
125 "only one -c command may be specified.");
128 gp
->c_option
= optarg
;
140 v_estr(gp
->progname
, 0,
141 "usage: -D requires s or w argument.");
146 case 'e': /* Ex mode. */
150 case 'F': /* No snapshot. */
151 v_estr(gp
->progname
, 0,
152 "-F option no longer supported.");
154 case 'l': /* Set lisp, showmatch options. */
158 case 'G': /* gtags mode. */
162 case 'R': /* Readonly. */
165 case 'r': /* Recover. */
166 if (flagchk
== 't') {
167 v_estr(gp
->progname
, 0,
168 "only one of -r and -t may be specified.");
180 case 'T': /* Trace. */
181 (void)vtrace_init(optarg
);
185 if (flagchk
== 'r') {
186 v_estr(gp
->progname
, 0,
187 "only one of -r and -t may be specified.");
190 if (flagchk
== 't') {
191 v_estr(gp
->progname
, 0,
192 "only one tag file may be specified.");
198 case 'v': /* Vi mode. */
207 (void)gp
->scr_usage();
214 * -s option is only meaningful to ex.
216 * If not reading from a terminal, it's like -s was specified.
218 if (silent
&& !LF_ISSET(SC_EX
)) {
219 v_estr(gp
->progname
, 0, "-s option is only applicable to ex.");
222 if (LF_ISSET(SC_EX
) && F_ISSET(gp
, G_SCRIPTED
))
226 * Build and initialize the first/current screen. This is a bit
227 * tricky. If an error is returned, we may or may not have a
228 * screen structure. If we have a screen structure, put it on a
229 * display queue so that the error messages get displayed.
232 * Everything we do until we go interactive is done in ex mode.
234 if (screen_init(gp
, NULL
, &sp
)) {
236 TAILQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
242 TAILQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
245 if (v_key_init(sp
)) /* Special key initialization. */
248 { int oargs
[5], *oargp
= oargs
;
249 if (lflag
) { /* Command-line options. */
251 *oargp
++ = O_SHOWMATCH
;
254 *oargp
++ = O_READONLY
;
257 *oargp
++ = O_GTAGSMODE
;
261 *oargp
= -1; /* Options initialization. */
262 if (opts_init(sp
, oargs
))
265 if (wsizearg
!= NULL
) {
267 (void)snprintf(path
, sizeof(path
), "window=%s", wsizearg
);
268 a
.bp
= (CHAR_T
*)path
;
269 a
.len
= strlen(path
);
274 (void)opts_set(sp
, av
, NULL
);
276 if (silent
) { /* Ex batch mode option values. */
277 O_CLR(sp
, O_AUTOPRINT
);
279 O_CLR(sp
, O_VERBOSE
);
281 F_SET(sp
, SC_EX_SILENT
);
284 sp
->rows
= O_VAL(sp
, O_LINES
); /* Make ex formatting work. */
285 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
287 if (!silent
&& startup
) { /* Read EXINIT, exrc files. */
290 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
298 * List recovery files if -r specified without file arguments.
299 * Note, options must be initialized and startup information
300 * read before doing this.
302 if (flagchk
== 'r' && argv
[0] == NULL
) {
312 * Initialize the default ^D, ^U scrolling value here, after the
313 * user has had every opportunity to set the window option.
315 * It's historic practice that changing the value of the window
316 * option did not alter the default scrolling value, only giving
317 * a count to ^D/^U did that.
319 sp
->defscroll
= (O_VAL(sp
, O_WINDOW
) + 1) / 2;
322 * If we don't have a command-line option, switch into the right
323 * editor now, so that we position default files correctly, and
324 * so that any tags file file-already-locked messages are in the
325 * vi screen, not the ex screen.
328 * If we have a command-line option, the error message can end
329 * up in the wrong place, but I think that the combination is
332 if (gp
->c_option
== NULL
) {
333 F_CLR(sp
, SC_EX
| SC_VI
);
334 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
));
337 /* Open a tag file if specified. */
339 CHAR2INT(sp
, tag_f
, strlen(tag_f
) + 1, w
, wlen
);
340 if (ex_tag_first(sp
, w
))
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
, 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(char *name
, char **argv
)
447 * Translate old style arguments into something getopt will like.
448 * Make sure it's not text space memory, because ex modifies the
450 * Change "+" into "-c$".
451 * Change "+<anything else>" into "-c<anything else>".
452 * Change "-" into "-s"
453 * The c, T, t and w options take arguments so they can't be
456 * Stop if we find "--" as an argument, the user may want to edit
457 * a file named "+foo".
459 while (*++argv
&& strcmp(argv
[0], "--"))
460 if (argv
[0][0] == '+') {
461 if (argv
[0][1] == '\0') {
462 MALLOC_NOMSG(NULL
, argv
[0], char *, 4);
465 (void)strcpy(argv
[0], "-c$");
468 len
= strlen(argv
[0]);
469 MALLOC_NOMSG(NULL
, argv
[0], char *, len
+ 2);
474 (void)strcpy(argv
[0] + 2, p
+ 1);
476 } else if (argv
[0][0] == '-') {
477 if (argv
[0][1] == '\0') {
478 MALLOC_NOMSG(NULL
, argv
[0], char *, 3);
479 if (argv
[0] == NULL
) {
480 nomem
: v_estr(name
, errno
, NULL
);
483 (void)strcpy(argv
[0], "-s");
485 if ((argv
[0][1] == 'c' || argv
[0][1] == 'T' ||
486 argv
[0][1] == 't' || argv
[0][1] == 'w') &&
500 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
501 v_estr(gp
->progname
, errno
, _PATH_TTY
);
505 (void)printf("process %lu waiting, enter <CR> to continue: ",
507 (void)fflush(stdout
);
510 if (read(fd
, &ch
, 1) != 1) {
514 } while (ch
!= '\n' && ch
!= '\r');
520 v_estr(const char *name
, int eno
, const char *msg
)
522 (void)fprintf(stderr
, "%s", name
);
524 (void)fprintf(stderr
, ": %s", msg
);
526 (void)fprintf(stderr
, ": %s", strerror(errno
));
527 (void)fprintf(stderr
, "\n");