hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/kernel/shell.rv
blob: 3e247671a74d9c3f00f27c5e57defce85413bcbf (plain)
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
# i eventually want the shell to be a standalone program and shit

fn pad(text, width) do
  let result = text
  while result:len() < width do result = result ~ " " end
  return result
end

fn help_row(label, description) do
  let col = pad(label, 22)
  if label:len() >= 22 do col = label ~ "  " end
  puts("    #{col}#{description}\n")
end

fn help_group(title, group_name) do
  puts("\n  #{title}\n")
  for index in 0..commands:len() do
    if commands[index].group == group_name do help_row(commands[index].usage, commands[index].help) end
  end
end

fn cmd_help(context) do
  help_group("shell", "shell")
  help_group("gils (builtin)", "gils")
  # the rest of gils, and the programs, are real files in /bin now
  const items = fs:entries("/bin")
  if items != :nil do
    puts("\n  gils (/bin scripts)\n")
    for index in 0..items:len() do
      const name = items[index].name
      if (name:len() > 3 and name[(name:len() - 3)..] == ".rv") do help_row(name[0..(name:len() - 3)], "") end
    end
    puts("\n  programs (/bin)\n")
    for index in 0..items:len() do
      const name = items[index].name
      if (name:len() <= 3 or name[(name:len() - 3)..] != ".rv") do help_row(name, "") end
    end
  end

  puts("\n  fonts (font <name>)\n")
  for index in 0..fonts:len() do help_row(fonts[index].name, "8x#{fonts[index].height}") end
  const font_files = fs:entries("/fonts")
  if font_files != :nil do
    puts("\n  fonts on disk (font load /fonts/x.psf)\n")
    for index in 0..font_files:len() do help_row(font_files[index].name, "") end
  end
  puts("\n")
end

fn welcome() do
  let fs_status = "no filesystem"
  if fs.ready do fs_status = "revelfs mounted" end
  puts("\n")
  puts("   kernel written in revo - by an alcoholic\n")
  puts("   #{(mem_total() shr 20)} MiB ram | #{fs_status} | #{prog_count()} programs\n")
  puts("   type help to see what it does\n\n")
  puts(prompt())
end

# global so /bin scripts can call it from a nested eval fiber
global arg_name = fn(line, start) do
  let quoted = :false
  let begin = start
  if (begin < line:len() and line[begin] == "\"") do begin += 1 quoted = :true end
  let index = begin
  if quoted do
    while (index < line:len() and line[index] != "\"") do index += 1 end
  end
  else do
    while (index < line:len() and line[index] != " ") do index += 1 end
  end
  const name = line[begin..index]
  let rest = index
  if (quoted and rest < line:len()) do rest += 1 end
  if (rest < line:len() and line[rest] == " ") do rest += 1 end
  { name = name, rest = rest }
end

fn parent_of(path) do
  if path == "/" return "/"
  let index = path:len() - 1
  while (index > 0 and path[index] != "/") do index -= 1 end
  if index == 0 return "/"
  return path[0..index]
end

fn cmd_cd(context) do
  const arg = if context.argv:len() > 1 context.argv[1] else "/"
  const target = if arg == ".." parent_of(wd.path) else abspath(arg)
  if target == "/proc" or target == "/bin" or fs:is_dir(target) do wd.path = target end
  else do puts("cd: #{arg}: not a directory\n") end
end

fn cmd_pwd(context) puts("#{wd.path}\n")

fn wipe() console:clear()
fn cmd_clear(context) wipe()

fn zoom_by(delta) do
  const entry = console.font_entry
  # ttf is vector, stepping the size interactively is fine
  if (entry != :nil and entry.kind == :ttf) do
    let size = entry.size + delta * 4
    if size < 8 do size = 8 end
    if size > 72 do size = 72 end
    if size == entry.size do puts("zoom #{size}px\n") return :nil end
    const face = load_ttf(entry.path, size)
    if face == :nil do puts("zoom: rasterize failed\n") return :nil end
    add_font(entry.name, face.glyphs, face.width, face.height, :ttf, entry.path, size)
    console:set_font(find_font(entry.name))
    puts("zoom #{size}px\n")
    return :nil
  end
  # bitmap fonts js use integer pixel scale
  let scale = console.scale + delta
  if scale < 1 do scale = 1 end
  if scale > 6 do scale = 6 end
  console.scale = scale
  wipe()
  puts("zoom #{scale}x\n")
end

fn cmd_zoom(context) do
  const direction = if context.argv:len() > 1 context.argv[1] else ""
  if direction == "in" do zoom_by(1) end
  else if direction == "out" do zoom_by(-1) end
  else do puts("usage: zoom in | zoom out\n") end
end

fn font_basename(path) do
  let start = 0
  let index = 0
  while index < path:len() do
    if path[index] == "/" do start = index + 1 end
    index += 1
  end
  let name = path[start..]
  let dot = -1
  for i in 0..name:len() do if name[i] == "." do dot = i end end
  if dot > 0 do name = name[0..dot] end
  return name
end

fn load_psf(path) do
  const size = fs:size(path)
  if size < 4 return :nil
  const data = fs:read_bytes(path, 0, size)
  if (data[0]:ascii() != 0x36 or data[1]:ascii() != 0x04) do return :nil end
  const height = data[3]:ascii()
  if (height == 0 or size < 4 + 128 * height) do return :nil end
  let glyphs = {}
  for code in 32..128 do
    const base = 4 + code * height
    let rows = {}
    for row in 0..height do rows:push(reverse8(data[base + row]:ascii())) end
    glyphs:push(rows)
  end
  return { glyphs = glyphs, width = 8, height = height }
end

fn has_slash(text) do
  for index in 0..text:len() do if text[index] == "/" return :true end
  return :false
end

fn ends_with(text, suffix) do
  if text:len() < suffix:len() return :false
  return text[(text:len() - suffix:len())..] == suffix
end

fn str_to_int(text) do
  let value = 0
  for index in 0..text:len() do
    const code = text[index]:ascii()
    if (code >= 48 and code <= 57) do value = value * 10 + (code - 48) end
  end
  return value
end

fn font_file(name) do
  if has_slash(name) do
    const p = abspath(name)
    if (ends_with(p, ".ttf") and fs:size(p) >= 0) return { path = p, kind = :ttf }
    if (ends_with(p, ".psf") and fs:size(p) >= 0) return { path = p, kind = :psf }
    if fs:size(p ~ ".ttf") >= 0 return { path = p ~ ".ttf", kind = :ttf }
    if fs:size(p ~ ".psf") >= 0 return { path = p ~ ".psf", kind = :psf }
    return { path = p, kind = :none }
  end
  if fs:size("/fonts/#{name}.psf") >= 0 return { path = "/fonts/#{name}.psf", kind = :psf }
  if fs:size("/fonts/#{name}.ttf") >= 0 return { path = "/fonts/#{name}.ttf", kind = :ttf }
  return { path = name, kind = :none }
end

fn cmd_font(context) do
  if context.argv:len() < 2 do
    puts("fonts:")
    for index in 0..fonts:len() do puts(" #{fonts[index].name}(#{fonts[index].width}x#{fonts[index].height})") end
    puts("\n")
    const font_files = fs:entries("/fonts")
    if font_files != :nil do
      puts("on disk:")
      for index in 0..font_files:len() do puts(" #{font_files[index].name}") end
      puts("\n")
    end
    return :nil
  end
  # `font load <path> [size]` still works, but a bare name/path is the same thing
  let name = context.argv[1]
  let arg_at = 1
  if name == "load" do
    if context.argv:len() < 3 do puts("usage: font load <path> [size]\n") return :nil end
    name = context.argv[2]
    arg_at = 2
  end
  # an explicit size (ttf only) forces a fresh rasterize. without one we just
  # switch to whatever is already in the registry under that name
  let size = 0
  if context.argv:len() > arg_at + 1 do size = str_to_int(context.argv[arg_at + 1]) end
  if size == 0 do
    const existing = find_font(name)
    if existing != :nil do console:set_font(existing) return :nil end
  end
  const info = font_file(name)
  if info.kind == :none do puts("font: #{name}: no such font\n") return :nil end
  const reg_name = font_basename(info.path)
  if info.kind == :psf do
    const prior = find_font(reg_name)
    if prior != :nil do console:set_font(prior) return :nil end
    const parsed = load_psf(info.path)
    if parsed == :nil do puts("font: #{name}: not a psf font\n") return :nil end
    add_font(reg_name, parsed.glyphs, parsed.width, parsed.height, :bitmap, :nil, 0)
    console:set_font(find_font(reg_name))
    return :nil
  end
  # ttf: rasterize ascii at the requested size (default 18)
  if size == 0 do size = 18 end
  const face = load_ttf(info.path, size)
  if face == :nil do puts("font: #{name}: bad ttf\n") return :nil end
  add_font(reg_name, face.glyphs, face.width, face.height, :ttf, info.path, size)
  console:set_font(find_font(reg_name))
end

fn bcd(byte) (byte shr 4) * 10 + (byte band 15)
fn rtc(cmos_reg) do
  outb(0x70, cmos_reg)
  inb(0x71)
end
fn cmd_time(context) puts("utc #{bcd(rtc(4))}:#{bcd(rtc(2))}:#{bcd(rtc(0))}\n")

# 0x prefixed 16 digit hex
fn hex(value) do
  const digits = "0123456789abcdef"
  let result = ""
  for index in 0..16 do result = result ~ digits[(value shr ((15 - index) * 4)) band 15] end
  "0x#{result}"
end

fn mem_type_name(kind) do
  if kind == 0 return "usable"
  if kind == 1 return "reserved"
  if kind == 2 return "acpi-reclaimable"
  if kind == 3 return "acpi-nvs"
  if kind == 4 return "bad"
  if kind == 5 return "bootloader-reclaimable"
  if kind == 6 return "kernel+modules"
  if kind == 7 return "framebuffer"
  "unknown"
end

# the boot memory map dump
fn dump_memmap() do
  puts("memory map:\n")
  for index in 0..memmap_count() do
    puts("  #{hex(memmap_base(index))} len=#{hex(memmap_len(index))} type=#{mem_type_name(memmap_kind(index))}\n")
  end
  puts("usable: #{(mem_total() shr 20)} MiB, free: #{(mem_free() shr 20)} MiB\n")
end

fn cmd_mem(context) puts("#{(mem_free() shr 20)} MiB free / #{(mem_total() shr 20)} MiB usable\n")
fn cmd_uptime(context) puts("#{math.floor(uptime() / 100)}s (#{uptime()} ticks)\n")
fn cmd_ps(context) puts("#{proc_count()} process(es) running\n")
fn cmd_echo(context) do if context.line:len() > 5 puts("#{context.line[5..]}\n") else puts("\n") end
fn cmd_mkfs(context) fs:format()

fn cmd_run(context) do
  if context.argv:len() <= 1 do list_progs() return :nil end
  const parsed = arg_name(context.rest, 0)
  if run_prog(parsed.name, context.rest[parsed.rest..]) == :true return :hold
  puts("no such program: #{parsed.name}\n")
  return :nil
end

fn run(line) do
  # the fact that the shell needs to interface directly with ed isnt, very nice
  # this is a TODO btw
  if gils_ed_active() do gils_ed_feed(line) return :nil end
  if line:trim():len() == 0 do puts(prompt()) return :nil end
  const argv = line:split(" ")
  const context = make_ctx(line, argv)
  const command = lookup(argv[0])
  if command != :nil do
    # :hold means the command took over (modal editor / launched a program) so
    # we dont draw a fresh prompt, otherwise we do
    const result = command.handler(context)
    if result != :hold do puts(prompt()) end
    return :nil
  end

  if run_script(argv[0], context) == :true do puts(prompt()) return :nil end
  if run_prog(argv[0], context.rest) == :true return :nil
  puts("#{argv[0]}: command not found\n")
  puts(prompt())
end

register("help", "shell", "show this", cmd_help)
register("clear", "shell", "wipe the screen", cmd_clear)
register("time", "shell", "utc clock", cmd_time)
register("mem", "shell", "free / total ram", cmd_mem)
register("uptime", "shell", "seconds since boot", cmd_uptime)
register("ps", "shell", "running processes", cmd_ps)
register("zoom in|out", "shell", "scale the font", cmd_zoom)
register("font [name] [size]", "shell", "switch/load a console font (psf or ttf)", cmd_font)
register("echo <text>", "shell", "print it back", cmd_echo)
register("mkfs", "shell", "format the disk", cmd_mkfs)
register("cd <path>", "gils", "change directory", cmd_cd)
register("pwd", "gils", "print working dir", cmd_pwd)
register("run [name] [arg]", "shell", "run a program explicitly", cmd_run)