Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtdocbook / doc2sdl / dtdocbook
1 #!/bin/ksh
2
3 # get the name of this command for errors, warnings and messages
4 command_name=`basename $0`
5
6
7 # initialize the variables that get set by options
8 typeset -i compress=0
9 typeset -i decompress=0
10 typeset -i help=0
11 typeset -i log=0
12 mapfiles=""
13 catfiles=""
14 outname=""
15 typeset -i remove=0
16 typeset -i uncompressed=0
17 typeset -i verbose=0
18 typeset -i debug=0
19
20
21 # We use this string a couple of different places.
22 c_and_d="Compress (-c) and decompress (-d)"
23
24
25 # Create a funtion to call on fatal error before we know about file
26 # names.
27 function fatal {
28     echo "$command_name fatal error:"
29     echo "    $1"
30     exit 1
31 }
32
33
34 # Create a funtion to call on fatal error merging any error files.
35 function fatalError {
36     echo "$command_name fatal error:"
37     echo "    $1"
38     if [[ -a $basename.out.err ]]; then
39         cat $basename.out.err >> $basename.log
40         rm -f $basename.out.err
41     fi
42     exit 1
43 }
44
45
46 # Create a function to call for warnings.
47 function warn {
48     echo "$command_name warning:"
49     echo "    $1"
50 }
51
52
53 # Process the options.
54 while 
55     getopts ":cdg:hlm:o:rs:t:uvxH:I:L:S:" opt
56 do
57     case $opt in
58         (c) compress=1;;
59         (d) decompress=1;;
60         (g) if [[ "$catfiles" = "" ]] then
61                 catfiles="m $OPTARG"
62             else
63                 catfiles="$catfiles -m $OPTARG"
64             fi;;
65         (h) help=1;;
66         (i) info_dir="$OPTARG";;
67         (l) log=1;;
68         (m) mapfiles="$mapfiles $OPTARG";;
69         (o) oname="$OPTARG"
70             if [[ $oname = -* ]] then
71                 warn "Output file name (-o $oname) begins with a \"-\""
72             fi;;
73         (r) remove=1;;
74         (s) sgml_dir="$OPTARG";;
75         (t) dbk_lib="$OPTARG";;
76         (u) uncompressed=1;;
77         (v) verbose=1;;
78         (x) debug=1;;
79         # undocumented options to be used at build time
80         (H) helptag2="$OPTARG";;
81         (I) instant="$OPTARG";;
82         (L) x_locale="$OPTARG";;
83         (S) sgmls="$OPTARG";;
84
85         (?) fatal "Unknown option: -$OPTARG";;
86     esac
87 done
88
89 dbk_lib="${dbk_lib:-/usr/dt/dthelp/dtdocbook}"  # if no -t, use installed dir
90 sgml_dir="${sgml_dir:-${dbk_lib}}"              # if no -s, use -t
91 info_dir="${info_dir:-/usr/dt/infolib}"         # if no -i, use installed dir
92 sgml_cat="${sgml_dir}/SGML"
93 if [[ ${#sgmls} -eq 0 ]] then                  # if no -S, use installed one
94     if [[ -x ${info_dir}/etc/nsgmls ]] then
95         sgmls="${info_dir}/etc/nsgmls"
96     else
97         sgmls="${info_dir}/etc/sgmls"
98     fi
99 fi
100 sgmls="${sgmls:-${info_dir}/etc/sgmls}"         # if no -S, use installed one
101 instant="${instant:-${dbk_lib}/instant}"        # if no -I, use installed one
102 x_locale="${x_locale:-${dbk_lib}/xlate_locale}" # if no -L, use installed one
103 helptag2="${helptag2:-dthelp_htag2}"            # if no -H, use one in PATH
104
105 # Set the environment variables for instant(1) to find its files
106 export TPT_LIB="${dbk_lib}"
107 export LOCALE_DIR="${dbk_lib}/$($x_locale)"
108
109 # Determine whether we are using sgmls or nsgmls
110 parser=`basename $sgmls`
111
112 # If running sgmls, set the environment variable for sgmls(1) to find
113 # its files; if running nsgmls (or anything besides sgmls), set the
114 # environment variable for finding the default catalog - also, if we
115 # are running sgmls, delete any -g options since sgmls doesn't support
116 # catalogs
117 if ([[ "$parser" = sgmls ]]) then
118     export SGML_PATH="${sgml_cat}/%P:${sgml_cat}/%S:%S"
119     catfiles=""
120 elif ([[ "$SGML_CATALOG_FILES" = "" ]]) then
121         export SGML_CATALOG_FILES="${sgml_cat}/catalog"
122     else
123         export SGML_CATALOG_FILES="${SGML_CATALOG_FILES}:${sgml_cat}/catalog"
124 fi
125
126 # Set the environment variable to be picked up inside instant(1) when it
127 # goes to call Tcl.
128 export DBKTCL_DIR="${dbk_lib}/"
129
130
131 # The user asked for help, give it and exit.
132 if (( $help )); then
133     echo "$command_name [options] <file>"
134     echo "options:"
135     echo "    -c         compress an existing SDL file"
136     echo "    -d         decompress an existing SDL file"
137     echo "    -g         specify additional catalog file (repeatable)"
138     echo "    -h         emit this message"
139     echo "    -l         leave <basename>.log in current directory"
140     echo "    -m <maps>  add <maps> to list of SDATA or CMAP files"
141     echo "    -o <file>  use <file> as the output file name"
142     echo "    -r         remove leftover intermediate files"
143     echo "    -s <dir>   docbook.sgml is in <dir>"
144     echo "    -t <dir>   read translation specs, etc., from <dir>"
145     echo "    -u         do not compress during translation"
146     echo "    -v         verbose"
147     echo "    -x         leave intermediate files, for debugging"
148     exit 0
149 fi
150
151
152 # Check for too many input files or none.
153 if (( $OPTIND < $# )); then
154     fatal "Too many names after the options, should only be input file name"
155 elif (( $OPTIND > $# )); then
156     fatal "No input file name specified"
157 fi
158
159
160 # Get the name of the input file.
161 iname=`eval echo \\\${\$OPTIND}`
162
163 # Check for mutually exclusive options.
164 if (( $compress && $decompress )); then
165     fatal "$c_and_d are mutually exclusive."
166 fi
167
168
169 # Get the basename and directory of the input file.
170 basename=`basename $iname`
171 dirname=`dirname $iname`
172
173
174 # Look for an extension on the input file, if it's .sgm (or .sdl for
175 # -c and -d), use it as is, else add the proper extension.
176 if [[ $basename != *.* ]] then
177     if (( $compress || $decompress )); then
178         iname=$iname.sdl
179     else
180         iname=$iname.sgm
181     fi
182 else
183     iname=$dirname/$basename
184     set -A basearray `echo $basename | tr "." " "`
185     basename=${basearray[0]}
186     length=${#basearray[*]}
187     if (( length = $length - 1 )); then
188         if (( $compress || $decompress )); then
189             if [[ ${basearray[$length]} = "sgm" ]] then
190                 fatal "$c_and_d take .sdl as an extension, not .sgm"
191             elif [[  ${basearray[$length]} = "sdl" ]] then
192                 unset basearray[$length]
193             else
194                 iname=$iname.sdl
195             fi
196         else
197             if [[ ${basearray[$length]} = "sdl" ]] then
198                 fatal "Use .sgm or no extension, not .sdl"
199             elif [[  ${basearray[$length]} = "sgm" ]] then
200                 unset basearray[$length]
201             else
202                 iname=$iname.sgm
203             fi
204         fi
205         unset basearray[0]
206         for i in ${basearray[*]}
207         do
208             basename=$basename.$i
209         done
210     fi
211 fi
212
213
214 # If no output file was specified, use <basename>.sdl.
215 if [[ $oname = "" ]] then
216     oname=$basename.sdl
217 fi
218
219
220 # Did the user ask to only remove old files (i.e., "clean")?  If so,
221 # do it and exit.
222 if (( $remove )); then
223     if (( $verbose )); then
224         echo "rm -f $basename.esis"
225     fi
226     rm -f $basename.esis
227     if (( $verbose )); then
228         echo "rm -f $basename.out.sdl"
229     fi
230     rm -f $basename.out.sdl
231     if (( $verbose )); then
232         echo "rm -f $basename.out.snb"
233     fi
234     rm -f $basename.out.snb
235     if (( $verbose )); then
236         echo "rm -f $basename.log"
237     fi
238     rm -f $basename.log
239     if (( $verbose )); then
240         echo "rm -f $basename.out.err"
241     fi
242     rm -f $basename.out.err
243     if (( $verbose )); then
244         echo "rm -f $oname"
245     fi
246     rm -f $oname
247     exit 0
248 fi
249
250 # make sure the error files are clean
251 if (( $verbose )); then
252     echo "rm -f $basename.log"
253 fi
254 rm -f $basename.log
255 if (( $? )); then
256     fatal "Could not remove $basename.log - permissions?"
257 fi
258 if (( $verbose )); then
259     echo "rm -f $basename.out.err"
260 fi
261 rm -f $basename.out.err
262 if (( $? )); then
263     fatal "Could not remove $basename.out.err - permissions?"
264 fi
265
266
267 # Did the user ask for only compression an existing .sdl file?
268 # If so, do it and exit with the return code of dthelp_htag2.
269 if (( $compress )); then
270     if (( $verbose )); then
271         echo $helptag2 -c $iname
272         $helptag2 -c $iname
273         exit $?
274     fi
275     $helptag2 -c $iname 2>/dev/null
276     exit $?
277 fi
278
279
280 # Did the user ask for only decompression an existing .sdl file?
281 # If so, do it and exit with the return code of dthelp_htag2.
282 if (( $decompress )); then
283     if (( $verbose )); then
284         echo $helptag2 -d $iname
285         $helptag2 -d $iname
286         exit $?
287     fi
288     $helptag2 -d $iname 2>/dev/null
289     exit $?
290 fi
291
292
293 # If we get here, we really want to process a .sgm file.  First run
294 # sgmls(1) on it.
295 if [[ -a $basename.esis ]] then
296     if (( $verbose )); then
297         echo rm $basename.esis
298     fi
299     rm $basename.esis
300     if (( $? )); then
301         fatalError "Could not remove $basename.esis - permissions?"
302     fi
303 fi
304 if (( $verbose )); then
305     echo "${sgmls} -deglru$catfiles ${sgml_dir}/docbook.sgml $iname > $basename.esis"
306     ${sgmls} -deglru$catfiles ${sgml_dir}/docbook.sgml $iname > $basename.esis
307     if (( $? )); then
308         if (( !$debug )); then
309             echo "rm -f $basename.esis"
310             rm -f $basename.esis
311         fi
312         fatalError "Error processing $iname by $parser"
313     fi
314 else
315     ${sgmls} -deglru$catfiles ${sgml_dir}/docbook.sgml $iname \
316                                       > $basename.esis 2> $basename.log
317     if (( $? )); then
318         if (( !$debug )); then
319             rm -f $basename.esis
320         fi
321         fatalError "Error processing $iname by $parser"
322     fi
323 fi
324
325
326 # The sgmls(1) run succeeded.  Run instant(1) on the result to create
327 # an unenhanced .sdl file (e.g., no LOIDS yet).
328 if [[ -a $basename.out.sdl ]] then
329     if (( $verbose )); then
330         echo rm -f $basename.out.sdl
331     fi
332     rm -f $basename.out.sdl
333     if (( $? )); then
334         fatalError "Could not remove $basename.out.sdl - permissions?"
335     fi
336     if (( $verbose )); then
337         echo rm -f $basename.out.snb
338     fi
339     rm -f $basename.out.snb
340     if (( $? )); then
341         fatalError "Could not remove $basename.out.snb - permissions?"
342     fi
343 fi
344 if (( $verbose )); then
345     echo "${instant} -o $basename.out.sdl \\\\"
346     if [[ $mapfiles != "" ]] then
347         echo "        $mapfiles \\\\"
348     fi
349     echo "        -c docbook.cmap \\\\"
350     echo "        -t docbook.ts \\\\"
351     echo "        $basename.esis"
352     ${instant} -o $basename.out.sdl  \
353         $mapfiles                 \
354         -c docbook.cmap \
355         -t docbook.ts   \
356         $basename.esis
357     status=$?
358     if ([[ $status -eq 255 ]]) then
359         warn "Warning(s) processing $basename.esis by instant"
360     elif ([[ $status -eq 1 ]]) then
361         if (( !$debug )); then
362             echo "rm -f $basename.esis"
363             rm -f $basename.esis
364             echo "rm -f $basename.out.sdl"
365             rm -f $basename.out.sdl
366             echo "rm -f $basename.out.snb"
367             rm -f $basename.out.snb
368         fi
369         fatalError "Error processing $basename.esis by instant"
370     fi
371 else
372     ${instant} -o $basename.out.sdl  \
373         $mapfiles                 \
374         -c docbook.cmap \
375         -t docbook.ts   \
376         $basename.esis 2> $basename.log
377     status=$?
378     if ([[ $status -eq 255 ]]) then
379         warn "Warning(s) processing $basename.esis by instant"
380     elif ([[ $status -eq 1 ]]) then
381         if (( !$debug )); then
382             rm -f $basename.esis
383             rm -f $basename.out.sdl
384             rm -f $basename.out.snb
385         fi
386         fatalError "Error processing $basename.esis by instant"
387     fi
388 fi
389 if (( !$debug )); then
390     if (( $verbose )); then
391         echo "rm -f $basename.esis"
392     fi
393     rm -f $basename.esis
394 fi
395
396
397 # The run of instant(1) succeeded. Run dthelp_htag2(1) to create the
398 # generated elements (e.g., the list of ids or LOIDS), incorporate the 
399 # table of semantics and styles (TOSS) and do the compression, if
400 # requested.
401 if (( $uncompressed )); then
402     flags=-ot
403 else
404     flags=-otc
405 fi
406 if [[ -a $oname ]] then
407     if (( $verbose )); then
408         echo rm $oname
409     fi
410     rm $oname
411     if (( $? )); then
412         fatalError "Could not remove $oname - permissions?"
413     fi
414 fi
415 if (( $verbose )); then
416     echo "$helptag2 $flags $basename.out.sdl $oname"
417     $helptag2 $flags $basename.out.sdl $oname
418     if (( $? )); then
419         if (( !$debug )); then
420             echo "rm -f $basename.out.sdl"
421             rm -f $basename.out.sdl
422             echo "rm -f $basename.out.snb"
423             rm -f $basename.out.snb
424             echo "rm -f $oname"
425             rm -f $oname
426         fi
427         fatalError "Error processing $basename.out.sdl by $helptag2"
428     fi
429 else
430     $helptag2 $flags $basename.out.sdl $oname 2>/dev/null
431     if (( $? )); then
432         if (( !$debug )); then
433             rm -f $basename.out.sdl
434             rm -f $basename.out.snb
435             rm -f $oname
436         fi
437         fatalError "Error processing $basename.out.sdl by $helptag2"
438     fi
439 fi
440 if (( !$debug )); then
441     if (( $verbose )); then
442         echo "rm -f $basename.out.sdl"
443     fi
444     rm -f $basename.out.sdl
445     if (( $verbose )); then
446         echo "rm -f $basename.out.snb"
447     fi
448     rm -f $basename.out.snb
449 fi
450
451
452 # If we get here, all went well - we know the .log files are writable.
453 if (( !$debug )); then
454     if (( $verbose )); then
455         echo "cat $basename.out.err >> $basename.log"
456     fi
457     cat $basename.out.err >> $basename.log
458     if (( $verbose )); then
459         echo "rm -f $basename.out.err"
460     fi
461     rm -f $basename.out.err
462 fi
463
464 # if we're not in debug mode and the log file wasn't requested, remove it
465 if (( !$debug & !$log )); then
466     if (( $verbose )); then
467         echo "rm -f $basename.log"
468     fi
469     rm -f $basename.log
470 fi
471
472 if (( $verbose )); then
473     echo "$command_name successfully processed $iname"
474 fi
475
476 exit 0