Skip to content

Commit

Permalink
Replace sh_lexstates with sh_lexrstates
Browse files Browse the repository at this point in the history
After dropping support for non-ASCII character encodings (e.g., EBCDIC)
we no longer need to copy `sh_lexrstates` to `sh_lexstates`. Just
renamed the former to the latter.

This also cleans up a couple of related bogosities such as the magic
`sh_lexstates[2]` reference in src/cmd/ksh93/edit/completion.c with
`ST_NORM` in place of `2`.

Resolves #929
  • Loading branch information
krader1961 committed Oct 6, 2018
1 parent 470c9d8 commit 5f79c7e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/cmd/ksh93/data/lexstates.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ static const char sh_lexstate9[256] = {
S_NOP, S_NOP, S_NOP, S_BRACE, S_PAT, S_ENDCH, S_NOP, S_NOP, // 120 - 127
};

const char *sh_lexrstates[ST_NONE] = {sh_lexstate0, sh_lexstate1, sh_lexstate2, sh_lexstate3,
sh_lexstate4, sh_lexstate5, sh_lexstate6, sh_lexstate7,
sh_lexstate8, sh_lexstate9, sh_lexstate5};
const char *sh_lexstates[ST_NONE] = {sh_lexstate0, sh_lexstate1, sh_lexstate2, sh_lexstate3,
sh_lexstate4, sh_lexstate5, sh_lexstate6, sh_lexstate7,
sh_lexstate8, sh_lexstate9, sh_lexstate5};

const char e_lexversion[] = "%d: invalid binary script version";
const char e_lexspace[] = "line %d: use space or tab to separate operators %c and %c";
Expand Down
7 changes: 4 additions & 3 deletions src/cmd/ksh93/edit/completion.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@
static char *fmtx(Shell_t *shp, const char *string) {
const char *cp = string;
int n, c;
unsigned char *state = (unsigned char *)sh_lexstates[2];
const unsigned char *norm_state = (const unsigned char *)sh_lexstates[ST_NORM];
int offset = stktell(shp->stk);
if (*cp == '#' || *cp == '~') sfputc(shp->stk, '\\');
while ((c = mb1char(cp)), (c > UCHAR_MAX) || (n = state[c]) == 0 || n == S_EPAT) {
while ((c = mb1char(cp)), (c > UCHAR_MAX) || (n = norm_state[c]) == 0 || n == S_EPAT) {
; // empty loop
}
if (n == S_EOF && *string != '#') return (char *)string;
sfwrite(shp->stk, string, --cp - string);
for (string = cp; (c = mb1char(cp)); string = cp) {
if ((n = cp - string) == 1) {
if ((n = state[c]) && n != S_EPAT) sfputc(shp->stk, '\\');
n = norm_state[c];
if (n && n != S_EPAT) sfputc(shp->stk, '\\');
sfputc(shp->stk, c);
} else {
sfwrite(shp->stk, string, n);
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/ksh93/include/lexstates.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@
#define isexp(c) (sh_lexstates[ST_MACRO][c] == S_PAT || (c) == '$' || (c) == '`')
#define ismeta(c) (sh_lexstates[ST_NAME][c] == S_BREAK)

extern char *sh_lexstates[ST_NONE];
extern const char *sh_lexrstates[ST_NONE];
extern const char *sh_lexstates[ST_NONE];
extern const char e_lexversion[];
extern const char e_lexspace[];
extern const char e_lexslash[];
Expand Down
4 changes: 0 additions & 4 deletions src/cmd/ksh93/sh/defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "cdt.h"
#include "fault.h"
#include "jobs.h"
#include "lexstates.h"
#include "name.h"

Shell_t sh = {.shcomp = 0};
Expand All @@ -41,9 +40,6 @@ Dtdisc_t _Nvdisc = {.key = offsetof(Namval_t, nvname), .size = -1, .comparf = nv
struct shared *shgd = NULL;
int32_t sh_mailchk = 600;

// Reserve room for writable state table.
char *sh_lexstates[ST_NONE] = {0};

// These two magic pointers are used to distinguish the purpose of the `extra` parameter of the
// `sh_addbuiltin()` function. It should be one of these two values, NULL, or a `Namfun_t*`.
static int _builtin_delete;
Expand Down
1 change: 0 additions & 1 deletion src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,6 @@ Shell_t *sh_init(int argc, char *argv[], Shinit_f userinit) {

n = strlen(e_version);
if (e_version[n - 1] == '$' && e_version[n - 2] == ' ') e_version[n - 2] = 0;
memcpy(sh_lexstates, sh_lexrstates, ST_NONE * sizeof(char *));
if (!beenhere) {
beenhere = 1;
shp = sh_getinterp();
Expand Down
25 changes: 12 additions & 13 deletions src/cmd/ksh93/sh/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,6 @@ static_fn void comsubst(Mac_t *mp, Shnode_t *t, volatile int type) {
// Copy <str> onto the stack.
//
static_fn void mac_copy(Mac_t *mp, const char *str, size_t size) {
char *state;
const char *cp = str;
const char *ep = cp + size;
int c, n, nopat, len;
Expand All @@ -2088,7 +2087,7 @@ static_fn void mac_copy(Mac_t *mp, const char *str, size_t size) {
if (mp->sp) {
sfwrite(mp->sp, str, size);
} else if (mp->pattern >= 2 || (mp->pattern && nopat) || mp->assign == 3) {
state = sh_lexstates[ST_MACRO];
const char *macro_state = sh_lexstates[ST_MACRO];
// Insert \ before file expansion characters.
while (size-- > 0) {
len = mblen(cp, ep - cp);
Expand All @@ -2097,7 +2096,7 @@ static_fn void mac_copy(Mac_t *mp, const char *str, size_t size) {
size -= (len - 1);
continue;
}
c = state[n = *(unsigned char *)cp++];
c = macro_state[n = *(unsigned char *)cp++];
if (mp->assign == 3 && mp->pattern != 4) {
if (c == S_BRACT) {
nopat = 0;
Expand All @@ -2123,7 +2122,7 @@ static_fn void mac_copy(Mac_t *mp, const char *str, size_t size) {
} else if (mp->pattern == 2 && c == S_SLASH) {
c = 1;
} else if (mp->pattern == 3 && c == S_ESC &&
(state[*(unsigned char *)cp] == S_DIG || (*cp == ESCAPE))) {
(macro_state[*(unsigned char *)cp] == S_DIG || (*cp == ESCAPE))) {
if (!(c = mp->quote)) cp++;
} else {
c = 0;
Expand All @@ -2139,20 +2138,20 @@ static_fn void mac_copy(Mac_t *mp, const char *str, size_t size) {
if (c) sfwrite(stkp, str, c);
} else if (!mp->quote && mp->split && (mp->ifs || mp->pattern)) {
// Split words at ifs characters.
state = mp->shp->ifstable;
char *ifs_state = mp->shp->ifstable;
if (mp->pattern) {
char *sp = "&|()";
while ((c = *sp++)) {
if (state[c] == 0) state[c] = S_EPAT;
if (ifs_state[c] == 0) ifs_state[c] = S_EPAT;
}
sp = "*?[{";
while ((c = *sp++)) {
if (state[c] == 0) state[c] = S_PAT;
if (ifs_state[c] == 0) ifs_state[c] = S_PAT;
}
if (state[ESCAPE] == 0) state[ESCAPE] = S_ESC;
if (ifs_state[ESCAPE] == 0) ifs_state[ESCAPE] = S_ESC;
}
while (size-- > 0) {
n = state[c = *(unsigned char *)cp++];
n = ifs_state[c = *(unsigned char *)cp++];
if (n != S_MBYTE && (len = mblen(cp - 1, ep - cp + 1)) > 1) {
sfwrite(stkp, cp - 1, len);
cp += --len;
Expand All @@ -2176,7 +2175,7 @@ static_fn void mac_copy(Mac_t *mp, const char *str, size_t size) {
}
if (n == S_SPACE || n == S_NL) {
while (size > 0 &&
((n = state[c = *(unsigned char *)cp++]) == S_SPACE || n == S_NL)) {
((n = ifs_state[c = *(unsigned char *)cp++]) == S_SPACE || n == S_NL)) {
size--;
}
if (n == S_MBYTE && sh_strchr(mp->ifsp, cp - 1, ep - cp + 1) >= 0) {
Expand All @@ -2193,7 +2192,7 @@ static_fn void mac_copy(Mac_t *mp, const char *str, size_t size) {
mp->patfound = 0;
if (n == S_DELIM) {
while (size > 0 &&
((n = state[c = *(unsigned char *)cp++]) == S_SPACE || n == S_NL)) {
((n = ifs_state[c = *(unsigned char *)cp++]) == S_SPACE || n == S_NL)) {
size--;
}
}
Expand All @@ -2206,11 +2205,11 @@ static_fn void mac_copy(Mac_t *mp, const char *str, size_t size) {
if (mp->pattern) {
cp = "&|()";
while ((c = *cp++)) {
if (state[c] == S_EPAT) state[c] = 0;
if (ifs_state[c] == S_EPAT) ifs_state[c] = 0;
}
cp = "*?[{";
while ((c = *cp++)) {
if (state[c] == S_PAT) state[c] = 0;
if (ifs_state[c] == S_PAT) ifs_state[c] = 0;
}
if (mp->shp->ifstable[ESCAPE] == S_ESC) mp->shp->ifstable[ESCAPE] = 0;
}
Expand Down

0 comments on commit 5f79c7e

Please sign in to comment.