Changeset ae45201 in mainline for uspace/app/bdsh/input.c


Ignore:
Timestamp:
2011-06-11T21:32:54Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6ea9a1d
Parents:
28ee877e
Message:

Allow redirecting output of tasks to file in bdsh

File:
1 edited

Legend:

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

    r28ee877e rae45201  
    5858
    5959/* Private helpers */
    60 static int run_command(char **, cliuser_t *);
     60static int run_command(char **, cliuser_t *, FILE **);
     61static void print_pipe_usage(void);
    6162
    6263/* Tokenizes input from console, sees if the first word is a built-in, if so
     
    6869        int rc = 0;
    6970        tokenizer_t tok;
     71        int i, pipe_count, processed_pipes;
     72        int pipe_pos[2];
     73        char **actual_cmd;
     74        char *redir_from = NULL;
     75        char *redir_to = NULL;
    7076
    7177        if (NULL == usr->line)
     
    8288        }
    8389       
    84         rc = run_command(cmd, usr);
     90        /* Until full support for pipes is implemented, allow for a simple case:
     91         * [from <file> |] command [| to <file>]
     92         *
     93         * First find the pipes and check that there are no more
     94         */
     95        int cmd_length = 0;
     96        for (i = 0, pipe_count = 0; cmd[i] != NULL; i++, cmd_length++) {
     97                if (cmd[i][0] == '|') {
     98                        if (pipe_count >= 2) {
     99                                print_pipe_usage();
     100                                rc = ENOTSUP;
     101                                goto finit;
     102                        }
     103                        pipe_pos[pipe_count] = i;
     104                        pipe_count++;
     105                }
     106        }
     107       
     108        actual_cmd = cmd;
     109        processed_pipes = 0;
     110       
     111        /* Check if the first part (from <file> |) is present */
     112        if (pipe_count > 0 && pipe_pos[0] == 2 && str_cmp(cmd[0], "from") == 0) {
     113                /* Ignore the first three tokens (from, file, pipe) and set from */
     114                redir_from = cmd[1];
     115                actual_cmd = cmd + 3;
     116                processed_pipes++;
     117        }
     118       
     119        /* Check if the second part (| to <file>) is present */
     120        if ((pipe_count - processed_pipes) > 0 &&
     121            pipe_pos[processed_pipes] == cmd_length - 3 &&
     122            str_cmp(cmd[cmd_length-2], "to") == 0) {
     123                /* Ignore the last three tokens (pipe, to, file) and set to */
     124                redir_to = cmd[cmd_length-1];
     125                cmd[cmd_length-3] = NULL;
     126                cmd_length -= 3;
     127                processed_pipes++;
     128        }
     129       
     130        if (processed_pipes != pipe_count) {
     131                print_pipe_usage();
     132                rc = ENOTSUP;
     133                goto finit;
     134        }
     135       
     136        if (actual_cmd[0] == NULL) {
     137                print_pipe_usage();
     138                rc = ENOTSUP;
     139                goto finit;
     140        }
     141       
     142        FILE *files[4];
     143        files[0] = stdin;
     144        files[1] = stdout;
     145        files[2] = stderr;
     146        files[3] = 0;
     147       
     148        if (redir_from) {
     149                FILE *from = fopen(redir_from, "r");
     150                if (from == NULL) {
     151                        printf("Cannot open file %s\n", redir_from);
     152                        rc = errno;
     153                        goto finit;
     154                }
     155                files[0] = from;
     156        }
     157       
     158        if (redir_to) {
     159                FILE *to = fopen(redir_to, "w");
     160                if (to == NULL) {
     161                        printf("Cannot open file %s\n", redir_to);
     162                        rc = errno;
     163                        goto finit;
     164                }
     165                files[1] = to;
     166        }
     167       
     168        rc = run_command(cmd, usr, files);
    85169       
    86170finit:
     
    94178}
    95179
    96 int run_command(char **cmd, cliuser_t *usr)
     180void print_pipe_usage()
     181{
     182        printf("Invalid syntax!\n");
     183        printf("Usage of redirection (pipes in the future):\n");
     184        printf("from filename | command ...\n");
     185        printf("from filename | command ... | to filename\n");
     186        printf("command ... | to filename\n");
     187       
     188}
     189
     190int run_command(char **cmd, cliuser_t *usr, FILE *files[])
    97191{
    98192        int id = 0;
     
    114208
    115209        /* See what try_exec thinks of it */
    116         return try_exec(cmd[0], cmd);
     210        return try_exec(cmd[0], cmd, files);
    117211}
    118212
Note: See TracChangeset for help on using the changeset viewer.