Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support loading kshcode from memory to support embedding #752

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ extern char *sh_getcwd(void);

#if SHOPT_SCRIPTONLY
#define is_option(s,x) ((x)==SH_INTERACTIVE || (x)==SH_HISTORY ? 0 : ((s)->v[((x)&WMASK)/WBITS] & (1L << ((x) % WBITS))) )
#define on_option(s,x) ( (x)==SH_INTERACTIVE || (x)==SH_HISTORY ? errormsg(SH_DICT,ERROR_exit(1),e_scriptonly) : ((s)->v[((x)&WMASK)/WBITS] |= (1L << ((x) % WBITS))) )
#define on_option(s,x) ((x)==SH_INTERACTIVE || (x)==SH_HISTORY ? 0 : ((s)->v[((x)&WMASK)/WBITS] |= (1L << ((x) % WBITS))) )
#define off_option(s,x) ((x)==SH_INTERACTIVE || (x)==SH_HISTORY ? 0 : ((s)->v[((x)&WMASK)/WBITS] &= ~(1L << ((x) % WBITS))) )
#else
#define is_option(s,x) ((s)->v[((x)&WMASK)/WBITS] & (1L << ((x) % WBITS)))
Expand Down
28 changes: 27 additions & 1 deletion src/cmd/ksh93/sh/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static int sh_source(Sfio_t *iop, const char *file)
#define REMOTE(m) !(m)
#endif

int sh_main(int ac, char *av[], Shinit_f userinit)
int sh_mainex(int ac, char *av[], Shinit_f userinit,char *code,long codelen)
{
char *name;
int fdin;
Expand Down Expand Up @@ -231,6 +231,12 @@ int sh_main(int ac, char *av[], Shinit_f userinit)
shell_c:
iop = sfnew(NULL,sh.comdiv,strlen(sh.comdiv),0,SFIO_STRING|SFIO_READ);
}
else if(code && codelen) { /* read kshcode from memory */
sh.comdiv=code;
if (codelen == 1) codelen=strlen(code);
iop = sfnew(NULL,sh.comdiv,codelen,0,SFIO_STRING|SFIO_READ);
code=NULL;
}
else
{
name = error_info.id;
Expand Down Expand Up @@ -367,6 +373,26 @@ int sh_main(int ac, char *av[], Shinit_f userinit)
sh_done(0);
}

int sh_main(int ac, char *av[], Shinit_f userinit)
{
return sh_mainex(ac, av, userinit, NULL , 0);
}

int sh_maincode(int ac, char *av[], char *code)
{
if (code) {
long len;
char *ptr;
len=strlen(code);
ptr=(char *)sh_malloc(len+8);
if (ptr) {
strcpy(ptr,code);
return sh_mainex(ac, av, NULL,ptr,len);
}
}
return 0;
}

/*
* iop is not null when the input is a string
* fdin is the input file descriptor
Expand Down