Explicitly tag commands in vi(1)

2021-11-20 Thread Simon Branch
Hello!  Here's a diff that adds explicit .Tg macros to vi(1), so that
you can jump to vi or ex commands using -Otag or :t.  This patch
*should* include every command, but I couldn't figure out how to tag the
'!' and ':!' commands; none of these worked:

   .Tg
   .Tg !
   .Tg !\&
   .Tg "!"

This patch doesn't tag special keys either (word erase, literal next,
etc.), because tag names can't contain whitespace and I'd prefer
consistency with the manpage.  One solution would be to say WERASE and
LNEXT instead, which is what ksh(1) does, and this also makes it easier
to lookup what 'word erase' and 'literal next' really are.

   $ man -k Dv=WERASE Dv=LNEXT
   stty(1) - set the options for a terminal device interface
   termios(4) - general terminal line discipline

   $ man 4 termios -Otag=WERASE
   WERASESpecial character on input and is recognized if the ICANON
 flag is set.  Erases the last word ...

If a line is explicitly tagged with the .Tg macro, mandoc removes any
other automatically-created tags with the same name.  So this patch also
explicitly tags the 'print', 'number', and 'list' options and the
[-ceFRrSstvw] flags.

Comments?  OK?

Index: src/usr.bin/vi/docs/USD.doc/vi.man/vi.1
===
RCS file: /cvs/src/usr.bin/vi/docs/USD.doc/vi.man/vi.1,v
retrieving revision 1.79
diff -u -p -r1.79 vi.1
--- src/usr.bin/vi/docs/USD.doc/vi.man/vi.1 8 Mar 2021 02:47:29 -   
1.79
+++ src/usr.bin/vi/docs/USD.doc/vi.man/vi.1 20 Nov 2021 23:05:26 -
@@ -87,6 +87,7 @@ It's probably enough to get you going.
 .Pp
 The following options are available:
 .Bl -tag -width "-w size "
+.Tg c
 .It Fl c Ar cmd
 Execute
 .Ar cmd
@@ -99,19 +100,23 @@ This is the POSIX 1003.2 interface for t
 syntax.
 .Nm nex Ns / Ns Nm nvi
 supports both the old and new syntax.
+.Tg e
 .It Fl e
 Start editing in ex mode, as if the command name were
 .Nm ex .
+.Tg F
 .It Fl F
 Don't copy the entire file when first starting to edit.
 (The default is to make a copy in case someone else modifies
 the file during your edit session.)
+.Tg R
 .It Fl R
 Start editing in read-only mode, as if the command name was
 .Nm view ,
 or the
 .Cm readonly
 option was set.
+.Tg r
 .It Fl r
 Recover the specified files or, if no files are specified,
 list the files that could be recovered.
@@ -119,10 +124,12 @@ If no recoverable files by the specified
 the file is edited as if the
 .Fl r
 option had not been specified.
+.Tg S
 .It Fl S
 Run with the
 .Cm secure
 edit option set, disallowing all access to external programs.
+.Tg s
 .It Fl s
 Enter batch mode; applicable only to
 .Nm ex
@@ -137,14 +144,17 @@ This is the POSIX 1003.2 interface for t
 argument.
 .Nm nex Ns / Ns Nm nvi
 supports both the old and new syntax.
+.Tg t
 .It Fl t Ar tag
 Start editing at the specified
 .Ar tag
 (see
 .Xr ctags 1 ) .
+.Tg v
 .It Fl v
 Start editing in vi mode, as if the command name was
 .Nm vi .
+.Tg w
 .It Fl w Ar size
 Set the initial window size to the specified number of lines.
 .El
@@ -553,6 +563,7 @@ and considered part of the motion.
 .Pp
 .Bl -tag -width Ds -compact
 .It Xo
+.Tg control-A
 .Aq Cm control-A
 .Xc
 Search forward
@@ -560,6 +571,7 @@ for the word starting at the cursor posi
 .Pp
 .It Xo
 .Op Ar count
+.Tg control-B
 .Aq Cm control-B
 .Xc
 Page backwards
@@ -569,6 +581,7 @@ Two lines of overlap are maintained, if 
 .Pp
 .It Xo
 .Op Ar count
+.Tg control-D
 .Aq Cm control-D
 .Xc
 Scroll forward
@@ -587,6 +600,7 @@ command, scroll half the number of lines
 .Pp
 .It Xo
 .Op Ar count
+.Tg control-E
 .Aq Cm control-E
 .Xc
 Scroll forward
@@ -595,6 +609,7 @@ lines, leaving the current line and colu
 .Pp
 .It Xo
 .Op Ar count
+.Tg control-F
 .Aq Cm control-F
 .Xc
 Page forward
@@ -602,6 +617,7 @@ Page forward
 screens.
 Two lines of overlap are maintained, if possible.
 .Pp
+.Tg control-G
 .It Aq Cm control-G
 Display the following file information:
 the file name (as given to
@@ -614,10 +630,12 @@ and the current line number as a percent
 .Pp
 .It Xo
 .Op Ar count
+.Tg control-H
 .Aq Cm control-H
 .Xc
 .It Xo
 .Op Ar count
+.Tg
 .Cm h
 .Xc
 Move the cursor back
@@ -626,30 +644,37 @@ characters in the current line.
 .Pp
 .It Xo
 .Op Ar count
+.Tg control-J
 .Aq Cm control-J
 .Xc
 .It Xo
 .Op Ar count
+.Tg control-N
 .Aq Cm control-N
 .Xc
 .It Xo
 .Op Ar count
+.Tg
 .Cm j
 .Xc
 Move the cursor down
 .Ar count
 lines without changing the current column.
 .Pp
+.Tg control-L
 .It Aq Cm control-L
+.Tg control-R
 .It Aq Cm control-R
 Repaint the screen.
 .Pp
 .It Xo
 .Op Ar count
+.Tg control-M
 .Aq Cm control-M
 .Xc
 .It Xo
 .Op Ar count
+.Tg
 .Cm +
 .Xc
 Move the cursor down
@@ -658,21 +683,25 @@ lines to the first non-blank character o
 .Pp
 .It Xo
 .Op Ar count
+.Tg control-P
 .Aq Cm control-P
 .Xc
 .It Xo
 .Op Ar count
+.Tg
 .Cm k
 .Xc
 Move the cursor up
 .Ar count
 lines, without changing the current column.
 .Pp
+.Tg control-T
 .It Aq Cm control-T
 Return to the most 

Add missing .Dv in termios(4)

2021-11-09 Thread Simon Branch
Simple change to format this sentence correctly in non-terminal
environments.

diff --git share/man/man4/termios.4 share/man/man4/termios.4
index dfb7609dd6f..89818ca46c2 100644
--- share/man/man4/termios.4
+++ share/man/man4/termios.4
@@ -1011,7 +1011,7 @@ overflowing the input queue.
 The precise conditions under which
 .Dv STOP
 and
-START
+.Dv START
 characters are transmitted are implementation defined.
 .Pp
 If



Re: Document when fileno(3) returns -1

2021-10-29 Thread Simon Branch
On Fri, Oct 29, 2021 at 04:17:18PM -0700, Simon Branch wrote:
> Here's a simple patch that describes ferror(3)'s error conditions.  Feel free 
> to
> change the wording to be more consistent with OpenBSD conventions; for 
> example,
> I wasn't sure whether to use .St when talking about POSIX, since 'IEEE Std
> 1003.1-2008 ("POSIX.1")' seemed a little long for the main text.  It may also
> be worth linking to the following functions, which return FILE pointers that
> trigger this case:
> 
>- open_memstream(3)
>- open_wmemstream(3)
>- fmemopen(3)
>- fropen(3)
>- fwopen(3)
>- any other ones I'm missing
Whoops, missed funopen(3)...  Knew that I was forgetting something.



Document when fileno(3) returns -1

2021-10-29 Thread Simon Branch
Here's a simple patch that describes ferror(3)'s error conditions.  Feel free to
change the wording to be more consistent with OpenBSD conventions; for example,
I wasn't sure whether to use .St when talking about POSIX, since 'IEEE Std
1003.1-2008 ("POSIX.1")' seemed a little long for the main text.  It may also
be worth linking to the following functions, which return FILE pointers that
trigger this case:

   - open_memstream(3)
   - open_wmemstream(3)
   - fmemopen(3)
   - fropen(3)
   - fwopen(3)
   - any other ones I'm missing

diff --git lib/libc/stdio/ferror.3 lib/libc/stdio/ferror.3
index 526c07e32a8..159c2163be8 100644
--- lib/libc/stdio/ferror.3
+++ lib/libc/stdio/ferror.3
@@ -76,10 +76,31 @@ The function
 examines the argument
 .Fa stream
 and returns its integer file descriptor.
+If no file descriptor is associated with
+.Fa stream ,
+.Fn fileno
+returns -1.
 .Sh ERRORS
-These functions should not fail and do not set the external
+The
+.Fn clearerr ,
+.Fn feof ,
+and
+.Fn ferror
+functions should not fail, and do not set the external
 variable
 .Va errno .
+.Pp
+The
+.Fn fileno
+function may fail, and return -1, if
+.Fa stream
+is not associated with a file descriptor.
+.Ox
+does not follow the optional POSIX recommendation to set
+.Va errno
+to
+.Er EBADF
+in that case.
 .Sh SEE ALSO
 .Xr open 2 ,
 .Xr stdio 3



Regularize shift $((OPTIND - 1)) after getopts

2023-08-30 Thread Simon Branch
Hello tech@,

In an arithmetic substitution -- that is, the $(( ... )) construct in a
shell script -- one can reference variables either with ($a) or without
(a) the dollar sign.  However, the latter is slightly better: the
variable is interpreted as an integer, and then the integer is used
inside the expression.  With the former, the variable's contents are
spliced into the expression before it is parsed.  For example:

  $ a='1+5'
  $ echo $((a * 2))
  12
  $ echo $(($a * 2))
  11

Another gotcha with $(($a)):

  $ echo $((a=2, a))
  2
  $ echo $((a=3, $a))  # oops, $a was spliced in before the assignment
  2

All this to say, $(($a)) should be reserved for where it is required,
not used as the default form.

The following idiom is used throughout the source tree, including in
sh.1 and ksh.1:

  while getopts ...
...
  done
  shift $(($OPTIND - 1))

and it has a couple of variations:

  shift $((OPTIND - 1))  my preferred option
  shift OPTIND-1 works because ksh is magical
  shift $(( OPTIND -1 )) and other silly whitespace conventions
  shift "$(($OPTIND - 1))"   unnecessary: $(()) evaluates to an integer

My patch changes all of these to the $((OPTIND - 1)) usage, which
encourages good shell practice and is clearly understandable.  It
ignores src/gnu since I believe those files are vendored in, and there's
no reason to introduce conflicts when pulling them from upstream.

Reasonable?

  - Simon
Index: bin/ksh/ksh.1
===
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.219
diff -u -r1.219 ksh.1
--- bin/ksh/ksh.1   10 Jun 2023 07:24:21 -  1.219
+++ bin/ksh/ksh.1   30 Aug 2023 18:41:27 -
@@ -3219,7 +3219,7 @@
?)  echo "Usage: ..."; exit 2 ;;
esac
 done
-shift $(($OPTIND - 1))
+shift $((OPTIND - 1))
 echo "Non-option arguments: " "$@"
 .Ed
 .Pp
Index: bin/ksh/sh.1
===
RCS file: /cvs/src/bin/ksh/sh.1,v
retrieving revision 1.157
diff -u -r1.157 sh.1
--- bin/ksh/sh.113 May 2023 18:34:49 -  1.157
+++ bin/ksh/sh.130 Aug 2023 18:41:27 -
@@ -524,7 +524,7 @@
?)  echo "Usage: ..."; exit 2 ;;
esac
 done
-shift $(($OPTIND - 1))
+shift $((OPTIND - 1))
 echo "Non-option arguments: " "$@"
 .Ed
 .It Ic hash Op Fl r | Ar utility
Index: distrib/miniroot/install.sub
===
RCS file: /cvs/src/distrib/miniroot/install.sub,v
retrieving revision 1.1255
diff -u -r1.1255 install.sub
--- distrib/miniroot/install.sub21 Aug 2023 14:33:55 -  1.1255
+++ distrib/miniroot/install.sub30 Aug 2023 18:41:27 -
@@ -3507,7 +3507,7 @@
*)  usage;;
esac
 done
-shift $((OPTIND-1))
+shift $((OPTIND - 1))
 (($# == 0)) || usage
 
 # The installer can be started by using the symbolic links 'install', 'upgrade'
Index: etc/netstart
===
RCS file: /cvs/src/etc/netstart,v
retrieving revision 1.234
diff -u -r1.234 netstart
--- etc/netstart18 Dec 2022 15:52:52 -  1.234
+++ etc/netstart30 Aug 2023 18:41:28 -
@@ -354,7 +354,7 @@
*)  usage;;
esac
 done
-shift $((OPTIND-1))
+shift $((OPTIND - 1))
 
 if ifconfig lo0 inet6 >/dev/null 2>&1; then
IP6KERNEL=true
Index: etc/rc.d/rc.subr
===
RCS file: /cvs/src/etc/rc.d/rc.subr,v
retrieving revision 1.160
diff -u -r1.160 rc.subr
--- etc/rc.d/rc.subr19 Oct 2022 21:04:45 -  1.160
+++ etc/rc.d/rc.subr30 Aug 2023 18:41:28 -
@@ -327,7 +327,7 @@
*) _rc_usage;;
esac
 done
-shift $((OPTIND-1))
+shift $((OPTIND - 1))
 
 _RC_RUNDIR=/var/run/rc.d
 _RC_RUNFILE=${_RC_RUNDIR}/${_name}
Index: sys/arch/sparc64/stand/bootblk/genassym.sh
===
RCS file: /cvs/src/sys/arch/sparc64/stand/bootblk/genassym.sh,v
retrieving revision 1.4
diff -u -r1.4 genassym.sh
--- sys/arch/sparc64/stand/bootblk/genassym.sh  2 Apr 2020 06:06:22 -   
1.4
+++ sys/arch/sparc64/stand/bootblk/genassym.sh  30 Aug 2023 18:41:57 -
@@ -51,7 +51,7 @@
;;
esac
 done
-shift "$(($OPTIND - 1))"
+shift $((OPTIND - 1))
 if [ $# -eq 0 ]; then
usage
exit 1
Index: usr.bin/compress/znew
===
RCS file: /cvs/src/usr.bin/compress/znew,v
retrieving revision 1.3
diff -u -r1.3 znew
--- usr.bin/compress/znew   22 Jul 2005 09:34:16 -  1.3
+++ usr.bin/compress/znew   30 Aug 2023 18:41:59 -
@@ -117,7 +117,7 @@
esac
 done
 
-shift OPTIND-1
+shift $((OPTIND - 1))
 
 if test $# -eq 0; then
echo "$usage"
Index: usr.bin/diff3/diff3.ksh
===
RCS file: 

mg.1: add no-tab-mode to set-default-mode description

2023-06-27 Thread Simon Branch
Hello!

I've been enjoying the resurrection of no-tab-mode in mg(1).
It is simple to enable by default:

set-default-mode notab

The manual page says that "built in modes include fill, indent and
overwrite".  This patch makes the list more complete by adding notab,
and adds "can be set globally with set-default-mode" to no-tab-mode's
documentation (since it is there for the other modes).  Hopefully this
makes the manual page more useful, obvious, and regular.

Thank you,
Simon

Index: mg.1
===
RCS file: /cvs/src/usr.bin/mg/mg.1,v
retrieving revision 1.134
diff -u -r1.134 mg.1
--- mg.128 Apr 2023 10:02:03 -  1.134
+++ mg.128 Jun 2023 01:27:46 -
@@ -770,6 +770,8 @@
 .It Ic no-tab-mode
 Toggle notab mode.
 In this mode, spaces are inserted rather than tabs.
+Can be set globally with
+.Ic set-default-mode .
 .It Ic not-modified
 Turn off the modified flag in the current buffer.
 .It Ic open-line
@@ -920,7 +922,7 @@
 .It Ic set-default-mode
 Append the supplied mode to the list of default modes
 used by subsequent buffer creation.
-Built in modes include: fill, indent and overwrite.
+Built in modes include: fill, indent, notab and overwrite.
 .It Ic set-fill-column
 Prompt the user for a fill column.
 Used by