Changeset 04803bf in mainline for uspace/app/getterm/getterm.c
- Timestamp:
- 2011-03-21T22:00:17Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 143932e3
- Parents:
- b50b5af2 (diff), 7308e84 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/getterm/getterm.c
rb50b5af2 r04803bf 27 27 */ 28 28 29 /** @addtogroup get vc GetVC29 /** @addtogroup getterm GetTerm 30 30 * @brief Console initialization task. 31 31 * @{ … … 36 36 37 37 #include <sys/types.h> 38 #include <fcntl.h> 38 39 #include <unistd.h> 39 40 #include <stdio.h> 40 41 #include <task.h> 42 #include <str_error.h> 43 #include <errno.h> 41 44 #include "version.h" 45 #include "welcome.h" 46 47 #define APP_NAME "getterm" 42 48 43 49 static void usage(void) 44 50 { 45 printf("Usage: getvc <device> <path>\n");51 printf("Usage: %s <terminal> [-w] <command> [<arguments...>]\n", APP_NAME); 46 52 } 47 53 48 static void closeall(void)54 static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode) 49 55 { 50 fclose(stdin); 51 fclose(stdout); 52 fclose(stderr); 56 if (fclose(*stream)) 57 return; 53 58 54 close(0); 55 close(1); 56 close(2); 57 } 58 59 static task_id_t spawn(char *fname) 60 { 61 char *argv[2]; 59 *stream = NULL; 62 60 63 argv[0] = fname; 64 argv[1] = NULL; 61 int oldfd = open(path, flags); 62 if (oldfd < 0) 63 return; 65 64 66 task_id_t id = task_spawn(fname, argv); 65 if (oldfd != fd) { 66 if (dup2(oldfd, fd) != fd) 67 return; 68 69 if (close(oldfd)) 70 return; 71 } 67 72 68 if (id == 0) 69 printf("Error spawning %s\n", fname); 70 71 return id; 73 *stream = fdopen(fd, mode); 72 74 } 73 75 74 76 int main(int argc, char *argv[]) 75 77 { 78 int rc; 76 79 task_exit_t texit; 77 80 int retval; 81 task_id_t id; 82 char *fname, *term; 83 char **cmd_args; 84 bool print_wmsg; 78 85 79 if (argc < 3) { 86 argv++; 87 argc--; 88 if (argc < 1) { 80 89 usage(); 81 90 return -1; 82 91 } 92 93 if (str_cmp(*argv, "-w") == 0) { 94 print_wmsg = true; 95 argv++; 96 argc--; 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; 83 109 84 closeall(); 110 reopen(&stdin, 0, term, O_RDONLY, "r"); 111 reopen(&stdout, 1, term, O_WRONLY, "w"); 112 reopen(&stderr, 2, term, O_WRONLY, "w"); 85 113 86 stdin = fopen(argv[1], "r");87 stdout = fopen(argv[1], "w");88 stderr = fopen(argv[1], "w");89 90 114 /* 91 * FIXME: f open() should actually detect that we are opening a console115 * FIXME: fdopen() should actually detect that we are opening a console 92 116 * and it should set line-buffering mode automatically. 93 117 */ 94 118 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); 95 119 96 if ((stdin == NULL) 97 || (stdout == NULL) 98 || (stderr == NULL)) 120 if (stdin == NULL) 99 121 return -2; 100 122 101 version_print(argv[1]); 102 task_id_t id = spawn(argv[2]); 103 task_wait(id, &texit, &retval); 123 if (stdout == NULL) 124 return -3; 104 125 126 if (stderr == NULL) 127 return -4; 128 129 version_print(term); 130 if (print_wmsg) 131 welcome_msg_print(); 132 133 rc = task_spawnv(&id, fname, (const char * const *) cmd_args); 134 if (rc != EOK) { 135 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname, 136 str_error(rc)); 137 return -5; 138 } 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 105 147 return 0; 106 148 }
Note:
See TracChangeset
for help on using the changeset viewer.