source: mainline/uspace/app/getterm/getterm.c@ f77c1c9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f77c1c9 was f77c1c9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Return VFS handles separately from error codes.

  • Property mode set to 100644
File size: 4.4 KB
Line 
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 <stdint.h>
38#include <stdio.h>
39#include <task.h>
40#include <str_error.h>
41#include <errno.h>
42#include <loc.h>
43#include <vfs/vfs.h>
44#include "version.h"
45#include "welcome.h"
46
47#define APP_NAME "getterm"
48
49static void usage(void)
50{
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");
57}
58
59static void reopen(FILE **stream, int fd, const char *path, int mode,
60 const char *fmode)
61{
62 if (fclose(*stream))
63 return;
64
65 *stream = NULL;
66
67 int oldfd;
68 int rc = vfs_lookup_open(path, WALK_REGULAR, mode, &oldfd);
69 if (rc != EOK)
70 return;
71
72 if (oldfd != fd) {
73 int newfd;
74 if (vfs_clone(oldfd, fd, false, &newfd) != EOK)
75 return;
76
77 assert(newfd == fd);
78
79 if (vfs_put(oldfd))
80 return;
81 }
82
83 *stream = fdopen(fd, fmode);
84}
85
86int main(int argc, char *argv[])
87{
88 argv++;
89 argc--;
90 if (argc < 4) {
91 usage();
92 return 1;
93 }
94
95 char *term = *argv;
96 argv++;
97 argc--;
98
99 char *locfs = *argv;
100 argv++;
101 argc--;
102
103 bool print_msg = false;
104 bool wait = false;
105
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 }
115
116 argv++;
117 argc--;
118 }
119
120 if (argc < 1) {
121 usage();
122 return 3;
123 }
124
125 /* Skip "--" */
126 argv++;
127 argc--;
128
129 char *cmd = *argv;
130 char **args = argv;
131
132 if (wait) {
133 /* Wait for the terminal service to be ready */
134 service_id_t service_id;
135 int rc = loc_service_get_id(term, &service_id, IPC_FLAG_BLOCKING);
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 }
142
143 char term_node[LOC_NAME_MAXLEN];
144 snprintf(term_node, LOC_NAME_MAXLEN, "%s/%s", locfs, term);
145
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");
149
150 if (stdin == NULL)
151 return 4;
152
153 if (stdout == NULL)
154 return 5;
155
156 if (stderr == NULL)
157 return 6;
158
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);
164
165 version_print(term);
166 if (print_msg)
167 welcome_msg_print();
168
169 task_id_t id;
170 task_wait_t twait;
171
172 int rc = task_spawnv(&id, &twait, cmd, (const char * const *) args);
173 if (rc != EOK) {
174 printf("%s: Error spawning %s (%s)\n", APP_NAME, cmd,
175 str_error(rc));
176 return rc;
177 }
178
179 task_exit_t texit;
180 int retval;
181 rc = task_wait(&twait, &texit, &retval);
182 if (rc != EOK) {
183 printf("%s: Error waiting for %s (%s)\n", APP_NAME, cmd,
184 str_error(rc));
185 return rc;
186 }
187
188 return 0;
189}
190
191/** @}
192 */
Note: See TracBrowser for help on using the repository browser.