#!/usr/bin/env bash
-
read_header()
{
local key value i
---)
break
;;
- OF:)
- output_file="$value"
- array_name=${value%command_list}cmds
+ BN:)
+ base_name="$value"
;;
SF:)
source_files="$value"
;;
- AT:)
- array_type="$value"
- ;;
- SI:)
- for i in $value; do
- system_includes="$system_includes
-#include <$i.h>"
- done
- ;;
- IN:)
- for i in $value; do
- includes="$includes
-#include \"$i.h\""
- done
- includes="$includes
-#include \"$output_file.h\""
- ;;
SN:)
section_name="$value"
;;
template_members="$value"
esac
done
- if test -z "$output_file"; then
- echo "header error" 1&>2
- exit 1
- fi
}
read_one_command()
template=0
template_name=""
template_prototype=""
- mkdir -p man/man1
while read key value; do
case "$key" in
---)
done
}
-dump_proto()
+make_proto()
{
- local source_file match=""
-
- echo '/**'
- echo " * $desc_txt"
- echo ' *'
- echo " * Usage: $usage_txt"
- echo ' * '
- echo "$help_txt" | sed -e 's/^/ * /g'
- echo ' */'
+ local regex='\(__noreturn \)*\(static \)*int com_'
+ local source_file match="" all_commands CR='
+'
if test -n "$prototype"; then
- echo "$prototype"
- echo
+ result="$prototype$CR"
return
fi
+ all_commands="$(cat $source_files | grep "$regex")"
+ result=
for source_file in $source_files; do
- match=$(grep "^\(__noreturn \)*int com_$name_txt(" $source_file | head -n 1 | sed -e 's/$/;/1')
+ match=$(grep "$regex$name_txt(" <<< "$all_commands" | head -n 1 | sed -e 's/$/;/1')
if test -n "$match"; then
- echo "$match"
+ result="$result$match$CR"
break
fi
done
- echo
}
-dump_array_member()
+make_array_member()
{
- local TAB=' '
- echo '{'
- echo ".name = \"$name_txt\","
- echo ".handler = com_$name_txt,"
+ local TAB=' ' CR='
+'
+ local tmp
+
+ result="{.name = \"$name_txt\", .handler = com_$name_txt, "
if test -n "$perms_txt"; then
- echo ".perms = $perms_txt,"
+ result="$result .perms = $perms_txt,"
fi
- echo ".description = \"$desc_txt\","
- echo ".usage = \"$usage_txt\","
- echo ".help = "
- printf "%s\n" "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g' \
- -e "s/$TAB/\\\t/g"
- echo '},'
+ result="$result.description = \"$desc_txt\", .usage = \"$usage_txt\", \\$CR .help = "
+ tmp="$(printf "%s\n" "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g' \
+ -e "s/$TAB/\\\t/g" -e's/$/\\/g')"
+ result="$result$tmp$CR}, \\$CR"
}
-dump_completion()
+make_completion()
{
- echo " {.name = \"$name_txt\", .completer = ${name_txt}_completer}, \\"
+ local CR='
+'
+ result=" {.name = \"$name_txt\", .completer = ${name_txt}_completer}, \\$CR"
}
template_loop()
{
+ local loop_result=
+
local t_name="$name_txt"
local t_perms="$perms_txt"
local t_desc="$desc_txt"
local t_source_files="$source_files"
local member
for member in $template_members; do
- local sed_cmd="sed -e s/@member@/$member/g"
- #echo "sed_cmd: $sed_cmd"
- name_txt=$(echo "$t_name" | $sed_cmd)
- #name_txt="$tname"
- perms_txt=$(echo "$t_perms" | $sed_cmd)
- desc_txt=$(echo "$t_desc" | $sed_cmd)
- usage_txt=$(echo "$t_usage" | $sed_cmd)
- help_txt=$(printf "%s\n" "$t_help" | $sed_cmd)
- prototype=$(echo "$template_prototype" | $sed_cmd)
+ name_txt="${t_name//@member@/$member}"
+ perms_txt="${t_perms//@member@/$member}"
+ desc_txt="${t_desc//@member@/$member}"
+ usage_txt="${t_usage//@member@/$member}"
+ help_txt="${t_help//@member@/$member}"
+ prototype="${template_prototype//@member@/$member}"
+ result=
$1
+ loop_result="$loop_result$result"
done
-
-}
-
-com_c_file()
-{
- echo "$system_includes"
- echo "$includes"
- echo "struct $array_type $array_name[] = {"
- while : ; do
- read_one_command
- if test $ret -lt 0; then
- exit 1
- fi
- if test $ret -eq 0; then
- break
- fi
- if test $template -eq 0; then
- dump_array_member
- continue
- fi
- template_loop dump_array_member
- done
- echo '{.name = NULL}};'
+ result="$loop_result"
+ # reset global variables
+ name_txt="$t_name"
+ perms_txt="$t_perms"
+ desc_txt="$t_desc"
+ usage_txt="$t_usage"
+ help_txt="$t_help"
+ source_files="$t_source_files"
}
com_header()
{
- echo "extern struct $array_type $array_name[];"
+ local array_members CR='
+'
+
while : ; do
read_one_command
if test $ret -lt 0; then
break
fi
if test $template -eq 0; then
- dump_proto
+ make_proto
+ printf "%s" "$result"
+ make_array_member
+ array_members="$array_members$result"
continue
fi
- template_loop dump_proto
+ template_loop make_proto
+ printf "%s" "$result"
+ template_loop make_array_member
+ array_members="$array_members$result"
done
+ array_members="$array_members{.name = NULL} \\$CR"
+ echo "#define DEFINE_$(tr 'a-z' 'A-Z' <<< "$base_name")_CMD_ARRAY $array_members"
}
com_completion()
break
fi
if test $template -eq 0; then
- dump_completion
+ make_completion
+ printf "%s" "$result"
continue
fi
- template_loop dump_completion
+ template_loop make_completion
+ printf "%s" "$result"
done
echo
}
arg="$1"
shift
case "$arg" in
- "c")
- com_c_file
- ;;
"h")
com_header
;;