Changeset 04803bf in mainline for uspace/app/getterm/getterm.c


Ignore:
Timestamp:
2011-03-21T22:00:17Z (15 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
143932e3
Parents:
b50b5af2 (diff), 7308e84 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes (needs fixes).

File:
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/app/getterm/getterm.c

    rb50b5af2 r04803bf  
    2727 */
    2828
    29 /** @addtogroup getvc GetVC
     29/** @addtogroup getterm GetTerm
    3030 * @brief Console initialization task.
    3131 * @{
     
    3636
    3737#include <sys/types.h>
     38#include <fcntl.h>
    3839#include <unistd.h>
    3940#include <stdio.h>
    4041#include <task.h>
     42#include <str_error.h>
     43#include <errno.h>
    4144#include "version.h"
     45#include "welcome.h"
     46
     47#define APP_NAME  "getterm"
    4248
    4349static void usage(void)
    4450{
    45         printf("Usage: getvc <device> <path>\n");
     51        printf("Usage: %s <terminal> [-w] <command> [<arguments...>]\n", APP_NAME);
    4652}
    4753
    48 static void closeall(void)
     54static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
    4955{
    50         fclose(stdin);
    51         fclose(stdout);
    52         fclose(stderr);
     56        if (fclose(*stream))
     57                return;
    5358       
    54         close(0);
    55         close(1);
    56         close(2);
    57 }
    58 
    59 static task_id_t spawn(char *fname)
    60 {
    61         char *argv[2];
     59        *stream = NULL;
    6260       
    63         argv[0] = fname;
    64         argv[1] = NULL;
     61        int oldfd = open(path, flags);
     62        if (oldfd < 0)
     63                return;
    6564       
    66         task_id_t id = task_spawn(fname, argv);
     65        if (oldfd != fd) {
     66                if (dup2(oldfd, fd) != fd)
     67                        return;
     68               
     69                if (close(oldfd))
     70                        return;
     71        }
    6772       
    68         if (id == 0)
    69                 printf("Error spawning %s\n", fname);
    70        
    71         return id;
     73        *stream = fdopen(fd, mode);
    7274}
    7375
    7476int main(int argc, char *argv[])
    7577{
     78        int rc;
    7679        task_exit_t texit;
    7780        int retval;
     81        task_id_t id;
     82        char *fname, *term;
     83        char **cmd_args;
     84        bool print_wmsg;
    7885
    79         if (argc < 3) {
     86        argv++;
     87        argc--;
     88        if (argc < 1) {
    8089                usage();
    8190                return -1;
    8291        }
     92
     93        if (str_cmp(*argv, "-w") == 0) {
     94                print_wmsg = true;
     95                argv++;
     96                argc--;
     97        } else {
     98                print_wmsg = false;
     99        }
     100
     101        if (argc < 2) {
     102                usage();
     103                return -1;
     104        }
     105
     106        term = *argv++;
     107        fname = *argv;
     108        cmd_args = argv;
    83109       
    84         closeall();
     110        reopen(&stdin, 0, term, O_RDONLY, "r");
     111        reopen(&stdout, 1, term, O_WRONLY, "w");
     112        reopen(&stderr, 2, term, O_WRONLY, "w");
    85113       
    86         stdin = fopen(argv[1], "r");
    87         stdout = fopen(argv[1], "w");
    88         stderr = fopen(argv[1], "w");
    89 
    90114        /*
    91          * FIXME: fopen() should actually detect that we are opening a console
     115         * FIXME: fdopen() should actually detect that we are opening a console
    92116         * and it should set line-buffering mode automatically.
    93117         */
    94118        setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
    95119       
    96         if ((stdin == NULL)
    97             || (stdout == NULL)
    98             || (stderr == NULL))
     120        if (stdin == NULL)
    99121                return -2;
    100122       
    101         version_print(argv[1]);
    102         task_id_t id = spawn(argv[2]);
    103         task_wait(id, &texit, &retval);
     123        if (stdout == NULL)
     124                return -3;
    104125       
     126        if (stderr == NULL)
     127                return -4;
     128       
     129        version_print(term);
     130        if (print_wmsg)
     131                welcome_msg_print();
     132
     133        rc = task_spawnv(&id, fname, (const char * const *) cmd_args);
     134        if (rc != EOK) {
     135                printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
     136                    str_error(rc));
     137                return -5;
     138        }
     139
     140        rc = task_wait(id, &texit, &retval);
     141        if (rc != EOK) {
     142                printf("%s: Error waiting for %s (%s)\n", APP_NAME, fname,
     143                    str_error(rc));
     144                return -6;
     145        }
     146
    105147        return 0;
    106148}
Note: See TracChangeset for help on using the changeset viewer.