pg: exec file fixes
[vpp.git] / src / vlib / unix / cli.c
index 2f34c8d..7b9a231 100644 (file)
@@ -449,13 +449,6 @@ static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
 
 #undef _
 
-/** CLI session events. */
-typedef enum
-{
-  UNIX_CLI_PROCESS_EVENT_READ_READY,  /**< A file descriptor has data to be read. */
-  UNIX_CLI_PROCESS_EVENT_QUIT,       /**< A CLI session wants to close. */
-} unix_cli_process_event_type_t;
-
 /** CLI session telnet negotiation timer events. */
 typedef enum
 {
@@ -1263,9 +1256,9 @@ unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
    */
   unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
 
-  if (!um->cli_no_banner)
+  if (!um->cli_no_banner && (um->flags & UNIX_FLAG_NOBANNER) == 0)
     {
-      if (cf->ansi_capable)
+      if (cf->ansi_capable && (um->flags & UNIX_FLAG_NOCOLOR) == 0)
        {
          banner = unix_cli_banner_color;
          len = ARRAY_LEN (unix_cli_banner_color);
@@ -2708,6 +2701,7 @@ unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
     vec_free (cf->command_history[i]);
 
   vec_free (cf->command_history);
+  vec_free (cf->input_vector);
 
   clib_file_del (fm, uf);
 
@@ -2892,9 +2886,9 @@ unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
        * the same new name.
        * Then, throw away the old shared name-vector.
        */
-      for (i = 0; i < vec_len (vlib_mains); i++)
+      for (i = 0; i < vlib_get_n_threads (); i++)
        {
-         this_vlib_main = vlib_mains[i];
+         this_vlib_main = vlib_get_main_by_index (i);
          if (this_vlib_main == 0)
            continue;
          n = vlib_get_node (this_vlib_main,
@@ -3358,11 +3352,16 @@ unix_cli_exec (vlib_main_t * vm,
   char *file_name;
   int fd;
   unformat_input_t sub_input;
+  unformat_input_t _line_input, *line_input = &_line_input;
   clib_error_t *error;
-
+  unix_cli_main_t *cm = &unix_cli_main;
+  unix_cli_file_t *cf;
+  u8 *file_data = 0;
   file_name = 0;
   fd = -1;
   error = 0;
+  struct stat s;
+
 
   if (!unformat (input, "%s", &file_name))
     {
@@ -3379,25 +3378,69 @@ unix_cli_exec (vlib_main_t * vm,
     }
 
   /* Make sure its a regular file. */
-  {
-    struct stat s;
+  if (fstat (fd, &s) < 0)
+    {
+      error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
+      goto done;
+    }
 
-    if (fstat (fd, &s) < 0)
-      {
-       error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
-       goto done;
-      }
+  if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
+    {
+      error = clib_error_return (0, "not a regular file `%s'", file_name);
+      goto done;
+    }
 
-    if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
-      {
-       error = clib_error_return (0, "not a regular file `%s'", file_name);
-       goto done;
-      }
-  }
+  /* Read the file */
+  vec_validate (file_data, s.st_size);
 
-  unformat_init_clib_file (&sub_input, fd);
+  if (read (fd, file_data, s.st_size) != s.st_size)
+    {
+      error = clib_error_return_unix (0, "Failed to read %d bytes from '%s'",
+                                     s.st_size, file_name);
+      vec_free (file_data);
+      goto done;
+    }
+
+  /* The macro expander expects a c string... */
+  vec_add1 (file_data, 0);
+
+  unformat_init_vector (&sub_input, file_data);
+
+  /* Run the file contents through the macro processor */
+  if (vec_len (sub_input.buffer) > 1)
+    {
+      u8 *expanded;
+      clib_macro_main_t *mm = 0;
+
+      /* Initial config process? Use the global macro table. */
+      if (pool_is_free_index
+         (cm->cli_file_pool, cm->current_input_file_index))
+       mm = &cm->macro_main;
+      else
+       {
+         /* Otherwise, use the per-cli-process macro table */
+         cf = pool_elt_at_index (cm->cli_file_pool,
+                                 cm->current_input_file_index);
+         mm = &cf->macro_main;
+       }
+
+      expanded = (u8 *) clib_macro_eval (mm,
+                                        (i8 *) sub_input.buffer,
+                                        1 /* complain */ ,
+                                        0 /* level */ ,
+                                        8 /* max_level */ );
+      /* Macro processor NULL terminates the return */
+      _vec_len (expanded) -= 1;
+      vec_reset_length (sub_input.buffer);
+      vec_append (sub_input.buffer, expanded);
+      vec_free (expanded);
+    }
 
-  vlib_cli_input (vm, &sub_input, 0, 0);
+  while (unformat_user (&sub_input, unformat_line_input, line_input))
+    {
+      vlib_cli_input (vm, line_input, 0, 0);
+      unformat_free (line_input);
+    }
   unformat_free (&sub_input);
 
 done:
@@ -3530,7 +3573,7 @@ unix_show_files (vlib_main_t * vm,
                   "Read", "Write", "Error", "File Name", "Description");
 
   /* *INDENT-OFF* */
-  pool_foreach (f, fm->file_pool,(
+  pool_foreach (f, fm->file_pool)
    {
       int rv;
       s = format (s, "/proc/self/fd/%d%c", f->file_descriptor, 0);
@@ -3543,7 +3586,7 @@ unix_show_files (vlib_main_t * vm,
                       f->read_events, f->write_events, f->error_events,
                       path, f->description);
       vec_reset_length (s);
-    }));
+    }
   /* *INDENT-ON* */
   vec_free (s);
 
@@ -3683,7 +3726,7 @@ unix_cli_show_cli_sessions (vlib_main_t * vm,
 
 #define fl(x, y) ( (x) ? toupper((y)) : tolower((y)) )
   /* *INDENT-OFF* */
-  pool_foreach (cf, cm->cli_file_pool, ({
+  pool_foreach (cf, cm->cli_file_pool)  {
     uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
     n = vlib_get_node (vm, cf->process_node_index);
     vlib_cli_output (vm,
@@ -3696,7 +3739,7 @@ unix_cli_show_cli_sessions (vlib_main_t * vm,
                     fl (cf->line_mode, 'l'),
                     fl (cf->has_epipe, 'p'),
                     fl (cf->ansi_capable, 'a'));
-  }));
+  }
   /* *INDENT-ON* */
 #undef fl