-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocscript
executable file
·141 lines (114 loc) · 2.07 KB
/
docscript
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
#!/bin/bash
#
# docscript
#
declare MYDIR=
declare MYLIB=
declare MYDATA=
declare background=
declare usagestr=
declare optcount=0
declare b_verbose=false
declare file=
declare b_func=false
MYDIR="$(dirname "$(which "$(basename "$0")")")"
MYLIB="$MYDIR"/lib
MYDATA=$(realpath ./.data)
[ -d "$MYDATA" ] || mkdir -p "$MYDATA"
source "$MYLIB"/ui.source
usagestr=$(
cat <<EOF
$(basename "$0") [options] file
Arguments
---------
file : script having doc delimiters #** and #*
Options
-------
-v : verbose output instead of single line
-f : specific function or data block
\0
EOF
)
# control_c: run if user hits control-c
#
# Global
# CTLC_EXIT - bash environment variable
#
control_c() {
echo -e "\nCtrl-c detected\nCleaning up and exiting."
exit $CTLC_EXIT
}
# init
#
# GLOBALS
# background
#
init() {
local bgtemp
ui_setbg bgtemp
background=$bgtemp
}
# parseopts
#
# Globals
#
parseopts() {
local arg=
local opt=
for arg in $@; do
if [ "${arg:0:1}" == "-" ]; then
opt="${arg:1}"
shift
((optcount++))
case "$opt" in
v ) b_verbose=true
;;
f ) fname="$1"
b_verbose=true
b_func=true
shift
((optcount++))
;;
h ) echo -e "$usagestr"
exit 0
esac
fi
done
return 0
}
main() {
local b_doc=false
local line=
local -i fnamsiz
local -a lary=()
parseopts "$@"
shift "$optcount"
(($# > 0)) || { echo -e "$usagestr"; exit 1; }
file="$1"
[ -f "$file" ] || { echo -e "$usagestr"; exit 1; }
while IFS= read -r line; do
read -ra lary <<< "$line"
$b_doc || [ "${line:0:1}" == "#" ] || continue
if [ "${lary[0]:0:3}" == "#**" ]; then
if $b_func; then
fnamsiz=${#fname}
[[ "${lary[1]}" == *"$fname"* ]] || continue
fi
$b_verbose && echo -e "\n$(ui_putnchar "=" 75)"
echo "${lary[@]:1}"
b_doc=true
$b_verbose && echo "$(ui_putnchar "-" 75)"
continue
fi
$b_verbose || continue
if [[ "${lary[0]:0:2}" != "#*" ]]; then
if $b_doc; then
[[ "${line:0:1}" == "#" ]] && echo "${line:1}" || echo "$line"
fi
else
b_doc=false
fi
done < "$file"
ui_exit ui_exit_ok
}
main "$@"