diff options
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/mg/buffer.c | 69 | ||||
-rw-r--r-- | usr.bin/mg/def.h | 11 | ||||
-rw-r--r-- | usr.bin/mg/dir.c | 31 | ||||
-rw-r--r-- | usr.bin/mg/dired.c | 5 | ||||
-rw-r--r-- | usr.bin/mg/file.c | 59 | ||||
-rw-r--r-- | usr.bin/mg/grep.c | 98 |
6 files changed, 138 insertions, 135 deletions
diff --git a/usr.bin/mg/buffer.c b/usr.bin/mg/buffer.c index 1d9fc42ef3b..bc4b5cb8860 100644 --- a/usr.bin/mg/buffer.c +++ b/usr.bin/mg/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.56 2006/04/06 05:28:17 kjell Exp $ */ +/* $OpenBSD: buffer.c,v 1.57 2006/05/02 17:10:25 kjell Exp $ */ /* This file is in the public domain. */ @@ -13,6 +13,7 @@ #include "kbd.h" /* needed for modes */ static struct buffer *makelist(void); +static struct buffer *bnew(void); /* ARGSUSED */ int @@ -457,16 +458,13 @@ anycb(int f) /* * Search for a buffer, by name. * If not found, and the "cflag" is TRUE, - * create a buffer and put it in the list of - * all buffers. Return pointer to the BUFFER - * block for the buffer. + * create a new buffer. Return pointer to the found + * (or new) buffer. */ struct buffer * bfind(const char *bname, int cflag) { struct buffer *bp; - struct line *lp; - int i; bp = bheadp; while (bp != NULL) { @@ -477,18 +475,34 @@ bfind(const char *bname, int cflag) if (cflag != TRUE) return (NULL); - bp = calloc(1, sizeof(struct buffer)); - if (bp == NULL) { - ewprintf("Can't get %d bytes", sizeof(struct buffer)); - return (NULL); - } + bp = bnew(); + if ((bp->b_bname = strdup(bname)) == NULL) { ewprintf("Can't get %d bytes", strlen(bname) + 1); free(bp); return (NULL); } + + return (bp); +} + +/* + * Create a new buffer and put it in the list of + * all buffers. + */ +static struct buffer * +bnew() +{ + struct buffer *bp; + struct line *lp; + int i; + + bp = calloc(1, sizeof(struct buffer)); + if (bp == NULL) { + ewprintf("Can't get %d bytes", sizeof(struct buffer)); + return (NULL); + } if ((lp = lalloc(0)) == NULL) { - free(bp->b_bname); free(bp); return (NULL); } @@ -509,11 +523,13 @@ bfind(const char *bname, int cflag) bp->b_modes[i] = defb_modes[i]; } while (i++ < defb_nmodes); bp->b_fname[0] = '\0'; + bp->b_cwd[0] = '\0'; bzero(&bp->b_fi, sizeof(bp->b_fi)); lp->l_fp = lp; lp->l_bp = lp; bp->b_bufp = bheadp; bheadp = bp; + return (bp); } @@ -602,7 +618,7 @@ showbuffer(struct buffer *bp, struct mgwin *wp, int flags) * include the number, if necessary. */ int -baugname(char *bn, const char *fn, size_t bs) +augbname(char *bn, const char *fn, size_t bs) { int count; size_t remain, len; @@ -741,3 +757,30 @@ popbuftop(struct buffer *bp) return (popbuf(bp) != NULL); } #endif + +/* + * Return the working directory for the current buffer, terminated + * with a '/'. First, try to extract it from the current buffer's + * filename. If that fails, use global cwd. + */ +int +getbufcwd(char *path, size_t plen) +{ + char cwd[NFILEN]; + + if (plen == 0) + return (FALSE); + + if (curbp->b_cwd[0] != '\0') { + (void)strlcpy(path, curbp->b_cwd, plen); + } else { + if (getcwdir(cwd, sizeof(cwd)) == FALSE) + goto error; + (void)strlcpy(path, cwd, plen); + } + return (TRUE); +error: + path[0] = '\0'; + return (FALSE); +} + diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index a9549d5e195..312f721e477 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.85 2006/04/06 05:28:17 kjell Exp $ */ +/* $OpenBSD: def.h,v 1.86 2006/05/02 17:10:25 kjell Exp $ */ /* This file is in the public domain. */ @@ -249,6 +249,7 @@ struct buffer { char b_nwnd; /* Count of windows on buffer */ char b_flag; /* Flags */ char b_fname[NFILEN]; /* File name */ + char b_cwd[NFILEN]; /* working directory */ struct fileinfo b_fi; /* File attributes */ LIST_HEAD(, undo_rec) b_undo; /* Undo actions list */ int b_undopos; /* Where we were during last undo */ @@ -316,6 +317,7 @@ int charswaiting(void); void dirinit(void); int changedir(int, int); int showcwdir(int, int); +int getcwdir(char *, size_t); /* dired.c */ struct buffer *dired_(char *); @@ -375,7 +377,7 @@ struct mgwin *wpopup(void); /* buffer.c */ int togglereadonly(int, int); -struct buffer *bfind(const char *, int); +struct buffer *bfind(const char *, int); int poptobuffer(int, int); int killbuffer(struct buffer *); int killbuffer_cmd(int, int); @@ -386,12 +388,13 @@ int addlinef(struct buffer *, char *, ...); int anycb(int); int bclear(struct buffer *); int showbuffer(struct buffer *, struct mgwin *, int); -int baugname(char *, const char *, size_t); -struct mgwin *popbuf(struct buffer *); +int augbname(char *, const char *, size_t); +struct mgwin *popbuf(struct buffer *); int bufferinsert(int, int); int usebuffer(int, int); int notmodified(int, int); int popbuftop(struct buffer *); +int getbufcwd(char *, size_t); /* display.c */ int vtresize(int, int, int); diff --git a/usr.bin/mg/dir.c b/usr.bin/mg/dir.c index 656b6c08f14..9f82d5f3fd0 100644 --- a/usr.bin/mg/dir.c +++ b/usr.bin/mg/dir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dir.c,v 1.16 2005/12/20 05:04:28 kjell Exp $ */ +/* $OpenBSD: dir.c,v 1.17 2006/05/02 17:10:25 kjell Exp $ */ /* This file is in the public domain. */ @@ -11,8 +11,7 @@ #include "def.h" -char *wdir; -static char cwd[NFILEN]; +static char mgcwd[NFILEN]; /* * Initialize anything the directory management routines need. @@ -20,11 +19,12 @@ static char cwd[NFILEN]; void dirinit(void) { - if ((wdir = getcwd(cwd, sizeof(cwd))) == NULL) { + mgcwd[0] = '\0'; + if (getcwd(mgcwd, sizeof(mgcwd)) == NULL) { ewprintf("Can't get current directory!"); chdir("/"); - (void)strlcpy(cwd, "/", sizeof(cwd)); } + (void)strlcat(mgcwd, "/", sizeof(mgcwd)); } /* @@ -34,10 +34,10 @@ dirinit(void) int changedir(int f, int n) { - char bufc[NPAT], *bufp; + char bufc[NFILEN], *bufp; - (void)strlcpy(bufc, wdir, sizeof(bufc)); - if ((bufp = eread("Change default directory: ", bufc, NPAT, + (void)strlcpy(bufc, mgcwd, sizeof(bufc)); + if ((bufp = eread("Change default directory: ", bufc, NFILEN, EFDEF | EFNEW | EFCR)) == NULL) return (ABORT); else if (bufp[0] == '\0') @@ -46,9 +46,9 @@ changedir(int f, int n) ewprintf("Can't change dir to %s", bufc); return (FALSE); } else { - if ((wdir = getcwd(cwd, sizeof(cwd))) == NULL) + if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL) panic("Can't get current directory!"); - ewprintf("Current directory is now %s", wdir); + ewprintf("Current directory is now %s", bufp); return (TRUE); } } @@ -60,6 +60,15 @@ changedir(int f, int n) int showcwdir(int f, int n) { - ewprintf("Current directory: %s", wdir); + ewprintf("Current directory: %s", mgcwd); + return (TRUE); +} + +int +getcwdir(char *buf, size_t len) +{ + if (strlcpy(buf, mgcwd, len) >= len) + return (FALSE); + return (TRUE); } diff --git a/usr.bin/mg/dired.c b/usr.bin/mg/dired.c index 48b5aa1fe45..e89f5c803ec 100644 --- a/usr.bin/mg/dired.c +++ b/usr.bin/mg/dired.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dired.c,v 1.35 2005/12/20 05:04:28 kjell Exp $ */ +/* $OpenBSD: dired.c,v 1.36 2006/05/02 17:10:25 kjell Exp $ */ /* This file is in the public domain. */ @@ -630,7 +630,8 @@ dired_(char *dname) return (NULL); } bp->b_dotp = lforw(bp->b_linep); /* go to first line */ - (void) strlcpy(bp->b_fname, dname, sizeof(bp->b_fname)); + (void)strlcpy(bp->b_fname, dname, sizeof(bp->b_fname)); + (void)strlcpy(bp->b_cwd, dname, sizeof(bp->b_cwd)); if ((bp->b_modes[1] = name_mode("dired")) == NULL) { bp->b_modes[0] = name_mode("fundamental"); ewprintf("Could not find mode dired"); diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c index 2d48a3728cf..74120b0a472 100644 --- a/usr.bin/mg/file.c +++ b/usr.bin/mg/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.53 2006/04/06 05:28:17 kjell Exp $ */ +/* $OpenBSD: file.c,v 1.54 2006/05/02 17:10:25 kjell Exp $ */ /* This file is in the public domain. */ @@ -6,10 +6,10 @@ * File commands. */ -#include "def.h" - #include <libgen.h> +#include "def.h" + /* * Insert a file into the current buffer. Real easy - just call the * insertfile routine with the file name. @@ -20,7 +20,10 @@ fileinsert(int f, int n) { char fname[NFILEN], *bufp, *adjf; - bufp = eread("Insert file: ", fname, NFILEN, EFNEW | EFCR | EFFILE); + 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') @@ -42,19 +45,11 @@ int filevisit(int f, int n) { struct buffer *bp; - char fname[NFILEN], *bufp, *adjf, *slash; + char fname[NFILEN], *bufp, *adjf; int status; - if (curbp->b_fname && curbp->b_fname[0] != '\0') { - if (strlcpy(fname, curbp->b_fname, sizeof(fname)) >= sizeof(fname)) - return (FALSE); - if ((slash = strrchr(fname, '/')) != NULL) { - *(slash + 1) = '\0'; - } - } - else + if (getbufcwd(fname, sizeof(fname)) != TRUE) fname[0] = '\0'; - bufp = eread("Find file: ", fname, NFILEN, EFNEW | EFCR | EFFILE | EFDEF); if (bufp == NULL) @@ -88,18 +83,11 @@ int filevisitalt(int f, int n) { struct buffer *bp; - char fname[NFILEN], *bufp, *adjf, *slash; + char fname[NFILEN], *bufp, *adjf; int status; - if (curbp->b_fname && curbp->b_fname[0] != '\0') { - if (strlcpy(fname, curbp->b_fname, sizeof(fname)) >= sizeof(fname)) - return (FALSE); - if ((slash = strrchr(fname, '/')) != NULL) { - *(slash + 1) = '\0'; - } - } else + if (getbufcwd(fname, sizeof(fname)) != TRUE) fname[0] = '\0'; - bufp = eread("Find alternate file: ", fname, NFILEN, EFNEW | EFCR | EFFILE | EFDEF); if (bufp == NULL) @@ -152,8 +140,10 @@ poptofile(int f, int n) char fname[NFILEN], *adjf, *bufp; int status; + if (getbufcwd(fname, sizeof(fname)) != TRUE) + fname[0] = '\0'; if ((bufp = eread("Find file in other window: ", fname, NFILEN, - EFNEW | EFCR | EFFILE)) == NULL) + EFNEW | EFCR | EFFILE | EFDEF)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); @@ -194,7 +184,7 @@ findbuffer(char *fn) return (bp); } /* Not found. Create a new one, adjusting name first */ - if (baugname(bname, fname, sizeof(bname)) == FALSE) + if (augbname(bname, fname, sizeof(bname)) == FALSE) return (NULL); bp = bfind(bname, TRUE); @@ -305,8 +295,12 @@ insertfile(char *fname, char *newname, int replacebuf) /* cheap */ bp = curbp; - if (newname != NULL) - (void)strlcpy(bp->b_fname, newname, sizeof bp->b_fname); + if (newname != NULL) { + (void)strlcpy(bp->b_fname, newname, sizeof(bp->b_fname)); + (void)strlcpy(bp->b_cwd, dirname(newname), + sizeof(bp->b_cwd)); + (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd)); + } /* hard file open */ if ((s = ffropen(fname, (replacebuf == TRUE) ? bp : NULL)) == FIOERR) @@ -331,6 +325,9 @@ insertfile(char *fname, char *newname, int replacebuf) undo_enable(x); curbp = bp; return (showbuffer(bp, curwp, WFHARD | WFMODE)); + } else { + (void)strlcpy(bp->b_cwd, dirname(fname), sizeof(bp->b_cwd)); + (void)strlcat(bp->b_cwd, "/", sizeof(bp->b_cwd)); } opos = curwp->w_doto; @@ -474,8 +471,10 @@ filewrite(int f, int n) char fname[NFILEN], bn[NBUFN]; char *adjfname, *bufp; + if (getbufcwd(fname, sizeof(fname)) != TRUE) + fname[0] = '\0'; if ((bufp = eread("Write file: ", fname, NFILEN, - EFNEW | EFCR | EFFILE)) == NULL) + EFDEF | EFNEW | EFCR | EFFILE)) == NULL) return (ABORT); else if (bufp[0] == '\0') return (FALSE); @@ -487,8 +486,10 @@ filewrite(int f, int n) bzero(&curbp->b_fi, sizeof(curbp->b_fi)); if ((s = writeout(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)); free(curbp->b_bname); - if (baugname(bn, basename(curbp->b_fname), sizeof(bn)) + if (augbname(bn, basename(curbp->b_fname), sizeof(bn)) == FALSE) return (FALSE); if ((curbp->b_bname = strdup(bn)) == NULL) diff --git a/usr.bin/mg/grep.c b/usr.bin/mg/grep.c index 9deffa87d00..e6a47b3845d 100644 --- a/usr.bin/mg/grep.c +++ b/usr.bin/mg/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.27 2006/04/03 00:19:32 kjell Exp $ */ +/* $OpenBSD: grep.c,v 1.28 2006/05/02 17:10:25 kjell Exp $ */ /* * Copyright (c) 2001 Artur Grabowski <art@openbsd.org>. * Copyright (c) 2005 Kjell Wooding <kjell@openbsd.org>. @@ -39,8 +39,7 @@ int next_error(int, int); static int grep(int, int); static int compile(int, int); static int gid(int, int); -static struct buffer *compile_mode(const char *, const char *, const char *); -static int getbufcwd(char *, size_t); +static struct buffer *compile_mode(const char *, const char *); static int xlint(int, int); void grep_init(void); @@ -88,14 +87,6 @@ grep(int f, int n) char cprompt[NFILEN], *bufp; struct buffer *bp; struct mgwin *wp; - char path[NFILEN]; - - /* get buffer cwd */ - if (getbufcwd(path, sizeof(path)) == FALSE) { - ewprintf("Failed. " - "Can't get working directory of current buffer."); - return (FALSE); - } (void)strlcpy(cprompt, "grep -n ", sizeof(cprompt)); if ((bufp = eread("Run grep: ", cprompt, NFILEN, @@ -105,7 +96,7 @@ grep(int f, int n) return (FALSE); (void)snprintf(command, sizeof(command), "%s /dev/null", bufp); - if ((bp = compile_mode("*grep*", command, path)) == NULL) + if ((bp = compile_mode("*grep*", command)) == NULL) return (FALSE); if ((wp = popbuf(bp)) == NULL) return (FALSE); @@ -122,14 +113,6 @@ xlint(int f, int n) char cprompt[NFILEN], *bufp; struct buffer *bp; struct mgwin *wp; - char path[NFILEN]; - - /* get buffer cwd */ - if (getbufcwd(path, sizeof(path)) == FALSE) { - ewprintf("Failed. " - "Can't get working directory of current buffer."); - return (FALSE); - } (void)strlcpy(cprompt, "make lint ", sizeof(cprompt)); if ((bufp = eread("Run lint: ", cprompt, NFILEN, @@ -139,7 +122,7 @@ xlint(int f, int n) return (FALSE); (void)snprintf(command, sizeof(command), "%s 2>&1", bufp); - if ((bp = compile_mode("*lint*", command, path)) == NULL) + if ((bp = compile_mode("*lint*", command)) == NULL) return (FALSE); if ((wp = popbuf(bp)) == NULL) return (FALSE); @@ -156,14 +139,6 @@ compile(int f, int n) char cprompt[NFILEN], *bufp; struct buffer *bp; struct mgwin *wp; - char path[NFILEN]; - - /* get buffer cwd */ - if (getbufcwd(path, sizeof(path)) == FALSE) { - ewprintf("Failed. " - "Can't get working directory of current buffer."); - return (FALSE); - } (void)strlcpy(cprompt, compile_last_command, sizeof(cprompt)); if ((bufp = eread("Compile command: ", cprompt, NFILEN, @@ -177,7 +152,7 @@ compile(int f, int n) (void)snprintf(command, sizeof(command), "%s 2>&1", bufp); - if ((bp = compile_mode("*compile*", command, path)) == NULL) + if ((bp = compile_mode("*compile*", command)) == NULL) return (FALSE); if ((wp = popbuf(bp)) == NULL) return (FALSE); @@ -197,14 +172,6 @@ gid(int f, int n) struct buffer *bp; struct mgwin *wp; int i, j; - char path[NFILEN]; - - /* get buffer cwd */ - if (getbufcwd(path, sizeof(path)) == FALSE) { - ewprintf("Failed. " - "Can't get working directory of current buffer."); - return (FALSE); - } /* catch ([^\s(){}]+)[\s(){}]* */ @@ -241,7 +208,7 @@ gid(int f, int n) return (FALSE); (void)snprintf(command, sizeof(command), "gid %s", cprompt); - if ((bp = compile_mode("*gid*", command, path)) == NULL) + if ((bp = compile_mode("*gid*", command)) == NULL) return (FALSE); if ((wp = popbuf(bp)) == NULL) return (FALSE); @@ -251,7 +218,7 @@ gid(int f, int n) } struct buffer * -compile_mode(const char *name, const char *command, const char *path) +compile_mode(const char *name, const char *command) { struct buffer *bp; FILE *fpipe; @@ -266,14 +233,16 @@ compile_mode(const char *name, const char *command, const char *path) if (bclear(bp) != TRUE) return (NULL); - addlinef(bp, "cd %s", path); + if (getbufcwd(bp->b_cwd, sizeof(bp->b_cwd)) != TRUE) + return (NULL); + addlinef(bp, "cd %s", bp->b_cwd); addline(bp, command); addline(bp, ""); if (getcwd(cwd, sizeof(cwd)) == NULL) panic("Can't get current directory!"); - if (chdir(path) == -1) { - ewprintf("Can't change dir to %s", path); + if (chdir(bp->b_cwd) == -1) { + ewprintf("Can't change dir to %s", bp->b_cwd); return (NULL); } if ((fpipe = popen(command, "r")) == NULL) { @@ -321,7 +290,7 @@ compile_goto_error(int f, int n) struct mgwin *wp; char *fname, *line, *lp, *ln; int lineno, len; - char *adjf; + char *adjf, path[NFILEN]; const char *errstr; struct line *last; @@ -350,8 +319,15 @@ compile_goto_error(int f, int n) lineno = (int)strtonum(ln, INT_MIN, INT_MAX, &errstr); if (errstr) goto fail; - - adjf = adjustname(fname); + + if (fname && fname[0] != '/') { + (void)strlcpy(path, curbp->b_cwd, sizeof(path)); + if (strlcat(path, fname, sizeof(path)) >= sizeof(path)) + goto fail; + adjf = path; + } else { + adjf = adjustname(fname); + } free(line); if (adjf == NULL) @@ -397,33 +373,3 @@ next_error(int f, int n) return (compile_goto_error(f, n)); } - -/* - * Return the working directory for the current buffer, terminated - * with a '/'. First, try to extract it from the current buffer's - * filename. If that fails, use global cwd. - */ -static int -getbufcwd(char *path, size_t plen) -{ - char *dname, cwd[NFILEN]; - if (plen == 0) - goto error; - - if (curbp->b_fname && curbp->b_fname[0] != '\0' && - (dname = dirname(curbp->b_fname)) != NULL) { - if (strlcpy(path, dname, plen) >= plen) - goto error; - if (strlcat(path, "/", plen) >= plen) - goto error; - } else { - if ((dname = getcwd(cwd, sizeof(cwd))) == NULL) - goto error; - if (strlcpy(path, dname, plen) >= plen) - goto error; - } - return (TRUE); -error: - path = NULL; - return (FALSE); -} |