1#!/bin/sh
23
die () {
4echo "$@" >&2
5exit 1
6}
78
command_list () {
9grep -v '^#' "$1"
10}
1112
get_categories () {
13tr ' ' '\n'|
14grep -v '^$' |
15sort |
16uniq
17}
1819
category_list () {
20command_list "$1" |
21cut -c 40- |
22get_categories
23}
2425
get_synopsis () {
26sed -n '
27/^NAME/,/'"$1"'/H
28${
29x
30s/.*'"$1"' - \(.*\)/N_("\1")/
31p
32}' "Documentation/$1.txt"
33}
3435
define_categories () {
36echo
37echo "/* Command categories */"
38bit=0
39category_list "$1" |
40while read cat
41do
42echo "#define CAT_$cat (1UL << $bit)"
43bit=$(($bit+1))
44done
45test "$bit" -gt 32 && die "Urgh.. too many categories?"
46}
4748
print_command_list () {
49echo "static struct cmdname_help command_list[] = {"
5051
command_list "$1" |
52while read cmd rest
53do
54printf " { \"$cmd\", $(get_synopsis $cmd), 0"
55for cat in $(echo "$rest" | get_categories)
56do
57printf " | CAT_$cat"
58done
59echo " },"
60done
61echo "};"
62}
6364
echo "/* Automatically generated by generate-cmdlist.sh */
65struct cmdname_help {
66const char *name;
67const char *help;
68uint32_t category;
69};
70"
71if test -z "$2"
72then
73define_categories "$1"
74echo
75print_command_list "$1"
76exit 0
77fi
7879
echo "static const char *common_cmd_groups[] = {"
8081
grps=grps$$.tmp
82match=match$$.tmp
83trap "rm -f '$grps' '$match'" 0 1 2 3 15
8485
sed -n '
861,/^### common groups/b
87/^### command list/q
88/^#/b
89/^[ ]*$/b
90h;s/^[^ ][^ ]*[ ][ ]*\(.*\)/ N_("\1"),/p
91g;s/^\([^ ][^ ]*\)[ ].*/\1/w '$grps'
92' "$1"
93printf '};\n\n'
9495
n=0
96substnum=
97while read grp
98do
99echo "^git-..*[ ]$grp"
100substnum="$substnum${substnum:+;}s/[ ]$grp/$n/"
101n=$(($n+1))
102done <"$grps" >"$match"
103104
printf 'static struct cmdname_help common_cmds[] = {\n'
105grep -f "$match" "$1" |
106sed 's/^git-//' |
107sort |
108while read cmd tags
109do
110tag=$(echo "$tags" | sed "$substnum; s/[^0-9]//g")
111echo " {\"$cmd\", $(get_synopsis git-$cmd), $tag},"
112done
113echo "};"