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