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

Last change on this file since 46577995 was 46577995, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

After this commit, HelenOS is free of code that mixes error codes with non-error
values on the assumption that error codes are negative.

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