static int parse_message_header(http_parser_t *parser) { int field_num; http_header_field_t *field; field_num = add_field(parser); field = &parser->req.fields[field_num]; field->valid = 0; parse_crlf(parser); field->name = parse_token(parser); if (!field) return 1; if (parse_char(parser, ':')) { free(field->name); return 1; } // optional while (parse_char(parser, ' ') == 0) ; field->value = parse_field_content(parser); if (!field->value) return 1; while (parse_char(parser, ' ') == 0) ; field->valid = 1; return 0; }
/* Syntax: array ( type, N|argN ) */ static int parse_array(struct protolib *plib, struct locus *loc, char **str, struct arg_type_info *info) { eat_spaces(str); if (parse_char(loc, str, '(') < 0) return -1; eat_spaces(str); int own; struct arg_type_info *elt_info = parse_lens(plib, loc, str, NULL, 0, &own, NULL); if (elt_info == NULL) return -1; eat_spaces(str); parse_char(loc, str, ','); eat_spaces(str); int own_length; struct expr_node *length = parse_argnum(loc, str, &own_length, 0); if (length == NULL) { if (own) { type_destroy(elt_info); free(elt_info); } return -1; } type_init_array(info, elt_info, own, length, own_length); eat_spaces(str); parse_char(loc, str, ')'); return 0; }
static int parse_crlf(http_parser_t *parser) { if (parse_char(parser, '\r')) return 1; if (parse_char(parser, '\n')) return 1; return 0; }
static struct arg_type_info * parse_lens(struct protolib *plib, struct locus *loc, char **str, struct param **extra_param, size_t param_num, int *ownp, int *forwardp) { int own_lens; struct lens *lens = name2lens(str, &own_lens); int has_args = 1; struct arg_type_info *info; if (lens != NULL) { eat_spaces(str); /* Octal lens gets special treatment, because of * backward compatibility. */ if (lens == &octal_lens && **str != '(') { has_args = 0; info = type_get_simple(ARGTYPE_INT); *ownp = 0; } else if (parse_char(loc, str, '(') < 0) { report_error(loc->filename, loc->line_no, "expected type argument after the lens"); return NULL; } } if (has_args) { eat_spaces(str); info = parse_type(plib, loc, str, extra_param, param_num, ownp, forwardp); if (info == NULL) { fail: if (own_lens && lens != NULL) lens_destroy(lens); return NULL; } } if (lens != NULL && has_args) { eat_spaces(str); parse_char(loc, str, ')'); } /* We can't modify shared types. Make a copy if we have a * lens. */ if (lens != NULL && unshare_type_info(loc, &info, ownp) < 0) goto fail; if (lens != NULL) { info->lens = lens; info->own_lens = own_lens; } return info; }
static bool parse_mulExpr(const char *&str, double &r) { if (!parse_factor(str, r)) return false; double rv; for (;;) { if (parse_char(str, '*') && parse_factor(str, rv)) { r *= rv; } else if (parse_char(str, '/') && parse_factor(str, rv)) { r /= rv; } else return true; } }
static bool parse_addExpr(const char *&str, double &r) { if (!parse_mulExpr(str, r)) return false; double rv; for (;;) { if (parse_char(str, '+') && parse_mulExpr(str, rv)) { r += rv; } else if (parse_char(str, '-') && parse_mulExpr(str, rv)) { r -= rv; } else return true; } }
// Request-URI = "*" | absoluteURI | abs_path | authority const char* parse_request_uri(unsigned char** p) { if (!parse_char(p, '*')) return NULL; if (!parse_httpurl(p)) return NULL; if (!parse_char(p, '/')) return parse_hpath(p); return ERR; }
static bool parse_feature_value_prefix (const char **pp, const char *end, hb_feature_t *feature) { if (parse_char (pp, end, '-')) feature->value = 0; else { parse_char (pp, end, '+'); feature->value = 1; } return true; }
/* Syntax: struct ( type,type,type,... ) */ static int parse_struct(struct protolib *plib, struct locus *loc, char **str, struct arg_type_info *info, int *forwardp) { eat_spaces(str); if (**str == ';') { if (forwardp == NULL) { report_error(loc->filename, loc->line_no, "Forward struct can be declared only " "directly after a typedef."); return -1; } /* Forward declaration is currently handled as an * empty struct. */ type_init_struct(info); *forwardp = 1; return 0; } if (parse_char(loc, str, '(') < 0) return -1; eat_spaces(str); // Empty arg list with whitespace inside type_init_struct(info); while (1) { eat_spaces(str); if (**str == 0 || **str == ')') { parse_char(loc, str, ')'); return 0; } /* Field delimiter. */ if (type_struct_size(info) > 0) parse_char(loc, str, ','); eat_spaces(str); int own; struct arg_type_info *field = parse_lens(plib, loc, str, NULL, 0, &own, NULL); if (field == NULL || type_struct_add(info, field, own)) { type_destroy(info); return -1; } } }
static struct expr_node * parse_zero(struct locus *loc, char **str, int *ownp) { eat_spaces(str); if (**str == '(') { ++*str; int own; struct expr_node *arg = parse_argnum(loc, str, &own, 0); if (arg == NULL) return NULL; if (parse_char(loc, str, ')') < 0) { fail: expr_destroy(arg); free(arg); return NULL; } struct expr_node *ret = build_zero_w_arg(arg, own); if (ret == NULL) goto fail; *ownp = 1; return ret; } else { *ownp = 0; return expr_node_zero(); } }
int UBX::receive(unsigned timeout) { /* poll descriptor */ pollfd fds[1]; fds[0].fd = _fd; fds[0].events = POLLIN; uint8_t buf[128]; /* timeout additional to poll */ uint64_t time_started = hrt_absolute_time(); ssize_t count = 0; bool handled = false; while (true) { /* poll for new data, wait for only UBX_PACKET_TIMEOUT (2ms) if something already received */ int ret = poll(fds, sizeof(fds) / sizeof(fds[0]), handled ? UBX_PACKET_TIMEOUT : timeout); if (ret < 0) { /* something went wrong when polling */ return -1; } else if (ret == 0) { /* return success after short delay after receiving a packet or timeout after long delay */ return handled ? 1 : -1; } else if (ret > 0) { /* if we have new data from GPS, go handle it */ if (fds[0].revents & POLLIN) { /* * We are here because poll says there is some data, so this * won't block even on a blocking device. But don't read immediately * by 1-2 bytes, wait for some more data to save expensive read() calls. * If more bytes are available, we'll go back to poll() again. */ usleep(UBX_WAIT_BEFORE_READ * 1000); count = read(_fd, buf, sizeof(buf)); /* pass received bytes to the packet decoder */ for (int i = 0; i < count; i++) { if (parse_char(buf[i]) > 0) { /* return to configure during configuration or to the gps driver during normal work * if a packet has arrived */ if (handle_message() > 0) handled = true; } } } } /* abort after timeout if no useful packets received */ if (time_started + timeout * 1000 < hrt_absolute_time()) { return -1; } } }
void scr_cw( void ) { char * pa; char * p; int len; p = scan_start; while( *p && *p != ' ' ) { // over cw p++; } while( *p && *p == ' ' ) { // next word start p++; } pa = p; while( *p && *p != ' ' ) { // end of word p++; } len = p - pa; if( len > 2 ) { xx_line_err( err_inv_cw_sep, pa ); return; } else if( len > 0 ) { // 1 char or 2 hex characters CW_sep_char = parse_char( pa, len ); } else { CW_sep_char = '\0'; } add_to_sysdir( "$cw", CW_sep_char ); scan_restart = pa + len; return; }
// HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT const char* parse_http_version(unsigned char** p) { const unsigned char STR_HTTP[] = "HTTP/"; unsigned char* ptmp = *p; if(parse_string(p, STR_HTTP, sizeof(STR_HTTP)-1, 1)) return (char*)ERR; int failed = 0; if(parse_digits(p)) failed = 1; if(!failed && parse_char(p, '.')) failed = 1; if(!failed && parse_digits(p)) failed = 1; if(failed) { *p = ptmp; return ERR; } return NULL; }
/* Parses version string of the form <int>.<int> */ static bool parse_version(const char **string) { return parse_uint(string, &major_version) && parse_char(string, '.') && parse_uint(string, &minor_version); }
int UBX::receive(unsigned timeout) { /* poll descriptor */ pollfd fds[1]; fds[0].fd = _fd; fds[0].events = POLLIN; uint8_t buf[32]; /* timeout additional to poll */ uint64_t time_started = hrt_absolute_time(); int j = 0; ssize_t count = 0; while (true) { /* pass received bytes to the packet decoder */ while (j < count) { if (parse_char(buf[j]) > 0) { /* return to configure during configuration or to the gps driver during normal work * if a packet has arrived */ if (handle_message() > 0) return 1; } /* in case we keep trying but only get crap from GPS */ if (time_started + timeout*1000 < hrt_absolute_time() ) { return -1; } j++; } /* everything is read */ j = count = 0; /* then poll for new data */ int ret = ::poll(fds, sizeof(fds) / sizeof(fds[0]), timeout); if (ret < 0) { /* something went wrong when polling */ return -1; } else if (ret == 0) { /* Timeout */ return -1; } else if (ret > 0) { /* if we have new data from GPS, go handle it */ if (fds[0].revents & POLLIN) { /* * We are here because poll says there is some data, so this * won't block even on a blocking device. If more bytes are * available, we'll go back to poll() again... */ count = ::read(_fd, buf, sizeof(buf)); } } } }
/* read from a file until a whole line is ready for use */ int pconf_file_next(PCONF_CTX *ctx) { if (!check_magic(ctx)) return 0; ctx->linenum++; /* start over for the new line */ ctx->numargs = 0; ctx->state = STATE_FINDWORDSTART; while ((ctx->ch = fgetc(ctx->f)) != EOF) { parse_char(ctx); if (ctx->state == STATE_PARSEERR) return 1; if (ctx->state == STATE_ENDOFLINE) return 1; } /* deal with files that don't end in a newline */ if (ctx->numargs != 0) { /* still building a word? */ if (ctx->wordptr != ctx->wordbuf) endofword(ctx); return 1; } /* finished with nothing left over */ return 0; }
/* parse a provided line */ int pconf_line(PCONF_CTX *ctx, const char *line) { size_t i, linelen; if (!check_magic(ctx)) return 0; ctx->linenum++; /* start over for the new line */ ctx->numargs = 0; ctx->state = STATE_FINDWORDSTART; linelen = strlen(line); for (i = 0; i < linelen; i++) { ctx->ch = line[i]; parse_char(ctx); if (ctx->state == STATE_PARSEERR) return 1; if (ctx->state == STATE_ENDOFLINE) return 1; } /* deal with any lingering characters */ /* still building a word? */ if (ctx->wordptr != ctx->wordbuf) endofword(ctx); /* tie it off */ return 1; }
static int parse_absolute_path(http_parser_t *parser) { char *path, *token; path = malloc(PATH_LEN); ASSERT(path != NULL); memset(path, 0, PATH_LEN); do { if (parser->parse_ptr - parser->data >= parser->len) return 1; if (parse_char(parser, '/')) break; token = parse_token(parser); if (!token) return 1; strlcat(path, "/", PATH_LEN); strlcat(path, token, PATH_LEN); free(token); } while (1); parser->req.path = path; return 0; }
int read_num(FILE* f, int* p_value) { int digit; int value = 0; bool is_first = true; while(1) { digit = parse_char(fgetc(f)); if(is_first) { // ignore extra spaces if(digit == END_NUM) continue; // case of only spaces before line ending if(digit < 0) break; is_first = false; } // stop when we encountered a non-digit if(digit < 0) break; // add digit to final value value = 10 * value + digit; } *p_value = !is_first ? value : -1; return digit; }
void CParser::parse(const std::string& command) { if(current_mode.top().super_mode == ESM_EDIT) lookup_line(command); for(std::string::const_iterator i = command.begin(); i != command.end(); ++i) { parse_char(*i); } }
// media-type = type "/" subtype *( ";" parameter ) // type = token // subtype = token const char* parse_media_type(unsigned char** p) { if (parse_token(p)) return ERR; if (parse_char(p, '/')) return ERR; if (parse_token(p)) return ERR; while (0 != (char)**p) { if (parse_char(p, ';')) break; if (parse_parameter(p)) return ERR; } return NULL; }
void cop_tr_table( char * p ) { bool first_found; bool no_data; char * pa; int i; uint8_t token_char; uint8_t first_char; uint32_t len; char cwcurr[4]; cwcurr[0] = SCR_char; cwcurr[1] = 't'; cwcurr[2] = 'r'; cwcurr[3] = '\0'; // if there is no data, then the table will be reset first_found = false; first_char = 0; no_data = true; while( *p ) { while( *p && *p == ' ' ) { // next char start p++; } pa = p; while( *p && *p != ' ' ) { // next char start p++; } len = p - pa; if( len == 0 ) break; // exit loop if no next char token_char = parse_char( pa, len ); no_data = false; if( first_found ) { // we now have two chars tr_table[first_char] = token_char; first_found = false; } else { // we found a first or only char first_char = token_char; first_found = true; } } if( first_found ) { // we now have two chars tr_table[first_char] = first_char; } if( no_data ) { // reset the table if no_data is still true for( i = 0; i < 0x100; i++ ) { tr_table[i] = i; } } return; }
struct ptf *ptf_parse_file(char *filename) { FILE *f; char buffer[1024]; struct ptf *root; struct ptf_parser_state state; if(filename == NULL) { fprintf(stderr, "Internal error: " "No filename was given to ptf_read()\n"); return NULL; }; f = fopen(filename, "r"); if(f == NULL) { perror(filename); return NULL; }; init_parser(&state, filename); while(!feof(f)) { size_t r, n; if(ferror(f)) { perror(filename); abort_parsing(&state); fclose(f); return NULL; }; n = fread(buffer, 1, 1024, f); for(r=0; r<n && (state.flag.error==0); r++) parse_char(&state, buffer[r]); }; fclose(f); if(state.section_level != 0) { parse_error(&state, "Input file seems to be incomplete, " "one or more sections are not closed with '}'\n"); }; if(state.flag.error) { abort_parsing(&state); return NULL; }; return state.tree; }
// parameter = attribute "=" value // attribute = token const char* parse_parameter(unsigned char** p) { if (parse_token(p)) return ERR; if (parse_char(p, '=')) return ERR; if (parse_value(p)) return ERR; return NULL; }
static const char* parse_str(const char* ptr, char sep, str* out) { char ch = 0; /* str_truncate(out, 0); */ for (;;) { if (*ptr == sep || *ptr == NUL) return ptr; ptr = parse_char(ptr, &ch); str_catc(out, ch); } return ptr; }
bool parse(ForwardIterator& iter, ForwardIterator end) { while ((iter != end) && (REQ_VALID != state_)) { char c(*iter++); if ((fail_ = !parse_char(c))) // Note: deliberate assignment return false; } valid_ = (REQ_VALID == state_); return valid_; }
static bool parse_feature_value_postfix (const char **pp, const char *end, hb_feature_t *feature) { bool had_equal = parse_char (pp, end, '='); bool had_value = parse_uint (pp, end, &feature->value) || parse_bool (pp, end, &feature->value); /* CSS doesn't use equal-sign between tag and value. * If there was an equal-sign, then there *must* be a value. * A value without an eqaul-sign is ok, but not required. */ return !had_equal || had_value; }
static int class_add_char(struct peg_grammar_parser *pgp, struct peg_cursor *pc, byte_t *cset) { struct peg_cursor npc = *pc; int c1; int c2; int rv; rv = parse_char(pgp, &npc, &c1); if ( rv < 0 ) return -1; if ( rv == 0 ) { pgp->err = PEG_ERR_BAD_CLASS; goto err; } if ( CHAR(pgp, &npc) != '-' ) { cset_add(cset, c1); } else { npc.pos += 1; rv = parse_char(pgp, &npc, &c2); if ( rv < 0 ) return -1; if ( rv == 0 ) { pgp->err = PEG_ERR_BAD_RANGE; goto err; } while ( c1 <= c2 ) { cset_add(cset, c1); ++c1; } } *pc = npc; return 1; err: pgp->eloc = *pc; return -1; }
bool parse(ForwardIterator& iter, ForwardIterator end) { while ((iter != end) && (CHUNK_VALID != state_)) { char c(*iter++); if (!parse_char(c)) return false; } valid_ = (CHUNK_VALID == state_); return valid_; }
// transfer-extension = token *( ";" parameter ) const char* parse_transfer_extension(unsigned char** p) { if (parse_token(p)) return ERR; while (0 != (char)**p) { if (parse_char(p, ';')) break; if (parse_parameter(p)) return ERR; } return NULL; }