mg keeps the path to the last loaded tag file in tagsfn which was used
both for the lazily loading (now removed) and as a flag to know if any
tags are currently loaded.  It's redundant, we can just check if the
rb tree is empty instead.

The only difference I can think of is with the corner cases of an
empty tags file (echo -n > tags): with diff below mg would always ask
the user which tag file to load on M-. (find-tag) where currently it
asks only on the first try.  I don't think it's an issue.

ok?

Index: tags.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/tags.c,v
retrieving revision 1.25
diff -u -p -r1.25 tags.c
--- tags.c      29 Mar 2023 07:29:17 -0000      1.25
+++ tags.c      29 Mar 2023 08:07:23 -0000
@@ -38,8 +38,6 @@ static void              unloadtags(void
 
 #define DEFAULTFN "tags"
 
-char *tagsfn = NULL;
-
 /* ctags(1) entries are parsed and maintained in a tree. */
 struct ctag {
        RB_ENTRY(ctag) entry;
@@ -87,42 +85,18 @@ tagsvisit(int f, int n)
        if (bufp == NULL)
                return (ABORT);
 
-       if (tagsfn == NULL) {
-               if (bufp[0] == '\0') {
-                       if ((tagsfn = strdup(fname)) == NULL) {
-                               dobeep();
-                               ewprintf("Out of memory");
-                               return (FALSE);
-                       }
-               } else {
-                       /* bufp points to local variable, so duplicate. */
-                       if ((tagsfn = strdup(bufp)) == NULL) {
-                               dobeep();
-                               ewprintf("Out of memory");
-                               return (FALSE);
-                       }
-               }
-       } else {
-               if ((temp = strdup(bufp)) == NULL) {
-                       dobeep();
-                       ewprintf("Out of memory");
-                       return (FALSE);
-               }
-               free(tagsfn);
-               tagsfn = temp;
+       if (!RB_EMPTY(&tags)) {
                if (eyorn("Keep current list of tags table also") == FALSE) {
                        ewprintf("Starting a new list of tags table");
                        unloadtags();
                }
        }
 
-       if (loadtags(tagsfn) == FALSE) {
-               free(tagsfn);
-               tagsfn = NULL;
-               return (FALSE);
-       }
+       temp = bufp;
+       if (temp[0] == '\0')
+               temp = fname;
 
-       return (TRUE);
+       return (loadtags(temp));
 }
 
 /*
@@ -156,7 +130,7 @@ findtag(int f, int n)
                return (FALSE);
        }
 
-       if (tagsfn == NULL)
+       if (RB_EMPTY(&tags))
                if ((ret = tagsvisit(f, n)) != TRUE)
                        return (ret);
        return pushtag(tok);
@@ -328,7 +302,6 @@ closetags(void)
                free(s);
        }
        unloadtags();
-       free(tagsfn);
 }
 
 /*

Reply via email to