/* * Set fill column to n for justify. */ int setfillcol(int f, int n) { char buf[32], *rep; const char *es; int nfill; if ((f & FFARG) != 0) { fillcol = n; } else { if ((rep = eread("Set fill-column: ", buf, sizeof(buf), EFNEW | EFCR)) == NULL) return (ABORT); else if (rep[0] == '\0') return (FALSE); nfill = strtonum(rep, 0, INT_MAX, &es); if (es != NULL) { dobeep(); ewprintf("Invalid fill column: %s", rep); return (FALSE); } fillcol = nfill; ewprintf("Fill column set to %d", fillcol); } return (TRUE); }
/* * Do the input for local-set-key, global-set-key and define-key * then call remap to do the work. */ static int dobind(KEYMAP *curmap, const char *p, int unbind) { KEYMAP *pref_map = NULL; PF funct; char bprompt[80], *bufp, *pep; int c, s, n; if (macrodef) { /* * Keystrokes aren't collected. Not hard, but pretty useless. * Would not work for function keys in any case. */ ewprintf("Can't rebind key in macro"); return (FALSE); } if (inmacro) { for (s = 0; s < maclcur->l_used - 1; s++) { if (doscan(curmap, c = CHARMASK(maclcur->l_text[s]), &curmap) != NULL) { if (remap(curmap, c, NULL, NULL) != TRUE) return (FALSE); } } (void)doscan(curmap, c = maclcur->l_text[s], NULL); maclcur = maclcur->l_fp; } else { n = strlcpy(bprompt, p, sizeof(bprompt)); if (n >= sizeof(bprompt)) n = sizeof(bprompt) - 1; pep = bprompt + n; for (;;) { ewprintf("%s", bprompt); pep[-1] = ' '; pep = getkeyname(pep, sizeof(bprompt) - (pep - bprompt), c = getkey(FALSE)); if (doscan(curmap, c, &curmap) != NULL) break; *pep++ = '-'; *pep = '\0'; } } if (unbind) funct = rescan; else { if ((bufp = eread("%s to command: ", bprompt, sizeof(bprompt), EFFUNC | EFNEW, bprompt)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); if (((funct = name_function(bprompt)) == NULL) ? (pref_map = name_map(bprompt)) == NULL : funct == NULL) { ewprintf("[No match]"); return (FALSE); } } return (remap(curmap, c, funct, pref_map)); }
/* ARGSUSED */ int usebuffer(int f, int n) { char bufn[NBUFN], *bufp; /* Get buffer to use from user */ if (curbp->b_altb == NULL) bufp = eread("Switch to buffer: ", bufn, NBUFN, EFNEW | EFBUF); else bufp = eread("Switch to buffer (default %s): ", bufn, NBUFN, EFNUL | EFNEW | EFBUF, curbp->b_altb->b_bname); if (bufp == NULL) return (FALSE); return (usebufname(bufp)); }
static void verify_sync(void) { int fd = eopen(Option.file, O_RDONLY); int rc = eread(fd, Data, sizeof(Data)); if (rc != sizeof(Data)) { fatal("Expeted %d bytes read but read %d", sizeof(Data), rc); } verify_data(); eclose(fd); }
/* ARGSUSED */ static int gid(int f, int n) { char command[NFILEN]; char cprompt[NFILEN], *bufp; int c; struct buffer *bp; struct mgwin *wp; int i, j, len; /* catch ([^\s(){}]+)[\s(){}]* */ i = curwp->w_doto; /* Skip backwards over delimiters we are currently on */ while (i > 0) { c = lgetc(curwp->w_dotp, i); if (isalnum(c) || c == '_') break; i--; } /* Skip the symbol itself */ for (; i > 0; i--) { c = lgetc(curwp->w_dotp, i - 1); if (!isalnum(c) && c != '_') break; } /* Fill the symbol in cprompt[] */ for (j = 0; j < sizeof(cprompt) - 1 && i < llength(curwp->w_dotp); j++, i++) { c = lgetc(curwp->w_dotp, i); if (!isalnum(c) && c != '_') break; cprompt[j] = c; } cprompt[j] = '\0'; if ((bufp = eread("Run gid (with args): ", cprompt, NFILEN, (j ? EFDEF : 0) | EFNEW | EFCR)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); len = snprintf(command, sizeof(command), "gid %s", cprompt); if (len < 0 || len >= sizeof(command)) return (FALSE); if ((bp = compile_mode("*gid*", command)) == NULL) return (FALSE); if ((wp = popbuf(bp, WNONE)) == NULL) return (FALSE); curbp = bp; compile_win = curwp = wp; return (TRUE); }
/* ARGSUSED */ int filewrite(int f, int n) { struct stat statbuf; int s; char fname[NFILEN], bn[NBUFN], tmp[NFILEN + 25]; char *adjfname, *bufp; FILE *ffp; if (getbufcwd(fname, sizeof(fname)) != TRUE) fname[0] = '\0'; if ((bufp = eread("Write file: ", fname, NFILEN, EFDEF | EFNEW | EFCR | EFFILE)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); adjfname = adjustname(fname, TRUE); if (adjfname == NULL) return (FALSE); /* Check if file exists; write checks done later */ if (stat(adjfname, &statbuf) == 0) { if (S_ISDIR(statbuf.st_mode)) { dobeep(); ewprintf("%s is a directory", adjfname); return (FALSE); } snprintf(tmp, sizeof(tmp), "File `%s' exists; overwrite", adjfname); if ((s = eyorn(tmp)) != TRUE) return (s); } /* old attributes are no longer current */ bzero(&curbp->b_fi, sizeof(curbp->b_fi)); if ((s = writeout(&ffp, curbp, adjfname)) == TRUE) { (void)strlcpy(curbp->b_fname, adjfname, sizeof(curbp->b_fname)); if (getbufcwd(curbp->b_cwd, sizeof(curbp->b_cwd)) != TRUE) (void)strlcpy(curbp->b_cwd, "/", sizeof(curbp->b_cwd)); if (augbname(bn, curbp->b_fname, sizeof(bn)) == FALSE) return (FALSE); free(curbp->b_bname); if ((curbp->b_bname = strdup(bn)) == NULL) return (FALSE); (void)fupdstat(curbp); curbp->b_flag &= ~BFCHG; upmodes(curbp); undo_add_boundary(FFRAND, 1); undo_add_modified(); } return (s); }
/* Read the ELF header from a file descriptor and stuff it into a structure. Make sure it is really an elf header etc. */ static void read_elf_header(int fd, Elf_Ehdr *ehp) { eread(fd, ehp, sizeof(Elf_Ehdr), __func__); if (strncmp((const char *) ehp->e_ident, elf_magic_string, 4)) { fprintf(stderr, "Bad ELF magic number --- not an elf file. Exiting in %s.\n", __func__); exit(-1); } }
/* ARGSUSED */ int evalexpr(int f, int n) { char exbuf[128], *bufp; if ((bufp = eread("Eval: ", exbuf, sizeof(exbuf), EFNEW | EFCR)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); return (excline(exbuf)); }
/* ARGSUSED */ int evalfile(int f, int n) { char fname[NFILEN], *bufp; if ((bufp = eread("Load file: ", fname, NFILEN, EFNEW | EFCR)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); return (load(fname)); }
/* * Read a pattern. * Stash it in the external variable "re_pat". The "pat" is * not updated if the user types in an empty line. If the user typed * an empty line, and there is no old pattern, it is an error. * Display the old pattern, in the style of Jeff Lomicka. There is * some do-it-yourself control expansion. */ static int re_readpattern(char *prompt) { static int dofree = 0; int flags, error, s; char tpat[NPAT], *rep; if (re_pat[0] == '\0') rep = eread("%s: ", tpat, NPAT, EFNEW | EFCR, prompt); else rep = eread("%s: (default %s) ", tpat, NPAT, EFNUL | EFNEW | EFCR, prompt, re_pat); if (rep == NULL) return (ABORT); if (rep[0] != '\0') { /* New pattern given */ (void)strlcpy(re_pat, tpat, sizeof(re_pat)); if (casefoldsearch) flags = REG_EXTENDED | REG_ICASE; else flags = REG_EXTENDED; if (dofree) regfree(&re_buff); error = regcomp(&re_buff, re_pat, flags); if (error != 0) { char message[256]; regerror(error, &re_buff, message, sizeof(message)); ewprintf("Regex Error: %s", message); re_pat[0] = '\0'; return (FALSE); } dofree = 1; s = TRUE; } else if (rep[0] == '\0' && re_pat[0] != '\0') /* Just using old pattern */ s = TRUE; else s = FALSE; return (s); }
/* * Ask user for a tag while treating word at dot as default. Visit tags * file if not yet done, load tags and jump to definition of the tag. */ int findtag(int f, int n) { char utok[MAX_TOKEN], dtok[MAX_TOKEN]; char *tok, *bufp; int ret; if (curtoken(f, n, dtok) == FALSE) { dtok[0] = '\0'; bufp = eread("Find tag:", utok, MAX_TOKEN, EFNUL | EFNEW); } else bufp = eread("Find tag (default %s):", utok, MAX_TOKEN, EFNUL | EFNEW, dtok); if (bufp == NULL) return (ABORT); else if (bufp[0] == '\0') tok = dtok; else tok = utok; if (tok[0] == '\0') { dobeep(); ewprintf("There is no default tag"); return (FALSE); } if (tagsfn == NULL) if ((ret = tagsvisit(f, n)) != TRUE) return (ret); if (!loaded) { if (loadtags(tagsfn) == FALSE) { free(tagsfn); tagsfn = NULL; return (FALSE); } loaded = TRUE; } return pushtag(tok); }
/* * Like eyorn, but for more important questions. User must type all of * "yes" or "no" and the trailing newline. */ int eyesno(const char *sp) { char buf[64], *rep; #ifndef NO_MACRO if (inmacro) return (TRUE); #endif /* !NO_MACRO */ rep = eread("%s? (yes or no) ", buf, sizeof(buf), EFNUL | EFNEW | EFCR, sp); for (;;) { if (rep == NULL) return (ABORT); if (rep[0] != '\0') { #ifndef NO_MACRO if (macrodef) { struct line *lp = maclcur; maclcur = lp->l_bp; maclcur->l_fp = lp->l_fp; free(lp); } #endif /* !NO_MACRO */ if ((rep[0] == 'y' || rep[0] == 'Y') && (rep[1] == 'e' || rep[1] == 'E') && (rep[2] == 's' || rep[2] == 'S') && (rep[3] == '\0')) return (TRUE); if ((rep[0] == 'n' || rep[0] == 'N') && (rep[1] == 'o' || rep[0] == 'O') && (rep[2] == '\0')) return (FALSE); } rep = eread("Please answer yes or no. %s? (yes or no) ", buf, sizeof(buf), EFNUL | EFNEW | EFCR, sp); } /* NOTREACHED */ }
/* * Read a pattern. Stash it in the external variable "pat". The "pat" is not * updated if the user types in an empty line. If the user typed an empty * line, and there is no old pattern, it is an error. Display the old pattern, * in the style of Jeff Lomicka. There is some do-it-yourself control * expansion. */ int readpattern(char *prompt) { char tpat[NPAT], *rep; int retval; if (pat[0] == '\0') rep = eread("%s: ", tpat, NPAT, EFNEW | EFCR, prompt); else rep = eread("%s: (default %s) ", tpat, NPAT, EFNUL | EFNEW | EFCR, prompt, pat); /* specified */ if (rep == NULL) { retval = ABORT; } else if (rep[0] != '\0') { (void)strlcpy(pat, tpat, sizeof(pat)); retval = TRUE; } else if (pat[0] != '\0') { retval = TRUE; } else retval = FALSE; return (retval); }
/* ARGSUSED */ int setprefix(int f, int n) { char buf[PREFIXLENGTH], *rep; int retval; if (prefix_string[0] == '\0') rep = eread("Prefix string: ", buf, sizeof(buf), EFNEW | EFCR); else rep = eread("Prefix string (default %s): ", buf, sizeof(buf), EFNUL | EFNEW | EFCR, prefix_string); if (rep == NULL) return (ABORT); if (rep[0] != '\0') { (void)strlcpy(prefix_string, rep, sizeof(prefix_string)); retval = TRUE; } else if (rep[0] == '\0' && prefix_string[0] != '\0') { /* CR -- use old one */ retval = TRUE; } else retval = FALSE; return (retval); }
/* ARGSUSED */ int gotoline(int f, int n) { struct line *clp; char buf[32], *bufp; const char *err; if (!(f & FFARG)) { if ((bufp = eread("Goto line: ", buf, sizeof(buf), EFNUL | EFNEW | EFCR)) == NULL) return (ABORT); if (bufp[0] == '\0') return (ABORT); n = (int)strtonum(buf, INT_MIN, INT_MAX, &err); if (err) { ewprintf("Line number %s", err); return (FALSE); } } if (n >= 0) { if (n == 0) n++; curwp->w_dotline = n; clp = lforw(curbp->b_headp); /* "clp" is first line */ while (--n > 0) { if (lforw(clp) == curbp->b_headp) { curwp->w_dotline = curwp->w_bufp->b_lines; break; } clp = lforw(clp); } } else { curwp->w_dotline = curwp->w_bufp->b_lines + n; clp = lback(curbp->b_headp); /* "clp" is last line */ while (n < 0) { if (lback(clp) == curbp->b_headp) { curwp->w_dotline = 1; break; } clp = lback(clp); n++; } } curwp->w_dotp = clp; curwp->w_doto = 0; curwp->w_rflag |= WFMOVE; return (TRUE); }
/* ARGSUSED */ int filevisitalt(int f, int n) { char fname[NFILEN], *bufp; if (getbufcwd(fname, sizeof(fname)) != TRUE) fname[0] = '\0'; bufp = eread("Find alternate file: ", fname, NFILEN, EFNEW | EFCR | EFFILE | EFDEF); if (bufp == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); return (do_filevisitalt(fname)); }
/* * (re)fill block buffers from archive. `justhdr' means we don't care * about the data before the next header block. */ static char * refill(int ar, char *bufs, int justhdr) { int i, n; unsigned bytes = Tblock * nblock; static int done, first = 1, seekable; if (done) return nil; blkoff = seek(ar, 0, 1); /* note position for `tar r' */ if (first) seekable = blkoff >= 0; /* try to size non-pipe input at first read */ if (first && usefile && !fixednblock) { n = eread(arname, ar, bufs, bytes); if (n == 0) sysfatal("EOF reading archive %s: %r", arname); i = n; if (i % Tblock != 0) sysfatal("%s: archive block size (%d) error", arname, i); i /= Tblock; if (i != nblock) { nblock = i; fprint(2, "%s: blocking = %d\n", argv0, nblock); endblk = (Hdr *)bufs + nblock; bytes = n; } } else if (justhdr && seekable && nexthdr - blkoff >= bytes) { /* optimisation for huge archive members on seekable media */ if (seek(ar, bytes, 1) < 0) sysfatal("can't seek on archive %s: %r", arname); n = bytes; } else n = ereadn(arname, ar, bufs, bytes); first = 0; if (n == 0) sysfatal("unexpected EOF reading archive %s", arname); if (n % Tblock != 0) sysfatal("partial block read from archive %s", arname); if (n != bytes) { done = 1; memset(bufs + n, 0, bytes - n); } return bufs; }
int ask_makedir(void) { char bufc[NFILEN]; char *path; if (getbufcwd(bufc, sizeof(bufc)) != TRUE) return (ABORT); if ((path = eread("Make directory: ", bufc, NFILEN, EFDEF | EFNEW | EFCR | EFFILE)) == NULL) return (ABORT); else if (path[0] == '\0') return (FALSE); return (do_makedir(path)); }
/* ARGSUSED */ int killbuffer_cmd(int f, int n) { struct buffer *bp; char bufn[NBUFN], *bufp; if (f & FFRAND) /* dired mode 'q' */ bp = curbp; else if ((bufp = eread("Kill buffer: (default %s) ", bufn, NBUFN, EFNUL | EFNEW | EFBUF, curbp->b_bname)) == NULL) return (ABORT); else if (bufp[0] == '\0') bp = curbp; else if ((bp = bfind(bufn, FALSE)) == NULL) return (FALSE); return (killbuffer(bp)); }
/* ARGSUSED */ int fileinsert(int f, int n) { char fname[NFILEN], *bufp, *adjf; if (getbufcwd(fname, sizeof(fname)) != TRUE) fname[0] = '\0'; bufp = eread("Insert file: ", fname, NFILEN, EFNEW | EFCR | EFFILE | EFDEF); if (bufp == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); adjf = adjustname(bufp, TRUE); if (adjf == NULL) return (FALSE); return (insertfile(adjf, NULL, FALSE)); }
/*ARGSUSED */ int shellcommand(int f, int n) { char *cmd, cmdbuf[NFILEN]; char *argv[] = {"sh", "-c", (char *) NULL, (char *) NULL}; if (n > 1) return (ABORT); if ((cmd = eread("Shell command: ", cmdbuf, sizeof(cmdbuf), EFNEW | EFCR)) == NULL || (cmd[0] == '\0')) return (ABORT); argv[2] = cmd; return shellcmdoutput(argv, NULL, 0); }
/* ARGSUSED */ int gotoline(int f, int n) { char buf[32], *bufp; const char *err; if (!(f & FFARG)) { if ((bufp = eread("Goto line: ", buf, sizeof(buf), EFNUL | EFNEW | EFCR)) == NULL) return (ABORT); if (bufp[0] == '\0') return (ABORT); n = (int)strtonum(buf, INT_MIN, INT_MAX, &err); if (err) { dobeep(); ewprintf("Line number %s", err); return (FALSE); } } return(setlineno(n)); }
/*ARGSUSED */ int piperegion(int f, int n) { struct region region; int len; char *cmd, cmdbuf[NFILEN], *text; char *argv[] = {"sh", "-c", (char *) NULL, (char *) NULL}; /* C-u M-| is not supported yet */ if (n > 1) return (ABORT); if (curwp->w_markp == NULL) { dobeep(); ewprintf("The mark is not set now, so there is no region"); return (FALSE); } if ((cmd = eread("Shell command on region: ", cmdbuf, sizeof(cmdbuf), EFNEW | EFCR)) == NULL || (cmd[0] == '\0')) return (ABORT); argv[2] = cmd; if (getregion(®ion) != TRUE) return (FALSE); len = region.r_size; if ((text = malloc(len + 1)) == NULL) { dobeep(); ewprintf("Cannot allocate memory."); return (FALSE); } region_get_data(®ion, text, len); return shellcmdoutput(argv, text, len); }
/* ARGSUSED */ static int xlint(int f, int n) { char cprompt[NFILEN], *bufp; struct buffer *bp; struct mgwin *wp; (void)strlcpy(cprompt, "make lint ", sizeof(cprompt)); if ((bufp = eread("Run lint: ", cprompt, NFILEN, EFDEF | EFNEW | EFCR)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); if ((bp = compile_mode("*lint*", cprompt)) == NULL) return (FALSE); if ((wp = popbuf(bp, WNONE)) == NULL) return (FALSE); curbp = bp; compile_win = curwp = wp; return (TRUE); }
/* ARGSUSED */ int redefine_key(int f, int n) { static char buf[48]; char tmp[32], *bufp; KEYMAP *mp; (void)strlcpy(buf, "Define key map: ", sizeof(buf)); if ((bufp = eread(buf, tmp, sizeof(tmp), EFNEW)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); (void)strlcat(buf, tmp, sizeof(buf)); if ((mp = name_map(tmp)) == NULL) { ewprintf("Unknown map %s", tmp); return (FALSE); } if (strlcat(buf, "key: ", sizeof(buf)) >= sizeof(buf)) return (FALSE); return (dobind(mp, buf, FALSE)); }
/* ARGSUSED */ int d_create_directory(int f, int n) { char tocreate[MAXPATHLEN], *bufp; size_t off; struct buffer *bp; off = strlcpy(tocreate, curbp->b_fname, sizeof(tocreate)); if (off >= sizeof(tocreate) - 1) return (FALSE); if ((bufp = eread("Create directory: ", tocreate, sizeof(tocreate), EFDEF | EFNEW | EFCR)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); if (mkdir(tocreate, 0755) == -1) { ewprintf("Creating directory: %s, %s", strerror(errno), tocreate); return (FALSE); } bp = dired_(curbp->b_fname); return (showbuffer(bp, curwp, WFFULL | WFMODE)); }
/* ARGSUSED */ int changedir(int f, int n) { char bufc[NFILEN], *bufp; (void)strlcpy(bufc, mgcwd, sizeof(bufc)); if ((bufp = eread("Change default directory: ", bufc, NFILEN, EFDEF | EFNEW | EFCR | EFFILE)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); /* Append trailing slash */ if (chdir(bufc) == -1) { ewprintf("Can't change dir to %s", bufc); return (FALSE); } if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL) panic("Can't get current directory!"); if (mgcwd[strlen(mgcwd) - 1] != '/') (void)strlcat(mgcwd, "/", sizeof(mgcwd)); ewprintf("Current directory is now %s", bufp); return (TRUE); }
/* ARGSUSED */ static int grep(int f, int n) { char cprompt[NFILEN], *bufp; struct buffer *bp; struct mgwin *wp; (void)strlcpy(cprompt, "grep -n ", sizeof(cprompt)); if ((bufp = eread("Run grep: ", cprompt, NFILEN, EFDEF | EFNEW | EFCR)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); if (strlcat(cprompt, " /dev/null", sizeof(cprompt)) >= sizeof(cprompt)) return (FALSE); if ((bp = compile_mode("*grep*", cprompt)) == NULL) return (FALSE); if ((wp = popbuf(bp, WNONE)) == NULL) return (FALSE); curbp = bp; compile_win = curwp = wp; return (TRUE); }
/* ARGSUSED */ int insert(int f, int n) { char buf[128], *bufp, *cp; int count, c; if (inmacro) { while (--n >= 0) { for (count = 0; count < maclcur->l_used; count++) { if ((((c = maclcur->l_text[count]) == '\n') ? lnewline() : linsert(1, c)) != TRUE) return (FALSE); } } maclcur = maclcur->l_fp; return (TRUE); } if (n == 1) /* CFINS means selfinsert can tack on the end */ thisflag |= CFINS; if ((bufp = eread("Insert: ", buf, sizeof(buf), EFNEW)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); while (--n >= 0) { cp = buf; while (*cp) { if (((*cp == '\n') ? lnewline() : linsert(1, *cp)) != TRUE) return (FALSE); cp++; } } return (TRUE); }
/* * Set fill column to n for justify. */ int setfillcol(int f, int n) { char buf[32], *rep; int nfill; if ((f & FFARG) != 0) { fillcol = n; } else { if ((rep = eread("Set fill-column: ", buf, sizeof(buf), EFNEW | EFCR)) == NULL) return (ABORT); else if (rep[0] == '\0') return (FALSE); nfill = atoi(rep); if (nfill < 0 || nfill > INT_MAX) { dobeep(); return (FALSE); } fillcol = nfill; ewprintf("Fill column set to %d", fillcol); } return (TRUE); }