Modified the directory structure. Added makefile, install and uninstall scripts. Removed some files that need to be refactored.

This commit is contained in:
Olivier Poirier 2023-09-25 21:26:37 -07:00
parent 0d25739571
commit 8e0b911e72
19 changed files with 178 additions and 44 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.wip/

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
# posix-dergutils - Utilities For Dragons
DESTDIR=${HOME}
PREFIX=/.local
install:
./install ${DESTDIR} ${PREFIX}
uninstall:
./uninstall ${DESTDIR} ${PREFIX}
.PHONY: install uninstall

19
README.md Normal file
View File

@ -0,0 +1,19 @@
#Commands For Dergs
These POSIX shell scripts are meant to be UNIXy - they do one thing each and do it well, so you can sling them together and perform complex tasks.
Need to check if something is a number? isnum.
Need to take the last argument and pipe it, executing the rest as a command? lastpipe. (I'll be making one that can offset the location by n later.)
Need the former, but for the first argument? firstpipe.
You get the idea!
## What do I need?
There will be descriptions of these functions embedded on their head, including a requirements list for the applications they call.
Later, I'll make a script that can tell if one of these is missing and let you know.
I'd also like to do a --reqs argument that will print these along with an indicator of installation status and a return code.
---
### Why POSIX?
My other project isn't ready yet, and it's good to make POSIX environments friendlier.
The only problem occurs when an external program is not there.

18
install Executable file
View File

@ -0,0 +1,18 @@
#!/bin/sh
# Copies all of the executable files in non-hidden subdirectories into an installation directory and prefix.
# Requires find and sed to work.
if type sed && type find ; then
if [ "$DESTDIR" = "" ] ; then
if [ -d "$1" ] ; then DESTDIR="$1" ; else DESTDIR="$HOME" ; fi
fi
if [ "$PREFIX" = "" ] ; then
if [ "$2" != "" ] && [ -d "$DESTDIR$2" ] ; then PREFIX="$2" ; else PREFIX="/.local" ; fi
fi
for file in $(find ./ -not -path '*/.*' -path './*/*' -type f -perm 755) ; do
cp "$file" "$DESTDIR$PREFIX/bin/$(echo $file | sed 's/.*\///')"
done
else
echo "Please install find and sed." >&2
exit 1
fi

3
primitives/catpid Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
echo $$
cat

41
primitives/fifork Executable file
View File

@ -0,0 +1,41 @@
#!/bin/sh
# This executes a function and sends the result into a FIFO.
# It will create a new FIFO if the first supplied argument does not point to one.
# If no command is specified, it will read from a file containing a command to perform.
# It uses the script 'tmpfifo' to create FIFOs.
# Modify tmpfifo or replace it to change default location and naming convention.
Fifork () {
TMPFIFO="$1"
shift
ARGS="$@"
echo "$DEFAULTCMD" > "$TMPFIFO-command"
chmod +x "$TMPFIFO-command"
if [ "$ARGS" != "" ] ; then
echo "$ARGS" > "$TMPFIFO-command"
fi
"$TMPFIFO-command" >> "$TMPFIFO"
rm "$TMPFIFO"
rm "$TMPFIFO-command"
}
if [ -p "$1" ] ; then
TMPFIFO="$1"
else
FIFONAME="$(echo "$1" | sed 's/.*\///')"
TMPFIFO="$(tmpfifo $FIFONAME)"
fi
FIFONAME="$(echo "$TMPFIFO" | sed 's/.*\///')"
DEFAULTCMD='#!/bin/sh
FIFO=$(tmpfifo '"$FIFONAME-input"'); tail -f $FIFO ; rm $FIFO'
if [ "$2" != "" ] ; then
shift
fi
echo "$TMPFIFO"
Fifork "$TMPFIFO" "$@" &

3
primitives/lastarg Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
for last in "$@" ; do :; done
echo "$last"

49
primitives/lthread Executable file
View File

@ -0,0 +1,49 @@
#!/bin/sh
THREAD_LIMIT="$1"
if ! isnum "$DEFAULT_THREAD_LIMIT" ; then
DEFAULT_THREAD_LIMIT="$(nproc)"
fi
if ! isnum "$LIMIT" ; then
THREAD_LIMIT="$DEFAULT_THREAD_LIMIT"
else
shift
fi
ARGS="$@"
if [ "$ARGS" = "" ] ; then
set -- "cat"
fi
ARGS="$@"
THREAD_FIFO="$(tmpfifo $1)"
PID_FIFO="$(tmpfifo pid)"
Recurse() {
while [ "$?" = "0" ] ; do
pipe "$@" 2>/dev/null
echo "$!" > "$PID_FIFO"
done
}
set --
for thread in $(seq $THREAD_LIMIT) ; do
Recurse "$THREAD_FIFO" "$ARGS" &
echo "$!" > "$PID_FIFO" &
done
lparg cat 2>/dev/null 1>>"$THREAD_FIFO"
PIDS="$(cat $PID_FIFO)"
for pid in "$PIDS" ; do
if isnum "$pid" ; then
echo ps h -q "$pid" ; read thing
ps h -q "$pid" >/dev/null
if [ "$?" = "0" ] ; then kill "$pid" ; fi
fi
done
if [ -e "$THREAD_FIFO" ] ; then rm "$THREAD_FIFO" ; fi
if [ -e "$PID_FIFO" ] ; then rm "$PID_FIFO" ; fi

10
primitives/nolastarg Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
ARGS="$@"
COUNT="$#"
set --
for arg in $ARGS ; do
if [ "$#" -lt "$(( COUNT - 1 ))" ] ; then
set -- $@ $arg
fi
done
echo "$@"

View File

@ -6,10 +6,12 @@
if ( [ "$1" = "-h" ] || [ "$1" = "--help" ] ) && [ "$2" = "" ] ; then
echo "Usage: usage [args to check] <usage string> "
else
CHECK='-h
--help'
if [ "$1" != "" ] ; then
if [ -z $USAGE_LOCK ] ; then
export USAGE_LOCK=1
spipe lseqcmd 2 '-h' '--help' largcmd cat "$@"
echo $CHECK | lseqcmd spipe largcmd cat "$@"
CODE="$?"
unset USAGE_LOCK
exit $CODE

43
spipe
View File

@ -1,43 +0,0 @@
#!/bin/sh
# Sends the last argument to an executable through a pipe, with the other arguments being processed by the executable as usual.
# This makes it possible to send strings to a program (such as sed) instead of piping or reading from a file.
# Requires: <program> <package (Devuan)>
# echo coreutils
IFS='
'
USAGE='Usage: spipe <command> [args...]'
if usage "$1" "$USAGE" ; then exit ; fi
if [ ! -z $(which $1) ] && [ "$2" != "" ] ; then
CMD="$1"
shift
VARS=1
ARGS="$@"
until [ "$2" = "" ] ; do
VARS=$(($VARS+1))
shift
done
set -- $ARGS
COUNT=1
until [ "$COUNT" = "$VARS" ] ; do
COUNT=$(($COUNT+1))
set -- "$@" "$1"
shift
STRING="$1"
done
shift
echo "${STRING}" | "$CMD" "${@}"
exit
else
if [ "$1" = "" ] ; then
echo "No executable name supplied." >&2
echo "$USAGE"
exit 1
else
echo "Couldn't find executable: $1"
exit 127
fi
fi
exit 1

19
uninstall Executable file
View File

@ -0,0 +1,19 @@
#!/bin/sh
# Removes all of the executable filenames found in non-hidden subdirectories from an installation directory and prefix.
# Requires find and sed to work.
if type sed && type find ; then
if [ "$DESTDIR" = "" ] ; then
if [ -d "$1" ] ; then DESTDIR="$1" ; else DESTDIR="$HOME" ; fi
fi
if [ "$PREFIX" = "" ] ; then
if [ "$2" != "" ] && [ -d "$DESTDIR$2" ] ; then PREFIX="$2" ; else PREFIX="/.local" ; fi
fi
for file in $(find ./ -not -path '*/.*' -path './*/*' -type f -perm 755 | sed 's/.*\///') ; do
rm -f "$DESTDIR$PREFIX/bin/$file"
done
else
echo "Please install find and sed." >&2
exit 1
fi