t_chlst *ft_strsplit_nbr(char *str, int i) { int i2; t_chlst *lst; t_chlst *first; if (!(lst = new_chlst())) return (NULL); first = lst; while (str[i] != '\0') { if (i != 0) { lst->next = new_chlst(); lst = lst->next; } while (is_delim(str[i]) == 1 && str[i] != '\0') i++; i2 = i; while (is_delim(str[i]) == 0) i++; if (!(lst->str = ft_strnew(i - i2))) return (NULL); ft_strncpy(lst->str, &str[i2], i - i2); } return (first); }
int get_token(void) { int i; while (is_white(*cur_pos) && *cur_pos) cur_pos++; if (*cur_pos == NULL) { cur_token[0] = NULL; token_type = Unknown; return 0; /* end of line */ } /* check relation operator */ if (strchr("!<>=", *cur_pos)) { cur_token[0] = *cur_pos++; /* get first char */ cur_token[1] = NULL; if (*cur_pos == '=') /* ==, !=, >=, <= */ { cur_token[1] = *cur_pos++; cur_token[2] = NULL; } if (strcmp(cur_token, "=") == 0) token_type = Operator; else token_type = Rel_Op; return 1; } if (is_delim(*cur_pos)) { cur_token[0] = *cur_pos++; cur_token[1] = NULL; token_type = Unknown; return 1; } if (is_alpha(*cur_pos)) { i = 0; while (!is_delim(*cur_pos)) cur_token[i++] = *cur_pos++; cur_token[i] = NULL; if (lookup_var(cur_token) != -1) token_type = Variable; else if (lookup_func(cur_token) != -1) token_type = Function; else token_type = Unknown; return 1; } if (is_digit(*cur_pos)) { i = 0; while (is_digit(*cur_pos)) cur_token[i++] = *cur_pos++; cur_token[i] = NULL; token_type = Number; return 1; } return 0; }
static long _str_split(char *s, const char *delims, char ***out, int max) { /* TODO: get rid of str_len */ long long len = str_len(s) + 1; char esc = '\\', prev = '\0'; int parts = 0; int size = max; char **res = *out; for (long long i = 0, start = 0; i < len; ++i) { if ((is_delim(s[i], delims) && prev != esc) || (i + 1) == len) { parts++; if (max == 0 && parts > size) { size = size == 0 ? 2 : size * 2; res = mem_realloc(res, size * sizeof(char*)); } else if (max != 0 && parts > max) { parts = -1; break; } res[parts - 1] = s + start; s[i] = '\0'; start = i + 1; } prev = (prev == esc) ? '\0' : s[i]; } *out = res; return parts; }
static void write_name (char **outptr, char *endptr, const char *name_data) { char *p; const char *s; int i, length; s = name_data; length = name_data ? strlen(name_data) : 0; p = *outptr; #ifndef is_delim /* Avoid '{' and '}' for PostScript compatibility? */ #define is_delim(c) ((c) == '(' || (c) == ')' || \ (c) == '/' || \ (c) == '<' || (c) == '>' || \ (c) == '[' || (c) == ']' || \ (c) == '{' || (c) == '}' || \ (c) == '%') #endif *p++ = '/'; for (i = 0; i < length; i++) { if (s[i] < '!' || s[i] > '~' || s[i] == '#' || is_delim(s[i])) { /* ^ "space" is here. */ *p++ = '#'; sputx(s[i], &p, endptr); } else { *p++ = s[i]; } } *outptr = p; }
static void copy_token(char *dst, size_t dst_len, char const *src, size_t src_len) { assert(dst_len > 0); while (src_len > 0 && dst_len > 1 && !is_delim(*src)) { *dst++ = *src++; dst_len --; src_len --; } *dst = '\0'; }
ARG readargline(int f, ARG arg) { char c; int r; int len = 0; static char *buff = NULL; if(!arg){ return(NULL); } if(!buff){ buff = malloc(ctx->arg_max); }else{ memset(buff, 0, ctx->arg_max); } while(is_loop){ if(!(r = read(f, &c, 1))){ // EOF if(len){ break; } errno = 0; clrarg(arg); return(NULL); } if(r == -1){ if(errno == EINTR){ errno = 0; continue; } mtnlogger(mtn, 0, "[error] %s: %s\n", __func__, strerror(errno)); clrarg(arg); return(NULL); } if(c == 0 || c == '\n'){ if(!len){ continue; } break; } if(is_delim(c)){ if(len){ buff[len] = 0; arg = addarg(arg, buff); len = 0; } continue; } buff[len++] = c; } buff[len] = 0; return(is_loop ? addarg(arg, buff) : clrarg(arg)); }
char* a_strtok__get(AStrTok* Tokenizer) { const char* const string = Tokenizer->string; const char* const delims = Tokenizer->delims; const int numDelims = Tokenizer->numDelims; while(is_delim(string[Tokenizer->index], delims, numDelims)) { Tokenizer->index++; } if(string[Tokenizer->index] == '\0') { // Reset index, in case A_STRTOK_ITERATE is called again Tokenizer->index = 0; return NULL; } const int start = Tokenizer->index; do { Tokenizer->index++; } while(string[Tokenizer->index] != '\0' && !is_delim(string[Tokenizer->index], delims, numDelims)); const int len = Tokenizer->index - start; if(len > Tokenizer->currentBufferLen) { if(Tokenizer->currentToken) { free(Tokenizer->currentToken); } Tokenizer->currentToken = a_mem_malloc(len + 1); Tokenizer->currentBufferLen = len; } memcpy(Tokenizer->currentToken, &string[start], len); Tokenizer->currentToken[len] = '\0'; return Tokenizer->currentToken; }
int snmpTagValid(const char *tag, const size_t tagLen) { size_t i = 0; for (i = 0; i < tagLen; i++) { if (is_delim(tag[i])) { /* * Delimeters aren't allowed. */ return 0; } } return 1; }
char *get_word(char *str, int *start, char *delim, char *word) { int stop; stop = *start; if (str == NULL || delim == NULL) return (NULL); while ((is_delim(str[stop], delim)) == 0 && str[stop] != '\0') stop = stop + 1; if ((word = strcatrealloc(word, &str[*start], (stop - *start))) == NULL) return (NULL); if ((word = strcatrealloc(word, "/", 1)) == NULL) return (NULL); *start = stop + 1; return (word); }
/* ** Function that gets the lexical type ** of a requested element (always symbolized ** by two chars in order to fit to || or && etc. */ t_lextype get_lextype(char a, char b, char c) { t_lextype lextype; lextype = WORD; if (is_delim(b, c)) lextype = DELIM; else if (is_redirection(b, c)) lextype = REDIR; else if (is_and(b, c)) lextype = AND; else if (IS_PAR(b)) lextype = PAR; else if (IS_QUOTE(b) && a != '\\') lextype = QUOTE; else if (IS_DQUOTE(b) && a != '\\') lextype = DQUOTE; else if (IS_BQUOTE(b)) lextype = BACKQUOTE; return (lextype); }
/* * --------------------------------------------------------------------------- * read_ie * * * Arguments: * arg The command-line argument string * periodic_ie Pointer to the array to receive IE. * * Returns: * number of bytes on success, -1 if the string does not conform to expected format. * --------------------------------------------------------------------------- */ static int read_ie(char *arg, unsigned char *periodic_ie) { char *p = arg; int i, n, offset; i = 0; n = 0; offset = 0; do { n = read_byte(p, &periodic_ie[offset]); p += n; if (!is_delim(*p)) break; p++; offset ++; } while ((n > 0) && (n < UNIFI_CFG_MAX_ADDTS_IE_LENGTH)); return offset+1; } /* read_ie() */
static void docommand(char *line) { /* * look through the internal commands and determine whether or not this * command is one of them. If it is, call the command. If not, call * execute to run it as an external program. * * line - the command line of the program to run */ #ifdef FEATURE_INSTALLABLE_COMMANDS /* Duplicate the command line into such buffer in order to allow Installable Commands to alter the command line. *line cannot be modified as pipes would be destroyed. */ /* Place both buffers immediately following each other in order to make sure the contents of args can be appended to com without any buffer overflow checks. *2 -> one buffer for com and one for args +2 -> max length byte of com + cur length of com +3 -> max length byte of args + cur length of args + additional '\0' */ char buf[2*BUFFER_SIZE_MUX_AE+3+1]; #define com (buf + 1) #define args (buf + 1 + BUFFER_SIZE_MUX_AE + 2) #define BUFFER_SIZE BUFFER_SIZE_MUX_AE #else char com[MAX_INTERNAL_COMMAND_SIZE]; #define BUFFER_SIZE MAX_INTERNAL_COMMAND_SIZE #endif char *cp; char *rest; /* pointer to the rest of the command line */ struct CMD *cmdptr; assert(line); /* delete leading & trailing whitespaces */ line = trim(line); #ifdef FEATURE_INSTALLABLE_COMMANDS #if BUFFER_SIZE < MAX_INTERNAL_COMMAND_SIZE if(strlen(line) > BUFFER_SIZE) { error_line_too_long(); return; } #endif line = strcpy(args, line); #endif if (*(rest = line)) /* Anything to do ? */ { cp = com; /* Copy over 1st word as lower case */ /* Internal commands are constructed out of non-delimiter characters; ? had been parsed already */ while(*rest && !is_delim(*rest) && !strchr(QUOTE_STR, *rest)) *cp++ = toupper(*rest++); if(*rest && strchr(QUOTE_STR, *rest)) /* If the first word is quoted, it is no internal command */ cp = com; /* invalidate it */ *cp = '\0'; /* Terminate first word */ if(*com) { #ifdef FEATURE_INSTALLABLE_COMMANDS /* Check for installed COMMAND extension */ if(runExtension(com, args)) return; /* OK, executed! */ dprintf( ("[Command on return of Installable Commands check: >%s<]\n", com) ); #endif /* Scan internal command table */ for (cmdptr = cmds; cmdptr->name && strcmp(com, cmdptr->name) != 0 ; cmdptr++); } if(*com && cmdptr->name) { /* internal command found */ switch(cmdptr->flags & (CMD_SPECIAL_ALL | CMD_SPECIAL_DIR)) { case CMD_SPECIAL_ALL: /* pass everything into command */ break; case CMD_SPECIAL_DIR: /* pass '\\' & '.' too */ if(*rest == '\\' || *rest == '.') break; default: /* pass '/', ignore ',', ';' & '=' */ if(*rest == '/') break; if(!*rest || isspace(*rest)) { /* normal delimiter */ rest = ltrim(rest); break; } if(strchr(",;=", *rest)) { rest = ltrim(rest + 1); break; } /* else syntax error */ error_syntax(NULL); return; } /* JPP this will print help for any command */ if (strstr(rest, "/?")) { displayString(cmdptr->help_id); } else { dprintf(("CMD '%s' : '%s'\n", com, rest)); cmdptr->func(rest); } } else { #ifdef FEATURE_INSTALLABLE_COMMANDS if(*com) { /* external command */ /* Installable Commands are allowed to change both: "com" and "args". Therefore, we may need to reconstruct the external command line */ /* Because com and *rest are located within the very same buffer and rest is definitely terminated with '\0', the followinf memmove() operation is fully robust against buffer overflows */ memmove(com + strlen(com), rest, strlen(rest) + 1); /* Unsave, but probably more efficient operation: strcat(com, rest); -- 2000/12/10 ska*/ line = com; } #endif /* no internal command --> spawn an external one */ cp = unquote(line, rest = skip_word(line)); if(!cp) { error_out_of_memory(); return; } execute(cp, ltrim(rest)); free(cp); } } #undef line #undef com #undef args #undef BUFFER_SIZE }
void remove_leading_delimiters (TokenizerPtr tok) { while (is_delim(tok, tok->offset) && (tok->offset < strlen(tok->text))) tok->offset++; }
static void docommand(char *line) { /* * look through the internal commands and determine whether or not this * command is one of them. If it is, call the command. If not, call * execute to run it as an external program. * * line - the command line of the program to run */ char *com; /* the first word in the command */ char *cp; char *rest; /* pointer to the rest of the command line */ struct CMD *cmdptr; assert(line); /* delete leading & trailing whitespaces */ line = rest = trim(line); if (*rest) /* Anything to do ? */ { if ((cp = com = malloc(strlen(line) + 1)) == NULL) { error_out_of_memory(); return; } /* Copy over 1st word as lower case */ /* Internal commands are constructed out of alphabetic characters; ? had been parsed already */ while (isalpha(*rest)) *cp++ = tolower(*rest++); if(*rest && (!is_delim(*rest) || strchr(QUOTE_STR, *rest))) /* If the first word is quoted, it is no internal command */ cp = com; /* invalidate it */ *cp = '\0'; /* Terminate first word */ /* Scan internal command table */ if(*com) for (cmdptr = cmds; cmdptr->name && strcmp(com, cmdptr->name) != 0 ; cmdptr++); if(*com && cmdptr->name) { /* internal command found */ free(com); /* free()'ed during call */ switch(cmdptr->flags & (CMD_SPECIAL_ALL | CMD_SPECIAL_DIR)) { case CMD_SPECIAL_ALL: /* pass everything into command */ break; case CMD_SPECIAL_DIR: /* pass '\\' too */ if(*rest == '\\') break; default: /* pass '/', ignore ',', ';' & '=' */ if(*rest == '/') break; if(!*rest || isspace(*rest)) { /* normal delimiter */ rest = ltrim(rest); break; } if(strchr(",;=", *rest)) { rest = ltrim(rest + 1); break; } /* else syntax error */ error_syntax(NULL); return; } /* JPP this will print help for any command */ if (strstr(rest, "/?")) { displayString(cmdptr->help_id); } else { dprintf(("CMD '%s' : '%s'\n", com, rest)); cmdptr->func(rest); } } else { /* no internal command --> spawn an external one */ free(com); com = unquote(line, rest = skip_word(line)); if(!com) { error_out_of_memory(); return; } execute(com, ltrim(rest)); free(com); } } }
int split_quoted (const char *s, int *argc, char *argv[], int argv_sz) { char arg_buff[MAX_ARG_LEN]; /* current argument buffer */ char *ap, *ae; /* arg_buff current ptr & end-guard */ const char *c; /* current input charcter ptr */ char qc; /* current quote character */ enum states state; /* current state */ enum err_codes err; /* error end-code */ int flags; /* warning flags */ ap = &arg_buff[0]; ae = &arg_buff[MAX_ARG_LEN - 1]; c = &s[0]; state = ST_DELIM; err = ERR_OK; flags = 0; qc = SQ; /* silence compiler waring */ while ( state != ST_END ) { switch (state) { case ST_DELIM: while ( is_delim(*c) ) c++; if ( *c == SQ || *c == DQ ) { qc = *c; c++; state = ST_QUOTE; break; } if ( *c == EOS ) { state = ST_END; break; } if ( *c == BS ) { c++; if ( *c == NL ) { c++; break; } if ( *c == EOS ) { state = ST_END; err = ERR_BS_AT_EOS; break; } } /* All other cases incl. character after BS */ save(); c++; state = ST_ARG; break; case ST_QUOTE: while ( *c != qc && ( *c != BS || qc == SQ ) && *c != EOS ) { save(); c++; } if ( *c == qc ) { c++; state = ST_ARG; break; } if ( *c == BS ) { assert (qc == DQ); c++; if ( *c == NL) { c++; break; } if (*c == EOS) { state = ST_END; err = ERR_BS_AT_EOS; break; } if ( ! is_dq_escapable(*c) ) { c--; save(); c++; } save(); c++; break; } if ( *c == EOS ) { state = ST_END; err = ERR_SQ_OPEN_AT_EOS; break; } assert(0); case ST_ARG: if ( *c == SQ || *c == DQ ) { qc = *c; c++; state = ST_QUOTE; break; } if ( is_delim(*c) || *c == EOS ) { push(); state = (*c == EOS) ? ST_END : ST_DELIM; c++; break; } if ( *c == BS ) { c++; if ( *c == NL ) { c++; break; } if ( *c == EOS ) { state = ST_END; err = ERR_BS_AT_EOS; break; } } /* All other cases, incl. character after BS */ save(); c++; break; default: assert(0); } } return ( err != ERR_OK ) ? -1 : flags; }
size_t csv_parse(struct csv_parser *p, const void *s, size_t len, void (*cb1)(void *, size_t, void *), void (*cb2)(int c, void *), void *data) { unsigned const char *us = s; /* Access input data as array of unsigned char */ unsigned char c; /* The character we are currently processing */ size_t pos = 0; /* The number of characters we have processed in this call */ /* Store key fields into local variables for performance */ unsigned char delim = p->delim_char; unsigned char quote = p->quote_char; int (*is_space)(unsigned char) = p->is_space; int (*is_term)(unsigned char) = p->is_term; int (*is_delim)(unsigned char) = p->is_delim; int quoted = p->quoted; int pstate = p->pstate; size_t spaces = p->spaces; size_t entry_pos = p->entry_pos; if (!p->entry_buf && pos < len) { /* Buffer hasn't been allocated yet and len > 0 */ if (csv_increase_buffer(p) != 0) { p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; return pos; } } while (pos < len) { /* Check memory usage, increase buffer if neccessary */ if (entry_pos == ((p->options & CSV_APPEND_NULL) ? p->entry_size - 1 : p->entry_size) ) { if (csv_increase_buffer(p) != 0) { p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; return pos; } } c = us[pos++]; switch (pstate) { case ROW_NOT_BEGUN: case FIELD_NOT_BEGUN: if ((is_space ? is_space(c) : c == CSV_SPACE || c == CSV_TAB) && !(is_delim ? is_delim(c) : c == delim)) { /* Space or Tab */ continue; } else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) { /* Carriage Return or Line Feed */ if (pstate == FIELD_NOT_BEGUN) { SUBMIT_FIELD(p); SUBMIT_ROW(p, (unsigned char)c); } else { /* ROW_NOT_BEGUN */ /* Don't submit empty rows by default */ if (p->options & CSV_REPALL_NL) { SUBMIT_ROW(p, (unsigned char)c); } } continue; } else if (is_delim ? is_delim(c) : c == delim) { /* delim */ SUBMIT_FIELD(p); break; } else if (c == quote) { /* Quote */ pstate = FIELD_BEGUN; quoted = 1; } else { /* Anything else */ pstate = FIELD_BEGUN; quoted = 0; SUBMIT_CHAR(p, c); } break; case FIELD_BEGUN: if (c == quote) { /* Quote */ if (quoted) { SUBMIT_CHAR(p, c); pstate = FIELD_MIGHT_HAVE_ENDED; } else { /* STRICT ERROR - double quote inside non-quoted field */ if (p->options & CSV_STRICT) { p->status = CSV_EPARSE; p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; return pos-1; } SUBMIT_CHAR(p, c); spaces = 0; } } else if (is_delim ? is_delim(c) : c == delim) { /* delim */ if (quoted) { SUBMIT_CHAR(p, c); } else { SUBMIT_FIELD(p); } } else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) { /* Carriage Return or Line Feed */ if (!quoted) { SUBMIT_FIELD(p); SUBMIT_ROW(p, (unsigned char)c); } else { SUBMIT_CHAR(p, c); } } else if (!quoted && (is_space? is_space(c) : c == CSV_SPACE || c == CSV_TAB)) { /* Tab or space for non-quoted field */ SUBMIT_CHAR(p, c); spaces++; } else { /* Anything else */ SUBMIT_CHAR(p, c); spaces = 0; } break; case FIELD_MIGHT_HAVE_ENDED: /* This only happens when a quote character is encountered in a quoted field */ if (is_delim ? is_delim(c) : c == delim) { /* delim */ entry_pos -= spaces + 1; /* get rid of spaces and original quote */ SUBMIT_FIELD(p); } else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) { /* Carriage Return or Line Feed */ entry_pos -= spaces + 1; /* get rid of spaces and original quote */ SUBMIT_FIELD(p); SUBMIT_ROW(p, (unsigned char)c); } else if (is_space ? is_space(c) : c == CSV_SPACE || c == CSV_TAB) { /* Space or Tab */ SUBMIT_CHAR(p, c); spaces++; } else if (c == quote) { /* Quote */ if (spaces) { /* STRICT ERROR - unescaped double quote */ if (p->options & CSV_STRICT) { p->status = CSV_EPARSE; p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; return pos-1; } spaces = 0; SUBMIT_CHAR(p, c); } else { /* Two quotes in a row */ pstate = FIELD_BEGUN; } } else { /* Anything else */ /* STRICT ERROR - unescaped double quote */ if (p->options & CSV_STRICT) { p->status = CSV_EPARSE; p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; return pos-1; } pstate = FIELD_BEGUN; spaces = 0; SUBMIT_CHAR(p, c); } break; default: break; } } p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; return pos; }