source: mainline/uspace/app/getterm/getterm.c@ 9c4cf0d

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

Rename close() to vfs_put()

This is motivated mainly by the fact that a file handle does not
necessarily correspond to an open file and close() was no longer the
the opposite operation to open().

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