[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 |
|
---|
[a63966d] | 29 | /** @addtogroup getterm
|
---|
[49f78c7] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[8d2dd7f2] | 36 | #include <stdint.h>
|
---|
[49f78c7] | 37 | #include <stdio.h>
|
---|
| 38 | #include <task.h>
|
---|
[d9fae235] | 39 | #include <str_error.h>
|
---|
[0485135] | 40 | #include <errno.h>
|
---|
[593e023] | 41 | #include <loc.h>
|
---|
[fcab7ef] | 42 | #include <vfs/vfs.h>
|
---|
[1d6dd2a] | 43 | #include <str.h>
|
---|
[49f78c7] | 44 | #include "version.h"
|
---|
[4deb8b5] | 45 | #include "welcome.h"
|
---|
[49f78c7] | 46 |
|
---|
[d9fae235] | 47 | #define APP_NAME "getterm"
|
---|
| 48 |
|
---|
[49f78c7] | 49 | static void usage(void)
|
---|
| 50 | {
|
---|
[593e023] | 51 | printf("Usage: %s <terminal> <locfs> [--msg] [--wait] -- "
|
---|
| 52 | "<command> [<arguments...>]\n", APP_NAME);
|
---|
| 53 | printf(" <terminal> Terminal device\n");
|
---|
| 54 | printf(" <locfs> Mount point of locfs\n");
|
---|
| 55 | printf(" --msg Print welcome message\n");
|
---|
| 56 | printf(" --wait Wait for the terminal to be ready\n");
|
---|
[49f78c7] | 57 | }
|
---|
| 58 |
|
---|
[b19e892] | 59 | static void reopen(FILE **stream, int fd, const char *path, int mode,
|
---|
| 60 | const char *fmode)
|
---|
[49f78c7] | 61 | {
|
---|
[2b88074b] | 62 | if (fclose(*stream))
|
---|
| 63 | return;
|
---|
[a35b458] | 64 |
|
---|
[2b88074b] | 65 | *stream = NULL;
|
---|
[a35b458] | 66 |
|
---|
[f77c1c9] | 67 | int oldfd;
|
---|
[b7fd2a0] | 68 | errno_t rc = vfs_lookup_open(path, WALK_REGULAR, mode, &oldfd);
|
---|
[f77c1c9] | 69 | if (rc != EOK)
|
---|
[2b88074b] | 70 | return;
|
---|
[a35b458] | 71 |
|
---|
[2b88074b] | 72 | if (oldfd != fd) {
|
---|
[f77c1c9] | 73 | int newfd;
|
---|
| 74 | if (vfs_clone(oldfd, fd, false, &newfd) != EOK)
|
---|
[2b88074b] | 75 | return;
|
---|
[a35b458] | 76 |
|
---|
[f77c1c9] | 77 | assert(newfd == fd);
|
---|
| 78 |
|
---|
[9c4cf0d] | 79 | if (vfs_put(oldfd))
|
---|
[2b88074b] | 80 | return;
|
---|
| 81 | }
|
---|
[a35b458] | 82 |
|
---|
[b19e892] | 83 | *stream = fdopen(fd, fmode);
|
---|
[49f78c7] | 84 | }
|
---|
| 85 |
|
---|
[d52b0044] | 86 | int main(int argc, char *argv[])
|
---|
[49f78c7] | 87 | {
|
---|
[dd567c6] | 88 | argv++;
|
---|
| 89 | argc--;
|
---|
[593e023] | 90 | if (argc < 4) {
|
---|
[49f78c7] | 91 | usage();
|
---|
[593e023] | 92 | return 1;
|
---|
[49f78c7] | 93 | }
|
---|
[a35b458] | 94 |
|
---|
[593e023] | 95 | char *term = *argv;
|
---|
| 96 | argv++;
|
---|
| 97 | argc--;
|
---|
[a35b458] | 98 |
|
---|
[593e023] | 99 | char *locfs = *argv;
|
---|
| 100 | argv++;
|
---|
| 101 | argc--;
|
---|
[a35b458] | 102 |
|
---|
[593e023] | 103 | bool print_msg = false;
|
---|
| 104 | bool wait = false;
|
---|
[a35b458] | 105 |
|
---|
[593e023] | 106 | while ((argc > 0) && (str_cmp(*argv, "--") != 0)) {
|
---|
| 107 | if (str_cmp(*argv, "--msg") == 0) {
|
---|
| 108 | print_msg = true;
|
---|
| 109 | } else if (str_cmp(*argv, "--wait") == 0) {
|
---|
| 110 | wait = true;
|
---|
| 111 | } else {
|
---|
| 112 | usage();
|
---|
| 113 | return 2;
|
---|
| 114 | }
|
---|
[a35b458] | 115 |
|
---|
[dd567c6] | 116 | argv++;
|
---|
| 117 | argc--;
|
---|
[4deb8b5] | 118 | }
|
---|
[a35b458] | 119 |
|
---|
[593e023] | 120 | if (argc < 1) {
|
---|
[4deb8b5] | 121 | usage();
|
---|
[593e023] | 122 | return 3;
|
---|
[4deb8b5] | 123 | }
|
---|
[a35b458] | 124 |
|
---|
[593e023] | 125 | /* Skip "--" */
|
---|
| 126 | argv++;
|
---|
| 127 | argc--;
|
---|
[a35b458] | 128 |
|
---|
[593e023] | 129 | char *cmd = *argv;
|
---|
| 130 | char **args = argv;
|
---|
[a35b458] | 131 |
|
---|
[593e023] | 132 | if (wait) {
|
---|
| 133 | /* Wait for the terminal service to be ready */
|
---|
| 134 | service_id_t service_id;
|
---|
[b7fd2a0] | 135 | errno_t rc = loc_service_get_id(term, &service_id, IPC_FLAG_BLOCKING);
|
---|
[593e023] | 136 | if (rc != EOK) {
|
---|
| 137 | printf("%s: Error waiting on %s (%s)\n", APP_NAME, term,
|
---|
| 138 | str_error(rc));
|
---|
| 139 | return rc;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
[a35b458] | 142 |
|
---|
[593e023] | 143 | char term_node[LOC_NAME_MAXLEN];
|
---|
| 144 | snprintf(term_node, LOC_NAME_MAXLEN, "%s/%s", locfs, term);
|
---|
[a35b458] | 145 |
|
---|
[b19e892] | 146 | reopen(&stdin, 0, term_node, MODE_READ, "r");
|
---|
| 147 | reopen(&stdout, 1, term_node, MODE_WRITE, "w");
|
---|
| 148 | reopen(&stderr, 2, term_node, MODE_WRITE, "w");
|
---|
[a35b458] | 149 |
|
---|
[2b88074b] | 150 | if (stdin == NULL)
|
---|
[593e023] | 151 | return 4;
|
---|
[a35b458] | 152 |
|
---|
[2b88074b] | 153 | if (stdout == NULL)
|
---|
[593e023] | 154 | return 5;
|
---|
[a35b458] | 155 |
|
---|
[2b88074b] | 156 | if (stderr == NULL)
|
---|
[593e023] | 157 | return 6;
|
---|
[a35b458] | 158 |
|
---|
[884c56b] | 159 | /*
|
---|
| 160 | * FIXME: fdopen() should actually detect that we are opening a console
|
---|
| 161 | * and it should set line-buffering mode automatically.
|
---|
| 162 | */
|
---|
| 163 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
|
---|
[a35b458] | 164 |
|
---|
[4deb8b5] | 165 | version_print(term);
|
---|
[593e023] | 166 | if (print_msg)
|
---|
[4deb8b5] | 167 | welcome_msg_print();
|
---|
[a35b458] | 168 |
|
---|
[593e023] | 169 | task_id_t id;
|
---|
[1c635d6] | 170 | task_wait_t twait;
|
---|
[a35b458] | 171 |
|
---|
[1433ecda] | 172 | errno_t rc = task_spawnv(&id, &twait, cmd, (const char *const *) args);
|
---|
[d52b0044] | 173 | if (rc != EOK) {
|
---|
[593e023] | 174 | printf("%s: Error spawning %s (%s)\n", APP_NAME, cmd,
|
---|
[d52b0044] | 175 | str_error(rc));
|
---|
[593e023] | 176 | return rc;
|
---|
[c7dc8ad] | 177 | }
|
---|
[a35b458] | 178 |
|
---|
[593e023] | 179 | task_exit_t texit;
|
---|
| 180 | int retval;
|
---|
[1c635d6] | 181 | rc = task_wait(&twait, &texit, &retval);
|
---|
[d52b0044] | 182 | if (rc != EOK) {
|
---|
[593e023] | 183 | printf("%s: Error waiting for %s (%s)\n", APP_NAME, cmd,
|
---|
[d52b0044] | 184 | str_error(rc));
|
---|
[593e023] | 185 | return rc;
|
---|
[d52b0044] | 186 | }
|
---|
[a35b458] | 187 |
|
---|
[d52b0044] | 188 | return 0;
|
---|
[49f78c7] | 189 | }
|
---|
| 190 |
|
---|
| 191 | /** @}
|
---|
| 192 | */
|
---|