From: op Date: Wed, 29 Mar 2023 07:29:17 +0000 (+0000) Subject: change tagvisit (aka visit-tag-table) to immediately load the tag file X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=da7e10015efd2887075c33990c1413265244664d;p=openbsd change tagvisit (aka visit-tag-table) to immediately load the tag file before it used to only record the path to the tag file which was loaded on demand upon find-tag (M-.). tagvisit did to do a stat + access dance to know if the path was pointing to a regular, readable file and loadtags (called lazily by find-tag) trusted those checks... Instead, just load the tags in tagvisit and drop the lazy mechanics. ok tb@ --- diff --git a/usr.bin/mg/mg.1 b/usr.bin/mg/mg.1 index f23e3c0472b..beb85f1151f 100644 --- a/usr.bin/mg/mg.1 +++ b/usr.bin/mg/mg.1 @@ -1,7 +1,7 @@ -.\" $OpenBSD: mg.1,v 1.127 2022/10/20 18:59:24 op Exp $ +.\" $OpenBSD: mg.1,v 1.128 2023/03/29 07:29:17 op Exp $ .\" This file is in the public domain. .\" -.Dd $Mdocdate: October 20 2022 $ +.Dd $Mdocdate: March 29 2023 $ .Dt MG 1 .Os .Sh NAME @@ -967,7 +967,7 @@ As it moves, convert any characters to upper case. Toggle the visible bell. If this toggle is on, the modeline will flash. .It visit-tags-table -Record name of the tags file to be used for subsequent find-tag. +Load tags file to be used for subsequent find-tag. .It what-cursor-position Display a bunch of useful information about the current location of dot. diff --git a/usr.bin/mg/tags.c b/usr.bin/mg/tags.c index 41684a863a8..fcb32515c63 100644 --- a/usr.bin/mg/tags.c +++ b/usr.bin/mg/tags.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tags.c,v 1.24 2023/03/28 21:33:21 tb Exp $ */ +/* $OpenBSD: tags.c,v 1.25 2023/03/29 07:29:17 op Exp $ */ /* * This file is in the public domain. @@ -39,7 +39,6 @@ static void unloadtags(void); #define DEFAULTFN "tags" char *tagsfn = NULL; -int loaded = FALSE; /* ctags(1) entries are parsed and maintained in a tree. */ struct ctag { @@ -66,15 +65,13 @@ ctagcmp(struct ctag *s, struct ctag *t) } /* - * Record the filename that contain tags to be used while loading them - * on first use. If a filename is already recorded, ask user to retain - * already loaded tags (if any) and unload them if user chooses not to. + * Load a tags file. If a tags file is already loaded, ask the user to + * retain loaded tags (i any) and unload them if the user chooses not to. */ int tagsvisit(int f, int n) { char fname[NFILEN], *bufp, *temp; - struct stat sb; if (getbufcwd(fname, sizeof(fname)) == FALSE) fname[0] = '\0'; @@ -90,20 +87,6 @@ tagsvisit(int f, int n) if (bufp == NULL) return (ABORT); - if (stat(bufp, &sb) == -1) { - dobeep(); - ewprintf("stat: %s", strerror(errno)); - return (FALSE); - } else if (S_ISREG(sb.st_mode) == 0) { - dobeep(); - ewprintf("Not a regular file"); - return (FALSE); - } else if (access(bufp, R_OK) == -1) { - dobeep(); - ewprintf("Cannot access file %s", bufp); - return (FALSE); - } - if (tagsfn == NULL) { if (bufp[0] == '\0') { if ((tagsfn = strdup(fname)) == NULL) { @@ -131,8 +114,14 @@ tagsvisit(int f, int n) ewprintf("Starting a new list of tags table"); unloadtags(); } - loaded = FALSE; } + + if (loadtags(tagsfn) == FALSE) { + free(tagsfn); + tagsfn = NULL; + return (FALSE); + } + return (TRUE); } @@ -170,14 +159,6 @@ findtag(int f, int n) 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); } @@ -300,6 +281,7 @@ poptag(int f, int n) int loadtags(const char *fn) { + struct stat sb; char *l; FILE *fd; @@ -308,6 +290,18 @@ loadtags(const char *fn) ewprintf("Unable to open tags file: %s", fn); return (FALSE); } + if (fstat(fileno(fd), &sb) == -1) { + dobeep(); + ewprintf("fstat: %s", strerror(errno)); + fclose(fd); + return (FALSE); + } + if (!S_ISREG(sb.st_mode)) { + dobeep(); + ewprintf("Not a regular file"); + fclose(fd); + return (FALSE); + } while ((l = fparseln(fd, NULL, NULL, "\\\\\0", FPARSELN_UNESCCONT | FPARSELN_UNESCREST)) != NULL) { if (addctag(l) == FALSE) {