| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Martin Decky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup getterm GetTerm
|
|---|
| 30 | * @brief Console initialization task.
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <sys/types.h>
|
|---|
| 38 | #include <fcntl.h>
|
|---|
| 39 | #include <unistd.h>
|
|---|
| 40 | #include <stdio.h>
|
|---|
| 41 | #include <task.h>
|
|---|
| 42 | #include <str_error.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include "version.h"
|
|---|
| 45 | #include "welcome.h"
|
|---|
| 46 |
|
|---|
| 47 | #define APP_NAME "getterm"
|
|---|
| 48 |
|
|---|
| 49 | static void usage(void)
|
|---|
| 50 | {
|
|---|
| 51 | printf("Usage: %s <terminal> [-w] <command> [<arguments...>]\n", APP_NAME);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
|
|---|
| 55 | {
|
|---|
| 56 | if (fclose(*stream))
|
|---|
| 57 | return;
|
|---|
| 58 |
|
|---|
| 59 | *stream = NULL;
|
|---|
| 60 |
|
|---|
| 61 | int oldfd = open(path, flags);
|
|---|
| 62 | if (oldfd < 0)
|
|---|
| 63 | return;
|
|---|
| 64 |
|
|---|
| 65 | if (oldfd != fd) {
|
|---|
| 66 | if (dup2(oldfd, fd) != fd)
|
|---|
| 67 | return;
|
|---|
| 68 |
|
|---|
| 69 | if (close(oldfd))
|
|---|
| 70 | return;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | *stream = fdopen(fd, mode);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | int main(int argc, char *argv[])
|
|---|
| 77 | {
|
|---|
| 78 | int rc;
|
|---|
| 79 | task_exit_t texit;
|
|---|
| 80 | int retval;
|
|---|
| 81 | task_id_t id;
|
|---|
| 82 | char *fname, *term;
|
|---|
| 83 | char **cmd_args;
|
|---|
| 84 | bool print_wmsg;
|
|---|
| 85 |
|
|---|
| 86 | argv++;
|
|---|
| 87 | argc--;
|
|---|
| 88 | if (argc < 1) {
|
|---|
| 89 | usage();
|
|---|
| 90 | return -1;
|
|---|
| 91 | }
|
|---|
| 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;
|
|---|
| 109 |
|
|---|
| 110 | reopen(&stdin, 0, term, O_RDONLY, "r");
|
|---|
| 111 | reopen(&stdout, 1, term, O_WRONLY, "w");
|
|---|
| 112 | reopen(&stderr, 2, term, O_WRONLY, "w");
|
|---|
| 113 |
|
|---|
| 114 | /*
|
|---|
| 115 | * FIXME: fdopen() should actually detect that we are opening a console
|
|---|
| 116 | * and it should set line-buffering mode automatically.
|
|---|
| 117 | */
|
|---|
| 118 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
|
|---|
| 119 |
|
|---|
| 120 | if (stdin == NULL)
|
|---|
| 121 | return -2;
|
|---|
| 122 |
|
|---|
| 123 | if (stdout == NULL)
|
|---|
| 124 | return -3;
|
|---|
| 125 |
|
|---|
| 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 |
|
|---|
| 147 | return 0;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /** @}
|
|---|
| 151 | */
|
|---|