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
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>
[84a1a54]39#include <stdlib.h>
[49f78c7]40#include <task.h>
[d9fae235]41#include <str_error.h>
[0485135]42#include <errno.h>
[593e023]43#include <loc.h>
[fcab7ef]44#include <vfs/vfs.h>
[49f78c7]45#include "version.h"
[4deb8b5]46#include "welcome.h"
[49f78c7]47
[d9fae235]48#define APP_NAME "getterm"
49
[49f78c7]50static void usage(void)
51{
[593e023]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");
[49f78c7]58}
59
[b19e892]60static void reopen(FILE **stream, int fd, const char *path, int mode,
61 const char *fmode)
[49f78c7]62{
[2b88074b]63 if (fclose(*stream))
64 return;
[49f78c7]65
[2b88074b]66 *stream = NULL;
67
[f77c1c9]68 int oldfd;
[46577995]69 errno_t rc = vfs_lookup_open(path, WALK_REGULAR, mode, &oldfd);
[f77c1c9]70 if (rc != EOK)
[2b88074b]71 return;
72
73 if (oldfd != fd) {
[f77c1c9]74 int newfd;
75 if (vfs_clone(oldfd, fd, false, &newfd) != EOK)
[2b88074b]76 return;
77
[f77c1c9]78 assert(newfd == fd);
79
[9c4cf0d]80 if (vfs_put(oldfd))
[2b88074b]81 return;
82 }
83
[b19e892]84 *stream = fdopen(fd, fmode);
[49f78c7]85}
86
[d52b0044]87int main(int argc, char *argv[])
[49f78c7]88{
[dd567c6]89 argv++;
90 argc--;
[593e023]91 if (argc < 4) {
[49f78c7]92 usage();
[593e023]93 return 1;
[49f78c7]94 }
[593e023]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
[dd567c6]117 argv++;
118 argc--;
[4deb8b5]119 }
[593e023]120
121 if (argc < 1) {
[4deb8b5]122 usage();
[593e023]123 return 3;
[4deb8b5]124 }
[49f78c7]125
[593e023]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;
[46577995]136 errno_t rc = loc_service_get_id(term, &service_id, IPC_FLAG_BLOCKING);
[593e023]137 if (rc != EOK) {
138 printf("%s: Error waiting on %s (%s)\n", APP_NAME, term,
139 str_error(rc));
[84a1a54]140 return EXIT_RC(rc);
[593e023]141 }
142 }
143
144 char term_node[LOC_NAME_MAXLEN];
145 snprintf(term_node, LOC_NAME_MAXLEN, "%s/%s", locfs, term);
146
[b19e892]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");
[49f78c7]150
[2b88074b]151 if (stdin == NULL)
[593e023]152 return 4;
[49f78c7]153
[2b88074b]154 if (stdout == NULL)
[593e023]155 return 5;
[2b88074b]156
157 if (stderr == NULL)
[593e023]158 return 6;
[2b88074b]159
[884c56b]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
[4deb8b5]166 version_print(term);
[593e023]167 if (print_msg)
[4deb8b5]168 welcome_msg_print();
[593e023]169
170 task_id_t id;
[1c635d6]171 task_wait_t twait;
[593e023]172
[46577995]173 errno_t rc = task_spawnv(&id, &twait, cmd, (const char * const *) args);
[d52b0044]174 if (rc != EOK) {
[593e023]175 printf("%s: Error spawning %s (%s)\n", APP_NAME, cmd,
[d52b0044]176 str_error(rc));
[84a1a54]177 return EXIT_RC(rc);
[c7dc8ad]178 }
[593e023]179
180 task_exit_t texit;
181 int retval;
[1c635d6]182 rc = task_wait(&twait, &texit, &retval);
[d52b0044]183 if (rc != EOK) {
[593e023]184 printf("%s: Error waiting for %s (%s)\n", APP_NAME, cmd,
[d52b0044]185 str_error(rc));
[84a1a54]186 return EXIT_RC(rc);
[d52b0044]187 }
[593e023]188
[d52b0044]189 return 0;
[49f78c7]190}
191
192/** @}
193 */
Note: See TracBrowser for help on using the repository browser.