static MYX_GRT_SHELL_COMMAND shell_execute(MYX_GRT *grt, const char *linebuf)
{
  char *cmd;
  unsigned int cmd_len;
  char *cmd_param= NULL;
  MYX_GRT_SHELL_COMMAND res= MYX_GRT_SHELL_COMMAND_UNKNOWN;
  char *preprocessed_cmd= NULL;

  cmd= g_strdup(linebuf);
  cmd= str_trim(cmd);
  cmd_len= (unsigned int)strlen(cmd);

  //Quit command
  if( (strcmp2(cmd, "quit")==0) || (strcmp2(cmd, "quit;")==0) || (strcmp2(cmd, "exit")==0) ||
    (strcmp2(cmd, "exit;")==0) || (strcmp2(cmd, "\\q")==0) || (strcmp2(cmd, "q")==0) || (strcmp2(cmd, "\\e")==0))
  {
    myx_grt_printf(grt, "Exiting...");

    res= MYX_GRT_SHELL_COMMAND_EXIT;
  }
  else if( (strcmp2(cmd, "run")==0) || (str_beginswith(cmd, "\\r")) || (str_beginswith(cmd, "run ")))
  {
    char *file_name= get_value_from_text_ex(cmd, (int)strlen(cmd), "(run|\\\\r)\\s+(.+)", 2);

    if((file_name) && (file_name[0]))
    {
      preprocessed_cmd= g_strdup_printf("run(\"%s\")"NL, file_name);
      res= MYX_GRT_SHELL_COMMAND_STATEMENT;
    }
    else
    {
      myx_grt_shell_show_command_help(grt, "run");
      res= MYX_GRT_SHELL_COMMAND_HELP;
    }

    if(file_name)
      g_free(file_name);
  }
  // Automatically convert cd.. to cd(".." and
  //   cd objectname to cd("objectname")
  else if ((strcmp2(cmd, "cd")==0) || (strcmp2(cmd, "cd..")==0) || (str_beginswith(cmd, "cd ")))
  {
    char *path= get_value_from_text_ex(cmd, (int)strlen(cmd), "cd\\s*(.+)", 1);

    if((path) && (path[0]))
    {
      preprocessed_cmd= g_strdup_printf("grt.cd(\"%s\")"NL, path);
      res= MYX_GRT_SHELL_COMMAND_STATEMENT;
    }
    else
    {
      preprocessed_cmd= g_strdup_printf("print grt.pwd()"NL);
      res= MYX_GRT_SHELL_COMMAND_STATEMENT;
    }

    if(path)
      g_free(path);
  }
  // Automatically convert ls -m module to showModule()
  else if (str_beginswith(cmd, "ls -m "))
  {
    char *path= get_value_from_text_ex(cmd, (int)strlen(cmd), "ls\\s+\\-m\\s+(.+)", 1);

    if((path) && (path[0]))
    {
      preprocessed_cmd= g_strdup_printf("grt.showModule(\"%s\")"NL, path);
      res= MYX_GRT_SHELL_COMMAND_STATEMENT;
    }
  }
  // Automatically convert ls -m to list()
  else 
    // TODO: Parsing for the poor. What if there is more than a space char between the command and its parameter?
    if ((strcmp2(cmd, "ls -m") == 0) || (strcmp2(cmd, "dir -m") == 0))
    {
      preprocessed_cmd= g_strdup("print '\\n'.join(grt.listModules())"NL);
      res= MYX_GRT_SHELL_COMMAND_STATEMENT;
    }
  // Automatically convert ls to ls()
  else 
    if ((strcmp(cmd, "ls") == 0) || (strcmp(cmd, "dir") == 0) || str_beginswith(cmd, "ls ") || str_beginswith(cmd, "dir "))
  {
    preprocessed_cmd= g_strdup("grt.ls()"NL);
    res= MYX_GRT_SHELL_COMMAND_STATEMENT;
  }
  // Automatically convert show to show(current)
  else if (strcmp2(cmd, "show") == 0)
  {
    preprocessed_cmd= g_strdup_printf("print("MYX_SHELL_CURNODE")"NL);
    res= MYX_GRT_SHELL_COMMAND_STATEMENT;
  }
  // Automatically convert show objectname to show(getGlobal("objectname"))
  else if (str_beginswith(cmd, "show "))
  {
    char *path= get_value_from_text_ex(cmd, (int)strlen(cmd), "show\\s+(.+)", 1);

    if ((path) && (path[0]))
    {
      preprocessed_cmd= g_strdup_printf("print(grt.getGlobal(\"%s\"))"NL, path);
      res= MYX_GRT_SHELL_COMMAND_STATEMENT;
    }

    g_free(path);
  }
  g_free(cmd);
  if(cmd_param)
    g_free(cmd_param);

  //If the command is still unknown, it needs to be a Lua command
  if((res == MYX_GRT_SHELL_COMMAND_UNKNOWN) || (res == MYX_GRT_SHELL_COMMAND_STATEMENT))
  {
    if (preprocessed_cmd)
      py_shell_execute(grt, preprocessed_cmd);
    else
      py_shell_execute(grt, linebuf);
    
    if (preprocessed_cmd)
      g_free(preprocessed_cmd);
    
    return MYX_GRT_SHELL_COMMAND_STATEMENT;
  }
  
  if (preprocessed_cmd)
    g_free(preprocessed_cmd);

  return res;
}
Example #2
0
struct serviceconf* parse_conf_file (char* filepath) {

  // TODO:
  //   cur_uname = uname ();
  //   cur_uname_m = uname ("-m");
  //   archmatcher="(^$(uname) $(uname
  // -m))|(^$(uname)/)|(^/)|(^[^/]+=)|(^[^/]+$)"
  //
  // TODO: Have platform dependent interpretters.
  //
  // TODO: Split key and value, and perform ~~ (or $THIS) replacement.
  FILE* file;
  char line_storage[CONFLINE_MAX];

  struct serviceconf* sc =
      (struct serviceconf*)malloc (sizeof(struct serviceconf));
  sc->interpretter = NULL;
  sc->envs = NULL;

  struct utsname unm;
  uname (&unm);

  char* whitespacechars = " \f\n\r\t\v";
  if ((file = fopen (filepath, "r"))) {
    while (fgets (line_storage, CONFLINE_MAX, file)) {
      char* line = strndup (line_storage, strcspn (line_storage, "\n"));
      if (line[0] == '/') {
        line = line + 1;  // Move beyond the '/'.
        char* platform_specifier;
        platform_specifier = strsep (&line, "/");
        // fprintf (stderr, "specifier: %s\n", platform_specifier);
        if ((*platform_specifier) &&
            ((platform_specifier =
                  str_beginswith (platform_specifier, unm.sysname)) == NULL)) {
          // fprintf (stderr, "sysname does not match\n");
          continue;
        }
        if (*platform_specifier) {
          platform_specifier++;  // Move past delimeter.
        }
        if ((*platform_specifier) &&
            ((platform_specifier =
                  str_beginswith (platform_specifier, unm.machine)) == NULL)) {
          // fprintf (stderr, "machine does not match\n");
          continue;
        }
      }

      /* If the line starts with a '/', there must be another '/'
         somewhere after that.  Take the string between the two
         '/'s as platform specifier, and drop the line unless the
         specifier matches current platform.  Removed. */

      // printf ("%s, %s\n", line, str_beginswith (line, "#!"));

      if (sc->interpretter == NULL) {
        char* line1 = str_beginswith (line, "#!");
        if (line1) {
          sc->interpretter = str_split (
              str_replace (line1, "~", getenv ("HOME")), whitespacechars);
          fprintf (stderr, "%s\n", line);  // printout accepted line
          continue;
        }
      }

      // Skip leading whitespace, take until '#'.
      line += strspn (line, whitespacechars);
      line = strsep (&line, "#");

      // line_eff = str_replace (line, "~~", former_value_of_key);
      // Move the above to envp_dup_update_or_add.
      line = str_replace (line, "~", getenv ("HOME"));
      // not empty
      if (line && *line) {
        fprintf (stderr, "%s\n", line);  // Printout accepted line.
        sc->envs = strl_cons (line, sc->envs);
      }
    }
    fclose (file);
  }
  return sc;
}