Add bash and zsh completions for llppac
[llpp.git] / misc / completions / mkcomp.sh
blob5345269f47d8542d61becd7515d9a91a47f55c9b
1 #!/bin/sh
2 # mkcomp --- make completion files
4 # Usage: mkcomp <template file>
6 parse_infile () {
7 # Output list of file extensions from all input files, one per line.
8 # Delete lines beginning with `#', empty lines, and strip leading and
9 # trailing whitespace.
10 sed -f - <<EOF $@
11 /^#/d
12 /^[[:space:]]*$/d
13 s/^[[:space:]]+//g
14 s/[[:space:]]+$//g
15 EOF
18 print_regex () {
19 # print regex of the file extensions from all input files
20 # e.g. (foo|bar|baz)
21 n=0
23 printf '('
24 parse_infile $@ | \
25 while read -r line; do
26 if test -n "$line"; then
27 if test $n -eq 0; then
28 printf "%s" "$line"
29 else
30 printf "|%s" "$line"
33 n=$((n+1))
34 done
35 printf ')'
38 # combine llpp.in and llppac.in if making a llppac completion
39 f="$1"
41 if test "$(basename "$f")" = llppac; then
42 llppac=llppac.in
44 re="$(print_regex llpp.in $llppac)"
46 sed -e "s/@re@/$re/g" "$f.mk" > "$f"
47 printf "wrote: %s\n" "$f"