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