Exemplo n.º 1
0
static int	word_counter(char *str, int flag)
{
  int		i;
  int		word_number;

  init_counters(&i, &word_number);
  while (str[i])
    {
      while (str[i] == ' ' || str[i] == '\t')
	i++;
      if (str[i] && str[i] != ' ' && str[i] != '\t')
	{
	  if (flag == 0)
	    word_number++;
	  while (str[i] && ((flag == 0 && str[i] != '\t' && str[i] != ' ')
			    || (flag == 1 || flag == 2 || flag == 5)))
	    {
	      if (str[i] == '\'' || str[i] == '\"' || str[i] == '`')
		flag = check_quote(flag, str[i]);
	      if (flag == 3 || flag == 4 || flag == 6)
		flag = 0;
	      i++;
	    }
	}
    }
  return (word_number);
}
Exemplo n.º 2
0
static void	copy_into_tab(char **board, char *str, int flag)
{
  int		i;
  int		j;

  init_counters(&i, &j);
  while (*str)
    {
      while (*str == ' ' || *str == '\t')
	str++;
      while (*str && ((*str != ' ' && *str != '\t' && flag == 0) || flag != 0))
	{
	  flag = check_quote(flag, *str);
	  board[i][j++] = *str;
	  str++;
	  if (flag == 3 || flag == 4 || flag == 6)
	    flag = 0;
	  if (!(*str) || ((*str == '\t' || *str == ' ') && flag == 0))
	    {
	      board[i++][j] = '\0';
	      j = 0;
	    }
	}
    }
  board[i] = NULL;
}
Exemplo n.º 3
0
int		echo(t_plist *plist, char **tab, char **env)
{
  int		j;
  int		i;
  t_list	*list;

  list = plist->begin;
  i = 1;
  j = 0;
  if (tab[i] && ((my_strcmp("-n", tab[i])) == 0))
    echo_n(tab);
  else if (tab[i] && (tab[i][0] == '$'))
    echo_valuenv(tab, list);
  else
    {
      while (tab[i])
	{
	  if (check_quote(tab, env) == -1)
	    return (error_quote());
	  custom_putstr(tab, i, j);
	  i++;
	  j = 0;
	}
      my_putchar('\n');
    }
  return (0);
}
Exemplo n.º 4
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));
}
Exemplo n.º 5
0
int		wdcnt(char *input)
{
	char	curquot;
	int		idx;
	int		wcnt;

	curquot = '\0';
	idx = 0;
	wcnt = 0;
	if (input == NULL || input[0] == '\0')
		return (0);
	while (input[idx] != '\0')
	{
		if (input[idx] == ' ' && curquot == '\0')
			wcnt++;
		if (input[idx] == curquot)
		{
			if (input[idx + 1] == ' ')
				wdcnt_setvars(&wcnt, &curquot, &idx);
		}
		curquot = check_quote(input[idx], curquot);
		idx++;
	}
	if (idx > 0)
		wcnt++;
	return (wcnt);
}
Exemplo n.º 6
0
void replace_string(char *text, FILE *file)
{
  int i,j;

  if (check_quote(text)==0)
  {
    fprintf(file,"%s",text);
  }
  else
  {
    for (i=0;text[i]!='"';i++)
    {
      fputc(text[i],file);
    }
    fputc('N',file);
    fputc('_',file);
    fputc('(',file);
    fputc(text[i],file);

    for (j=i+1;!(text[j]=='"' && (text[j-1]!='\\' || (text[j-1]=='\\' && text[j-2]=='\\')));j++)
    {
      fputc(text[j],file);
    }
    fputc('"',file);
    fputc(')',file);

    for (i=j+1;text[i]!=0;i++)
    {
      fputc(text[i],file);
    }
  }
}
Exemplo n.º 7
0
void replace_string(char *text, FILE *file)
{
    int i, j;

    if (strstr(text, " (MasC=uerade)...\"") != NULL) {
        remove_trailing_mascuerade(text);
    }

    if (strstr(text, "...\"") != NULL) {
        remove_trailing_3_dots(text);
    }

    if (strstr(text, " :\"") != NULL) {
        remove_trailing_space_colon(text);
    }

    if (strstr(text, ": \"") != NULL) {
        remove_trailing_colon_space(text);
    }

    if (strstr(text, ":\"") != NULL) {
        remove_trailing_colon(text);
    }

    if (strstr(text, "..") != NULL) {
        remove_trailing_range(text);
    }

    if (strstr(text, "\"<") != NULL && strstr(text, ">\"") != NULL) {
        remove_brackets(text);
    }

    if (check_quote(text) == 0) {
        fprintf(file, "%s", text);
        if (gettext_open_mark) {
            check_gettext_end(text);
        }
    } else {
        for (i = 0; text[i] != '"'; i++) {
            fputc(text[i], file);
        }
        fputc('N', file);
        fputc('_', file);
        fputc('(', file);
        fputc(text[i], file);

        for (j = i + 1; !(text[j] == '"' && (text[j - 1] != '\\' || (text[j - 1] == '\\' && text[j - 2] == '\\'))); j++) {
            fputc(text[j], file);
        }
        fputc('"', file);
        fputc(')', file);

        for (i = j + 1; text[i] != 0; i++) {
            fputc(text[i], file);
        }
    }
}
Exemplo n.º 8
0
t_env		*env_var_in_list(t_env *env, char *name, char *value)
{
  t_env		*new_var;
  t_env		*tmp;

  if (!(new_var = my_malloc(sizeof(t_env))))
    return (env);
  new_var->name = my_strdup(name);
  new_var->value = my_strdup(check_quote(value));
  new_var->nxt = NULL;
  if (env == NULL)
    return (new_var);
  tmp = env;
  while (tmp->nxt != NULL)
    tmp = tmp->nxt;
  tmp->nxt = new_var;
  return (env);
}
Exemplo n.º 9
0
static char *
check_quote(char *cur)
{
    if(quote)
    {
        while((cur = strchr(cur, '*')))
            if((*(++cur) == '/'))
            {
                cur++;
                quote = 0;
                break;
            }
        if(!cur)
            return cur;
    }
    while((*cur == ' ') || (*cur == '\t'))
        cur++;
    /* now we've hit something .. check for single line quotes */
    if (!*cur || *cur == '#' || *cur == '\n' ||
            (*cur == '/' && *(cur+1) == '/'))
        return NULL;
    /* check for multiple line quotes */
    if((*cur == '/') && (*(cur+1) == '*'))
    {
        cur += 2;
        quote = 1;
        while((cur = strchr(cur, '*')))
            if((*(++cur) == '/'))
            {
                cur++;
                quote = 0;
                break;
            }
        if(!cur)
            return cur;
        else
            return check_quote(cur);
    }
    return cur;
}
Exemplo n.º 10
0
int
initconf(char *filename)
{
    int lnum = 0, blnum = 0, clear = 0;
    char line[LINE_MAX];
    char *cur = NULL;
    char *tok;
    tConf *block = NULL;
    FILE *file;
    int including = 0;

    current_file = filename;

    if(!(file = fopen(filename, "r")))
    {
        if(forked)
            sendto_realops("Unable to open config file %s", filename);
        else
            printf("Unable to open config file %s\n", filename);
        return -1;
    }

    while(!BadPtr(cur) || ((fgets(line, LINE_MAX, file) != NULL) && ++lnum
                           && (cur = line)))
    {
        cur = check_quote(cur);
        if(BadPtr(cur))
            continue;

        if (including)
        {
            if (including == 1)
            {
jmp_including:
                if (*cur == '"' || *cur == '<')
                    cur++;
                tok = cur;
                while (*cur && *cur != ' ' && *cur != '\t' && *cur != '"'
                        && *cur != '>' && *cur != ';' && *cur != '\n')
                    cur++;
                if (*cur == ';')
                    including = 0;
                else
                    including++;
                *cur++ = 0;

                if (!*tok)
                {
                    confparse_error("Bad include filename", lnum);
                    fclose(file);
                    return -1;
                }

                /* parse new file */
                if(initconf(tok) == -1)
                {
                    current_file = filename;
                    confparse_error("while processing include directive",lnum);
                    fclose(file);
                    return -1;
                }

                /* reset */
                current_file = filename;

                cur = check_quote(cur);
                if (BadPtr(cur))
                    continue;
            }
            if (including == 2)
            {
                if (*cur != ';')
                {
                    confparse_error("Missing semicolon", lnum);
                    fclose(file);
                    return -1;
                }
                including = 0;
                cur++;
                cur = check_quote(cur);
                if (BadPtr(cur))
                    continue;
            }
        }

        /* now, we should be ok to get that token.. */
        if(!block)
        {
            tok = cur;
            while((*cur != ' ') && (*cur != '\n') && (*cur != '{'))
                cur++;      /* find the whitespace following the token */
            if(*cur == '{')
                clear = 1;
            *cur = '\0';
            cur++;

            if (!mycmp("INCLUDE", tok))
            {
                if(clear)
                {
                    confparse_error("Unexpected opening bracket", lnum);
                    fclose(file);
                    return -1;
                }
                including++;
                cur = check_quote(cur);
                if (BadPtr(cur))
                    continue;
                goto jmp_including; /* XXX */
            }

            for(block = tconftab; block->tok; block++)
                if(!mycmp(block->tok, tok))
                    break;
            if(!block->tok)
            {
                confparse_error("Unknown block type", lnum);
                fclose(file);
                return -1;
            }
            blnum = lnum;
        }
        cur = check_quote(cur);
        if(BadPtr(cur))
            continue;
        if((*cur ==  '{') || clear)
            cur++;
        else
        {
            confparse_error("Junk after block name", lnum);
            fclose(file);
            return -1;
        }
        if((cur = parse_block(block, cur, file, &lnum)) == NULL)
        {
            fclose(file);
            return -1;
        }
        clear = 0;
        block = NULL;
        continue;
    }
    if(clear)
    {
        confparse_error("Unexpected EOF:  Syntax error", blnum);
        fclose(file);
        return -1;
    }
    fclose(file);
    return 1;
}
Exemplo n.º 11
0
static char *
parse_block(tConf *block, char *cur, FILE *file, int *lnum)
{
    char *tok, *var, *var2;
    char line[LINE_MAX];
    tConf *b2 = NULL;
    sConf *item = NULL;
    sConf *sconftab = block->subtok;
    cVar  *vars[MAX_VALUES] = { 0 };
    int   vnum = 0, tlnum = 0, clear = 0, done = 0, skip = 0;

    if((sconftab) && (sconftab->flag == SCONFF_STRING))
    {
        /* this subtype only takes freeform variables
         * dont bother looking for tokens
         */
        int i = 0;
        while(!BadPtr(cur) || ((fgets(line, LINE_MAX, file) != NULL) &&
                               (*lnum)++ && (cur = line)))
        {
            cur = check_quote(cur);
            if(BadPtr(cur))
                continue;
            if(clear)
            {
                if(*cur != ';')
                {
                    confparse_error("Missing semicolon", *lnum);
                    free_vars(vars);
                    return NULL;
                }
                else
                    cur++;
                clear = 0;
                cur = check_quote(cur);
                if(BadPtr(cur))
                    continue;
            }
            if(done)
            {
                if(*cur != ';')
                {
                    confparse_error("Missing block end semicolon", *lnum);
                    free_vars(vars);
                    return NULL;
                }
                else
                    cur++;
                if(((*block->func) (vars, *lnum)) == -1)
                {
                    free_vars(vars);
                    return NULL;
                }
                if(BadPtr(cur))
                    *cur = '#';     /* we cant return a bad pointer because
                                     * that will pull us out of the conf read
                                     * so this will just get ignored
                                     * kludgy, but effective */
                free_vars(vars);
                return cur;
            }
            cur = check_quote(cur);
            if(BadPtr(cur))
                continue;
            if(*cur == '}')
            {
                done = 1;
                cur++;
                cur = check_quote(cur);
                if(BadPtr(cur))
                    continue;
                if(*cur != ';')
                {
                    confparse_error("Missing block end semicolon", *lnum);
                    free_vars(vars);
                    return NULL;
                }
                else
                    cur++;
                if(((*block->func) (vars, *lnum)) == -1)
                {
                    free_vars(vars);
                    return NULL;
                }
                if(BadPtr(cur))
                    *cur = '#';     /* we cant return a bad pointer because
                                     * that will pull us out of the conf read
                                     * so this will just get ignored
                                     * kludgy, but effective */
                free_vars(vars);
                return cur;
            }
            vars[vnum] = (cVar *) MyMalloc(sizeof(cVar));
            memset((char *) vars[vnum], '\0', sizeof(cVar));
            vars[vnum]->loaded = 1;
            vars[vnum]->type = NULL;
            tok = cur;
            if(*cur == '"')
            {
                i = 1;
                cur++;
            }
            var = cur;
            if(i == 1)
            {
                while(!BadPtr(cur) && (*cur != '"'))
                    cur++;
                if(BadPtr(cur))
                {
                    confparse_error("Cant find closequote", *lnum);
                    free_vars(vars);
                    return NULL;
                }
                *cur = '\0';
                cur++;
                while(!BadPtr(cur) && (*cur != ';'))
                    cur++;
            }
            else
            {
                while(!BadPtr(cur) && (*cur != ';'))
                {
                    if((*cur == ' '))
                    {
                        *cur = '\0';
                        if(vars[vnum]->loaded == 1)
                        {
                            DupString(vars[vnum]->value, var);
                            vars[vnum]->loaded = 2;
                        }
                    }
                    else if(vars[vnum]->loaded == 2)
                    {
                        confparse_error("Junk after value", *lnum);
                        free_vars(vars);
                        return NULL;
                    }
                    cur++;
                }
            }
            tlnum = *lnum;
            if(BadPtr(cur))
            {
                clear = 1;
                continue;
            }
            *cur = '\0';
            cur++;
            if(vars[vnum]->loaded == 1)
                DupString(vars[vnum]->value, var);
            vars[vnum]->loaded = 3;
            vnum++;
        }
        confparse_error("Unexpected EOF: Syntax Error", tlnum);
        free_vars(vars);
        return NULL;
    }

    while(!BadPtr(cur) || ((fgets(line, LINE_MAX, file) != NULL) && (*lnum)++
                           && (cur = line)))
    {
        cur = check_quote(cur);
        if(BadPtr(cur))
            continue;
        if(clear)
        {
            /* if we're looking for a closing semicolon, check for it first
             * if we cant find it, ignore it and hope for the best
             */
            if(*cur != ';')
            {
                confparse_error("Missing semicolon ", *lnum);
                free_vars(vars);
                return NULL;
            }
            else
                cur++;
            clear = 0;
            if(vars[vnum])
            {
                vars[vnum]->loaded = 3;
                vnum++;
            }
            item = NULL;
            cur = check_quote(cur);
            if(BadPtr(cur))
                continue;
        }
        if(done)
        {
            /* we've found the end of our block, now we're looking for the
             * closing semicolon.  if we cant find it, ignore it and
             * hope for the best
             */
            if(*cur != ';')
            {
                confparse_error("Missing block end semicolon", *lnum);
                free_vars(vars);
                return NULL;
            }
            else
                cur++;
            if(((*block->func) (vars, *lnum)) == -1)
            {
                free_vars(vars);
                return NULL;
            }
            if(BadPtr(cur))
                *cur = '#';     /* we cant return a bad pointer because
                                 * that will pull us out of the conf read
                                 * so this will just get ignored
                                 * kludgy, but effective */
            free_vars(vars);
            return cur;
        }
        if(b2 && b2->tok)
        {
            /* we've identified a nested block in a previous loop.
             * we didnt get an openquote yet, so look for that.
             * we must find this.  keep looking til we do.
             */
            if(*cur != '{')
            {
                confparse_error("Junk after nested block token", *lnum);
                free_vars(vars);
                return NULL;
            }
            cur++;
            cur = check_quote(cur);
            cur = parse_block(b2, cur, file, lnum);
            b2 = NULL;
            continue;
        }
        if(!item || !item->tok)
        {
            /* if we dont already have a specific token we're working on
             * find one here.
             */
            cur = check_quote(cur);
            if(BadPtr(cur))
                continue;
            tok = cur;
            tlnum = *lnum;
            if(*cur == '}')
            {
                /* if we've got a closebracket, then we've hit the end
                 * of our block.
                 */
                done = 1;
                cur++;
                cur = check_quote(cur);
                if(BadPtr(cur))
                    continue;
                if(*cur != ';')
                {
                    confparse_error("Missing block end semicolon", *lnum);
                    free_vars(vars);
                    return NULL;
                }
                else
                    cur++;
                if(((*block->func) (vars, *lnum)) == -1)
                {
                    free_vars(vars);
                    return NULL;
                }
                if(BadPtr(cur))
                    *cur = '#';     /* we cant return a bad pointer because
                                     * that will pull us out of the conf read
                                     * so this will just get ignored
                                     * kludgy, but effective */
                free_vars(vars);
                return cur;

            }
            /* our token ends where whitespace or a semicolon begins */
            while(!BadPtr(cur) && ((*cur != ' ') && (*cur != ';') &&
                                   (*cur != '\t') && (*cur != '\n')))
                cur++;
            if(BadPtr(cur))
            {
                confparse_error("Unterminated token", *lnum);
                free_vars(vars);
                return NULL;
            }
            else
            {
                if(*cur == ';')
                    skip = 1;
                *cur = '\0';
            }
            cur++;
            if(block->nest)
            {
                /* we allow nested stuff inside here, so check for it. */
                for(b2 = tconftab; b2->tok; b2++)
                    if(!mycmp(b2->tok, tok))
                        break;
                if(b2 && b2->tok)
                    if(!(block->nest & b2->flag))
                        b2 = NULL;
                if(b2 && b2->tok)
                {
                    /* recurse through the block we found */
                    tlnum = *lnum;
                    cur = check_quote(cur);
                    if(BadPtr(cur))
                        continue;
                    if(*cur != '{')
                    {
                        confparse_error("Junk after nested block name", *lnum);
                        free_vars(vars);
                        return NULL;
                    }
                    cur++;
                    cur = check_quote(cur);
                    cur = parse_block(b2, cur, file, lnum);
                    if(!cur)
                    {
                        free_vars(vars);
                        return NULL;
                    }
                    b2 = NULL;
                    continue;
                }
            }
            /* find our token */
            for(item = sconftab; item && item->tok; item++)
                if(!mycmp(item->tok, tok))
                    break;
            if(!item->tok)
            {
                confparse_error("Unknown token", *lnum);
                free_vars(vars);
                return NULL;
            }
            /* create our variable */
            vars[vnum] = (cVar *) MyMalloc(sizeof(cVar));
            memset((char *) vars[vnum], '\0', sizeof(cVar));
            vars[vnum]->type = item;
            vars[vnum]->loaded = 1;
        }
        if(item->var & VARTYPE_NONE)
        {
            /* we dont need to grab a variable for this type
             * just look for the closing semicolon, and move on */
            vars[vnum]->loaded = 2;
            if(!skip)
            {
                /* we've already gotten our semicolon back
                 * at the end of our token.  dont look for it. */
                cur = check_quote(cur);
                while(!BadPtr(cur) && (*cur != ';'))
                    cur++;
                if(BadPtr(cur))
                {
                    clear = 1;
                    continue;
                }
                cur++;
            }
            skip = 0;
            vars[vnum]->loaded = 3;
            vnum++;
            item = NULL;
            continue;
        }
        if(item->var & VARTYPE_STRING)
        {
            /* we're looking for a string here, so we require
             * quotes around the string...
             */
            cur = check_quote(cur);
            while(!BadPtr(cur) && (*cur != '"'))
                cur++;
            if(BadPtr(cur))
                continue;
            cur++;
            var = cur;
            while(!BadPtr(cur) && (*cur != '"'))
                cur++;
            if(BadPtr(cur))
            {
                confparse_error("Unterminated quote", *lnum);
                free_vars(vars);
                return NULL;
            }
            *cur = '\0';
            cur++;
            DupString(vars[vnum]->value, var);
            vars[vnum]->loaded = 2;
            while(!BadPtr(cur) && (*cur != ';'))
                cur++;
            if(BadPtr(cur))
            {
                clear = 1;
                continue;
            }
            cur++;
            vars[vnum]->loaded = 3;
            vnum++;
            item = NULL;
            continue;
        }
        if(item->var & VARTYPE_INT)
        {
            cur = check_quote(cur);
            var = cur;
            while(!BadPtr(cur) && ((*cur != ';') && (*cur != '\t') &&
                                   (*cur != '\n') && (*cur != ' ')))
                cur++;
            if(BadPtr(cur))
            {
                clear = 1;
                continue;
            }
            if(*cur != ';')
                clear = 1;
            *cur = '\0';
            cur++;
            var2 = var;
            while(*var)
            {
                if(IsDigit(*var))
                    var++;
                else
                {
                    confparse_error("Expecting integer value", *lnum);
                    free_vars(vars);
                    return NULL;
                }
            }
            if(!item)
                continue;
            var = var2;
            DupString(vars[vnum]->value, var);
            vars[vnum]->loaded = 3;
            vnum++;
            item = NULL;
            continue;
        }
        if(item->var & VARTYPE_NAME)
        {
            cur = check_quote(cur);
            if(!BadPtr(cur) && (*cur == '"'))
                cur++;
            var = cur;
            while(!BadPtr(cur) && (*cur != ';'))
            {
                if((*cur == ' ') || (*cur == '"') || (*cur == '\t'))
                {
                    *cur = '\0';
                    if(vars[vnum]->loaded == 1)
                    {
                        DupString(vars[vnum]->value, var);
                        vars[vnum]->loaded = 2;
                    }
                }
                cur++;
            }
            if(BadPtr(cur))
            {
                clear = 1;
                continue;
            }
            *cur = '\0';
            cur++;
            if(vars[vnum]->loaded == 1)
                DupString(vars[vnum]->value, var);
            vars[vnum]->loaded = 3;
            vnum++;
            item = NULL;
            continue;
        }
        confparse_error("Unexpected EOF:  Syntax Error", tlnum);
        free_vars(vars);
        return NULL;
    }
    confparse_error("Unexpected EOF:  Syntax Error", tlnum);
    free_vars(vars);
    return NULL;
}