Subversion Repositories HelenOS

Rev

Rev 3809 | Rev 3813 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
  2.  * All rights reserved.
  3.  * Copyright (c) 2008, Jiri Svoboda - 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 are met:
  7.  *
  8.  * Redistributions of source code must retain the above copyright notice, this
  9.  * list of conditions and the following disclaimer.
  10.  *
  11.  * Redistributions in binary form must reproduce the above copyright notice,
  12.  * this list of conditions and the following disclaimer in the documentation
  13.  * and/or other materials provided with the distribution.
  14.  *
  15.  * Neither the name of the original program's authors nor the names of its
  16.  * contributors may be used to endorse or promote products derived from this
  17.  * software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <io/stream.h>
  36. #include <console.h>
  37.  
  38. #include "config.h"
  39. #include "util.h"
  40. #include "scli.h"
  41. #include "input.h"
  42. #include "errors.h"
  43. #include "exec.h"
  44.  
  45. static void read_line(char *, int);
  46.  
  47. /* Tokenizes input from console, sees if the first word is a built-in, if so
  48.  * invokes the built-in entry point (a[0]) passing all arguments in a[] to
  49.  * the handler */
  50. int tok_input(cliuser_t *usr)
  51. {
  52.     char *cmd[WORD_MAX];
  53.     int n = 0, i = 0;
  54.     int rc = 0;
  55.     char *tmp;
  56.  
  57.     if (NULL == usr->line)
  58.         return CL_EFAIL;
  59.  
  60.     tmp = cli_strdup(usr->line);
  61.  
  62.     cmd[n] = cli_strtok(tmp, " ");
  63.     while (cmd[n] && n < WORD_MAX) {
  64.         cmd[++n] = cli_strtok(NULL, " ");
  65.     }
  66.  
  67.     /* We have rubbish */
  68.     if (NULL == cmd[0]) {
  69.         rc = CL_ENOENT;
  70.         goto finit;
  71.     }
  72.  
  73.     /* Its a builtin command ? */
  74.     if ((i = (is_builtin(cmd[0]))) > -1) {
  75.         rc = run_builtin(i, cmd, usr);
  76.         goto finit;
  77.     /* Its a module ? */
  78.     } else if ((i = (is_module(cmd[0]))) > -1) {
  79.         rc = run_module(i, cmd);
  80.         goto finit;
  81.     }
  82.  
  83.     /* See what try_exec thinks of it */
  84.     rc = try_exec(cmd[0], cmd);
  85.  
  86. finit:
  87.     if (NULL != usr->line) {
  88.         free(usr->line);
  89.         usr->line = (char *) NULL;
  90.     }
  91.     if (NULL != tmp)
  92.         free(tmp);
  93.  
  94.     return rc;
  95. }
  96.  
  97. /* Borrowed from Jiri Svoboda's 'cli' uspace app */
  98. static void read_line(char *buffer, int n)
  99. {
  100.     char c;
  101.     int chars;
  102.  
  103.     chars = 0;
  104.     while (chars < n - 1) {
  105.         c = getchar();
  106.         if (c < 0)
  107.             return;
  108.         if (c == '\n')
  109.             break;
  110.         if (c == '\b') {
  111.             if (chars > 0) {
  112.                 putchar('\b');
  113.                 --chars;
  114.             }
  115.             continue;
  116.         }
  117.         putchar(c);
  118.         buffer[chars++] = c;
  119.     }
  120.     putchar('\n');
  121.     buffer[chars] = '\0';
  122. }
  123.  
  124. /* TODO:
  125.  * Implement something like editline() / readline(), if even
  126.  * just for command history and making arrows work. */
  127. void get_input(cliuser_t *usr)
  128. {
  129.     char line[INPUT_MAX];
  130.     size_t len = 0;
  131.  
  132.     console_set_style(STYLE_EMPHASIS);
  133.     printf("%s", usr->prompt);
  134.     console_set_style(STYLE_NORMAL);
  135.  
  136.     read_line(line, INPUT_MAX);
  137.     len = strlen(line);
  138.     /* Make sure we don't have rubbish or a C/R happy user */
  139.     if (len == 0 || line[0] == '\n')
  140.         return;
  141.     usr->line = cli_strdup(line);
  142.  
  143.     return;
  144. }
  145.  
  146.