[c7dc8ad] | 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 redir Redirector
|
---|
| 30 | * @brief Redirect stdin/stdout/stderr.
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdlib.h>
|
---|
[19f857a] | 38 | #include <str.h>
|
---|
[c7dc8ad] | 39 | #include <stdio.h>
|
---|
| 40 | #include <task.h>
|
---|
[d9fae235] | 41 | #include <str_error.h>
|
---|
[0485135] | 42 | #include <errno.h>
|
---|
[fcab7ef] | 43 | #include <vfs/vfs.h>
|
---|
[d9fae235] | 44 |
|
---|
| 45 | #define NAME "redir"
|
---|
[c7dc8ad] | 46 |
|
---|
| 47 | static void usage(void)
|
---|
| 48 | {
|
---|
[4688fab8] | 49 | fprintf(stderr, "Usage: %s [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n",
|
---|
[d9fae235] | 50 | NAME);
|
---|
[c7dc8ad] | 51 | }
|
---|
| 52 |
|
---|
[b19e892] | 53 | static void reopen(FILE **stream, int fd, const char *path, int flags, int mode,
|
---|
| 54 | const char *fmode)
|
---|
[c7dc8ad] | 55 | {
|
---|
| 56 | if (fclose(*stream))
|
---|
| 57 | return;
|
---|
| 58 |
|
---|
| 59 | *stream = NULL;
|
---|
| 60 |
|
---|
[b19e892] | 61 | int oldfd = vfs_lookup_open(path, WALK_REGULAR | flags, mode);
|
---|
[c7dc8ad] | 62 | if (oldfd < 0)
|
---|
| 63 | return;
|
---|
| 64 |
|
---|
| 65 | if (oldfd != fd) {
|
---|
[fcab7ef] | 66 | if (vfs_clone(oldfd, fd, false) != fd)
|
---|
[c7dc8ad] | 67 | return;
|
---|
| 68 |
|
---|
[9c4cf0d] | 69 | if (vfs_put(oldfd))
|
---|
[c7dc8ad] | 70 | return;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[b19e892] | 73 | *stream = fdopen(fd, fmode);
|
---|
[c7dc8ad] | 74 | }
|
---|
| 75 |
|
---|
[1c635d6] | 76 | static task_id_t spawn(task_wait_t *wait, int argc, char *argv[])
|
---|
[c7dc8ad] | 77 | {
|
---|
[0485135] | 78 | const char **args;
|
---|
[a7e7716] | 79 | task_id_t id = 0;
|
---|
[0485135] | 80 | int rc;
|
---|
| 81 |
|
---|
| 82 | args = (const char **) calloc(argc + 1, sizeof(char *));
|
---|
[c7dc8ad] | 83 | if (!args) {
|
---|
[4688fab8] | 84 | fprintf(stderr, "No memory available\n");
|
---|
[c7dc8ad] | 85 | return 0;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | int i;
|
---|
| 89 | for (i = 0; i < argc; i++)
|
---|
| 90 | args[i] = argv[i];
|
---|
| 91 |
|
---|
| 92 | args[argc] = NULL;
|
---|
| 93 |
|
---|
[1c635d6] | 94 | rc = task_spawnv(&id, wait, argv[0], args);
|
---|
[c7dc8ad] | 95 |
|
---|
| 96 | free(args);
|
---|
| 97 |
|
---|
[0485135] | 98 | if (rc != EOK) {
|
---|
[4688fab8] | 99 | fprintf(stderr, "%s: Error spawning %s (%s)\n", NAME, argv[0],
|
---|
[0485135] | 100 | str_error(rc));
|
---|
[a7e7716] | 101 | return 0;
|
---|
[0485135] | 102 | }
|
---|
[c7dc8ad] | 103 |
|
---|
| 104 | return id;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | int main(int argc, char *argv[])
|
---|
| 108 | {
|
---|
| 109 | if (argc < 3) {
|
---|
| 110 | usage();
|
---|
| 111 | return -1;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | int i;
|
---|
| 115 | for (i = 1; i < argc; i++) {
|
---|
| 116 | if (str_cmp(argv[i], "-i") == 0) {
|
---|
| 117 | i++;
|
---|
| 118 | if (i >= argc) {
|
---|
| 119 | usage();
|
---|
| 120 | return -2;
|
---|
| 121 | }
|
---|
[b19e892] | 122 | reopen(&stdin, 0, argv[i], 0, MODE_READ, "r");
|
---|
[c7dc8ad] | 123 | } else if (str_cmp(argv[i], "-o") == 0) {
|
---|
| 124 | i++;
|
---|
| 125 | if (i >= argc) {
|
---|
| 126 | usage();
|
---|
| 127 | return -3;
|
---|
| 128 | }
|
---|
[b19e892] | 129 | reopen(&stdout, 1, argv[i], WALK_MAY_CREATE, MODE_WRITE,
|
---|
| 130 | "w");
|
---|
[c7dc8ad] | 131 | } else if (str_cmp(argv[i], "-e") == 0) {
|
---|
| 132 | i++;
|
---|
| 133 | if (i >= argc) {
|
---|
| 134 | usage();
|
---|
| 135 | return -4;
|
---|
| 136 | }
|
---|
[b19e892] | 137 | reopen(&stderr, 2, argv[i], WALK_MAY_CREATE, MODE_WRITE,
|
---|
| 138 | "w");
|
---|
[c7dc8ad] | 139 | } else if (str_cmp(argv[i], "--") == 0) {
|
---|
| 140 | i++;
|
---|
| 141 | break;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (i >= argc) {
|
---|
| 146 | usage();
|
---|
| 147 | return -5;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /*
|
---|
| 151 | * FIXME: fdopen() should actually detect that we are opening a console
|
---|
| 152 | * and it should set line-buffering mode automatically.
|
---|
| 153 | */
|
---|
| 154 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
|
---|
[1c635d6] | 155 |
|
---|
| 156 | task_wait_t wait;
|
---|
| 157 | task_id_t id = spawn(&wait, argc - i, argv + i);
|
---|
[c7dc8ad] | 158 |
|
---|
| 159 | if (id != 0) {
|
---|
| 160 | task_exit_t texit;
|
---|
| 161 | int retval;
|
---|
[1c635d6] | 162 | task_wait(&wait, &texit, &retval);
|
---|
[c7dc8ad] | 163 |
|
---|
| 164 | return retval;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | return -6;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /** @}
|
---|
| 171 | */
|
---|