Subversion Repositories HelenOS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
  2.  * All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are met:
  6.  *
  7.  * Redistributions of source code must retain the above copyright notice, this
  8.  * list of conditions and the following disclaimer.
  9.  *
  10.  * Redistributions in binary form must reproduce the above copyright notice,
  11.  * this list of conditions and the following disclaimer in the documentation
  12.  * and/or other materials provided with the distribution.
  13.  *
  14.  * Neither the name of the original program's authors nor the names of its
  15.  * contributors may be used to endorse or promote products derived from this
  16.  * software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include "config.h"
  36. #include "scli.h"
  37. #include "input.h"
  38. #include "util.h"
  39. #include "errors.h"
  40. #include "cmds/cmds.h"
  41.  
  42. #ifndef HELENOS
  43. #include <linux/limits.h>
  44. #endif
  45.  
  46. /* See scli.h */
  47. static cliuser_t usr;
  48.  
  49. /* Modified by the 'quit' module, which is compiled before this */
  50. extern unsigned int cli_quit;
  51.  
  52. /* Globals that are modified during start-up that modules/builtins should
  53.  * be aware of. */
  54. volatile unsigned int cli_interactive = 1;
  55. volatile unsigned int cli_verbocity = 1;
  56.  
  57. /* The official name of this program
  58.  * (change to your liking in configure.ac and re-run autoconf) */
  59. const char *progname = PACKAGE_NAME;
  60.  
  61. /* (re)allocates memory to store the current working directory, gets
  62.  * and updates the current working directory, formats the prompt string */
  63. int cli_set_prompt(cliuser_t *usr)
  64. {
  65.     usr->prompt = (char *) realloc(usr->prompt, PATH_MAX);
  66.     if (NULL == usr->prompt) {
  67.         cli_error(CL_ENOMEM, "Can not allocate prompt");
  68.         return 1;
  69.     }
  70.     memset(usr->prompt, 0, sizeof(usr->prompt));
  71.  
  72.     usr->cwd = (char *) realloc(usr->cwd, PATH_MAX);
  73.     if (NULL == usr->cwd) {
  74.         cli_error(CL_ENOMEM, "Can not allocate cwd");
  75.         return 1;
  76.     }
  77.     memset(usr->cwd, 0, sizeof(usr->cwd));
  78.  
  79.     usr->cwd = getcwd(usr->cwd, PATH_MAX - 1);
  80.     if (NULL == usr->cwd)
  81.         snprintf(usr->cwd, PATH_MAX, "(unknown)");
  82.  
  83.     if (usr->lasterr != 0)
  84.         snprintf(usr->prompt,
  85.             PATH_MAX,
  86.             "[%lld] %s # ",
  87.             usr->lasterr, usr->cwd);
  88.     else
  89.         snprintf(usr->prompt,
  90.             PATH_MAX,
  91.             "%s # ",
  92.             usr->cwd);
  93.  
  94.     return 0;
  95. }
  96.  
  97. int cli_init(cliuser_t *usr)
  98. {
  99.     usr->line = (char *) NULL;
  100.     usr->name = "root";
  101.     usr->home = "/";
  102.     usr->cwd = (char *) NULL;
  103.     usr->prompt = (char *) NULL;
  104.     chdir(usr->home);
  105.     usr->lasterr = 0;
  106.     return (int) cli_set_prompt(usr);
  107. }
  108.  
  109. /* Destructor */
  110. void cli_finit(cliuser_t *usr)
  111. {
  112.     if (NULL != usr->line)
  113.         free(usr->line);
  114.     if (NULL != usr->prompt)
  115.         free(usr->prompt);
  116.     if (NULL != usr->cwd)
  117.         free(usr->cwd);
  118. }
  119.  
  120. int main(int argc, char *argv[])
  121. {
  122.     int ret = 0;
  123.     int i = 0;
  124.  
  125.     if (cli_init(&usr))
  126.         exit(EXIT_FAILURE);
  127.  
  128.     while (!cli_quit) {
  129.         cli_set_prompt(&usr);
  130.         get_input(&usr);
  131.         if (NULL != usr.line) {
  132.             ret = tok_input(&usr);
  133.             usr.lasterr = ret;
  134.         }
  135.         i++;
  136.     }
  137.     goto finit;
  138.  
  139. finit:
  140.     cli_finit(&usr);
  141.     return ret;
  142. }
  143.