parsav  Diff

Differences From Artifact [1c10e6f8f8]:

To Artifact [d1470e4b10]:


    11     11   			local path = {}
    12     12   			for m in l:gmatch('([^:]+)') do path[#path+1]=m end
    13     13   			local tgt = lib
    14     14   			for i=1,#path-1 do
    15     15   				if tgt[path[i]] == nil then tgt[path[i]] = {} end
    16     16   				tgt = tgt[path[i]]
    17     17   			end
    18         -			tgt[path[#path]] = terralib.loadfile(l:gsub(':','/') .. '.t')()
           18  +			tgt[path[#path]:gsub('-','_')] = terralib.loadfile(l:gsub(':','/') .. '.t')()
    19     19   		end
    20     20   	end;
    21     21   	loadlib = function(name,hdr)
    22     22   		local p = config.pkg[name]
    23     23   		-- for _,v in pairs(p.dylibs) do
    24     24   		-- 	terralib.linklibrary(p.libdir .. '/' .. v)
    25     25   		-- end
................................................................................
   175    175   	else -- print time since last msg
   176    176   		var dfs = arrayof(int8, 0x30 + diff/10, 0x30 + diff%10, 0x20, 0)
   177    177   		[ lib.emit(false, 2, ' \27[36m+', `&dfs[0]) ]
   178    178   	end
   179    179   end
   180    180   
   181    181   local defrep = function(level,n,code)
          182  +	if level >= 3 and config.debug == false then
          183  +		return macro(function(...) return {} end)
          184  +	end
   182    185   	return macro(function(...)
   183    186   		local fn = (...).filename
   184    187   		local ln = tostring((...).linenumber)
   185    188   		local dbgtag = string.format('\27[35m · \27[34m%s:\27[1m%s\27[m\n', fn,ln)
   186    189   		local q = lib.emit(level < 3 and true or dbgtag, 2, noise_header(code,n), ...)
   187         -		return quote if noise >= level then timehdr(); [q] end end
          190  +		return quote
          191  +		--lib.io.fmt(['attempting to emit at ' .. fn..':'..ln.. '\n'])
          192  +		if noise >= level then timehdr(); [q] end end
   188    193   	end);
   189    194   end
   190    195   lib.dbg = defrep(3,'debug', '32')
   191    196   lib.report = defrep(2,'info', '35')
   192    197   lib.warn = defrep(1,'warn', '33')
   193    198   lib.bail = macro(function(...)
   194    199   	local q = lib.emit(true, 2, noise_header('31','fatal'), ...)
................................................................................
   328    333   lib.md = lib.loadlib('mbedtls','mbedtls/md.h')
   329    334   lib.b64 = lib.loadlib('mbedtls','mbedtls/base64.h')
   330    335   lib.net = lib.loadlib('mongoose','mongoose.h')
   331    336   lib.pq = lib.loadlib('libpq','libpq-fe.h')
   332    337   
   333    338   lib.load {
   334    339   	'mem',  'math', 'str', 'file', 'crypt';
   335         -	'http', 'session', 'tpl', 'store';
          340  +	'http', 'html', 'session', 'tpl', 'store';
   336    341   
   337    342   	'smackdown'; -- md-alike parser
   338    343   }
   339    344   
   340    345   local be = {}
   341    346   for _, b in pairs(config.backends) do
   342    347   	be[#be+1] = terralib.loadfile('backend/' .. b .. '.t')()
................................................................................
   367    372   	local t = lib.tpl.mk { body = v, id = 'view/'..k }
   368    373   	data.view[k] = t
   369    374   end
   370    375   
   371    376   lib.load {
   372    377   	'srv';
   373    378   	'render:nav';
          379  +	'render:nym';
   374    380   	'render:login';
   375    381   	'render:profile';
          382  +
   376    383   	'render:compose';
   377    384   	'render:tweet';
   378    385   	'render:userpage';
   379    386   	'render:timeline';
          387  +
   380    388   	'render:docpage';
          389  +
          390  +	'render:conf:profile';
          391  +	'render:conf';
   381    392   	'route';
   382    393   }
   383    394   
   384    395   do
   385    396   	local p = string.format('parsav: %s\nbuilt on %s\n', config.build.str, config.build.when)
   386    397   	terra version() lib.io.send(1, p, [#p]) end
   387    398   end
   388    399   
   389         -terra noise_init()
          400  +terra lib.noise_init(default_level: uint)
   390    401   	starttime = lib.osclock.time(nil)
   391    402   	lastnoisetime = 0
   392    403   	var n = lib.proc.getenv('parsav_noise')
   393    404   	if n ~= nil then
   394    405   		if n[0] >= 0x30 and n[0] <= 0x39 and n[1] == 0 then
   395    406   			noise = n[0] - 0x30
   396    407   			return
   397    408   		end
   398    409   	end
   399         -	noise = 1
          410  +	noise = default_level
   400    411   end
          412  +lib.load{'mgtool'}
   401    413   
   402    414   local options = lib.cmdparse {
   403    415   	version = {'V', 'display information about the binary build and exit'};
          416  +	verbose = {'v', 'increase logging verbosity', inc=1};
   404    417   	quiet = {'q', 'do not print to standard out'};
   405    418   	help = {'h', 'display this list'};
   406         -	backend_file = {'b', 'init from specified backend file', 1};
   407         -	static_dir = {'S', 'directory with overrides for static content', 1};
   408         -	builtin_data = {'B', 'do not load static content overrides at runtime under any circumstances'};
          419  +	backend_file = {'B', 'init from specified backend file', consume=1};
          420  +	static_dir = {'S', 'directory with overrides for static content', consume=1};
          421  +	builtin_data = {'D', 'do not load static content overrides at runtime under any circumstances'};
          422  +	instance = {'i', 'set an instance name to make it easier to control multiple daemons', consume = 1};
   409    423   }
   410    424   
   411    425   
   412    426   local static_setup = quote end
   413    427   local mapin = quote end
   414    428   local odir = symbol(rawstring)
   415    429   local pathbuf = symbol(lib.str.acc)
................................................................................
   437    451   	[static_setup]
   438    452   	if mode.builtin_data then return end
   439    453   
   440    454   	var [odir] = lib.proc.getenv('parsav_override_dir')
   441    455   	if mode.static_dir ~= nil then
   442    456   		odir=@mode.static_dir
   443    457   	end
   444         -	if odir == nil then return end
          458  +	if odir == nil then [
          459  +		config.prefix_static and quote
          460  +			odir = [config.prefix_static]
          461  +		end or quote return end
          462  +	] end
   445    463   
   446    464   	var [pathbuf] defer pathbuf:free()
   447    465   	pathbuf:compose(odir,'/')
   448    466   	[mapin]
   449    467   end
   450    468   
   451         -terra entry(argc: int, argv: &rawstring): int
          469  +local terra entry_daemon(argc: int, argv: &rawstring): int
   452    470   	if argc < 1 then lib.bail('bad invocation!') end
   453    471   
   454         -	noise_init()
          472  +	lib.noise_init(1)
   455    473   	[lib.init]
   456    474   
   457    475   	-- shut mongoose the fuck up
   458    476   	lib.net.mg_log_set_callback([terra(msg: &opaque, sz: int, u: &opaque) end], nil)
   459    477   	var srv: lib.srv.overlord
   460    478   
   461    479   	do var mode: options
   462    480   		mode:parse(argc,argv) defer mode:free()
   463    481   		static_init(&mode)
   464    482   		if mode.version then version() return 0 end
   465    483   		if mode.help then
   466         -			lib.io.send(1,  [options.helptxt], [#options.helptxt])
          484  +			[ lib.emit(true, 1, 'usage: ',`argv[0],' ', options.helptxt.flags, ' [<args>…]', options.helptxt.opts) ]
   467    485   			return 0
   468    486   		end
   469    487   		var cnf: rawstring
   470    488   		if mode.backend_file ~= nil
   471    489   			then cnf = @mode.backend_file
   472    490   			else cnf = lib.proc.getenv('parsav_backend_file')
   473    491   		end
   474         -		if cnf == nil then cnf = "backend.conf" end
          492  +		if cnf == nil then cnf = [config.prefix_conf .. "backend.conf"] end
   475    493   
   476         -		srv:start(cnf)
          494  +		srv:setup(cnf)
          495  +		srv:start(lib.trn(mode.instance ~= nil, @mode.instance, nil))
   477    496   	end
   478    497   
   479    498   	lib.report('listening for requests')
   480    499   	while true do
   481    500   		srv:poll()
   482    501   	end
   483    502   	srv:shutdown()
   484    503   
   485    504   	return 0
   486    505   end
          506  +
   487    507   
   488    508   local bflag = function(long,short)
   489    509   	if short and util.has(buildopts, short) then return true end
   490    510   	if long and util.has(buildopts, long) then return true end
   491    511   	return false
   492    512   end
   493    513   
   494    514   if bflag('dump-config','C') then
   495    515   	print(util.dump(config))
   496    516   	os.exit(0)
   497    517   end
   498    518   
   499    519   local holler = print
   500         -local out = config.exe and 'parsav' or ('parsav.' .. config.outform)
          520  +local suffix = config.exe and '' or ('.'..config.outform)
          521  +local out = 'parsavd' .. suffix
   501    522   local linkargs = {}
          523  +local target = config.tgttrip and terralib.newtarget {
          524  +	Triple = config.tgttrip;
          525  +	CPU = config.tgtcpu;
          526  +	FloatABIHard = config.tgthf;
          527  +} or nil
   502    528   
   503    529   if bflag('quiet','q') then holler = function() end end
   504    530   if bflag('asan','s') then linkargs[#linkargs+1] = '-fsanitize=address' end
   505    531   if bflag('lsan','S') then linkargs[#linkargs+1] = '-fsanitize=leak' end
   506    532   
          533  +for _,p in pairs(config.pkg) do util.append(linkargs, p.linkargs) end
          534  +local linkargs_d = linkargs -- controller is not multithreaded
   507    535   if config.posix then
   508         -	linkargs[#linkargs+1] = '-pthread'
          536  +	linkargs_d[#linkargs_d+1] = '-pthread'
   509    537   end
   510         -for _,p in pairs(config.pkg) do util.append(linkargs, p.linkargs) end
   511    538   holler('linking with args',util.dump(linkargs))
   512         -terralib.saveobj(out, {
   513         -		main = entry
   514         -	},
   515         -	linkargs,
   516         -	config.tgttrip and terralib.newtarget {
   517         -		Triple = config.tgttrip;
   518         -		CPU = config.tgtcpu;
   519         -		FloatABIHard = config.tgthf;
   520         -	} or nil)
          539  +
          540  +terralib.saveobj('parsavd'..suffix, { main = entry_daemon }, linkargs_d, target)
          541  +terralib.saveobj('parsav' ..suffix, { main = lib.mgtool }, linkargs, target)