source: mainline/uspace/app/getterm/getterm.c@ 8d2dd7f2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8d2dd7f2 was 8d2dd7f2, checked in by Jakub Jermar <jakub@…>, 8 years ago

Reduce the number of files that include <sys/types.h>

  • Property mode set to 100644
File size: 4.3 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 = vfs_lookup_open(path, WALK_REGULAR, mode);
68 if (oldfd < 0)
69 return;
70
71 if (oldfd != fd) {
72 if (vfs_clone(oldfd, fd, false) != fd)
73 return;
74
75 if (vfs_put(oldfd))
76 return;
77 }
78
79 *stream = fdopen(fd, fmode);
80}
81
82int main(int argc, char *argv[])
83{
84 argv++;
85 argc--;
86 if (argc < 4) {
87 usage();
88 return 1;
89 }
90
91 char *term = *argv;
92 argv++;
93 argc--;
94
95 char *locfs = *argv;
96 argv++;
97 argc--;
98
99 bool print_msg = false;
100 bool wait = false;
101
102 while ((argc > 0) && (str_cmp(*argv, "--") != 0)) {
103 if (str_cmp(*argv, "--msg") == 0) {
104 print_msg = true;
105 } else if (str_cmp(*argv, "--wait") == 0) {
106 wait = true;
107 } else {
108 usage();
109 return 2;
110 }
111
112 argv++;
113 argc--;
114 }
115
116 if (argc < 1) {
117 usage();
118 return 3;
119 }
120
121 /* Skip "--" */
122 argv++;
123 argc--;
124
125 char *cmd = *argv;
126 char **args = argv;
127
128 if (wait) {
129 /* Wait for the terminal service to be ready */
130 service_id_t service_id;
131 int rc = loc_service_get_id(term, &service_id, IPC_FLAG_BLOCKING);
132 if (rc != EOK) {
133 printf("%s: Error waiting on %s (%s)\n", APP_NAME, term,
134 str_error(rc));
135 return rc;
136 }
137 }
138
139 char term_node[LOC_NAME_MAXLEN];
140 snprintf(term_node, LOC_NAME_MAXLEN, "%s/%s", locfs, term);
141
142 reopen(&stdin, 0, term_node, MODE_READ, "r");
143 reopen(&stdout, 1, term_node, MODE_WRITE, "w");
144 reopen(&stderr, 2, term_node, MODE_WRITE, "w");
145
146 if (stdin == NULL)
147 return 4;
148
149 if (stdout == NULL)
150 return 5;
151
152 if (stderr == NULL)
153 return 6;
154
155 /*
156 * FIXME: fdopen() should actually detect that we are opening a console
157 * and it should set line-buffering mode automatically.
158 */
159 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
160
161 version_print(term);
162 if (print_msg)
163 welcome_msg_print();
164
165 task_id_t id;
166 task_wait_t twait;
167
168 int rc = task_spawnv(&id, &twait, cmd, (const char * const *) args);
169 if (rc != EOK) {
170 printf("%s: Error spawning %s (%s)\n", APP_NAME, cmd,
171 str_error(rc));
172 return rc;
173 }
174
175 task_exit_t texit;
176 int retval;
177 rc = task_wait(&twait, &texit, &retval);
178 if (rc != EOK) {
179 printf("%s: Error waiting for %s (%s)\n", APP_NAME, cmd,
180 str_error(rc));
181 return rc;
182 }
183
184 return 0;
185}
186
187/** @}
188 */
Note: See TracBrowser for help on using the repository browser.