Another CHAR_T patch.
[nvi.git] / common / main.c
blobec97b44ebdd92a8b5e6b990bfad11e81f185ad3b
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.56 2000/07/14 14:29:16 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:16 $";
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];
69 CHAR_T *w;
70 size_t wlen;
72 gp = wp->gp;
74 /* Initialize the busy routine, if not defined by the screen. */
75 if (gp->scr_busy == NULL)
76 gp->scr_busy = vs_busy;
77 /* Initialize the message routine, if not defined by the screen. */
78 if (gp->scr_msg == NULL)
79 gp->scr_msg = vs_msg;
81 /* Common global structure initialization. */
82 CIRCLEQ_INIT(&gp->hq);
83 gp->noprint = DEFAULT_NOPRINT;
85 /* Structures shared by screens so stored in the GS structure. */
86 CIRCLEQ_INIT(&gp->frefq);
87 CIRCLEQ_INIT(&gp->exfq);
88 CIRCLEQ_INIT(&gp->dcb_store.textq);
89 LIST_INIT(&gp->cutq);
90 LIST_INIT(&gp->seqq);
92 /* Set initial screen type and mode based on the program name. */
93 readonly = 0;
94 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex"))
95 LF_INIT(SC_EX);
96 else {
97 /* Nview, view are readonly. */
98 if (!strcmp(gp->progname, "nview") ||
99 !strcmp(gp->progname, "view"))
100 readonly = 1;
102 /* Vi is the default. */
103 LF_INIT(SC_VI);
106 /* Convert old-style arguments into new-style ones. */
107 if (v_obsolete(gp->progname, argv))
108 return (1);
110 /* Parse the arguments. */
111 flagchk = '\0';
112 tag_f = wsizearg = NULL;
113 lflag = secure = silent = 0;
114 startup = 1;
116 /* Set the file snapshot flag. */
117 F_SET(gp, G_SNAPSHOT);
119 #ifdef DEBUG
120 while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != EOF)
121 #else
122 while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != EOF)
123 #endif
124 switch (ch) {
125 case 'c': /* Run the command. */
127 * XXX
128 * We should support multiple -c options.
130 if (gp->c_option != NULL) {
131 v_estr(gp->progname, 0,
132 "only one -c command may be specified.");
133 return (1);
135 gp->c_option = optarg;
136 break;
137 #ifdef DEBUG
138 case 'D':
139 switch (optarg[0]) {
140 case 's':
141 startup = 0;
142 break;
143 case 'w':
144 attach(gp);
145 break;
146 default:
147 v_estr(gp->progname, 0,
148 "usage: -D requires s or w argument.");
149 return (1);
151 break;
152 #endif
153 case 'e': /* Ex mode. */
154 LF_CLR(SC_VI);
155 LF_SET(SC_EX);
156 break;
157 case 'F': /* No snapshot. */
158 F_CLR(gp, G_SNAPSHOT);
159 break;
160 case 'l': /* Set lisp, showmatch options. */
161 lflag = 1;
162 break;
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;
226 if (db_env_create(&gp->env, 0)) {
227 v_estr(gp->progname, 0, "Unable to create DB environment.");
228 goto err;
230 gp->env->set_errfile(gp->env, stderr);
231 gp->env->open(gp->env, NULL, 0, 0);
232 gp->env = NULL;
235 * Build and initialize the first/current screen. This is a bit
236 * tricky. If an error is returned, we may or may not have a
237 * screen structure. If we have a screen structure, put it on a
238 * display queue so that the error messages get displayed.
240 * !!!
241 * Everything we do until we go interactive is done in ex mode.
243 if (screen_init(gp, NULL, &sp)) {
244 if (sp != NULL) {
245 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
246 sp->wp = wp;
248 goto err;
250 F_SET(sp, SC_EX);
251 CIRCLEQ_INSERT_HEAD(&wp->scrq, sp, q);
252 sp->wp = wp;
254 if (v_key_init(sp)) /* Special key initialization. */
255 goto err;
257 { int oargs[5], *oargp = oargs;
258 if (lflag) { /* Command-line options. */
259 *oargp++ = O_LISP;
260 *oargp++ = O_SHOWMATCH;
262 if (readonly)
263 *oargp++ = O_READONLY;
264 if (secure)
265 *oargp++ = O_SECURE;
266 *oargp = -1; /* Options initialization. */
267 if (opts_init(sp, oargs))
268 goto err;
270 if (wsizearg != NULL) {
271 ARGS *av[2], a, b;
272 (void)snprintf(path, sizeof(path), "window=%s", wsizearg);
273 a.bp = (CHAR_T *)path;
274 a.len = strlen(path);
275 b.bp = NULL;
276 b.len = 0;
277 av[0] = &a;
278 av[1] = &b;
279 (void)opts_set(sp, av, NULL);
281 if (silent) { /* Ex batch mode option values. */
282 O_CLR(sp, O_AUTOPRINT);
283 O_CLR(sp, O_PROMPT);
284 O_CLR(sp, O_VERBOSE);
285 O_CLR(sp, O_WARN);
286 F_SET(sp, SC_EX_SILENT);
289 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */
290 sp->cols = O_VAL(sp, O_COLUMNS);
292 if (!silent && startup) { /* Read EXINIT, exrc files. */
293 if (ex_exrc(sp))
294 goto err;
295 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
296 if (screen_end(sp))
297 goto err;
298 goto done;
303 * List recovery files if -r specified without file arguments.
304 * Note, options must be initialized and startup information
305 * read before doing this.
307 if (flagchk == 'r' && argv[0] == NULL) {
308 if (rcv_list(sp))
309 goto err;
310 if (screen_end(sp))
311 goto err;
312 goto done;
316 * !!!
317 * Initialize the default ^D, ^U scrolling value here, after the
318 * user has had every opportunity to set the window option.
320 * It's historic practice that changing the value of the window
321 * option did not alter the default scrolling value, only giving
322 * a count to ^D/^U did that.
324 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2;
327 * If we don't have a command-line option, switch into the right
328 * editor now, so that we position default files correctly, and
329 * so that any tags file file-already-locked messages are in the
330 * vi screen, not the ex screen.
332 * XXX
333 * If we have a command-line option, the error message can end
334 * up in the wrong place, but I think that the combination is
335 * unlikely.
337 if (gp->c_option == NULL) {
338 F_CLR(sp, SC_EX | SC_VI);
339 F_SET(sp, LF_ISSET(SC_EX | SC_VI));
342 /* Open a tag file if specified. */
343 if (tag_f != NULL) {
344 CHAR2INT(sp, tag_f, strlen(tag_f) + 1, w, wlen);
345 if (ex_tag_first(sp, w))
346 goto err;
350 * Append any remaining arguments as file names. Files are recovery
351 * files if -r specified. If the tag option or ex startup commands
352 * loaded a file, then any file arguments are going to come after it.
354 if (*argv != NULL) {
355 if (sp->frp != NULL) {
356 /* Cheat -- we know we have an extra argv slot. */
357 MALLOC_NOMSG(sp,
358 *--argv, char *, strlen(sp->frp->name) + 1);
359 if (*argv == NULL) {
360 v_estr(gp->progname, errno, NULL);
361 goto err;
363 (void)strcpy(*argv, sp->frp->name);
365 sp->argv = sp->cargv = argv;
366 F_SET(sp, SC_ARGNOFREE);
367 if (flagchk == 'r')
368 F_SET(sp, SC_ARGRECOVER);
372 * If the ex startup commands and or/the tag option haven't already
373 * created a file, create one. If no command-line files were given,
374 * use a temporary file.
376 if (sp->frp == NULL) {
377 if (sp->argv == NULL) {
378 if ((frp = file_add(sp, NULL)) == NULL)
379 goto err;
380 } else {
381 if ((frp = file_add(sp, sp->argv[0])) == NULL)
382 goto err;
383 if (F_ISSET(sp, SC_ARGRECOVER))
384 F_SET(frp, FR_RECOVER);
387 if (file_init(sp, frp, NULL, 0))
388 goto err;
389 if (EXCMD_RUNNING(wp)) {
390 (void)ex_cmd(sp);
391 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
392 if (screen_end(sp))
393 goto err;
394 goto done;
400 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex
401 * was forced to initialize the screen during startup. We'd like to
402 * wait for a single character from the user, but we can't because
403 * we're not in raw mode. We can't switch to raw mode because the
404 * vi initialization will switch to xterm's alternate screen, causing
405 * us to lose the messages we're pausing to make sure the user read.
406 * So, wait for a complete line.
408 if (F_ISSET(sp, SC_SCR_EX)) {
409 p = msg_cmsg(sp, CMSG_CONT_R, &len);
410 (void)write(STDOUT_FILENO, p, len);
411 for (;;) {
412 if (v_event_get(sp, &ev, 0, 0))
413 goto err;
414 if (ev.e_event == E_INTERRUPT ||
415 ev.e_event == E_CHARACTER &&
416 (ev.e_value == K_CR || ev.e_value == K_NL))
417 break;
418 (void)gp->scr_bell(sp);
422 /* Switch into the right editor, regardless. */
423 F_CLR(sp, SC_EX | SC_VI);
424 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT);
427 * Main edit loop. Vi handles split screens itself, we only return
428 * here when switching editor modes or restarting the screen.
430 while (sp != NULL)
431 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp))
432 goto err;
434 done: rval = 0;
435 if (0)
436 err: rval = 1;
438 return (rval);
442 * v_obsolete --
443 * Convert historic arguments into something getopt(3) will like.
445 static int
446 v_obsolete(name, argv)
447 char *name, *argv[];
449 size_t len;
450 char *p;
453 * Translate old style arguments into something getopt will like.
454 * Make sure it's not text space memory, because ex modifies the
455 * strings.
456 * Change "+" into "-c$".
457 * Change "+<anything else>" into "-c<anything else>".
458 * Change "-" into "-s"
459 * The c, T, t and w options take arguments so they can't be
460 * special arguments.
462 * Stop if we find "--" as an argument, the user may want to edit
463 * a file named "+foo".
465 while (*++argv && strcmp(argv[0], "--"))
466 if (argv[0][0] == '+') {
467 if (argv[0][1] == '\0') {
468 MALLOC_NOMSG(NULL, argv[0], char *, 4);
469 if (argv[0] == NULL)
470 goto nomem;
471 (void)strcpy(argv[0], "-c$");
472 } else {
473 p = argv[0];
474 len = strlen(argv[0]);
475 MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
476 if (argv[0] == NULL)
477 goto nomem;
478 argv[0][0] = '-';
479 argv[0][1] = 'c';
480 (void)strcpy(argv[0] + 2, p + 1);
482 } else if (argv[0][0] == '-')
483 if (argv[0][1] == '\0') {
484 MALLOC_NOMSG(NULL, argv[0], char *, 3);
485 if (argv[0] == NULL) {
486 nomem: v_estr(name, errno, NULL);
487 return (1);
489 (void)strcpy(argv[0], "-s");
490 } else
491 if ((argv[0][1] == 'c' || argv[0][1] == 'T' ||
492 argv[0][1] == 't' || argv[0][1] == 'w') &&
493 argv[0][2] == '\0')
494 ++argv;
495 return (0);
498 #ifdef DEBUG
499 static void
500 attach(gp)
501 GS *gp;
503 int fd;
504 char ch;
506 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
507 v_estr(gp->progname, errno, _PATH_TTY);
508 return;
511 (void)printf("process %lu waiting, enter <CR> to continue: ",
512 (u_long)getpid());
513 (void)fflush(stdout);
515 do {
516 if (read(fd, &ch, 1) != 1) {
517 (void)close(fd);
518 return;
520 } while (ch != '\n' && ch != '\r');
521 (void)close(fd);
523 #endif
525 static void
526 v_estr(name, eno, msg)
527 char *name, *msg;
528 int eno;
530 (void)fprintf(stderr, "%s", name);
531 if (msg != NULL)
532 (void)fprintf(stderr, ": %s", msg);
533 if (eno)
534 (void)fprintf(stderr, ": %s", strerror(errno));
535 (void)fprintf(stderr, "\n");