1#!/bin/sh
2# -*- tcl -*- \
3exec tclsh "$0" ${1+"$@"}
4
5lappend auto_path [file dirname [file dirname [info script]]]
6
7if 0 {
8    puts auto_path=\n\t[join $auto_path \n\t]
9    catch {puts tcl_pkgPath=\n\t[join $tcl_pkgPath \n\t]}
10    catch {puts tcl_libPath=\n\t[join $tcl_libPath \n\t]}
11
12    puts [package require doctools]
13    exit
14}
15
16package require doctools
17
18
19
20# ---------------------------------------------------------------------
21#  1. Handle command line options, input and output
22#  2. Initialize a doctools object.
23#  3. Run the input through the object.
24#  4. Write output.
25# ---------------------------------------------------------------------
26
27proc usage {{exitstate 1}} {
28    global argv0
29    puts "Usage: $argv0\
30	    ?-h|--help|-help|-??\
31	    ?-help-fmt|--help-fmt?\
32	    ?-module module?\
33	    ?-deprecated?\
34	    ?-copyright text?\
35	    format in|- ?out|-?"
36    exit $exitstate
37}
38
39# ---------------------------------------------------------------------
40
41proc fmthelp {} {
42    # Tcllib FR #527029: short reference of formatting commands.
43
44    global argv0
45    puts "$argv0 [doctools::help]"
46    exit 0
47}
48
49# ---------------------------------------------------------------------
50# 1. Handle command line options, input and output
51
52proc cmdline {} {
53    global argv0 argv format in out extmodule deprecated copyright
54
55    set copyright ""
56    set extmodule ""
57    set deprecated 0
58
59    while {[string match -* [set opt [lindex $argv 0]]]} {
60	switch -exact -- $opt {
61	    -module {
62		set extmodule [lindex $argv 1]
63		set argv [lrange $argv 2 end]
64		continue
65	    }
66	    -copyright {
67		set copyright [lindex $argv 1]
68		set argv [lrange $argv 2 end]
69		continue
70	    }
71	    -deprecated {
72		set deprecated 1
73		set argv [lrange $argv 1 end]
74	    }
75	    -help - -h - --help - -? {
76		# Tcllib FR #527029
77		usage 0
78	    }
79	    -help-fmt - --help-fmt {
80		# Tcllib FR #527029
81		fmthelp
82	    }
83	    default {
84		# Unknown option
85		usage
86	    }
87	}
88    }
89
90    if {[llength $argv] < 3} {
91	usage
92    }
93    foreach {format in out} $argv break
94
95    if {$format == {} || $in == {}} {
96	usage
97    }
98    if {$out == {}} {set out -}
99    return $format
100}
101
102# ---------------------------------------------------------------------
103#  3. Read input. Also providing the namespace with file information.
104
105proc get_input {} {
106    global in
107    if {[string equal $in -]} {
108	return [read stdin]
109    } else {
110	set if [open $in r]
111	set text [read $if]
112	close $if
113	return $text
114    }
115}
116
117# ---------------------------------------------------------------------
118# 4. Write output.
119
120proc write_out {text} {
121    global out
122    if {[string equal $out -]} {
123	puts -nonewline stdout $text
124    } else {
125	set of [open $out w]
126	puts -nonewline $of $text
127	close $of
128    }
129}
130
131
132# ---------------------------------------------------------------------
133# Get it all together
134
135proc main {} {
136    global format deprecated extmodule in copyright
137
138    #if {[catch {}
139	cmdline
140
141	::doctools::new dt -format $format -deprecated $deprecated -file $in
142	if {$extmodule != {}} {
143	    dt configure -module $extmodule
144	}
145	if {$copyright != {}} {
146	    dt configure -copyright $copyright
147	}
148
149	write_out [dt format [get_input]]
150
151	set warnings [dt warnings]
152	if {[llength $warnings] > 0} {
153	    puts stderr [join $warnings \n]
154	}
155
156	#{} msg]} {}
157	#puts stderr "Execution error: $msg"
158    #{}
159    return
160}
161
162
163# ---------------------------------------------------------------------
164main
165exit
166