Beispiel #1
0
static void
collect_arguments (symbol *sym, struct obstack *argptr,
                   struct obstack *arguments)
{
  token_data td;
  token_data *tdp;
  bool more_args;
  bool groks_macro_args = SYMBOL_MACRO_ARGS (sym);

  TOKEN_DATA_TYPE (&td) = TOKEN_TEXT;
  TOKEN_DATA_TEXT (&td) = SYMBOL_NAME (sym);
  tdp = (token_data *) obstack_copy (arguments, &td, sizeof td);
  obstack_ptr_grow (argptr, tdp);

  if (peek_token () == TOKEN_OPEN)
    {
      next_token (&td, NULL); /* gobble parenthesis */
      do
        {
          more_args = expand_argument (arguments, &td);

          if (!groks_macro_args && TOKEN_DATA_TYPE (&td) == TOKEN_FUNC)
            {
              TOKEN_DATA_TYPE (&td) = TOKEN_TEXT;
              TOKEN_DATA_TEXT (&td) = (char *) "";
            }
          tdp = (token_data *) obstack_copy (arguments, &td, sizeof td);
          obstack_ptr_grow (argptr, tdp);
        }
      while (more_args);
    }
}
static void expand_diversion(char * filename)
{
  struct _stat stat;
  int fd;
  char * buf, * endbuf, * p, * q, * s;
  int inquote;

  if (_stat(filename, &stat) == -1 ||
      (fd = _open(filename, O_RDONLY | O_BINARY, 0)) == -1) {
    fprintf(stderr, "Cannot open file %s\n", filename);
    exit(2);
  }
  buf = (char *) malloc(stat.st_size + 1);
  if (buf == NULL) out_of_memory();
  _read(fd, buf, stat.st_size);
  endbuf = buf + stat.st_size;
  _close(fd);
  for (p = buf; p < endbuf; /*nothing*/) {
    /* Skip leading blanks */
    while (p < endbuf && isspace(*p)) p++;
    if (p >= endbuf) break;
    s = p;
    /* Skip to end of argument, taking quotes into account */
    q = s;
    inquote = 0;
    while (p < endbuf) {
      if (! inquote) {
        if (isspace(*p)) break;
        if (*p == '"') { inquote = 1; p++; continue; }
        *q++ = *p++;
      } else {
        switch (*p) {
          case '"':
            inquote = 0; p++; continue;
          case '\\':
            if (p + 4 <= endbuf && strncmp(p, "\\\\\\\"", 4) == 0) {
              p += 4; *q++ = '\\'; *q++ = '"'; continue;
            }
            if (p + 3 <= endbuf && strncmp(p, "\\\\\"", 3) == 0) {
              p += 3; *q++ = '\\'; inquote = 0; continue;
            }
            if (p + 2 <= endbuf && p[1] == '"') {
              p += 2; *q++ = '"'; continue;
            }
            /* fallthrough */
        default:
          *q++ = *p++;
        }
      }
    }
    /* Delimit argument and expand it */
    *q++ = 0;
    expand_argument(s);
    p++;
  }
}
Beispiel #3
0
CAMLexport void caml_expand_command_line(int * argcp, char *** argvp)
{
    int i;
    argc = 0;
    argvsize = 16;
    argv = (char **) malloc(argvsize * sizeof(char *));
    if (argv == NULL) out_of_memory();
    for (i = 0; i < *argcp; i++) expand_argument((*argvp)[i]);
    argv[argc] = NULL;
    *argcp = argc;
    *argvp = argv;
}
Beispiel #4
0
/* Scan STRING for variable references and expansion-function calls.  Only
   LENGTH bytes of STRING are actually scanned.  If LENGTH is -1, scan until
   a null byte is found.

   Write the results to LINE, which must point into `variable_buffer'.  If
   LINE is NULL, start at the beginning of the buffer.
   Return a pointer to LINE, or to the beginning of the buffer if LINE is
   NULL.
 */
char *
variable_expand_string (char *line, const char *string, long length)
{
  struct variable *v;
  const char *p, *p1;
  char *abuf = NULL;
  char *o;
  unsigned int line_offset;

  if (!line)
    line = initialize_variable_output();
  o = line;
  line_offset = line - variable_buffer;

  if (length == 0)
    {
      variable_buffer_output (o, "", 1);
      return (variable_buffer);
    }

  /* If we want a subset of the string, allocate a temporary buffer for it.
     Most of the functions we use here don't work with length limits.  */
  if (length > 0 && string[length] != '\0')
    {
      abuf = xmalloc(length+1);
      memcpy(abuf, string, length);
      abuf[length] = '\0';
      string = abuf;
    }
  p = string;

  while (1)
    {
      /* Copy all following uninteresting chars all at once to the
         variable output buffer, and skip them.  Uninteresting chars end
	 at the next $ or the end of the input.  */

      p1 = strchr (p, '$');

      o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);

      if (p1 == 0)
	break;
      p = p1 + 1;

      /* Dispatch on the char that follows the $.  */

      switch (*p)
	{
	case '$':
	  /* $$ seen means output one $ to the variable output buffer.  */
	  o = variable_buffer_output (o, p, 1);
	  break;

	case '(':
	case '{':
	  /* $(...) or ${...} is the general case of substitution.  */
	  {
	    char openparen = *p;
	    char closeparen = (openparen == '(') ? ')' : '}';
            const char *begp;
	    const char *beg = p + 1;
	    char *op;
            char *abeg = NULL;
	    const char *end, *colon;

	    op = o;
	    begp = p;
	    if (handle_function (&op, &begp))
	      {
		o = op;
		p = begp;
		break;
	      }

	    /* Is there a variable reference inside the parens or braces?
	       If so, expand it before expanding the entire reference.  */

	    end = strchr (beg, closeparen);
	    if (end == 0)
              /* Unterminated variable reference.  */
              fatal (*expanding_var, _("unterminated variable reference"));
	    p1 = lindex (beg, end, '$');
	    if (p1 != 0)
	      {
		/* BEG now points past the opening paren or brace.
		   Count parens or braces until it is matched.  */
		int count = 0;
		for (p = beg; *p != '\0'; ++p)
		  {
		    if (*p == openparen)
		      ++count;
		    else if (*p == closeparen && --count < 0)
		      break;
		  }
		/* If COUNT is >= 0, there were unmatched opening parens
		   or braces, so we go to the simple case of a variable name
		   such as `$($(a)'.  */
		if (count < 0)
		  {
		    abeg = expand_argument (beg, p); /* Expand the name.  */
		    beg = abeg;
		    end = strchr (beg, '\0');
		  }
	      }
	    else
	      /* Advance P to the end of this reference.  After we are
                 finished expanding this one, P will be incremented to
                 continue the scan.  */
	      p = end;

	    /* This is not a reference to a built-in function and
	       any variable references inside are now expanded.
	       Is the resultant text a substitution reference?  */

	    colon = lindex (beg, end, ':');
	    if (colon)
	      {
		/* This looks like a substitution reference: $(FOO:A=B).  */
		const char *subst_beg, *subst_end, *replace_beg, *replace_end;

		subst_beg = colon + 1;
		subst_end = lindex (subst_beg, end, '=');
		if (subst_end == 0)
		  /* There is no = in sight.  Punt on the substitution
		     reference and treat this as a variable name containing
		     a colon, in the code below.  */
		  colon = 0;
		else
		  {
		    replace_beg = subst_end + 1;
		    replace_end = end;

		    /* Extract the variable name before the colon
		       and look up that variable.  */
		    v = lookup_variable (beg, colon - beg);
		    if (v == 0)
		      warn_undefined (beg, colon - beg);

                    /* If the variable is not empty, perform the
                       substitution.  */
		    if (v != 0 && *v->value != '\0')
		      {
			char *pattern, *replace, *ppercent, *rpercent;
			char *value = (v->recursive
                                       ? recursively_expand (v)
				       : v->value);

                        /* Copy the pattern and the replacement.  Add in an
                           extra % at the beginning to use in case there
                           isn't one in the pattern.  */
                        pattern = alloca (subst_end - subst_beg + 2);
                        *(pattern++) = '%';
                        memcpy (pattern, subst_beg, subst_end - subst_beg);
                        pattern[subst_end - subst_beg] = '\0';

                        replace = alloca (replace_end - replace_beg + 2);
                        *(replace++) = '%';
                        memcpy (replace, replace_beg,
                               replace_end - replace_beg);
                        replace[replace_end - replace_beg] = '\0';

                        /* Look for %.  Set the percent pointers properly
                           based on whether we find one or not.  */
			ppercent = find_percent (pattern);
			if (ppercent)
                          {
                            ++ppercent;
                            rpercent = find_percent (replace);
                            if (rpercent)
                              ++rpercent;
                          }
			else
                          {
                            ppercent = pattern;
                            rpercent = replace;
                            --pattern;
                            --replace;
                          }

                        o = patsubst_expand_pat (o, value, pattern, replace,
                                                 ppercent, rpercent);

			if (v->recursive)
			  free (value);
		      }
		  }
	      }

	    if (colon == 0)
	      /* This is an ordinary variable reference.
		 Look up the value of the variable.  */
		o = reference_variable (o, beg, end - beg);

	  if (abeg)
	    free (abeg);
	  }
	  break;

	case '\0':
	  break;

	default:
	  if (isblank ((unsigned char)p[-1]))
	    break;

	  /* A $ followed by a random char is a variable reference:
	     $a is equivalent to $(a).  */
          o = reference_variable (o, p, 1);

	  break;
	}

      if (*p == '\0')
	break;

      ++p;
    }

  if (abuf)
    free (abuf);

  variable_buffer_output (o, "", 1);
  return (variable_buffer + line_offset);
}
SValue SyntaxTree::Expand(const sptr<ICommand>& shell, const SString& expand, bool* command_expansion)
{
	return expand_argument(shell, expand, command_expansion);
}
/* RETURN VALUE: 0: success; -1: failure */
int
read_default (void)
{
  static const char sep[] = " \t";
  FILE *fp;
  char data[EXPAND_PATH_LENGTH +1];
  char code[EXPAND_PATH_LENGTH +1];
  char param[EXPAND_PATH_LENGTH +1];
  char *word;
  int i, num, *v[17];

  strcpy (jserver_dir, JSERVER_DIR);

  if ((fp = fopen (jserverrcfile, "r")) == NULL)
    {
      log_err ("can't open %s\n", jserverrcfile);
      return (-1);
    }

  /* copy the pointer for looping */
  v[0]  = &default_para.n;
  v[1]  = &default_para.nsho;
  v[2]  = &default_para.p1;
  v[3]  = &default_para.p2;
  v[4]  = &default_para.p3;
  v[5]  = &default_para.p4;
  v[6]  = &default_para.p5;
  v[7]  = &default_para.p6;
  v[8]  = &default_para.p7;
  v[9]  = &default_para.p8;
  v[10] = &default_para.p9;
  v[11] = &default_para.p10;
  v[12] = &default_para.p11;
  v[13] = &default_para.p12;
  v[14] = &default_para.p13;
  v[15] = &default_para.p14;
  v[16] = &default_para.p15;

  while (fgets (data, EXPAND_PATH_LENGTH, fp) != NULL)
    {
      num = sscanf (data, "%s", code);
      if (num <= 0)
	{
	  /* no command on the line */
	  continue;
	}
      code[EXPAND_PATH_LENGTH] = '\0';
      if (code[0] == ';')
	{
	  /* the line is commented out */
	  continue;
	}

/*
        if(strcmp(code, "jt_len") == 0){
            jt_len = atoi(s[0]);
        }else if(strcmp(code, "hjt_len") == 0){
            hjt_len = atoi(s[0]);
        }
*/

      if (strcmp (code, "max_client") == 0)
        {
          num = sscanf (data, "%s %d ", code, &max_client);
	  if (num != 2)
	    {
	      log_err ("command %s invalid.", code);
	      continue;
	    }
          log_debug ("max_client=%d", max_client);
        }
      else if (strcmp (code, "max_sticky_env") == 0)
        {
          num = sscanf (data, "%s %d ", code, &max_sticky_env);
	  if (num != 2)
	    {
	      log_err ("command %s invalid.", code);
	      continue;
	    }
          log_debug ("max_sticky_env=%d", max_sticky_env);
        }
      else if (strcmp ((code + 1), "server_dir") == 0)
        {
	  num = sscanf (data , "%s %s", code, param);
	  if (num != 2)
	    {
	      log_err ("command %s invalid.", code);
	      continue;
	    }
          if (expand_expr (param) != 0)
            {
              log_err ("command %s: can't expand %s\n", code, param);
            }
          strcpy (jserver_dir, param);
	  log_debug ("jserver_dir=%s", jserver_dir);
        }
      else if (strcmp (code, "def_param") == 0)
        {
	  word = strtok (data, sep); /* discard first word "def_param" */
	  for (i = 0; (i <= 16); i++) {
	    word = strtok (NULL, sep);
	    if (word == NULL)
	      {
		log_err ("command %s has only %d parameters.", code, i);
	        return (-1);
	      }
	    *v[i] = atoi (word); /* XXX: default to 0 if error */
	  }
	  log_debug ("command %s has %d parameters", code, i);
        }
#ifndef CHINESE
      /* else if (strcmp (code, "set_giji_eisuu") == 0 && num >= 2) */
      else if (strcmp (code, "set_giji_eisuu") == 0)
        {
	  word = strtok (data, sep); /* discard first word "set_giji_eisuu" */
	  for (i = 0; (word || i < 20); i++)
	    {
	      word = strtok (NULL, sep);
	      if (word == NULL)
		{
		  break;
		}
              giji_eisuu[i] = expand_argument (word);
	    }
	  log_debug ("command %s has %d parameters.", code, i);
          for (; i < 20; i++)
            {
              giji_eisuu[i] = 0xffff;
            }
        }
#endif
    }
  fclose (fp);
  return (0);
}