Skip to content

Commit 26938ab

Browse files
committed
treat '-f -' as a request to read commands from stdin in a way which works
with a pipe or a file redirection Write to stdout if in a pipe Need to update the header for vtysh_read_file Try without a comment
1 parent 88351c8 commit 26938ab

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

vtysh/vtysh_config.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,13 @@ void vtysh_config_dump(void)
467467
}
468468

469469
/* Read up configuration file from file_name. */
470-
static int vtysh_read_file(FILE *confp)
470+
static int vtysh_read_file(FILE *confp, int output_fileno)
471471
{
472472
struct vty *vty;
473473
int ret;
474474

475475
vty = vty_new();
476-
vty->wfd = STDERR_FILENO;
476+
vty->wfd = output_fileno;
477477
vty->type = VTY_TERM;
478478
vty->node = CONFIG_NODE;
479479

@@ -497,16 +497,22 @@ int vtysh_read_config(const char *config_default_dir)
497497
FILE *confp = NULL;
498498
int ret;
499499

500-
confp = fopen(config_default_dir, "r");
500+
int is_real_file = strcmp(config_default_dir,"-") ;
501+
if ( is_real_file == 0 ) {
502+
confp = stdin ;
503+
}
504+
else {
505+
confp = fopen(config_default_dir, "r");
506+
}
501507
if (confp == NULL) {
502508
fprintf(stderr,
503509
"%% Can't open configuration file %s due to '%s'.\n",
504510
config_default_dir, safe_strerror(errno));
505511
return (CMD_ERR_NO_FILE);
506512
}
507513

508-
ret = vtysh_read_file(confp);
509-
fclose(confp);
514+
ret = vtysh_read_file(confp, is_real_file ? STDERR_FILENO : STDOUT_FILENO );
515+
if ( is_real_file ) fclose(confp);
510516

511517
return (ret);
512518
}

vtysh/vtysh_main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,10 @@ int main(int argc, char **argv, char **env)
551551
}
552552

553553
if (inputfile) {
554-
vtysh_flock_config(inputfile);
554+
int is_real_file = ( strcmp(inputfile, "-") ) ;
555+
if ( is_real_file ) vtysh_flock_config(inputfile);
555556
ret = vtysh_read_config(inputfile);
556-
vtysh_unflock_config();
557+
if ( is_real_file ) vtysh_unflock_config();
557558
exit(ret);
558559
}
559560

0 commit comments

Comments
 (0)