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