remove unused global db environment
[nvi.git] / common / main.c
blob305a17747445b675eda3dfb0f4d426582db3c243
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.62 2001/11/01 10:28:25 skimo Exp $ (Berkeley) $Date: 2001/11/01 10:28:25 $";
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(WIN *wp, int argc, char **argv)
55 extern int optind;
56 extern char *optarg;
57 const char *p;
58 EVENT ev;
59 FREF *frp;
60 SCR *sp;
61 GS *gp;
62 size_t len;
63 u_int flags;
64 int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
65 char *tag_f, *wsizearg, path[256];
66 CHAR_T *w;
67 size_t wlen;
69 gp = wp->gp;
71 /* Initialize the busy routine, if not defined by the screen. */
72 if (gp->scr_busy == NULL)
73 gp->scr_busy = vs_busy;
74 /* Initialize the message routine, if not defined by the screen. */
75 if (wp->scr_msg == NULL)
76 wp->scr_msg = vs_msg;
78 /* Set initial screen type and mode based on the program name. */
79 readonly = 0;
80 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
81 LF_INIT(SC_EX);
82 else {
83 /* Nview, view are readonly. */
84 if (!strcmp(gp->progname, "nview") ||
85 !strcmp(gp->progname, "view"))
86 readonly = 1;
88 /* Vi is the default. */
89 LF_INIT(SC_VI);
92 /* Convert old-style arguments into new-style ones. */
93 if (v_obsolete(gp->progname, argv))
94 return (1);
96 /* Parse the arguments. */
97 flagchk = '\0';
98 tag_f = wsizearg = NULL;
99 lflag = secure = silent = 0;
100 startup = 1;
102 /* Set the file snapshot flag. */
103 F_SET(gp, G_SNAPSHOT);
105 #ifdef DEBUG
106 while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != EOF)
107 #else
108 while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != EOF)
109 #endif
110 switch (ch) {
111 case 'c': /* Run the command. */
113 * XXX
114 * We should support multiple -c options.
116 if (gp->c_option != NULL) {
117 v_estr(gp->progname, 0,
118 "only one -c command may be specified.");
119 return (1);
121 gp->c_option = optarg;
122 break;
123 #ifdef DEBUG
124 case 'D':
125 switch (optarg[0]) {
126 case 's':
127 startup = 0;
128 break;
129 case 'w':
130 attach(gp);
131 break;
132 default:
133 v_estr(gp->progname, 0,
134 "usage: -D requires s or w argument.");
135 return (1);
137 break;
138 #endif
139 case 'e': /* Ex mode. */
140 LF_CLR(SC_VI);
141 LF_SET(SC_EX);
142 break;
143 case 'F': /* No snapshot. */
144 F_CLR(gp, G_SNAPSHOT);
145 break;
146 case 'l': /* Set lisp, showmatch options. */
147 lflag = 1;
148 break;
149 case 'R': /* Readonly. */
150 readonly = 1;
151 break;
152 case 'r': /* Recover. */
153 if (flagchk == 't') {
154 v_estr(gp->progname, 0,
155 "only one of -r and -t may be specified.");
156 return (1);
158 flagchk = 'r';
159 break;
160 case 'S':
161 secure = 1;
162 break;
163 case 's':
164 silent = 1;
165 break;
166 #ifdef TRACE
167 case 'T': /* Trace. */
168 (void)vtrace_init(optarg);
169 break;
170 #endif
171 case 't': /* Tag. */
172 if (flagchk == 'r') {
173 v_estr(gp->progname, 0,
174 "only one of -r and -t may be specified.");
175 return (1);
177 if (flagchk == 't') {
178 v_estr(gp->progname, 0,
179 "only one tag file may be specified.");
180 return (1);
182 flagchk = 't';
183 tag_f = optarg;
184 break;
185 case 'v': /* Vi mode. */
186 LF_CLR(SC_EX);
187 LF_SET(SC_VI);
188 break;
189 case 'w':
190 wsizearg = optarg;
191 break;
192 case '?':
193 default:
194 (void)gp->scr_usage();
195 return (1);
197 argc -= optind;
198 argv += optind;
201 * -s option is only meaningful to ex.
203 * If not reading from a terminal, it's like -s was specified.
205 if (silent && !LF_ISSET(SC_EX)) {
206 v_estr(gp->progname, 0, "-s option is only applicable to ex.");
207 goto err;
209 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
210 silent = 1;
213 * Build and initialize the first/current screen. This is a bit
214 * tricky. If an error is returned, we may or may not have a
215 * screen structure. If we have a screen structure, put it on a
216 * display queue so that the error messages get displayed.
218 * !!!
219 * Everything we do until we go interactive is done in ex mode.
221 if (screen_init(gp, NULL, &sp)) {
222 if (sp != NULL) {
223 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
224 sp->wp = wp;
226 goto err;
228 F_SET(sp, SC_EX);
229 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
230 sp->wp = wp;
232 if (v_key_init(sp)) /* Special key initialization. */
233 goto err;
235 { int oargs[5], *oargp = oargs;
236 if (lflag) { /* Command-line options. */
237 *oargp++ = O_LISP;
238 *oargp++ = O_SHOWMATCH;
240 if (readonly)
241 *oargp++ = O_READONLY;
242 if (secure)
243 *oargp++ = O_SECURE;
244 *oargp = -1; /* Options initialization. */
245 if (opts_init(sp, oargs))
246 goto err;
248 if (wsizearg != NULL) {
249 ARGS *av[2], a, b;
250 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
251 a.bp = (CHAR_T *)path;
252 a.len = strlen(path);
253 b.bp = NULL;
254 b.len = 0;
255 av[0] = &a;
256 av[1] = &b;
257 (void)opts_set(sp, av, NULL);
259 if (silent) { /* Ex batch mode option values. */
260 O_CLR(sp, O_AUTOPRINT);
261 O_CLR(sp, O_PROMPT);
262 O_CLR(sp, O_VERBOSE);
263 O_CLR(sp, O_WARN);
264 F_SET(sp, SC_EX_SILENT);
267 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
268 sp->cols = O_VAL(sp, O_COLUMNS);
270 if (!silent && startup) { /* Read EXINIT, exrc files. */
271 if (ex_exrc(sp))
272 goto err;
273 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
274 if (screen_end(sp))
275 goto err;
276 goto done;
281 * List recovery files if -r specified without file arguments.
282 * Note, options must be initialized and startup information
283 * read before doing this.
285 if (flagchk == 'r' && argv[0] == NULL) {
286 if (rcv_list(sp))
287 goto err;
288 if (screen_end(sp))
289 goto err;
290 goto done;
294 * !!!
295 * Initialize the default ^D, ^U scrolling value here, after the
296 * user has had every opportunity to set the window option.
298 * It's historic practice that changing the value of the window
299 * option did not alter the default scrolling value, only giving
300 * a count to ^D/^U did that.
302 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
305 * If we don't have a command-line option, switch into the right
306 * editor now, so that we position default files correctly, and
307 * so that any tags file file-already-locked messages are in the
308 * vi screen, not the ex screen.
310 * XXX
311 * If we have a command-line option, the error message can end
312 * up in the wrong place, but I think that the combination is
313 * unlikely.
315 if (gp->c_option == NULL) {
316 F_CLR(sp, SC_EX | SC_VI);
317 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
320 /* Open a tag file if specified. */
321 if (tag_f != NULL) {
322 CHAR2INT(sp, tag_f, strlen(tag_f) + 1, w, wlen);
323 if (ex_tag_first(sp, w))
324 goto err;
328 * Append any remaining arguments as file names. Files are recovery
329 * files if -r specified. If the tag option or ex startup commands
330 * loaded a file, then any file arguments are going to come after it.
332 if (*argv != NULL) {
333 if (sp->frp != NULL) {
334 /* Cheat -- we know we have an extra argv slot. */
335 MALLOC_NOMSG(sp,
336 *--argv, char *, strlen(sp->frp->name) + 1);
337 if (*argv == NULL) {
338 v_estr(gp->progname, errno, NULL);
339 goto err;
341 (void)strcpy(*argv, sp->frp->name);
343 sp->argv = sp->cargv = argv;
344 F_SET(sp, SC_ARGNOFREE);
345 if (flagchk == 'r')
346 F_SET(sp, SC_ARGRECOVER);
350 * If the ex startup commands and or/the tag option haven't already
351 * created a file, create one. If no command-line files were given,
352 * use a temporary file.
354 if (sp->frp == NULL) {
355 if (sp->argv == NULL) {
356 if ((frp = file_add(sp, NULL)) == NULL)
357 goto err;
358 } else {
359 if ((frp = file_add(sp, sp->argv[0])) == NULL)
360 goto err;
361 if (F_ISSET(sp, SC_ARGRECOVER))
362 F_SET(frp, FR_RECOVER);
365 if (file_init(sp, frp, NULL, 0))
366 goto err;
367 if (EXCMD_RUNNING(wp)) {
368 (void)ex_cmd(sp);
369 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
370 if (screen_end(sp))
371 goto err;
372 goto done;
378 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
379 * was forced to initialize the screen during startup. We'd like to
380 * wait for a single character from the user, but we can't because
381 * we're not in raw mode. We can't switch to raw mode because the
382 * vi initialization will switch to xterm's alternate screen, causing
383 * us to lose the messages we're pausing to make sure the user read.
384 * So, wait for a complete line.
386 if (F_ISSET(sp, SC_SCR_EX)) {
387 p = msg_cmsg(sp, CMSG_CONT_R, &len);
388 (void)write(STDOUT_FILENO, p, len);
389 for (;;) {
390 if (v_event_get(sp, &ev, 0, 0))
391 goto err;
392 if (ev.e_event == E_INTERRUPT ||
393 (ev.e_event == E_CHARACTER &&
394 (ev.e_value == K_CR || ev.e_value == K_NL)))
395 break;
396 (void)gp->scr_bell(sp);
400 /* Switch into the right editor, regardless. */
401 F_CLR(sp, SC_EX | SC_VI);
402 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
405 * Main edit loop. Vi handles split screens itself, we only return
406 * here when switching editor modes or restarting the screen.
408 while (sp != NULL)
409 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
410 goto err;
412 done: rval = 0;
413 if (0)
414 err: rval = 1;
416 return (rval);
420 * v_obsolete --
421 * Convert historic arguments into something getopt(3) will like.
423 static int
424 v_obsolete(char *name, char **argv)
426 size_t len;
427 char *p;
430 * Translate old style arguments into something getopt will like.
431 * Make sure it's not text space memory, because ex modifies the
432 * strings.
433 * Change "+" into "-c$".
434 * Change "+<anything else>" into "-c<anything else>".
435 * Change "-" into "-s"
436 * The c, T, t and w options take arguments so they can't be
437 * special arguments.
439 * Stop if we find "--" as an argument, the user may want to edit
440 * a file named "+foo".
442 while (*++argv && strcmp(argv[0], "--"))
443 if (argv[0][0] == '+') {
444 if (argv[0][1] == '\0') {
445 MALLOC_NOMSG(NULL, argv[0], char *, 4);
446 if (argv[0] == NULL)
447 goto nomem;
448 (void)strcpy(argv[0], "-c$");
449 } else {
450 p = argv[0];
451 len = strlen(argv[0]);
452 MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
453 if (argv[0] == NULL)
454 goto nomem;
455 argv[0][0] = '-';
456 argv[0][1] = 'c';
457 (void)strcpy(argv[0] + 2, p + 1);
459 } else if (argv[0][0] == '-') {
460 if (argv[0][1] == '\0') {
461 MALLOC_NOMSG(NULL, argv[0], char *, 3);
462 if (argv[0] == NULL) {
463 nomem: v_estr(name, errno, NULL);
464 return (1);
466 (void)strcpy(argv[0], "-s");
467 } else
468 if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
469 argv[0][1] == 't' || argv[0][1] == 'w') &&
470 argv[0][2] == '\0')
471 ++argv;
473 return (0);
476 #ifdef DEBUG
477 static void
478 attach(GS *gp)
480 int fd;
481 char ch;
483 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
484 v_estr(gp->progname, errno, _PATH_TTY);
485 return;
488 (void)printf("process %lu waiting, enter <CR> to continue: ",
489 (u_long)getpid());
490 (void)fflush(stdout);
492 do {
493 if (read(fd, &ch, 1) != 1) {
494 (void)close(fd);
495 return;
497 } while (ch != '\n' && ch != '\r');
498 (void)close(fd);
500 #endif
502 static void
503 v_estr(char *name, int eno, char *msg)
505 (void)fprintf(stderr, "%s", name);
506 if (msg != NULL)
507 (void)fprintf(stderr, ": %s", msg);
508 if (eno)
509 (void)fprintf(stderr, ": %s", strerror(errno));
510 (void)fprintf(stderr, "\n");