summaryrefslogtreecommitdiff
path: root/autoopts/tpl/usage.tlib
blob: 91bb239dbaa005568b1dc8e01f3cce1ed7a79e6a (plain)
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
[=  AutoGen5 Template  -*- Mode: shell-script -*-

 help-text

# This file is part of AutoGen.
# AutoGen Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
#
# AutoGen is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# AutoGen is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

=][= INCLUDE "tpl-config.tlib" =][=

 ;; This template is designed to emit help text from the current set
 ;; of option definitions.
 ;;
 (make-tmp-dir)
 (out-push-new (shellf "echo ${tmp_dir}/%s.def" (get "prog-name")))
 (define emit-def (lambda (vname)
   (if (exist? vname)
       (sprintf "\n%s = %s;" vname (kr-string (get vname))) ) ))
=]
AutoGen Definitions options.tpl;
no-libopts;
[=

FOR var IN  prog-name  prog-title  argument
            environrc  export      homerc         include
            long-opts  rcfile      version        detail
            explain    package     preserve-case  prog-desc
            opts-ptr   gnu-usage   reorder-args   usage-opt

            version-value   help-value  more-help-value
            save-opts-value usage-value load-opts-value
  =][=
  (emit-def (get "var"))    =][=
ENDFOR var IN ....          =][=

IF (exist? "config-header") =]
config-header = '[= prog-name =]-config.h';[=
ENDIF                       =][=

FOR copyright               =]
copyright = {[=

  FOR var IN  date owner type text author eaddr
    =][=
    (emit-def (get "var"))  =][=
  ENDFOR var IN ....        =]
};[=
ENDFOR copyright            =]

main = { main-type = shell-process; };
[=

FOR flag

=]
flag = {[=

  FOR var IN name descrip value max min must-set enable disable enabled
             ifdef ifndef no-preset settable equivalence documentation
             immediate immed-disable also
             arg-type arg-optional arg-default default arg-range
             stack-arg unstack-arg
    =][=
    (emit-def (get "var"))  =][=
  ENDFOR var IN ....        =][=

  IF (exist? "keyword")     =]
    keyword = '[=    (join "', '" (stack "keyword")) =]';[=
  ENDIF  keyword exists     =][=

  IF (exist? "flags-must")  =]
    flags-must = '[= (join "', '" (stack "flags-must")) =]';[=
  ENDIF  flags-must exists  =][=

  IF (exist? "flags-cant")  =]
    flags-cant = '[= (join "', '" (stack "flags-cant")) =]';[=
  ENDIF  flags-cant exists  =]
};[=

ENDFOR flag                 =][=

(out-pop)
(out-push-new)             \=]
# redirect stdout.  We see this IFF there is a problem
#
redirect_log=${tmp_dir}/redirected.log
exec 8>&1 9>&2 1> ${redirect_log} 2>&1
redirect_die() {
    exec 2>&9 1>&9 9>&- 8>&-
    cat ${redirect_log}
    die "$@"
}

inc_list="-I${PWD} -I[=(. inc-dir)=]"
cfg_ldflags="[=(. libs)=]"
cfg_cflags=${CFLAGS}
exe=${tmp_dir}/[= prog-name =]
[= # START-BUILDTREE-ISMS:

#  The following code is sedded away in install-hook.sh.
#  The goal is to remove build tree-isms when installing this file.

\=]
test -z "${top_builddir}" && ldflags='' || \
    ldflags=`exec 2>/dev/null
        find ${top_builddir}/autoopts -name "libopts*.${OBJEXT}" | head -1`

test -f "${ldflags}" || {
    ldflags='[=(. lib-dir)=]/libopts.a'
    test -f "${ldflags}" || redirect_die "Cannot locate libopts.a"
}
ldflags="$ldflags ${cfg_ldflags}"
test -d "${top_builddir}" && \
    inc_list="-I${top_builddir} -I${top_builddir}/autoopts ${inc_list}"
test -d "${top_srcdir}" && \
    inc_list="-I${top_srcdir}/autoopts ${inc_list}"

[= # END-BUILDTREE-ISMS the following code is for installed version:

test -x "${AGexe}" || redirect_die "AGexe is invalid:  ${AGexe}"
aocfg=`dirname ${AGexe}`/autoopts-config
test -x "$aocfg" || redirect_die "missing $ag"
ldflags="${cfg_ldflags} `${aocfg} ldflags`"
cfg_cflags="${cfg_cflags} `${aocfg} cflags`"

# END-INSTALL-ONLY-CODE \=]
[= IF (exist? "config-header") \=]
inc_list="-I${tmp_dir} ${inc_list}"
while :
do
    h='[= config-header =]'
    test -f "$h" && break
    hdr=$h
    h=`basename "${hdr}"`
    test -f "$h" && break
    g=$h
    d=`pwd`

    while :
    do
        d=`dirname $d`
        test "X$d" = X/ && \
            redirect_die "cannot locate [= config-header =]"
        h="$d/$g"
        test -f "$h" && break
        h="$d/$hdr"
        test -f "$h" && break
    done
    break
done
cp "${h}" ${exe}-config.h
[= ENDIF \=]
flags="-DTEST_[= (string-upcase! (string->c-name! (get "prog-name")))
    =]_OPTS=1 ${inc_list} ${cfg_cflags}"
cd ${tmp_dir}
${AGexe} -Toptions.tpl [= prog-name =].def || \
    redirect_die "Cannot gen [= prog-name =]"
cd -
${CC:-cc} ${flags} -g -o TMPexe$$ ${exe}.c ${ldflags} || \
    redirect_die cannot compile ${exe}.c
mv -f TMPexe$$ ${exe}
exec 1>&8 2>&9 9>&- 8>&-

${exe} [=

  (if (== (get "usage-type") "short")
      (if (exist? "usage-opt")
          (if (exist? "long-opts")
              "--usage"
              (string-append "-" (get "usage-value" "u"))
          )
          "--give-me-short-usage 2>&1 | sed -e '/: illegal option /d'"
      )
      (if (exist? "long-opts")
          "--help"
          (string-append "-" (get "help-value" "?"))
   )  )   =] || \
  die "cannot obtain ${exe} help in ${tmp_dir}"[=

(shell (out-pop #t))

=]
[=

## Local Variables:
## Mode: shell-script
## indent-tabs-mode:       nil
## sh-basic-offset:          4
## sh-indent-after-do:       4
## sh-indentation:           4
## sh-indent-for-case-label: 0
## sh-indent-for-case-alt:   4
## End:

# end of usage.tlib \=]