/* * cl_rename -- * Rename the file. * * PUBLIC: int cl_rename __P((SCR *, char *, int)); */ int cl_rename(SCR *sp, char *name, int on) { CL_PRIVATE *clp; FILE *pfp; GS *gp; char buf[256], *p; gp = sp->gp; clp = CLP(sp); if (on) { clp->focus = sp; if (!F_ISSET(clp, CL_RENAME_OK)) return (0); /* * XXX * We can only rename windows for xterm. */ if (strncmp(OG_STR(gp, GO_TERM), "xterm", sizeof("xterm") - 1)) return (0); /* * XXX * Try and figure out the current name of this window. There * are two forms of the xwininfo output I've seen: * * Window id: 0x400000d "name" * Window id: 0x140000d (name) */ #define COMMAND \ "expr \"`xwininfo -id $WINDOWID | grep id:`\" : '.* [\"(]\\(.*\\)[\")]'" if (clp->oname == NULL && (pfp = popen(COMMAND, "r")) != NULL) { if (fgets(buf, sizeof(buf), pfp) != NULL && (p = strchr(buf, '\n')) != NULL) { *p = '\0'; clp->oname = strdup(buf); } (void)fclose(pfp); } cl_setname(gp, name); F_SET(clp, CL_RENAME); } else if (F_ISSET(clp, CL_RENAME)) { cl_setname(gp, clp->oname); F_CLR(clp, CL_RENAME); } return (0); }
/* * 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); }