Skip to content
Draft
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 builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -3381,7 +3381,7 @@ do_mkbool(int nargs)
int
gawk_system(const char *command)
{
#if defined(VMS) || defined(__MINGW32__)
#if defined(VMS) || defined(__MINGW32__) || defined(__wasi__)
return system(command);
#else /* ! (defined(VMS) || defined(__MINGW32__)) */
pid_t childpid;
Expand Down
84 changes: 82 additions & 2 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1839,6 +1839,18 @@ devopen_simple(const char *name, const char *mode, bool try_real_open)
return openfd;
}

#ifdef __wasi__
/* refused_inet_name --- true for a /inet special file name; this build has no network access */

static bool
refused_inet_name(const char *name)
{
return strncmp(name, "/inet/", 6) == 0
|| strncmp(name, "/inet4/", 7) == 0
|| strncmp(name, "/inet6/", 7) == 0;
}
#endif

/* devopen --- handle /dev/std{in,out,err}, /dev/fd/N, /inet, regular files */

/*
Expand All @@ -1862,6 +1874,11 @@ devopen(const char *name, const char *mode)

if (do_traditional) {
goto strictopen;
#ifdef __wasi__
} else if (refused_inet_name(name)) {
errno = EAFNOSUPPORT;
return INVALID_HANDLE;
#endif
} else if (inetfile(name, strlen(name), & isi)) {
#ifdef HAVE_SOCKETS
#define DEFAULT_RETRIES 20
Expand Down Expand Up @@ -2525,6 +2542,54 @@ two_way_open(const char *str, struct redirect *rp, int extfd)
return true;
}

#elif defined(__wasi__)
/* case 4: two way pipe to a command started as a Golem tool call */
{
int rfd, wfd;

if (refused_inet_name(str)) {
errno = EAFNOSUPPORT;
return false;
}

if (golem_gawk_coprocess(str, & rfd, & wfd) != 0)
return false;
rp->pid = -1;
rp->iop = iop_alloc(rfd, str, 0);
find_input_parser(rp->iop);
iop_finish(rp->iop);
rp->ifp = NULL;
rp->output.fp = NULL;
if (rp->iop->valid) {
rp->ifp = fdopen(rfd, binmode("r"));
if (rp->ifp != NULL)
rp->output.fp = fdopen(wfd, binmode("w"));
}
if (rp->ifp == NULL || rp->output.fp == NULL) {
/*
* The command starts only at the first read or the close, so nothing
* has run: close each descriptor once and keep the setup's errno.
* rp->ifp owns rfd when it exists; otherwise iop_close closes rfd
* unless iop_finish already did.
*/
int save_errno = rp->iop->valid ? errno : rp->iop->errcode;

if (rp->ifp != NULL) {
(void) fclose(rp->ifp);
rp->ifp = NULL;
rp->iop->public.fd = INVALID_HANDLE;
}
(void) iop_close(rp->iop);
rp->iop = NULL;
(void) close(wfd);
errno = save_errno;
return false;
}
rp->output.mode = "w";
rp->output.name = str;
find_output_wrapper(& rp->output);
return true;
}
#else /*PIPES_SIMULATED*/

fatal(_("`|&' not supported"));
Expand Down Expand Up @@ -2815,7 +2880,16 @@ gawk_popen(const char *cmd, struct redirect *rp)
static int
gawk_pclose(struct redirect *rp)
{
#ifdef __wasi__
int rval = 0, aval, fd;

/* the read end of a two-way pipe can be closed before close(cmd) closes the rest */
if (rp->iop == NULL && rp->ifp == NULL)
return rp->status;
fd = rp->iop != NULL ? rp->iop->public.fd : INVALID_HANDLE;
#else
int rval, aval, fd = rp->iop->public.fd;
#endif

if (rp->iop != NULL) {
rp->iop->public.fd = dup(fd); /* kludge to allow close() + pclose() */
Expand All @@ -2824,6 +2898,12 @@ gawk_pclose(struct redirect *rp)
rp->iop = NULL;
aval = pclose(rp->ifp);
rp->ifp = NULL;
#ifdef __wasi__
/* the callers expect a sanitized status, as from the real-pipe version */
if (aval >= 0)
aval = sanitize_exit_status(aval);
rp->status = (rval < 0 ? rval : aval);
#endif
return (rval < 0 ? rval : aval);
}

Expand Down Expand Up @@ -4702,7 +4782,7 @@ save_pipe(int fd, FILE *fp, pid_t pid)
static FILE *
gawk_popen_write(const char *cmd)
{
#if defined(VMS) || defined(__MINGW32__)
#if defined(VMS) || defined(__MINGW32__) || defined(__wasi__)
return popen(cmd, binmode("w"));
#else
pid_t childpid;
Expand Down Expand Up @@ -4763,7 +4843,7 @@ gawk_popen_write(const char *cmd)
static int
gawk_popen_write_close(FILE *fp)
{
#if defined(VMS) || defined(__MINGW32__)
#if defined(VMS) || defined(__MINGW32__) || defined(__wasi__)
return pclose(fp);
#else
write_pipe *wp;
Expand Down
9 changes: 9 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,9 @@ parse_args(int argc, char **argv)
break;

case 'D':
#ifdef __wasi__
fatal(_("-D/--debug: the debugger is not supported in this build"));
#endif
do_flags |= DO_DEBUG;
if (optarg != NULL && optarg[0] != '\0')
command_file = optarg;
Expand Down Expand Up @@ -1705,6 +1708,9 @@ parse_args(int argc, char **argv)
break;

case 'M':
#ifdef __wasi__
fatal(_("-M/--bignum: arbitrary precision arithmetic is not supported in this build"));
#endif
#ifdef HAVE_MPFR
do_flags |= DO_MPFR;
#else
Expand All @@ -1731,6 +1737,9 @@ parse_args(int argc, char **argv)
break;

case 'T': // --persist[=file]
#ifdef __wasi__
fatal(_("--persist: persistent memory is not supported in this build"));
#endif
#ifdef USE_PERSISTENT_MALLOC
if (optarg == NULL)
optarg = "/some/file";
Expand Down
11 changes: 7 additions & 4 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,18 +925,21 @@ format_integer_digits(NODE *arg, struct flags *flags, bool *used_float)
// octal or hex or unsigned decimal
chbuf = (flags->format == 'X' ? Uchbuf : lchbuf);

/* Converting an out-of-range double is undefined, so check the range first. */
if (tmpval < 0) {
uval = (uintmax_t) (intmax_t) tmpval;
if ((AWKNUM)(intmax_t)uval != double_to_int(tmpval)) {
if (tmpval < -0x1p63
|| (uval = (uintmax_t) (intmax_t) tmpval,
(AWKNUM)(intmax_t)uval != double_to_int(tmpval))) {
flags->format = 'g';
free((void *) buf);
*used_float = true;

return format_float(arg, flags);
}
} else {
uval = (uintmax_t) tmpval;
if ((AWKNUM)uval != double_to_int(tmpval)) {
if (tmpval >= 0x1p64
|| (uval = (uintmax_t) tmpval,
(AWKNUM)uval != double_to_int(tmpval))) {
flags->format = 'g';
free((void *) buf);
*used_float = true;
Expand Down