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 4 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ cal/test082.cal
charbit.h
chatbit
chk_c
.cache/clangd
conf.h
const_tmp
cscript/4dsphere
Expand All @@ -53,6 +54,8 @@ cscript/simple
cscript/square
custom/.all
custom/libcustcalc*
custom/.gitignore
compile_commands.json
debug.out
.dynamic
endian
Expand Down
52 changes: 52 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
vike2000 marked this conversation as resolved.
Show resolved Hide resolved
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "lldb launch",
"cwd": "${workspaceFolder}",
"program": "./calc",
"env": {
"DYLD_LIBRARY_PATH": ".",
},
"preRunCommands": [
"command script import .vscode/launch/_env.py .vscode/launch/env.py",
],
"args": ["-Cpf", "custom/pfe-launch.cal"],
},
{
"preLaunchTask": "compiledb make clobber all #DEBUG=… w/ macports readline",
"type": "lldb",
"request": "launch",
"name": "lldb launch + compiledb make clobber all #DEBUG=… w/ macports readline",
"cwd": "${workspaceFolder}",
"program": "./calc",
"env": {
"DYLD_LIBRARY_PATH": ".",
},
"preRunCommands": [
"command script import .vscode/launch/_env.py .vscode/launch/env.py",
],
"args": ["-Cpf", "custom/pfe-launch.cal"],
},
{
"preLaunchTask": "compiledb make clobber all #… w/ macports readline",
"type": "lldb",
"request": "launch",
"name": "lldb launch + compiledb make clobber all #… w/ macports readline",
"cwd": "${workspaceFolder}",
"program": "./calc",
"env": {
"DYLD_LIBRARY_PATH": ".",
},
"preRunCommands": [
"command script import .vscode/launch/_env.py .vscode/launch/env.py",
],
"args": ["-Cpf", "custom/pfe-launch.cal"],
},
]
}
8 changes: 8 additions & 0 deletions .vscode/launch/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

vike2000 marked this conversation as resolved.
Show resolved Hide resolved
# customized dotenv format allowing for any space in key to align keys and values but retain any space in values

# general
verbose =1
# specific
wait =1
leaks =0
3 changes: 3 additions & 0 deletions .vscode/launch/_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

os.environ['ENV_PY'] = '.vscode/launch/.env'
25 changes: 25 additions & 0 deletions .vscode/launch/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
import os
import re
import lldb
#from pprint import pprint

class EnvPyFormatException(Exception):
pass

env_array = []

with open(os.path.join(os.environ['ENV_PY'])) as file:
for line in file:
if not re.match("^\s*(#.*)?$" , line):
if m := re.match("^\s*(?P<key>\S+)\s*=(?P<value>.*)$" , line):
env_array.append(m.group('key')+"="+m.group('value'))
else:
raise EnvPyFormatException("Line doesn't match ` `, ` # comment` or ` key =value` where space is optional:"
, line)

target = lldb.debugger.GetSelectedTarget()

launch_info = target.GetLaunchInfo()
launch_info.SetEnvironmentEntries(env_array, True)
target.SetLaunchInfo(launch_info)
19 changes: 19 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "compiledb make clobber all #… w/ macports readline",
"type": "shell",
"command": "~/Library/Python/3.8/bin/compiledb make clobber all READLINE_INCLUDE=' -I/opt/local/include/readline' READLINE_LIB=' -L/opt/local/lib -l''readline'",
"problemMatcher": ["$gcc"]
},
{
"label": "compiledb make clobber all #DEBUG=… w/ macports readline",
"type": "shell",
"command": "~/Library/Python/3.8/bin/compiledb make clobber all DEBUG='-O0 -g' READLINE_INCLUDE=' -I/opt/local/include/readline' READLINE_LIB=' -L/opt/local/lib -l''readline'",
"problemMatcher": ["$gcc"]
},
]
}
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
2 changes: 2 additions & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-DCALC_SRC
-DCUSTOM
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
91 changes: 91 additions & 0 deletions custom/pfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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.
##
## Under source code control: 1997/03/09 20:28:01
## File existed as early as: 1997
##
## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
11 changes: 11 additions & 0 deletions custom/pfe+conf.cal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

; _ = config("resource_debug" ,0)
## ; _ = config("trace" ,2)
; _ = config("tilde" ,0)
; _ = config("display" ,8)

; define inputname ( ) = custom("inputname" )
; define dirname (path ) = custom("dirname",path )
; p = strcat(dirname(inputname()),"/pfe.cal" )
;read $p

Loading