Subversion Repositories HelenOS

Rev

Rev 3275 | Rev 3339 | 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.  
  37. #include "config.h"
  38. #include "util.h"
  39. #include "scli.h"
  40. #include "input.h"
  41. #include "errors.h"
  42. #include "exec.h"
  43.  
  44. extern volatile unsigned int cli_interactive;
  45.  
  46. /* More than a macro than anything */
  47. void cli_restricted(char *cmd)
  48. {
  49.     cli_verbose("%s is not available in %s mode\n", cmd,
  50.         cli_interactive ? "interactive" : "non-interactive");
  51.  
  52.     return;
  53. }
  54.  
  55. /* Tokenizes input from console, sees if the first word is a built-in, if so
  56.  * invokes the built-in entry point (a[0]) passing all arguments in a[] to
  57.  * the handler */
  58. int tok_input(cliuser_t *usr)
  59. {
  60.     char *cmd[WORD_MAX];
  61.     int n = 0, i = 0;
  62.     int rc = 0;
  63.     char *tmp;
  64.  
  65.     if (NULL == usr->line)
  66.         return CL_EFAIL;
  67.  
  68.     tmp = cli_strdup(usr->line);
  69.  
  70.     /* Break up what the user typed, space delimited */
  71.     cmd[n] = cli_strtok(tmp, " ");
  72.     while (cmd[n] && n < WORD_MAX) {
  73.         cmd[++n] = cli_strtok(NULL, " ");
  74.     }
  75.  
  76.     /* We have rubbish */
  77.     if (NULL == cmd[0]) {
  78.         rc = CL_ENOENT;
  79.         goto finit;
  80.     }
  81.  
  82.     /* Check what kind of command argv[0] might be */
  83.     if ((i = (is_builtin(cmd[0]))) > -1) {
  84.         /* Its a builtin */
  85.         if (builtin_is_restricted(i)) {
  86.             /* Try an external command matching argv[0] */
  87.                 rc = try_exec(cmd[0], cmd);
  88.                 if (rc)
  89.                     cli_restricted(cmd[0]);
  90.                 goto finit;
  91.         }
  92.         rc = run_builtin(i, cmd, usr);
  93.         goto finit;
  94.     } else if ((i = (is_module(cmd[0]))) > -1) {
  95.         /* Its a module, it can't modify cliuser_t */
  96.         if (module_is_restricted(i)) {
  97.             rc = try_exec(cmd[0], cmd);
  98.             if (rc)
  99.                 cli_restricted(cmd[0]);
  100.             goto finit;
  101.         }
  102.         rc = run_module(i, cmd);
  103.         goto finit;
  104.     } else {
  105.         /* See what try_exec() thinks of it */
  106.         rc = try_exec(cmd[0], cmd);
  107.         goto finit;
  108.     }
  109.  
  110. finit:
  111.     if (NULL != usr->line) {
  112.         free(usr->line);
  113.         usr->line = (char *) NULL;
  114.     }
  115.     if (NULL != tmp)
  116.         free(tmp);
  117.  
  118.     return rc;
  119. }
  120.  
  121. /* Borrowed from Jiri Svoboda's 'cli' uspace app */
  122. void read_line(char *buffer, int n)
  123. {
  124.     char c;
  125.     int chars;
  126.  
  127.     chars = 0;
  128.     while (chars < n - 1) {
  129.         c = getchar();
  130.         if (c < 0)
  131.             return;
  132.         if (c == '\n')
  133.             break;
  134.         if (c == '\b') {
  135.             if (chars > 0) {
  136.                 putchar('\b');
  137.                 --chars;
  138.             }
  139.             continue;
  140.         }
  141.         putchar(c);
  142.         buffer[chars++] = c;
  143.     }
  144.     putchar('\n');
  145.     buffer[chars] = '\0';
  146. }
  147.  
  148. void get_input(cliuser_t *usr)
  149. {
  150.     char line[INPUT_MAX];
  151.     size_t len = 0;
  152.  
  153.     printf("%s", usr->prompt);
  154.     read_line(line, INPUT_MAX);
  155.     len = strlen(line);
  156.     /* Make sure we don't have rubbish or a C/R happy user */
  157.     if (len == 0 || line[0] == '\n')
  158.         return;
  159.     if (len == 1 && line[len-1] == '\n')
  160.         return;
  161.     usr->line = cli_strdup(line);
  162.  
  163.     return;
  164. }
  165.  
  166.