+/* built-in 'read VAR' handler */
+static int builtin_read(struct child_prog *child)
+{
+ int res = 0, len, newlen;
+ char *s;
+ char string[MAX_READ];
+
+ if (child->argv[1]) {
+ /* argument (VAR) given: put "VAR=" into buffer */
+ strcpy(string, child->argv[1]);
+ len = strlen(string);
+ string[len++] = '=';
+ string[len] = '\0';
+ fgets(&string[len], sizeof(string) - len, stdin); /* read string */
+ newlen = strlen(string);
+ if(newlen > len)
+ string[--newlen] = '\0'; /* chomp trailing newline */
+ /*
+ ** string should now contain "VAR=<value>"
+ ** copy it (putenv() won't do that, so we must make sure
+ ** the string resides in a static buffer!)
+ */
+ res = -1;
+ if((s = strdup(string)))
+ res = putenv(s);
+ if (res)
+ fprintf(stderr, "read: %s\n", strerror(errno));
+ }
+ else
+ fgets(string, sizeof(string), stdin);
+
+ return (res);
+}
+
+#ifdef BB_FEATURE_SH_IF_EXPRESSIONS
+/* Built-in handler for 'if' commands */
+static int builtin_if(struct child_prog *child)
+{
+ struct job *cmd = child->family;
+ int status;
+ char* charptr1=cmd->text+3; /* skip over the leading 'if ' */
+
+ /* Now run the 'if' command */
+ debug_printf( "job=%p entering builtin_if ('%s')-- context=%d\n", cmd, charptr1, cmd->job_context);
+ status = run_command_predicate(charptr1);
+ debug_printf( "if test returned ");
+ if (status == 0) {
+ debug_printf( "TRUE\n");
+ cmd->job_context |= IF_TRUE_CONTEXT;
+ } else {
+ debug_printf( "FALSE\n");
+ cmd->job_context |= IF_FALSE_CONTEXT;
+ }
+ debug_printf("job=%p builtin_if set job context to %x\n", cmd, cmd->job_context);
+ shell_context++;
+
+ return status;
+}
+
+/* Built-in handler for 'then' (part of the 'if' command) */
+static int builtin_then(struct child_prog *child)
+{
+ struct job *cmd = child->family;
+ char* charptr1=cmd->text+5; /* skip over the leading 'then ' */
+
+ debug_printf( "job=%p entering builtin_then ('%s')-- context=%d\n", cmd, charptr1, cmd->job_context);
+ if (! (cmd->job_context & (IF_TRUE_CONTEXT|IF_FALSE_CONTEXT))) {
+ shell_context = 0; /* Reset the shell's context on an error */
+ error_msg("%s `then'", syntax_err);
+ return EXIT_FAILURE;
+ }
+
+ cmd->job_context |= THEN_EXP_CONTEXT;
+ debug_printf("job=%p builtin_then set job context to %x\n", cmd, cmd->job_context);
+
+ /* If the if result was FALSE, skip the 'then' stuff */
+ if (cmd->job_context & IF_FALSE_CONTEXT) {
+ return EXIT_SUCCESS;
+ }
+
+ /* Seems the if result was TRUE, so run the 'then' command */
+ debug_printf( "'then' now running '%s'\n", charptr1);
+
+ return(run_command_predicate(charptr1));
+}
+
+/* Built-in handler for 'else' (part of the 'if' command) */
+static int builtin_else(struct child_prog *child)
+{
+ struct job *cmd = child->family;
+ char* charptr1=cmd->text+5; /* skip over the leading 'else ' */
+
+ debug_printf( "job=%p entering builtin_else ('%s')-- context=%d\n", cmd, charptr1, cmd->job_context);
+
+ if (! (cmd->job_context & THEN_EXP_CONTEXT)) {
+ shell_context = 0; /* Reset the shell's context on an error */
+ error_msg("%s `else'", syntax_err);
+ return EXIT_FAILURE;
+ }
+ /* If the if result was TRUE, skip the 'else' stuff */
+ if (cmd->job_context & IF_TRUE_CONTEXT) {
+ return EXIT_SUCCESS;
+ }
+
+ cmd->job_context |= ELSE_EXP_CONTEXT;
+ debug_printf("job=%p builtin_else set job context to %x\n", cmd, cmd->job_context);
+
+ /* Now run the 'else' command */
+ debug_printf( "'else' now running '%s'\n", charptr1);
+ return(run_command_predicate(charptr1));
+}
+
+/* Built-in handler for 'fi' (part of the 'if' command) */
+static int builtin_fi(struct child_prog *child)
+{
+ struct job *cmd = child->family;
+ debug_printf( "job=%p entering builtin_fi ('%s')-- context=%d\n", cmd, "", cmd->job_context);
+ if (! (cmd->job_context & (IF_TRUE_CONTEXT|IF_FALSE_CONTEXT))) {
+ shell_context = 0; /* Reset the shell's context on an error */
+ error_msg("%s `fi'", syntax_err);
+ return EXIT_FAILURE;
+ }
+ /* Clear out the if and then context bits */
+ cmd->job_context &= ~(IF_TRUE_CONTEXT|IF_FALSE_CONTEXT|THEN_EXP_CONTEXT|ELSE_EXP_CONTEXT);
+ debug_printf("job=%p builtin_fi set job context to %x\n", cmd, cmd->job_context);
+ shell_context--;
+ return EXIT_SUCCESS;
+}
+#endif
+