81 lines
2.1 KiB
Plaintext
81 lines
2.1 KiB
Plaintext
|
#!/bin/bash
|
||
|
#
|
||
|
# Processes input text and outputs result
|
||
|
#
|
||
|
# Copyright (C) 2011 Mike Gerwitz
|
||
|
#
|
||
|
# This file is part of gsgp. This program is free software: you can
|
||
|
# redistribute it and/or modify it under the terms of the GNU General Public
|
||
|
# License as published by the Free Software Foundation, either version 3 of
|
||
|
# the License, or (at your option) any later version.
|
||
|
#
|
||
|
# This program 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 General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
##
|
||
|
|
||
|
words="$1"
|
||
|
max_words=2
|
||
|
data=$( cat - )
|
||
|
|
||
|
# scene data should be provided via stdin
|
||
|
[ "$data" ] || {
|
||
|
echo "No scene data provided" >&2
|
||
|
exit 1
|
||
|
}
|
||
|
|
||
|
# our processing method depends heavily on the word count
|
||
|
count=$( grep -o '\w\+' <<< "$words" | wc -l )
|
||
|
|
||
|
# no use in allowing more words than we're able to interpret
|
||
|
if [ $count -gt $max_words ]; then
|
||
|
echo "Too many words" >&2
|
||
|
exit 2
|
||
|
fi
|
||
|
|
||
|
action=$( cut -d' ' -f1 <<< "$words" )
|
||
|
object=$( cut -d' ' -f2 <<< "$words" )
|
||
|
|
||
|
# check for am action on a known object
|
||
|
action_data=$( awk "
|
||
|
/OBJECT $object/,/^$/ {
|
||
|
if ( /^[ \t]*ACTION $action / ) {
|
||
|
print
|
||
|
}
|
||
|
}
|
||
|
" <<< "$data" \
|
||
|
| sed 's/ *ACTION [^ ]\+ \(.*\)$/\1/'
|
||
|
)
|
||
|
|
||
|
# if we were unable to find the associated object/action, then let the user know
|
||
|
# that they've performed an invalid action
|
||
|
if [ ! "$action_data" ]; then
|
||
|
echo "MSG Cannot $words"
|
||
|
exit 0
|
||
|
fi
|
||
|
|
||
|
# what type of command should we execute?
|
||
|
cmd=$( cut -d' ' -f1 <<< "$action_data" )
|
||
|
target=$( cut -s -d' ' -f2 <<< "$action_data" )
|
||
|
|
||
|
case "$cmd" in
|
||
|
GO)
|
||
|
if [ ! "$target" ]; then
|
||
|
echo "ERROR: GO command requires target scene" >&2
|
||
|
exit 4
|
||
|
fi
|
||
|
|
||
|
# the action will be to go to the target scene
|
||
|
echo "GO $target"
|
||
|
;;
|
||
|
*)
|
||
|
echo "ERROR: Unknown command '$cmd' for action: $words" >&2
|
||
|
exit 4
|
||
|
;;
|
||
|
esac
|
||
|
|