NetBSD/lib
christos 0a374fd7e5 From Ingo Schwarze:
Reduce obfuscation of errno handling. There is only one purpose
non-local errno handling is needed for:  Inside el_wgets(), several
functions call down indirectly to el_wgetc(), many of them via the
dispatch table.  When el_wgetc() fails, it does properly report
failure, but then various cleanup is done which may clobber errno.
But when returning due to failure, el_wgets() wants to have errno
set to the reason of the original read failure, not to the reason
of some subsequent failure of some cleanup operation.  So el_wgetc()
needs to save errno, and if it's non-zero, el_wgets() needs to
restore it on failure.

This core logic is currently obscured by the fact that el_errno
is set and inspected at some additional places where it isn't needed.
Besides, since el_wgetc() and and el_wgets() are both in read.c,
el_errno does not need to be in struct editline, it can and should
be local to read.c in struct el_read_t.

Let's look at what can be simplified.

 1. keymacro_get() abuses el_errno instead of having a proper
    error return code.  Adding that error return code is easy
    because node_trav() already detects the condition and an
    adequate code is already defined.  Returning it, testing
    for it in read_getcmd(), and returning with error from there
    removes the need to inspect el_errno from el_wgets() after
    calling read_getcmd().
    Note that resetting lastchar and cursor and clearing buffer[0]
    is irrelevant.  The code returns from el_wgets() right afterwards.
    Outside el_wgets(), these variables are no longer relevant.
    When el_wgets() is called the next time, it will call ch_reset()
    anyway, resetting the two pointers.  And as long as lastchar
    points to the beginning of the buffer, the contents of the
    buffer won't be used for anything.

 2. read_getcmd() doesn't need to set el_errno again after el_wgetc()
    failure since el_wgetc() already did so.  While here, remove
    the silly "if EOF or error" comments from the el_wgetc()
    return value tests.  It's a public interface documented in a
    manual, so people working on the implementation can obviously
    be expected to know how it works.  It's a case of

      count++;  /* Increment count. */

 3. In the two code paths of el_wgets() that lead up to "goto noedit",
    there is no need to save the errno because nothing that might
    change it happens before returning.

For clarity, since el_wgets() is the function restoring the errno,
also move initializing it to the same function.

Finally, note that restoring errno when the saved value is zero is
wrong.  No library code is ever allowed to clear a previously set
value of errno.  Only application programs are allowed to do that,
and even they usually don't need to do so, except when using certain
ill-designed interfaces like strtol(3).

I tested that the behaviour remains sane in the following cases,
all during execution of el_wgets(3) and with a signal handler
for USR1 installed without SA_RESTART.

 * Enter some text and maybe move around a bit.
   Then send a USR1 signal.
   The signal gets processed, then read_char() resumes reading.
   Send another USR1 signal.
   Now el_wgets() sets errno=EINTR and returns -1.

 * Press Ctrl-V to activate ed-quoted-insert.
   Then send a USR1 signal.
   The signal gets processed, then read_char() resumes reading.
   Send another USR1 signal.
   ed_quoted_insert() returns ed_end_of_file(), i.e. CC_EOF,
   and el_wgets() returns 0.

 * Press a key starting a keyboard macro.
   Then send a USR1 signal.
   The signal gets processed, then read_char() resumes reading.
   Send another USR1 signal.
   Now el_wgets() sets errno=EINTR and returns -1.

 * Press : to enter builtin command mode.
   Start typing a command.
   Then send a USR1 signal.
   The signal gets processed, then read_char() resumes reading.
   Send another USR1 signal.
   Now c_gets() returns -1, ed_command() beeps and returns CC_REFRESH,
   and el_wgets() resumes operation as it should.

I also tested with "el_set(el, EL_EDITMODE, 0)", and it returns
the right value and sets errno correctly.
2016-05-24 15:00:45 +00:00
..
csu We need the -O1 hack (for gcc 5.3) for crtbegin.c as well. 2016-05-10 10:23:09 +00:00
i18n_module
libarch Define _KERNTYPES for things that need it. 2016-01-23 21:22:45 +00:00
libbluetooth correct comment in literal section 2016-01-22 08:51:40 +00:00
libbpfjit
libbsdmalloc
libbz2 Reorg docs, part 1: 2014-07-05 19:22:41 +00:00
libc remove stray debugging. 2016-05-15 20:37:48 +00:00
libc_vfp Possibly build libc_vfp if MACHINE_CPU is aarch64 too. 2015-07-08 01:08:24 +00:00
libcompat PR/50711: David Binderman: Fix memory leak on error 2016-01-26 16:05:18 +00:00
libcrypt fix error messages 2015-06-17 00:15:26 +00:00
libcurses Clear the "forced" flag after updating a line, otherwise we'll always do 2016-01-10 08:11:06 +00:00
libdm The actual header file for these functions is dm.h, not libdm.h. 2016-01-22 22:12:40 +00:00
libedit From Ingo Schwarze: 2016-05-24 15:00:45 +00:00
libexecinfo Don't subtract base if not pie. 2016-04-20 14:00:16 +00:00
libform PR/50919: David Binderman: Re-do all the debug stuff in a more sustainable way. 2016-03-09 19:47:13 +00:00
libintl back to the defines (fixing a typo -- extra 'g') 2015-06-08 15:04:20 +00:00
libipsec
libisns
libkern Define _KERNTYPES for things that need it. 2016-01-23 21:22:45 +00:00
libkvm some ports need <sys/wait.h> 2016-04-04 22:14:38 +00:00
liblwres
libm No fenv for m68k with SOFTFLOAT for now - so at least it builds. 2016-03-30 07:44:06 +00:00
libmenu fix unused warnings 2013-10-18 19:53:59 +00:00
libnpf - Change LDADD/DPADD in library dependencies to LIBDPLIBS 2016-01-05 13:07:46 +00:00
libossaudio Add missing defines for 16, 24 and 32 bit NE and OE formats. 2014-09-09 10:45:18 +00:00
libp2k Don't include <rump/rumpvnode_if.h> from rump.h. It's not needed 2016-01-25 11:45:57 +00:00
libpam Adapt to the new API. 2015-04-04 02:51:10 +00:00
libpanel Specify path of a local internal header of libpanel 2015-11-22 04:30:33 +00:00
libpci unsigned -> unsigned int 2016-01-23 07:21:18 +00:00
libperfuse Define _KERNTYPES for things that need it. 2016-01-23 21:22:45 +00:00
libpmc pmc_evid_, pmc_ctr_t etc are defined in <machine/types.h> but are not exposed 2016-01-23 21:44:55 +00:00
libposix MKCOMPAT fixes for when compat MACHINE_CPU != normal MACHINE_CPU 2014-08-10 23:25:49 +00:00
libppath
libprop
libpthread Formatting, typos, whitespace fixes. 2016-04-24 09:01:45 +00:00
libpthread_dbg don't use kernel types. 2016-01-23 14:02:21 +00:00
libpuffs there is no pflags, just flags. 2016-04-11 00:47:19 +00:00
libquota Some NFS servers return RPC_PROGNOTREGISTERED instead of RPC_PROGVERSMISMATCH 2016-01-30 16:31:28 +00:00
libradius
librefuse Define _KERNTYPES for things that need it. 2016-01-23 21:22:45 +00:00
libresolv PR/50907: David Binderman: Remove useless strlen()'s and memcpy()'s. 2016-03-07 14:35:39 +00:00
librmt
librpcsvc remove __P 2013-12-20 21:04:09 +00:00
librt Note that SCHED_OTHER must have priority PRI_NONE. 2016-02-20 16:00:10 +00:00
librump This is not needed anymore. 2015-08-21 06:56:35 +00:00
librumpclient Define _KERNTYPES for things that need it. 2016-01-23 21:22:45 +00:00
librumpdev
librumphijack Define _KERNTYPES for things that need it. 2016-01-23 21:22:45 +00:00
librumpnet
librumpuser Move librumpuser compile-time options into the librumpuser source 2016-01-25 00:24:23 +00:00
librumpvfs Move rump kernel man pages from various sources to sys/rump 2014-11-09 17:39:37 +00:00
libskey Uses FILE *, needs stdio.h. 2016-01-22 23:25:51 +00:00
libss
libtelnet Avoid enum type mismatch. 2014-04-26 22:10:40 +00:00
libterminfo - if we are freeing cur_term, set it to NULL. 2016-04-01 19:59:08 +00:00
libukfs Don't include <rump/rumpvnode_if.h> from rump.h. It's not needed 2016-01-25 11:45:57 +00:00
libusbhid Uses __BEGIN_DECLS so needs sys/cdefs.h; also needs stdint.h. 2016-01-22 23:51:23 +00:00
libutil Make relative date changes ("+ 2 months") etc, work a little more sanely. 2016-05-03 18:14:54 +00:00
libwrap change static to volatile. 2016-03-16 22:32:32 +00:00
liby
libz Merge riastradh-drm2 to HEAD. 2014-03-18 18:20:35 +00:00
lua Fix function name, no functional change. 2016-02-15 15:56:33 +00:00
npf
bumpversion
checkoldver
checkver
checkvers
Makefile don't build libmudflap* for GCC 5.3. 2016-03-17 00:09:47 +00:00
Makefile.inc