generate-cmdlist.sh 1.79 KB
Newer Older
1 2
#!/bin/sh

3 4 5 6 7 8
die () {
	echo "$@" >&2
	exit 1
}

command_list () {
9
	eval "grep -ve '^#' $exclude_programs" <"$1"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
}

get_categories () {
	tr ' ' '\n'|
	grep -v '^$' |
	sort |
	uniq
}

category_list () {
	command_list "$1" |
	cut -c 40- |
	get_categories
}

25 26 27 28 29 30 31 32 33 34
get_synopsis () {
	sed -n '
		/^NAME/,/'"$1"'/H
		${
			x
			s/.*'"$1"' - \(.*\)/N_("\1")/
			p
		}' "Documentation/$1.txt"
}

35 36 37 38 39 40 41 42 43 44 45 46 47
define_categories () {
	echo
	echo "/* Command categories */"
	bit=0
	category_list "$1" |
	while read cat
	do
		echo "#define CAT_$cat (1UL << $bit)"
		bit=$(($bit+1))
	done
	test "$bit" -gt 32 && die "Urgh.. too many categories?"
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
define_category_names () {
	echo
	echo "/* Category names */"
	echo "static const char *category_names[] = {"
	bit=0
	category_list "$1" |
	while read cat
	do
		echo "	\"$cat\", /* (1UL << $bit) */"
		bit=$(($bit+1))
	done
	echo "	NULL"
	echo "};"
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
print_command_list () {
	echo "static struct cmdname_help command_list[] = {"

	command_list "$1" |
	while read cmd rest
	do
		printf "	{ \"$cmd\", $(get_synopsis $cmd), 0"
		for cat in $(echo "$rest" | get_categories)
		do
			printf " | CAT_$cat"
		done
		echo " },"
	done
	echo "};"
}

79 80 81 82
print_config_list () {
	cat <<EOF
static const char *config_name_list[] = {
EOF
83
	grep -h '^[a-zA-Z].*\..*::$' Documentation/*config.txt Documentation/config/*.txt |
84 85 86 87 88 89 90 91 92 93 94 95
	sed '/deprecated/d; s/::$//; s/,  */\n/g' |
	sort |
	while read line
	do
		echo "	\"$line\","
	done
	cat <<EOF
	NULL,
};
EOF
}

96 97 98 99 100 101 102 103
exclude_programs=
while test "--exclude-program" = "$1"
do
	shift
	exclude_programs="$exclude_programs -e \"^$1 \""
	shift
done

104
echo "/* Automatically generated by generate-cmdlist.sh */
105
struct cmdname_help {
106 107
	const char *name;
	const char *help;
108
	uint32_t category;
109
};
110
"
Duy Nguyen's avatar
Duy Nguyen committed
111 112
define_categories "$1"
echo
113 114
define_category_names "$1"
echo
Duy Nguyen's avatar
Duy Nguyen committed
115
print_command_list "$1"
116 117
echo
print_config_list