reset screen offset of top line if it exceeds the number of screens
[nvi.git] / common / main.c
blob520b423c12b711a8610092319a8dd1146e27911d
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.63 2001/11/01 15:24:43 skimo Exp $ (Berkeley) $Date: 2001/11/01 15:24:43 $";
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 v_estr(gp->progname, 0,
145 "-F option no longer supported.");
146 break;
147 case 'l': /* Set lisp, showmatch options. */
148 lflag = 1;
149 break;
150 case 'R': /* Readonly. */
151 readonly = 1;
152 break;
153 case 'r': /* Recover. */
154 if (flagchk == 't') {
155 v_estr(gp->progname, 0,
156 "only one of -r and -t may be specified.");
157 return (1);
159 flagchk = 'r';
160 break;
161 case 'S':
162 secure = 1;
163 break;
164 case 's':
165 silent = 1;
166 break;
167 #ifdef TRACE
168 case 'T': /* Trace. */
169 (void)vtrace_init(optarg);
170 break;
171 #endif
172 case 't': /* Tag. */
173 if (flagchk == 'r') {
174 v_estr(gp->progname, 0,
175 "only one of -r and -t may be specified.");
176 return (1);
178 if (flagchk == 't') {
179 v_estr(gp->progname, 0,
180 "only one tag file may be specified.");
181 return (1);
183 flagchk = 't';
184 tag_f = optarg;
185 break;
186 case 'v': /* Vi mode. */
187 LF_CLR(SC_EX);
188 LF_SET(SC_VI);
189 break;
190 case 'w':
191 wsizearg = optarg;
192 break;
193 case '?':
194 default:
195 (void)gp->scr_usage();
196 return (1);
198 argc -= optind;
199 argv += optind;
202 * -s option is only meaningful to ex.
204 * If not reading from a terminal, it's like -s was specified.
206 if (silent && !LF_ISSET(SC_EX)) {
207 v_estr(gp->progname, 0, "-s option is only applicable to ex.");
208 goto err;
210 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
211 silent = 1;
214 * Build and initialize the first/current screen. This is a bit
215 * tricky. If an error is returned, we may or may not have a
216 * screen structure. If we have a screen structure, put it on a
217 * display queue so that the error messages get displayed.
219 * !!!
220 * Everything we do until we go interactive is done in ex mode.
222 if (screen_init(gp, NULL, &sp)) {
223 if (sp != NULL) {
224 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
225 sp->wp = wp;
227 goto err;
229 F_SET(sp, SC_EX);
230 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
231 sp->wp = wp;
233 if (v_key_init(sp)) /* Special key initialization. */
234 goto err;
236 { int oargs[5], *oargp = oargs;
237 if (lflag) { /* Command-line options. */
238 *oargp++ = O_LISP;
239 *oargp++ = O_SHOWMATCH;
241 if (readonly)
242 *oargp++ = O_READONLY;
243 if (secure)
244 *oargp++ = O_SECURE;
245 *oargp = -1; /* Options initialization. */
246 if (opts_init(sp, oargs))
247 goto err;
249 if (wsizearg != NULL) {
250 ARGS *av[2], a, b;
251 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
252 a.bp = (CHAR_T *)path;
253 a.len = strlen(path);
254 b.bp = NULL;
255 b.len = 0;
256 av[0] = &a;
257 av[1] = &b;
258 (void)opts_set(sp, av, NULL);
260 if (silent) { /* Ex batch mode option values. */
261 O_CLR(sp, O_AUTOPRINT);
262 O_CLR(sp, O_PROMPT);
263 O_CLR(sp, O_VERBOSE);
264 O_CLR(sp, O_WARN);
265 F_SET(sp, SC_EX_SILENT);
268 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
269 sp->cols = O_VAL(sp, O_COLUMNS);
271 if (!silent && startup) { /* Read EXINIT, exrc files. */
272 if (ex_exrc(sp))
273 goto err;
274 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
275 if (screen_end(sp))
276 goto err;
277 goto done;
282 * List recovery files if -r specified without file arguments.
283 * Note, options must be initialized and startup information
284 * read before doing this.
286 if (flagchk == 'r' && argv[0] == NULL) {
287 if (rcv_list(sp))
288 goto err;
289 if (screen_end(sp))
290 goto err;
291 goto done;
295 * !!!
296 * Initialize the default ^D, ^U scrolling value here, after the
297 * user has had every opportunity to set the window option.
299 * It's historic practice that changing the value of the window
300 * option did not alter the default scrolling value, only giving
301 * a count to ^D/^U did that.
303 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
306 * If we don't have a command-line option, switch into the right
307 * editor now, so that we position default files correctly, and
308 * so that any tags file file-already-locked messages are in the
309 * vi screen, not the ex screen.
311 * XXX
312 * If we have a command-line option, the error message can end
313 * up in the wrong place, but I think that the combination is
314 * unlikely.
316 if (gp->c_option == NULL) {
317 F_CLR(sp, SC_EX | SC_VI);
318 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
321 /* Open a tag file if specified. */
322 if (tag_f != NULL) {
323 CHAR2INT(sp, tag_f, strlen(tag_f) + 1, w, wlen);
324 if (ex_tag_first(sp, w))
325 goto err;
329 * Append any remaining arguments as file names. Files are recovery
330 * files if -r specified. If the tag option or ex startup commands
331 * loaded a file, then any file arguments are going to come after it.
333 if (*argv != NULL) {
334 if (sp->frp != NULL) {
335 /* Cheat -- we know we have an extra argv slot. */
336 MALLOC_NOMSG(sp,
337 *--argv, char *, strlen(sp->frp->name) + 1);
338 if (*argv == NULL) {
339 v_estr(gp->progname, errno, NULL);
340 goto err;
342 (void)strcpy(*argv, sp->frp->name);
344 sp->argv = sp->cargv = argv;
345 F_SET(sp, SC_ARGNOFREE);
346 if (flagchk == 'r')
347 F_SET(sp, SC_ARGRECOVER);
351 * If the ex startup commands and or/the tag option haven't already
352 * created a file, create one. If no command-line files were given,
353 * use a temporary file.
355 if (sp->frp == NULL) {
356 if (sp->argv == NULL) {
357 if ((frp = file_add(sp, NULL)) == NULL)
358 goto err;
359 } else {
360 if ((frp = file_add(sp, sp->argv[0])) == NULL)
361 goto err;
362 if (F_ISSET(sp, SC_ARGRECOVER))
363 F_SET(frp, FR_RECOVER);
366 if (file_init(sp, frp, NULL, 0))
367 goto err;
368 if (EXCMD_RUNNING(wp)) {
369 (void)ex_cmd(sp);
370 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
371 if (screen_end(sp))
372 goto err;
373 goto done;
379 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
380 * was forced to initialize the screen during startup. We'd like to
381 * wait for a single character from the user, but we can't because
382 * we're not in raw mode. We can't switch to raw mode because the
383 * vi initialization will switch to xterm's alternate screen, causing
384 * us to lose the messages we're pausing to make sure the user read.
385 * So, wait for a complete line.
387 if (F_ISSET(sp, SC_SCR_EX)) {
388 p = msg_cmsg(sp, CMSG_CONT_R, &len);
389 (void)write(STDOUT_FILENO, p, len);
390 for (;;) {
391 if (v_event_get(sp, &ev, 0, 0))
392 goto err;
393 if (ev.e_event == E_INTERRUPT ||
394 (ev.e_event == E_CHARACTER &&
395 (ev.e_value == K_CR || ev.e_value == K_NL)))
396 break;
397 (void)gp->scr_bell(sp);
401 /* Switch into the right editor, regardless. */
402 F_CLR(sp, SC_EX | SC_VI);
403 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
406 * Main edit loop. Vi handles split screens itself, we only return
407 * here when switching editor modes or restarting the screen.
409 while (sp != NULL)
410 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
411 goto err;
413 done: rval = 0;
414 if (0)
415 err: rval = 1;
417 return (rval);
421 * v_obsolete --
422 * Convert historic arguments into something getopt(3) will like.
424 static int
425 v_obsolete(char *name, char **argv)
427 size_t len;
428 char *p;
431 * Translate old style arguments into something getopt will like.
432 * Make sure it's not text space memory, because ex modifies the
433 * strings.
434 * Change "+" into "-c$".
435 * Change "+<anything else>" into "-c<anything else>".
436 * Change "-" into "-s"
437 * The c, T, t and w options take arguments so they can't be
438 * special arguments.
440 * Stop if we find "--" as an argument, the user may want to edit
441 * a file named "+foo".
443 while (*++argv && strcmp(argv[0], "--"))
444 if (argv[0][0] == '+') {
445 if (argv[0][1] == '\0') {
446 MALLOC_NOMSG(NULL, argv[0], char *, 4);
447 if (argv[0] == NULL)
448 goto nomem;
449 (void)strcpy(argv[0], "-c$");
450 } else {
451 p = argv[0];
452 len = strlen(argv[0]);
453 MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
454 if (argv[0] == NULL)
455 goto nomem;
456 argv[0][0] = '-';
457 argv[0][1] = 'c';
458 (void)strcpy(argv[0] + 2, p + 1);
460 } else if (argv[0][0] == '-') {
461 if (argv[0][1] == '\0') {
462 MALLOC_NOMSG(NULL, argv[0], char *, 3);
463 if (argv[0] == NULL) {
464 nomem: v_estr(name, errno, NULL);
465 return (1);
467 (void)strcpy(argv[0], "-s");
468 } else
469 if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
470 argv[0][1] == 't' || argv[0][1] == 'w') &&
471 argv[0][2] == '\0')
472 ++argv;
474 return (0);
477 #ifdef DEBUG
478 static void
479 attach(GS *gp)
481 int fd;
482 char ch;
484 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
485 v_estr(gp->progname, errno, _PATH_TTY);
486 return;
489 (void)printf("process %lu waiting, enter <CR> to continue: ",
490 (u_long)getpid());
491 (void)fflush(stdout);
493 do {
494 if (read(fd, &ch, 1) != 1) {
495 (void)close(fd);
496 return;
498 } while (ch != '\n' && ch != '\r');
499 (void)close(fd);
501 #endif
503 static void
504 v_estr(char *name, int eno, char *msg)
506 (void)fprintf(stderr, "%s", name);
507 if (msg != NULL)
508 (void)fprintf(stderr, ": %s", msg);
509 if (eno)
510 (void)fprintf(stderr, ": %s", strerror(errno));
511 (void)fprintf(stderr, "\n");