Fix assigning array variables in ksh

2021-02-21 Thread Vadim Zhukov
Hello all.

This continues the 'Another potential ksh bug?' thread on misc@:
https://marc.info/?l=openbsd-misc=160736700220621=2
This present is a bit too late for Christmas, but at least the Day of
Red Army is coming soon. I'm sure noone is against this patch then.

The code in typeset() function, which is responsible for almost all
shell variable tweaking, contains a bug: in case of array it passes
"foo=var" instead of only variable name ("foo"), if the value was
ever given. This results in, e.g., a bug reported by Jordan Geoghegan.
Generally speaking, we had creating a (temporary) variable via global()
in vpbase, and of course it didn't get the read-only marker.

This fix is to use 'tvar' variable, which always contains shell
variable name, without value.

As a bonus, this fixes the ifs.t:IFS-null-1 test (previously failing).
I'm too lazy to check why. :-)

Okay anyone?

--
WBR,
  Vadim Zhukov


Index: regress/bin/ksh/obsd-regress.t
===
RCS file: /cvs/src/regress/bin/ksh/obsd-regress.t,v
retrieving revision 1.10
diff -u -p -r1.10 obsd-regress.t
--- regress/bin/ksh/obsd-regress.t  8 Dec 2018 21:03:51 -   1.10
+++ regress/bin/ksh/obsd-regress.t  21 Feb 2021 18:51:54 -
@@ -503,3 +503,16 @@ description:
 stdin:
kill -s SIGINFO $$
 ---
+
+name: overwrite-ro-array
+description:
+   do not allow to override first element of a read-only array
+   via the non-array access.
+stdin:
+   arr[0]=foo
+   readonly arr
+   arr=bar
+expected-exit: e == 1
+expected-stderr-pattern:
+   /: arr: is read only$/
+---
Index: bin/ksh/var.c
===
RCS file: /cvs/src/bin/ksh/var.c,v
retrieving revision 1.71
diff -u -p -r1.71 var.c
--- bin/ksh/var.c   21 Feb 2020 18:21:23 -  1.71
+++ bin/ksh/var.c   21 Feb 2021 18:51:54 -
@@ -644,7 +644,7 @@ typeset(const char *var, int set, int cl
global(tvar);
set &= ~(LOCAL|LOCAL_COPY);
 
-   vpbase = (vp->flag & ARRAY) ? global(arrayname(var)) : vp;
+   vpbase = (vp->flag & ARRAY) ? global(arrayname(tvar)) : vp;
 
/* only allow export flag to be set.  at ksh allows any attribute to
 * be changed, which means it can be truncated or modified



mg: specify a different startup file

2021-02-21 Thread Mark Lumsden

This diff allows mg(1) to specify an alternative startup file on
the fly when starting mg:

$ mg -u ~/.mg2

I decided to use "u" since that is what emacs uses to load
another user's init file:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Init-File.html

Perhaps "f" would be better but that is being used to set mg modes

Also, I decided not to load the default startup file if the
alternative is not found since it seems more obvious that something
is wrong that way.

Any comments/ok?

Mark

Index: def.h
===
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.166
diff -u -p -u -p -r1.166 def.h
--- def.h   9 Feb 2020 10:13:13 -   1.166
+++ def.h   21 Feb 2021 12:55:41 -
@@ -471,7 +471,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   *startupfile(char *, char *);
 int copy(char *, char *);
 struct list*make_file_list(char *);
 int fisdir(const char *);
Index: fileio.c
===
RCS file: /cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.106
diff -u -p -u -p -r1.106 fileio.c
--- fileio.c22 Jun 2019 10:21:57 -  1.106
+++ fileio.c21 Feb 2021 12:55:41 -
@@ -330,7 +330,7 @@ adjustname(const char *fn, int slashslas
  * to the startup file name.
  */
 char *
-startupfile(char *suffix)
+startupfile(char *suffix, char *conffile)
 {
static char  file[NFILEN];
char*home;
@@ -339,7 +339,9 @@ startupfile(char *suffix)
if ((home = getenv("HOME")) == NULL || *home == '\0')
goto nohome;

-   if (suffix == NULL) {
+   if (conffile != NULL) {
+   (void)strncpy(file, conffile, NFILEN);
+   } else if (suffix == NULL) {
ret = snprintf(file, sizeof(file), _PATH_MG_STARTUP, home);
if (ret < 0 || ret >= sizeof(file))
return (NULL);
Index: main.c
===
RCS file: /cvs/src/usr.bin/mg/main.c,v
retrieving revision 1.87
diff -u -p -u -p -r1.87 main.c
--- main.c  22 Jun 2019 15:38:15 -  1.87
+++ main.c  21 Feb 2021 12:55:41 -
@@ -48,7 +48,8 @@ extern void closetags(void);
 static __dead void
 usage()
 {
-   fprintf(stderr, "usage: %s [-nR] [-f mode] [+number] [file ...]\n",
+   fprintf(stderr, "usage: %s [-nR] [-f mode] [-u file] [+number] "
+   "[file ...]\n",
__progname);
exit(1);
 }
@@ -56,7 +57,7 @@ usage()
 int
 main(int argc, char **argv)
 {
-   char*cp, *init_fcn_name = NULL;
+   char*cp, *conffile = NULL, *init_fcn_name = NULL;
PF   init_fcn = NULL;
int  o, i, nfiles;
int  nobackups = 0;
@@ -66,7 +67,7 @@ main(int argc, char **argv)
NULL) == -1)
err(1, "pledge");

-   while ((o = getopt(argc, argv, "nRf:")) != -1)
+   while ((o = getopt(argc, argv, "nRf:u:")) != -1)
switch (o) {
case 'R':
allbro = 1;
@@ -80,6 +81,9 @@ main(int argc, char **argv)
"initial function");
init_fcn_name = optarg;
break;
+   case 'u':
+   conffile = optarg;
+   break;
default:
usage();
}
@@ -129,7 +133,7 @@ main(int argc, char **argv)
update(CMODE);

/* user startup file. */
-   if ((cp = startupfile(NULL)) != NULL)
+   if ((cp = startupfile(NULL, conffile)) != NULL)
(void)load(cp);

/*
Index: mg.1
===
RCS file: /cvs/src/usr.bin/mg/mg.1,v
retrieving revision 1.118
diff -u -p -u -p -r1.118 mg.1
--- mg.18 Nov 2019 19:54:40 -   1.118
+++ mg.121 Feb 2021 12:55:42 -
@@ -11,6 +11,7 @@
 .Nm mg
 .Op Fl nR
 .Op Fl f Ar mode
+.Op Fl u Ar file
 .Op + Ns Ar number
 .Op Ar
 .Sh DESCRIPTION
@@ -38,6 +39,11 @@ line of the file, +-2 will be second las
 Run the mode command for all buffers created from
 arguments on the command line, including the
 scratch buffer and all files.
+.It Fl u Ar file
+Use
+.Ar file
+as the startup file, instead of the default
+.Pa ~/.mg .
 .It Fl n
 Turn off backup file generation.
 .It Fl R
Index: ttykbd.c
===
RCS file: /cvs/src/usr.bin/mg/ttykbd.c,v
retrieving revision 1.19
diff -u -p -u -p -r1.19 ttykbd.c
--- ttykbd.c17 Dec 2017 14:37:57 -  1.19
+++ ttykbd.c21 Feb 2021 12:55:42 -
@@ -58,7 

Re: veb(4), a virtual ethernet bridge (that could replace bridge(4)?)

2021-02-21 Thread Vitaliy Makkoveev
Hello.


> + ifp->if_ioctl = veb_ioctl;
> + ifp->if_input = veb_input;
> + //ifp->if_rtrequest = veb_rtrequest;
> + ifp->if_output = veb_output;
> + ifp->if_enqueue = veb_enqueue;

Could you replace c++ style comment in veb_clone_create()?

> +veb_clone_destroy(struct ifnet *ifp)
> +{
> + struct veb_softc *sc = ifp->if_softc;
> + struct veb_port *p, *np;
> +
> + NET_LOCK();
> + sc->sc_dead = 1;
> +
> + if (ISSET(ifp->if_flags, IFF_RUNNING))
> + veb_down(sc);
> + NET_UNLOCK();
> +
> + if_detach(ifp);


Also veb_down() looks strange here. I guess it is no reason to 
play with `if_flags' here and smr_barrier() could be called after
if_detach(). This makes `sc_dead’ unnecessary.