commit 98fc2f167ba8e7efae68c15faa0430e674146adf from: Stefan Sperling date: Mon Jul 31 15:54:30 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. commit - d759fe74722c022dcd2a5a19883955a8fad0be91 commit + 98fc2f167ba8e7efae68c15faa0430e674146adf 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 - 2c6b28c32e26056d4f1939cbe118d5492a29d10d blob + c440e51e830f8c0921801bbfb8fa0ba494c27689 --- commands.c +++ commands.c @@ -387,7 +387,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; @@ -1465,16 +1466,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); @@ -2041,10 +2044,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 */ } @@ -3081,6 +3086,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 - 98be4c1f2a50bf22257eacd7a22db984d21c26e7 blob + 6c8970da0e631c868440bba600cfb153a60347ad --- 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 - 746039c12f247f72374cc49c9792c96bcbe0ed52 blob + e0969dbbb8268b782af52a08caaad3fdc026cd70 --- main.c +++ main.c @@ -40,7 +40,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; @@ -89,11 +89,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) @@ -103,9 +98,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++) {