124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
...
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
...
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
...
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
...
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
local q = quote
var [val]
[exp]
in val end
return q
end);
proc = {
exit = terralib.externfunction('exit', int -> {});
getenv = terralib.externfunction('getenv', rawstring -> rawstring);
};
io = {
send = terralib.externfunction('write', {int, rawstring, intptr} -> ptrdiff);
recv = terralib.externfunction('read', {int, rawstring, intptr} -> ptrdiff);
say = macro(function(msg) return `lib.io.send(2, msg, [#(msg:asvalue())]) end);
fmt = terralib.externfunction('printf',
terralib.types.funcpointer({rawstring},{int},true));
};
str = { sz = terralib.externfunction('strlen', rawstring -> intptr) };
copy = function(tbl)
local new = {}
................................................................................
osclock = terralib.includec 'time.h';
}
if config.posix then
lib.uio = terralib.includec 'sys/uio.h';
lib.emit = lib.emitv -- use more efficient call where available
else lib.emit = lib.emit_unitary end
local starttime = global(lib.osclock.time_t)
local lastnoisetime = global(lib.osclock.time_t)
local noise = global(uint8,1)
local noise_header = function(code,txt,mod)
if mod then
return string.format('\27[%s;1m(%s %s)\27[m ', code,mod,txt)
else
return string.format('\27[%s;1m(%s)\27[m ', code,txt)
end
end
local terra timehdr()
var now = lib.osclock.time(nil)
var diff = now - lastnoisetime
if diff > 30 then -- print cur time
lastnoisetime = now
var curtime: int8[26]
lib.osclock.ctime_r(&now, &curtime[0])
for i=0,26 do if curtime[i] == @'\n' then curtime[i] = 0 break end end -- :/
[ lib.emit(false, 2, '\27[1m[', `&curtime[0], ']\27[;36m\n +00 ') ]
else -- print time since last msg
var dfs = arrayof(int8, 0x30 + diff/10, 0x30 + diff%10, 0x20, 0)
[ lib.emit(false, 2, ' \27[36m+', `&dfs[0]) ]
................................................................................
if level >= 3 and config.debug == false then
return macro(function(...) return {} end)
end
return macro(function(...)
local fn = (...).filename
local ln = tostring((...).linenumber)
local dbgtag = string.format('\27[35m · \27[34m%s:\27[1m%s\27[m\n', fn,ln)
local q = lib.emit(level < 3 and true or dbgtag, 2, noise_header(code,n), ...)
return quote
--lib.io.fmt(['attempting to emit at ' .. fn..':'..ln.. '\n'])
if noise >= level then timehdr(); [q] end end
end);
end
lib.dbg = defrep(3,'debug', '32')
lib.report = defrep(2,'info', '35')
lib.warn = defrep(1,'warn', '33')
lib.bail = macro(function(...)
local q = lib.emit(true, 2, noise_header('31','fatal'), ...)
return quote
timehdr(); [q]
lib.proc.exit(1)
end
end);
lib.stat = terralib.memoize(function(ty)
local n = struct {
ok: bool
union {
................................................................................
lib.pk = lib.loadlib('mbedtls','mbedtls/pk.h')
lib.md = lib.loadlib('mbedtls','mbedtls/md.h')
lib.b64 = lib.loadlib('mbedtls','mbedtls/base64.h')
lib.net = lib.loadlib('mongoose','mongoose.h')
lib.pq = lib.loadlib('libpq','libpq-fe.h')
lib.load {
'mem', 'math', 'str', 'file', 'crypt';
'http', 'html', 'session', 'tpl', 'store';
'smackdown'; -- md-alike parser
}
local be = {}
for _, b in pairs(config.backends) do
................................................................................
}
do
local p = string.format('parsav: %s\nbuilt on %s\n', config.build.str, config.build.when)
terra version() lib.io.send(1, p, [#p]) end
end
terra lib.noise_init(default_level: uint)
starttime = lib.osclock.time(nil)
lastnoisetime = 0
var n = lib.proc.getenv('parsav_noise')
if n ~= nil then
if n[0] >= 0x30 and n[0] <= 0x39 and n[1] == 0 then
noise = n[0] - 0x30
return
end
end
noise = default_level
end
lib.load{'mgtool'}
local options = lib.cmdparse {
version = {'V', 'display information about the binary build and exit'};
verbose = {'v', 'increase logging verbosity', inc=1};
quiet = {'q', 'do not print to standard out'};
help = {'h', 'display this list'};
backend_file = {'B', 'init from specified backend file', consume=1};
static_dir = {'S', 'directory with overrides for static content', consume=1};
builtin_data = {'D', 'do not load static content overrides at runtime under any circumstances'};
instance = {'i', 'set an instance name to make it easier to control multiple daemons', consume = 1};
}
local static_setup = quote end
local mapin = quote end
local odir = symbol(rawstring)
local pathbuf = symbol(lib.str.acc)
................................................................................
end or quote return end
] end
var [pathbuf] defer pathbuf:free()
pathbuf:compose(odir,'/')
[mapin]
end
local terra entry_daemon(argc: int, argv: &rawstring): int
if argc < 1 then lib.bail('bad invocation!') end
lib.noise_init(1)
[lib.init]
-- shut mongoose the fuck up
lib.net.mg_log_set_callback([terra(msg: &opaque, sz: int, u: &opaque) end], nil)
var srv: lib.srv.overlord
do var mode: options
................................................................................
mode:parse(argc,argv) defer mode:free()
static_init(&mode)
if mode.version then version() return 0 end
if mode.help then
[ lib.emit(true, 1, 'usage: ',`argv[0],' ', options.helptxt.flags, ' [<args>…]', options.helptxt.opts) ]
return 0
end
var cnf: rawstring
if mode.backend_file ~= nil
then cnf = @mode.backend_file
else cnf = lib.proc.getenv('parsav_backend_file')
end
if cnf == nil then cnf = [config.prefix_conf .. "backend.conf"] end
srv:setup(cnf)
srv:start(lib.trn(mode.instance ~= nil, @mode.instance, nil))
end
lib.report('listening for requests')
while true do
srv:poll()
end
srv:shutdown()
return 0
end
local bflag = function(long,short)
if short and util.has(buildopts, short) then return true end
if long and util.has(buildopts, long) then return true end
return false
end
|
>
>
>
>
>
>
>
>
|
|
<
|
|
|
|
|
|
|
|
>
|
|
|
|
>
|
>
|
>
>
|
|
|
|
|
|
>
>
>
>
>
|
>
|
>
>
>
>
>
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
|
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
...
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
...
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
218
219
220
...
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
...
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
local q = quote
var [val]
[exp]
in val end
return q
end);
proc = {
fork = terralib.externfunction('fork', {} -> int);
daemonize = terralib.externfunction('daemon', {int,int} -> {});
exit = terralib.externfunction('exit', int -> {});
getenv = terralib.externfunction('getenv', rawstring -> rawstring);
exec = terralib.externfunction('execv', {rawstring,&rawstring} -> int);
execp = terralib.externfunction('execvp', {rawstring,&rawstring} -> int);
};
io = {
send = terralib.externfunction('write', {int, rawstring, intptr} -> ptrdiff);
recv = terralib.externfunction('read', {int, rawstring, intptr} -> ptrdiff);
close = terralib.externfunction('close', {int} -> int);
say = macro(function(msg) return `lib.io.send(2, msg, [#(msg:asvalue())]) end);
fmt = terralib.externfunction('printf',
terralib.types.funcpointer({rawstring},{int},true));
};
str = { sz = terralib.externfunction('strlen', rawstring -> intptr) };
copy = function(tbl)
local new = {}
................................................................................
osclock = terralib.includec 'time.h';
}
if config.posix then
lib.uio = terralib.includec 'sys/uio.h';
lib.emit = lib.emitv -- use more efficient call where available
else lib.emit = lib.emit_unitary end
lib.noise = {
level = global(uint8,1);
starttime = global(lib.osclock.time_t);
lasttime = global(lib.osclock.time_t);
header = function(code,txt,mod)
if mod then
return string.format('\27[%s;1m(%s %s)\27[m ', code,mod,txt)
else
return string.format('\27[%s;1m(%s)\27[m ', code,txt)
end
end;
}
local terra timehdr()
var now = lib.osclock.time(nil)
var diff = now - lib.noise.lasttime
if diff > 30 then -- print cur time
lib.noise.lasttime = now
var curtime: int8[26]
lib.osclock.ctime_r(&now, &curtime[0])
for i=0,26 do if curtime[i] == @'\n' then curtime[i] = 0 break end end -- :/
[ lib.emit(false, 2, '\27[1m[', `&curtime[0], ']\27[;36m\n +00 ') ]
else -- print time since last msg
var dfs = arrayof(int8, 0x30 + diff/10, 0x30 + diff%10, 0x20, 0)
[ lib.emit(false, 2, ' \27[36m+', `&dfs[0]) ]
................................................................................
if level >= 3 and config.debug == false then
return macro(function(...) return {} end)
end
return macro(function(...)
local fn = (...).filename
local ln = tostring((...).linenumber)
local dbgtag = string.format('\27[35m · \27[34m%s:\27[1m%s\27[m\n', fn,ln)
local q = lib.emit(level < 3 and true or dbgtag, 2, lib.noise.header(code,n), ...)
return quote
--lib.io.fmt(['attempting to emit at ' .. fn..':'..ln.. '\n'])
if lib.noise.level >= level then timehdr(); [q] end end
end);
end
local terra final_cleanup :: {} -> {}
lib.dbg = defrep(3,'debug', '32')
lib.report = defrep(2,'info', '35')
lib.warn = defrep(1,'warn', '33')
lib.bail = macro(function(...)
local q = lib.emit(true, 2, lib.noise.header('31','fatal'), ...)
return quote
if lib.noise.level ~= 0 then
timehdr(); [q]
end
final_cleanup()
lib.proc.exit(1)
end
end);
lib.stat = terralib.memoize(function(ty)
local n = struct {
ok: bool
union {
................................................................................
lib.pk = lib.loadlib('mbedtls','mbedtls/pk.h')
lib.md = lib.loadlib('mbedtls','mbedtls/md.h')
lib.b64 = lib.loadlib('mbedtls','mbedtls/base64.h')
lib.net = lib.loadlib('mongoose','mongoose.h')
lib.pq = lib.loadlib('libpq','libpq-fe.h')
lib.load {
'mem', 'math', 'str', 'file', 'crypt', 'ipc';
'http', 'html', 'session', 'tpl', 'store';
'smackdown'; -- md-alike parser
}
local be = {}
for _, b in pairs(config.backends) do
................................................................................
}
do
local p = string.format('parsav: %s\nbuilt on %s\n', config.build.str, config.build.when)
terra version() lib.io.send(1, p, [#p]) end
end
terra lib.noise.init(default_level: uint)
lib.noise.starttime = lib.osclock.time(nil)
lib.noise.lasttime = 0
var n = lib.proc.getenv('parsav_noise')
if n ~= nil then
if n[0] >= 0x30 and n[0] <= 0x39 and n[1] == 0 then
lib.noise.level = n[0] - 0x30
return
end
end
lib.noise.level = default_level
end
lib.load{'mgtool'}
local options = lib.cmdparse {
version = {'V', 'display information about the binary build and exit'};
verbose = {'v', 'increase logging verbosity', inc=1};
quiet = {'q', 'do not print to standard out'};
help = {'h', 'display this list'};
backend_file = {'B', 'init from specified backend file', consume=1};
static_dir = {'S', 'directory with overrides for static content', consume=1};
builtin_data = {'D', 'do not load static content overrides at runtime under any circumstances'};
instance = {'i', 'set an instance name to make it easier to control multiple daemons', consume = 1};
no_ipc = {'I', 'disable IPC'};
}
local static_setup = quote end
local mapin = quote end
local odir = symbol(rawstring)
local pathbuf = symbol(lib.str.acc)
................................................................................
end or quote return end
] end
var [pathbuf] defer pathbuf:free()
pathbuf:compose(odir,'/')
[mapin]
end
terra final_cleanup()
lib.ipc.global_emperor:release()
end
local terra entry_daemon(argc: int, argv: &rawstring): int
if argc < 1 then lib.bail('bad invocation!') end
lib.noise.init(1)
[lib.init]
-- shut mongoose the fuck up
lib.net.mg_log_set_callback([terra(msg: &opaque, sz: int, u: &opaque) end], nil)
var srv: lib.srv.overlord
do var mode: options
................................................................................
mode:parse(argc,argv) defer mode:free()
static_init(&mode)
if mode.version then version() return 0 end
if mode.help then
[ lib.emit(true, 1, 'usage: ',`argv[0],' ', options.helptxt.flags, ' [<args>…]', options.helptxt.opts) ]
return 0
end
if mode.quiet then lib.noise.level = 0 end
var cnf: rawstring
if mode.backend_file ~= nil
then cnf = @mode.backend_file
else cnf = lib.proc.getenv('parsav_backend_file')
end
if cnf == nil then cnf = [config.prefix_conf .. "/backend.conf"] end
srv:setup(cnf)
if argv[0][0] == @'-' and argv[0][1] == 0 then
lib.ipc.notify_parent(lib.ipc.signals.state_success)
argv[0] = 'parsav service daemon'
end
srv:start(lib.trn(mode.instance ~= nil, @mode.instance, nil))
end
lib.ipc.global_emperor = lib.ipc.emperor.mk(true) defer lib.ipc.global_emperor:release()
lib.report('listening for requests')
while lib.ipc.proc_active do
srv:poll()
while true do
var d: lib.ipc.demand
lib.ipc.global_emperor:poll(&d)
var a = lib.ipc.ack {success = true}
if d.cmd == lib.ipc.cmd.none then break
elseif d.cmd == lib.ipc.cmd.stop then
lib.ipc.proc_active = false
elseif d.cmd == lib.ipc.cmd.enumerate then
if srv.id ~= nil then
lib.str.ncpy(&a.iname[0], srv.id, [(`a.iname).tree.type.N])
else a.iname[0] = 0 end
elseif d.cmd == lib.ipc.cmd.chnoise then
lib.noise.level = d.operand
elseif d.cmd == lib.ipc.cmd.cfgrefresh then
srv.cfg:free()
srv.cfg:load()
end
d:ack(&lib.ipc.global_emperor, &a)
end
end
srv:shutdown()
return 0
end
local bflag = function(long,short)
if short and util.has(buildopts, short) then return true end
if long and util.has(buildopts, long) then return true end
return false
end
|