1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00
cde/src/lib/libast/misc/optlib.h
Johnothan King bea4fd56e8 Fix 'read -a' failure to create array (re: d55e9686) (#516)
The commit that backported read -a did not add a case label for it
to read.c. This was under the assumption that AST optget(3) would
always convert -a to -A. However, that was only done for first use.

The cause is the short-form options caching mechanism in optget(3).
On the first run, the pre-caching result would be returned, but the
equivalent option (-a) would be cached as if it is its own option,
so on the second and subsequent runs, optget returned 'a' instead
of 'A'. This only happens if no long-form equivalent is present.

Reproducer:

  $ read -A foo <<< 'foo bar baz'
  $ unset foo
  $ read -a foo <<< 'foo bar baz'
  $ echo ${foo[0]}
  foo bar baz

Expected: foo

src/lib/libast/misc/optget.c,
src/lib/libast/misc/optlib.h:
- [by Martijn Dekker] Implement caching for short-option
  equivalents. If a short-form equivalent is found, instead of
  caching it as a separate option, cache the equivalent in a new
  equiv[] array. Check this when reading the cache and substitute
  the main option for the equivalent if one is cached.

src/lib/libcmd/cp.c:
- Fix cp -r/cp -R symlink handling. The -r and -R options sometimes
  ignored -P, -L and -H.
- The -r and -R options no longer follow symlinks by default.

src/cmd/ksh93/bltins/whence.c,
src/lib/libcmd/*.c:
- Remove case labels that are redundant now that the optget(3)
  caching bug is fixed.

src/cmd/ksh93/tests/libcmd.sh:
- Added. This is the new script for the /opt/ast/bin path-bound
  built-ins from libcmd. Other relevant tests are moved into here.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
2022-08-20 17:11:52 +01:00

113 lines
3.6 KiB
C

/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2022 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
* A copy of the License is available at *
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html *
* (with md5 checksum 84283fa8859daf213bdda5a9f8d1be1d) *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* Martijn Dekker <martijn@inlv.org> *
* Johnothan King <johnothanking@protonmail.com> *
* *
***********************************************************************/
/*
* Glenn Fowler
* AT&T Research
*
* command line option parser and usage formatter private definitions
*/
#ifndef _OPTLIB_H
#define _OPTLIB_H
#include <ast.h>
#include <cdt.h>
#define OPT_append 0x001
#define OPT_cache 0x002
#define OPT_functions 0x004
#define OPT_ignore 0x008
#define OPT_long 0x010
#define OPT_minus 0x020
#define OPT_module 0x040
#define OPT_numeric 0x080
#define OPT_old 0x100
#define OPT_plus 0x200
#define OPT_cache_flag 0x001
#define OPT_cache_invert 0x002
#define OPT_cache_numeric 0x004
#define OPT_cache_optional 0x008
#define OPT_cache_string 0x010
#define OPT_CACHE 128
#define OPT_FLAGS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
struct Optdisc_s;
typedef struct Optpass_s
{
char* opts;
char* oopts;
char* id;
char* catalog;
char* release;
char section[4];
unsigned char version;
unsigned char prefix;
unsigned short flags;
} Optpass_t;
typedef struct Optcache_s
{
struct Optcache_s* next;
Optpass_t pass;
int caching;
unsigned char flags[sizeof(OPT_FLAGS)];
char equiv[sizeof(OPT_FLAGS)]; /* short option equivalents */
} Optcache_t;
typedef struct Optstate_s
{
Sfio_t* mp; /* opt_info.msg string stream */
Sfio_t* vp; /* translation string stream */
Sfio_t* xp; /* translation string stream */
Sfio_t* cp; /* compatibility string stream */
Optpass_t pass[8]; /* optjoin() list */
char* argv[2]; /* initial argv copy */
char* strv[3]; /* optstr() argv */
char* str; /* optstr() string */
Sfio_t* strp; /* optstr() stream */
int force; /* force this style */
int pindex; /* prev index for backup */
int poffset; /* prev offset for backup */
int npass; /* # optjoin() passes */
int join; /* optjoin() pass # */
int plus; /* + ok */
int style; /* default opthelp() style */
int width; /* format line width */
int flags; /* display flags */
int emphasis; /* ANSI term emphasis ok */
int localized; /* locale initialized */
Dtdisc_t msgdisc; /* msgdict discipline */
Dt_t* msgdict; /* default ast.id catalog msgs */
Optcache_t* cache; /* OPT_cache cache */
char** conformance; /* conformance ID vector */
} Optstate_t;
#define _OPT_PRIVATE_ \
char pad[2*sizeof(void*)]; \
Optstate_t* state;
#include <error.h>
extern Optstate_t* optstate(Opt_t*);
#endif