summaryrefslogtreecommitdiffstats
path: root/src/vpp-api/lua/examples/cli/lua-cli.lua
blob: 4a27af536046adada688eaefc018082ae7208ae6 (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
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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
--[[
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
]]

-- Experimental prototype CLI using API to VPP, with tab completion
--
-- Written by Andrew Yourtchenko (ayourtch@cisco.com) 2010,2016
--

vpp = require "vpp-lapi"


local dotdotdot = "..."

-- First the "readline" routine

readln = {
split = function(str, pat)
  local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  local fpat = "(.-)" .. pat
  local last_end = 1
  if str then
    local s, e, cap = str:find(fpat, 1)
    while s do
      if s ~= 1 or cap ~= "" then
        table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
    end
    if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
    end
  end
  return t
end,

reader = function()
  local rl = {}

  rl.init = function()
    os.execute("stty -icanon min 1 -echo")
    rl.rawmode = true
  end

  rl.done = function()
    os.execute("stty icanon echo")
    rl.rawmode = false
  end

  rl.prompt = ">"
  rl.history = { "" }
  rl.history_index = 1
  rl.history_length = 1

  rl.hide_cmd = function()
    local bs = string.char(8) .. " " .. string.char(8)
    for i = 1, #rl.command do
      io.stdout:write(bs)
    end
  end

  rl.show_cmd = function()
    if rl.command then
      io.stdout:write(rl.command)
    end
  end

  rl.store_history = function(cmd)
    if cmd == "" then
      return
    end
    rl.history[rl.history_length] = cmd
    rl.history_length = rl.history_length + 1
    rl.history_index = rl.history_length
    rl.history[rl.history_length] = ""
  end

  rl.readln = function()
    local done = false
    local need_prompt = true
    rl.command = ""

    if not rl.rawmode then
      rl.init()
    end

    while not done do
      if need_prompt then
        io.stdout:write(rl.prompt)
	io.stdout:write(rl.command)
        need_prompt = false
      end

      local ch = io.stdin:read(1)
      if ch:byte(1) == 27 then
        -- CONTROL
        local ch2 = io.stdin:read(1)
        -- arrows
        if ch2:byte(1) == 91 then
          local ch3 = io.stdin:read(1)
          local b = ch3:byte(1)
          if b == 65 then
            ch = "UP"
          elseif b == 66 then
            ch = "DOWN"
          elseif b == 67 then
            ch = "RIGHT"
          elseif b == 68 then
            ch = "LEFT"
          end
          -- print("Byte: " .. ch3:byte(1))
          -- if ch3:byte(1)
        end
      end

      if ch == "?" then
        io.stdout:write(ch)
        io.stdout:write("\n")
        if rl.help then
          rl.help(rl)
        end
        need_prompt = true
      elseif ch == "\t" then
        if rl.tab_complete then
          rl.tab_complete(rl)
        end
        io.stdout:write("\n")
        need_prompt = true
      elseif ch == "\n" then
        io.stdout:write(ch)
        done = true
      elseif ch == "\004" then
        io.stdout:write("\n")
        rl.command = nil
	done = true
      elseif ch == string.char(127) then
        if rl.command ~= "" then
          io.stdout:write(string.char(8) .. " " .. string.char(8))
          rl.command = string.sub(rl.command, 1, -2)
        end
      elseif #ch > 1 then
        -- control char
        if ch == "UP" then
          rl.hide_cmd()
          if rl.history_index == #rl.history then
            rl.history[rl.history_index] = rl.command
          end
          if rl.history_index > 1 then
            rl.history_index = rl.history_index - 1
            rl.command = rl.history[rl.history_index]
          end
          rl.show_cmd()
        elseif ch == "DOWN" then
          rl.hide_cmd()
          if rl.history_index < rl.history_length then
            rl.history_index = rl.history_index + 1
            rl.command = rl.history[rl.history_index]
          end
          rl.show_cmd()
        end
      else
        io.stdout:write(ch)
        rl.command = rl.command .. ch
      end
    end
    if rl.command then
      rl.store_history(rl.command)
    end
    return rl.command
  end
  return rl
end

}

--[[

r = reader()

local done = false

while not done do
  local cmd = r.readln()
  print("Command: " .. tostring(cmd))
  if not cmd or cmd == "quit" then
    done = true
  end
end

r.done()

]]

--------- MDS show tech parser

local print_section = nil
local list_sections = false

local curr_section = "---"
local curr_parser = nil

-- by default operate in batch mode
local batch_mode = true

local db = {}
local device = {}
device.output = {}
local seen_section = {}

function start_collection(name)
  device = {}
  seen_section = {}
end

function print_error(errmsg)
  print("@#$:" .. errmsg)
end

function keys(tbl)
  local t = {}
  for k, v in pairs(tbl) do
    table.insert(t, k)
  end
  return t
end

function tset (parent, ...)

  -- print ('set', ...)

  local len = select ('#', ...)
  local key, value = select (len-1, ...)
  local cutpoint, cutkey

  for i=1,len-2 do

    local key = select (i, ...)
    local child = parent[key]

    if value == nil then
      if child == nil then  return
      elseif next (child, next (child)) then  cutpoint = nil  cutkey = nil
      elseif cutpoint == nil then  cutpoint = parent  cutkey = key  end

    elseif child == nil then  child = {}  parent[key] = child  end

    parent = child
    end

  if value == nil and cutpoint then  cutpoint[cutkey] = nil
  else  parent[key] = value  return value  end
  end


function tget (parent, ...)
  local len = select ('#', ...)
  for i=1,len do
    parent = parent[select (i, ...)]
    if parent == nil then  break  end
    end
  return parent
  end


local pager_lines = 23
local pager_printed = 0
local pager_skipping = false
local pager_filter_pipe = nil

function pager_reset()
  pager_printed = 0
  pager_skipping = false
  if pager_filter_pipe then
    pager_filter_pipe:close()
    pager_filter_pipe = nil
  end
end


function print_more()
  io.stdout:write(" --More-- ")
end

function print_nomore()
  local bs = string.char(8)
  local bs10 = bs ..  bs ..  bs ..  bs ..  bs ..  bs ..  bs ..  bs ..  bs ..  bs
  io.stdout:write(bs10 .. "          " .. bs10)
end

function print_line(txt)
  if pager_filter_pipe then
    pager_filter_pipe:write(txt .. "\n")
    return
  end
  if pager_printed >= pager_lines then
    print_more()
    local ch = io.stdin:read(1)
    if ch == " " then
      pager_printed = 0
    elseif ch == "\n" then
      pager_printed = pager_printed - 1
    elseif ch == "q" then
      pager_printed = 0
      pager_skipping = true
    end
    print_nomore()
  end
  if not pager_skipping then
    print(txt)
    pager_printed = pager_printed + 1
  else
    -- skip printing
  end
end

function paged_write(text)
  local t = readln.split(text, "[\n]")
  if string.sub(text, -1) == "\n" then
    table.insert(t, "")
  end
  for i, v in ipairs(t) do
    if i < #t then
      print_line(v)
    else
      if pager_filter_pipe then
        pager_filter_pipe:write(v)
      else
        io.stdout:write(v)
      end
    end
  end
end





function get_choices(tbl, key)
  local res = {}
  for k, v in pairs(tbl) do
    if string.sub(k, 1, #key) == key then
      table.insert(res, k)
    elseif 0 < #key and dotdotdot == k then
      table.insert(res, k)
    end
  end
  return res
end

function get_exact_choice(choices, val)
  local exact_idx = nil
  local substr_idx = nil
  local substr_seen = false

  if #choices == 1 then
    if choices[1] == dotdotdot then
      return 1
    elseif string.sub(choices[1], 1, #val) == val then
      return 1
    else
      return nil
    end
  else
    for i, v in ipairs(choices) do
      if v == val then
        exact_idx = i
        substr_seen = true
      elseif choices[i] ~= dotdotdot and string.sub(choices[i], 1, #val) == val then
        if substr_seen then
          substr_idx = nil
        else
          substr_idx = i
          substr_seen = true
        end
      elseif choices[i] == dotdotdot then
        if substr_seen then
          substr_idx = nil
        else
          substr_idx = i
          substr_seen = true
        end
      end
    end
  end
  return exact_idx or substr_idx
end

function device_cli_help(rl)
  local key = readln.split(rl.command, "[ ]+")
  local tree = rl.tree
  local keylen = #key
  local fullcmd = ""
  local error = false
  local terse = true

  if ((#rl.command >= 1) and (string.sub(rl.command, -1) == " ")) or (#rl.command == 0) then
    table.insert(key, "")
    terse = false
  end

  for i, v in ipairs(key) do
    local choices = get_choices(tree, v)
    local idx = get_exact_choice(choices, v)
    if idx then
      local choice = choices[idx]
      tree = tree[choice]
      fullcmd = fullcmd .. choice .. " "
    else
      if i < #key then
        error = true
      end
    end

    if i == #key and not error then
      for j, w in ipairs(choices) do
        if terse then
          paged_write(w .. "\t")
        else
          paged_write("  " .. w .. "\n")
        end
      end
      paged_write("\n")
      if terse then
        paged_write(" \n")
      end
    end
  end
  pager_reset()
end

function device_cli_tab_complete(rl)
  local key = readln.split(rl.command, "[ ]+")
  local tree = rl.tree
  local keylen = #key
  local fullcmd = ""
  local error = false

  for i, v in ipairs(key) do
    local choices = get_choices(tree, v)
    local idx = get_exact_choice(choices, v)
    if idx and choices[idx] ~= dotdotdot then
      local choice = choices[idx]
      tree = tree[choice]
      -- print("level " .. i .. " '" .. choice .. "'")
      fullcmd = fullcmd .. choice .. " "
    else
      -- print("level " .. i .. " : " .. table.concat(choices, " ") .. " ")
      error = true
    end
  end
  if not error then
    rl.command = fullcmd
  else
    -- print("\n\nerror\n")
  end
  pager_reset()
end

function device_cli_exec(rl)

  local cmd_nopipe = rl.command
  local cmd_pipe = nil

  local pipe1, pipe2 = string.find(rl.command, "[|]")
  if pipe1 then
    cmd_nopipe = string.sub(rl.command, 1, pipe1-1)
    cmd_pipe = string.sub(rl.command, pipe2+1, -1)
  end

  local key = readln.split(cmd_nopipe .. " <cr>", "[ ]+")
  local tree = rl.tree
  local keylen = #key
  local fullcmd = ""
  local error = false
  local func = nil

  if cmd_pipe then
    pager_filter_pipe = io.popen(cmd_pipe, "w")
  end


  rl.choices = {}

  for i, v in ipairs(key) do
    local choices = get_choices(tree, v)
    local idx = get_exact_choice(choices, v)
    if idx then
      local choice = choices[idx]
      if i == #key then
        func = tree[choice]
      else
        if choice == dotdotdot then
          -- keep the tree the same, update the choice value to match the input string
          choices[idx] = v
          choice = v
        else
          tree = tree[choice]
        end
      end
      -- print("level " .. i .. " '" .. choice .. "'")
      table.insert(rl.choices, choice)
    else
      -- print("level " .. i .. " : " .. table.concat(choices, " ") .. " ")
      error = true
      return nil
    end
  end
  return func
end

function populate_tree(commands)
  local tree = {}

  for k, v in pairs(commands) do
    local key = readln.split(k .. " <cr>", "[ ]+")
    local xtree = tree
    for i, kk in ipairs(key) do
      if i == 1 and kk == "sh" then
        kk = "show"
      end
      if i == #key then
        if type(v) == "function" then
          xtree[kk] = v
        else
          xtree[kk] = function(rl) paged_write(table.concat(v, "\n") .. "\n") end
        end
      else
        if not xtree[kk] then
          xtree[kk] = {}
        end
        xtree = xtree[kk]
      end
    end
  end
  return tree
end

function trim (s)
  return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end


function init_vpp(vpp)
  local root_dir = "/home/ubuntu/vpp"
  local pneum_path = root_dir .. "/build-root/install-vpp_debug-native/vpp-api/lib64/libpneum.so"

  vpp:init({ pneum_path = pneum_path })

  vpp:init({ pneum_path = pneum_path })
  vpp:json_api(root_dir .. "/build-root/install-vpp_debug-native/vpp/vpp-api/vpe.api.json")



  vpp:connect("lua_cli")
end

function run_cli(vpp, cli)
  local reply = vpp:api_call("cli_inband", { cmd = cli })
  if reply and #reply == 1 then
    local rep = reply[1]
    if 0 == rep.retval then
      return rep.reply
    else
      return "XXXXXLUACLI: API RETVAL ERROR : " .. tostring(rep.retval)
    end
  else
    return "XXXXXLUACLI ERROR, RAW REPLY: " .. vpp.dump(reply)
  end
end


function toprintablestring(s)
  if type(s) == "string" then
    return "\n"..vpp.hex_dump(s)
  else
    return tostring(s)
  end
end

function interactive_cli(r)
  while not done do
    pager_reset()
    local cmd = r.readln()
    if not cmd then
      done = true
    elseif cmd == "quit" or cmd == "exit" then
      done = true
    else
      local func = device_cli_exec(r)
      if func then
	func(r)
      else
	if trim(cmd) == "" then
	else
	  for i = 1, #r.prompt do
	    paged_write(" ")
	  end
	  paged_write("^\n% Invalid input detected at '^' marker.\n\n")
	end
      end
    end
  end
end

device = {}
device.output = {}

init_vpp(vpp)
cmds_str = run_cli(vpp, "?")
vpp_cmds = readln.split(cmds_str, "\n")
vpp_clis = {}

for linenum, line in ipairs(vpp_cmds) do
  local m,h = string.match(line, "^  (.-)  (.*)$")
  if m and #m > 0 then
    table.insert(vpp_clis, m)
    device.output["vpp debug cli " .. m] = function(rl)
      -- print("ARBITRARY CLI" .. vpp.dump(rl.choices))
      print("LUACLI command: " .. table.concat(rl.choices, " "))
      local sub = {}
      --
      for i=4, #rl.choices -1 do
        table.insert(sub, rl.choices[i])
      end
      local cli = table.concat(sub, " ")
      print("Running CLI: " .. tostring(cli))
      paged_write(run_cli(vpp, cli))
    end
    device.output["vpp debug cli " .. m .. " " .. dotdotdot] = function(rl)
      print("ARGH")
    end

    local ret = run_cli(vpp, "help " .. m)
    device.output["help vpp debug cli " .. m] = { ret }
  end
end

for linenum, line in ipairs(vpp_clis) do
  -- print(line, ret)
end

for msgnum, msgname in pairs(vpp.msg_number_to_name) do
  local cli, numspaces = string.gsub(msgname, "_", " ")
  device.output["call " .. cli .. " " .. dotdotdot] = function(rl)
    print("ARGH")
  end
  device.output["call " .. cli] = function(rl)
    print("LUACLI command: " .. table.concat(rl.choices, " "))
    print("Running API: " .. msgname) -- vpp.dump(rl.choices))
    local out = {}
    local args = {}
    local ntaken = 0
    local argname = ""
    for i=(1+1+numspaces+1), #rl.choices-1 do
      -- print(i, rl.choices[i])
      if ntaken > 0 then
        ntaken = ntaken -1
      else
        local fieldname = rl.choices[i]
        local field = vpp.msg_name_to_fields[msgname][fieldname]
        if field then
          local s = rl.choices[i+1]
          s=s:gsub("\\x(%x%x)",function (x) return string.char(tonumber(x,16)) end)
          args[fieldname] = s
          ntaken = 1
        end
      end
    end
    -- print("ARGS: ", vpp.dump(args))
    local ret = vpp:api_call(msgname, args)
    for i, reply in ipairs(ret) do
      table.insert(out, "=================== Entry #" .. tostring(i))
      for k, v in pairs(reply) do
        table.insert(out, "   " .. tostring(k) .. " : " .. toprintablestring(v))
      end
    end
    -- paged_write(vpp.dump(ret) .. "\n\n")
    paged_write(table.concat(out, "\n").."\n\n")
  end
  device.output["call " .. cli .. " help"] = function(rl)
    local out = {}
    for k, v in pairs(vpp.msg_name_to_fields[msgname]) do
      table.insert(out, tostring(k) .. " : " .. v["ctype"] .. " ; " .. tostring(vpp.dump(v)) )
    end
    -- paged_write(vpp.dump(vpp.msg_name_to_fields[msgname]) .. "\n\n")
    paged_write(table.concat(out, "\n").."\n\n")
  end
-- vpp.msg_name_to_number = {}
end



local r = readln.reader()
local done = false

r.prompt = "VPP(luaCLI)#"

r.help = device_cli_help
r.tab_complete = device_cli_tab_complete
print("===== CLI view, use ^D to end =====")

r.tree = populate_tree(device.output)
-- readln.pretty("xxxx", r.tree)


for idx, an_arg in ipairs(arg) do
  local fname = an_arg
  if fname == "-i" then
    pager_lines = 23
    interactive_cli(r)
  else
    pager_lines = 100000000
    for line in io.lines(fname) do
      r.command = line
      local func = device_cli_exec(r)
      if func then
	func(r)
      end
    end
  end
end

if #arg == 0 then
  print("You should specify '-i' as an argument for the interactive session,")
  print("but with no other sources of commands, we start interactive session now anyway")
   interactive_cli(r)
end

vpp:disconnect()
r.done()