[49f78c7] | 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 |
|
---|
[df747bd8] | 29 | /** @addtogroup getterm GetTerm
|
---|
[49f78c7] | 30 | * @brief Console initialization task.
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <sys/types.h>
|
---|
[2b88074b] | 38 | #include <fcntl.h>
|
---|
[49f78c7] | 39 | #include <unistd.h>
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 | #include <task.h>
|
---|
[d9fae235] | 42 | #include <str_error.h>
|
---|
[0485135] | 43 | #include <errno.h>
|
---|
[49f78c7] | 44 | #include "version.h"
|
---|
| 45 |
|
---|
[d9fae235] | 46 | #define APP_NAME "getterm"
|
---|
| 47 |
|
---|
[49f78c7] | 48 | static void usage(void)
|
---|
| 49 | {
|
---|
[d52b0044] | 50 | printf("Usage: %s <terminal> <command> [<arguments...>]\n", APP_NAME);
|
---|
[49f78c7] | 51 | }
|
---|
| 52 |
|
---|
[2b88074b] | 53 | static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
|
---|
[49f78c7] | 54 | {
|
---|
[2b88074b] | 55 | if (fclose(*stream))
|
---|
| 56 | return;
|
---|
[49f78c7] | 57 |
|
---|
[2b88074b] | 58 | *stream = NULL;
|
---|
| 59 |
|
---|
| 60 | int oldfd = open(path, flags);
|
---|
| 61 | if (oldfd < 0)
|
---|
| 62 | return;
|
---|
| 63 |
|
---|
| 64 | if (oldfd != fd) {
|
---|
| 65 | if (dup2(oldfd, fd) != fd)
|
---|
| 66 | return;
|
---|
| 67 |
|
---|
| 68 | if (close(oldfd))
|
---|
| 69 | return;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | *stream = fdopen(fd, mode);
|
---|
[49f78c7] | 73 | }
|
---|
| 74 |
|
---|
[d52b0044] | 75 | int main(int argc, char *argv[])
|
---|
[49f78c7] | 76 | {
|
---|
[0485135] | 77 | int rc;
|
---|
[d52b0044] | 78 | task_exit_t texit;
|
---|
| 79 | int retval;
|
---|
| 80 | task_id_t id;
|
---|
| 81 | char *fname;
|
---|
[49f78c7] | 82 |
|
---|
| 83 | if (argc < 3) {
|
---|
| 84 | usage();
|
---|
| 85 | return -1;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[2b88074b] | 88 | reopen(&stdin, 0, argv[1], O_RDONLY, "r");
|
---|
| 89 | reopen(&stdout, 1, argv[1], O_WRONLY, "w");
|
---|
| 90 | reopen(&stderr, 2, argv[1], O_WRONLY, "w");
|
---|
[49f78c7] | 91 |
|
---|
[e3fa92aa] | 92 | /*
|
---|
[2b88074b] | 93 | * FIXME: fdopen() should actually detect that we are opening a console
|
---|
[e3fa92aa] | 94 | * and it should set line-buffering mode automatically.
|
---|
| 95 | */
|
---|
| 96 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
|
---|
[49f78c7] | 97 |
|
---|
[2b88074b] | 98 | if (stdin == NULL)
|
---|
[49f78c7] | 99 | return -2;
|
---|
| 100 |
|
---|
[2b88074b] | 101 | if (stdout == NULL)
|
---|
| 102 | return -3;
|
---|
| 103 |
|
---|
| 104 | if (stderr == NULL)
|
---|
| 105 | return -4;
|
---|
| 106 |
|
---|
[49f78c7] | 107 | version_print(argv[1]);
|
---|
[d52b0044] | 108 | fname = argv[2];
|
---|
[2b88074b] | 109 |
|
---|
[d52b0044] | 110 | rc = task_spawnv(&id, fname, (const char * const *) &argv[2]);
|
---|
| 111 | if (rc != EOK) {
|
---|
| 112 | printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
|
---|
| 113 | str_error(rc));
|
---|
| 114 | return -5;
|
---|
[c7dc8ad] | 115 | }
|
---|
[d52b0044] | 116 |
|
---|
| 117 | rc = task_wait(id, &texit, &retval);
|
---|
| 118 | if (rc != EOK) {
|
---|
| 119 | printf("%s: Error waiting for %s (%s)\n", APP_NAME, fname,
|
---|
| 120 | str_error(rc));
|
---|
| 121 | return -6;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return 0;
|
---|
[49f78c7] | 125 | }
|
---|
| 126 |
|
---|
| 127 | /** @}
|
---|
| 128 | */
|
---|