Changeset 6ea9a1d in mainline for uspace/app/bdsh/input.c


Ignore:
Timestamp:
2011-06-11T22:32:24Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
36ab7c7
Parents:
ae45201
Message:

Allow shell builtins to be redirected too

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/input.c

    rae45201 r6ea9a1d  
    5858
    5959/* Private helpers */
    60 static int run_command(char **, cliuser_t *, FILE **);
     60static int run_command(char **, cliuser_t *, iostate_t *);
    6161static void print_pipe_usage(void);
    6262
     
    140140        }
    141141       
    142         FILE *files[4];
    143         files[0] = stdin;
    144         files[1] = stdout;
    145         files[2] = stderr;
    146         files[3] = 0;
     142        iostate_t new_iostate = {
     143                .stdin = stdin,
     144                .stdout = stdout,
     145                .stderr = stderr
     146        };
     147       
     148        FILE *from = NULL;
     149        FILE *to = NULL;
    147150       
    148151        if (redir_from) {
    149                 FILE *from = fopen(redir_from, "r");
     152                from = fopen(redir_from, "r");
    150153                if (from == NULL) {
    151154                        printf("Cannot open file %s\n", redir_from);
    152155                        rc = errno;
    153                         goto finit;
     156                        goto finit_with_files;
    154157                }
    155                 files[0] = from;
    156         }
     158                new_iostate.stdin = from;
     159        }
     160       
    157161       
    158162        if (redir_to) {
    159                 FILE *to = fopen(redir_to, "w");
     163                to = fopen(redir_to, "w");
    160164                if (to == NULL) {
    161165                        printf("Cannot open file %s\n", redir_to);
    162166                        rc = errno;
    163                         goto finit;
     167                        goto finit_with_files;
    164168                }
    165                 files[1] = to;
    166         }
    167        
    168         rc = run_command(cmd, usr, files);
     169                new_iostate.stdout = to;
     170        }
     171       
     172        rc = run_command(cmd, usr, &new_iostate);
     173       
     174finit_with_files:
     175        if (from != NULL) {
     176                fclose(from);
     177        }
     178        if (to != NULL) {
     179                fclose(to);
     180        }
    169181       
    170182finit:
     
    188200}
    189201
    190 int run_command(char **cmd, cliuser_t *usr, FILE *files[])
     202int run_command(char **cmd, cliuser_t *usr, iostate_t *new_iostate)
    191203{
    192204        int id = 0;
     
    199211        /* Is it a builtin command ? */
    200212        if ((id = (is_builtin(cmd[0]))) > -1) {
    201                 return run_builtin(id, cmd, usr);
     213                return run_builtin(id, cmd, usr, new_iostate);
    202214        }
    203215       
    204216        /* Is it a module ? */
    205217        if ((id = (is_module(cmd[0]))) > -1) {
    206                 return run_module(id, cmd);
     218                return run_module(id, cmd, new_iostate);
    207219        }
    208220
    209221        /* See what try_exec thinks of it */
    210         return try_exec(cmd[0], cmd, files);
     222        return try_exec(cmd[0], cmd, new_iostate);
    211223}
    212224
Note: See TracChangeset for help on using the changeset viewer.