This file is indexed.

/usr/share/games/instead/stead/dbg.lua is in instead-data 1.9.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
-- add this: reuire "dbg"
-- in your project
-- for debug tools
require "input"

local function _xref_escape(n)
	local delim = ':'
	if stead.api_version >= "1.2.2" then
		delim = stead.delim;
	end
	if xact then
		n = n:gsub("\\?[\\"..delim.."]", { [delim] = "\\"..delim } )
	end
	return n
end

local function ordered_n(t)
	local ordered = {};
	local i,v, max;
	max = 0;
	for i,v in pairs(t) do
		local o = { k = i; v = v };
		stead.table.insert(ordered, o);
		max = max + 1;
	end
	stead.table.sort(ordered, function(a, b)
		if isObject(a.v) and not isObject(b.v) then
			return true
		end
		if not isObject(a.v) and isObject(b.v) then
			return false
		end
		if isObject(a.v) and isObject(b.v) then
			local n = stead.call(a.v, 'nam');
			local m = stead.call(b.v, 'nam');
			if type(n) ~= 'string' and type(m) ~= 'string' then
				return false
			end
			if type(n) ~= 'string' then
				return true
			end
			if type(m) ~= 'string' then
				return false
			end
			if n == m then
				local o1, o2
				o1 = stead.deref(a.v)
				o2 = stead.deref(b.v)
				if type(o1) == 'string' and type(o2) == 'string' then
					return o1 < o2
				end
			end
			return n < m;
		end
		return false
	end)
	ordered.i = 1;
	ordered.max = max;
	return ordered;
end

local function snext(t, k)
	local v
	if not k then
		k = ordered_n(t);
	end
	if k.i > k.max then
		return nil
	end
	v = k[k.i]
	k.i = k.i + 1
	return k, v.v, v.k;
end

local function spairs(s, var)
	return snext, s, nil;
end

function dbg_disp_obj()
	local v = obj {
		nam = 'disp',
		act = true,
		dsc = function(s)
			local r = s._txt
			s._txt = nil;
			return r
		end;
		save = function(self, name, h, need)
			if need then
				h:write(stead.string.format("%s  = dbg_disp_obj();\n", name));
			end
			stead.savemembers(h, self, name, false);
		end
	}
	return v;
end

dbg_dump_obj = function(w)
	w = stead.ref(w)

	if type(w) ~= 'table' then
		seen('disp')._txt = '^^No such object.';
		return true
	end
	local i,o,n
	local rc = ''
	for i,o in pairs(w) do
		local t = stead.tostring(o);
		if t == i then
			t = tostring(o);
		end
		if t then
			if rc ~='' then rc = rc..'^' end
			local n = '';
			if isObject(o) then
				n = stead.call(o, 'nam');
				if type(n) ~= 'string' then n = '' else n = ' : '..n; end
			end
			rc = stead.cat(rc, stead.par(' ', tostring(i)..' : '..t..n));
		end
	end
	seen('disp')._txt = stead.cat('^^', rc)
	return true;
end

dbg_dump_globals = function()
	local i,o
	local rc=''
	if type(variables) ~= 'table' then
		return
	end
	for i,o in ipairs(variables) do
		local v = _G[o];
		local t = stead.tostring(v);
		if t then
			if rc ~='' then rc = rc..'^' end
			rc = stead.cat(rc, stead.par(' ', tostring(o)..' : '..t));
		end
	end
	seen('disp')._txt = stead.cat('^^', rc)
	return true;
end

dbg_here = function()
	return debug_tool._here
end

dbg_list_objects = function()
	local i,o
	local dis = function(o)
		if isDisabled(o) then
			return ", disabled"
		end
		return ''
	end
	local rc = stead.par(' ', 'Room:'..tostring(stead.deref(dbg_here())), 
			'Nam:'..tostring(stead.call(dbg_here(),'nam')));
	for i,o in opairs(objs(dbg_here())) do
		rc = rc..'^';
		o = stead.ref(o)
		rc = stead.cat(rc, stead.par(' ', 'Id: '..tostring(o.id)..', '..
			tostring(stead.deref(o))..': '..tostring(stead.call(o, 'nam'))..dis(o)));
	end
--	seen('disp')._txt = rc
	return rc
end

dbg_list_inv = function()
	local i,o
	local rc=''
	local dis = function(o)
		if isDisabled(o) then
			return ", disabled"
		end
		return ''
	end

	local tak = function(o)
		if taken(o) then
			return ", taken"
		end
		return ''
	end

	for i,o in opairs(inv()) do
		if rc ~='' then rc = rc..'^' end
		o = stead.ref(o)
		rc = stead.cat(rc, stead.par(' ', 'Id: '..tostring(o.id)..', '..
			tostring(stead.deref(o))..': '..tostring(stead.call(o, 'nam'))..dis(o)..tak(o)));
	end
	if rc == '' then return end
--	seen('disp')._txt = rc
	return rc
end

dbg_execute_cmd = room {
	nam = "Execute Lua code",
	debug = true,
	pic = true,
	system_type = true, 
	forcedsc = true,
	dsc = "Enter Lua code here to exec.",
	inp_enter = function(s)
		if type(s.obj[1]._txt) == 'string' then
			local f = stead.eval(s.obj[1]._txt);
			if f then
				seen('disp')._txt = stead.cat('^^', f());
				return true
			end
			seen('disp')._txt = "^^Error in exec.";
			return true
		end
		return stead.back();
	end,
	obj = { inp('inp', '{Enter cmd}: ', 'return "Hello World!"'), 
		obj { nam = 'Back', dsc = '^{Back}', act = code [[ stead.back() ]] },
		dbg_disp_obj(),
	},
	exit = function(s)
		s.obj[1]:state(false)
	end;
}

dbg_dump_object = room {
	nam = "Dump object",
	debug = true,
	pic = true,
	system_type = true, 
	forcedsc = true,
	dsc = "Enter object name here to dump.",
	inp_enter = function(s)
		local w = s.obj[1]._txt
		if type(w) == 'string' then
			if not stead.ref(w) then w = objs(dbg_here()):srch(w); end
			return dbg_dump_obj(w);
		end
		return stead.back();
	end,
	obj = { inp('inp', '{Enter object}: ', 'main'), 
		obj{nam = 'Here', dsc = '^{Dump here}', act = code[[ return dbg_dump_obj(dbg_here())]]},
		obj{nam = 'Player',dsc =  '^{Dump player}', act = code[[ return dbg_dump_obj(stead.me())]]},
		obj{nam = 'Lifes', dsc = '^{Dump lifes}', act = code[[ return dbg_dump_obj(debug_tool.lifes)]]},
		obj{nam = 'Ways', dsc = '^{Dump ways}', act = code[[ return dbg_dump_obj(ways(dbg_here()))]]},
		obj{nam = 'Globals', dsc = '^{Dump globals}', act = code [[return dbg_dump_globals()]] },
		obj{nam = 'Back', dsc = '^{Back}', act = code [[ return stead.back() ]] },
		dbg_disp_obj() },
	exit = function(s)
		s.obj[1]:state(false)
	end;
}

dbg_choose_location = dlg {
	debug = true,
	pic = true,
	system_type = true, 
	forcedsc = true,
	nam = 'Go to',
	dsc = 'Select location.',
	gen = function(s)
		local k,v,kk
		objs(s):zap();
		for k,v,kk in spairs(_G) do
			if isRoom(v) and not v.debug then
				local n = tostring(stead.call(v, 'nam'));
				local o = kk;
				if type(o) == 'string' then
					n = n..' : '..o;
					n = _xref_escape(n);
					put(phr(n, true, [[timer:set(debug_tool._timer); game.lifes:cat(debug_tool.lifes); return stead.walk(]]..o..[[)]]), s);
				end
			end
		end
		put (phr('Back',true, 'return stead.back()'), s)
	end
}

dbg_choose_object = dlg {
	debug = true,
	pic = true,
	system_type = true, 
	forcedsc = true,
	nam = 'Get object',
	dsc = 'Select object to get.',
	gen = function(s)
		local k,v,kk
		objs(s):zap();
		for k,v,kk in spairs(_G) do
			if isObject(v) and not isPhrase(v) and not isRoom(v) and not isPlayer(v) and not v.debug and not have(v) and not isStatus(v) then
				local n = tostring(stead.call(v, 'nam'));
				local o = kk;
				if type(o) == 'string' then
					n = n..' : '..o;
					n = _xref_escape(n);
					put(phr(n, true, o..':enable(); return take('..o..')'), s);
				end
			end
		end
		put (phr('Back',true, 'return stead.back()'), s)
	end
}

dbg_drop_object = dlg {
	debug = true,
	pic = true,
	forcedsc = true,
	system_type = true, 
	nam = 'Drop object',
	dsc = 'Select object to drop.',
	gen = function(s)
		local k,v
		objs(s):zap();
		for k,v in ipairs(inv()) do
			v = stead.ref(v);
			if not v.debug then
				local n = tostring(stead.call(v, 'nam'));
				local o = stead.deref(v);
				if type(o) == 'string' then
					n = n..' : '..o;
					n = _xref_escape(n);
					put (phr(n, true, o..':enable(); drop('..o..','..stead.deref(dbg_here())..')'), s)
				end
			end
		end
		put (phr('Back', true, 'return stead.back()'), s)
	end
}

function dbg_exit()
	local r
	if stead.api_version < "1.2.0" then
		r = stead.call(dbg_here(), 'dsc');
	end
	game.lifes:cat(debug_tool.lifes);
	timer:set(debug_tool._timer);
	return stead.par ('^^', stead.back(), r);
end

debug_dlg = dlg {
	debug = true,
	pic = true,
	system_type = true, 
	forcedsc = true,
	nam = 'Debug Tool',
	dsc = 'Select tool.',
	obj = {
		phr('Go to location...', true, [[pon(); dbg_choose_location:gen(); return stead.walk('dbg_choose_location')]]),
		phr('Get object...', true, [[pon(); dbg_choose_object:gen(); return stead.walk('dbg_choose_object')]]),
		phr('Put object...', true, [[pon(); dbg_drop_object:gen(); return stead.walk('dbg_drop_object')]]),
		phr('Current scene...', true, [[pon(); return dbg_list_objects();]]),
		phr('Inventory...', true, [[pon(); return dbg_list_inv();]]),
		phr('Dump object...', true, [[pon(); return stead.walk(dbg_dump_object);]]),
		phr('Exec Lua string...', true, [[pon(); return stead.walk('dbg_execute_cmd')]]),
		phr('Exit',true , [[pon(); return dbg_exit()]]),
	},
};

debug_tool = menu {
	debug = true,
	system_type = true,
	forcedsc = true,
	nam = txtb('debug'),
	lifes = list {},
	inv = function(s)
		if stead.here().debug then
			return nil, true --nothing todo
		end
		debug_dlg.__from__ = stead.here();
		s._timer = timer:get();
		timer:stop();
		s.lifes:zap();
		s.lifes:cat(game.lifes);
		game.lifes:zap();
		s._here = stead.here();
		stead.me().where = 'debug_dlg'; -- force to go
		return stead.walk(self.where);
	end,
};

game.action = stead.hook(game.action, 
function (f, s, cmd, ...)
	if cmd == 'use_debug' then
		return debug_tool:inv()
	elseif cmd == 'exit_debug' then
		stead.me().where = 'debug_dlg';
		dbg_execute_cmd.obj[1]:state(false)
		dbg_dump_object.obj[1]:state(false)
		return dbg_exit()
	end
	return f(s, cmd, ...)
end)

stead.module_init(function()
	input.key = stead.hook(input.key,
	function(f, s, down, key, ...)
		if down and key == 'f7' then 
			if stead.here().debug then
				return 'exit_debug'
			else
				return 'use_debug'
			end
		end
		
		return f(s, down, key, ...)
	end)
	putf('debug_tool', stead.me());
end)


-- vim:ts=4