int shellcmdoutput(char* const argv[], char* const text, int len) { struct buffer *bp; char *shellp; int ret; bp = bfind("*Shell Command Output*", TRUE); bp->b_flag |= BFREADONLY; if (bclear(bp) != TRUE) { free(text); return (FALSE); } shellp = getenv("SHELL"); ret = pipeio(shellp, argv, text, len, bp); if (ret == TRUE) { eerase(); if (lforw(bp->b_headp) == bp->b_headp) addline(bp, "(Shell command succeeded with no output)"); } free(text); return (ret); }
int main(int argc, char **argv) { char *cp, *init_fcn_name = NULL; PF init_fcn = NULL; int o, i, nfiles; int nobackups = 0; struct buffer *bp = NULL; #ifdef MRUBY mrb_mg_init(); int noinitfile = 0; #endif /* MRUBY */ while ((o = getopt(argc, argv, "nf:q")) != -1) switch (o) { case 'n': nobackups = 1; break; case 'f': if (init_fcn_name != NULL) errx(1, "cannot specify more than one " "initial function"); init_fcn_name = optarg; break; case 'q': noinitfile = 1; break; default: usage(); } argc -= optind; argv += optind; maps_init(); /* Keymaps and modes. */ funmap_init(); /* Functions. */ /* * This is where we initialize standalone extensions that should * be loaded dynamically sometime in the future. */ { extern void grep_init(void); extern void theo_init(void); extern void cmode_init(void); extern void dired_init(void); dired_init(); grep_init(); theo_init(); cmode_init(); #ifdef UTF8 utf8_init(); #endif /* UTF8 */ } if (init_fcn_name && (init_fcn = name_function(init_fcn_name)) == NULL) errx(1, "Unknown function `%s'", init_fcn_name); vtinit(); /* Virtual terminal. */ dirinit(); /* Get current directory. */ edinit(bp); /* Buffers, windows. */ ttykeymapinit(); /* Symbols, bindings. */ /* * doing update() before reading files causes the error messages from * the file I/O show up on the screen. (and also an extra display of * the mode line if there are files specified on the command line.) */ update(); /* user startup file */ #ifdef MRUBY if (noinitfile == 0 && (cp = startupfile(NULL)) != NULL) mrb_mg_load(cp); #else if ((cp = startupfile(NULL)) != NULL) (void)load(cp); #endif /* MRUBY */ /* * Now ensure any default buffer modes from the startup file are * given to any files opened when parsing the startup file. * Note *scratch* will also be updated. */ for (bp = bheadp; bp != NULL; bp = bp->b_bufp) { bp->b_flag = defb_flag; for (i = 0; i <= defb_nmodes; i++) { bp->b_modes[i] = defb_modes[i]; } } /* Force FFOTHARG=1 so that this mode is enabled, not simply toggled */ if (init_fcn) init_fcn(FFOTHARG, 1); if (nobackups) makebkfile(FFARG, 0); for (nfiles = 0, i = 0; i < argc; i++) { if (argv[i][0] == '+' && strlen(argv[i]) >= 2) { long long lval; const char *errstr; lval = strtonum(&argv[i][1], INT_MIN, INT_MAX, &errstr); if (argv[i][1] == '\0' || errstr != NULL) goto notnum; startrow = lval; } else { notnum: cp = adjustname(argv[i], FALSE); if (cp != NULL) { if (nfiles == 1) splitwind(0, 1); if ((curbp = findbuffer(cp)) == NULL) { vttidy(); errx(1, "Can't find current buffer!"); } (void)showbuffer(curbp, curwp, 0); if (readin(cp) != TRUE) killbuffer(curbp); else { /* Ensure enabled, not just toggled */ if (init_fcn_name) init_fcn(FFOTHARG, 1); nfiles++; } } } } if (nfiles > 2) listbuffers(0, 1); /* fake last flags */ thisflag = 0; for (;;) { if (epresf == KCLEAR) eerase(); if (epresf == TRUE) epresf = KCLEAR; if (winch_flag) { do_redraw(0, 0, TRUE); winch_flag = 0; } update(); lastflag = thisflag; thisflag = 0; switch (doin()) { case TRUE: break; case ABORT: ewprintf("Quit"); /* FALLTHRU */ case FALSE: default: ttbeep(); macrodef = FALSE; } } }
/*ARGSUSED */ int diffbuffer(int f, int n) { struct buffer *bp; struct line *lp, *lpend; size_t len; int ret; char *text, *ttext; char * const argv[] = {DIFFTOOL, "-u", "-p", curbp->b_fname, "-", (char *)NULL}; len = 0; /* C-u is not supported */ if (n > 1) return (ABORT); if (access(DIFFTOOL, X_OK) != 0) { dobeep(); ewprintf("%s not found or not executable.", DIFFTOOL); return (FALSE); } if (curbp->b_fname[0] == 0) { dobeep(); ewprintf("Cannot diff buffer not associated with any files."); return (FALSE); } lpend = curbp->b_headp; for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) { len+=llength(lp); if (lforw(lp) != lpend) /* no implied \n on last line */ len++; } if ((text = calloc(len + 1, sizeof(char))) == NULL) { dobeep(); ewprintf("Cannot allocate memory."); return (FALSE); } ttext = text; for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) { if (llength(lp) != 0) { memcpy(ttext, ltext(lp), llength(lp)); ttext += llength(lp); } if (lforw(lp) != lpend) /* no implied \n on last line */ *ttext++ = '\n'; } bp = bfind("*Diff*", TRUE); bp->b_flag |= BFREADONLY; if (bclear(bp) != TRUE) { free(text); return (FALSE); } ret = pipeio(DIFFTOOL, argv, text, len, bp); if (ret == TRUE) { eerase(); if (lforw(bp->b_headp) == bp->b_headp) addline(bp, "Diff finished (no differences)."); } free(text); return (ret); }
int do_makedir(char *path) { struct stat sb; int finished, ishere; mode_t dir_mode, f_mode, oumask; char *slash; if ((path = adjustname(path, TRUE)) == NULL) return (FALSE); /* Remove trailing slashes */ slash = strrchr(path, '\0'); while (--slash > path && *slash == '/') *slash = '\0'; slash = path; oumask = umask(0); f_mode = 0777 & ~oumask; dir_mode = f_mode | S_IWUSR | S_IXUSR; for (;;) { slash += strspn(slash, "/"); slash += strcspn(slash, "/"); finished = (*slash == '\0'); *slash = '\0'; ishere = !stat(path, &sb); if (finished && ishere) { dobeep(); ewprintf("Cannot create directory %s: file exists", path); return(FALSE); } else if (!finished && ishere && S_ISDIR(sb.st_mode)) { *slash = '/'; continue; } if (mkdir(path, finished ? f_mode : dir_mode) == 0) { if (f_mode > 0777 && chmod(path, f_mode) < 0) { umask(oumask); return (ABORT); } } else { if (!ishere || !S_ISDIR(sb.st_mode)) { if (!ishere) { dobeep(); ewprintf("Creating directory: " "permission denied, %s", path); } else eerase(); umask(oumask); return (FALSE); } } if (finished) break; *slash = '/'; } eerase(); umask(oumask); return (TRUE); }
int main(int argc, char **argv) { char *cp, *init_fcn_name = NULL; PF init_fcn = NULL; int o, i, nfiles; struct buffer *bp = NULL; while ((o = getopt(argc, argv, "f:")) != -1) switch (o) { case 'f': if (init_fcn_name != NULL) errx(1, "cannot specify more than one " "initial function"); init_fcn_name = optarg; break; default: usage(); } argc -= optind; argv += optind; setlocale(LC_CTYPE, ""); maps_init(); /* Keymaps and modes. */ funmap_init(); /* Functions. */ if (init_fcn_name && (init_fcn = name_function(init_fcn_name)) == NULL) errx(1, "Unknown function `%s'", init_fcn_name); vtinit(); /* Virtual terminal. */ dirinit(); /* Get current directory. */ edinit(bp); /* Buffers, windows. */ bellinit(); /* Audible and visible bell. */ /* * doing update() before reading files causes the error messages from * the file I/O show up on the screen. (and also an extra display of * the mode line if there are files specified on the command line.) */ update(CMODE); /* Force FFOTHARG=1 so that this mode is enabled, not simply toggled */ if (init_fcn) init_fcn(FFOTHARG, 1); for (nfiles = 0, i = 0; i < argc; i++) { if (argv[i][0] == '+' && strlen(argv[i]) >= 2) { long lval; char *end; lval = strtol(&argv[i][1], &end, 0); if (argv[i][1] == '\0' || *end != '\0') goto notnum; startrow = lval; } else { notnum: cp = adjustname(argv[i], FALSE); if (cp != NULL) { if (nfiles == 1) splitwind(0, 1); if ((curbp = findbuffer(cp)) == NULL) { vttidy(); errx(1, "Can't find current buffer!"); } (void)showbuffer(curbp, curwp, 0); if (readin(cp) != TRUE) killbuffer(curbp); else { /* Ensure enabled, not just toggled */ if (init_fcn_name) init_fcn(FFOTHARG, 1); nfiles++; } } } } if (nfiles > 2) listbuffers(0, 1); /* fake last flags */ thisflag = 0; for (;;) { if (epresf == KCLEAR) eerase(); if (epresf == TRUE) epresf = KCLEAR; if (winch_flag) { do_redraw(0, 0, TRUE); winch_flag = 0; } update(CMODE); lastflag = thisflag; thisflag = 0; switch (doin()) { case TRUE: break; case ABORT: ewprintf("Quit"); /* FALLTHRU */ case FALSE: default: macrodef = FALSE; } } }