On 2023/03/30 18:11:32 +0200, Theo Buehler <t...@theobuehler.org> wrote:
> On Thu, Mar 30, 2023 at 03:47:24PM +0200, Omar Polo wrote:
> > -   else if (bufp[0] == '\0')
> > +   if (bufp[0] == '\0')
> >             return (FALSE);
> > -   return (load(fname));
> > +   if ((bufp = adjustname(fname, TRUE)) == NULL)
> > +           return (FALSE);
> > +   ret = ffropen(&ffp, bufp, NULL);
> 
> Nit: I'd avoid the nesting (I know that you just moved the code):
> 
>       if (ret == FIODIR)
>               (void)ffclose(ffp, NULL);
>       if (ret != FIOSUC)
>               return (FALSE);

I prefer without the nesting too, changed.

> [...]
> > -   if (conffile != NULL && access(conffile, R_OK) != 0) {
> > +   if ((cp = startupfile(&ffp, NULL, conffile)) != NULL && conffile) {
> 
> This doesn't look right. I'd have expected
> 
>       if ((cp = startupfile(&ffp, NULL, conffile)) == NULL && conffile) {
> 
> (and indeed, your patch breaks -u).

Arggg... Initially I had startupfile() right before load(), I've moved
it before sending the mail and not re-tested afterwards, sorry!

I've also noticed that since startupfile() returns static memory
errors in -u would report the wrong path (ttykeymapinit() calls
startupfile() itself) so I'm changing it to use a caller provided
buffer for the path.  tbf i don't completely like it and it also
causes some churn in the diff in startupfile().

The alternative would be to call ttykeymapinit() after load() in
main.c, and delaying ttykeymapinit().  It'll make the diff smaller but
it feels a bit hackish to keep a pointer to static memory for that
long, and future hackers may be bitten again by this.

(delaying ttykeymapinit() is something on my todo list as well, it
should be called at least after update(CMODE) for errors to be
printed, but it's a separate issue.)

Index: def.h
===================================================================
RCS file: /home/cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.177
diff -u -p -r1.177 def.h
--- def.h       20 Oct 2022 18:59:24 -0000      1.177
+++ def.h       30 Mar 2023 16:34:07 -0000
@@ -486,7 +486,7 @@ int          ffputbuf(FILE *, struct buffer *, 
 int             ffgetline(FILE *, char *, int, int *);
 int             fbackupfile(const char *);
 char           *adjustname(const char *, int);
-char           *startupfile(char *, char *);
+FILE           *startupfile(char *, char *, char *, size_t);
 int             copy(char *, char *);
 struct list    *make_file_list(char *);
 int             fisdir(const char *);
@@ -594,7 +594,7 @@ int          extend(int, int);
 int             evalexpr(int, int);
 int             evalbuffer(int, int);
 int             evalfile(int, int);
-int             load(const char *);
+int             load(FILE *, const char *);
 int             excline(char *, int, int);
 char           *skipwhite(char *);
 
Index: extend.c
===================================================================
RCS file: /home/cvs/src/usr.bin/mg/extend.c,v
retrieving revision 1.77
diff -u -p -r1.77 extend.c
--- extend.c    8 Mar 2023 04:43:11 -0000       1.77
+++ extend.c    30 Mar 2023 16:22:52 -0000
@@ -620,37 +620,36 @@ evalbuffer(int f, int n)
 int
 evalfile(int f, int n)
 {
+       FILE    *ffp;
        char     fname[NFILEN], *bufp;
+       int      ret;
 
        if ((bufp = eread("Load file: ", fname, NFILEN,
            EFNEW | EFCR)) == NULL)
                return (ABORT);
-       else if (bufp[0] == '\0')
+       if (bufp[0] == '\0')
                return (FALSE);
-       return (load(fname));
+       if ((bufp = adjustname(fname, TRUE)) == NULL)
+               return (FALSE);
+       ret = ffropen(&ffp, bufp, NULL);
+       if (ret == FIODIR)
+               (void)ffclose(ffp, NULL);
+       if (ret != FIOSUC)
+               return (FALSE);
+       ret = load(ffp, bufp);
+       (void)ffclose(ffp, NULL);
+       return (ret);
 }
 
 /*
  * load - go load the file name we got passed.
  */
 int
-load(const char *fname)
+load(FILE *ffp, const char *fname)
 {
-       int      s = TRUE, line, ret;
+       int      s = TRUE, line;
        int      nbytes = 0;
        char     excbuf[BUFSIZE], fncpy[NFILEN];
-       FILE    *ffp;
-
-       if ((fname = adjustname(fname, TRUE)) == NULL)
-               /* just to be careful */
-               return (FALSE);
-
-       ret = ffropen(&ffp, fname, NULL);
-       if (ret != FIOSUC) {
-               if (ret == FIODIR)
-                       (void)ffclose(ffp, NULL);
-               return (FALSE);
-       }
 
        /* keep a note of fname in case of errors in loaded file. */
        (void)strlcpy(fncpy, fname, sizeof(fncpy));
@@ -666,7 +665,6 @@ load(const char *fname)
                        break;
                }
        }
-       (void)ffclose(ffp, NULL);
        excbuf[nbytes] = '\0';
        if (s != FIOEOF || (nbytes && excline(excbuf, nbytes, ++line) != TRUE))
                return (FALSE);
Index: fileio.c
===================================================================
RCS file: /home/cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.110
diff -u -p -r1.110 fileio.c
--- fileio.c    30 Mar 2023 07:26:15 -0000      1.110
+++ fileio.c    30 Mar 2023 16:46:37 -0000
@@ -328,10 +328,10 @@ adjustname(const char *fn, int slashslas
  * the terminal driver in particular), accepts a suffix to be appended
  * to the startup file name.
  */
-char *
-startupfile(char *suffix, char *conffile)
+FILE *
+startupfile(char *suffix, char *conffile, char *path, size_t len)
 {
-       static char      file[NFILEN];
+       FILE            *ffp;
        char            *home;
        int              ret;
 
@@ -339,34 +339,40 @@ startupfile(char *suffix, char *conffile
                goto nohome;
 
        if (conffile != NULL) {
-               (void)strlcpy(file, conffile, NFILEN);
+               (void)strlcpy(path, conffile, len);
        } else if (suffix == NULL) {
-               ret = snprintf(file, sizeof(file), _PATH_MG_STARTUP, home);
-               if (ret < 0 || ret >= sizeof(file))
+               ret = snprintf(path, len, _PATH_MG_STARTUP, home);
+               if (ret < 0 || ret >= len)
                        return (NULL);
        } else {
-               ret = snprintf(file, sizeof(file), _PATH_MG_TERM, home, suffix);
-               if (ret < 0 || ret >= sizeof(file))
+               ret = snprintf(path, len, _PATH_MG_TERM, home, suffix);
+               if (ret < 0 || ret >= len)
                        return (NULL);
        }
 
-       if (access(file, R_OK) == 0)
-               return (file);
+       ret = ffropen(&ffp, path, NULL);
+       if (ret == FIOSUC)
+               return (ffp);
+       if (ret == FIODIR)
+               (void)ffclose(ffp, NULL);
 nohome:
 #ifdef STARTUPFILE
        if (suffix == NULL) {
-               ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
-               if (ret < 0 || ret >= sizeof(file))
+               ret = snprintf(path, len, "%s", STARTUPFILE);
+               if (ret < 0 || ret >= len)
                        return (NULL);
        } else {
-               ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE,
+               ret = snprintf(path, len, "%s%s", STARTUPFILE,
                    suffix);
-               if (ret < 0 || ret >= sizeof(file))
+               if (ret < 0 || ret >= len)
                        return (NULL);
        }
 
-       if (access(file, R_OK) == 0)
-               return (file);
+       ret = ffropen(&ffp, path, NULL);
+       if (ret == FIOSUC)
+               return (ffp);
+       if (ret == FIODIR)
+               (void)ffclose(ffp, NULL);
 #endif /* STARTUPFILE */
        return (NULL);
 }
Index: main.c
===================================================================
RCS file: /home/cvs/src/usr.bin/mg/main.c,v
retrieving revision 1.92
diff -u -p -r1.92 main.c
--- main.c      30 Mar 2023 08:07:07 -0000      1.92
+++ main.c      30 Mar 2023 16:39:15 -0000
@@ -62,6 +62,8 @@ usage()
 int
 main(int argc, char **argv)
 {
+       FILE            *ffp;
+       char             file[NFILEN];
        char            *cp, *conffile = NULL, *init_fcn_name = NULL;
        char            *batchfile = NULL;
        PF               init_fcn = NULL;
@@ -107,7 +109,8 @@ main(int argc, char **argv)
                pty_init();
                conffile = batchfile;
        }
-       if (conffile != NULL && access(conffile, R_OK) != 0) {
+       if ((ffp = startupfile(NULL, conffile, file, sizeof(file))) == NULL &&
+           conffile != NULL) {
                 fprintf(stderr, "%s: Problem with file: %s\n", __progname,
                    conffile);
                 exit(1);
@@ -159,8 +162,10 @@ main(int argc, char **argv)
        update(CMODE);
 
        /* user startup file. */
-       if ((cp = startupfile(NULL, conffile)) != NULL)
-               (void)load(cp);
+       if (ffp) {
+               (void)load(ffp, file);
+               ffclose(ffp, NULL);
+       }
 
        if (batch)
                return (0);
Index: ttykbd.c
===================================================================
RCS file: /home/cvs/src/usr.bin/mg/ttykbd.c,v
retrieving revision 1.21
diff -u -p -r1.21 ttykbd.c
--- ttykbd.c    30 Mar 2023 08:07:07 -0000      1.21
+++ ttykbd.c    30 Mar 2023 16:56:18 -0000
@@ -31,7 +31,8 @@ char  *keystrings[] = {NULL};
 void
 ttykeymapinit(void)
 {
-       char    *cp;
+       char    *cp, file[NFILEN];
+       FILE    *ffp;
 
        /* Bind keypad function keys. */
        if (key_left)
@@ -57,10 +58,11 @@ ttykeymapinit(void)
        if (key_dc)
                dobindkey(fundamental_map, "delete-char", key_dc);
 
-       if ((cp = getenv("TERM"))) {
-               if (((cp = startupfile(cp, NULL)) != NULL) &&
-                   (load(cp) != TRUE))
+       if ((cp = getenv("TERM")) != NULL &&
+           (ffp = startupfile(cp, NULL, file, sizeof(file))) != NULL) {
+               if (load(ffp, file) != TRUE)
                        ewprintf("Error reading key initialization file");
+               (void)ffclose(ffp, NULL);
        }
        if (keypad_xmit)
                /* turn on keypad */

Reply via email to