Disable line cache again and add a comment as to why it is
[nvi.git] / common / main.c
blobc1b6ceb8a00d496c646f485273e5e6992e7d9e39
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
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";
18 #endif /* not lint */
20 #ifndef lint
21 static const char sccsid[] = "$Id: main.c,v 10.59 2000/07/21 22:09:28 skimo Exp $ (Berkeley) $Date: 2000/07/21 22:09:28 $";
22 #endif /* not lint */
24 #include <sys/types.h>
25 #include <sys/queue.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
29 #include <bitstring.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "common.h"
39 #include "../vi/vi.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 *[]));
47 * editor --
48 * Main editor routine.
50 * PUBLIC: int editor __P((WIN *, int, char *[]));
52 int
53 editor(wp, argc, argv)
54 WIN *wp;
55 int argc;
56 char *argv[];
58 extern int optind;
59 extern char *optarg;
60 const char *p;
61 EVENT ev;
62 FREF *frp;
63 SCR *sp;
64 GS *gp;
65 size_t len;
66 u_int flags;
67 int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
68 char *tag_f, *wsizearg, path[256];
69 CHAR_T *w;
70 size_t wlen;
72 gp = wp->gp;
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)
79 gp->scr_msg = vs_msg;
81 /* Set initial screen type and mode based on the program name. */
82 readonly = 0;
83 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
84 LF_INIT(SC_EX);
85 else {
86 /* Nview, view are readonly. */
87 if (!strcmp(gp->progname, "nview") ||
88 !strcmp(gp->progname, "view"))
89 readonly = 1;
91 /* Vi is the default. */
92 LF_INIT(SC_VI);
95 /* Convert old-style arguments into new-style ones. */
96 if (v_obsolete(gp->progname, argv))
97 return (1);
99 /* Parse the arguments. */
100 flagchk = '\0';
101 tag_f = wsizearg = NULL;
102 lflag = secure = silent = 0;
103 startup = 1;
105 /* Set the file snapshot flag. */
106 F_SET(gp, G_SNAPSHOT);
108 #ifdef DEBUG
109 while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != EOF)
110 #else
111 while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != EOF)
112 #endif
113 switch (ch) {
114 case 'c': /* Run the command. */
116 * XXX
117 * We should support multiple -c options.
119 if (gp->c_option != NULL) {
120 v_estr(gp->progname, 0,
121 "only one -c command may be specified.");
122 return (1);
124 gp->c_option = optarg;
125 break;
126 #ifdef DEBUG
127 case 'D':
128 switch (optarg[0]) {
129 case 's':
130 startup = 0;
131 break;
132 case 'w':
133 attach(gp);
134 break;
135 default:
136 v_estr(gp->progname, 0,
137 "usage: -D requires s or w argument.");
138 return (1);
140 break;
141 #endif
142 case 'e': /* Ex mode. */
143 LF_CLR(SC_VI);
144 LF_SET(SC_EX);
145 break;
146 case 'F': /* No snapshot. */
147 F_CLR(gp, G_SNAPSHOT);
148 break;
149 case 'l': /* Set lisp, showmatch options. */
150 lflag = 1;
151 break;
152 case 'R': /* Readonly. */
153 readonly = 1;
154 break;
155 case 'r': /* Recover. */
156 if (flagchk == 't') {
157 v_estr(gp->progname, 0,
158 "only one of -r and -t may be specified.");
159 return (1);
161 flagchk = 'r';
162 break;
163 case 'S':
164 secure = 1;
165 break;
166 case 's':
167 silent = 1;
168 break;
169 #ifdef TRACE
170 case 'T': /* Trace. */
171 (void)vtrace_init(optarg);
172 break;
173 #endif
174 case 't': /* Tag. */
175 if (flagchk == 'r') {
176 v_estr(gp->progname, 0,
177 "only one of -r and -t may be specified.");
178 return (1);
180 if (flagchk == 't') {
181 v_estr(gp->progname, 0,
182 "only one tag file may be specified.");
183 return (1);
185 flagchk = 't';
186 tag_f = optarg;
187 break;
188 case 'v': /* Vi mode. */
189 LF_CLR(SC_EX);
190 LF_SET(SC_VI);
191 break;
192 case 'w':
193 wsizearg = optarg;
194 break;
195 case '?':
196 default:
197 (void)gp->scr_usage();
198 return (1);
200 argc -= optind;
201 argv += optind;
204 * -s option is only meaningful to ex.
206 * If not reading from a terminal, it's like -s was specified.
208 if (silent && !LF_ISSET(SC_EX)) {
209 v_estr(gp->progname, 0, "-s option is only applicable to ex.");
210 goto err;
212 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
213 silent = 1;
216 if (db_env_create(&gp->env, 0)) {
217 v_estr(gp->progname, 0, "Unable to create DB environment.");
218 goto err;
220 gp->env->set_errfile(gp->env, stderr);
221 gp->env->open(gp->env, NULL, DB_CREATE | DB_INIT_MPOOL, 0);
223 gp->env = 0;
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.
231 * !!!
232 * Everything we do until we go interactive is done in ex mode.
234 if (screen_init(gp, NULL, &sp)) {
235 if (sp != NULL) {
236 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
237 sp->wp = wp;
239 goto err;
241 F_SET(sp, SC_EX);
242 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
243 sp->wp = wp;
245 if (v_key_init(sp)) /* Special key initialization. */
246 goto err;
248 { int oargs[5], *oargp = oargs;
249 if (lflag) { /* Command-line options. */
250 *oargp++ = O_LISP;
251 *oargp++ = O_SHOWMATCH;
253 if (readonly)
254 *oargp++ = O_READONLY;
255 if (secure)
256 *oargp++ = O_SECURE;
257 *oargp = -1; /* Options initialization. */
258 if (opts_init(sp, oargs))
259 goto err;
261 if (wsizearg != NULL) {
262 ARGS *av[2], a, b;
263 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
264 a.bp = (CHAR_T *)path;
265 a.len = strlen(path);
266 b.bp = NULL;
267 b.len = 0;
268 av[0] = &a;
269 av[1] = &b;
270 (void)opts_set(sp, av, NULL);
272 if (silent) { /* Ex batch mode option values. */
273 O_CLR(sp, O_AUTOPRINT);
274 O_CLR(sp, O_PROMPT);
275 O_CLR(sp, O_VERBOSE);
276 O_CLR(sp, O_WARN);
277 F_SET(sp, SC_EX_SILENT);
280 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
281 sp->cols = O_VAL(sp, O_COLUMNS);
283 if (!silent && startup) { /* Read EXINIT, exrc files. */
284 if (ex_exrc(sp))
285 goto err;
286 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
287 if (screen_end(sp))
288 goto err;
289 goto done;
294 * List recovery files if -r specified without file arguments.
295 * Note, options must be initialized and startup information
296 * read before doing this.
298 if (flagchk == 'r' && argv[0] == NULL) {
299 if (rcv_list(sp))
300 goto err;
301 if (screen_end(sp))
302 goto err;
303 goto done;
307 * !!!
308 * Initialize the default ^D, ^U scrolling value here, after the
309 * user has had every opportunity to set the window option.
311 * It's historic practice that changing the value of the window
312 * option did not alter the default scrolling value, only giving
313 * a count to ^D/^U did that.
315 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
318 * If we don't have a command-line option, switch into the right
319 * editor now, so that we position default files correctly, and
320 * so that any tags file file-already-locked messages are in the
321 * vi screen, not the ex screen.
323 * XXX
324 * If we have a command-line option, the error message can end
325 * up in the wrong place, but I think that the combination is
326 * unlikely.
328 if (gp->c_option == NULL) {
329 F_CLR(sp, SC_EX | SC_VI);
330 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
333 /* Open a tag file if specified. */
334 if (tag_f != NULL) {
335 CHAR2INT(sp, tag_f, strlen(tag_f) + 1, w, wlen);
336 if (ex_tag_first(sp, w))
337 goto err;
341 * Append any remaining arguments as file names. Files are recovery
342 * files if -r specified. If the tag option or ex startup commands
343 * loaded a file, then any file arguments are going to come after it.
345 if (*argv != NULL) {
346 if (sp->frp != NULL) {
347 /* Cheat -- we know we have an extra argv slot. */
348 MALLOC_NOMSG(sp,
349 *--argv, char *, strlen(sp->frp->name) + 1);
350 if (*argv == NULL) {
351 v_estr(gp->progname, errno, NULL);
352 goto err;
354 (void)strcpy(*argv, sp->frp->name);
356 sp->argv = sp->cargv = argv;
357 F_SET(sp, SC_ARGNOFREE);
358 if (flagchk == 'r')
359 F_SET(sp, SC_ARGRECOVER);
363 * If the ex startup commands and or/the tag option haven't already
364 * created a file, create one. If no command-line files were given,
365 * use a temporary file.
367 if (sp->frp == NULL) {
368 if (sp->argv == NULL) {
369 if ((frp = file_add(sp, NULL)) == NULL)
370 goto err;
371 } else {
372 if ((frp = file_add(sp, sp->argv[0])) == NULL)
373 goto err;
374 if (F_ISSET(sp, SC_ARGRECOVER))
375 F_SET(frp, FR_RECOVER);
378 if (file_init(sp, frp, NULL, 0))
379 goto err;
380 if (EXCMD_RUNNING(wp)) {
381 (void)ex_cmd(sp);
382 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
383 if (screen_end(sp))
384 goto err;
385 goto done;
391 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
392 * was forced to initialize the screen during startup. We'd like to
393 * wait for a single character from the user, but we can't because
394 * we're not in raw mode. We can't switch to raw mode because the
395 * vi initialization will switch to xterm's alternate screen, causing
396 * us to lose the messages we're pausing to make sure the user read.
397 * So, wait for a complete line.
399 if (F_ISSET(sp, SC_SCR_EX)) {
400 p = msg_cmsg(sp, CMSG_CONT_R, &len);
401 (void)write(STDOUT_FILENO, p, len);
402 for (;;) {
403 if (v_event_get(sp, &ev, 0, 0))
404 goto err;
405 if (ev.e_event == E_INTERRUPT ||
406 ev.e_event == E_CHARACTER &&
407 (ev.e_value == K_CR || ev.e_value == K_NL))
408 break;
409 (void)gp->scr_bell(sp);
413 /* Switch into the right editor, regardless. */
414 F_CLR(sp, SC_EX | SC_VI);
415 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
418 * Main edit loop. Vi handles split screens itself, we only return
419 * here when switching editor modes or restarting the screen.
421 while (sp != NULL)
422 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
423 goto err;
425 done: rval = 0;
426 if (0)
427 err: rval = 1;
429 return (rval);
433 * v_obsolete --
434 * Convert historic arguments into something getopt(3) will like.
436 static int
437 v_obsolete(name, argv)
438 char *name, *argv[];
440 size_t len;
441 char *p;
444 * Translate old style arguments into something getopt will like.
445 * Make sure it's not text space memory, because ex modifies the
446 * strings.
447 * Change "+" into "-c$".
448 * Change "+<anything else>" into "-c<anything else>".
449 * Change "-" into "-s"
450 * The c, T, t and w options take arguments so they can't be
451 * special arguments.
453 * Stop if we find "--" as an argument, the user may want to edit
454 * a file named "+foo".
456 while (*++argv && strcmp(argv[0], "--"))
457 if (argv[0][0] == '+') {
458 if (argv[0][1] == '\0') {
459 MALLOC_NOMSG(NULL, argv[0], char *, 4);
460 if (argv[0] == NULL)
461 goto nomem;
462 (void)strcpy(argv[0], "-c$");
463 } else {
464 p = argv[0];
465 len = strlen(argv[0]);
466 MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
467 if (argv[0] == NULL)
468 goto nomem;
469 argv[0][0] = '-';
470 argv[0][1] = 'c';
471 (void)strcpy(argv[0] + 2, p + 1);
473 } else if (argv[0][0] == '-')
474 if (argv[0][1] == '\0') {
475 MALLOC_NOMSG(NULL, argv[0], char *, 3);
476 if (argv[0] == NULL) {
477 nomem: v_estr(name, errno, NULL);
478 return (1);
480 (void)strcpy(argv[0], "-s");
481 } else
482 if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
483 argv[0][1] == 't' || argv[0][1] == 'w') &&
484 argv[0][2] == '\0')
485 ++argv;
486 return (0);
489 #ifdef DEBUG
490 static void
491 attach(gp)
492 GS *gp;
494 int fd;
495 char ch;
497 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
498 v_estr(gp->progname, errno, _PATH_TTY);
499 return;
502 (void)printf("process %lu waiting, enter <CR> to continue: ",
503 (u_long)getpid());
504 (void)fflush(stdout);
506 do {
507 if (read(fd, &ch, 1) != 1) {
508 (void)close(fd);
509 return;
511 } while (ch != '\n' && ch != '\r');
512 (void)close(fd);
514 #endif
516 static void
517 v_estr(name, eno, msg)
518 char *name, *msg;
519 int eno;
521 (void)fprintf(stderr, "%s", name);
522 if (msg != NULL)
523 (void)fprintf(stderr, ": %s", msg);
524 if (eno)
525 (void)fprintf(stderr, ": %s", strerror(errno));
526 (void)fprintf(stderr, "\n");