/* * main -- * This is the main loop for the standalone curses editor. */ int main(int argc, char *argv[]) { CL_PRIVATE *clp; GS *gp; size_t rows, cols; int rval; char *ttype; /* Create and initialize the global structure. */ __global_list = gp = gs_init(); /* Create and initialize the CL_PRIVATE structure. */ clp = cl_init(gp); /* * Initialize the terminal information. * * We have to know what terminal it is from the start, since we may * have to use termcap/terminfo to find out how big the screen is. */ if ((ttype = getenv("TERM")) == NULL) ttype = "unknown"; term_init(ttype); /* Add the terminal type to the global structure. */ if ((OG_D_STR(gp, GO_TERM) = OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL) err(1, NULL); /* Figure out how big the screen is. */ if (cl_ssize(NULL, 0, &rows, &cols, NULL)) exit (1); /* Add the rows and columns to the global structure. */ OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows; OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols; /* Ex wants stdout to be buffered. */ (void)setvbuf(stdout, NULL, _IOFBF, 0); /* Start catching signals. */ if (sig_init(gp, NULL)) exit (1); /* Run ex/vi. */ rval = editor(gp, argc, argv); /* Clean up signals. */ sig_end(gp); /* Clean up the terminal. */ (void)cl_quit(gp); /* * XXX * Reset the O_MESG option. */ if (clp->tgw != TGW_UNKNOWN) (void)cl_omesg(NULL, clp, clp->tgw == TGW_SET); /* * XXX * Reset the X11 xterm icon/window name. */ if (F_ISSET(clp, CL_RENAME)) { (void)printf(XTERM_RENAME, ttype); (void)fflush(stdout); } /* If a killer signal arrived, pretend we just got it. */ if (clp->killersig) { (void)signal(clp->killersig, SIG_DFL); (void)kill(getpid(), clp->killersig); /* NOTREACHED */ } /* Free the global and CL private areas. */ #if defined(DEBUG) || defined(PURIFY) free(clp); free(gp); #endif exit (rval); }
/* * cl_screen -- * Switch screen types. * * PUBLIC: int cl_screen __P((SCR *, u_int32_t)); */ int cl_screen(SCR *sp, u_int32_t flags) { CL_PRIVATE *clp; WINDOW *win; GS *gp; int ret, error; sigset_t oset; gp = sp->gp; clp = CLP(sp); win = CLSP(sp) ? CLSP(sp) : stdscr; ret = 0; /* * During initialization of the screen, block signals to make sure that * curses/terminfo routines are not interrupted. */ error = sigprocmask(SIG_BLOCK, &__sigblockset, &oset); /* See if the current information is incorrect. */ if (F_ISSET(gp, G_SRESTART)) { if (CLSP(sp)) { delwin(CLSP(sp)); sp->cl_private = NULL; } if (cl_quit(gp)) { ret = 1; goto end; } F_CLR(gp, G_SRESTART); } /* See if we're already in the right mode. */ if ((LF_ISSET(SC_EX) && F_ISSET(sp, SC_SCR_EX)) || (LF_ISSET(SC_VI) && F_ISSET(sp, SC_SCR_VI))) goto end; /* * Fake leaving ex mode. * * We don't actually exit ex or vi mode unless forced (e.g. by a window * size change). This is because many curses implementations can't be * called twice in a single program. Plus, it's faster. If the editor * "leaves" vi to enter ex, when it exits ex we'll just fall back into * vi. */ if (F_ISSET(sp, SC_SCR_EX)) F_CLR(sp, SC_SCR_EX); /* * Fake leaving vi mode. * * Clear out the rest of the screen if we're in the middle of a split * screen. Move to the last line in the current screen -- this makes * terminal scrolling happen naturally. Note: *don't* move past the * end of the screen, as there are ex commands (e.g., :read ! cat file) * that don't want to. Don't clear the info line, its contents may be * valid, e.g. :file|append. */ if (F_ISSET(sp, SC_SCR_VI)) { F_CLR(sp, SC_SCR_VI); if (TAILQ_NEXT(sp, q) != NULL) { (void)wmove(win, RLNO(sp, sp->rows), 0); wclrtobot(win); } (void)wmove(win, RLNO(sp, sp->rows) - 1, 0); wrefresh(win); } /* Enter the requested mode. */ if (LF_ISSET(SC_EX)) { if (cl_ex_init(sp)) { ret = 1; goto end; } F_SET(clp, CL_IN_EX | CL_SCR_EX_INIT); /* * If doing an ex screen for ex mode, move to the last line * on the screen. */ if (F_ISSET(sp, SC_EX) && clp->cup != NULL) tputs(tgoto(clp->cup, 0, O_VAL(sp, O_LINES) - 1), 1, cl_putchar); } else { if (cl_vi_init(sp)) { ret = 1; goto end; } F_CLR(clp, CL_IN_EX); F_SET(clp, CL_SCR_VI_INIT); } end: /* Unblock signals. */ if (error == 0) (void)sigprocmask(SIG_SETMASK, &oset, NULL); return ret; }
/* * main -- * This is the main loop for the standalone curses editor. */ int main(int argc, char *argv[]) { static int reenter; CL_PRIVATE *clp; GS *gp; size_t rows, cols; int rval; char *ttype; #ifdef RUNNING_IP char *ip_arg; char **p_av, **t_av; #endif /* If loaded at 0 and jumping through a NULL pointer, stop. */ if (reenter++) abort(); /* Create and initialize the global structure. */ __global_list = gp = gs_init(argv[0]); /* * Strip out any arguments that vi isn't going to understand. There's * no way to portably call getopt twice, so arguments parsed here must * be removed from the argument list. */ #ifdef RUNNING_IP ip_arg = NULL; for (p_av = t_av = argv;;) { if (*t_av == NULL) { *p_av = NULL; break; } if (!strcmp(*t_av, "--")) { while ((*p_av++ = *t_av++) != NULL); break; } if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) { if (t_av[0][2] != '\0') { ip_arg = t_av[0] + 2; ++t_av; --argc; continue; } if (t_av[1] != NULL) { ip_arg = t_av[1]; t_av += 2; argc -= 2; continue; } } *p_av++ = *t_av++; } /* * If we're being called as an editor library, we're done here, we * get loaded with the curses screen, we don't share much code. */ if (ip_arg != NULL) exit (ip_main(argc, argv, gp, ip_arg)); #endif /* Create and initialize the CL_PRIVATE structure. */ clp = cl_init(gp); /* * Initialize the terminal information. * * We have to know what terminal it is from the start, since we may * have to use termcap/terminfo to find out how big the screen is. */ if ((ttype = getenv("TERM")) == NULL) ttype = "unknown"; term_init(gp->progname, ttype); /* Add the terminal type to the global structure. */ if ((OG_D_STR(gp, GO_TERM) = OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL) perr(gp->progname, NULL); /* Figure out how big the screen is. */ if (cl_ssize(NULL, 0, &rows, &cols, NULL)) exit (1); /* Add the rows and columns to the global structure. */ OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows; OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols; /* Ex wants stdout to be buffered. */ (void)setvbuf(stdout, NULL, _IOFBF, 0); /* Start catching signals. */ if (sig_init(gp, NULL)) exit (1); /* Run ex/vi. */ rval = editor(gp, argc, argv); /* Clean up signals. */ sig_end(gp); /* Clean up the terminal. */ (void)cl_quit(gp); /* * XXX * Reset the O_MESG option. */ if (clp->tgw != TGW_UNKNOWN) (void)cl_omesg(NULL, clp, clp->tgw == TGW_SET); /* * XXX * Reset the X11 xterm icon/window name. */ if (F_ISSET(clp, CL_RENAME)) { (void)printf(XTERM_RENAME, ttype); (void)fflush(stdout); } /* If a killer signal arrived, pretend we just got it. */ if (clp->killersig) { (void)signal(clp->killersig, SIG_DFL); (void)kill(getpid(), clp->killersig); /* NOTREACHED */ } /* Free the global and CL private areas. */ #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY) free(clp); free(gp); #endif exit (rval); }
/* * main -- * This is the main loop for the standalone curses editor. */ int main(int argc, char **argv) { static int reenter; CL_PRIVATE *clp; GS *gp; WIN *wp; size_t rows, cols; int rval; char **p_av, **t_av; const char *ttype; /* If loaded at 0 and jumping through a NULL pointer, stop. */ if (reenter++) abort(); /* Create and initialize the global structure. */ __global_list = gp = gs_init(argv[0]); /* * Strip out any arguments that vi isn't going to understand. There's * no way to portably call getopt twice, so arguments parsed here must * be removed from the argument list. */ for (p_av = t_av = argv;;) { if (*t_av == NULL) { *p_av = NULL; break; } if (!strcmp(*t_av, "--")) { while ((*p_av++ = *t_av++) != NULL); break; } *p_av++ = *t_av++; } /* Create new window */ wp = gs_new_win(gp); /* Create and initialize the CL_PRIVATE structure. */ clp = cl_init(wp); /* * Initialize the terminal information. * * We have to know what terminal it is from the start, since we may * have to use termcap/terminfo to find out how big the screen is. */ if ((ttype = getenv("TERM")) == NULL) { if (isatty(STDIN_FILENO)) fprintf(stderr, "%s: warning: TERM is not set\n", gp->progname); ttype = "unknown"; } term_init(gp->progname, ttype); /* Add the terminal type to the global structure. */ if ((OG_D_STR(gp, GO_TERM) = OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL) perr(gp->progname, NULL); /* Figure out how big the screen is. */ if (cl_ssize(NULL, 0, &rows, &cols, NULL)) exit (1); /* Add the rows and columns to the global structure. */ OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows; OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols; /* Ex wants stdout to be buffered. */ (void)setvbuf(stdout, NULL, _IOFBF, 0); /* Start catching signals. */ if (sig_init(gp, NULL)) exit (1); /* Run ex/vi. */ rval = editor(wp, argc, argv); /* Clean out the global structure. */ gs_end(gp); /* Clean up signals. */ sig_end(gp); /* Clean up the terminal. */ (void)cl_quit(gp); /* * XXX * Reset the O_MESG option. */ if (clp->tgw != TGW_UNKNOWN) (void)cl_omesg(NULL, clp, clp->tgw == TGW_SET); /* * XXX * Reset the X11 xterm icon/window name. */ if (F_ISSET(clp, CL_RENAME)) cl_setname(gp, clp->oname); /* If a killer signal arrived, pretend we just got it. */ if (clp->killersig) { (void)signal(clp->killersig, SIG_DFL); (void)kill(getpid(), clp->killersig); /* NOTREACHED */ } /* Free the global and CL private areas. */ #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY) cl_end(clp); free(gp); #endif exit (rval); }