1 /* $OpenBSD: cl_main.c,v 1.20 2009/10/27 23:59:47 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 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 17 #include <bitstring.h> 18 #include <curses.h> 19 #include <errno.h> 20 #include <fcntl.h> 21 #include <signal.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <term.h> 26 #include <termios.h> 27 #include <unistd.h> 28 29 #include "../common/common.h" 30 #ifdef RUNNING_IP 31 #include "../ip/ip.h" 32 #endif 33 #include "cl.h" 34 #include "pathnames.h" 35 36 GS *__global_list; /* GLOBAL: List of screens. */ 37 sigset_t __sigblockset; /* GLOBAL: Blocked signals. */ 38 39 static void cl_func_std(GS *); 40 static CL_PRIVATE *cl_init(GS *); 41 static GS *gs_init(char *); 42 static void perr(char *, char *); 43 static int setsig(int, struct sigaction *, void (*)(int)); 44 static void sig_end(GS *); 45 static void term_init(char *, char *); 46 47 /* 48 * main -- 49 * This is the main loop for the standalone curses editor. 50 */ 51 int 52 main(int argc, char *argv[]) 53 { 54 static int reenter; 55 CL_PRIVATE *clp; 56 GS *gp; 57 size_t rows, cols; 58 int rval; 59 char *ttype; 60 #ifdef RUNNING_IP 61 char *ip_arg; 62 char **p_av, **t_av; 63 #endif 64 65 /* If loaded at 0 and jumping through a NULL pointer, stop. */ 66 if (reenter++) 67 abort(); 68 69 /* Create and initialize the global structure. */ 70 __global_list = gp = gs_init(argv[0]); 71 72 /* 73 * Strip out any arguments that vi isn't going to understand. There's 74 * no way to portably call getopt twice, so arguments parsed here must 75 * be removed from the argument list. 76 */ 77 #ifdef RUNNING_IP 78 ip_arg = NULL; 79 for (p_av = t_av = argv;;) { 80 if (*t_av == NULL) { 81 *p_av = NULL; 82 break; 83 } 84 if (!strcmp(*t_av, "--")) { 85 while ((*p_av++ = *t_av++) != NULL); 86 break; 87 } 88 if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) { 89 if (t_av[0][2] != '\0') { 90 ip_arg = t_av[0] + 2; 91 ++t_av; 92 --argc; 93 continue; 94 } 95 if (t_av[1] != NULL) { 96 ip_arg = t_av[1]; 97 t_av += 2; 98 argc -= 2; 99 continue; 100 } 101 } 102 *p_av++ = *t_av++; 103 } 104 105 /* 106 * If we're being called as an editor library, we're done here, we 107 * get loaded with the curses screen, we don't share much code. 108 */ 109 if (ip_arg != NULL) 110 exit (ip_main(argc, argv, gp, ip_arg)); 111 #endif 112 113 /* Create and initialize the CL_PRIVATE structure. */ 114 clp = cl_init(gp); 115 116 /* 117 * Initialize the terminal information. 118 * 119 * We have to know what terminal it is from the start, since we may 120 * have to use termcap/terminfo to find out how big the screen is. 121 */ 122 if ((ttype = getenv("TERM")) == NULL) 123 ttype = "unknown"; 124 term_init(gp->progname, ttype); 125 126 /* Add the terminal type to the global structure. */ 127 if ((OG_D_STR(gp, GO_TERM) = 128 OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL) 129 perr(gp->progname, NULL); 130 131 /* Figure out how big the screen is. */ 132 if (cl_ssize(NULL, 0, &rows, &cols, NULL)) 133 exit (1); 134 135 /* Add the rows and columns to the global structure. */ 136 OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows; 137 OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols; 138 139 /* Ex wants stdout to be buffered. */ 140 (void)setvbuf(stdout, NULL, _IOFBF, 0); 141 142 /* Start catching signals. */ 143 if (sig_init(gp, NULL)) 144 exit (1); 145 146 /* Run ex/vi. */ 147 rval = editor(gp, argc, argv); 148 149 /* Clean up signals. */ 150 sig_end(gp); 151 152 /* Clean up the terminal. */ 153 (void)cl_quit(gp); 154 155 /* 156 * XXX 157 * Reset the O_MESG option. 158 */ 159 if (clp->tgw != TGW_UNKNOWN) 160 (void)cl_omesg(NULL, clp, clp->tgw == TGW_SET); 161 162 /* 163 * XXX 164 * Reset the X11 xterm icon/window name. 165 */ 166 if (F_ISSET(clp, CL_RENAME)) { 167 (void)printf(XTERM_RENAME, ttype); 168 (void)fflush(stdout); 169 } 170 171 /* If a killer signal arrived, pretend we just got it. */ 172 if (clp->killersig) { 173 (void)signal(clp->killersig, SIG_DFL); 174 (void)kill(getpid(), clp->killersig); 175 /* NOTREACHED */ 176 } 177 178 /* Free the global and CL private areas. */ 179 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY) 180 free(clp); 181 free(gp); 182 #endif 183 184 exit (rval); 185 } 186 187 /* 188 * gs_init -- 189 * Create and partially initialize the GS structure. 190 */ 191 static GS * 192 gs_init(name) 193 char *name; 194 { 195 GS *gp; 196 char *p; 197 198 /* Figure out what our name is. */ 199 if ((p = strrchr(name, '/')) != NULL) 200 name = p + 1; 201 202 /* Allocate the global structure. */ 203 CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS)); 204 if (gp == NULL) 205 perr(name, NULL); 206 207 208 gp->progname = name; 209 return (gp); 210 } 211 212 /* 213 * cl_init -- 214 * Create and partially initialize the CL structure. 215 */ 216 static CL_PRIVATE * 217 cl_init(gp) 218 GS *gp; 219 { 220 CL_PRIVATE *clp; 221 int fd; 222 223 /* Allocate the CL private structure. */ 224 CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE)); 225 if (clp == NULL) 226 perr(gp->progname, NULL); 227 gp->cl_private = clp; 228 229 /* 230 * Set the CL_STDIN_TTY flag. It's purpose is to avoid setting 231 * and resetting the tty if the input isn't from there. We also 232 * use the same test to determine if we're running a script or 233 * not. 234 */ 235 if (isatty(STDIN_FILENO)) 236 F_SET(clp, CL_STDIN_TTY); 237 else 238 F_SET(gp, G_SCRIPTED); 239 240 /* 241 * We expect that if we've lost our controlling terminal that the 242 * open() (but not the tcgetattr()) will fail. 243 */ 244 if (F_ISSET(clp, CL_STDIN_TTY)) { 245 if (tcgetattr(STDIN_FILENO, &clp->orig) == -1) 246 goto tcfail; 247 } else if ((fd = open(_PATH_TTY, O_RDONLY, 0)) != -1) { 248 if (tcgetattr(fd, &clp->orig) == -1) { 249 tcfail: perr(gp->progname, "tcgetattr"); 250 exit (1); 251 } 252 (void)close(fd); 253 } 254 255 /* Initialize the list of curses functions. */ 256 cl_func_std(gp); 257 258 return (clp); 259 } 260 261 /* 262 * term_init -- 263 * Initialize terminal information. 264 */ 265 static void 266 term_init(name, ttype) 267 char *name, *ttype; 268 { 269 int err; 270 271 /* Set up the terminal database information. */ 272 setupterm(ttype, STDOUT_FILENO, &err); 273 switch (err) { 274 case -1: 275 (void)fprintf(stderr, 276 "%s: No terminal database found\n", name); 277 exit (1); 278 case 0: 279 (void)fprintf(stderr, 280 "%s: %s: unknown terminal type\n", name, ttype); 281 exit (1); 282 } 283 } 284 285 #define GLOBAL_CLP \ 286 CL_PRIVATE *clp = GCLP(__global_list); 287 static void 288 h_hup(signo) 289 int signo; 290 { 291 GLOBAL_CLP; 292 293 F_SET(clp, CL_SIGHUP); 294 clp->killersig = SIGHUP; 295 } 296 297 static void 298 h_int(signo) 299 int signo; 300 { 301 GLOBAL_CLP; 302 303 F_SET(clp, CL_SIGINT); 304 } 305 306 static void 307 h_term(signo) 308 int signo; 309 { 310 GLOBAL_CLP; 311 312 F_SET(clp, CL_SIGTERM); 313 clp->killersig = SIGTERM; 314 } 315 316 static void 317 h_winch(signo) 318 int signo; 319 { 320 GLOBAL_CLP; 321 322 F_SET(clp, CL_SIGWINCH); 323 } 324 #undef GLOBAL_CLP 325 326 /* 327 * sig_init -- 328 * Initialize signals. 329 * 330 * PUBLIC: int sig_init(GS *, SCR *); 331 */ 332 int 333 sig_init(gp, sp) 334 GS *gp; 335 SCR *sp; 336 { 337 CL_PRIVATE *clp; 338 339 clp = GCLP(gp); 340 341 if (sp == NULL) { 342 (void)sigemptyset(&__sigblockset); 343 if (sigaddset(&__sigblockset, SIGHUP) || 344 setsig(SIGHUP, &clp->oact[INDX_HUP], h_hup) || 345 sigaddset(&__sigblockset, SIGINT) || 346 setsig(SIGINT, &clp->oact[INDX_INT], h_int) || 347 sigaddset(&__sigblockset, SIGTERM) || 348 setsig(SIGTERM, &clp->oact[INDX_TERM], h_term) 349 #ifdef SIGWINCH 350 || 351 sigaddset(&__sigblockset, SIGWINCH) || 352 setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch) 353 #endif 354 ) { 355 perr(gp->progname, NULL); 356 return (1); 357 } 358 } else 359 if (setsig(SIGHUP, NULL, h_hup) || 360 setsig(SIGINT, NULL, h_int) || 361 setsig(SIGTERM, NULL, h_term) 362 #ifdef SIGWINCH 363 || 364 setsig(SIGWINCH, NULL, h_winch) 365 #endif 366 ) { 367 msgq(sp, M_SYSERR, "signal-reset"); 368 } 369 return (0); 370 } 371 372 /* 373 * setsig -- 374 * Set a signal handler. 375 */ 376 static int 377 setsig(signo, oactp, handler) 378 int signo; 379 struct sigaction *oactp; 380 void (*handler)(int); 381 { 382 struct sigaction act; 383 384 /* 385 * Use sigaction(2), not signal(3), since we don't always want to 386 * restart system calls. The example is when waiting for a command 387 * mode keystroke and SIGWINCH arrives. Besides, you can't portably 388 * restart system calls (thanks, POSIX!). On the other hand, you 389 * can't portably NOT restart system calls (thanks, Sun!). SunOS 390 * used SA_INTERRUPT as their extension to NOT restart read calls. 391 * We sure hope nobody else used it for anything else. Mom told me 392 * there'd be days like this. She just never told me that there'd 393 * be so many. 394 */ 395 act.sa_handler = handler; 396 sigemptyset(&act.sa_mask); 397 398 #ifdef SA_INTERRUPT 399 act.sa_flags = SA_INTERRUPT; 400 #else 401 act.sa_flags = 0; 402 #endif 403 return (sigaction(signo, &act, oactp)); 404 } 405 406 /* 407 * sig_end -- 408 * End signal setup. 409 */ 410 static void 411 sig_end(gp) 412 GS *gp; 413 { 414 CL_PRIVATE *clp; 415 416 clp = GCLP(gp); 417 (void)sigaction(SIGHUP, NULL, &clp->oact[INDX_HUP]); 418 (void)sigaction(SIGINT, NULL, &clp->oact[INDX_INT]); 419 (void)sigaction(SIGTERM, NULL, &clp->oact[INDX_TERM]); 420 #ifdef SIGWINCH 421 (void)sigaction(SIGWINCH, NULL, &clp->oact[INDX_WINCH]); 422 #endif 423 } 424 425 /* 426 * cl_func_std -- 427 * Initialize the standard curses functions. 428 */ 429 static void 430 cl_func_std(gp) 431 GS *gp; 432 { 433 gp->scr_addstr = cl_addstr; 434 gp->scr_attr = cl_attr; 435 gp->scr_baud = cl_baud; 436 gp->scr_bell = cl_bell; 437 gp->scr_busy = NULL; 438 gp->scr_clrtoeol = cl_clrtoeol; 439 gp->scr_cursor = cl_cursor; 440 gp->scr_deleteln = cl_deleteln; 441 gp->scr_event = cl_event; 442 gp->scr_ex_adjust = cl_ex_adjust; 443 gp->scr_fmap = cl_fmap; 444 gp->scr_insertln = cl_insertln; 445 gp->scr_keyval = cl_keyval; 446 gp->scr_move = cl_move; 447 gp->scr_msg = NULL; 448 gp->scr_optchange = cl_optchange; 449 gp->scr_refresh = cl_refresh; 450 gp->scr_rename = cl_rename; 451 gp->scr_screen = cl_screen; 452 gp->scr_suspend = cl_suspend; 453 gp->scr_usage = cl_usage; 454 } 455 456 /* 457 * perr -- 458 * Print system error. 459 */ 460 static void 461 perr(name, msg) 462 char *name, *msg; 463 { 464 (void)fprintf(stderr, "%s:", name); 465 if (msg != NULL) 466 (void)fprintf(stderr, "%s:", msg); 467 (void)fprintf(stderr, "%s\n", strerror(errno)); 468 exit(1); 469 } 470