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

pipe/fork/exec and friends as custom functions #153

Open
wants to merge 13 commits into
base: master
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ cal/test082.cal
charbit.h
chatbit
chk_c
.cache/clangd
.clang-format
clang-format-testee.c
compile_commands.json
compile_flags.txt
conf.h
const_tmp
cscript/4dsphere
Expand All @@ -53,6 +58,7 @@ cscript/simple
cscript/square
custom/.all
custom/libcustcalc*
custom/.gitignore
debug.out
.dynamic
endian
Expand Down Expand Up @@ -184,4 +190,5 @@ unused_tmp
ustat_tmp
ver_calc
vs_tmp
.vscode
win32/
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
The following are the changes from calc version 2.15.0.7 to 2.15.0.8:

Adding support for pipe/fork/exec via custom functions.

The following are the changes from calc version 2.15.0.6 to 2.15.0.7:

Thanks to GitHub user @bambooleafz a critical bug (GitHub issue
Expand Down
1 change: 1 addition & 0 deletions custhelp
8 changes: 4 additions & 4 deletions custom/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ include ${TARGET_MKF}
#
# Put your custom calc resource files here.
#
CUSTOM_CALC_FILES= argv.cal halflen.cal pzasusb8.cal pmodm127.cal register.cal
CUSTOM_CALC_FILES= argv.cal halflen.cal pzasusb8.cal pmodm127.cal register.cal pfe.cal

# The custom help files to install
#
# Put your custom help files here.
#
CUSTOM_HELP= argv devnull help sysinfo pzasusb8 pmodm127 register
CUSTOM_HELP= argv devnull help sysinfo pzasusb8 pmodm127 register pfe

# Any .h files that are needed by programs that use
# libcustcalc${LIB_EXT_VERSION}
Expand All @@ -110,7 +110,7 @@ CUSTOM_H_SRC=
# Put your custom .c files here.
#
CUSTOM_SRC= c_argv.c c_devnull.c c_help.c c_sysinfo.c c_pzasusb8.c \
c_pmodm127.c c_register.c
c_pmodm127.c c_register.c u_pfe.c

# Any .o files that are needed by program that use
# libcustcalc${LIB_EXT_VERSION}.
Expand All @@ -122,7 +122,7 @@ CUSTOM_SRC= c_argv.c c_devnull.c c_help.c c_sysinfo.c c_pzasusb8.c \
# Put your custom .o files here.
#
CUSTOM_OBJ= c_argv.o c_devnull.o c_help.o c_sysinfo.o c_pzasusb8.o \
c_pmodm127.o c_register.o
c_pmodm127.o c_register.o u_pfe.o


##########################################################################
Expand Down
2 changes: 1 addition & 1 deletion custom/c_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ c_help(char *UNUSED(name), int UNUSED(count), VALUE **vals)
/*
* give the help
*/
customhelp((char *)vals[0]->v_str);
customhelp((char *)vals[0]->v_str->s_str);

/*
* return NULL
Expand Down
71 changes: 71 additions & 0 deletions custom/custtbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ E_FUNC VALUE c_pzasusb8(char*, int, VALUE**);
E_FUNC VALUE c_pmodm127(char*, int, VALUE**);
E_FUNC VALUE c_register(char*, int, VALUE**);

#define U_FUNC(name) E_FUNC VALUE name(char*, int, VALUE**);

// pipe/fork/exec
U_FUNC(u_pfe_fork)
U_FUNC(u_pfe_pipe)
U_FUNC(u_pfe_close)
U_FUNC(u_pfe_execvp)
U_FUNC(u_pfe_dup)
U_FUNC(u_pfe_dup2)
U_FUNC(u_pfe_write)
U_FUNC(u_pfe_read)
U_FUNC(u_pfe_select)
U_FUNC(u_pfe_poll)

U_FUNC(u_pfe_wait4)

U_FUNC(u_pfe_pfe)
U_FUNC(u_pfe_pwrite)
U_FUNC(u_pfe_pread)

// vike's various additions
U_FUNC(u_vadd_basename)
U_FUNC(u_vadd_dirname)
U_FUNC(u_vadd_getcwd)
U_FUNC(u_vadd_getpid)
U_FUNC(u_vadd_getppid)
U_FUNC(u_vadd_inputname) //cspell:ignore inputname

#endif /* CUSTOM */

Expand Down Expand Up @@ -136,6 +163,50 @@ CONST struct custom cust[] = {
{ "register", "get or set customer registers",
1, 2, c_register },

{ "fork", "create process",
0, 0, u_pfe_fork },
{ "pipe", "create descriptor pair for interprocess communication",
0, 0, u_pfe_pipe },
{ "dup", "duplicate a file descriptor",
1, 1, u_pfe_dup },
{ "dup2", "duplicate a file descriptor",
2, 2, u_pfe_dup2 },
{ "close", "remove a file descriptor",
1, 1, u_pfe_close },
{ "execvp", "execute a file",
2, 2, u_pfe_execvp },
{ "write", "write output",
2, 2, u_pfe_write },
{ "read", "read input",
1, 2, u_pfe_read },
{ "select", "examine file descriptors",
3, 4, u_pfe_select },
{ "poll", "synchronous I/O multiplexing",
2, 3, u_pfe_poll },

{ "wait4", "wait for process",
1, 4, u_pfe_wait4 },

{ "pfe", "pipe/fork/exec",
4, 4, u_pfe_pfe },
{ "pwrite", "write and close",
2, 2, u_pfe_pwrite },
{ "pread", "read until eof, close and wait for exit status",
3, 3, u_pfe_pread },

{ "getpid", "get calling process identification",
0, 0, u_vadd_getpid },
{ "getppid", "get parent process identification",
0, 0, u_vadd_getppid },
{ "getcwd", "get working directory pathname",
0, 0, u_vadd_getcwd },
{ "inputname", "get name of input",
0, 0, u_vadd_inputname },
{ "basename", "extract the base portion of a pathname",
1, 1, u_vadd_basename },
{ "dirname", "extract the directory part of a pathname",
1, 1, u_vadd_dirname },


#endif /* CUSTOM */

Expand Down
85 changes: 85 additions & 0 deletions custom/pfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
NAME
pfe - pipe/fork/exec and friends

SYNOPSIS
pipe = custom("pipe") ## create descriptor pair for interprocess communication
pid = custom("fork") ## create process
tgt = custom("dup", src) ## duplicate a file descriptor
tgt = custom("dup2", src, tgt) ## duplicate a file descriptor
e = custom("close", fd) ## remove a file descriptor
e = custom("execvp", arg0, args) ## execute a file
count = custom("write", fd, str) ## write output
str = custom("read", fd) ## read input
count = custom("select", rd, wt, exc[, timeout]) ## examine file descriptors
count = custom("poll", chk, &ret[, msecs]) ## synchronous I/O multiplexing
pid = custom("wait", [pid, ]&stt[, opts][, &usg]) ## wait for process

pid = custom("pfe", &in, &out, &err, args) ## pipe/fork/exec
count = custom("pwrite", fd, str) ## write and close
e = custom("pread", pid, out, err) ## read until eof, close and wait for exit status

str = custom("inputname") ## get name of input
path = custom("getcwd") ## get working directory pathname

path = custom("basename", path) ## extract the base portion of a pathname
path = custom("dirname", path) ## extract the directory part of a pathname

pid = custom("getpid") ## get calling process identification
pid = custom("getppid") ## get parent process identification

TYPES
fd int num
pipe list of fd
pid int num
src tgt fd
arg0 str
args list of str
e int num
count int num
rd wt exc list of fd
in out err list of fd
timeout num

arg1 str
...argN str
chk list of list(fd[, arg1[, ...argN]])
ret list of list(fd[, arg1[, ...argN]])
msec int num

stt assoc str => int num
opts list of str
usg assoc str => int num

path str

EXAMPLE
global i, o, e
if(pid = pfe(&i, &o, &e,list("sh","-c","read||:;echo $REPLY"))){
pwrite(i,"test")
pread(pid,o,e)}

LIMITS
calc must be built with ALLOW_CUSTOM= -DCUSTOM
calc must be executed with a -C arg.

LIBRARY
none

SEE ALSO
custom

## Copyright (C) 2024 Viktor Bergquist
##
## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License
## as published by the Free Software Foundation.
##
## Calc is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
## Public License for more details.
##
## A copy of version 2.1 of the GNU Lesser General Public License is
## distributed with calc under the filename COPYING-LGPL. You should have
## received a copy with calc; if not, write to Free Software Foundation, Inc.
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Loading