generate-cmdlist.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/sh
  2. die () {
  3. echo "$@" >&2
  4. exit 1
  5. }
  6. command_list () {
  7. eval "grep -ve '^#' $exclude_programs" <"$1"
  8. }
  9. get_categories () {
  10. tr ' ' '\012'|
  11. grep -v '^$' |
  12. sort |
  13. uniq
  14. }
  15. category_list () {
  16. command_list "$1" |
  17. cut -c 40- |
  18. get_categories
  19. }
  20. get_synopsis () {
  21. sed -n '
  22. /^NAME/,/'"$1"'/H
  23. ${
  24. x
  25. s/.*'"$1"' - \(.*\)/N_("\1")/
  26. p
  27. }' "Documentation/$1.txt"
  28. }
  29. define_categories () {
  30. echo
  31. echo "/* Command categories */"
  32. bit=0
  33. category_list "$1" |
  34. while read cat
  35. do
  36. echo "#define CAT_$cat (1UL << $bit)"
  37. bit=$(($bit+1))
  38. done
  39. test "$bit" -gt 32 && die "Urgh.. too many categories?"
  40. }
  41. define_category_names () {
  42. echo
  43. echo "/* Category names */"
  44. echo "static const char *category_names[] = {"
  45. bit=0
  46. category_list "$1" |
  47. while read cat
  48. do
  49. echo " \"$cat\", /* (1UL << $bit) */"
  50. bit=$(($bit+1))
  51. done
  52. echo " NULL"
  53. echo "};"
  54. }
  55. print_command_list () {
  56. echo "static struct cmdname_help command_list[] = {"
  57. command_list "$1" |
  58. while read cmd rest
  59. do
  60. printf " { \"$cmd\", $(get_synopsis $cmd), 0"
  61. for cat in $(echo "$rest" | get_categories)
  62. do
  63. printf " | CAT_$cat"
  64. done
  65. echo " },"
  66. done
  67. echo "};"
  68. }
  69. exclude_programs=
  70. while test "--exclude-program" = "$1"
  71. do
  72. shift
  73. exclude_programs="$exclude_programs -e \"^$1 \""
  74. shift
  75. done
  76. echo "/* Automatically generated by generate-cmdlist.sh */
  77. struct cmdname_help {
  78. const char *name;
  79. const char *help;
  80. uint32_t category;
  81. };
  82. "
  83. define_categories "$1"
  84. echo
  85. define_category_names "$1"
  86. echo
  87. print_command_list "$1"