1 /* $OpenBSD: main.c,v 1.7 2001/01/29 01:58:30 niklas Exp $ */ 2 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. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #ifndef lint 15 static const char copyright[] = 16 "@(#) Copyright (c) 1992, 1993, 1994\n\ 17 The Regents of the University of California. All rights reserved.\n\ 18 @(#) Copyright (c) 1992, 1993, 1994, 1995, 1996\n\ 19 Keith Bostic. All rights reserved.\n"; 20 #endif /* not lint */ 21 22 #ifndef lint 23 static const char sccsid[] = "@(#)main.c 10.48 (Berkeley) 10/11/96"; 24 #endif /* not lint */ 25 26 #include <sys/types.h> 27 #include <sys/queue.h> 28 #include <sys/stat.h> 29 #include <sys/time.h> 30 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> 39 40 #include "common.h" 41 #include "../vi/vi.h" 42 #include "pathnames.h" 43 44 static void attach __P((GS *)); 45 static void v_estr __P((char *, int, char *)); 46 static int v_obsolete __P((char *, char *[])); 47 48 /* 49 * editor -- 50 * Main editor routine. 51 * 52 * PUBLIC: int editor __P((GS *, int, char *[])); 53 */ 54 int 55 editor(gp, argc, argv) 56 GS *gp; 57 int argc; 58 char *argv[]; 59 { 60 extern int optind; 61 extern char *optarg; 62 const char *p; 63 EVENT ev; 64 FREF *frp; 65 SCR *sp; 66 size_t len; 67 u_int flags; 68 int ch, flagchk, lflag, secure, startup, readonly, rval, silent; 69 char *tag_f, *wsizearg, path[256]; 70 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 (gp->scr_msg == NULL) 76 gp->scr_msg = vs_msg; 77 78 /* Common global structure initialization. */ 79 CIRCLEQ_INIT(&gp->dq); 80 CIRCLEQ_INIT(&gp->hq); 81 LIST_INIT(&gp->ecq); 82 LIST_INSERT_HEAD(&gp->ecq, &gp->excmd, q); 83 gp->noprint = DEFAULT_NOPRINT; 84 85 /* Structures shared by screens so stored in the GS structure. */ 86 CIRCLEQ_INIT(&gp->frefq); 87 CIRCLEQ_INIT(&gp->dcb_store.textq); 88 LIST_INIT(&gp->cutq); 89 LIST_INIT(&gp->seqq); 90 91 /* Set initial screen type and mode based on the program name. */ 92 readonly = 0; 93 if (!strcmp(gp->progname, "ex") || !strcmp(gp->progname, "nex")) 94 LF_INIT(SC_EX); 95 else { 96 /* Nview, view are readonly. */ 97 if (!strcmp(gp->progname, "nview") || 98 !strcmp(gp->progname, "view")) 99 readonly = 1; 100 101 /* Vi is the default. */ 102 LF_INIT(SC_VI); 103 } 104 105 /* Convert old-style arguments into new-style ones. */ 106 if (v_obsolete(gp->progname, argv)) 107 return (1); 108 109 /* Parse the arguments. */ 110 flagchk = '\0'; 111 tag_f = wsizearg = NULL; 112 lflag = secure = silent = 0; 113 startup = 1; 114 115 /* Set the file snapshot flag. */ 116 F_SET(gp, G_SNAPSHOT); 117 118 #ifdef DEBUG 119 while ((ch = getopt(argc, argv, "c:D:eFlRrSsT:t:vw:")) != -1) 120 #else 121 while ((ch = getopt(argc, argv, "c:eFlRrSst:vw:")) != -1) 122 #endif 123 switch (ch) { 124 case 'c': /* Run the command. */ 125 /* 126 * XXX 127 * We should support multiple -c options. 128 */ 129 if (gp->c_option != NULL) { 130 v_estr(gp->progname, 0, 131 "only one -c command may be specified."); 132 return (1); 133 } 134 gp->c_option = optarg; 135 break; 136 #ifdef DEBUG 137 case 'D': 138 switch (optarg[0]) { 139 case 's': 140 startup = 0; 141 break; 142 case 'w': 143 attach(gp); 144 break; 145 default: 146 v_estr(gp->progname, 0, 147 "usage: -D requires s or w argument."); 148 return (1); 149 } 150 break; 151 #endif 152 case 'e': /* Ex mode. */ 153 LF_CLR(SC_VI); 154 LF_SET(SC_EX); 155 break; 156 case 'F': /* No snapshot. */ 157 F_CLR(gp, G_SNAPSHOT); 158 break; 159 case 'l': /* Set lisp, showmatch options. */ 160 lflag = 1; 161 break; 162 case 'R': /* Readonly. */ 163 readonly = 1; 164 break; 165 case 'r': /* Recover. */ 166 if (flagchk == 't') { 167 v_estr(gp->progname, 0, 168 "only one of -r and -t may be specified."); 169 return (1); 170 } 171 flagchk = 'r'; 172 break; 173 case 'S': 174 secure = 1; 175 break; 176 case 's': 177 silent = 1; 178 break; 179 #ifdef DEBUG 180 case 'T': /* Trace. */ 181 if ((gp->tracefp = fopen(optarg, "w")) == NULL) { 182 v_estr(gp->progname, errno, optarg); 183 goto err; 184 } 185 (void)fprintf(gp->tracefp, 186 "\n===\ntrace: open %s\n", optarg); 187 break; 188 #endif 189 case 't': /* Tag. */ 190 if (flagchk == 'r') { 191 v_estr(gp->progname, 0, 192 "only one of -r and -t may be specified."); 193 return (1); 194 } 195 if (flagchk == 't') { 196 v_estr(gp->progname, 0, 197 "only one tag file may be specified."); 198 return (1); 199 } 200 flagchk = 't'; 201 tag_f = optarg; 202 break; 203 case 'v': /* Vi mode. */ 204 LF_CLR(SC_EX); 205 LF_SET(SC_VI); 206 break; 207 case 'w': 208 wsizearg = optarg; 209 break; 210 case '?': 211 default: 212 (void)gp->scr_usage(); 213 return (1); 214 } 215 argc -= optind; 216 argv += optind; 217 218 /* 219 * -s option is only meaningful to ex. 220 * 221 * If not reading from a terminal, it's like -s was specified. 222 */ 223 if (silent && !LF_ISSET(SC_EX)) { 224 v_estr(gp->progname, 0, "-s option is only applicable to ex."); 225 goto err; 226 } 227 if (LF_ISSET(SC_EX) && F_ISSET(gp, G_SCRIPTED)) 228 silent = 1; 229 230 /* 231 * Build and initialize the first/current screen. This is a bit 232 * tricky. If an error is returned, we may or may not have a 233 * screen structure. If we have a screen structure, put it on a 234 * display queue so that the error messages get displayed. 235 * 236 * !!! 237 * Everything we do until we go interactive is done in ex mode. 238 */ 239 if (screen_init(gp, NULL, &sp)) { 240 if (sp != NULL) 241 CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q); 242 goto err; 243 } 244 F_SET(sp, SC_EX); 245 CIRCLEQ_INSERT_HEAD(&gp->dq, sp, q); 246 247 if (v_key_init(sp)) /* Special key initialization. */ 248 goto err; 249 250 { int oargs[5], *oargp = oargs; 251 if (lflag) { /* Command-line options. */ 252 *oargp++ = O_LISP; 253 *oargp++ = O_SHOWMATCH; 254 } 255 if (readonly) 256 *oargp++ = O_READONLY; 257 if (secure) 258 *oargp++ = O_SECURE; 259 *oargp = -1; /* Options initialization. */ 260 if (opts_init(sp, oargs)) 261 goto err; 262 } 263 if (wsizearg != NULL) { 264 ARGS *av[2], a, b; 265 (void)snprintf(path, sizeof(path), "window=%s", wsizearg); 266 a.bp = (CHAR_T *)path; 267 a.len = strlen(path); 268 b.bp = NULL; 269 b.len = 0; 270 av[0] = &a; 271 av[1] = &b; 272 (void)opts_set(sp, av, NULL); 273 } 274 if (silent) { /* Ex batch mode option values. */ 275 O_CLR(sp, O_AUTOPRINT); 276 O_CLR(sp, O_PROMPT); 277 O_CLR(sp, O_VERBOSE); 278 O_CLR(sp, O_WARN); 279 F_SET(sp, SC_EX_SILENT); 280 } 281 282 sp->rows = O_VAL(sp, O_LINES); /* Make ex formatting work. */ 283 sp->cols = O_VAL(sp, O_COLUMNS); 284 285 if (!silent && startup) { /* Read EXINIT, exrc files. */ 286 if (ex_exrc(sp)) 287 goto err; 288 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) { 289 if (screen_end(sp)) 290 goto err; 291 goto done; 292 } 293 } 294 295 /* 296 * List recovery files if -r specified without file arguments. 297 * Note, options must be initialized and startup information 298 * read before doing this. 299 */ 300 if (flagchk == 'r' && argv[0] == NULL) { 301 if (rcv_list(sp)) 302 goto err; 303 if (screen_end(sp)) 304 goto err; 305 goto done; 306 } 307 308 /* 309 * !!! 310 * Initialize the default ^D, ^U scrolling value here, after the 311 * user has had every opportunity to set the window option. 312 * 313 * It's historic practice that changing the value of the window 314 * option did not alter the default scrolling value, only giving 315 * a count to ^D/^U did that. 316 */ 317 sp->defscroll = (O_VAL(sp, O_WINDOW) + 1) / 2; 318 319 /* 320 * If we don't have a command-line option, switch into the right 321 * editor now, so that we position default files correctly, and 322 * so that any tags file file-already-locked messages are in the 323 * vi screen, not the ex screen. 324 * 325 * XXX 326 * If we have a command-line option, the error message can end 327 * up in the wrong place, but I think that the combination is 328 * unlikely. 329 */ 330 if (gp->c_option == NULL) { 331 F_CLR(sp, SC_EX | SC_VI); 332 F_SET(sp, LF_ISSET(SC_EX | SC_VI)); 333 } 334 335 /* Open a tag file if specified. */ 336 if (tag_f != NULL && ex_tag_first(sp, tag_f)) 337 goto err; 338 339 /* 340 * Append any remaining arguments as file names. Files are recovery 341 * files if -r specified. If the tag option or ex startup commands 342 * loaded a file, then any file arguments are going to come after it. 343 */ 344 if (*argv != NULL) { 345 if (sp->frp != NULL) { 346 /* Cheat -- we know we have an extra argv slot. */ 347 MALLOC_NOMSG(sp, 348 *--argv, char *, strlen(sp->frp->name) + 1); 349 if (*argv == NULL) { 350 v_estr(gp->progname, errno, NULL); 351 goto err; 352 } 353 (void)strcpy(*argv, sp->frp->name); 354 } 355 sp->argv = sp->cargv = argv; 356 F_SET(sp, SC_ARGNOFREE); 357 if (flagchk == 'r') 358 F_SET(sp, SC_ARGRECOVER); 359 } 360 361 /* 362 * If the ex startup commands and or/the tag option haven't already 363 * created a file, create one. If no command-line files were given, 364 * use a temporary file. 365 */ 366 if (sp->frp == NULL) { 367 if (sp->argv == NULL) { 368 if ((frp = file_add(sp, NULL)) == NULL) 369 goto err; 370 } else { 371 if ((frp = file_add(sp, (CHAR_T *)sp->argv[0])) == NULL) 372 goto err; 373 if (F_ISSET(sp, SC_ARGRECOVER)) 374 F_SET(frp, FR_RECOVER); 375 } 376 377 if (file_init(sp, frp, NULL, 0)) 378 goto err; 379 if (EXCMD_RUNNING(gp)) { 380 (void)ex_cmd(sp); 381 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) { 382 if (screen_end(sp)) 383 goto err; 384 goto done; 385 } 386 } 387 } 388 389 /* 390 * Check to see if we need to wait for ex. If SC_SCR_EX is set, ex 391 * was forced to initialize the screen during startup. We'd like to 392 * wait for a single character from the user, but we can't because 393 * we're not in raw mode. We can't switch to raw mode because the 394 * vi initialization will switch to xterm's alternate screen, causing 395 * us to lose the messages we're pausing to make sure the user read. 396 * So, wait for a complete line. 397 */ 398 if (F_ISSET(sp, SC_SCR_EX)) { 399 p = msg_cmsg(sp, CMSG_CONT_R, &len); 400 (void)write(STDOUT_FILENO, p, len); 401 for (;;) { 402 if (v_event_get(sp, &ev, 0, 0)) 403 goto err; 404 if (ev.e_event == E_INTERRUPT || 405 ev.e_event == E_CHARACTER && 406 (ev.e_value == K_CR || ev.e_value == K_NL)) 407 break; 408 (void)gp->scr_bell(sp); 409 } 410 } 411 412 /* Switch into the right editor, regardless. */ 413 F_CLR(sp, SC_EX | SC_VI); 414 F_SET(sp, LF_ISSET(SC_EX | SC_VI) | SC_STATUS_CNT); 415 416 /* 417 * Main edit loop. Vi handles split screens itself, we only return 418 * here when switching editor modes or restarting the screen. 419 */ 420 while (sp != NULL) 421 if (F_ISSET(sp, SC_EX) ? ex(&sp) : vi(&sp)) 422 goto err; 423 424 done: rval = 0; 425 if (0) 426 err: rval = 1; 427 428 /* Clean out the global structure. */ 429 v_end(gp); 430 431 return (rval); 432 } 433 434 /* 435 * v_end -- 436 * End the program, discarding screens and most of the global area. 437 * 438 * PUBLIC: void v_end __P((GS *)); 439 */ 440 void 441 v_end(gp) 442 GS *gp; 443 { 444 MSGS *mp; 445 SCR *sp; 446 447 /* If there are any remaining screens, kill them off. */ 448 if (gp->ccl_sp != NULL) { 449 (void)file_end(gp->ccl_sp, NULL, 1); 450 (void)screen_end(gp->ccl_sp); 451 } 452 while ((sp = gp->dq.cqh_first) != (void *)&gp->dq) 453 (void)screen_end(sp); 454 while ((sp = gp->hq.cqh_first) != (void *)&gp->hq) 455 (void)screen_end(sp); 456 457 #ifdef HAVE_PERL_INTERP 458 perl_end(gp); 459 #endif 460 461 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY) 462 { FREF *frp; 463 /* Free FREF's. */ 464 while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) { 465 CIRCLEQ_REMOVE(&gp->frefq, frp, q); 466 if (frp->name != NULL) 467 free(frp->name); 468 if (frp->tname != NULL) 469 free(frp->tname); 470 free(frp); 471 } 472 } 473 474 /* Free key input queue. */ 475 if (gp->i_event != NULL) 476 free(gp->i_event); 477 478 /* Free cut buffers. */ 479 cut_close(gp); 480 481 /* Free map sequences. */ 482 seq_close(gp); 483 484 /* Free default buffer storage. */ 485 (void)text_lfree(&gp->dcb_store.textq); 486 487 /* Close message catalogs. */ 488 msg_close(gp); 489 #endif 490 491 /* Ring the bell if scheduled. */ 492 if (F_ISSET(gp, G_BELLSCHED)) 493 (void)fprintf(stderr, "\07"); /* \a */ 494 495 /* 496 * Flush any remaining messages. If a message is here, it's almost 497 * certainly the message about the event that killed us (although 498 * it's possible that the user is sourcing a file that exits from the 499 * editor). 500 */ 501 while ((mp = gp->msgq.lh_first) != NULL) { 502 (void)fprintf(stderr, "%s%.*s", 503 mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf); 504 LIST_REMOVE(mp, q); 505 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY) 506 free(mp->buf); 507 free(mp); 508 #endif 509 } 510 511 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY) 512 /* Free any temporary space. */ 513 if (gp->tmp_bp != NULL) 514 free(gp->tmp_bp); 515 516 #if defined(DEBUG) 517 /* Close debugging file descriptor. */ 518 if (gp->tracefp != NULL) 519 (void)fclose(gp->tracefp); 520 #endif 521 #endif 522 } 523 524 /* 525 * v_obsolete -- 526 * Convert historic arguments into something getopt(3) will like. 527 */ 528 static int 529 v_obsolete(name, argv) 530 char *name, *argv[]; 531 { 532 size_t len; 533 char *p; 534 535 /* 536 * Translate old style arguments into something getopt will like. 537 * Make sure it's not text space memory, because ex modifies the 538 * strings. 539 * Change "+" into "-c$". 540 * Change "+<anything else>" into "-c<anything else>". 541 * Change "-" into "-s" 542 * The c, T, t and w options take arguments so they can't be 543 * special arguments. 544 * 545 * Stop if we find "--" as an argument, the user may want to edit 546 * a file named "+foo". 547 */ 548 while (*++argv && strcmp(argv[0], "--")) 549 if (argv[0][0] == '+') { 550 if (argv[0][1] == '\0') { 551 MALLOC_NOMSG(NULL, argv[0], char *, 4); 552 if (argv[0] == NULL) 553 goto nomem; 554 (void)strcpy(argv[0], "-c$"); 555 } else { 556 p = argv[0]; 557 len = strlen(argv[0]); 558 MALLOC_NOMSG(NULL, argv[0], char *, len + 2); 559 if (argv[0] == NULL) 560 goto nomem; 561 argv[0][0] = '-'; 562 argv[0][1] = 'c'; 563 (void)strcpy(argv[0] + 2, p + 1); 564 } 565 } else if (argv[0][0] == '-') 566 if (argv[0][1] == '\0') { 567 MALLOC_NOMSG(NULL, argv[0], char *, 3); 568 if (argv[0] == NULL) { 569 nomem: v_estr(name, errno, NULL); 570 return (1); 571 } 572 (void)strcpy(argv[0], "-s"); 573 } else 574 if ((argv[0][1] == 'c' || argv[0][1] == 'T' || 575 argv[0][1] == 't' || argv[0][1] == 'w') && 576 argv[0][2] == '\0') 577 ++argv; 578 return (0); 579 } 580 581 #ifdef DEBUG 582 static void 583 attach(gp) 584 GS *gp; 585 { 586 int fd; 587 char ch; 588 589 if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) { 590 v_estr(gp->progname, errno, _PATH_TTY); 591 return; 592 } 593 594 (void)printf("process %lu waiting, enter <CR> to continue: ", 595 (u_long)getpid()); 596 (void)fflush(stdout); 597 598 do { 599 if (read(fd, &ch, 1) != 1) { 600 (void)close(fd); 601 return; 602 } 603 } while (ch != '\n' && ch != '\r'); 604 (void)close(fd); 605 } 606 #endif 607 608 static void 609 v_estr(name, eno, msg) 610 char *name, *msg; 611 int eno; 612 { 613 (void)fprintf(stderr, "%s", name); 614 if (msg != NULL) 615 (void)fprintf(stderr, ": %s", msg); 616 if (eno) 617 (void)fprintf(stderr, ": %s", strerror(errno)); 618 (void)fprintf(stderr, "\n"); 619 } 620