input.c

00001 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
00002  * All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *
00007  * Redistributions of source code must retain the above copyright notice, this
00008  * list of conditions and the following disclaimer.
00009  *
00010  * Redistributions in binary form must reproduce the above copyright notice,
00011  * this list of conditions and the following disclaimer in the documentation
00012  * and/or other materials provided with the distribution.
00013  *
00014  * Neither the name of the original program's authors nor the names of its
00015  * contributors may be used to endorse or promote products derived from this
00016  * software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <str.h>
00034 #include <io/console.h>
00035 #include <io/keycode.h>
00036 #include <io/style.h>
00037 #include <io/color.h>
00038 #include <vfs/vfs.h>
00039 #include <clipboard.h>
00040 #include <macros.h>
00041 #include <errno.h>
00042 #include <assert.h>
00043 #include <bool.h>
00044 #include <tinput.h>
00045 
00046 #include "config.h"
00047 #include "util.h"
00048 #include "scli.h"
00049 #include "input.h"
00050 #include "errors.h"
00051 #include "exec.h"
00052 
00053 extern volatile unsigned int cli_quit;
00054 
00056 static tinput_t *tinput;
00057 
00058 /* Tokenizes input from console, sees if the first word is a built-in, if so
00059  * invokes the built-in entry point (a[0]) passing all arguments in a[] to
00060  * the handler */
00061 int tok_input(cliuser_t *usr)
00062 {
00063         char *cmd[WORD_MAX];
00064         int n = 0, i = 0;
00065         int rc = 0;
00066         char *tmp;
00067 
00068         if (NULL == usr->line)
00069                 return CL_EFAIL;
00070 
00071         tmp = str_dup(usr->line);
00072 
00073         cmd[n] = strtok(tmp, " ");
00074         while (cmd[n] && n < WORD_MAX) {
00075                 cmd[++n] = strtok(NULL, " ");
00076         }
00077 
00078         /* We have rubbish */
00079         if (NULL == cmd[0]) {
00080                 rc = CL_ENOENT;
00081                 goto finit;
00082         }
00083 
00084         /* Its a builtin command ? */
00085         if ((i = (is_builtin(cmd[0]))) > -1) {
00086                 rc = run_builtin(i, cmd, usr);
00087                 goto finit;
00088         /* Its a module ? */
00089         } else if ((i = (is_module(cmd[0]))) > -1) {
00090                 rc = run_module(i, cmd);
00091                 goto finit;
00092         }
00093 
00094         /* See what try_exec thinks of it */
00095         rc = try_exec(cmd[0], cmd);
00096 
00097 finit:
00098         if (NULL != usr->line) {
00099                 free(usr->line);
00100                 usr->line = (char *) NULL;
00101         }
00102         if (NULL != tmp)
00103                 free(tmp);
00104 
00105         return rc;
00106 }
00107 
00108 void get_input(cliuser_t *usr)
00109 {
00110         char *str;
00111         int rc;
00112 
00113         fflush(stdout);
00114         console_set_style(fphone(stdout), STYLE_EMPHASIS);
00115         printf("%s", usr->prompt);
00116         fflush(stdout);
00117         console_set_style(fphone(stdout), STYLE_NORMAL);
00118 
00119         rc = tinput_read(tinput, &str);
00120         if (rc == ENOENT) {
00121                 /* User requested exit */
00122                 cli_quit = 1;
00123                 putchar('\n');
00124                 return;
00125         }
00126 
00127         if (rc != EOK) {
00128                 /* Error in communication with console */
00129                 return;
00130         }
00131 
00132         /* Check for empty input. */
00133         if (str_cmp(str, "") == 0) {
00134                 free(str);
00135                 return;
00136         }
00137 
00138         usr->line = str;
00139         return;
00140 }
00141 
00142 int input_init(void)
00143 {
00144         tinput = tinput_new();
00145         if (tinput == NULL) {
00146                 printf("Failed to initialize input.\n");
00147                 return 1;
00148         }
00149 
00150         return 0;
00151 }

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