streamlet-examples/streamlet-common.sh
author František Kučera <franta-hg@frantovo.cz>
Fri, 13 May 2022 21:35:30 +0200
branchv_0
changeset 96 c34106244a54
parent 38 4191af89968a
permissions -rw-r--r--
portable order of (i++) parameters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/bin/bash
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
# Relational pipes
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
# This program is free software: you can redistribute it and/or modify
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
# it under the terms of the GNU General Public License as published by
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
# the Free Software Foundation, version 3 of the License.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
# This program is distributed in the hope that it will be useful,
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
# GNU General Public License for more details.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
#
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
# You should have received a copy of the GNU General Public License
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
# Unlike the protocol and the message format,
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
# these helper functions are not part of the public API.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
# Thus when writing custom streamlets, it is better to copy this file
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
# and review its changes while upgrading to new upstream version.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
send() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
	local msgVar="EXEC_MSG_$1";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
	printf '%s\0' "${!msgVar}"
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
	shift;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	printf '%s\0' "$#"
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	for param in "$@"; do printf '%s\0' "$param"; done 
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
read_nullbyte() { local IFS=; for v in "$@"; do export "$v"; read -r -d '' "$v"; done }
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
processMessages() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	while read_nullbyte code length; do
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		local msgVar="EXEC_MSG_$code";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		local functionName="processMessage_${!msgVar}";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
		local parameters=();
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		for (( i=0; i<$length; i++)); do
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
			read_nullbyte parameter;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
			parameters+=("$parameter")
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		done
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		type "$functionName" &>/dev/null || functionName="processUnsupportedMessage";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		"$functionName" "${parameters[@]}";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	done
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
processUnsupportedMessage() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	echo "Unsupported message: $*" >&2
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	# TODO: send error, no debug
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	# send STREAMLET_ERROR "UNSUPPORTED_MESSAGE"
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
initialize() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	versionsSupported=();
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	optionNames=();
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	optionValues=();
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	inputAttributeNames=();
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	inputAttributeTypes=();
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	outputAttributeAliases=();
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	currentRelation="";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	currentFile="";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
processMessage_VERSION_SUPPORTED() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	versionsSupported+=("$1");
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
processMessage_WAITING_FOR_VERSION() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	for v in "${versionsSupported[@]}"; do
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
		if [[ "x$v" == "x1" ]]; then
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
			send VERSION_ACCEPTED 1;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
			return;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		fi
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	done
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	send STREAMLET_ERROR "INCOMPATIBLE_VERSION" "Only version 1 is supported by this streamlet."
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
processMessage_RELATION_START() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	currentRelation="$1";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	return;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
processMessage_INPUT_ATTRIBUTE_METADATA() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	inputAttributeNames+=("$1");
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	inputAttributeTypes+=("$2");
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
processMessage_OUTPUT_ATTRIBUTE_ALIAS() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	outputAttributeAliases+=("$1");
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
processMessage_OPTION() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	optionNames+=("$1");
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	optionValues+=("$2");
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
processMessage_INPUT_ATTRIBUTE() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	local index="$1";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
	local value="$2";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	local isNull="$3";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
	if [[ "_${inputAttributeNames[$index]}" == "_path" ]]; then
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
		currentFile="$value";
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	fi
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
}
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
processMessage_RELATION_END() {
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	# Now we should stop reading messages,
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
	# but the executor also closes our STDIN,
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
	# so the loop in processMessages() terminates.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	# Override this function to do some clean-up.
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
	return;
f9cada1d46a4 streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
}