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
RevLine 
[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
[df747bd8]29/** @addtogroup getterm GetTerm
[49f78c7]30 * @brief Console initialization task.
31 * @{
32 */
33/**
34 * @file
35 */
36
[8d2dd7f2]37#include <stdint.h>
[49f78c7]38#include <stdio.h>
39#include <task.h>
[d9fae235]40#include <str_error.h>
[0485135]41#include <errno.h>
[593e023]42#include <loc.h>
[fcab7ef]43#include <vfs/vfs.h>
[49f78c7]44#include "version.h"
[4deb8b5]45#include "welcome.h"
[49f78c7]46
[d9fae235]47#define APP_NAME "getterm"
48
[49f78c7]49static 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]59static void reopen(FILE **stream, int fd, const char *path, int mode,
60 const char *fmode)
[49f78c7]61{
[2b88074b]62 if (fclose(*stream))
63 return;
[49f78c7]64
[2b88074b]65 *stream = NULL;
66
[f77c1c9]67 int oldfd;
68 int rc = vfs_lookup_open(path, WALK_REGULAR, mode, &oldfd);
69 if (rc != EOK)
[2b88074b]70 return;
71
72 if (oldfd != fd) {
[f77c1c9]73 int newfd;
74 if (vfs_clone(oldfd, fd, false, &newfd) != EOK)
[2b88074b]75 return;
76
[f77c1c9]77 assert(newfd == fd);
78
[9c4cf0d]79 if (vfs_put(oldfd))
[2b88074b]80 return;
81 }
82
[b19e892]83 *stream = fdopen(fd, fmode);
[49f78c7]84}
85
[d52b0044]86int 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 }
[593e023]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
[dd567c6]116 argv++;
117 argc--;
[4deb8b5]118 }
[593e023]119
120 if (argc < 1) {
[4deb8b5]121 usage();
[593e023]122 return 3;
[4deb8b5]123 }
[49f78c7]124
[593e023]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
[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");
[49f78c7]149
[2b88074b]150 if (stdin == NULL)
[593e023]151 return 4;
[49f78c7]152
[2b88074b]153 if (stdout == NULL)
[593e023]154 return 5;
[2b88074b]155
156 if (stderr == NULL)
[593e023]157 return 6;
[2b88074b]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);
164
[4deb8b5]165 version_print(term);
[593e023]166 if (print_msg)
[4deb8b5]167 welcome_msg_print();
[593e023]168
169 task_id_t id;
[1c635d6]170 task_wait_t twait;
[593e023]171
[1c635d6]172 int 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 }
[593e023]178
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 }
[593e023]187
[d52b0044]188 return 0;
[49f78c7]189}
190
191/** @}
192 */
Note: See TracBrowser for help on using the repository browser.