getterm.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Martin Decky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <sys/types.h>
00038 #include <fcntl.h>
00039 #include <unistd.h>
00040 #include <stdio.h>
00041 #include <task.h>
00042 #include <str_error.h>
00043 #include <errno.h>
00044 #include "version.h"
00045 #include "welcome.h"
00046 
00047 #define APP_NAME  "getterm"
00048 
00049 static void usage(void)
00050 {
00051         printf("Usage: %s <terminal> [-w] <command> [<arguments...>]\n", APP_NAME);
00052 }
00053 
00054 static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
00055 {
00056         if (fclose(*stream))
00057                 return;
00058         
00059         *stream = NULL;
00060         
00061         int oldfd = open(path, flags);
00062         if (oldfd < 0)
00063                 return;
00064         
00065         if (oldfd != fd) {
00066                 if (dup2(oldfd, fd) != fd)
00067                         return;
00068                 
00069                 if (close(oldfd))
00070                         return;
00071         }
00072         
00073         *stream = fdopen(fd, mode);
00074 }
00075 
00076 int main(int argc, char *argv[])
00077 {
00078         int rc;
00079         task_exit_t texit;
00080         int retval;
00081         task_id_t id;
00082         char *fname, *term;
00083         char **cmd_args;
00084         bool print_wmsg;
00085 
00086         argv++;
00087         argc--;
00088         if (argc < 1) {
00089                 usage();
00090                 return -1;
00091         }
00092 
00093         if (str_cmp(*argv, "-w") == 0) {
00094                 print_wmsg = true;
00095                 argv++;
00096                 argc--;
00097         } else {
00098                 print_wmsg = false;
00099         }
00100 
00101         if (argc < 2) {
00102                 usage();
00103                 return -1;
00104         }
00105 
00106         term = *argv++;
00107         fname = *argv;
00108         cmd_args = argv;
00109         
00110         reopen(&stdin, 0, term, O_RDONLY, "r");
00111         reopen(&stdout, 1, term, O_WRONLY, "w");
00112         reopen(&stderr, 2, term, O_WRONLY, "w");
00113         
00114         /*
00115          * FIXME: fdopen() should actually detect that we are opening a console
00116          * and it should set line-buffering mode automatically.
00117          */
00118         setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
00119         
00120         if (stdin == NULL)
00121                 return -2;
00122         
00123         if (stdout == NULL)
00124                 return -3;
00125         
00126         if (stderr == NULL)
00127                 return -4;
00128         
00129         version_print(term);
00130         if (print_wmsg)
00131                 welcome_msg_print();
00132 
00133         rc = task_spawnv(&id, fname, (const char * const *) cmd_args);
00134         if (rc != EOK) {
00135                 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
00136                     str_error(rc));
00137                 return -5;
00138         }
00139 
00140         rc = task_wait(id, &texit, &retval);
00141         if (rc != EOK) {
00142                 printf("%s: Error waiting for %s (%s)\n", APP_NAME, fname,
00143                     str_error(rc));
00144                 return -6;
00145         }
00146 
00147         return 0;
00148 }
00149 

Generated on Thu Jun 2 07:45:41 2011 for HelenOS/USB by  doxygen 1.4.7