1 /* $NetBSD: main.c,v 1.2 2008/10/29 17:50:49 christos Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char copyright
[] =
16 "%Z% Copyright (c) 1992, 1993, 1994\n\
17 The Regents of the University of California. All rights reserved.\n\
18 %Z% Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
19 Keith Bostic. All rights reserved.\n";
23 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";
26 #include <sys/types.h>
27 #include <sys/queue.h>
31 #include <bitstring.h>
42 #include "pathnames.h"
44 static void v_estr
__P((const char *, int, const char *));
45 static int v_obsolete
__P((char *, char *[]));
49 * Main editor routine.
51 * PUBLIC: int editor __P((WIN *, int, char *[]));
54 editor(WIN
*wp
, int argc
, char **argv
)
65 int ch
, flagchk
, lflag
, secure
, startup
, readonly
, rval
, silent
;
69 char *tag_f
, *wsizearg
, path
[256];
75 /* Initialize the busy routine, if not defined by the screen. */
76 if (gp
->scr_busy
== NULL
)
77 gp
->scr_busy
= vs_busy
;
78 /* Initialize the message routine, if not defined by the screen. */
79 if (wp
->scr_msg
== NULL
)
82 /* Set initial screen type and mode based on the program name. */
84 if (!strcmp(gp
->progname
, "ex") || !strcmp(gp
->progname
, "nex"))
87 /* Nview, view are readonly. */
88 if (!strcmp(gp
->progname
, "nview") ||
89 !strcmp(gp
->progname
, "view"))
92 /* Vi is the default. */
96 /* Convert old-style arguments into new-style ones. */
97 if (v_obsolete(gp
->progname
, argv
))
100 /* Parse the arguments. */
102 tag_f
= wsizearg
= NULL
;
103 lflag
= secure
= silent
= 0;
106 /* Set the file snapshot flag. */
107 F_SET(gp
, G_SNAPSHOT
);
109 while ((ch
= getopt(argc
, argv
, "c:"
117 "lRrSsT:t:vw:")) != EOF
)
119 case 'c': /* Run the command. */
122 * We should support multiple -c options.
124 if (gp
->c_option
!= NULL
) {
125 v_estr(gp
->progname
, 0,
126 "only one -c command may be specified.");
129 gp
->c_option
= optarg
;
141 v_estr(gp
->progname
, 0,
142 "usage: -D requires s or w argument.");
147 case 'e': /* Ex mode. */
151 case 'F': /* No snapshot. */
152 v_estr(gp
->progname
, 0,
153 "-F option no longer supported.");
155 case 'l': /* Set lisp, showmatch options. */
159 case 'G': /* gtags mode. */
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
))
227 * Build and initialize the first/current screen. This is a bit
228 * tricky. If an error is returned, we may or may not have a
229 * screen structure. If we have a screen structure, put it on a
230 * display queue so that the error messages get displayed.
233 * Everything we do until we go interactive is done in ex mode.
235 if (screen_init(gp
, NULL
, &sp
)) {
237 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
243 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, sp
, q
);
246 if (v_key_init(sp
)) /* Special key initialization. */
249 { int oargs
[5], *oargp
= oargs
;
250 if (lflag
) { /* Command-line options. */
252 *oargp
++ = O_SHOWMATCH
;
255 *oargp
++ = O_READONLY
;
258 *oargp
++ = O_GTAGSMODE
;
262 *oargp
= -1; /* Options initialization. */
263 if (opts_init(sp
, oargs
))
266 if (wsizearg
!= NULL
) {
268 (void)snprintf(path
, sizeof(path
), "window=%s", wsizearg
);
269 a
.bp
= (CHAR_T
*)path
;
270 a
.len
= strlen(path
);
275 (void)opts_set(sp
, av
, NULL
);
277 if (silent
) { /* Ex batch mode option values. */
278 O_CLR(sp
, O_AUTOPRINT
);
280 O_CLR(sp
, O_VERBOSE
);
282 F_SET(sp
, SC_EX_SILENT
);
285 sp
->rows
= O_VAL(sp
, O_LINES
); /* Make ex formatting work. */
286 sp
->cols
= O_VAL(sp
, O_COLUMNS
);
288 if (!silent
&& startup
) { /* Read EXINIT, exrc files. */
291 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
299 * List recovery files if -r specified without file arguments.
300 * Note, options must be initialized and startup information
301 * read before doing this.
303 if (flagchk
== 'r' && argv
[0] == NULL
) {
313 * Initialize the default ^D, ^U scrolling value here, after the
314 * user has had every opportunity to set the window option.
316 * It's historic practice that changing the value of the window
317 * option did not alter the default scrolling value, only giving
318 * a count to ^D/^U did that.
320 sp
->defscroll
= (O_VAL(sp
, O_WINDOW
) + 1) / 2;
323 * If we don't have a command-line option, switch into the right
324 * editor now, so that we position default files correctly, and
325 * so that any tags file file-already-locked messages are in the
326 * vi screen, not the ex screen.
329 * If we have a command-line option, the error message can end
330 * up in the wrong place, but I think that the combination is
333 if (gp
->c_option
== NULL
) {
334 F_CLR(sp
, SC_EX
| SC_VI
);
335 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
));
338 /* Open a tag file if specified. */
340 CHAR2INT(sp
, tag_f
, strlen(tag_f
) + 1, w
, wlen
);
341 if (ex_tag_first(sp
, w
))
346 * Append any remaining arguments as file names. Files are recovery
347 * files if -r specified. If the tag option or ex startup commands
348 * loaded a file, then any file arguments are going to come after it.
351 if (sp
->frp
!= NULL
) {
352 /* Cheat -- we know we have an extra argv slot. */
354 *--argv
, char *, strlen(sp
->frp
->name
) + 1);
356 v_estr(gp
->progname
, errno
, NULL
);
359 (void)strcpy(*argv
, sp
->frp
->name
);
361 sp
->argv
= sp
->cargv
= argv
;
362 F_SET(sp
, SC_ARGNOFREE
);
364 F_SET(sp
, SC_ARGRECOVER
);
368 * If the ex startup commands and or/the tag option haven't already
369 * created a file, create one. If no command-line files were given,
370 * use a temporary file.
372 if (sp
->frp
== NULL
) {
373 if (sp
->argv
== NULL
) {
374 if ((frp
= file_add(sp
, NULL
)) == NULL
)
377 if ((frp
= file_add(sp
, sp
->argv
[0])) == NULL
)
379 if (F_ISSET(sp
, SC_ARGRECOVER
))
380 F_SET(frp
, FR_RECOVER
);
383 if (file_init(sp
, frp
, NULL
, 0))
385 if (EXCMD_RUNNING(wp
)) {
387 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
396 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
397 * was forced to initialize the screen during startup. We'd like to
398 * wait for a single character from the user, but we can't because
399 * we're not in raw mode. We can't switch to raw mode because the
400 * vi initialization will switch to xterm's alternate screen, causing
401 * us to lose the messages we're pausing to make sure the user read.
402 * So, wait for a complete line.
404 if (F_ISSET(sp
, SC_SCR_EX
)) {
405 p
= msg_cmsg(sp
, CMSG_CONT_R
, &len
);
406 (void)write(STDOUT_FILENO
, p
, len
);
408 if (v_event_get(sp
, &ev
, 0, 0))
410 if (ev
.e_event
== E_INTERRUPT
||
411 (ev
.e_event
== E_CHARACTER
&&
412 (ev
.e_value
== K_CR
|| ev
.e_value
== K_NL
)))
414 (void)gp
->scr_bell(sp
);
418 /* Switch into the right editor, regardless. */
419 F_CLR(sp
, SC_EX
| SC_VI
);
420 F_SET(sp
, LF_ISSET(SC_EX
| SC_VI
) | SC_STATUS_CNT
);
423 * Main edit loop. Vi handles split screens itself, we only return
424 * here when switching editor modes or restarting the screen.
427 if (F_ISSET(sp
, SC_EX
) ? ex(&sp
) : vi(&sp
))
439 * Convert historic arguments into something getopt(3) will like.
442 v_obsolete(char *name
, char **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(const char *name
, int eno
, const char *msg
)
523 (void)fprintf(stderr
, "%s", name
);
525 (void)fprintf(stderr
, ": %s", msg
);
527 (void)fprintf(stderr
, ": %s", strerror(errno
));
528 (void)fprintf(stderr
, "\n");