add VI_SEL_END, VI_SEL_START, SI_SELECT
[nvi.git] / common / main.c
blobc63fca7993cca9457499065a5b7f8e5382c699c4
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.50 1996/12/17 14:51:28 bostic Exp $ (Berkeley) $Date: 1996/12/17 14:51: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((GS *, int, char *[]));
52 int
53 editor(gp, argc, argv)
54 GS *gp;
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 size_t len;
65 u_int flags;
66 int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
67 char *tag_f, *wsizearg, path[256];
69 /* Initialize the busy routine, if not defined by the screen. */
70 if (gp->scr_busy == NULL)
71 gp->scr_busy = vs_busy;
72 /* Initialize the message routine, if not defined by the screen. */
73 if (gp->scr_msg == NULL)
74 gp->scr_msg = vs_msg;
76 /* Common global structure initialization. */
77 CIRCLEQ_INIT(&gp->dq);
78 CIRCLEQ_INIT(&gp->hq);
79 LIST_INIT(&gp->ecq);
80 LIST_INSERT_HEAD(&gp->ecq, &gp->excmd, q);
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->dcb_store.textq);
86 LIST_INIT(&gp->cutq);
87 LIST_INIT(&gp->seqq);
89 /* Set initial screen type and mode based on the program name. */
90 readonly = 0;
91 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
92 LF_INIT(SC_EX);
93 else {
94 /* Nview, view are readonly. */
95 if (!strcmp(gp->progname, "nview") ||
96 !strcmp(gp->progname, "view"))
97 readonly = 1;
99 /* Vi is the default. */
100 LF_INIT(SC_VI);
103 /* Convert old-style arguments into new-style ones. */
104 if (v_obsolete(gp->progname, argv))
105 return (1);
107 /* Parse the arguments. */
108 flagchk = '\0';
109 tag_f = wsizearg = NULL;
110 lflag = secure = silent = 0;
111 startup = 1;
113 /* Set the file snapshot flag. */
114 F_SET(gp, G_SNAPSHOT);
116 #ifdef DEBUG
117 while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != EOF)
118 #else
119 while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != EOF)
120 #endif
121 switch (ch) {
122 case 'c': /* Run the command. */
124 * XXX
125 * We should support multiple -c options.
127 if (gp->c_option != NULL) {
128 v_estr(gp->progname, 0,
129 "only one -c command may be specified.");
130 return (1);
132 gp->c_option = optarg;
133 break;
134 #ifdef DEBUG
135 case 'D':
136 switch (optarg[0]) {
137 case 's':
138 startup = 0;
139 break;
140 case 'w':
141 attach(gp);
142 break;
143 default:
144 v_estr(gp->progname, 0,
145 "usage: -D requires s or w argument.");
146 return (1);
148 break;
149 #endif
150 case 'e': /* Ex mode. */
151 LF_CLR(SC_VI);
152 LF_SET(SC_EX);
153 break;
154 case 'F': /* No snapshot. */
155 F_CLR(gp, G_SNAPSHOT);
156 break;
157 case 'l': /* Set lisp, showmatch options. */
158 lflag = 1;
159 break;
160 case 'R': /* Readonly. */
161 readonly = 1;
162 break;
163 case 'r': /* Recover. */
164 if (flagchk == 't') {
165 v_estr(gp->progname, 0,
166 "only one of -r and -t may be specified.");
167 return (1);
169 flagchk = 'r';
170 break;
171 case 'S':
172 secure = 1;
173 break;
174 case 's':
175 silent = 1;
176 break;
177 #ifdef TRACE
178 case 'T': /* Trace. */
179 (void)vtrace_init(optarg);
180 break;
181 #endif
182 case 't': /* Tag. */
183 if (flagchk == 'r') {
184 v_estr(gp->progname, 0,
185 "only one of -r and -t may be specified.");
186 return (1);
188 if (flagchk == 't') {
189 v_estr(gp->progname, 0,
190 "only one tag file may be specified.");
191 return (1);
193 flagchk = 't';
194 tag_f = optarg;
195 break;
196 case 'v': /* Vi mode. */
197 LF_CLR(SC_EX);
198 LF_SET(SC_VI);
199 break;
200 case 'w':
201 wsizearg = optarg;
202 break;
203 case '?':
204 default:
205 (void)gp->scr_usage();
206 return (1);
208 argc -= optind;
209 argv += optind;
212 * -s option is only meaningful to ex.
214 * If not reading from a terminal, it's like -s was specified.
216 if (silent && !LF_ISSET(SC_EX)) {
217 v_estr(gp->progname, 0, "-s option is only applicable to ex.");
218 goto err;
220 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
221 silent = 1;
224 * Build and initialize the first/current screen. This is a bit
225 * tricky. If an error is returned, we may or may not have a
226 * screen structure. If we have a screen structure, put it on a
227 * display queue so that the error messages get displayed.
229 * !!!
230 * Everything we do until we go interactive is done in ex mode.
232 if (screen_init(gp, NULL, &sp)) {
233 if (sp != NULL)
234 CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
235 goto err;
237 F_SET(sp, SC_EX);
238 CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q);
240 if (v_key_init(sp)) /* Special key initialization. */
241 goto err;
243 { int oargs[5], *oargp = oargs;
244 if (lflag) { /* Command-line options. */
245 *oargp++ = O_LISP;
246 *oargp++ = O_SHOWMATCH;
248 if (readonly)
249 *oargp++ = O_READONLY;
250 if (secure)
251 *oargp++ = O_SECURE;
252 *oargp = -1; /* Options initialization. */
253 if (opts_init(sp, oargs))
254 goto err;
256 if (wsizearg != NULL) {
257 ARGS *av[2], a, b;
258 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
259 a.bp = (CHAR_T *)path;
260 a.len = strlen(path);
261 b.bp = NULL;
262 b.len = 0;
263 av[0] = &a;
264 av[1] = &b;
265 (void)opts_set(sp, av, NULL);
267 if (silent) { /* Ex batch mode option values. */
268 O_CLR(sp, O_AUTOPRINT);
269 O_CLR(sp, O_PROMPT);
270 O_CLR(sp, O_VERBOSE);
271 O_CLR(sp, O_WARN);
272 F_SET(sp, SC_EX_SILENT);
275 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
276 sp->cols = O_VAL(sp, O_COLUMNS);
278 if (!silent && startup) { /* Read EXINIT, exrc files. */
279 if (ex_exrc(sp))
280 goto err;
281 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
282 if (screen_end(sp))
283 goto err;
284 goto done;
289 * List recovery files if -r specified without file arguments.
290 * Note, options must be initialized and startup information
291 * read before doing this.
293 if (flagchk == 'r' && argv[0] == NULL) {
294 if (rcv_list(sp))
295 goto err;
296 if (screen_end(sp))
297 goto err;
298 goto done;
302 * !!!
303 * Initialize the default ^D, ^U scrolling value here, after the
304 * user has had every opportunity to set the window option.
306 * It's historic practice that changing the value of the window
307 * option did not alter the default scrolling value, only giving
308 * a count to ^D/^U did that.
310 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
313 * If we don't have a command-line option, switch into the right
314 * editor now, so that we position default files correctly, and
315 * so that any tags file file-already-locked messages are in the
316 * vi screen, not the ex screen.
318 * XXX
319 * If we have a command-line option, the error message can end
320 * up in the wrong place, but I think that the combination is
321 * unlikely.
323 if (gp->c_option == NULL) {
324 F_CLR(sp, SC_EX | SC_VI);
325 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
328 /* Open a tag file if specified. */
329 if (tag_f != NULL && ex_tag_first(sp, tag_f))
330 goto err;
333 * Append any remaining arguments as file names. Files are recovery
334 * files if -r specified. If the tag option or ex startup commands
335 * loaded a file, then any file arguments are going to come after it.
337 if (*argv != NULL) {
338 if (sp->frp != NULL) {
339 /* Cheat -- we know we have an extra argv slot. */
340 MALLOC_NOMSG(sp,
341 *--argv, char *, strlen(sp->frp->name) + 1);
342 if (*argv == NULL) {
343 v_estr(gp->progname, errno, NULL);
344 goto err;
346 (void)strcpy(*argv, sp->frp->name);
348 sp->argv = sp->cargv = argv;
349 F_SET(sp, SC_ARGNOFREE);
350 if (flagchk == 'r')
351 F_SET(sp, SC_ARGRECOVER);
355 * If the ex startup commands and or/the tag option haven't already
356 * created a file, create one. If no command-line files were given,
357 * use a temporary file.
359 if (sp->frp == NULL) {
360 if (sp->argv == NULL) {
361 if ((frp = file_add(sp, NULL)) == NULL)
362 goto err;
363 } else {
364 if ((frp = file_add(sp, (CHAR_T *)sp->argv[0])) == NULL)
365 goto err;
366 if (F_ISSET(sp, SC_ARGRECOVER))
367 F_SET(frp, FR_RECOVER);
370 if (file_init(sp, frp, NULL, 0))
371 goto err;
372 if (EXCMD_RUNNING(gp)) {
373 (void)ex_cmd(sp);
374 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
375 if (screen_end(sp))
376 goto err;
377 goto done;
383 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
384 * was forced to initialize the screen during startup. We'd like to
385 * wait for a single character from the user, but we can't because
386 * we're not in raw mode. We can't switch to raw mode because the
387 * vi initialization will switch to xterm's alternate screen, causing
388 * us to lose the messages we're pausing to make sure the user read.
389 * So, wait for a complete line.
391 if (F_ISSET(sp, SC_SCR_EX)) {
392 p = msg_cmsg(sp, CMSG_CONT_R, &len);
393 (void)write(STDOUT_FILENO, p, len);
394 for (;;) {
395 if (v_event_get(sp, &ev, 0, 0))
396 goto err;
397 if (ev.e_event == E_INTERRUPT ||
398 ev.e_event == E_CHARACTER &&
399 (ev.e_value == K_CR || ev.e_value == K_NL))
400 break;
401 (void)gp->scr_bell(sp);
405 /* Switch into the right editor, regardless. */
406 F_CLR(sp, SC_EX | SC_VI);
407 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
410 * Main edit loop. Vi handles split screens itself, we only return
411 * here when switching editor modes or restarting the screen.
413 while (sp != NULL)
414 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
415 goto err;
417 done: rval = 0;
418 if (0)
419 err: rval = 1;
421 /* Clean out the global structure. */
422 v_end(gp);
424 return (rval);
428 * v_end --
429 * End the program, discarding screens and most of the global area.
431 * PUBLIC: void v_end __P((GS *));
433 void
434 v_end(gp)
435 GS *gp;
437 MSGS *mp;
438 SCR *sp;
440 /* If there are any remaining screens, kill them off. */
441 if (gp->ccl_sp != NULL) {
442 (void)file_end(gp->ccl_sp, NULL, 1);
443 (void)screen_end(gp->ccl_sp);
445 while ((sp = gp->dq.cqh_first) != (void *)&gp->dq)
446 (void)screen_end(sp);
447 while ((sp = gp->hq.cqh_first) != (void *)&gp->hq)
448 (void)screen_end(sp);
450 #ifdef HAVE_PERL_INTERP
451 perl_end(gp);
452 #endif
454 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
455 { FREF *frp;
456 /* Free FREF's. */
457 while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) {
458 CIRCLEQ_REMOVE(&gp->frefq, frp, q);
459 if (frp->name != NULL)
460 free(frp->name);
461 if (frp->tname != NULL)
462 free(frp->tname);
463 free(frp);
467 /* Free key input queue. */
468 if (gp->i_event != NULL)
469 free(gp->i_event);
471 /* Free cut buffers. */
472 cut_close(gp);
474 /* Free map sequences. */
475 seq_close(gp);
477 /* Free default buffer storage. */
478 (void)text_lfree(&gp->dcb_store.textq);
480 /* Close message catalogs. */
481 msg_close(gp);
482 #endif
484 /* Ring the bell if scheduled. */
485 if (F_ISSET(gp, G_BELLSCHED))
486 (void)fprintf(stderr, "\07"); /* \a */
489 * Flush any remaining messages. If a message is here, it's almost
490 * certainly the message about the event that killed us (although
491 * it's possible that the user is sourcing a file that exits from the
492 * editor).
494 while ((mp = gp->msgq.lh_first) != NULL) {
495 (void)fprintf(stderr, "%s%.*s",
496 mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
497 LIST_REMOVE(mp, q);
498 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
499 free(mp->buf);
500 free(mp);
501 #endif
504 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
505 /* Free any temporary space. */
506 if (gp->tmp_bp != NULL)
507 free(gp->tmp_bp);
509 #if defined(TRACE)
510 /* Close tracing file descriptor. */
511 vtrace_end();
512 #endif
513 #endif
517 * v_obsolete --
518 * Convert historic arguments into something getopt(3) will like.
520 static int
521 v_obsolete(name, argv)
522 char *name, *argv[];
524 size_t len;
525 char *p;
528 * Translate old style arguments into something getopt will like.
529 * Make sure it's not text space memory, because ex modifies the
530 * strings.
531 * Change "+" into "-c$".
532 * Change "+<anything else>" into "-c<anything else>".
533 * Change "-" into "-s"
534 * The c, T, t and w options take arguments so they can't be
535 * special arguments.
537 * Stop if we find "--" as an argument, the user may want to edit
538 * a file named "+foo".
540 while (*++argv && strcmp(argv[0], "--"))
541 if (argv[0][0] == '+') {
542 if (argv[0][1] == '\0') {
543 MALLOC_NOMSG(NULL, argv[0], char *, 4);
544 if (argv[0] == NULL)
545 goto nomem;
546 (void)strcpy(argv[0], "-c$");
547 } else {
548 p = argv[0];
549 len = strlen(argv[0]);
550 MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
551 if (argv[0] == NULL)
552 goto nomem;
553 argv[0][0] = '-';
554 argv[0][1] = 'c';
555 (void)strcpy(argv[0] + 2, p + 1);
557 } else if (argv[0][0] == '-')
558 if (argv[0][1] == '\0') {
559 MALLOC_NOMSG(NULL, argv[0], char *, 3);
560 if (argv[0] == NULL) {
561 nomem: v_estr(name, errno, NULL);
562 return (1);
564 (void)strcpy(argv[0], "-s");
565 } else
566 if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
567 argv[0][1] == 't' || argv[0][1] == 'w') &&
568 argv[0][2] == '\0')
569 ++argv;
570 return (0);
573 #ifdef DEBUG
574 static void
575 attach(gp)
576 GS *gp;
578 int fd;
579 char ch;
581 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
582 v_estr(gp->progname, errno, _PATH_TTY);
583 return;
586 (void)printf("process %lu waiting, enter <CR> to continue: ",
587 (u_long)getpid());
588 (void)fflush(stdout);
590 do {
591 if (read(fd, &ch, 1) != 1) {
592 (void)close(fd);
593 return;
595 } while (ch != '\n' && ch != '\r');
596 (void)close(fd);
598 #endif
600 static void
601 v_estr(name, eno, msg)
602 char *name, *msg;
603 int eno;
605 (void)fprintf(stderr, "%s", name);
606 if (msg != NULL)
607 (void)fprintf(stderr, ": %s", msg);
608 if (eno)
609 (void)fprintf(stderr, ": %s", strerror(errno));
610 (void)fprintf(stderr, "\n");