]> git.corax.cc Git - toolbox/log
toolbox
4 years agodebian: Increase package version to 0.3-1
Matthias Kruk [Sat, 17 Apr 2021 02:07:57 +0000 (11:07 +0900)]
debian: Increase package version to 0.3-1

This commit increases the package version to 0.3-1 and removes the
description from the "Source" portion of the debian/control file so
the dpkg-buildpackage version used on deb.m10k.eu won't complain.

4 years agoinclude/queue: Include dependency "log"
Matthias Kruk [Sat, 17 Apr 2021 01:16:46 +0000 (10:16 +0900)]
include/queue: Include dependency "log"

The queue module depends on the log module, however it doesn't include
it during initialization. This commit makes sure the log module is
loaded along with the queue module.

4 years agoinclude/sem: Work around busy-waiting in sem_wait()
Matthias Kruk [Sat, 17 Apr 2021 00:28:00 +0000 (09:28 +0900)]
include/sem: Work around busy-waiting in sem_wait()

The sem_wait() function is implemented using busy-waiting, which is not
good for the environment. This commit adds a call to inotifywait as a
temporary workaround to avoid the busy-waiting.

4 years agoinclude/mutex: Use inotifywait to improve performance
Matthias Kruk [Thu, 15 Apr 2021 23:46:33 +0000 (08:46 +0900)]
include/mutex: Use inotifywait to improve performance

The current mutex implementation uses sleep to periodically check if
the lock can be acquired. This causes a lot of latency for short waits
and a lot of unnecessary sleep/wakeup cycles for long waits.
This commit changes the mutex implementation to use inotifywait
instead of sleep, increasing mutex performance for both short and long
waits.

4 years agoinclude/inst: Make inst_stop emit a meaningful message upon error
Matthias Kruk [Thu, 15 Apr 2021 23:27:31 +0000 (08:27 +0900)]
include/inst: Make inst_stop emit a meaningful message upon error

Instead of emitting a user-understandable error message, the inst_stop()
function merely lets an error message from the sem module slip through.
This commit changes the function to emit an error message that tells the
caller why exactly the function failed.

4 years agoinclude/inst: Add inst module for managing running instances of scripts
Matthias Kruk [Thu, 15 Apr 2021 13:37:00 +0000 (22:37 +0900)]
include/inst: Add inst module for managing running instances of scripts

This commit adds the inst module which provides functions for managing
instances of scripts:

 - The inst_start() function forks a function to the background
 - The inst_stop() function asks an instance to stop
 - The inst_list() function lists running instances
 - The inst_running() function tells an instance if it should stop

Internally, the module makes use of semaphores to list instances and
determine their state. Semaphores are kept in per-script directories
within $TOOLBOX_HOME/inst.

4 years agoinclude/sem: Add sem_peek() function
Matthias Kruk [Thu, 15 Apr 2021 13:35:00 +0000 (22:35 +0900)]
include/sem: Add sem_peek() function

This commit adds the sem_peek() function, which allows the caller to
determine the value of a semaphore without modifying it.

4 years agoinclude/opt: Remember arguments passed to opt_parse()
Matthias Kruk [Thu, 15 Apr 2021 13:28:08 +0000 (22:28 +0900)]
include/opt: Remember arguments passed to opt_parse()

To allow the user to distinguish running instances of a script, it is
necessary to present the arguments that an instance was started with.
The most logical place to remember such information is inside the opt
module. Therefore, this commit adds the opt_get_argv() function, which
allows the caller to retrieve the arguments that were passed to the
opt_parse() function.

4 years agoinclude/opt: Make --verbose and --shush default arguments
Matthias Kruk [Thu, 15 Apr 2021 12:05:35 +0000 (21:05 +0900)]
include/opt: Make --verbose and --shush default arguments

The -v/--verbose and -w/--shush options are frequently implemented in
scripts that use toolbox. To reduce code duplication and to make all
scripts follow the same argument naming conventions, this commit makes
-v/--verbose and -w/--shush standard arguments of the opt module.

4 years agoinclude/log: Add functions to increase and decrease the verbosity
Matthias Kruk [Thu, 15 Apr 2021 11:59:43 +0000 (20:59 +0900)]
include/log: Add functions to increase and decrease the verbosity

I frequently find myself implementing the same functions to increase
and decrease the verbosity of my scripts. This commit adds these
functions to the log module.

4 years agoinclude/queue: Add functions for file queues and duplicate-free queues
Matthias Kruk [Wed, 14 Apr 2021 23:52:21 +0000 (08:52 +0900)]
include/queue: Add functions for file queues and duplicate-free queues

For certain automation tasks, it would be helpful if filesystem objects
could be passed through queues. This commit adds such functionality to
the queue module.

For passing files, these functions can be used:
 - queue_put_file()
 - queue_get_file()

For duplicate-free transient data:
 - queue_put_unique()
 - queue_get()

For other transient data:
 - queue_put()
 - queue_get()

There are two things worth noting:
 1. The queues are line-based (transient data may not contain newlines)
 2. Queues cannot be used for transient data and files at the same time

4 years agoinclude/queue: Add queue module
Matthias Kruk [Tue, 13 Apr 2021 12:29:28 +0000 (21:29 +0900)]
include/queue: Add queue module

The queue module implements a mechanism to distribute data produced by
multiple producers to multiple consumers in a synchronized fashion.

4 years agoinclude/sem: Allow semaphores to be created in arbitrary locations
Matthias Kruk [Mon, 12 Apr 2021 23:45:07 +0000 (08:45 +0900)]
include/sem: Allow semaphores to be created in arbitrary locations

The current implementation cannot be used to created semaphores outside
of $TOOLBOX_HOME/sem. This makes it impossible to share semaphores with
scripts that are executed as a different user.
This commit changes the semaphore implementation so that only semaphore
names that don't contain slashes will be created in $TOOLBOX_HOME/sem.

4 years agoinclude/conf: Add simple configuration module
Matthias Kruk [Mon, 12 Apr 2021 10:57:33 +0000 (19:57 +0900)]
include/conf: Add simple configuration module

The "conf" module provides a simple way for scripts to store and retrieve
settings. Config files are stored per-script in $TOOLBOX_HOME/conf.

4 years agoinclude/ssh: Simplify SSH module API
Matthias Kruk [Fri, 9 Apr 2021 23:34:26 +0000 (08:34 +0900)]
include/ssh: Simplify SSH module API

The ssh_tunnel_close() and ssh_proxy_close() functions do essentially
the same thing, so they should be merged into one. Further, both of
the functions require the user to remember multiple values from the
ssh_*_open() call in order to close the connection. This seems
unnecessarily complicated.
This commit adds the ssh_close() function as a replacement for
ssh_tunnel_close() and ssh_proxy_close(). Further, the ssh_*_open()
functions now return a handle that can be passed to ssh_close() to
close the respective connection.

4 years agoinclude/log: Remove timestamp and pid from log path
Matthias Kruk [Tue, 6 Apr 2021 23:44:48 +0000 (08:44 +0900)]
include/log: Remove timestamp and pid from log path

Having the timestamp and pid in the log path causes a large number of
log files to be generated, which makes it difficult to find the correct
one.
This commit removes the timestamp and pid from the log path, causing
all processes with the same name to log to the same file. To distinguish
individual processes, the log_write() function will also log the pid of
the calling process.

4 years agoinclude/log: Use $0 to determine the script name
Matthias Kruk [Tue, 6 Apr 2021 23:39:47 +0000 (08:39 +0900)]
include/log: Use $0 to determine the script name

BASH_ARGV0 is not supported on older bash versions. This commit changes
the log module to use $0 to determine the script name instead.

4 years agodebian: Change architecture to "all" and fix warnings during build
Matthias Kruk [Sat, 3 Apr 2021 23:32:43 +0000 (08:32 +0900)]
debian: Change architecture to "all" and fix warnings during build

The architecture in the Debian control file is set to "any", causing
architecture-specific packages to be built. This commit changes the
architecture to "all", causing the resulting packages to be usable on
any architecture.
This commit further fixes other warnings emitted by dpkg-buildpackage
with regard to the changelog and fields in the control file.

4 years agodebian: Increase version to 0.2-1
Matthias Kruk [Sat, 3 Apr 2021 22:51:46 +0000 (07:51 +0900)]
debian: Increase version to 0.2-1

The Debian package needs to be updated to enable rolling out the code
to some machines. This commit increases the Debian package version to
0.2-1.

4 years agoMerge branch 'user-includes'
Matthias Kruk [Sat, 3 Apr 2021 22:51:10 +0000 (07:51 +0900)]
Merge branch 'user-includes'

4 years agosem,acpi/*: Export global variables
Matthias Kruk [Sat, 3 Apr 2021 22:35:14 +0000 (07:35 +0900)]
sem,acpi/*: Export global variables

Global variables declared in the sem and acpi/* modules are not exported,
causing them to become invisible in child processes. This effectively makes
it impossible to use these modules in scripts that fork themselves to the
background.
This commit changes the declarations so that the variables are exported.

4 years agotoolbox: Allow modules to be included from TOOLBOX_HOME user-includes
Matthias Kruk [Sat, 3 Apr 2021 05:42:42 +0000 (14:42 +0900)]
toolbox: Allow modules to be included from TOOLBOX_HOME

The current implementation requires that modules be located in the
global module search path, TOOLBOX_PATH/include. This commit modifies
the implementation so that modules may be included from the user's
TOOLBOX_HOME in addition to the global search path:
When include() is invoked, it will attempt to include the module from
the TOOLBOX_HOME/include first, and only if it didn't succeed, it will
attempt to load the module from TOOLBOX_PATH/include.

4 years agotoolbox: Fix target of toolbox.sh symlink
Matthias Kruk [Sat, 3 Apr 2021 04:40:08 +0000 (13:40 +0900)]
toolbox: Fix target of toolbox.sh symlink

The symlink to toolbox.sh points to the absolute path of the file in the
Debian buildroot, which is an invalid location in an installed system.
This commit fixes the symlink to point to the location of the installed
file.

4 years agotoolbox: Change the default prefix to appease Debian package builds
Matthias Kruk [Sat, 3 Apr 2021 04:21:41 +0000 (13:21 +0900)]
toolbox: Change the default prefix to appease Debian package builds

Debian package builds are failing because the installation prefix defaults
to /usr/local instead of /usr. This changes the default prefix to /usr, to
make Debian package builds happy.

4 years agoMerge branch 'debian'
Matthias Kruk [Sat, 3 Apr 2021 04:20:28 +0000 (13:20 +0900)]
Merge branch 'debian'

4 years agonet/iface: Print each AP only once
Matthias Kruk [Sat, 3 Apr 2021 04:15:00 +0000 (13:15 +0900)]
net/iface: Print each AP only once

A bug in _net_iface_parse_iwlist() caused found access points to be printed
multiple times. This commit fixes the bug, so that each access point is
printed only once.

4 years agoinclude/is: Add functions for determining the content of strings
Matthias Kruk [Sat, 3 Apr 2021 02:34:51 +0000 (11:34 +0900)]
include/is: Add functions for determining the content of strings

This commit adds ctype.h-style functions for determining what kind of
data is stored in strings.

4 years agoinclude/xrandr: Add functions for querying the monitor state from xrandr
Matthias Kruk [Sat, 3 Apr 2021 02:33:49 +0000 (11:33 +0900)]
include/xrandr: Add functions for querying the monitor state from xrandr

This adds convenience functions for getting the current resolution and
available resolutions for a monitor.

4 years agoinclude/clip: Add functions for clipboard manipulation
Matthias Kruk [Sat, 3 Apr 2021 02:26:28 +0000 (11:26 +0900)]
include/clip: Add functions for clipboard manipulation

This commit adds some convenience functions for reading from, writing to,
and swapping X clipboards.

4 years agoinclude/net: Create net module-group and add iface module to it
Matthias Kruk [Sat, 3 Apr 2021 01:25:20 +0000 (10:25 +0900)]
include/net: Create net module-group and add iface module to it

Network-related modules should be grouped. This commit adds the "net"
module-group and moves the iface module to this group.

4 years agoinclude/opt: Add parser for command line parameters
Matthias Kruk [Sun, 28 Mar 2021 02:11:59 +0000 (11:11 +0900)]
include/opt: Add parser for command line parameters

Parsing of command line parameters and printing the help text adds
a significant amount of boilerplate code to any shell script (when
done thoroughly). This commit adds the opt module, which will parse
short and long parameters, execute callbacks, and print the help
text if either "-h" or "--help" were passed.

4 years agoinclude/array: Add functions for printing and sorting arrays
Matthias Kruk [Sun, 28 Mar 2021 01:38:52 +0000 (10:38 +0900)]
include/array: Add functions for printing and sorting arrays

Printing and sorting arrays in-line tends to be rather awkward.
This commit adds the array_to_lines() and array_sort() functions
that take over this task.

4 years agoinclude/iface: Add function for scanning for wireless network
Matthias Kruk [Sat, 27 Mar 2021 03:10:05 +0000 (12:10 +0900)]
include/iface: Add function for scanning for wireless network

The output from `iwlist scan' is rather hard to look at and bothersome
to use in scripts. This commit adds a function that parses the output
into a more machine-friendly format.

4 years agodebian: Add files for debian package creation
Matthias Kruk [Wed, 24 Mar 2021 23:33:33 +0000 (08:33 +0900)]
debian: Add files for debian package creation

This commit adds the files that are necessary to create debian packages
for toolbox.

4 years agotoolbox: Add Makefile for easy installation
Matthias Kruk [Wed, 24 Mar 2021 23:30:38 +0000 (08:30 +0900)]
toolbox: Add Makefile for easy installation

This commit adds a Makefile that can be used to easily install and
uninstall the toolbox.

4 years agoinclude/gitlab: Add functions to call Gitlab API from the shell
Matthias Kruk [Wed, 24 Mar 2021 23:22:35 +0000 (08:22 +0900)]
include/gitlab: Add functions to call Gitlab API from the shell

Gitlab can be integrated with other software through Gitlab's REST
API. This commit adds functions that allow using various Gitlab API
functions from the shell, such as forking repositories, managing
branches, and creating merge requests.

4 years agoinclude/iruca: Add functions to get and set somebody's iruca status
Matthias Kruk [Wed, 24 Mar 2021 23:17:07 +0000 (08:17 +0900)]
include/iruca: Add functions to get and set somebody's iruca status

Iruca provides a REST-ful API that can be used more conveniently than
the website. This commit adds functions for setting and getting an iruca
user's status and status message from the commandline.

4 years agoinclude/ssh: Add functions for handling SSH tunnels and proxies
Matthias Kruk [Wed, 24 Mar 2021 22:26:46 +0000 (07:26 +0900)]
include/ssh: Add functions for handling SSH tunnels and proxies

SSH provides a clean way to open and close tunnels and proxies on demand
through control sockets, however keeping track of the control sockets can
be somewhat bothersome. This commit adds functions that provide an easy
way to open and close tunnels and proxies without the caller having to
bother with control sockets.

4 years agoinclude/array: Add function for checking if an element is in an array
Matthias Kruk [Wed, 24 Mar 2021 22:24:30 +0000 (07:24 +0900)]
include/array: Add function for checking if an element is in an array

Bash doesn't provide a simple way to check if an element is in an array.
This commit adds the array_contains convenience function to solve that
problem.

4 years agoinclude/json: Add functions for creating json objects and arrays
Matthias Kruk [Wed, 24 Mar 2021 22:20:36 +0000 (07:20 +0900)]
include/json: Add functions for creating json objects and arrays

Especially when interacting with REST-ful APIs via curl, it can be handy
to have functions to quickly generate JSON objects without having to
invoke jq. This commit adds functions for generating simple JSON objects
and arrays.

4 years agoinclude/log: Log messages to the logfile AND stderr
Matthias Kruk [Tue, 23 Mar 2021 23:56:27 +0000 (08:56 +0900)]
include/log: Log messages to the logfile AND stderr

The log module only writes messages to the log file, which is not
enough for scripts that are running in the foreground. This commit
changes the log_write() function to log messages to the logfile as
well as to stderr.

4 years agoinclude/iface: Add module for querying network interfaces
Matthias Kruk [Mon, 22 Mar 2021 00:48:39 +0000 (09:48 +0900)]
include/iface: Add module for querying network interfaces

This commit adds a module that allows the user to query the state of
wired and wireless network interfaces.

4 years agoinclude/acpi: Add modules for querying battery and PSU state
Matthias Kruk [Mon, 22 Mar 2021 00:46:46 +0000 (09:46 +0900)]
include/acpi: Add modules for querying battery and PSU state

This commit adds the acpi/battery and acpi/ac module which can be used
to query the state of batteries and power supply units in the system
through the kernel's sysfs ACPI interface.

4 years agoinclude/sem: Fix names of logging functions
Matthias Kruk [Mon, 22 Mar 2021 00:43:39 +0000 (09:43 +0900)]
include/sem: Fix names of logging functions

The functions that the sem module was using for logging do not exist in
the latest log module. This commit changes the sem module to use the
right functions.

4 years agoinclude/log: Add logging facilities
Matthias Kruk [Mon, 22 Mar 2021 00:36:25 +0000 (09:36 +0900)]
include/log: Add logging facilities

This commit adds the log module which can be adopted by scripts to log
messages to log files that are stored in TOOLBOX_HOME. The module also
provides convenience functions for highlighting output and printing
stack traces.

4 years agoinclude/sem: Add POSIX-like semaphore implementation
Matthias Kruk [Sat, 20 Mar 2021 23:55:16 +0000 (08:55 +0900)]
include/sem: Add POSIX-like semaphore implementation

In certain situations, mutex are not enough for synchronization. This
commit adds a POSIX-like semaphore implementation that can be used to
implement things like queues or singleton scripts.

4 years agoinclude/mutex: Add symlink-based mutex implementation
Matthias Kruk [Sat, 20 Mar 2021 23:49:59 +0000 (08:49 +0900)]
include/mutex: Add symlink-based mutex implementation

Scripts that access shared resources need a means to synchronize accesses
to those resources. This commit adds a symlink-based mutex implementation
that can be used for this purpose.

4 years agotoolbox: Implement toolbox initialization and module loading
Matthias Kruk [Sat, 20 Mar 2021 23:23:32 +0000 (08:23 +0900)]
toolbox: Implement toolbox initialization and module loading

When writing shell scripts, I often find myself copying or rewriting
functions because bash does not provide a mechanism to easily reuse
code. This commit adds a mechanism for loadable bash "modules".