2011-08-24 00:58:18 -04:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2011-08-24 23:35:02 -04:00
|
|
|
# Processes input text and outputs commands to be executed as a result
|
2011-08-24 00:58:18 -04:00
|
|
|
#
|
2011-08-24 23:35:02 -04:00
|
|
|
# This script does not actually execute the commands or further validate them.
|
2011-08-24 17:44:00 -04:00
|
|
|
#
|
2011-08-24 00:58:18 -04:00
|
|
|
# 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
|
2011-08-25 19:45:18 -04:00
|
|
|
scene=$( cat - )
|
2011-08-24 00:58:18 -04:00
|
|
|
|
|
|
|
# scene data should be provided via stdin
|
2011-08-25 19:45:18 -04:00
|
|
|
[ "$scene" ] || {
|
2011-08-24 00:58:18 -04:00
|
|
|
echo "No scene data provided" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# our processing method depends heavily on the word count
|
|
|
|
count=$( grep -o '\w\+' <<< "$words" | wc -l )
|
|
|
|
|
2011-08-25 19:45:18 -04:00
|
|
|
# the action is every word except the last, and the object being acted upon is
|
|
|
|
# the final word
|
|
|
|
action=$( sed 's/^\(.\+\) .\+$/\1/' <<< "$words" )
|
|
|
|
object=$( grep -o '[^ ]\+$' <<< "$words" )
|
2011-08-25 18:52:31 -04:00
|
|
|
|
|
|
|
# if only one word was given, then we need to ask what to do
|
|
|
|
if [ "$count" -eq 1 ]; then
|
|
|
|
# is it a known object?
|
2011-08-25 19:45:18 -04:00
|
|
|
grep -q "^OBJECT $action" <<< "$scene" && {
|
2011-08-25 18:52:31 -04:00
|
|
|
echo "MSG What about $action?"
|
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
|
|
|
# otherwise, we have no idea what the user is babbling about
|
|
|
|
echo "MSG Please elaborate."
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2011-08-24 23:35:02 -04:00
|
|
|
# check for am action on a known object and output the associated commands, left
|
|
|
|
# trimmed
|
|
|
|
awk "
|
2011-08-25 19:45:18 -04:00
|
|
|
/^OBJECT $object$/ {
|
2011-08-24 22:59:08 -04:00
|
|
|
found = 1
|
2011-08-25 18:39:48 -04:00
|
|
|
getline
|
2011-08-24 22:59:08 -04:00
|
|
|
}
|
2011-08-25 18:39:48 -04:00
|
|
|
found && /^[ \\t]*$|^[A-Z]/ {
|
|
|
|
found = 0
|
|
|
|
}
|
|
|
|
|
2011-08-24 22:59:08 -04:00
|
|
|
/^( |\\t)ACTION $action$/ {
|
|
|
|
if ( found ) {
|
|
|
|
getline
|
|
|
|
|
|
|
|
while ( !/^( |\\t)[^ \\t]|^$/ ) {
|
|
|
|
print
|
|
|
|
getline
|
|
|
|
}
|
2011-08-24 00:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
2011-08-25 19:45:18 -04:00
|
|
|
" <<< "$scene" \
|
2011-08-24 22:59:08 -04:00
|
|
|
| sed 's/^[ \t]\+//'
|
2011-08-24 00:58:18 -04:00
|
|
|
|