source: mainline/uspace/app/getterm/getterm.c@ 7494fb4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7494fb4 was 0485135, checked in by Jiri Svoboda <jiri@…>, 15 years ago

For more convenience, replace task_spawn() with task_spawnv() and task_spawnl().

  • Property mode set to 100644
File size: 3.0 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 <fcntl.h>
39#include <unistd.h>
40#include <stdio.h>
41#include <task.h>
42#include <str_error.h>
43#include <errno.h>
44#include "version.h"
45
46#define APP_NAME "getterm"
47
48static void usage(void)
49{
50 printf("Usage: %s <terminal> <path>\n", APP_NAME);
51}
52
53static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
54{
55 if (fclose(*stream))
56 return;
57
58 *stream = NULL;
59
60 int oldfd = open(path, flags);
61 if (oldfd < 0)
62 return;
63
64 if (oldfd != fd) {
65 if (dup2(oldfd, fd) != fd)
66 return;
67
68 if (close(oldfd))
69 return;
70 }
71
72 *stream = fdopen(fd, mode);
73}
74
75static task_id_t spawn(const char *fname)
76{
77 task_id_t id;
78 int rc;
79
80 rc = task_spawnl(&id, fname, fname, NULL);
81 if (rc != EOK) {
82 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
83 str_error(rc));
84 return 0;
85 }
86
87 return id;
88}
89
90int main(int argc, char *argv[])
91{
92 if (argc < 3) {
93 usage();
94 return -1;
95 }
96
97 reopen(&stdin, 0, argv[1], O_RDONLY, "r");
98 reopen(&stdout, 1, argv[1], O_WRONLY, "w");
99 reopen(&stderr, 2, argv[1], O_WRONLY, "w");
100
101 /*
102 * FIXME: fdopen() should actually detect that we are opening a console
103 * and it should set line-buffering mode automatically.
104 */
105 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
106
107 if (stdin == NULL)
108 return -2;
109
110 if (stdout == NULL)
111 return -3;
112
113 if (stderr == NULL)
114 return -4;
115
116 version_print(argv[1]);
117 task_id_t id = spawn(argv[2]);
118
119 if (id != 0) {
120 task_exit_t texit;
121 int retval;
122 task_wait(id, &texit, &retval);
123
124 return 0;
125 }
126
127 return -5;
128}
129
130/** @}
131 */
Note: See TracBrowser for help on using the repository browser.