source: mainline/uspace/app/getterm/getterm.c@ 119b46e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 119b46e was dd567c6, checked in by Martin Decky <martin@…>, 15 years ago

tiny cstyle

  • Property mode set to 100644
File size: 3.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
37#include <sys/types.h>
[2b88074b]38#include <fcntl.h>
[49f78c7]39#include <unistd.h>
40#include <stdio.h>
41#include <task.h>
[d9fae235]42#include <str_error.h>
[0485135]43#include <errno.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{
[4deb8b5]51 printf("Usage: %s <terminal> [-w] <command> [<arguments...>]\n", APP_NAME);
[49f78c7]52}
53
[2b88074b]54static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
[49f78c7]55{
[2b88074b]56 if (fclose(*stream))
57 return;
[49f78c7]58
[2b88074b]59 *stream = NULL;
60
61 int oldfd = open(path, flags);
62 if (oldfd < 0)
63 return;
64
65 if (oldfd != fd) {
66 if (dup2(oldfd, fd) != fd)
67 return;
68
69 if (close(oldfd))
70 return;
71 }
72
73 *stream = fdopen(fd, mode);
[49f78c7]74}
75
[d52b0044]76int main(int argc, char *argv[])
[49f78c7]77{
[0485135]78 int rc;
[d52b0044]79 task_exit_t texit;
80 int retval;
81 task_id_t id;
[4deb8b5]82 char *fname, *term;
83 char **cmd_args;
84 bool print_wmsg;
[49f78c7]85
[dd567c6]86 argv++;
87 argc--;
[4deb8b5]88 if (argc < 1) {
[49f78c7]89 usage();
90 return -1;
91 }
[4deb8b5]92
93 if (str_cmp(*argv, "-w") == 0) {
94 print_wmsg = true;
[dd567c6]95 argv++;
96 argc--;
[4deb8b5]97 } else {
98 print_wmsg = false;
99 }
100
101 if (argc < 2) {
102 usage();
103 return -1;
104 }
105
106 term = *argv++;
107 fname = *argv;
108 cmd_args = argv;
[49f78c7]109
[4deb8b5]110 reopen(&stdin, 0, term, O_RDONLY, "r");
111 reopen(&stdout, 1, term, O_WRONLY, "w");
112 reopen(&stderr, 2, term, O_WRONLY, "w");
[49f78c7]113
[e3fa92aa]114 /*
[2b88074b]115 * FIXME: fdopen() should actually detect that we are opening a console
[e3fa92aa]116 * and it should set line-buffering mode automatically.
117 */
118 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
[49f78c7]119
[2b88074b]120 if (stdin == NULL)
[49f78c7]121 return -2;
122
[2b88074b]123 if (stdout == NULL)
124 return -3;
125
126 if (stderr == NULL)
127 return -4;
128
[4deb8b5]129 version_print(term);
130 if (print_wmsg)
131 welcome_msg_print();
132
133 rc = task_spawnv(&id, fname, (const char * const *) cmd_args);
[d52b0044]134 if (rc != EOK) {
135 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
136 str_error(rc));
137 return -5;
[c7dc8ad]138 }
[d52b0044]139
140 rc = task_wait(id, &texit, &retval);
141 if (rc != EOK) {
142 printf("%s: Error waiting for %s (%s)\n", APP_NAME, fname,
143 str_error(rc));
144 return -6;
145 }
146
147 return 0;
[49f78c7]148}
149
150/** @}
151 */
Note: See TracBrowser for help on using the repository browser.