File: docsurgeon

package info (click to toggle)
ndctl 81-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,436 kB
  • sloc: ansic: 41,432; sh: 3,931; makefile: 28
file content (339 lines) | stat: -rwxr-xr-x 6,032 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash -eE

this_script="docsurgeon"
script_dir="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
env_file="${script_dir}/.env"
if [ -e "$env_file" ]; then
	# shellcheck source=.env
	. "$env_file"
fi

sources_file="${script_dir}/.sources"

parser_generator="${script_dir}/${this_script}_parser_generator.m4"
parser_lib="${script_dir}/${this_script}_parser.sh"
if [ ! -e "$parser_lib" ] || [ "$parser_generator" -nt "$parser_lib" ]; then
	if command -V argbash > /dev/null; then
		argbash --strip user-content "$parser_generator" -o "$parser_lib"
	else
		echo "error: please install argbash" >&2
		exit 1
	fi
fi

if [[ $1 != "bin" ]]; then
	# shellcheck source=docsurgeon_parser.sh
	. "${script_dir}/${this_script}_parser.sh" || { echo "Couldn't find $parser_lib" >&2; exit 1; }
fi

# some script defaults - override using '.env'
docbase="Documentation"
copyright_cli="// SPDX-License-Identifier: GPL-2.0"
copyright_footer_cli="include::../copyright.txt[]"
copyright_lib="// SPDX-License-Identifier: LGPL-2.0"
copyright_footer_lib="include::../../copyright.txt[]"

# List of files we're creating, to be edited/renamed later
# This starts out blank, and is filled in as we go by gen_*() functions
declare -a outfiles

cleanup()
{
	if [ ${#outfiles[@]} -gt 0 ]; then
		rm -f "${outfiles[@]}"
	fi
	set +x
}

trap cleanup EXIT

auto_detect_params()
{
	fs=""
	module=""
	section=""

	# if module and section were explicitly specified, respect them
	if [[ $_arg_module ]] && [[ $_arg_section ]]; then
		return
	fi

	# check if names are self-consistent, and determine 'fs'
	for name in ${_arg_name[@]}; do
		if [[ ! $fs ]]; then
			if [[ $name == *-* ]]; then
				fs="-"
			elif [[ $name == *_* ]]; then
				fs="_"
			else
				# can't autodetect section
				return
			fi
		fi
		if [[ $fs == "-" ]] && [[ $name == *_* ]]; then
			die "can't auto-detect params with mixed-style names"
		fi
		if [[ $fs == "_" ]] && [[ $name == *-* ]]; then
			die "can't auto-detect params with mixed-style names"
		fi
	done

	# try to detect module name
	for name in ${_arg_name[@]}; do
		str=${name%%$fs*}
		if [[ $module ]]; then
			if [[ $str != $module ]]; then
				die "Can't autodetect module because of mixed names ($str and $module)"
			fi
		else
			module="$str"
		fi
	done

	# try to detect section number
	case "$fs" in
	-)
		section=1
		;;
	_)
		section=3
		;;
	*)
		die "Unknown fs, can't autodetect section number"
		;;
	esac

	if [[ $module ]]; then
		_arg_module="$module"
	fi
	if [[ $section ]]; then
		_arg_section="$section"
	fi
}

process_options_logic()
{
	if [[ $_arg_debug == "on" ]]; then
		set -x
	fi

	auto_detect_params
}

gen_underline()
{
	name="$1"
	char="$2"
	num="${#name}"

	printf -v tmpstring "%-${num}s" " "
	echo "${tmpstring// /$char}"
}

gen_header()
{
	printf "\n%s\n%s\n" "$1" "$(gen_underline "$1" "=")"
}

gen_section()
{
	printf "\n%s\n%s\n" "$1" "$(gen_underline "$1" "-")"
}

gen_section_name()
{
	name="$1"

	gen_section "NAME"
	cat <<- EOF
		$name - 
	EOF
}

gen_section_synopsis_1()
{
	name="$1"

	gen_section "SYNOPSIS"
	cat <<- EOF
		[verse]
		'$_arg_module ${name#*-} [<options>]'
	EOF
}

gen_section_synopsis_3()
{
	name="$1"

	gen_section "SYNOPSIS"
	cat <<- EOF
		[verse]
		----
		#include <$_arg_module/lib$_arg_module.h>

		<type> $name();
		----
	EOF
}

gen_section_example_1()
{
	name="$1"

	gen_section "EXAMPLE"
	cat <<- EOF
		----
		# $_arg_module ${name#*-}
		----
	EOF
}

gen_section_example_3()
{
	name="$1"

	gen_section "EXAMPLE"
	cat <<- EOF
		See example usage in test/lib$_arg_module.c
	EOF
}

gen_section_options_1()
{
	gen_section "OPTIONS"
cat << EOF
-o::
--option::
	Description
EOF

	if [[ $_arg_human_option == "on" ]]; then
		printf "\n%s\n" "include::human-option.txt[]"
	fi
	if [[ $_arg_verbose_option == "on" ]]; then
		printf "\n%s\n" "include::verbose-option.txt[]"
	fi
}

gen_section_seealso_1()
{
	gen_section "SEE ALSO"
	cat <<- EOF
	link$_arg_module:$_arg_module-list[$_arg_section],
	EOF
}

gen_section_seealso_3()
{
	gen_section "SEE ALSO"
	cat <<- EOF
	linklib$_arg_module:${_arg_module}_other_API[$_arg_section],
	EOF
}

gen_cli()
{
	name="$1"
	path="$docbase/$_arg_module"
	if [ ! -d "$path" ]; then
		die "Not found: $path"
	fi

	tmp="$(mktemp -p "$path" "$name.txt.XXXX")"
	outfiles+=("$tmp")

	# Start template generation
	printf "%s\n" "$copyright_cli" > "$tmp"
	gen_header "$name($_arg_section)" >> "$tmp"
	gen_section_name "$name" >> "$tmp"
	gen_section_synopsis_1 "$name" >> "$tmp"
	gen_section "DESCRIPTION" >> "$tmp"
	gen_section_example_1 "$name" >> "$tmp"
	gen_section_options_1 >> "$tmp"
	printf "\n%s\n" "$copyright_footer_cli" >> "$tmp"
	gen_section_seealso_1 >> "$tmp"
}

gen_lib()
{
	name="$1"
	path="$docbase/$_arg_module/lib"
	if [ ! -d "$path" ]; then
		die "Not found: $path"
	fi

	tmp="$(mktemp -p "$path" "$name.txt.XXXX")"
	outfiles+=("$tmp")

	# Start template generation
	printf "%s\n" "$copyright_lib" > "$tmp"
	gen_header "$name($_arg_section)" >> "$tmp"
	gen_section_name "$name" >> "$tmp"
	gen_section_synopsis_3 "$name" >> "$tmp"
	gen_section "DESCRIPTION" >> "$tmp"
	gen_section "RETURN VALUE" >> "$tmp"
	gen_section_example_3 "$name" >> "$tmp"
	printf "\n%s\n" "$copyright_footer_lib" >> "$tmp"
	gen_section_seealso_3 >> "$tmp"
}

gen_man()
{
	name="$1"
	case "$_arg_section" in
	1)
		gen_cli "$name"
		;;
	3)
		gen_lib "$name"
		;;
	*)
		die "Unknown section: $_arg_section"
		;;
	esac
}

gen_include()
{
	echo "in gen_include"
}

main()
{
	process_options_logic

	cmd="$_arg_command"
	case "$cmd" in
	gen-man)
		for name in ${_arg_name[@]}; do
			gen_man "$name"
		done
		;;
	gen-include)
		for name in ${_arg_name[@]}; do
			gen_include
		done
		;;
	*)
		die "Unknown command: $cmd"
		;;
	esac

	if [[ $_arg_dump == "on" ]]; then
		for file in ${outfiles[@]}; do
			echo "${file##*/}"
			cat "$file"
			rm "$file"
		done
	elif [ ${#outfiles[@]} -gt 0 ]; then
		if [[ $_arg_edit = "on" ]]; then
			vim -p "${outfiles[@]}"
		fi

		for file in ${outfiles[@]}; do
			mv "$file" "${file%.*}"
		done
	fi
}

main "$@"