summaryrefslogtreecommitdiffstats
path: root/vtysh/vtysh.c
blob: 290b6629c3d6f131d3154c3c8272adc341146983 (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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
/* Virtual terminal interface shell.
 * Copyright (C) 2000 Kunihiro Ishiguro
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "zebra.h"
#include "misc.h"

#include <sys/un.h>
#include <setjmp.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/stat.h>

#include <readline/readline.h>
#include <readline/history.h>

#include "lib/command.h"
#include "lib/command_local.h"
#include "lib/command_execute.h"
#include "lib/memory.h"
#include "lib/log.h"
#include "bgpd/bgp_vty.h"

#include "lib/vty_command.h"
#include "lib/vty_io_vtysh.h"
#include "lib/vty_vtysh.h"
#include "lib/vty_cli.h"
#include "vtysh/vtysh.h"

#include "lib/distribute.h"
#include "lib/filter.h"
#include "lib/if.h"
#include "lib/if_rmap.h"
#include "lib/keychain.h"
#include "lib/plist.h"
#include "lib/routemap.h"

/* Using integrated config from Quagga.conf. Default is no.
 */
bool vtysh_integrated_vtysh_config = false ;

extern char config_default[];

/*==============================================================================
 * The vtysh has its own vty, which is used:
 *
 *   a) to parse commands to decide which (if any) daemon they belong to
 *
 *   b) to parse and execute commands in the vtysh itself.
 *
 * This vtysh has no input and its output is implicitly to the file handle
 * "fp" in the vout_base.  The output may be paged -- it is by default.  Just
 * before a top-level vtysh command is executed:
 *
 *   if it is to be paged: vout_base->fp    is set NULL
 *                         vout_base->pager is set to name of pager to use
 *
 *                         if there is any output, the pager is opened, and
 *                         vout_base->fp set appropriately.
 *
 *                         at end of top level command, any pager that was
 *                         opened is then closed.
 *
 *              otherwise: vout_base->fp    is set to stdout
 *                         vout_base->pager is set NULL
 *
 * vysh_pager_form set early in the morning, from VTYSH_PAGER or to the
 * default.  The default is:
 *
 *    more;more -%d
 *
 * where everything before the ';' is used if no terminal length is set, and
 * everything after the ';' is used if a terminal length > 0 is set.  If there
 * is no ':',
 *
 * So:  terminal length 12      -- turns on pager, with explicit length 12
 *      terminal length none    -- turns off pager
 *      terminal length default -- turns on pager, with its default length
 */

vty vtysh_vty     = NULL ;      /* The vtysh's own vty  */
vty vtysh_stderr  = NULL ;      /* For error messages   */

static char* vtysh_pager       = NULL ;         /* NULL => pager is off */

static char* vtysh_pager_form  = NULL ;         /* used to make pager   */

static int   vtysh_pager_lines = -1 ;

/*------------------------------------------------------------------------------
 * Execute given command, in the current vtysh_vty (using current context), in
 * all connected daemons.
 *
 * This is at the top level of the vtysh, and is used for:
 *
 *   * the interactive command mode
 *
 *   * the command line command mode
 *
 *   * commands to enter and exit CONFIG_NODE in configuration file reading.
 *
 *     All daemons, including the vtysh itself, can do these commands, so
 *     the only likely error is that a daemon is unable to enter configuration
 *     mode, and that is reported.
 *
 * If the command is not recognised by any connected daemon and not recognised
 * by the vtysh itself, an error is reported.  (Unlike the configuration file
 * reader, where this is simply ignored.)
 *
 * Uses the current setting of vtysh_pager.
 *
 * If "ignore_warnings" then will return CMD_SUCCESS instead of CMD_WARNING,
 * and if the command opens an input pipe, then will ignore warnings in there
 * too.
 *
 * Returns:  CMD_SUCCESS    -- OK, keep going
 *           CMD_STOP       -- OK, but exit
 *           CMD_WARNING    -- got a CMD_WARNING from at least one daemon
 *           CMD_ERROR      -- got a CMD_ERROR from at least one daemon
 */
extern cmd_ret_t
vtysh_execute(vty vty, const char* line, ulen prompt_len)
{
  uty_vtysh_out_prep(vty->vio, vtysh_pager) ;

  return vty_vtysh_command_loop(vty, line, true /* interactive */, prompt_len) ;
} ;

/*------------------------------------------------------------------------------
 * Initialise vtysh_pager_form, vtysh_pager_lines and vtysh_pager.
 *
 * Sets the pager *off*.
 */
static void
vtysh_pager_init(void)
{
  const char* form ;

  form = getenv ("VTYSH_PAGER") ;

  if (form == NULL)
    form = "more;more -%d" ;

  vtysh_pager_form  = strdup(form) ;
  vtysh_pager       = NULL ;
  vtysh_pager_lines = 0 ;
} ;

/*------------------------------------------------------------------------------
 * Set the vtysh_pager for the given number of lines, where:
 *
 *   lines <  0 -- sets to default for the pager
 *   lines == 0 -- turns of pager
 *   lines >  0 -- sets to given number of lines, if possible
 *
 * Returns:  previous vtysh_pager_lines setting
 */
extern int
vtysh_pager_set(int lines)
{
  int lines_was ;

  lines_was = vtysh_pager_lines ;
  vtysh_pager_lines = lines ;

  /* Turn off pager and free any existing name string.
   */
  if (vtysh_pager != NULL)
    {
      free(vtysh_pager) ;
      vtysh_pager = NULL ;
    } ;

  /* For lines == 0 we leave the pager off, otherwise we set the number of
   * lines given if > 0, or the default for the pager if < 0
   */
  if (lines != 0)
    {
      const char* s ;
      ulen l ;

      /* Find the ';', if any.
       *
       * Set s to start of form to use with explicit length if any.
       */
      s = strchr(vtysh_pager_form, ';') ;

      if ((lines > 0) && (s != NULL))
        {
          ulen l ;

          ++s ;                         /* step past ';'                */
          l = strlen(s) + 10 + 1 ;      /* form + some for digits       */

          vtysh_pager = malloc(l) ;
          snprintf(vtysh_pager, l, s, lines) ;
        }
      else
        {
          if (s != NULL)
            l = s - vtysh_pager_form ;
          else
            l = strlen(vtysh_pager_form) ;

          vtysh_pager = malloc(l + 1) ;
          memcpy(vtysh_pager, vtysh_pager_form, l) ;
          vtysh_pager[l] = '\0' ;
        } ;
    } ;

  /* Returns the now current pager setting
   */
  return lines_was ;
} ;

/*==============================================================================
 * Uses readline and some moderately deep readline completion magic.
 */

/* Result of cmd_complete_command() call will be stored here
 * and used in new_completion() in order to put the space in
 * correct places only. */
int complete_status;


static rl_command_func_t vtysh_describe ;
//static rl_compentry_func_t vtysh_completion_entry_function ;
static rl_completion_func_t vtysh_attempted_completion_function ;
static rl_compentry_func_t vtysh_entry_func ;

static int vtysh_completion(void) ;
static int vtysh_help_parse(const char* literal) ;
static void vtysh_show_descriptions(void) ;
static void vtysh_complete_keyword(elstring keyword, bool complete) ;

/*------------------------------------------------------------------------------
 *
 */
extern void
vtysh_readline_init (void)
{
  /* readline related settings.
   */
  rl_bind_key ('?', vtysh_describe);

//rl_completion_entry_function = vtysh_completion_entry_function;
  rl_attempted_completion_function = vtysh_attempted_completion_function;

  /* do not append space after completion. It will be appended
   * in new_completion() function explicitly.
   */
  rl_completion_append_character = '\0';
}

#if 0
/*------------------------------------------------------------------------------
 * Minimal rl_completion_entry_function(), to disable readline's filename
 * completion.
 */
static char *
vtysh_completion_entry_function(const char *ignore, int invoking_key)
{
  return NULL;
} ;
#endif

/*------------------------------------------------------------------------------
 * Respond to '?' on the command line.
 */
static int
vtysh_describe (int count, int key)
{
  uint  n_items ;

  /* Parse etc. to get list of possibilities -- deals with exception cases.
   *
   * If then have something to show, show it !
   */
  n_items = vtysh_help_parse("?") ;

  if (n_items > 0)
    vtysh_show_descriptions() ;

  return 0;
} ;

/*------------------------------------------------------------------------------
 * For 'rl_attempted_completion_function' -- ie: respond to TAB on command line
 *
 *   text   -- string that completion may replace
 *   start  )  boundaries of the string in the rl_line_buffer
 *   end    )
 *
 * For a command line:
 *
 *   ... abcde ...
 *
 * if the cursor is on the 'c', then get: text  == "ab"
 *                                        start == location of the 'a'
 *                                        end   == location of the 'c'
 */
static char **
vtysh_attempted_completion_function (const char *text, int start, int end)
{
  char** matches;

  matches = rl_completion_matches(text, vtysh_entry_func) ;

  if (matches)
    {
#if 0
      rl_point = rl_end;
      if (complete_status == CMD_COMPLETE_FULL_MATCH)
        rl_pending_input = ' ';
#endif
    }

  rl_attempted_completion_over = true ;

  return matches;
} ;

/*------------------------------------------------------------------------------
 * Return next possible completion -- called by rl_completion_matches().
 *
 * If state == 0, this is the first call of an attempted completion, so is
 * where all the work is done.  On all subsequent calls, the second and
 * subsequent completions are returned.
 */
static char *
vtysh_entry_func(const char *text, int state)
{
  static int index   = 0 ;
  static int n_items = 0 ;

  if (state == 0)
    {
      n_items = vtysh_completion() ;
      index = 0 ;
    } ;

  if (index < n_items)
    {
      cmd_item item ;
      ulen     str_len ;
      char*    entry ;

      item = vector_get_item(vtysh_vty->exec->parsed->item_v, index) ;
      ++index ;

      str_len = els_len_nn(item->str) ;
      entry = malloc(str_len + 1) ;

      memcpy(entry, els_body_nn(item->str), str_len) ;
      entry[str_len] = '\0' ;

      return entry ;
    } ;

  return NULL ;
} ;

/*------------------------------------------------------------------------------
 * Work out whether or how can complete the current work in the current
 * command line.
 *
 * If there are multiple ways to do this, then vtysh_vty->exec->parsed->item_v
 * is set to a list of the possibilities.
 *
 * If there is one way to do this, then does that here.
 */
static int
vtysh_completion(void)
{
  cmd_item item ;
  uint     n_items ;

  /* Parse etc. to get list of possibilities -- deals with exception cases.
   */
  n_items = vtysh_help_parse(" ") ;

  if (n_items == 0)
    return 0 ;                  /* no completions       */

  /* If have more than one possible completions then see if we should
   * extend the current token or move to the end of the current token.
   *
   * If not return the number of completions to be displayed.
   */
  if (n_items > 1)
    {
      elstring_t els ;

      if (cmd_part_complete(vtysh_vty->exec->parsed, els))
        {
          vtysh_complete_keyword(els, false) ;
          n_items = 0 ;
        } ;

      return n_items ;
    } ;

  /* One possible completion.
   *
   * If is keyword, replace current token and leave positioned after it.
   * Otherwise, give hint as to possible value for token.
   */
  item = vector_get_item(vtysh_vty->exec->parsed->item_v, 0) ;

  switch (item->type)
    {
      case item_null:
        zabort("invalid item_null") ;

      case item_eol:

      case item_option_word:

      case item_vararg:

      case item_word:

      case item_ipv6_prefix:
      case item_ipv6_address:
      case item_ipv4_prefix:
      case item_ipv4_address:

      case item_range:
        vtysh_show_descriptions() ;
        break ;

      case item_keyword:
        vtysh_complete_keyword(item->str, true) ;
        break ;

      default:
        zabort("unknown item type") ;
    } ;

  return 0 ;    /* nothing more to do   */
} ;

/*------------------------------------------------------------------------------
 * Deal with parsing of current line and discovering how much help can be
 * given.
 *
 * Sets: vtysh_vty->exec->context and vtysh->exec->context->line
 *       vtysh_vty->exec->parsed
 *
 * Returns: number of entries in the exec->parsed item_v vector (if any)
 *
 * If returns 0, there is nothing further to be done -- may have issued
 * error message or inserted the literal character.
 */
static int
vtysh_help_parse(const char* literal)
{
  cmd_context  context ;
  cmd_parsed   parsed ;

  const char* msg ;
  uint        n_items ;
  cmd_ret_t   ret ;

  /* First, fill command line from: rl_line_buffer, rl_point and rl_end.
   */
  context = vtysh_vty->exec->context ;
  parsed  = vtysh_vty->exec->parsed ;

  context->line  = vty_vtysh_prep_line(vtysh_vty, rl_line_buffer, rl_end,
                                                                     rl_point) ;

  /* Tokenise and work out where we are on the line...
   *
   * ...if we are in "special" place, simply insert the literal and return.
   */
  if (cmd_token_position(parsed, context->line))
    {
      rl_insert_text(literal) ;
      rl_redisplay() ;
      return 0 ;
    } ;

  /* The preflight checks avoid getting into trouble doing command completion
   * on a line with comment.
   */
  msg = cmd_help_preflight(parsed) ;
  if (msg != NULL)
    {
      printf("\n%% %s\n", msg) ;
      rl_on_new_line() ;
      return 0 ;
    } ;

  /* Now see what the cmd_completion can come up with.
   */
  ret = cmd_completion(parsed, context) ;

  if (ret == CMD_ERR_PARSING)
    {
#if 0
      if (cli->help_parsed->eloc >= 0)
        {
          uint eloc = cli->prompt_len + cli->help_parsed->eloc ;

          uty_cli_help_newline(cli) ;   /* clears cli_drawn etc.        */
          uty_cli_write_n(cli, telnet_dots, eloc) ;
          uty_cli_write_s(cli, "^") ;
        } ;
#endif
      printf("\n%% %s\n", qs_string(parsed->emess)) ;
      rl_on_new_line() ;
      return 0 ;
    } ;

  /* Will now have 0, 1 or more items which match at the current
   * cursor token.
   */
  n_items = vector_length(parsed->item_v) ;

  if (n_items == 0)
    {
      printf("\n%% %s\n", "command not recognised") ;
      rl_on_new_line() ;
    } ;

  return n_items ;
} ;

/*------------------------------------------------------------------------------
 * Show descriptions for all items in vtysh_vty->exec->parsed->item_v
 *
 * Starts by moving to new blank line, and ends by telling readline to redraw
 * the command line.
 */
static void
vtysh_show_descriptions(void)
{
  vector         list ;
  vector_index_t i ;

  int rows, cols ;

  rl_crlf() ;

  rl_get_screen_size(&rows, &cols) ;

  list = uty_cli_make_describe_list(vtysh_vty->exec->parsed->item_v, cols) ;

  for (i = 0 ; i < vector_length(list) ; ++i)
    {
      qstring  qs = vector_get_item(list, i) ;

      printf("%s\n", qs_string(qs)) ;

      qs_free(qs) ;
    } ;

  vector_free(list) ;

  rl_on_new_line();
} ;

/*------------------------------------------------------------------------------
 * Completion, replacing an existing keyword or inserting at the end.
 */
static void
vtysh_complete_keyword(elstring keyword, bool complete)
{
  int pre, rep, ins, mov ;
  char* str ;
  ulen str_len ;

  cmd_complete_keyword(vtysh_vty->exec->parsed, &pre, &rep, &ins, &mov) ;

  str_len = els_len_nn(keyword) ;
  str = malloc(str_len + ins + 1) ;
  memcpy(str, els_body_nn(keyword), str_len) ;
  if (complete)
    {
      while (ins-- > 0)
        str[str_len++] = ' ' ;
    } ;

  str[str_len] = '\0' ;

  rl_begin_undo_group() ;
  rl_point += pre ;
  rl_delete_text(rl_point, rl_point + rep) ;
  rl_insert_text(str) ;
  if (complete)
    rl_point += mov ;
  rl_end_undo_group() ;

  rl_redisplay() ;
} ;

/*==============================================================================
 * The client daemons
 */

static daemon_set_t vtysh_daemons_list(vty vty, int argc, argv_t argv) ;
static bool vtysh_daemons_check_ok(vty vty, daemon_set_t daemons) ;

/*------------------------------------------------------------------------------
 * Connect to all given daemons or to all available daemons.
 *
 * If given daemons is NULL, connect to all available -- which may be none
 * at all.
 *
 * Otherwise, deamons string is expected to contain names separated by spaces,
 * or commas: all of which must be valid names, and all of which must be
 * available.
 *
 * In the event of errors, uses vty_err(vty).
 *
 * Returns: true  => connected as required...
 *                   ...noting that if no daemons were required, may not have
 *                      any open.
 *          false => failed to connect exactly as required...
 *                   ...message sent to vty_err(vty)
 *                      will have connected to as many of the required daemons
 *                      as possible.
 */
extern bool
vtysh_daemons_connect(vty vtysh, daemon_set_t daemons, daemon_set_t required)
{
  daemon_set_t connected ;

  connected = vty_vtysh_open_clients(vtysh, daemons, required) ;

  return (required == 0) || (connected == required) ;
} ;

/*------------------------------------------------------------------------------
 * See if given daemons list from the -d options contains one or more valid
 * daemons, and if so which.
 *
 * List should not be empty because this is from the -d option(s)
 *
 * Returns:  0 => list empty, or contains unrecognised or invalid daemon
 *                                   names -- error message output by vty_err().
 *           Otherwise is set of daemons to be connected to.
 */
extern daemon_set_t
vtysh_daemons_list_ok(vty vtysh, qstring daemon_list)
{
  daemon_set_t daemons ;
  qstring  list ;
  bool     ok ;

  list = qs_set(NULL, daemon_list) ;

  daemons = cmd_daemons_from_list(list) ;

  ok = vtysh_daemons_check_ok(vtysh, daemons) ;

  if (qs_len(list) != 0)
    {
      vty_err(vtysh, "%% Failed: did not recognise: -d %s\n",
                                                        qs_string(list)) ;
      ok = false ;
    } ;

  if ((daemons == 0) && ok)
    {
      vty_err(vtysh, "%% Failed: empty -d !\n") ;
      ok = false ;
    } ;

  qs_free(list) ;

  return ok ? daemons : 0 ;
} ;

/*------------------------------------------------------------------------------
 * Connect to the given daemons
 */
DEFUN (vtysh_connect_daemons,
       vtysh_connect_daemons_cmd,
       "connect .DAEMONS",
       "Connect to daemons"
       "List of daemons to connect to\n")
{
  daemon_set_t daemons ;

  daemons = vtysh_daemons_list(vty, argc, argv) ;
  if (daemons == 0)
    return CMD_WARNING ;

  vty_out(vty, "TBD: %s\n", __func__) ;

  return CMD_SUCCESS;
} ;

/*------------------------------------------------------------------------------
 * Disconnect from the given daemons
 */
DEFUN (vtysh_disconnect_daemons,
       vtysh_disconnect_daemons_cmd,
       "disconnect .DAEMONS",
       "Disconnect from daemons"
       "List of daemons to disconnect from\n")
{
  daemon_set_t daemons ;

  daemons = vtysh_daemons_list(vty, argc, argv) ;
  if (daemons == 0)
    return CMD_WARNING ;

  vty_out(vty, "TBD: %s\n", __func__) ;

  return CMD_SUCCESS;
} ;

ALIAS (vtysh_disconnect_daemons,
       no_vtysh_connect_daemons_cmd,
       "no connect .DAEMONS",
       NO_STR
       "Disconnect from daemons"
       "List of daemons to disconnect from\n")

/*------------------------------------------------------------------------------
 * Show which daemons is currently connected to
 */
DEFUN (vtysh_show_daemons,
       vtysh_show_daemons_cmd,
       "show daemons",
       SHOW_STR
       "Show list of running daemons\n")
{
  vtysh_connected_ok(vty, false /* not quiet */, false /* not fail */) ;
  return CMD_SUCCESS;
} ;

/*------------------------------------------------------------------------------
 * Show clients to which we are currently connected
 *
 * If connected to at least one daemon, show which -- unless "quiet".  Return
 * true <=> OK.
 *
 * If not connected to any daemon: if "fail", issue error message to vty_err
 * and return false, otherwise output and NB message and return true.  (Note
 * that outputs the NB, even if is "quiet".
 */
extern bool
vtysh_connected_ok(vty vtysh, bool quiet, bool fail)
{
  daemon_set_t daemons ;

  daemons = vty_vtysh_check_clients(vtysh) ;

  if (daemons != 0)
    {
      if (!quiet)
        {
          qstring  list ;

          list = cmd_daemons_make_list(NULL, daemons) ;
          vty_out(vtysh, "Connected to: %s\n", qs_string(list)) ;

          qs_free(list) ;
        } ;
    }
  else if (!fail)
    {
      vty_out(vtysh, "Not connected to any daemon\n") ;
    }
  else
    {
      vty_err(vtysh, "%% not connected to any daemon\n") ;
      return false ;
    }

  vty_cmd_out_push(vtysh) ;
  return true ;
} ;

/*------------------------------------------------------------------------------
 * Process command arguments as a list of daemon names.
 *
 * If there are any duff daemons, report error by vty_err().
 */
static daemon_set_t
vtysh_daemons_list(vty vtysh, int argc, argv_t argv)
{
  daemon_set_t daemons ;

  daemons = cmd_deamon_list_arg(vtysh, argc, argv) ;

  if (!vtysh_daemons_check_ok(vtysh, daemons))
    daemons = 0 ;

  return daemons ;
} ;

/*------------------------------------------------------------------------------
 * Filter daemons to remove any that vtysh cannot connect to.
 *
 * If there are any duff daemons, report error by vty_err().
 *
 * Returns:  true <=> OK
 */
static bool
vtysh_daemons_check_ok(vty vtysh, daemon_set_t daemons)
{
  if ((daemons & ~ALL_RDS) == 0)
    return true ;

  vty_err(vtysh, "%% vtysh cannot connect to ") ;

  daemons &= ~ALL_RDS ;

  if (daemons & VTYSH_VD)
    {
      daemons &= ~VTYSH_VD ;
      vty_err(vtysh, "itself(!)%s", (daemons != 0) ? " or " : "\n") ;
    } ;

  if (daemons != 0)
    {
      qstring list ;

      list = cmd_daemons_make_list(NULL, daemons) ;
      vty_err(vtysh, "%s\n", qs_string(list)) ;

      qs_free(list) ;
    } ;

  return false ;
} ;

/*==============================================================================
 *
 */


/*------------------------------------------------------------------------------
 * Collect the integrated config, and then squirt all over the terminal.
 *
 * Note that we force the pager on, in default state, if it is off.
 */
static cmd_ret_t
vtysh_show_integrated_config(vty vty)
{
  cmd_ret_t ret ;

  if (false)
    {
      vty_out(vty, "%% %s TBA\n", __func__) ;
      return CMD_WARNING ;
    } ;

  /* First collect the integrated config.
   */
  ret = vtysh_config_collect_integrated(vty, true) ;

  /* Force out any pending output, and if the pager was off, set it on at the
   * default for the terminal.
   */
  if (ret == CMD_SUCCESS)
    ret = vty_cmd_out_push(vty) ;

  /* Spray result of collected configuration all over the screen, forcing the
   * pager on
   */
  if (ret == CMD_SUCCESS)
    {
      int       lines_was ;

      lines_was = vtysh_pager_lines ;
      if (lines_was == 0)
        vtysh_pager_set(-1) ;

      vty->config_to_vtysh = false ;
      ret = vty_write_config(vty, vtysh_config_write_config_node) ;

      if (lines_was == 0)
        vtysh_pager_set(0) ;
    } ;

  /* Discard the collected configuration & return
   */
//vtysh_config_reset_integrated() ;

  return ret ;
} ;

/*------------------------------------------------------------------------------
 * Collect the integrated config, and then write to the integrated config
 * file.
 */
static cmd_ret_t
vtysh_write_integrated_config(vty vty)
{
  cmd_ret_t ret ;

  if (false)
    {
      vty_out(vty, "%% %s TBA\n", __func__) ;
      return CMD_WARNING ;
    } ;

  /* First collect the integrated config.
   */
  ret = vtysh_config_collect_integrated(vty, true) ;

  /* Send result of collected configuration to the integrated configuration
   * file.
   */
  if (ret == CMD_SUCCESS)
    ret = vty_write_config_file(vty, host.int_config_file,
                                         vtysh_config_write_config_node, true) ;

  /* Discard the collected configuration & return
   */
//vtysh_config_reset_integrated() ;

  return ret ;
} ;

/*------------------------------------------------------------------------------
 * Turning on/off the integrated config
 *
 * When set, "service integrated-vtysh-config" affects "write file" and all its
 * aliases, such that they all become aliasies of "write integrated".  Also,
 * sets the host.config_dir (~~/) to the integrated configuration file
 * directory.
 *
 * When clear, "write file" and all its aliases, write each daemon's own
 * configuration, and the host.config_dir (~~/) is set to the vtysh's own
 * configuration file directory.
 */
extern cmd_ret_t
vtysh_set_integrated_config(on_off_b integrated)
{
  vtysh_integrated_vtysh_config = integrated ;

  cmd_set_integrated_vtysh_config(integrated) ;

  return CMD_SUCCESS ;
} ;

DEFUN (vtysh_integrated_config,
       vtysh_integrated_config_cmd,
       "service integrated-vtysh-config",
       "Set up miscellaneous service\n"
       "Write configuration into integrated file\n")
{
  return vtysh_set_integrated_config(on) ;
} ;

DEFUN (no_vtysh_integrated_config,
       no_vtysh_integrated_config_cmd,
       "no service integrated-vtysh-config",
       NO_STR
       "Set up miscellaneous service\n"
       "Write configuration into integrated file\n")
{
  return vtysh_set_integrated_config(off) ;
} ;

/*------------------------------------------------------------------------------
 * Set the new terminal length.
 *
 * Has no effect on the current, top level, command.
 */
static cmd_ret_t
vtysh_terminal_length(vty vty, int lines)
{
  vtysh_pager_set(lines) ;

  return CMD_SUCCESS;
} ;

/*------------------------------------------------------------------------------
 * Show the vtysh history
 */
static cmd_ret_t
vtysh_show_history(vty vty)
{
  HIST_ENTRY** history ;
  HIST_ENTRY*  entry ;

  history = history_list() ;
  if (history != NULL)
    entry = *history++ ;
  else
    entry = NULL ;

  while (entry != NULL)
    {
      vty_out(vty, "%s\n", entry->line) ;
      entry = *history++ ;
    } ;

  return CMD_SUCCESS ;
} ;

/*------------------------------------------------------------------------------
 * Execute command in a child process.
 */
static int
execute_command (const char *command, int argc, const char *arg1,
		 const char *arg2)
{
  int ret;
  pid_t pid;
  int status;

  /* Call fork(). */
  pid = fork ();

  if (pid < 0)
    {
      /* Failure of fork(). */
      fprintf (stderr, "Can't fork: %s\n", errtoa(errno, 0).str);
      exit (1);
    }
  else if (pid == 0)
    {
      /* This is child process. */
      switch (argc)
	{
	case 0:
	  ret = execlp (command, command, (const char *)NULL);
	  break;
	case 1:
	  ret = execlp (command, command, arg1, (const char *)NULL);
	  break;
	case 2:
	  ret = execlp (command, command, arg1, arg2, (const char *)NULL);
	  break;
	}

      /* When execlp succeeds, this part is not executed.
       */
      fprintf (stderr, "Can't execute %s: %s\n", command, errtoa(errno, 0).str);
      exit (1);
    }
  else
    {
      /* This is parent. */
      execute_flag = 1;
      ret = wait4 (pid, &status, 0, NULL);
      execute_flag = 0;
    }
  return 0;
}

DEFUN (vtysh_ping,
       vtysh_ping_cmd,
       "ping WORD",
       "Send echo messages\n"
       "Ping destination address or hostname\n")
{
  execute_command ("ping", 1, argv[0], NULL);
  return CMD_SUCCESS;
}

ALIAS (vtysh_ping,
       vtysh_ping_ip_cmd,
       "ping ip WORD",
       "Send echo messages\n"
       "IP echo\n"
       "Ping destination address or hostname\n")

DEFUN (vtysh_traceroute,
       vtysh_traceroute_cmd,
       "traceroute WORD",
       "Trace route to destination\n"
       "Trace route to destination address or hostname\n")
{
  execute_command ("traceroute", 1, argv[0], NULL);
  return CMD_SUCCESS;
}

ALIAS (vtysh_traceroute,
       vtysh_traceroute_ip_cmd,
       "traceroute ip WORD",
       "Trace route to destination\n"
       "IP trace\n"
       "Trace route to destination address or hostname\n")

#ifdef HAVE_IPV6
DEFUN (vtysh_ping6,
       vtysh_ping6_cmd,
       "ping ipv6 WORD",
       "Send echo messages\n"
       "IPv6 echo\n"
       "Ping destination address or hostname\n")
{
  execute_command ("ping6", 1, argv[0], NULL);
  return CMD_SUCCESS;
}

DEFUN (vtysh_traceroute6,
       vtysh_traceroute6_cmd,
       "traceroute ipv6 WORD",
       "Trace route to destination\n"
       "IPv6 trace\n"
       "Trace route to destination address or hostname\n")
{
  execute_command ("traceroute6", 1, argv[0], NULL);
  return CMD_SUCCESS;
}
#endif

DEFUN (vtysh_telnet,
       vtysh_telnet_cmd,
       "telnet WORD",
       "Open a telnet connection\n"
       "IP address or hostname of a remote system\n")
{
  execute_command ("telnet", 1, argv[0], NULL);
  return CMD_SUCCESS;
}

DEFUN (vtysh_telnet_port,
       vtysh_telnet_port_cmd,
       "telnet WORD PORT",
       "Open a telnet connection\n"
       "IP address or hostname of a remote system\n"
       "TCP Port number\n")
{
  execute_command ("telnet", 2, argv[0], argv[1]);
  return CMD_SUCCESS;
}

DEFUN (vtysh_ssh,
       vtysh_ssh_cmd,
       "ssh WORD",
       "Open an ssh connection\n"
       "[user@]host\n")
{
  execute_command ("ssh", 1, argv[0], NULL);
  return CMD_SUCCESS;
}

DEFUN (vtysh_start_shell,
       vtysh_start_shell_cmd,
       "start-shell",
       "Start UNIX shell\n")
{
  execute_command ("sh", 0, NULL, NULL);
  return CMD_SUCCESS;
}

DEFUN (vtysh_start_bash,
       vtysh_start_bash_cmd,
       "start-shell bash",
       "Start UNIX shell\n"
       "Start bash\n")
{
  execute_command ("bash", 0, NULL, NULL);
  return CMD_SUCCESS;
}

DEFUN (vtysh_start_zsh,
       vtysh_start_zsh_cmd,
       "start-shell zsh",
       "Start UNIX shell\n"
       "Start Z shell\n")
{
  execute_command ("zsh", 0, NULL, NULL);
  return CMD_SUCCESS;
}

/*==============================================================================
 * Commands and command setup
 */

/* The table of commands collected by vtysh/extract.pl
 */
extern cmd_table vtysh_collected_cmd_table ;

/* Commands for vtysh itself
 */
CMD_INSTALL_TABLE(static, vtysh_own_cmd_table, VTYSH_VD) =
{
  { VIEW_NODE,       &vtysh_show_daemons_cmd                            },
  { VIEW_NODE,       &vtysh_connect_daemons_cmd                         },
  { VIEW_NODE,       &vtysh_disconnect_daemons_cmd                      },
  { VIEW_NODE,       &no_vtysh_connect_daemons_cmd                      },

  { ENABLE_NODE,     &vtysh_show_daemons_cmd                            },
  { ENABLE_NODE,     &vtysh_connect_daemons_cmd                         },
  { ENABLE_NODE,     &vtysh_disconnect_daemons_cmd                      },
  { ENABLE_NODE,     &no_vtysh_connect_daemons_cmd                      },

  { VIEW_NODE,       &vtysh_ping_cmd                                    },
  { VIEW_NODE,       &vtysh_ping_ip_cmd                                 },
  { VIEW_NODE,       &vtysh_traceroute_cmd                              },
  { VIEW_NODE,       &vtysh_traceroute_ip_cmd                           },
  { VIEW_NODE,       &vtysh_ping6_cmd                                   },
  { VIEW_NODE,       &vtysh_traceroute6_cmd                             },
  { VIEW_NODE,       &vtysh_telnet_cmd                                  },
  { VIEW_NODE,       &vtysh_telnet_port_cmd                             },
  { VIEW_NODE,       &vtysh_ssh_cmd                                     },
  { VIEW_NODE,       &vtysh_start_shell_cmd                             },
  { VIEW_NODE,       &vtysh_start_bash_cmd                              },
  { VIEW_NODE,       &vtysh_start_zsh_cmd                               },

  { ENABLE_NODE,     &vtysh_ping_cmd                                    },
  { ENABLE_NODE,     &vtysh_ping_ip_cmd                                 },
  { ENABLE_NODE,     &vtysh_traceroute_cmd                              },
  { ENABLE_NODE,     &vtysh_traceroute_ip_cmd                           },
  { ENABLE_NODE,     &vtysh_ping6_cmd                                   },
  { ENABLE_NODE,     &vtysh_traceroute6_cmd                             },
  { ENABLE_NODE,     &vtysh_telnet_cmd                                  },
  { ENABLE_NODE,     &vtysh_telnet_port_cmd                             },
  { ENABLE_NODE,     &vtysh_ssh_cmd                                     },
  { ENABLE_NODE,     &vtysh_start_shell_cmd                             },
  { ENABLE_NODE,     &vtysh_start_bash_cmd                              },
  { ENABLE_NODE,     &vtysh_start_zsh_cmd                               },

  { CONFIG_NODE,     &vtysh_integrated_config_cmd                       },
  { CONFIG_NODE,     &no_vtysh_integrated_config_cmd                    },

  CMD_INSTALL_END
} ;

/* The table of call backs for the small number of common commands which have
 * special handling in the vtysh.
 */
vtysh_cmd_call_backs_t vtysh_cmd_call_back_table =
{
    .terminal_length         = vtysh_terminal_length,
    .show_history            = vtysh_show_history,
    .show_integrated_config  = vtysh_show_integrated_config,
    .write_integrated_config = vtysh_write_integrated_config,
} ;

/*------------------------------------------------------------------------------
 * Inititialise command handling for vtysh and install all nodes and vtysh
 * specific commands, then all the commands collected by vtysh/extract.pl.
 */
extern void
vtysh_cmd_init(void)
{
  /* Initialize commands -- install the basic nodes and the VTY_NODE
   */
  cmd_table_init (VTYSH_VD | ALL_RDS);

  /* Fire up all the lib/xx things which have commands that we now need.
   *
   * extract.pl does *not* pick these up -- there's no need, we can do it now !
   */
  distribute_list_cmd_init () ;
  access_list_cmd_init() ;
  if_cmd_init(NULL) ;
  if_rmap_cmd_init() ;
  keychain_cmd_init() ;
  prefix_list_cmd_init() ;
  route_map_cmd_init () ;

  /* Install specific vtysh commands
   */
  cmd_install_table(vtysh_own_cmd_table) ;

  /* Install the collected commands for all other daemons
   */
  cmd_install_table(vtysh_collected_cmd_table) ;

  /* All commands have now been installed
   */
  cmd_table_complete() ;
} ;

/*------------------------------------------------------------------------------
 * Initialise vty for vtysh, set up vtysh_vty and set output with no pager.
 *
 * Set up vtysh_vty initially for stdout/stderr.
 */
extern void
vtysh_vty_init (bool no_prefix)
{
  vty_init_for_vtysh() ;

  vtysh_vty = vty_vtysh_open(&vtysh_cmd_call_back_table, no_prefix) ;

  vtysh_pager_init() ;
} ;