From 6b69b4f4007ad2f04e100f0dbb7dc596591e984b Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 7 Jan 2025 23:03:10 +0000 Subject: [PATCH] remove writable copy of lexical state table pointers (re: cffd06ff) 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: https://github.com/att/ast/commit/5f79c7e0 --- src/cmd/ksh93/Mamfile | 1 - src/cmd/ksh93/data/lexstates.c | 4 ++-- src/cmd/ksh93/edit/completion.c | 4 ++-- src/cmd/ksh93/include/lexstates.h | 5 ++--- src/cmd/ksh93/sh/defs.c | 6 +---- src/cmd/ksh93/sh/init.c | 1 - src/cmd/ksh93/sh/macro.c | 37 +++++++++++++++---------------- src/cmd/ksh93/tests/locale.sh | 2 +- src/cmd/ksh93/tests/variables.sh | 2 +- 9 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/cmd/ksh93/Mamfile b/src/cmd/ksh93/Mamfile index 6a700b16c5bd..0841cfbaf9c8 100644 --- a/src/cmd/ksh93/Mamfile +++ b/src/cmd/ksh93/Mamfile @@ -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 diff --git a/src/cmd/ksh93/data/lexstates.c b/src/cmd/ksh93/data/lexstates.c index cbdc96ab7166..18968ad7d8be 100644 --- a/src/cmd/ksh93/data/lexstates.c +++ b/src/cmd/ksh93/data/lexstates.c @@ -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 * * * @@ -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, diff --git a/src/cmd/ksh93/edit/completion.c b/src/cmd/ksh93/edit/completion.c index e1c57cc91aea..425caf71bf6f 100644 --- a/src/cmd/ksh93/edit/completion.c +++ b/src/cmd/ksh93/edit/completion.c @@ -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 * * * @@ -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 diff --git a/src/cmd/ksh93/include/lexstates.h b/src/cmd/ksh93/include/lexstates.h index 4487f2f9d037..f66ffc151ef6 100644 --- a/src/cmd/ksh93/include/lexstates.h +++ b/src/cmd/ksh93/include/lexstates.h @@ -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 * * * @@ -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[]; diff --git a/src/cmd/ksh93/sh/defs.c b/src/cmd/ksh93/sh/defs.c index 47699018012d..00623fecf948 100644 --- a/src/cmd/ksh93/sh/defs.c +++ b/src/cmd/ksh93/sh/defs.c @@ -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 * * * @@ -24,7 +24,6 @@ #include "shopt.h" #include "defs.h" #include "jobs.h" -#include "shlex.h" Shell_t sh = {0}; @@ -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; diff --git a/src/cmd/ksh93/sh/init.c b/src/cmd/ksh93/sh/init.c index 78f6abb35f2a..3ab349b01c19 100644 --- a/src/cmd/ksh93/sh/init.c +++ b/src/cmd/ksh93/sh/init.c @@ -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(); diff --git a/src/cmd/ksh93/sh/macro.c b/src/cmd/ksh93/sh/macro.c index d5392eed4432..a29b60f5bdec 100644 --- a/src/cmd/ksh93/sh/macro.c +++ b/src/cmd/ksh93/sh/macro.c @@ -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 * * * @@ -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; @@ -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) { @@ -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) @@ -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++; @@ -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); @@ -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) { @@ -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; @@ -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; diff --git a/src/cmd/ksh93/tests/locale.sh b/src/cmd/ksh93/tests/locale.sh index 5d40b8cc01dd..5b3b7a209c1f 100755 --- a/src/cmd/ksh93/tests/locale.sh +++ b/src/cmd/ksh93/tests/locale.sh @@ -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 # # # diff --git a/src/cmd/ksh93/tests/variables.sh b/src/cmd/ksh93/tests/variables.sh index 1e83aa72d4d2..55296dfe559c 100755 --- a/src/cmd/ksh93/tests/variables.sh +++ b/src/cmd/ksh93/tests/variables.sh @@ -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 # # #