TIVI-153: add as dependency for iputils
[profile/ivi/docbook-utils.git] / bin / jw.in
1 #! /bin/sh
2 # Jade Wrapper
3 # Script to convert a SGML file into some other format
4 # Send any comments to Eric Bischoff <eric@caldera.de>
5 # This program is under GPL license. See LICENSE file for details.
6
7 # Set help message
8 SGML_HELP_MESSAGE="Usage: `basename $0` [<options>] <sgml_file>\n\
9 where <options> are:\n\
10 \040 -f|--frontend <frontend>: \t Specify the frontend (source format)\n\
11 \040 \t\t\t\t (default is docbook)\n\
12 \040 -b|--backend <backend>: \t Specify the backend (destination format)\n\
13 \040 \t\t\t\t (default is html)\n\
14 \040 -c|--cat <file>: \t\t Specify an extra SGML open catalog\n\
15 \040 -n|--nostd: \t\t\t Do not use the standard SGML open catalogs\n\
16 \040 -d|--dsl <file>|default|none:\t Specify an alternate style sheet\n\
17 \040 \t\t\t\t (default is to use the default stylesheet)\n\
18 \040 -l|--dcl <file>: \t\t Specify an alternate SGML declaration\n\
19 \040 \t\t\t\t (usual ones like xml.dcl get detected automatically)\n\
20 \040 -s|--sgmlbase <path>: \t Change base directory for SGML distribution\n\
21 \040 \t\t\t\t (usually @prefix@/share/sgml)\n\
22 \040 -p|--parser <program>: \t Specify the parser if several are installed\n\
23 \040 \t\t\t\t  (jade or openjade)\n\
24 \040 -o|--output <directory>: \t Set output directory\n\
25 \040 -u|--nochunks: \t\t Output only one big file\n\
26 \040 \t\t\t\t (overrides the stylesheet settings)\n\
27 \040 -i|--include <section>: \t Specify a SGML marked section to include\n\
28 \040 \t\t\t\t (should be marked as \"ignore\" in the SGML text)\n\
29 \040 -w|--warning <warning_type>|list: Control warnings or display the allowed warning types\n\
30 \040 -e|--errors <error_type>|list: Control errors or display the allowed error types\n\
31 \040 -h|--help: \t\t\t Print this help message and exit\n\
32 \040 -V <variable[=value]>: \t Set a variable\n\
33 \040 -v|--version: \t\t Print the version and exit"
34
35 # Set list of warning types
36 SGML_WARNINGS_LIST="\n\
37 \040 xml \t\t Warn about constructs that are not allowed by XML\n\
38 \040 mixed \t Warn about mixed content models that do not allow #pcdata anywhere\n\
39 \040 sgmldecl \t Warn about various dubious constructions in the SGML declaration\n\
40 \040 should \t Warn about various recommendations made in ISO 8879 that the document does not comply with\n\
41 \040 default \t Warn about defaulted references\n\
42 \040 duplicate \t Warn about duplicate entity declarations\n\
43 \040 undefined \t Warn about undefined elements: elements used in the DTD but not defined\n\
44 \040 unclosed \t Warn about unclosed start and end-tags\n\
45 \040 empty \t Warn about empty start and end-tags\n\
46 \040 net \t\t Warn about net-enabling start-tags and null end-tags\n\
47 \040 min-tag \t Warn about minimized start and end-tags. Equivalent to: unclosed, empty and net\n\
48 \040 unused-map \t Warn about unused short reference maps\n\
49 \040 unused-param \t Warn about parameter entities that are defined but not used in a DTD\n\
50 \040 notation-sysid Warn about notations for which no system identifier could be generated\n\
51 \040 all \t\t Equivalent to: mixed, should, default, undefined, sgmldecl, unused-map, unused-param, empty and unclosed\n\n\
52 \040 A warning can be disabled by using its name prefixed with no-. Example: -w all -w no-duplicate"
53
54 # Set list of error types
55 SGML_ERRORS_LIST="\n\
56 \040 no-idref \t No error for an ID reference value which no element has as its ID\n\
57 \040 no-significant No errors for characters not significant in the reference concrete syntax\n\
58 \040 no-valid \t Do not require the document to be type-valid"
59
60 # Get name of main SGML configuration file
61 which sgmlwhich >/dev/null 2>/dev/null
62 if [ $? -eq 0 ]
63 then
64   SGML_CONF=`sgmlwhich`
65 else
66   SGML_CONF="/etc/sgml/sgml.conf"
67 fi
68
69 # Set version message
70 SGML_VERSION_MESSAGE="DocBook-utils version @VERSION@ (jw version 1.1)"
71
72 # Set option: use standard catalogs
73 SGML_STANDARD_CATALOGS="yes"
74
75 # Set path to user SGML catalogs
76 SGML_EXTRA_CATALOGS=""
77
78 # Set SGML base directory and centralized catalogs directory
79 SGML_BASE_DIR="@prefix@/share/sgml"
80 SGML_CATALOGS_DIR="/etc/sgml"
81 if [ -f "$SGML_CONF" ]
82 then
83   RE='^[:space:]*SGML_BASE_DIR[:space:]*=[:space:]*'
84   SGML_BASE_DIR=`grep $RE $SGML_CONF | sed "s/$RE//"`
85   RE='^[:space:]*SGML_CATALOGS_DIR[:space:]*=[:space:]*'
86   SGML_CATALOGS_DIR=`grep $RE $SGML_CONF | sed "s/$RE//"`
87 fi
88
89 # Set frontend to use
90 SGML_FRONTEND="$SGML_BASE_DIR/docbook/utils-@VERSION@/frontends/docbook"
91
92 # Set backend to use
93 SGML_BACKEND="$SGML_BASE_DIR/docbook/utils-@VERSION@/backends/html"
94
95 # Set main stylesheet
96 SGML_STYLESHEET="default"
97
98 # Set flavour of jade parser to use
99 SGML_JADE=""
100
101 # Set SGML declaration and file to process
102 SGML_DECL=""
103 SGML_FILE=""
104
105 # Set output and current directories
106 SGML_OUTPUT_DIRECTORY="."
107 SGML_CURRENT_DIRECTORY=`pwd`
108
109 # Set of options to set
110 SGML_OPTIONS=""
111
112 # Set list of "include" marked sections
113 SGML_INCLUDE=""
114
115 # Set list of warning types and errors types
116 SGML_WARNINGS=""
117
118 # Process options
119 while [ $# -gt 0 ]
120 do case $1 in
121   -f|--frontend) case "$2" in
122                   /*) SGML_FRONTEND="$2" ;;
123                    *) d=$SGML_BASE_DIR/docbook/utils-@VERSION@/frontends
124                       SGML_FRONTEND="$d/$2" ;;
125                 esac
126                 shift 2
127                 ;;
128   -b|--backend) case "$2" in
129                   /*) SGML_BACKEND="$2" ;;
130                    *) d=$SGML_BASE_DIR/docbook/utils-@VERSION@/backends
131                       SGML_BACKEND="$d/$2" ;;
132                 esac
133                 shift 2
134                 ;;
135   -c|--cat)     case "$2" in
136                   /*) SGML_EXTRA_CATALOGS="$SGML_EXTRA_CATALOGS:$2" ;;
137    *) SGML_EXTRA_CATALOGS="$SGML_EXTRA_CATALOGS:$SGML_CURRENT_DIRECTORY/$2" ;;
138                 esac
139                 shift 2
140                 ;;
141   -d|--dsl)     case "$2" in
142                   /*|none|default) SGML_STYLESHEET="$2" ;;
143                    *) SGML_STYLESHEET="$SGML_CURRENT_DIRECTORY/$2" ;;
144                 esac
145                 shift 2
146                 ;;
147   -l|--dcl)     case "$2" in
148                   /*) SGML_DECL="$2" ;;
149                    *) SGML_DECL="$SGML_CURRENT_DIRECTORY/$2" ;;
150                 esac
151                 shift 2
152                 ;;
153   -n|--nostd)   SGML_STANDARD_CATALOGS="no"
154                 shift 1
155                 ;;
156   -s|--sgmlbase) case "$2" in
157                   /*) SGML_BASE_DIR="$2" ;;
158                    *) SGML_BASE_DIR="$SGML_CURRENT_DIRECTORY/$2" ;;
159                 esac
160                 shift 2
161                 ;;
162   -p|--parser)  case "$2" in
163                   /*) SGML_JADE="$2" ;;
164                    *) SGML_JADE="$SGML_CURRENT_DIRECTORY/$2" ;;
165                 esac
166                 shift 2
167                 ;;
168   -o|--output)  case "$2" in
169                   /*) SGML_OUTPUT_DIRECTORY="$2" ;;
170                    *) SGML_OUTPUT_DIRECTORY="$SGML_CURRENT_DIRECTORY/$2" ;;
171                 esac
172                 shift 2
173                 ;;
174   -u|--nochunks) SGML_OPTIONS="$SGML_OPTIONS -V nochunks"
175                 shift 1
176                 ;;
177   -V)           SGML_OPTIONS="$SGML_OPTIONS -V $2"
178                 shift 2
179                 ;;
180   -i|--include) case $2 in
181                   print|html)   echo "The marked section named \"$2\" is reserved for internal use by `basename $0`." >&2
182                                 exit 1
183                                 ;;
184                 esac
185                 SGML_INCLUDE="$SGML_INCLUDE -i $2"
186                 shift 2
187                 ;;
188   -w|--warning) case $2 in
189                   list) echo -e "List of allowed warning types (multiple -w options are allowed):\n"
190                         echo -e $SGML_WARNINGS_LIST
191                         exit 0
192                         ;;
193                   xml|mixed|sgmldecl|should|default|duplicate|undefined|unclosed|empty|net|min-tag|unused-map|unused-param|notation-sysid|all|no-xml|no-mixed|no-sgmldecl|no-should|no-default|no-duplicate|no-undefined|no-unclosed|no-empty|no-net|no-min-tag|no-unused-map|no-unused-param|no-notation-sysid) \
194                         SGML_WARNINGS="$SGML_WARNINGS -w $2"
195                         shift 2
196                         ;;
197                   *)    echo "Unknown warning type \"$2\". Allowed warning types are:" >&2
198                         echo -e $SGML_WARNINGS_LIST >&2
199                         exit 1
200                         ;;
201                 esac
202                 ;;
203   -e|--error)   case $2 in
204                   list) echo -e "List of allowed error types (multiple -e options are allowed):\n"
205                         echo -e $SGML_ERRORS_LIST
206                         exit 0
207                         ;;
208                   no-idref|no-significant|no-valid) \
209                         SGML_WARNINGS="$SGML_WARNINGS -w $2"
210                         shift 2
211                         ;;
212                   *)    echo "Unknown error type \"$2\". Allowed error types are:" >&2
213                         echo -e $SGML_ERRORS_LIST >&2
214                         exit 1
215                         ;;
216                 esac
217                 ;;
218   -h|--help)    echo -e "`basename $0` - Jade Wrapper\n"
219                 echo -e "$SGML_HELP_MESSAGE"
220                 exit 0
221                 ;;
222   -v|--version) echo -e "$SGML_VERSION_MESSAGE"
223                 exit 0
224                 ;;
225   -*)           echo -e "$SGML_HELP_MESSAGE" >&2
226                 exit 1
227                 ;;
228   *)            if [ -z "$SGML_FILE" ]
229                 then
230                   case "$1" in
231                     /*) SGML_FILE="$1" ;;
232                      *) SGML_FILE="$SGML_CURRENT_DIRECTORY/$1" ;;
233                   esac
234                   shift 1
235                 else
236                   echo -e "$SGML_HELP_MESSAGE" >&2
237                   exit 1
238                 fi
239                 ;;
240   esac
241 done
242
243 # Check that we have a frontend
244 if [ -z "$SGML_FRONTEND" ]
245 then
246   echo -e $SGML_HELP_MESSAGE >&2
247   exit 1
248 fi
249 if [ ! -s $SGML_FRONTEND ]
250 then
251   echo "`basename $0`: There is no frontend called \"$SGML_FRONTEND\"." >&2
252   exit 2
253 fi
254
255 # Check that we have a backend
256 if [ -z "$SGML_BACKEND" ]
257 then
258   echo -e $SGML_HELP_MESSAGE >&2
259   exit 1
260 fi
261 if [ ! -s $SGML_BACKEND ]
262 then
263   echo "`basename $0`: There is no backend called \"$SGML_BACKEND\"." >&2
264   exit 2
265 fi
266
267 # Check that we have a file to process
268 if [ -z "$SGML_FILE" ]
269 then
270   echo -e $SGML_HELP_MESSAGE >&2
271   exit 1
272 fi
273 if [ ! -s $SGML_FILE ]
274 then
275   echo "`basename $0`: Cannot open \"$SGML_FILE\"." >&2
276   exit 3
277 fi
278
279 # Separate the file name and the extension (if any)
280 SGML_FILE_NAME=`basename $SGML_FILE`
281 SGML_FILE_NAME=${SGML_FILE_NAME%.*}
282
283 # Determine if we are in an XML file
284 SGML_XML=`head "$SGML_FILE" | grep "<?xml.*?>" | wc -l`
285 if [ $SGML_XML -eq 1 ]; then
286   SGML_XML="xml"
287 else
288   SGML_XML="sgml"
289 fi
290
291 # Use SGML declaration for XML if needed
292 if [ "$SGML_DECL" = "" -a "$SGML_XML" = "xml" ]
293 then SGML_DECL=${SGML_BASE_DIR}/xml.dcl
294 fi
295
296 # Try to find the SGML normalizer
297 if [ -z "$SGML_NORM" ]
298 then
299   SGML_NORM=`which sgmlnorm 2>/dev/null`
300   if [ -z "SGML_NORM" ]
301   then
302     SGML_NORM=`which osgmlnorm 2>/dev/null`
303   fi
304 fi
305
306 # Set path to SGML catalogs (first try centralized catalog)
307 case $SGML_STANDARD_CATALOGS in
308   yes)  export SGML_CATALOGS_DIR SGML_FILE SGML_XML SGML_NORM
309         SGML_CENTRALIZED_CATALOG=`sh $SGML_FRONTEND centralized-catalog`
310         if [ -s $SGML_CENTRALIZED_CATALOG ]
311         then
312           SGML_CATALOG_FILES=$SGML_CENTRALIZED_CATALOG
313         else
314           SGML_CATALOG_FILES=`find $SGML_BASE_DIR -name catalog`
315           SGML_CATALOG_FILES=`echo "$SGML_CATALOG_FILES" | tr [:space:] :`
316         fi
317         ;;
318   no)   SGML_CATALOG_FILES=""
319         ;;
320 esac
321 SGML_CATALOG_FILES=${SGML_CATALOG_FILES}${SGML_EXTRA_CATALOGS}
322 SGML_CATALOG_FILES=`echo "$SGML_CATALOG_FILES" | sed 's/^://;s/:$//'`
323 if [ -z "$SGML_CATALOG_FILES" ]
324 then
325   echo "`basename $0`: Please specify at least one catalog" >&2
326   exit 4
327 fi
328
329 # Determine the backend type
330 case $SGML_BACKEND in
331   *html|*txt) \
332         SGML_TYPE=html
333         ;;
334   *)    SGML_TYPE=print
335         ;;
336 esac
337
338 # Determine which stylesheet to use
339 # (run the frontend to know it if the default stylesheet or no custom stylesheet is desired)
340 if [ "$SGML_STYLESHEET" = "default" -o "$SGML_STYLESHEET" = "none" ]
341 then
342   export SGML_BASE_DIR SGML_TYPE SGML_STYLESHEET
343   SGML_STYLESHEET=`sh $SGML_FRONTEND style-sheet`
344   SGML_RETURN=$?
345   if [ $SGML_RETURN -gt 0 ]
346   then exit `expr 7 + $SGML_RETURN`
347   fi
348 fi
349 if [ -z "$SGML_STYLESHEET" ]
350 then
351   echo "`basename $0`: Please specify at least one stylesheet" >&2
352   exit 5
353 fi
354
355 case "$SGML_OPTIONS" in
356   *paper-type*)
357         ;;
358   *)
359         # Set the paper size to A4 if the locale suggests so
360         papersize=
361         if [ -x /usr/bin/locale ]
362         then
363           h=$(/usr/bin/locale LC_PAPER 2>/dev/null | head -n 1)
364           if [ "$h" = 297 ]
365           then
366             papersize=A4
367           fi
368         fi
369         if [ -n "$papersize" ]
370         then
371           SGML_OPTIONS="-V paper-type=$papersize $SGML_OPTIONS"
372         fi
373         ;;
374 esac
375
376 # Choose a parser
377 if [ -z "$SGML_JADE" ]
378 then
379   which jade >/dev/null 2>/dev/null
380   if [ $? -eq 0 ]
381   then
382     SGML_JADE="jade"
383   else
384     which openjade >/dev/null 2>/dev/null
385     if [ $? -eq 0 ]
386     then 
387       SGML_JADE="openjade"
388     else
389       echo "`basename $0`: No parser available" >&2
390       exit 6
391     fi
392   fi
393 else
394   which $SGML_JADE >/dev/null 2>/dev/null
395   if [ $? -ne 0 ]
396   then 
397     echo "`basename $0`: parser $SGML_JADE is not available" >&2
398     exit 6
399   fi
400 fi
401
402 # Create output directory if not available
403 if [ -z "$SGML_OUTPUT_DIRECTORY" ]
404 then
405   echo -e $SGML_HELP_MESSAGE >&2
406   exit 1
407 fi
408 if [ ! -d $SGML_OUTPUT_DIRECTORY ]
409 then
410   mkdir $SGML_OUTPUT_DIRECTORY 2>/dev/null
411   if [ $? -ne 0 ]
412   then
413     echo "`basename $0`: Could not create \"$SGML_OUTPUT_DIRECTORY\" output directory" >&2
414     exit 7
415   fi
416 fi
417
418 # Prepare the parser's arguments
419 SGML_ARGUMENTS="$SGML_INCLUDE -i $SGML_TYPE -d $SGML_STYLESHEET $SGML_OPTIONS $SGML_WARNINGS $SGML_DECL $SGML_FILE"
420
421 # Call the backend
422 echo "Using catalogs: `echo $SGML_CATALOG_FILES | sed 's/:/, /g'`"
423 echo "Using stylesheet: $SGML_STYLESHEET"
424 echo "Working on: $SGML_FILE"
425 cd $SGML_OUTPUT_DIRECTORY
426 export SGML_JADE SGML_FILE_NAME SGML_ARGUMENTS
427 export SGML_CATALOG_FILES SGML_BASE_DIR SGML_FILE SGML_STYLESHEET
428 NOCHUNKS=`echo $SGML_OPTIONS | grep nochunks`
429 if [ -z "$NOCHUNKS" ]
430 then
431         sh $SGML_BACKEND
432 else
433         sh $SGML_BACKEND >$SGML_FILE_NAME.html
434 fi
435 SGML_RETURN=$?
436 cd $SGML_CURRENT_DIRECTORY
437
438 if [ $SGML_RETURN -gt 0 ]
439 then exit `expr 7 + $SGML_RETURN`
440 fi
441 echo "Done."
442 exit 0