commit 05ca4b38cd54ee0825808837fb61a618a9d5f894 from: Stefan Sperling date: Fri Sep 01 14:19:21 2023 UTC allow nsh to change its behaviour when standard input is not a TTY Introduce an 'interactive_mode' flag which indicates whether standard input is a TTY. This allows nsh to accept commands on standard input for scripting purposes. This will be needed for better integration of at(1). The 'enable' command will not work in non-interactive mode yet. This will be fixed soon. ok tom commit - 662852943ef10560c7208f2fbfa77d3c65da0f7a commit + 05ca4b38cd54ee0825808837fb61a618a9d5f894 blob - 558ac64311cec683fc889aba680d75cd27ce03b0 blob + 2a3c298673f93f97a419f91e4124069c9e698f95 --- bgpnsh/bgpnsh.c +++ bgpnsh/bgpnsh.c @@ -50,7 +50,7 @@ void sigalarm(int signo) } } -int editing = 1, config_mode = 0; +int editing = 1, config_mode = 0, interactive_mode = 1; int cli_rtable; int bridge; size_t Intlist_nitems = 0, Bridgelist_nitems = 0; blob - 321f97daac56ea301ca7f4c04bb3dc2ee3caa5b9 blob + 6b02de2bf2ad5e613b4eb9949ed3ef62ef6377b2 --- commands.c +++ commands.c @@ -389,7 +389,8 @@ quit(void) if (privexec) { exit(NSH_REXEC_EXIT_CODE_QUIT); } else { - printf("%% Session terminated.\n"); + if (interactive_mode) + printf("%% Session terminated.\n"); exit(0); } return 0; @@ -1470,16 +1471,18 @@ interface(int argc, char **argv, char *modhvar) return(0); } - /* human at the keyboard */ + /* human at the keyboard or commands on stdin */ for (;;) { char *margp; if (!editing) { /* command line editing disabled */ - printf("%s", iprompt()); + if (interactive_mode) + printf("%s", iprompt()); if (fgets(line, sizeof(line), stdin) == NULL) { if (feof(stdin) || ferror(stdin)) { - printf("\n"); + if (interactive_mode) + printf("\n"); ifname[0] = '\0'; close(ifs); return(0); @@ -2055,10 +2058,12 @@ command() for (;;) { if (!editing) { - printf("%s", cprompt()); + if (interactive_mode) + printf("%s", cprompt()); if (fgets(line, sizeof(line), stdin) == NULL) { if (feof(stdin) || ferror(stdin)) { - printf("\n"); + if (interactive_mode) + printf("\n"); (void) quit(); /* NOTREACHED */ } @@ -3182,6 +3187,14 @@ do_reboot(int how) "changes.\n" "%% The 'write-config' command will save changes to %s.\n", NSHRC); + if (!interactive_mode) + return -1; + } + + if (!interactive_mode) { + if (cmdargs(argv[0], argv) != 0) + printf("%% %s command failed\n", argv[0]); + return 0; } switch (how) { blob - e53f8c0594782ae9b395e6c1140d6f24f1078672 blob + 58bb2bcedb2df9ec04f76bac63813f7681b224cf --- externs.h +++ externs.h @@ -32,6 +32,7 @@ extern int margc; /* makeargv() arg count */ extern char *margv[]; /* makeargv() args */ extern int verbose; /* is verbose mode on? */ extern int editing; /* is command line editing mode on? */ +extern int interactive_mode; /* are we in interactive mode? */ extern int config_mode; /* are we in comfig mode? */ extern int bridge; /* are we in bridge mode (or interface mode?) */ extern int priv; /* privileged mode or not? */ blob - fc91cd7e2c9434604a078a320468afbd1a295280 blob + 6f63bce2a2d59e0fb1587fba9c14ace3e83be81c --- main.c +++ main.c @@ -41,7 +41,7 @@ char *vers = NSH_VERSION_STR; int bridge = 0; /* bridge mode for interface() */ int verbose = 0; /* verbose mode */ int priv = 0, privexec = 0, cli_rtable = 0; -int editing = 1, config_mode = 0;; +int editing = 0, interactive_mode = 0, config_mode = 0;; pid_t pid; History *histi = NULL; @@ -184,11 +184,6 @@ main(int argc, char *argv[]) } - if (getuid() != 0) { - printf("%% Functionality is limited without root privileges.\n" - "%% The 'enable' command will switch to the root user.\n"); - } - argc -= optind; argv += optind; if (cflag && iflag) @@ -198,9 +193,20 @@ main(int argc, char *argv[]) if (iflag) rmtemp(SQ3DBFILE); - if (!privexec) - printf("%% NSH v%s\n", vers); + interactive_mode = isatty(STDIN_FILENO); + if (interactive_mode) { + editing = 1; + if (getuid() != 0) { + printf("%% Functionality is limited without " + "root privileges.\n%% The 'enable' command " + "will switch to the root user.\n"); + } + + if (!privexec) + printf("%% NSH v%s\n", vers); + } + /* create temporal tables (if they aren't already there) */ if (db_create_table_rtables() < 0) printf("%% database rtables creation failed\n"); blob - 845ca6a754fc316fb000478eb7a7ce4662cb1109 blob + 4eb03693ee206fa01a768404f84e7556a212871e --- more.c +++ more.c @@ -58,7 +58,7 @@ more(char *fname) return(0); } - if (nsh_cbreak() < 0) + if (!interactive_mode || nsh_cbreak() < 0) nopager = 1; for (i = 0; (input = fgetln(f, &s)) != NULL; i++) {