| 1 | #!/bin/bash -eE |
| 2 | |
| 3 | this_script="docsurgeon" |
| 4 | script_dir="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)" |
| 5 | env_file="${script_dir}/.env" |
| 6 | if [ -e "$env_file" ]; then |
| 7 | # shellcheck source=.env |
| 8 | . "$env_file" |
| 9 | fi |
| 10 | |
| 11 | sources_file="${script_dir}/.sources" |
| 12 | |
| 13 | parser_generator="${script_dir}/${this_script}_parser_generator.m4" |
| 14 | parser_lib="${script_dir}/${this_script}_parser.sh" |
| 15 | if [ ! -e "$parser_lib" ] || [ "$parser_generator" -nt "$parser_lib" ]; then |
| 16 | if command -V argbash > /dev/null; then |
| 17 | argbash --strip user-content "$parser_generator" -o "$parser_lib" |
| 18 | else |
| 19 | echo "error: please install argbash" >&2 |
| 20 | exit 1 |
| 21 | fi |
| 22 | fi |
| 23 | |
| 24 | if [[ $1 != "bin" ]]; then |
| 25 | # shellcheck source=docsurgeon_parser.sh |
| 26 | . "${script_dir}/${this_script}_parser.sh" || { echo "Couldn't find $parser_lib" >&2; exit 1; } |
| 27 | fi |
| 28 | |
| 29 | # some script defaults - override using '.env' |
| 30 | docbase="Documentation" |
| 31 | copyright_cli="// SPDX-License-Identifier: GPL-2.0" |
| 32 | copyright_footer_cli="include::../copyright.txt[]" |
| 33 | copyright_lib="// SPDX-License-Identifier: LGPL-2.0" |
| 34 | copyright_footer_lib="include::../../copyright.txt[]" |
| 35 | |
| 36 | # List of files we're creating, to be edited/renamed later |
| 37 | # This starts out blank, and is filled in as we go by gen_*() functions |
| 38 | declare -a outfiles |
| 39 | |
| 40 | cleanup() |
| 41 | { |
| 42 | if [ ${#outfiles[@]} -gt 0 ]; then |
| 43 | rm -f "${outfiles[@]}" |
| 44 | fi |
| 45 | set +x |
| 46 | } |
| 47 | |
| 48 | trap cleanup EXIT |
| 49 | |
| 50 | auto_detect_params() |
| 51 | { |
| 52 | fs="" |
| 53 | module="" |
| 54 | section="" |
| 55 | |
| 56 | # if module and section were explicitly specified, respect them |
| 57 | if [[ $_arg_module ]] && [[ $_arg_section ]]; then |
| 58 | return |
| 59 | fi |
| 60 | |
| 61 | # check if names are self-consistent, and determine 'fs' |
| 62 | for name in ${_arg_name[@]}; do |
| 63 | if [[ ! $fs ]]; then |
| 64 | if [[ $name == *-* ]]; then |
| 65 | fs="-" |
| 66 | elif [[ $name == *_* ]]; then |
| 67 | fs="_" |
| 68 | else |
| 69 | # can't autodetect section |
| 70 | return |
| 71 | fi |
| 72 | fi |
| 73 | if [[ $fs == "-" ]] && [[ $name == *_* ]]; then |
| 74 | die "can't auto-detect params with mixed-style names" |
| 75 | fi |
| 76 | if [[ $fs == "_" ]] && [[ $name == *-* ]]; then |
| 77 | die "can't auto-detect params with mixed-style names" |
| 78 | fi |
| 79 | done |
| 80 | |
| 81 | # try to detect module name |
| 82 | for name in ${_arg_name[@]}; do |
| 83 | str=${name%%$fs*} |
| 84 | if [[ $module ]]; then |
| 85 | if [[ $str != $module ]]; then |
| 86 | die "Can't autodetect module because of mixed names ($str and $module)" |
| 87 | fi |
| 88 | else |
| 89 | module="$str" |
| 90 | fi |
| 91 | done |
| 92 | |
| 93 | # try to detect section number |
| 94 | case "$fs" in |
| 95 | -) |
| 96 | section=1 |
| 97 | ;; |
| 98 | _) |
| 99 | section=3 |
| 100 | ;; |
| 101 | *) |
| 102 | die "Unknown fs, can't autodetect section number" |
| 103 | ;; |
| 104 | esac |
| 105 | |
| 106 | if [[ $module ]]; then |
| 107 | _arg_module="$module" |
| 108 | fi |
| 109 | if [[ $section ]]; then |
| 110 | _arg_section="$section" |
| 111 | fi |
| 112 | } |
| 113 | |
| 114 | process_options_logic() |
| 115 | { |
| 116 | if [[ $_arg_debug == "on" ]]; then |
| 117 | set -x |
| 118 | fi |
| 119 | |
| 120 | auto_detect_params |
| 121 | } |
| 122 | |
| 123 | gen_underline() |
| 124 | { |
| 125 | name="$1" |
| 126 | char="$2" |
| 127 | num="${#name}" |
| 128 | |
| 129 | printf -v tmpstring "%-${num}s" " " |
| 130 | echo "${tmpstring// /$char}" |
| 131 | } |
| 132 | |
| 133 | gen_header() |
| 134 | { |
| 135 | printf "\n%s\n%s\n" "$1" "$(gen_underline "$1" "=")" |
| 136 | } |
| 137 | |
| 138 | gen_section() |
| 139 | { |
| 140 | printf "\n%s\n%s\n" "$1" "$(gen_underline "$1" "-")" |
| 141 | } |
| 142 | |
| 143 | gen_section_name() |
| 144 | { |
| 145 | name="$1" |
| 146 | |
| 147 | gen_section "NAME" |
| 148 | cat <<- EOF |
| 149 | $name - |
| 150 | EOF |
| 151 | } |
| 152 | |
| 153 | gen_section_synopsis_1() |
| 154 | { |
| 155 | name="$1" |
| 156 | |
| 157 | gen_section "SYNOPSIS" |
| 158 | cat <<- EOF |
| 159 | [verse] |
| 160 | '$_arg_module ${name#*-} [<options>]' |
| 161 | EOF |
| 162 | } |
| 163 | |
| 164 | gen_section_synopsis_3() |
| 165 | { |
| 166 | name="$1" |
| 167 | |
| 168 | gen_section "SYNOPSIS" |
| 169 | cat <<- EOF |
| 170 | [verse] |
| 171 | ---- |
| 172 | #include <$_arg_module/lib$_arg_module.h> |
| 173 | |
| 174 | <type> $name(); |
| 175 | ---- |
| 176 | EOF |
| 177 | } |
| 178 | |
| 179 | gen_section_example_1() |
| 180 | { |
| 181 | name="$1" |
| 182 | |
| 183 | gen_section "EXAMPLE" |
| 184 | cat <<- EOF |
| 185 | ---- |
| 186 | # $_arg_module ${name#*-} |
| 187 | ---- |
| 188 | EOF |
| 189 | } |
| 190 | |
| 191 | gen_section_example_3() |
| 192 | { |
| 193 | name="$1" |
| 194 | |
| 195 | gen_section "EXAMPLE" |
| 196 | cat <<- EOF |
| 197 | See example usage in test/lib$_arg_module.c |
| 198 | EOF |
| 199 | } |
| 200 | |
| 201 | gen_section_options_1() |
| 202 | { |
| 203 | gen_section "OPTIONS" |
| 204 | cat << EOF |
| 205 | -o:: |
| 206 | --option:: |
| 207 | Description |
| 208 | EOF |
| 209 | |
| 210 | if [[ $_arg_human_option == "on" ]]; then |
| 211 | printf "\n%s\n" "include::human-option.txt[]" |
| 212 | fi |
| 213 | if [[ $_arg_verbose_option == "on" ]]; then |
| 214 | printf "\n%s\n" "include::verbose-option.txt[]" |
| 215 | fi |
| 216 | } |
| 217 | |
| 218 | gen_section_seealso_1() |
| 219 | { |
| 220 | gen_section "SEE ALSO" |
| 221 | cat <<- EOF |
| 222 | link$_arg_module:$_arg_module-list[$_arg_section], |
| 223 | EOF |
| 224 | } |
| 225 | |
| 226 | gen_section_seealso_3() |
| 227 | { |
| 228 | gen_section "SEE ALSO" |
| 229 | cat <<- EOF |
| 230 | linklib$_arg_module:${_arg_module}_other_API[$_arg_section], |
| 231 | EOF |
| 232 | } |
| 233 | |
| 234 | gen_cli() |
| 235 | { |
| 236 | name="$1" |
| 237 | path="$docbase/$_arg_module" |
| 238 | if [ ! -d "$path" ]; then |
| 239 | die "Not found: $path" |
| 240 | fi |
| 241 | |
| 242 | tmp="$(mktemp -p "$path" "$name.txt.XXXX")" |
| 243 | outfiles+=("$tmp") |
| 244 | |
| 245 | # Start template generation |
| 246 | printf "%s\n" "$copyright_cli" > "$tmp" |
| 247 | gen_header "$name($_arg_section)" >> "$tmp" |
| 248 | gen_section_name "$name" >> "$tmp" |
| 249 | gen_section_synopsis_1 "$name" >> "$tmp" |
| 250 | gen_section "DESCRIPTION" >> "$tmp" |
| 251 | gen_section_example_1 "$name" >> "$tmp" |
| 252 | gen_section_options_1 >> "$tmp" |
| 253 | printf "\n%s\n" "$copyright_footer_cli" >> "$tmp" |
| 254 | gen_section_seealso_1 >> "$tmp" |
| 255 | } |
| 256 | |
| 257 | gen_lib() |
| 258 | { |
| 259 | name="$1" |
| 260 | path="$docbase/$_arg_module/lib" |
| 261 | if [ ! -d "$path" ]; then |
| 262 | die "Not found: $path" |
| 263 | fi |
| 264 | |
| 265 | tmp="$(mktemp -p "$path" "$name.txt.XXXX")" |
| 266 | outfiles+=("$tmp") |
| 267 | |
| 268 | # Start template generation |
| 269 | printf "%s\n" "$copyright_lib" > "$tmp" |
| 270 | gen_header "$name($_arg_section)" >> "$tmp" |
| 271 | gen_section_name "$name" >> "$tmp" |
| 272 | gen_section_synopsis_3 "$name" >> "$tmp" |
| 273 | gen_section "DESCRIPTION" >> "$tmp" |
| 274 | gen_section "RETURN VALUE" >> "$tmp" |
| 275 | gen_section_example_3 "$name" >> "$tmp" |
| 276 | printf "\n%s\n" "$copyright_footer_lib" >> "$tmp" |
| 277 | gen_section_seealso_3 >> "$tmp" |
| 278 | } |
| 279 | |
| 280 | gen_man() |
| 281 | { |
| 282 | name="$1" |
| 283 | case "$_arg_section" in |
| 284 | 1) |
| 285 | gen_cli "$name" |
| 286 | ;; |
| 287 | 3) |
| 288 | gen_lib "$name" |
| 289 | ;; |
| 290 | *) |
| 291 | die "Unknown section: $_arg_section" |
| 292 | ;; |
| 293 | esac |
| 294 | } |
| 295 | |
| 296 | gen_include() |
| 297 | { |
| 298 | echo "in gen_include" |
| 299 | } |
| 300 | |
| 301 | main() |
| 302 | { |
| 303 | process_options_logic |
| 304 | |
| 305 | cmd="$_arg_command" |
| 306 | case "$cmd" in |
| 307 | gen-man) |
| 308 | for name in ${_arg_name[@]}; do |
| 309 | gen_man "$name" |
| 310 | done |
| 311 | ;; |
| 312 | gen-include) |
| 313 | for name in ${_arg_name[@]}; do |
| 314 | gen_include |
| 315 | done |
| 316 | ;; |
| 317 | *) |
| 318 | die "Unknown command: $cmd" |
| 319 | ;; |
| 320 | esac |
| 321 | |
| 322 | if [[ $_arg_dump == "on" ]]; then |
| 323 | for file in ${outfiles[@]}; do |
| 324 | echo "${file##*/}" |
| 325 | cat "$file" |
| 326 | rm "$file" |
| 327 | done |
| 328 | elif [ ${#outfiles[@]} -gt 0 ]; then |
| 329 | if [[ $_arg_edit = "on" ]]; then |
| 330 | vim -p "${outfiles[@]}" |
| 331 | fi |
| 332 | |
| 333 | for file in ${outfiles[@]}; do |
| 334 | mv "$file" "${file%.*}" |
| 335 | done |
| 336 | fi |
| 337 | } |
| 338 | |
| 339 | main "$@" |