Ejemplo n.º 1
0
void escape_string(char *old_str, int *plen)
{
	const int old_len = *plen;
	char *new_str = umalloc(*plen);
	int new_i, i;

	/* "parse" into another string */

	for(i = new_i = 0; i < old_len; i++){
		char add;

		if(old_str[i] == '\\'){
			int len;

			i++;

			if(escape_multi_char(old_str + i, &add, &len)){
				i += len - 1;
			}else{
				add = escape_char(old_str[i]);

				if(add == -1)
					DIE_AT(NULL, "unknown escape char '\\%c'", add);
			}
		}else{
			add = old_str[i];
		}

		new_str[new_i++] = add;
	}

	memcpy(old_str, new_str, new_i);
	*plen = new_i;
	free(new_str);
}
Ejemplo n.º 2
0
static size_t escape_markup(char dst[], const char src[], size_t length)
{
    size_t      extra = 0;
    const char* stop = src + length;

    while (src < stop)
    {
        char        orig[2];
        const char* seq = escape_char(*src, orig);
        size_t      seqSize = strlen(seq);

        if (dst)
        {
            memcpy(dst, seq, seqSize);
            dst += seqSize;
        }

        // now record the extra size needed
        extra += seqSize - 1;   // minus one to subtract the original char

        // bump to the next src char
        src += 1;
    }
    return extra;
}
Ejemplo n.º 3
0
static int	fill_prompt(char *prompt, char *str)
{
  int	i;
  int	k;
  int	match;

  i = -1;
  k = -1;
  match = 0;
  if (str == NULL || prompt == NULL)
    return (EXIT_FAILURE);
  while (str[++k] && match < 2)
    {
      if (str[k] == '\\' && (str[k + 1] >= '0' && str[k + 1] <= '7'))
	{
	  prompt[++i] = magic_number(&str[k + 1]);
	  while (str[k] && (str[k + 1] >= '0' && str[k + 1] <= '7'))
	    ++k;
	}
      else if (str[k] == '\\' && str[k + 1] != '\0')
	prompt[++i] = escape_char(str[++k]);
      else if (str[k] != PROMPT_CHAR)
	prompt[++i] = str[k];
      else
	++match;
    }
  return (check_quote(match));
}
Ejemplo n.º 4
0
int	fill_prompt(char *prompt, char *str)
{
    int	i;
    int	k;
    int	match;

    i = -1;
    k = -1;
    match = 0;
    while (str[++k] && match < 2)
    {
        if (str[k] == '\\' && (str[k + 1] >= '0' && str[k + 1] <= '7'))
        {
            prompt[++i] = magic_number(&str[k + 1]);
            while (str[k] && (str[k + 1] >= '0' && str[k + 1] <= '7'))
                ++k;
        }
        else if (str[k] == '\\' && str[k + 1] != '\0')
            prompt[++i] = escape_char(str[++k]);
        else if (str[k] != PROMPT_CHAR)
            prompt[++i] = str[k];
        else
            ++match;
    }
    if ((match % 2) == 1)
    {
        fprintf(stderr, "Unmatched \".\n");
        return (EXIT_FAILURE);
    }
    return (EXIT_SUCCESS);
}
Ejemplo n.º 5
0
int main(int argc, char *argv[]) {
	int temp;
	char *ostring;
	int method;

	if ( *argv[1] == '1') {
		method = 0;
	} else if ( *argv[1] == '2') {
		method = 1;
	} else {
		printf("This argument is not supported");
		return 0;
	}

	printf("This has %d arguments  \n",argc);
	
	for (temp = 2; temp < argc; temp++) {

		ostring = (char *) malloc(strlen(argv[temp]) + 25);
		if (method) {
			print_escape(argv[temp],ostring);
		} else {
			escape_char(argv[temp],ostring);
		}

		printf("%s ",ostring);
		
		free(ostring);

	}
	printf("\n");
}
Ejemplo n.º 6
0
Archivo: parser.c Proyecto: Fusiow/msh
t_command			*quick_parse(char *str)
{
	t_parse		*list;

	if (!(str = check_line_cmd(str)))
		return (NULL);
	str = escape_char(str);
	list = tokenize(str);
	return (tab_contain(list, 0));
}
Ejemplo n.º 7
0
int classify_escape_char(       // CLASSIFY TYPE OF ESCAPE
    int chr )                   // - character after "\"
{
    int     retn;               // - classification returned

    if( octal_dig( chr ) != 8 ) {
        retn = ESCAPE_OCTAL;
    } else if( chr == 'x' ) {
        retn = ESCAPE_HEX;
    } else {
        retn = escape_char( chr );
    }
    return( retn );
}
Ejemplo n.º 8
0
static void write_string(struct json_builder *json, const char* str)
{
    int char_count = 0;
    const char* s = str;
    while (*s) {
        char_count++;
        if (should_escape_char(*s))
            char_count++;
        s++;
    }
    ensure_room_for(json, char_count+2);
    *json->write++ = '\"';
    while(*str) {
        if (should_escape_char(*str)) {
            *json->write++ = '\\';
            *json->write++ = escape_char(*str++);
        } else
            *json->write++ = *str++;
    }
    *json->write++ = '\"';
}
Ejemplo n.º 9
0
int parse_db(struct _asm_context *asm_context, int null_term_flag)
{
char token[TOKENLEN];
int token_type;
int data32;

  if (asm_context->segment==SEGMENT_BSS)
  {
    printf("Error: .bss segment doesn't support initialized data at %s:%d\n", asm_context->filename, asm_context->line);
    return -1;
  }

  while(1)
  {
    token_type=get_token(asm_context, token, TOKENLEN);
    if (token_type==TOKEN_EOL || token_type==TOKEN_EOF) break;

    if (token_type==TOKEN_QUOTED)
    {
      unsigned char *s=(unsigned char *)token;
      while(*s!=0)
      {
        if (*s=='\\')
        {
          int e=escape_char(asm_context, s);
          if (e==0)
          {
            return -1;
          }
          s=s+e;
        }

        memory_write_inc(asm_context, *s, DL_DATA);

        asm_context->data_count++;
        s++;
      }

      if (null_term_flag==1)
      {
        memory_write_inc(asm_context, 0, DL_DATA);
        asm_context->data_count++;
      }
    }
      else
    {
      pushback(asm_context, token, token_type);
      if (eval_expression(asm_context, &data32)==-1)
      {
        eat_operand(asm_context);
      }

      memory_write_inc(asm_context, (unsigned char)data32, DL_DATA);
      asm_context->data_count++;
    }

    token_type=get_token(asm_context, token, TOKENLEN);
    if (token_type==TOKEN_EOL || token_type==TOKEN_EOF) break;

    if (IS_NOT_TOKEN(token,','))
    {
      printf("Parse error: expecting a ',' on line %d.\n", asm_context->line);
      return -1;
    }
  }

  asm_context->line++;

  return 0;
}
Ejemplo n.º 10
0
int
sd_plugin_main (void **data)
{
    int i, k, argc;
    char **argv = NULL;
    void *tmp = data[0];
    command **tmpc = (command **)tmp;
    command *cmd = *tmpc;

    if (cmd->argc == 0)
        return 1;

    /* check if we are the first parser */
    unsigned int first = (cmd->argcf == 0);
    if (first)
    {
        cmd->argvf = xcalloc (cmd->argc, sizeof (char *));
        cmd->argcf = cmd->argc;
    }
    else
    {
        /* if not, copy the args because we are gonna change it a lot */
        argv = xcalloc (cmd->argcf, sizeof (char *));
        argc = cmd->argcf;
        for (i = 0; i < argc; i++)
        {
            argv[i] = xstrdup (cmd->argvf[i]);
        }
    }

    for (i = 0, k = 0; i < (first ? (cmd->argc) : (argc)); i++)
    {
        char *temp = (first ? cmd->argv[i] : argv[i]);

        escape_char (NULL, '\'');

        if (/*!(cmd->protected[i] & SINGLE_QUOTE) &&*/
            strpbrk (temp,"*?[]$`") != NULL)
        {
            wordexp_t p;
            int j;
            int r = wordexp (temp, &p, 0);
            if (r != 0)
            {
                fprintf (stderr, "shelldone: syntax error near '%s'\n",
                                 temp);

                if (!first)
                {
                    int z;
                    for (z = 0; z < argc; z++)
                        xfree (argv[z]);
                    xfree (argv);
                }

                return -1;
            }
            if (p.we_wordc == 0 || (p.we_wordc == 1 &&
                xstrcmp (p.we_wordv[0],temp) == 0))
            {
                /*
                fprintf (stderr, "shelldone: '%s' => '%s', k: %d %d\n",
                                 temp,
                                 cmd->argvf[k],
                                 k,
                                 cmd->argcf);
                */
                if (r == 0)
                    wordfree (&p);

                if (cmd->argvf[k] != NULL)
                    xfree (cmd->argvf[k]);

                cmd->argvf[k] = xmalloc (1 * sizeof (char));
                *(cmd->argvf[k]) = '\0';

                k++;

/*                return -1;*/
            }
            else if (r == 0)
            {
                int argcf = cmd->argcf;
                if (p.we_wordc > 1)
                {
                    cmd->argcf += p.we_wordc - 1;
                    cmd->argvf = xrealloc (cmd->argvf,cmd->argcf * sizeof(char *));
                }

                for (j = 0; j < (int) p.we_wordc; j++)
                {
                    char *t = xstrdup (p.we_wordv[j]);
                    if (k+j < argcf && cmd->argvf[k+j] != NULL)
                        xfree (cmd->argvf[k+j]);
                    cmd->argvf[k+j] = t;
                }
                k += j;
                wordfree (&p);
            }
        }
        else
        {
            if (first)
            {
                if (cmd->argvf[k] != NULL)
                    xfree (cmd->argvf[k]);
                cmd->argvf[k] = xstrdup (cmd->argv[i]);
            }
            k++;
        }
    }
    if (!first)
    {
        for (i = 0; i < argc; i++)
            xfree (argv[i]);
        xfree (argv);
    }
    if (first)
        cmd->argcf = k;

    return 1;
}