Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / main.c
blob215f74241342a10f4e62a35c4099e5bd6f847a10
1 /* $NetBSD: main.c,v 1.2 2008/10/29 17:50:49 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char copyright[] =
16 "%Z% Copyright (c) 1992, 1993, 1994\n\
17 The Regents of the University of California. All rights reserved.\n\
18 %Z% Copyright (c) 1992, 1993, 1994, 1995, 1996\n\
19 Keith Bostic. All rights reserved.\n";
20 #endif /* not lint */
22 #ifndef lint
23 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";
24 #endif /* not lint */
26 #include <sys/types.h>
27 #include <sys/queue.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
31 #include <bitstring.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "common.h"
41 #include "../vi/vi.h"
42 #include "pathnames.h"
44 static void v_estr __P((const char *, int, const char *));
45 static int v_obsolete __P((char *, char *[]));
48 * editor --
49 * Main editor routine.
51 * PUBLIC: int editor __P((WIN *, int, char *[]));
53 int
54 editor(WIN *wp, int argc, char **argv)
56 extern int optind;
57 extern char *optarg;
58 const char *p;
59 EVENT ev;
60 FREF *frp;
61 SCR *sp;
62 GS *gp;
63 size_t len;
64 u_int flags;
65 int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
66 #ifdef GTAGS
67 int gtags = 0;
68 #endif
69 char *tag_f, *wsizearg, path[256];
70 const CHAR_T *w;
71 size_t wlen;
73 gp = wp->gp;
75 /* Initialize the busy routine, if not defined by the screen. */
76 if (gp->scr_busy == NULL)
77 gp->scr_busy = vs_busy;
78 /* Initialize the message routine, if not defined by the screen. */
79 if (wp->scr_msg == NULL)
80 wp->scr_msg = vs_msg;
82 /* Set initial screen type and mode based on the program name. */
83 readonly = 0;
84 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
85 LF_INIT(SC_EX);
86 else {
87 /* Nview, view are readonly. */
88 if (!strcmp(gp->progname, "nview") ||
89 !strcmp(gp->progname, "view"))
90 readonly = 1;
92 /* Vi is the default. */
93 LF_INIT(SC_VI);
96 /* Convert old-style arguments into new-style ones. */
97 if (v_obsolete(gp->progname, argv))
98 return (1);
100 /* Parse the arguments. */
101 flagchk = '\0';
102 tag_f = wsizearg = NULL;
103 lflag = secure = silent = 0;
104 startup = 1;
106 /* Set the file snapshot flag. */
107 F_SET(gp, G_SNAPSHOT);
109 while ((ch = getopt(argc, argv, "c:"
110 #ifdef DEBUG
111 "D:"
112 #endif
113 "eF"
114 #ifdef GTAGS
116 #endif
117 "lRrSsT:t:vw:")) != EOF)
118 switch (ch) {
119 case 'c': /* Run the command. */
121 * XXX
122 * We should support multiple -c options.
124 if (gp->c_option != NULL) {
125 v_estr(gp->progname, 0,
126 "only one -c command may be specified.");
127 return (1);
129 gp->c_option = optarg;
130 break;
131 #ifdef DEBUG
132 case 'D':
133 switch (optarg[0]) {
134 case 's':
135 startup = 0;
136 break;
137 case 'w':
138 attach(gp);
139 break;
140 default:
141 v_estr(gp->progname, 0,
142 "usage: -D requires s or w argument.");
143 return (1);
145 break;
146 #endif
147 case 'e': /* Ex mode. */
148 LF_CLR(SC_VI);
149 LF_SET(SC_EX);
150 break;
151 case 'F': /* No snapshot. */
152 v_estr(gp->progname, 0,
153 "-F option no longer supported.");
154 break;
155 case 'l': /* Set lisp, showmatch options. */
156 lflag = 1;
157 break;
158 #ifdef GTAGS
159 case 'G': /* gtags mode. */
160 gtags = 1;
161 break;
162 #endif
163 case 'R': /* Readonly. */
164 readonly = 1;
165 break;
166 case 'r': /* Recover. */
167 if (flagchk == 't') {
168 v_estr(gp->progname, 0,
169 "only one of -r and -t may be specified.");
170 return (1);
172 flagchk = 'r';
173 break;
174 case 'S':
175 secure = 1;
176 break;
177 case 's':
178 silent = 1;
179 break;
180 #ifdef TRACE
181 case 'T': /* Trace. */
182 (void)vtrace_init(optarg);
183 break;
184 #endif
185 case 't': /* Tag. */
186 if (flagchk == 'r') {
187 v_estr(gp->progname, 0,
188 "only one of -r and -t may be specified.");
189 return (1);
191 if (flagchk == 't') {
192 v_estr(gp->progname, 0,
193 "only one tag file may be specified.");
194 return (1);
196 flagchk = 't';
197 tag_f = optarg;
198 break;
199 case 'v': /* Vi mode. */
200 LF_CLR(SC_EX);
201 LF_SET(SC_VI);
202 break;
203 case 'w':
204 wsizearg = optarg;
205 break;
206 case '?':
207 default:
208 (void)gp->scr_usage();
209 return (1);
211 argc -= optind;
212 argv += optind;
215 * -s option is only meaningful to ex.
217 * If not reading from a terminal, it's like -s was specified.
219 if (silent && !LF_ISSET(SC_EX)) {
220 v_estr(gp->progname, 0, "-s option is only applicable to ex.");
221 goto err;
223 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED))
224 silent = 1;
227 * Build and initialize the first/current screen. This is a bit
228 * tricky. If an error is returned, we may or may not have a
229 * screen structure. If we have a screen structure, put it on a
230 * display queue so that the error messages get displayed.
232 * !!!
233 * Everything we do until we go interactive is done in ex mode.
235 if (screen_init(gp, NULL, &sp)) {
236 if (sp != NULL) {
237 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
238 sp->wp = wp;
240 goto err;
242 F_SET(sp, SC_EX);
243 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
244 sp->wp = wp;
246 if (v_key_init(sp)) /* Special key initialization. */
247 goto err;
249 { int oargs[5], *oargp = oargs;
250 if (lflag) { /* Command-line options. */
251 *oargp++ = O_LISP;
252 *oargp++ = O_SHOWMATCH;
254 if (readonly)
255 *oargp++ = O_READONLY;
256 #ifdef GTAGS
257 if (gtags)
258 *oargp++ = O_GTAGSMODE;
259 #endif
260 if (secure)
261 *oargp++ = O_SECURE;
262 *oargp = -1; /* Options initialization. */
263 if (opts_init(sp, oargs))
264 goto err;
266 if (wsizearg != NULL) {
267 ARGS *av[2], a, b;
268 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
269 a.bp = (CHAR_T *)path;
270 a.len = strlen(path);
271 b.bp = NULL;
272 b.len = 0;
273 av[0] = &a;
274 av[1] = &b;
275 (void)opts_set(sp, av, NULL);
277 if (silent) { /* Ex batch mode option values. */
278 O_CLR(sp, O_AUTOPRINT);
279 O_CLR(sp, O_PROMPT);
280 O_CLR(sp, O_VERBOSE);
281 O_CLR(sp, O_WARN);
282 F_SET(sp, SC_EX_SILENT);
285 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
286 sp->cols = O_VAL(sp, O_COLUMNS);
288 if (!silent && startup) { /* Read EXINIT, exrc files. */
289 if (ex_exrc(sp))
290 goto err;
291 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
292 if (screen_end(sp))
293 goto err;
294 goto done;
299 * List recovery files if -r specified without file arguments.
300 * Note, options must be initialized and startup information
301 * read before doing this.
303 if (flagchk == 'r' && argv[0] == NULL) {
304 if (rcv_list(sp))
305 goto err;
306 if (screen_end(sp))
307 goto err;
308 goto done;
312 * !!!
313 * Initialize the default ^D, ^U scrolling value here, after the
314 * user has had every opportunity to set the window option.
316 * It's historic practice that changing the value of the window
317 * option did not alter the default scrolling value, only giving
318 * a count to ^D/^U did that.
320 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
323 * If we don't have a command-line option, switch into the right
324 * editor now, so that we position default files correctly, and
325 * so that any tags file file-already-locked messages are in the
326 * vi screen, not the ex screen.
328 * XXX
329 * If we have a command-line option, the error message can end
330 * up in the wrong place, but I think that the combination is
331 * unlikely.
333 if (gp->c_option == NULL) {
334 F_CLR(sp, SC_EX | SC_VI);
335 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
338 /* Open a tag file if specified. */
339 if (tag_f != NULL) {
340 CHAR2INT(sp, tag_f, strlen(tag_f) + 1, w, wlen);
341 if (ex_tag_first(sp, w))
342 goto err;
346 * Append any remaining arguments as file names. Files are recovery
347 * files if -r specified. If the tag option or ex startup commands
348 * loaded a file, then any file arguments are going to come after it.
350 if (*argv != NULL) {
351 if (sp->frp != NULL) {
352 /* Cheat -- we know we have an extra argv slot. */
353 MALLOC_NOMSG(sp,
354 *--argv, char *, strlen(sp->frp->name) + 1);
355 if (*argv == NULL) {
356 v_estr(gp->progname, errno, NULL);
357 goto err;
359 (void)strcpy(*argv, sp->frp->name);
361 sp->argv = sp->cargv = argv;
362 F_SET(sp, SC_ARGNOFREE);
363 if (flagchk == 'r')
364 F_SET(sp, SC_ARGRECOVER);
368 * If the ex startup commands and or/the tag option haven't already
369 * created a file, create one. If no command-line files were given,
370 * use a temporary file.
372 if (sp->frp == NULL) {
373 if (sp->argv == NULL) {
374 if ((frp = file_add(sp, NULL)) == NULL)
375 goto err;
376 } else {
377 if ((frp = file_add(sp, sp->argv[0])) == NULL)
378 goto err;
379 if (F_ISSET(sp, SC_ARGRECOVER))
380 F_SET(frp, FR_RECOVER);
383 if (file_init(sp, frp, NULL, 0))
384 goto err;
385 if (EXCMD_RUNNING(wp)) {
386 (void)ex_cmd(sp);
387 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
388 if (screen_end(sp))
389 goto err;
390 goto done;
396 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
397 * was forced to initialize the screen during startup. We'd like to
398 * wait for a single character from the user, but we can't because
399 * we're not in raw mode. We can't switch to raw mode because the
400 * vi initialization will switch to xterm's alternate screen, causing
401 * us to lose the messages we're pausing to make sure the user read.
402 * So, wait for a complete line.
404 if (F_ISSET(sp, SC_SCR_EX)) {
405 p = msg_cmsg(sp, CMSG_CONT_R, &len);
406 (void)write(STDOUT_FILENO, p, len);
407 for (;;) {
408 if (v_event_get(sp, &ev, 0, 0))
409 goto err;
410 if (ev.e_event == E_INTERRUPT ||
411 (ev.e_event == E_CHARACTER &&
412 (ev.e_value == K_CR || ev.e_value == K_NL)))
413 break;
414 (void)gp->scr_bell(sp);
418 /* Switch into the right editor, regardless. */
419 F_CLR(sp, SC_EX | SC_VI);
420 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
423 * Main edit loop. Vi handles split screens itself, we only return
424 * here when switching editor modes or restarting the screen.
426 while (sp != NULL)
427 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
428 goto err;
430 done: rval = 0;
431 if (0)
432 err: rval = 1;
434 return (rval);
438 * v_obsolete --
439 * Convert historic arguments into something getopt(3) will like.
441 static int
442 v_obsolete(char *name, char **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;
491 return (0);
494 #ifdef DEBUG
495 static void
496 attach(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(const char *name, int eno, const char *msg)
523 (void)fprintf(stderr, "%s", name);
524 if (msg != NULL)
525 (void)fprintf(stderr, ": %s", msg);
526 if (eno)
527 (void)fprintf(stderr, ": %s", strerror(errno));
528 (void)fprintf(stderr, "\n");