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
208
209
210
211
212
213
214
215
216
217
|
import version
import server/forkserver
let forks = newForkServer()
import std/options
import std/os
import config/config
import io/serversocket
import js/javascript
import local/client
import local/term
import utils/strwidth
import utils/twtstr
import chagashi/charset
proc main() =
let params = commandLineParams()
proc version(long: static bool = false): string =
result = "Chawan browser v0.1 "
when defined(debug):
result &= "(debug)"
else:
result &= "(release)"
proc help(i: int) =
let s = version() & """
Usage: cha [options] [URL(s) or file(s)...]
Options:
-- Interpret all following arguments as URLs
-c, --css <stylesheet> Pass stylesheet (e.g. -c 'a{color: blue}')
-d, --dump Print page to stdout
-h, --help Print this usage message
-o, --opt <config> Pass config options (e.g. -o 'page.q="quit()"')
-r, --run <script/file> Run passed script or file
-v, --version Print version information
-C, --config <file> Override config path
-I, --input-charset <enc> Specify document charset
-M, --monochrome Set color-mode to 'monochrome'
-O, --display-charset <enc> Specify display charset
-T, --type <type> Specify content mime type
-V, --visual Visual startup mode"""
if i == 0:
stdout.write(s & '\n')
else:
stderr.write(s & '\n')
quit(i)
var ctype = none(string)
var cs = CHARSET_UNKNOWN
var pages: seq[string]
var dump = false
var visual = false
var escape_all = false
var opts: seq[string]
var stylesheet = ""
var configPath = none(string)
var i = 0
while i < params.len:
let param = params[i]
if escape_all: # after --
pages.add(param)
inc i
continue
proc getnext(): string =
inc i
if i < params.len:
return params[i]
help(1)
proc pconfig() =
configPath = some(getnext())
proc pversion() =
echo version(true)
quit(0)
proc pmonochrome() =
opts.add("display.color-mode = monochrome")
proc pvisual() =
visual = true
proc ptype() =
ctype = some(getnext())
proc pgetCharset(): Charset =
let s = getnext()
let cs = getCharset(s)
if cs == CHARSET_UNKNOWN:
stderr.write("Unknown charset " & s & "\n")
quit(1)
return cs
proc pinput_charset() =
cs = pgetCharset()
proc poutput_charset() =
opts.add("encoding.display-charset = '" & $pgetCharset() & "'")
proc pdump() =
dump = true
proc pcss() =
stylesheet &= getnext()
proc popt() =
opts.add(getnext())
proc phelp() =
help(0)
proc prun() =
let script = dqEscape(getnext())
opts.add("start.startup-script = \"\"\"" & script & "\"\"\"")
opts.add("start.headless = true")
dump = true
if param.len == 0:
inc i
continue
const NeedsNextParam = {'C', 'I', 'O', 'T', 'c', 'o', 'r'}
if param[0] == '-':
if param.len == 1:
# If param == "-", i.e. it is a single dash, then ignore it.
# (Some programs use single-dash to read from stdin, but we do that
# automatically when stdin is not a tty. So ignoring it entirely
# is probably for the best.)
inc i
continue
if param[1] != '-':
for j in 1 ..< param.len:
if j != param.high and param[j] in NeedsNextParam:
# expecting next parameter, but not the last char...
help(1)
case param[j]
of 'C': pconfig()
of 'I': pinput_charset()
of 'M': pmonochrome()
of 'O': poutput_charset()
of 'T': ptype()
of 'V': pvisual()
of 'c': pcss()
of 'd': pdump()
of 'h': phelp()
of 'o': popt()
of 'r': prun()
of 'v': pversion()
else: help(1)
else:
case param
of "--config": pconfig()
of "--input-charset": pinput_charset()
of "--monochrome": pmonochrome()
of "--output-charset": poutput_charset()
of "--type": ptype()
of "--visual": pvisual()
of "--css": pcss()
of "--dump": pdump()
of "--help": phelp()
of "--opt": popt()
of "--run": prun()
of "--version": pversion()
of "--": escape_all = true
else: help(1)
else:
pages.add(param)
inc i
let jsrt = newJSRuntime()
let jsctx = jsrt.newJSContext()
let config = readConfig(configPath, jsctx)
var warnings = newSeq[string]()
for opt in opts:
let res = config.parseConfig(getCurrentDir(), opt, laxnames = true)
if not res.success:
stderr.write(res.errorMsg)
quit(1)
config.css.stylesheet &= stylesheet
set_cjk_ambiguous(config.display.double_width_ambiguous)
if pages.len == 0 and stdin.isatty():
if visual:
pages.add(config.start.visual_home)
else:
let http = getEnv("HTTP_HOME")
if http != "": pages.add(http)
else:
let www = getEnv("WWW_HOME")
if www != "": pages.add(www)
if pages.len == 0 and not config.start.headless:
if stdin.isatty:
help(1)
forks.loadForkServerConfig(config)
SocketDirectory = config.external.tmpdir
let c = newClient(config, forks, jsctx)
try:
c.launchClient(pages, ctype, cs, dump, warnings)
except CatchableError:
c.flushConsole()
raise
main()
|