Skip to content

Commit

Permalink
remove writable copy of lexical state table pointers (re: cffd06f)
Browse files Browse the repository at this point in the history
ksh init used to convert the lexical state tables on the fly on
systems using the EBCDIC character set. For this purpose, the
readonly sh_lexrstates table was copied to the read-write
sh_lexstates table, which was the only modified on EBCDIC systems.
So, as of this commit, ksh uses the read-only table directly.

src/cmd/ksh93/sh/defs.c,
src/cmd/ksh93/sh/init.c:
- Remove writable sh_lexstates[].

src/cmd/ksh93/include/lexstates.h,
src/cmd/ksh93/data/lexstates.c:
- Rename sh_lexrstates to the old read-write name, sh_lexstates.
  This minimises necessary code changes.

src/cmd/ksh93/sh/macro.c:
- Adapt to sh.lexstates now being const.
- Split 'state' into 'macro_state' (now const) and 'ifs_state' (not
  const as it refers to the writable IFS state table, sh.ifstable).

src/cmd/ksh93/edit/completion.c:
- Change sh_lexstates[2] to sh_lexstates[ST_NORM] for clarity and
  future-proofing (in case the tables are ever reordered).

This commit is backported from ksh2020:
att@5f79c7e0
  • Loading branch information
McDutchie committed Jan 7, 2025
1 parent a4e1bb5 commit 6b69b4f
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/cmd/ksh93/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ make install virtual
done

make sh/defs.c
prev include/shlex.h
prev include/jobs.h
prev include/defs.h
prev shopt.h
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/ksh93/data/lexstates.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1982-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* Copyright (c) 2020-2025 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand Down Expand Up @@ -718,7 +718,7 @@ static const char sh_lexstate11[256] =
/*
* This must be kept synchronous with all the above and the ST_* definitions in lexstates.h
*/
const char *sh_lexrstates[ST_NONE] =
const char *sh_lexstates[ST_NONE] =
{
sh_lexstate0, sh_lexstate1, sh_lexstate2, sh_lexstate3,
sh_lexstate4, sh_lexstate5, sh_lexstate6, sh_lexstate7,
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/ksh93/edit/completion.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* Copyright (c) 2020-2025 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand Down Expand Up @@ -38,7 +38,7 @@ static char *fmtx(const char *string)
const char *cp = string;
int n = 0, c;
int pos = 0;
unsigned char *state = (unsigned char*)sh_lexstates[2];
unsigned char *state = (unsigned char*)sh_lexstates[ST_NORM];
int offset = stktell(sh.stk);
char hc[3];
#if SHOPT_HISTEXPAND
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/ksh93/include/lexstates.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1982-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* Copyright (c) 2020-2025 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand Down Expand Up @@ -124,8 +124,7 @@
#define isexp(c) (((c) < 0 || (c) > 255) ? 0 : (sh_lexstates[ST_MACRO][c] == S_PAT || (c) == '$' || (c) == '`'))
#define ismeta(c) (((c) < 0 || (c) > 255) ? 0 : 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
6 changes: 1 addition & 5 deletions src/cmd/ksh93/sh/defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1982-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* Copyright (c) 2020-2025 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -24,7 +24,6 @@
#include "shopt.h"
#include "defs.h"
#include "jobs.h"
#include "shlex.h"

Shell_t sh = {0};

Expand All @@ -33,9 +32,6 @@ Dtdisc_t _Nvdisc =
offsetof(Namval_t,nvname), -1 , 0, 0, 0, nv_compare
};

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

struct jobs job = {0};
int32_t sh_mailchk = 600;

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 @@ -1262,7 +1262,6 @@ Shell_t *sh_init(int argc,char *argv[], Shinit_f userinit)
sh_onstate(SH_INIT);
/* truncate final " $\0\n" from e_version for ${.sh.version} output (it's there for what(1) or ident(1)) */
e_version[sizeof e_version - 5] = '\0';
memcpy(sh_lexstates,sh_lexrstates,ST_NONE*sizeof(char*));
sh.current_pid = sh.pid = getpid();
sh.current_ppid = sh.ppid = getppid();
sh.userid = getuid();
Expand Down
37 changes: 18 additions & 19 deletions src/cmd/ksh93/sh/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* Copyright (c) 2020-2025 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand Down Expand Up @@ -2385,7 +2385,6 @@ static void comsubst(Mac_t *mp,Shnode_t* t, int type)
*/
static void mac_copy(Mac_t *mp,const char *str, int size)
{
char *state;
const char *cp=str;
int c,n,nopat,len;
Stk_t *stkp=sh.stk;
Expand All @@ -2395,7 +2394,7 @@ static void mac_copy(Mac_t *mp,const char *str, int size)
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)
{
Expand All @@ -2405,7 +2404,7 @@ static void mac_copy(Mac_t *mp,const char *str, int 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)
Expand Down Expand Up @@ -2434,7 +2433,7 @@ static void mac_copy(Mac_t *mp,const char *str, int 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)))
else if(mp->pattern==3 && c==S_ESC && (macro_state[*(unsigned char*)cp]==S_DIG || (*cp==ESCAPE)))
{
if(!(c=mp->quote))
cp++;
Expand All @@ -2455,27 +2454,27 @@ static void mac_copy(Mac_t *mp,const char *str, int size)
else if(!mp->quote && mp->split && (mp->ifs||mp->pattern))
{
/* split words at ifs characters */
state = sh.ifstable;
char *ifs_state = sh.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(mbwide() && n!=S_MBYTE && (len=mbsize(cp-1))>1)
{
sfwrite(stkp,cp-1, len);
Expand Down Expand Up @@ -2519,7 +2518,7 @@ static void mac_copy(Mac_t *mp,const char *str, int size)
}
if(n==S_SPACE || n==S_NL)
{
while(size>0 && ((n=state[c= *(unsigned char*)cp++])==S_SPACE||n==S_NL))
while(size>0 && ((n = ifs_state[c = *(unsigned char*)cp++])==S_SPACE || n==S_NL))
size--;
if(mbwide() && n==S_MBYTE && sh_strchr(mp->ifsp,cp-1)>=0)
{
Expand All @@ -2536,7 +2535,7 @@ static void mac_copy(Mac_t *mp,const char *str, int size)
endfield(mp,n==S_DELIM||mp->quoted);
mp->patfound = 0;
if(n==S_DELIM)
while(size>0 && ((n=state[c= *(unsigned char*)cp++])==S_SPACE||n==S_NL))
while(size>0 && ((n = ifs_state[c = *(unsigned char*)cp++])==S_SPACE || n==S_NL))
size--;
if(size<=0)
break;
Expand All @@ -2551,14 +2550,14 @@ static void mac_copy(Mac_t *mp,const char *str, int size)
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(sh.ifstable[ESCAPE]==S_ESC)
sh.ifstable[ESCAPE] = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/tests/locale.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# #
# This software is part of the ast package #
# Copyright (c) 1982-2012 AT&T Intellectual Property #
# Copyright (c) 2020-2024 Contributors to ksh 93u+m #
# Copyright (c) 2020-2025 Contributors to ksh 93u+m #
# and is licensed under the #
# Eclipse Public License, Version 2.0 #
# #
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/tests/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# #
# This software is part of the ast package #
# Copyright (c) 1982-2012 AT&T Intellectual Property #
# Copyright (c) 2020-2024 Contributors to ksh 93u+m #
# Copyright (c) 2020-2025 Contributors to ksh 93u+m #
# and is licensed under the #
# Eclipse Public License, Version 2.0 #
# #
Expand Down

0 comments on commit 6b69b4f

Please sign in to comment.