interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / tool / mkshellc.tcl

149 lines · 4746 bytes Raw Blame History
1#!/usr/bin/tclsh
2#
3# Run this script to generate the "shell.c" source file from
4# constituent parts.
5#
6# No arguments are required. This script determines the location
7# of its input files relative to the location of the script itself.
8# This script should be tool/mkshellc.tcl. If the directory holding
9# the script is $DIR, then the component parts are located in $DIR/../src
10# and $DIR/../ext/misc.
11#
12set topdir [file dir [file dir [file normal $argv0]]]
13set out stdout
14fconfigure stdout -translation binary
15if {[lindex $argv 0]!=""} {
16 set output_file [lindex $argv 0]
17 file delete -force $output_file
18 set out [open $output_file wb]
19} else {
20 set output_file {}
21}
22
23############################## FIRST PASS ################################
24# Read through the shell.c.in source file to gather information. Do not
25# yet generate any code
26#
27set in [open $topdir/src/shell.c.in]
28fconfigure $in -translation binary
29set allSource(src/shell.c.in) 1
30set inUsage 0
31set dotcmd {}
32while {1} {
33 set lx [gets $in]
34 if {[eof $in]} break;
35 if {[regexp {^INCLUDE } $lx]} {
36 set cfile [lindex $lx 1]
37 if {[string match ../* $cfile]} {
38 set xfile [string range $cfile 3 end]
39 } else {
40 set xfile "src/$cfile"
41 }
42 set allSource($xfile) 1
43 } elseif {[regexp {^\*\* USAGE:\s+([^\s]+)} $lx all dotcmd]} {
44 set inUsage 1
45 set details [string trim [string range $lx 2 end]]
46 } elseif {$inUsage} {
47 if {![regexp {^\*\*} $lx] || [regexp { DOT-COMMAND: } $lx]} {
48 set inUsage 0
49 set Usage($dotcmd) [string trim $details]
50 } else {
51 append details \n
52 append details [string range [string trimright $lx] 3 end]
53 }
54 }
55}
56
57# Generate dot-command usage text based on the data accumulated in
58# the Usage() array.
59#
60proc generate_usage {out} {
61 global Usage
62 puts $out "/**************************************************************"
63 puts $out "** \"Usage\" help text automatically generated from comments */"
64 puts $out "static const struct \173"
65 puts $out " const char *zCmd; /* Name of the dot-command */"
66 puts $out " const char *zUsage; /* Documentation */"
67 puts $out "\175 aUsage\[\] = \173"
68 foreach dotcmd [array names Usage] {
69 puts $out " \173 \"$dotcmd\","
70 foreach line [split $Usage($dotcmd) \n] {
71 set x [string map [list \\ \\\\ \" \\\"] $line]
72 puts $out "\"$x\\n\""
73 }
74 puts $out " \175,"
75 }
76 puts $out "\175;"
77}
78# generate_usage stderr
79
80###### SECOND PASS #######
81# Make a second pass through shell.c.in to generate the the final
82# output, based on data gathered during the first pass.
83#
84
85puts $out {/*
86** This is the amalgamated source code to the "sqlite3" or "sqlite3.exe"
87** command-line shell (CLI) for SQLite. This file is automatically
88** generated by the tool/mkshellc.tcl script from the following sources:
89**}
90foreach fn [lsort [array names allSource]] {
91 puts $out "** $fn"
92}
93puts $out {**
94** To modify this program, get a copy of the canonical SQLite source tree,
95** edit the src/shell.c.in file and/or some of the other files that are
96** listed above, then rerun the command "make shell.c".
97*/}
98seek $in 0 start
99puts $out "/************************* Begin src/shell.c.in ******************/"
100proc omit_redundant_typedefs {line} {
101 global typedef_seen
102 if {[regexp {^typedef .* ([a-zA-Z0-9_]+);} $line all typename]} {
103 # --------------------\y jimtcl does not support \y
104 if {[info exists typedef_seen($typename)]} {
105 return "/* [string map {/* // */ //} $line] */"
106 }
107 set typedef_seen($typename) 1
108 }
109 return $line
110}
111set iLine 0
112while {1} {
113 set lx [omit_redundant_typedefs [gets $in]]
114 if {[eof $in]} break;
115 incr iLine
116 if {[regexp {^INCLUDE } $lx]} {
117 set cfile [lindex $lx 1]
118 if {[string match ../* $cfile]} {
119 set xfile [string range $cfile 3 end]
120 } else {
121 set xfile "src/$cfile"
122 }
123 puts $out "/************************* Begin $xfile ******************/"
124# puts $out "#line 1 \"$xfile\""
125 set in2 [open $topdir/$xfile]
126 fconfigure $in2 -translation binary
127 while {![eof $in2]} {
128 set lx [omit_redundant_typedefs [gets $in2]]
129 if {[regexp {^# *include "sqlite} $lx]} {
130 set lx "/* $lx */"
131 }
132 if {[regexp {^# *include "windirent.h"} $lx]} {
133 set lx "/* $lx */"
134 }
135 set lx [string map [list __declspec(dllexport) {}] $lx]
136 puts $out $lx
137 }
138 close $in2
139 puts $out "/************************* End $xfile ********************/"
140# puts $out "#line [expr $iLine+1] \"shell.c.in\""
141 } elseif {[regexp {^INSERT-USAGE-TEXT-HERE} $lx]} {
142 generate_usage $out
143 } else {
144 puts $out $lx
145 }
146}
147puts $out "/************************* End src/shell.c.in ******************/"
148close $in
149close $out