Skip to content

Commit

Permalink
Fix SHOPT_KIA after sh_lexopen() full reset change (re: 2e000c9)
Browse files Browse the repository at this point in the history
It broke SHOPT_KIA, which (when enabled) adds its own lexer struct
data, which should not ever be reset for ksh's -R option to work.
This was not noticed sooner because SHOPT_KIA code is not compiled
in by default (it needs to be enabled in SHOPT.sh).

I could revert that commit, but I'd rather divorce the KIA stuff
from the lexer struct.

src/cmd/ksh93/include/shlex.h,
src/cmd/ksh93/sh/defs.c:
- Sepaerate all the KIA stuff into one struct type named Kia_t.
- Declare a global Kia_t variable named 'kia'.

src/cmd/ksh93/sh/{lex,parse,args}.c:
- Update all references to KIA-related lexer struct members.
  • Loading branch information
McDutchie committed Nov 29, 2024
1 parent 0ebaab3 commit 26eec12
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 92 deletions.
2 changes: 0 additions & 2 deletions src/cmd/ksh93/Mamfile
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,6 @@ make install virtual
done

make sh/defs.c
prev include/timeout.h
prev include/edit.h
prev include/shlex.h
prev include/jobs.h
prev include/defs.h
Expand Down
31 changes: 17 additions & 14 deletions src/cmd/ksh93/include/shlex.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ struct _shlex_pvt_lexdata_
int lastc;
int lex_state;
int docextra;
#if SHOPT_KIA
off_t kiaoff;
#endif
};

/*
Expand All @@ -88,17 +85,6 @@ typedef struct _shlex_
int inlineno; /* saved value of sh.inlineno */
int firstline; /* saved value of sh.st.firstline */
int assignlevel; /* nesting level for assignment */
#if SHOPT_KIA
Sfio_t *kiafile; /* kia output file */
Sfio_t *kiatmp; /* kia reference file */
unsigned long script; /* script entity number */
unsigned long fscript; /* script file entity number */
unsigned long current; /* current entity number */
unsigned long unknown; /* <unknown> entity number */
off_t kiabegin; /* offset of first entry */
char *scriptname; /* name of script file */
Dt_t *entity_tree; /* for entity IDs */
#endif /* SHOPT_KIA */
/* The following two struct members are considered private to lex.c */
struct _shlex_pvt_lexdata_ lexd;
struct _shlex_pvt_lexstate_ lex;
Expand Down Expand Up @@ -181,7 +167,24 @@ extern Shnode_t *sh_dolparen(Lex_t*);
extern Lex_t *sh_lexopen(Lex_t*, int);
extern void sh_lexskip(Lex_t*,int,int,int);
extern noreturn void sh_syntax(Lex_t*, int);

#if SHOPT_KIA
typedef struct
{
off_t offset;
Sfio_t *file; /* kia output file */
Sfio_t *tmp; /* kia reference file */
unsigned long script; /* script entity number */
unsigned long fscript; /* script file entity number */
unsigned long current; /* current entity number */
unsigned long unknown; /* <unknown> entity number */
off_t begin; /* offset of first entry */
char *scriptname; /* name of script file */
Dt_t *entity_tree; /* for entity IDs */
} Kia_t;

extern Kia_t kia;

extern int kiaclose(Lex_t *);
extern unsigned long kiaentity(Lex_t*, const char*,int,int,int,int,unsigned long,int,int,const char*);
#endif /* SHOPT_KIA */
Expand Down
22 changes: 11 additions & 11 deletions src/cmd/ksh93/sh/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,25 @@ int sh_argopts(int argc,char *argv[])
errormsg(SH_DICT,ERROR_usage(2),"-R requires scriptname");
UNREACHABLE();
}
if(!(lp->kiafile=sfopen(NULL,ap->kiafile,"w+")))
if(!(kia.file=sfopen(NULL,ap->kiafile,"w+")))
{
errormsg(SH_DICT,ERROR_system(3),e_create,ap->kiafile);
UNREACHABLE();
}
if(!(lp->kiatmp=sftmp(2*SFIO_BUFSIZE)))
if(!(kia.tmp=sftmp(2*SFIO_BUFSIZE)))
{
errormsg(SH_DICT,ERROR_system(3),e_tmpcreate);
UNREACHABLE();
}
sfputr(lp->kiafile,";vdb;CIAO/ksh",'\n');
lp->kiabegin = sftell(lp->kiafile);
lp->entity_tree = dtopen(&_Nvdisc,Dtbag);
lp->scriptname = sh_strdup(sh_fmtq(argv[0]));
lp->script=kiaentity(lp,lp->scriptname,-1,'p',-1,0,0,'s',0,"");
lp->fscript=kiaentity(lp,lp->scriptname,-1,'f',-1,0,0,'s',0,"");
lp->unknown=kiaentity(lp,"<unknown>",-1,'p',-1,0,0,'0',0,"");
kiaentity(lp,"<unknown>",-1,'p',0,0,lp->unknown,'0',0,"");
lp->current = lp->script;
sfputr(kia.file,";vdb;CIAO/ksh",'\n');
kia.begin = sftell(kia.file);
kia.entity_tree = dtopen(&_Nvdisc,Dtbag);
kia.scriptname = sh_strdup(sh_fmtq(argv[0]));
kia.script=kiaentity(lp,kia.scriptname,-1,'p',-1,0,0,'s',0,"");
kia.fscript=kiaentity(lp,kia.scriptname,-1,'f',-1,0,0,'s',0,"");
kia.unknown=kiaentity(lp,"<unknown>",-1,'p',-1,0,0,'0',0,"");
kiaentity(lp,"<unknown>",-1,'p',0,0,kia.unknown,'0',0,"");
kia.current = kia.script;
ap->kiafile = 0;
}
#endif /* SHOPT_KIA */
Expand Down
8 changes: 5 additions & 3 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-2022 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -25,8 +25,6 @@
#include "defs.h"
#include "jobs.h"
#include "shlex.h"
#include "edit.h"
#include "timeout.h"

Shell_t sh = {0};

Expand All @@ -40,3 +38,7 @@ char *sh_lexstates[ST_NONE] = {0};

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

#if SHOPT_KIA
Kia_t kia;
#endif
32 changes: 16 additions & 16 deletions src/cmd/ksh93/sh/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,33 @@ static void refvar(Lex_t *lp, int type)
if(lp->lexd.first)
{
off = (fcseek(0)-(type+1)) - lp->lexd.first;
r=kiaentity(lp,lp->lexd.first+lp->lexd.kiaoff+type,off-lp->lexd.kiaoff,'v',-1,-1,lp->current,'v',0,"");
r=kiaentity(lp,lp->lexd.first+kia.offset+type,off-kia.offset,'v',-1,-1,kia.current,'v',0,"");
}
else
{
int n,offset = stktell(sh.stk);
void *savptr;
char *begin;
off = offset + (fcseek(0)-(type+1)) - fcfirst();
if(lp->lexd.kiaoff < offset)
if(kia.offset < offset)
{
/* variable starts on stack, copy remainder */
if(off>offset)
sfwrite(sh.stk,fcfirst()+type,off-offset);
n = stktell(sh.stk)-lp->lexd.kiaoff;
begin = stkptr(sh.stk,lp->lexd.kiaoff);
n = stktell(sh.stk)-kia.offset;
begin = stkptr(sh.stk,kia.offset);
}
else
{
/* variable in data buffer */
begin = fcfirst()+(type+lp->lexd.kiaoff-offset);
n = off-lp->lexd.kiaoff;
begin = fcfirst()+(type+kia.offset-offset);
n = off-kia.offset;
}
savptr = stkfreeze(sh.stk,0);
r=kiaentity(lp,begin,n,'v',-1,-1,lp->current,'v',0,"");
r=kiaentity(lp,begin,n,'v',-1,-1,kia.current,'v',0,"");
stkset(sh.stk,savptr,offset);
}
sfprintf(lp->kiatmp,"p;%..64d;v;%..64d;%d;%d;r;\n",lp->current,r,sh.inlineno,sh.inlineno);
sfprintf(kia.tmp,"p;%..64d;v;%..64d;%d;%d;r;\n",kia.current,r,sh.inlineno,sh.inlineno);
}
#endif /* SHOPT_KIA */

Expand Down Expand Up @@ -153,7 +153,7 @@ static void lex_advance(Sfio_t *iop, const char *buff, int size, void *context)
if(!lp->lexd.inlexskip)
lp->arg = stkseek(sh.stk,ARGVAL);
#if SHOPT_KIA
lp->lexd.kiaoff += ARGVAL;
kia.offset += ARGVAL;
#endif /* SHOPT_KIA */
}
if(size>0 && (lp->arg||lp->lexd.inlexskip))
Expand Down Expand Up @@ -814,9 +814,9 @@ int sh_lex(Lex_t* lp)
continue;
#if SHOPT_KIA
if(lp->lexd.first)
lp->lexd.kiaoff = fcseek(0)-lp->lexd.first;
kia.offset = fcseek(0)-lp->lexd.first;
else
lp->lexd.kiaoff = stktell(sh.stk)+fcseek(0)-fcfirst();
kia.offset = stktell(sh.stk)+fcseek(0)-fcfirst();
#endif /* SHOPT_KIA */
pushlevel(lp,'$',mode);
mode = ST_DOL;
Expand All @@ -843,7 +843,7 @@ int sh_lex(Lex_t* lp)
case S_EDOL:
/* end $identifier */
#if SHOPT_KIA
if(lp->kiafile)
if(kia.file)
refvar(lp,0);
#endif /* SHOPT_KIA */
if(lp->lexd.warn && c==LBRACT && !lp->lex.intest && !lp->lexd.arith && oldmode(lp)!= ST_NESTED)
Expand Down Expand Up @@ -960,7 +960,7 @@ int sh_lex(Lex_t* lp)
continue;
case S_MOD2:
#if SHOPT_KIA
if(lp->kiafile)
if(kia.file)
refvar(lp,1);
#endif /* SHOPT_KIA */
if(c!=':' && fcgetc(n)>0)
Expand Down Expand Up @@ -2465,11 +2465,11 @@ static void setupalias(Lex_t *lp, const char *string,Namval_t *np)
if(ap->np = np)
{
#if SHOPT_KIA
if(lp->kiafile)
if(kia.file)
{
unsigned long r;
r=kiaentity(lp,nv_name(np),-1,'p',0,0,lp->current,'a',0,"");
sfprintf(lp->kiatmp,"p;%..64d;p;%..64d;%d;%d;e;\n",lp->current,r,sh.inlineno,sh.inlineno);
r=kiaentity(lp,nv_name(np),-1,'p',0,0,kia.current,'a',0,"");
sfprintf(kia.tmp,"p;%..64d;p;%..64d;%d;%d;e;\n",kia.current,r,sh.inlineno,sh.inlineno);
}
#endif /* SHOPT_KIA */
if((ap->nextc=fcget())==0)
Expand Down
Loading

0 comments on commit 26eec12

Please sign in to comment.