author | František Kučera <franta-hg@frantovo.cz> |
Sat, 11 Jan 2020 19:15:58 +0100 | |
branch | v_0 |
changeset 33 | f9cada1d46a4 |
child 38 | 4191af89968a |
permissions | -rwxr-xr-x |
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 |
currentInputAttributeIndex=0; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
currentOutputAttributeIndex=0; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
currentRelation=""; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
currentFile=""; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
} |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
|
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
processMessage_VERSION_SUPPORTED() { |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
versionsSupported+=("$1"); |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
} |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
|
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
return; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
fi |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
done |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
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
|
80 |
} |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
|
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
processMessage_RELATION_START() { |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
currentRelation="$1"; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
return; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
} |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
|
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
87 |
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
|
88 |
inputAttributeNames+=("$1"); |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
inputAttributeTypes+=("$2"); |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
} |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
|
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
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
|
93 |
outputAttributeAliases+=("$1"); |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
} |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
|
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
processMessage_OPTION() { |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
optionNames+=("$1"); |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
optionValues+=("$2"); |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
} |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
|
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
processMessage_INPUT_ATTRIBUTE() { |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
local index="$1"; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
local value="$2"; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
local isNull="$3"; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
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
|
106 |
currentFile="$value"; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
fi |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
} |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
|
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
110 |
processMessage_RELATION_END() { |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
111 |
# 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
|
112 |
# 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
|
113 |
# 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
|
114 |
# 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
|
115 |
return; |
f9cada1d46a4
streamlet examples: common functions + inode, lines_count, mime_type
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
116 |
} |