*** empty log message ***
[nvi.git] / common / main.c
blob0c1a75d972576c2fac4b040299cb73be99e4dfec
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.55 2000/07/07 22:29:13 skimo Exp $ (Berkeley) $Date: 2000/07/07 22:29:13 $";
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];
70 gp = wp->gp;
72 /* Initialize the busy routine, if not defined by the screen. */
73 if (gp->scr_busy == NULL)
74 gp->scr_busy = vs_busy;
75 /* Initialize the message routine, if not defined by the screen. */
76 if (gp->scr_msg == NULL)
77 gp->scr_msg = vs_msg;
79 /* Common global structure initialization. */
80 CIRCLEQ_INIT(&gp->hq);
81 gp->noprint = DEFAULT_NOPRINT;
83 /* Structures shared by screens so stored in the GS structure. */
84 CIRCLEQ_INIT(&gp->frefq);
85 CIRCLEQ_INIT(&gp->exfq);
86 CIRCLEQ_INIT(&gp->dcb_store.textq);
87 LIST_INIT(&gp->cutq);
88 LIST_INIT(&gp->seqq);
90 /* Set initial screen type and mode based on the program name. */
91 readonly = 0;
92 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
93 LF_INIT(SC_EX);
94 else {
95 /* Nview, view are readonly. */
96 if (!strcmp(gp->progname, "nview") ||
97 !strcmp(gp->progname, "view"))
98 readonly = 1;
100 /* Vi is the default. */
101 LF_INIT(SC_VI);
104 /* Convert old-style arguments into new-style ones. */
105 if (v_obsolete(gp->progname, argv))
106 return (1);
108 /* Parse the arguments. */
109 flagchk = '\0';
110 tag_f = wsizearg = NULL;
111 lflag = secure = silent = 0;
112 startup = 1;
114 /* Set the file snapshot flag. */
115 F_SET(gp, G_SNAPSHOT);
117 #ifdef DEBUG
118 while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != EOF)
119 #else
120 while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != EOF)
121 #endif
122 switch (ch) {
123 case 'c': /* Run the command. */
125 * XXX
126 * We should support multiple -c options.
128 if (gp->c_option != NULL) {
129 v_estr(gp->progname, 0,
130 "only one -c command may be specified.");
131 return (1);
133 gp->c_option = optarg;
134 break;
135 #ifdef DEBUG
136 case 'D':
137 switch (optarg[0]) {
138 case 's':
139 startup = 0;
140 break;
141 case 'w':
142 attach(gp);
143 break;
144 default:
145 v_estr(gp->progname, 0,
146 "usage: -D requires s or w argument.");
147 return (1);
149 break;
150 #endif
151 case 'e': /* Ex mode. */
152 LF_CLR(SC_VI);
153 LF_SET(SC_EX);
154 break;
155 case 'F': /* No snapshot. */
156 F_CLR(gp, G_SNAPSHOT);
157 break;
158 case 'l': /* Set lisp, showmatch options. */
159 lflag = 1;
160 break;
161 case 'R': /* Readonly. */
162 readonly = 1;
163 break;
164 case 'r': /* Recover. */
165 if (flagchk == 't') {
166 v_estr(gp->progname, 0,
167 "only one of -r and -t may be specified.");
168 return (1);
170 flagchk = 'r';
171 break;
172 case 'S':
173 secure = 1;
174 break;
175 case 's':
176 silent = 1;
177 break;
178 #ifdef TRACE
179 case 'T': /* Trace. */
180 (void)vtrace_init(optarg);
181 break;
182 #endif
183 case 't': /* Tag. */
184 if (flagchk == 'r') {
185 v_estr(gp->progname, 0,
186 "only one of -r and -t may be specified.");
187 return (1);
189 if (flagchk == 't') {
190 v_estr(gp->progname, 0,
191 "only one tag file may be specified.");
192 return (1);
194 flagchk = 't';
195 tag_f = optarg;
196 break;
197 case 'v': /* Vi mode. */
198 LF_CLR(SC_EX);
199 LF_SET(SC_VI);
200 break;
201 case 'w':
202 wsizearg = optarg;
203 break;
204 case '?':
205 default:
206 (void)gp->scr_usage();
207 return (1);
209 argc -= optind;
210 argv += optind;
213 * -s option is only meaningful to ex.
215 * If not reading from a terminal, it's like -s was specified.
217 if (silent && !LF_ISSET(SC_EX)) {
218 v_estr(gp->progname, 0, "-s option is only applicable to ex.");
219 goto err;
221 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
222 silent = 1;
224 if (db_env_create(&gp->env, 0)) {
225 v_estr(gp->progname, 0, "Unable to create DB environment.");
226 goto err;
228 gp->env->set_errfile(gp->env, stderr);
229 gp->env->open(gp->env, NULL, 0, 0);
230 gp->env = NULL;
233 * Build and initialize the first/current screen. This is a bit
234 * tricky. If an error is returned, we may or may not have a
235 * screen structure. If we have a screen structure, put it on a
236 * display queue so that the error messages get displayed.
238 * !!!
239 * Everything we do until we go interactive is done in ex mode.
241 if (screen_init(gp, NULL, &sp)) {
242 if (sp != NULL) {
243 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
244 sp->wp = wp;
246 goto err;
248 F_SET(sp, SC_EX);
249 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
250 sp->wp = wp;
252 if (v_key_init(sp)) /* Special key initialization. */
253 goto err;
255 { int oargs[5], *oargp = oargs;
256 if (lflag) { /* Command-line options. */
257 *oargp++ = O_LISP;
258 *oargp++ = O_SHOWMATCH;
260 if (readonly)
261 *oargp++ = O_READONLY;
262 if (secure)
263 *oargp++ = O_SECURE;
264 *oargp = -1; /* Options initialization. */
265 if (opts_init(sp, oargs))
266 goto err;
268 if (wsizearg != NULL) {
269 ARGS *av[2], a, b;
270 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
271 a.bp = (CHAR_T *)path;
272 a.len = strlen(path);
273 b.bp = NULL;
274 b.len = 0;
275 av[0] = &a;
276 av[1] = &b;
277 (void)opts_set(sp, av, NULL);
279 if (silent) { /* Ex batch mode option values. */
280 O_CLR(sp, O_AUTOPRINT);
281 O_CLR(sp, O_PROMPT);
282 O_CLR(sp, O_VERBOSE);
283 O_CLR(sp, O_WARN);
284 F_SET(sp, SC_EX_SILENT);
287 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
288 sp->cols = O_VAL(sp, O_COLUMNS);
290 if (!silent && startup) { /* Read EXINIT, exrc files. */
291 if (ex_exrc(sp))
292 goto err;
293 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
294 if (screen_end(sp))
295 goto err;
296 goto done;
301 * List recovery files if -r specified without file arguments.
302 * Note, options must be initialized and startup information
303 * read before doing this.
305 if (flagchk == 'r' && argv[0] == NULL) {
306 if (rcv_list(sp))
307 goto err;
308 if (screen_end(sp))
309 goto err;
310 goto done;
314 * !!!
315 * Initialize the default ^D, ^U scrolling value here, after the
316 * user has had every opportunity to set the window option.
318 * It's historic practice that changing the value of the window
319 * option did not alter the default scrolling value, only giving
320 * a count to ^D/^U did that.
322 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
325 * If we don't have a command-line option, switch into the right
326 * editor now, so that we position default files correctly, and
327 * so that any tags file file-already-locked messages are in the
328 * vi screen, not the ex screen.
330 * XXX
331 * If we have a command-line option, the error message can end
332 * up in the wrong place, but I think that the combination is
333 * unlikely.
335 if (gp->c_option == NULL) {
336 F_CLR(sp, SC_EX | SC_VI);
337 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
340 /* Open a tag file if specified. */
341 if (tag_f != NULL && ex_tag_first(sp, tag_f))
342 goto err;
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.
349 if (*argv != NULL) {
350 if (sp->frp != NULL) {
351 /* Cheat -- we know we have an extra argv slot. */
352 MALLOC_NOMSG(sp,
353 *--argv, char *, strlen(sp->frp->name) + 1);
354 if (*argv == NULL) {
355 v_estr(gp->progname, errno, NULL);
356 goto err;
358 (void)strcpy(*argv, sp->frp->name);
360 sp->argv = sp->cargv = argv;
361 F_SET(sp, SC_ARGNOFREE);
362 if (flagchk == 'r')
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)
374 goto err;
375 } else {
376 if ((frp = file_add(sp, (CHAR_T *)sp->argv[0])) == NULL)
377 goto err;
378 if (F_ISSET(sp, SC_ARGRECOVER))
379 F_SET(frp, FR_RECOVER);
382 if (file_init(sp, frp, NULL, 0))
383 goto err;
384 if (EXCMD_RUNNING(wp)) {
385 (void)ex_cmd(sp);
386 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
387 if (screen_end(sp))
388 goto err;
389 goto done;
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);
406 for (;;) {
407 if (v_event_get(sp, &ev, 0, 0))
408 goto err;
409 if (ev.e_event == E_INTERRUPT ||
410 ev.e_event == E_CHARACTER &&
411 (ev.e_value == K_CR || ev.e_value == K_NL))
412 break;
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.
425 while (sp != NULL)
426 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
427 goto err;
429 done: rval = 0;
430 if (0)
431 err: rval = 1;
433 return (rval);
437 * v_obsolete --
438 * Convert historic arguments into something getopt(3) will like.
440 static int
441 v_obsolete(name, argv)
442 char *name, *argv[];
444 size_t len;
445 char *p;
448 * Translate old style arguments into something getopt will like.
449 * Make sure it's not text space memory, because ex modifies the
450 * strings.
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
455 * special arguments.
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);
464 if (argv[0] == NULL)
465 goto nomem;
466 (void)strcpy(argv[0], "-c$");
467 } else {
468 p = argv[0];
469 len = strlen(argv[0]);
470 MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
471 if (argv[0] == NULL)
472 goto nomem;
473 argv[0][0] = '-';
474 argv[0][1] = 'c';
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);
482 return (1);
484 (void)strcpy(argv[0], "-s");
485 } else
486 if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
487 argv[0][1] == 't' || argv[0][1] == 'w') &&
488 argv[0][2] == '\0')
489 ++argv;
490 return (0);
493 #ifdef DEBUG
494 static void
495 attach(gp)
496 GS *gp;
498 int fd;
499 char ch;
501 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
502 v_estr(gp->progname, errno, _PATH_TTY);
503 return;
506 (void)printf("process %lu waiting, enter <CR> to continue: ",
507 (u_long)getpid());
508 (void)fflush(stdout);
510 do {
511 if (read(fd, &ch, 1) != 1) {
512 (void)close(fd);
513 return;
515 } while (ch != '\n' && ch != '\r');
516 (void)close(fd);
518 #endif
520 static void
521 v_estr(name, eno, msg)
522 char *name, *msg;
523 int eno;
525 (void)fprintf(stderr, "%s", name);
526 if (msg != NULL)
527 (void)fprintf(stderr, ": %s", msg);
528 if (eno)
529 (void)fprintf(stderr, ": %s", strerror(errno));
530 (void)fprintf(stderr, "\n");