json parser::parse_object( const std::string& input, size_t& offset, std::error_code& error) { assert(!error); json object = json(class_type::object); offset++; consume_white_space(input, offset, error); if (input[offset] == '}') { offset++; return object; } while (true) { json Key = parse_next(input, offset, error); consume_white_space(input, offset, error); if (input[offset] != ':') { error = std::make_error_code(std::errc::invalid_argument); std::cerr << "Error: object: Expected colon, found '" << input[offset] << "'\n"; break; } offset++; consume_white_space(input, offset, error); json Value = parse_next(input, offset, error); object[Key.to_string()] = Value; consume_white_space(input, offset, error); if (input[offset] == ',') { offset++; continue; } else if (input[offset] == '}') { offset++; break; } else { error = std::make_error_code(std::errc::invalid_argument); std::cerr << "ERROR: object: Expected comma, found '" << input[offset] << "'\n"; break; } } return object; }
CommandLine::CommandLine(int argc, char* const argv[], const char* user, const char* pass, const char* usage_extra) : CommandLineBase(argc, argv, "hm:p:s:u:D?"), dtest_mode_(false), run_mode_(0), server_(0), user_(user && *user ? user : 0), pass_(pass && *pass ? pass : ""), usage_extra_(usage_extra) { int ch; while (successful() && ((ch = parse_next()) != EOF)) { switch (ch) { case 'm': run_mode_ = atoi(option_argument()); break; case 'p': pass_ = option_argument(); break; case 's': server_ = option_argument(); break; case 'u': user_ = option_argument(); break; case 'D': dtest_mode_ = true; break; default: parse_error(); return; } } finish_parse(); }
//---------------------------------------------------------------- // yae_version // void yae_version(unsigned int *major, unsigned int *minor, unsigned int *patch) { std::istringstream iss; iss.str(std::string(YAE_REVISION_TIMESTAMP)); unsigned int year = parse_next(iss); unsigned int month = parse_next(iss); unsigned int day = parse_next(iss); unsigned int hhmm = parse_next(iss); unsigned int hh = hhmm / 100; unsigned int mm = hhmm % 100; *major = (year < 2000 || year > 2255) ? 0 : (year - 2000); *minor = (month < 1 || month > 12) ? 0 : month; *patch = (day < 1 || day > 31) ? 0 : (mm + 60 * (hh + 24 * (day - 1))); }
/* the program root node */ void parse_prog(ParseState* ps) { if (parse_hasError(ps)) return; do { parse_next(ps); parse_statement(ps); } while (ps->token != TK_EOI && !parse_hasError(ps)); }
parser( const mem_chunk &mem, const file_id_t &input_file_id ) : buf(mem, input_file_id), hd_valid(false), body_bytes_left(0) { parse_next(); }
uint8_t parse_accept(ParseState* ps, token_t tok) { if (ps->token == tok) { parse_next(ps); return 1; } return 0; }
int parse_cmd(t_parse *p) { if (ft_strncmp(&LINE(POS), NAME_CMD_STRING, ft_strlen(NAME_CMD_STRING)) == 0 && parse_next(p)) { parse_cmd_string_name(p); if (ft_strlen(p->champ->name) > PROG_NAME_LENGTH) error(CMD_STRING_LENGTH, p); } else if (ft_strncmp(&LINE(POS), COMMENT_CMD_STRING, ft_strlen(COMMENT_CMD_STRING)) == 0 && parse_next(p)) { parse_cmd_string_comment(p); if (ft_strlen(p->champ->comment) > COMMENT_LENGTH) error(CMD_STRING_LENGTH, p); } else error(CMD, p); POS++; if (LINE(POS)) error(INVALID_CHAR, p); return (1); }
json parser::parse_array( const std::string& input, size_t& offset, std::error_code& error) { json array = json(class_type::array); uint32_t index = 0; offset++; consume_white_space(input, offset, error); if (input[offset] == ']') { offset++; return array; } while (true) { array[index++] = parse_next(input, offset, error); consume_white_space(input, offset, error); if (input[offset] == ',') { offset++; continue; } else if (input[offset] == ']') { offset++; break; } else { error = std::make_error_code(std::errc::invalid_argument); std::cerr << "ERROR: array: Expected ',' or ']', found '" << input[offset] << "'\n"; return json(class_type::array); } } return array; }
struct node *parse_factor(struct compiler *compiler) { switch (lexer_current(compiler)) { case T_BEGIN: return parse_begin(compiler); case T_IF: return parse_if(compiler); case T_UNLESS: return parse_unless(compiler); case T_CASE: return parse_case(compiler); case T_CLASS: return parse_class(compiler); case T_MODULE: return parse_module(compiler); case T_DEF: return parse_method(compiler); case T_YIELD: return parse_yield(compiler); case T_RETURN: return parse_return(compiler); case T_BREAK: return parse_break(compiler); case T_NEXT: return parse_next(compiler); case T_REDO: return parse_redo(compiler); case T_SQUARE_OPEN: { struct node *result = alloc_node(compiler, N_ARRAY); lexer_next(compiler); if(lexer_current(compiler) == T_SQUARE_CLOSE) result->left = 0; else result->left = parse_array_element(compiler); lexer_match(compiler, T_SQUARE_CLOSE); return result; } case T_STRING: { struct node *result = alloc_node(compiler, N_STRING); result->left = (void *)lexer_token(compiler)->start; lexer_next(compiler); return result; } case T_STRING_START: { struct node *result = alloc_node(compiler, N_STRING_CONTINUE); result->left = 0; result->middle = (void *)lexer_token(compiler)->start; lexer_next(compiler); result->right = parse_statements(compiler); while(lexer_current(compiler) == T_STRING_CONTINUE) { struct node *node = alloc_node(compiler, N_STRING_CONTINUE); node->left = result; node->middle = (void *)lexer_token(compiler)->start; lexer_next(compiler); node->right = parse_statements(compiler); result = node; } if(lexer_require(compiler, T_STRING_END)) { struct node *node = alloc_node(compiler, N_STRING_START); node->left = result; node->right = (void *)lexer_token(compiler)->start; lexer_next(compiler); return node; } return result; } case T_SELF: { lexer_next(compiler); return &self_node; } case T_TRUE: { lexer_next(compiler); return alloc_node(compiler, N_TRUE); } case T_FALSE: { lexer_next(compiler); return alloc_node(compiler, N_FALSE); } case T_NIL: { lexer_next(compiler); return &nil_node; } case T_NUMBER: { struct node *result = alloc_node(compiler, N_NUMBER); char *text = get_token_str(lexer_token(compiler)); result->left = (void* )atoi(text); lexer_next(compiler); return result; } case T_IVAR: { rt_value symbol = rt_symbol_from_lexer(compiler); lexer_next(compiler); switch (lexer_current(compiler)) { case T_ASSIGN_ADD: case T_ASSIGN_SUB: case T_ASSIGN_MUL: case T_ASSIGN_DIV: { struct node *result; enum token_type op_type = lexer_current(compiler) - OP_TO_ASSIGN; lexer_next(compiler); result = alloc_node(compiler, N_IVAR_ASSIGN); result->right = alloc_node(compiler, N_BINARY_OP); result->right->op = op_type; result->right->left = alloc_node(compiler, N_IVAR); result->right->left->left = (void *)symbol; result->right->right = parse_expression(compiler); result->left = (void *)symbol; return result; } case T_ASSIGN: { struct node *result; lexer_next(compiler); result = alloc_node(compiler, N_IVAR_ASSIGN); result->left = (void *)symbol; result->right = parse_expression(compiler); return result; } default: { struct node *result = alloc_node(compiler, N_IVAR); result->left = (void *)symbol; return result; } } } case T_IDENT: return parse_identifier(compiler); case T_EXT_IDENT: return parse_call(compiler, 0, &self_node, false); case T_PARAM_OPEN: { lexer_next(compiler); struct node *result = parse_statements(compiler); lexer_match(compiler, T_PARAM_CLOSE); return result; } default: { COMPILER_ERROR(compiler, "Expected expression but found %s", token_type_names[lexer_current(compiler)]); lexer_next(compiler); return 0; } } }
static int avail(char *resources, opal_list_t *available) { char *query, *line, *ptr; FILE *fp; orcm_pvsn_resource_t *res; opal_value_t *attr; char **types = NULL; int i, rc=ORTE_SUCCESS; int j; OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME))); /* A NULL for resources indicates the caller wants a list * of all known resource types. WW only has two of interest: * vnfs images and files */ if (NULL == resources) { opal_argv_append_nosize(&types, "images"); opal_argv_append_nosize(&types, "files"); } else { /* the resource request can contain a comma-separated list * of desired resource types, so split it here */ types = opal_argv_split(resources, ','); } for (i=0; NULL != types[i]; i++) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail looking for resource type %s", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), types[i])); if (0 == strcasecmp(types[i], "images")) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail getting vnfs list", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME))); /* get the vnfs image list */ (void)asprintf(&query, "%s vnfs list", cmd); if (NULL == (fp = popen(query, "r"))) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail query for resource type %s failed", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), types[i])); free(query); rc = ORTE_ERROR; break; } free(query); while (NULL != (line = orcm_getline(fp))) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail got input %s", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), line)); /* we need to discard the header, so check for it */ if (0 == strncmp(line, "VNFS NAME", strlen("VNFS NAME"))) { /* this is the header - ignore it */ OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail ignoring header", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME))); free(line); continue; } /* break into three sections - the first contains the * image name, the second the size, and the third the * chroot location */ ptr = line; if (NULL == (query = parse_next(line, &ptr))) { /* not even an image name was given */ free(line); continue; } res = OBJ_NEW(orcm_pvsn_resource_t); res->type = strdup("image"); opal_list_append(available, &res->super); attr = OBJ_NEW(opal_value_t); attr->key = strdup("name"); attr->type = OPAL_STRING; attr->data.string = strdup(query); opal_list_append(&res->attributes, &attr->super); if (NULL == (query = parse_next(ptr, &ptr))) { /* should have been the size, but no other info * was given - for now, don't worry about it */ free(line); continue; } attr = OBJ_NEW(opal_value_t); attr->key = strdup("size"); attr->type = OPAL_FLOAT; attr->data.fval = strtof(query, NULL); opal_list_append(&res->attributes, &attr->super); if (NULL == (query = parse_next(ptr, &ptr))) { /* should have been the location, but no other info * was given - for now, don't worry about it */ free(line); continue; } attr = OBJ_NEW(opal_value_t); attr->key = strdup("location"); attr->type = OPAL_STRING; attr->data.string = strdup(query); opal_list_append(&res->attributes, &attr->super); free(line); } pclose(fp); } else if (0 == strcasecmp(types[i], "files")) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail getting files list", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME))); /* get the files list */ (void)asprintf(&query, "%s file list", cmd); if (NULL == (fp = popen(query, "r"))) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail query for resource type %s failed", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), types[i])); free(query); rc = ORTE_ERROR; break; } free(query); while (NULL != (line = orcm_getline(fp))) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail got input %s", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), line)); /* we want the following sections of the output line: * 0 => ww file name * 2 => permissions * 4 => owner * 5 => group * 6 => size * 7 => path */ ptr = line; j=0; while (NULL != (query = parse_next(ptr, &ptr))) { switch(j) { case 0: attr = OBJ_NEW(opal_value_t); attr->key = strdup("name"); attr->type = OPAL_STRING; attr->data.string = strdup(query); opal_list_append(&res->attributes, &attr->super); break; case 2: attr = OBJ_NEW(opal_value_t); attr->key = strdup("permissions"); attr->type = OPAL_STRING; attr->data.string = strdup(query); opal_list_append(&res->attributes, &attr->super); break; case 4: attr = OBJ_NEW(opal_value_t); attr->key = strdup("owner"); attr->type = OPAL_STRING; attr->data.string = strdup(query); opal_list_append(&res->attributes, &attr->super); break; case 5: attr = OBJ_NEW(opal_value_t); attr->key = strdup("group"); attr->type = OPAL_STRING; attr->data.string = strdup(query); opal_list_append(&res->attributes, &attr->super); break; case 6: attr = OBJ_NEW(opal_value_t); attr->key = strdup("size"); attr->type = OPAL_SIZE; attr->data.size = (size_t)strtoul(query, NULL, 10); opal_list_append(&res->attributes, &attr->super); break; case 7: attr = OBJ_NEW(opal_value_t); attr->key = strdup("path"); attr->type = OPAL_STRING; attr->data.string = strdup(query); opal_list_append(&res->attributes, &attr->super); break; default: /* just ignore it */ break; } j++; } free(line); } pclose(fp); } else { orte_show_help("help-pvsn-ww.txt", "unknown-type", true, types[i]); rc = ORTE_ERR_BAD_PARAM; break; } } if (NULL != types) { opal_argv_free(types); } return rc; }
static bool parse_digest_response(struct digest_auth_request *request, const unsigned char *data, size_t size, const char **error) { char *copy, *key, *value; bool failed; /* realm="realm" username="******" nonce="randomized data" cnonce="??" nc=00000001 qop="auth|auth-int|auth-conf" digest-uri="serv-type/host[/serv-name]" response=32 HEX digits maxbuf=number (with auth-int, auth-conf, defaults to 64k) charset="utf-8" (iso-8859-1 if it doesn't exist) cipher="cipher-value" authzid="authzid-value" */ *error = NULL; failed = FALSE; if (size == 0) { *error = "Client sent no input"; return FALSE; } /* treating response as NUL-terminated string also gets rid of all potential problems with NUL characters in strings. */ copy = t_strdup_noconst(t_strndup(data, size)); while (*copy != '\0') { if (parse_next(©, &key, &value)) { if (!auth_handle_response(request, key, value, error)) { failed = TRUE; break; } } if (*copy == ',') copy++; } if (!failed) { if (!request->nonce_found) { *error = "Missing nonce parameter"; failed = TRUE; } else if (request->cnonce == NULL) { *error = "Missing cnonce parameter"; failed = TRUE; } else if (request->username == NULL) { *error = "Missing username parameter"; failed = TRUE; } } if (request->nonce_count == NULL) request->nonce_count = p_strdup(request->pool, "00000001"); if (request->qop_value == NULL) request->qop_value = p_strdup(request->pool, "auth"); return !failed; }
json parser::parse(const std::string& input, std::error_code& error) { assert(!error); std::size_t offset = 0; return parse_next(input, offset, error); }
CommandLine::CommandLine(int argc, char* const argv[]) : CommandLineBase(argc, argv, "hi:o:p:s:t:u:1:?"), input_(0), output_(0), pass_(""), server_(0), user_(0), input_source_(ss_unknown), output_sink_(ss_unknown) { // Parse the command line int ch; while (successful() && ((ch = parse_next()) != EOF)) { switch (ch) { case 'i': case 't': case '1': if (input_) { std::cerr << "Warning: overriding previous input " "source! Only last -i, -t or -1 is " "effective.\n"; } input_ = option_argument(); input_source_ = (ch == '1' ? ss_ssqls1 : ch == 'i' ? ss_ssqls2 : ss_table); break; case 'o': output_ = option_argument(); output_sink_ = ss_ssqls2; break; case 'p': pass_ = option_argument(); break; case 's': server_ = option_argument(); break; case 'u': user_ = option_argument(); break; default: parse_error(); } } finish_parse(); // Figure out whether command line makes sense, and if not, tell // user about it. if (successful()) { if (input_source_ == ss_unknown) { parse_error("No input source given! Need -i, -t or -1."); } else if ((input_source_ != ss_ssqls2) && !output_) { parse_error("Need -o if you give -t or -1!"); } } }
/* * Parse the protocol and point relevant fields, don't take logic decisions * based on this, just parse to locate things. */ int mk_http_parser(struct mk_http_request *req, struct mk_http_parser *p, char *buffer, int len) { int i; int s; int tmp; int ret; for (i = p->i; i < len; p->i++, p->chars++, i++) { /* FIRST LINE LEVEL: Method, URI & Protocol */ if (p->level == REQ_LEVEL_FIRST) { switch (p->status) { case MK_ST_REQ_METHOD: /* HTTP Method */ if (p->chars == -1) { switch (buffer[i]) { case 'G': p->method = MK_METHOD_GET; break; case 'P': p->method = MK_METHOD_POST; break; case 'H': p->method = MK_METHOD_HEAD; break; case 'D': p->method = MK_METHOD_DELETE; break; case 'O': p->method = MK_METHOD_OPTIONS; break; } continue; } if (buffer[i] == ' ') { mark_end(); p->status = MK_ST_REQ_URI; if (p->end < 2) { return MK_HTTP_PARSER_ERROR; } method_lookup(req, p, buffer); parse_next(); } break; case MK_ST_REQ_URI: /* URI */ if (buffer[i] == ' ') { mark_end(); p->status = MK_ST_REQ_PROT_VERSION; if (field_len() < 1) { return MK_HTTP_PARSER_ERROR; } request_set(&req->uri, p, buffer); parse_next(); continue; } else if (buffer[i] == '?') { mark_end(); request_set(&req->uri, p, buffer); p->status = MK_ST_REQ_QUERY_STRING; parse_next(); } else if (buffer[i] == '\r' || buffer[i] == '\n') { mk_http_error(MK_CLIENT_BAD_REQUEST, req->session, req); return MK_HTTP_PARSER_ERROR; } break; case MK_ST_REQ_QUERY_STRING: /* Query string */ if (buffer[i] == ' ') { mark_end(); request_set(&req->query_string, p, buffer); p->status = MK_ST_REQ_PROT_VERSION; parse_next(); } else if (buffer[i] == '\r' || buffer[i] == '\n') { mk_http_error(MK_CLIENT_BAD_REQUEST, req->session, req); return MK_HTTP_PARSER_ERROR; } break; case MK_ST_REQ_PROT_VERSION: /* Protocol Version */ /* * Most of the time we already have the strin version in our * buffer, for that case try to match the version and avoid * loop rounds. */ if (p->chars == 0 && i + 7 <= len) { tmp = p->start; if (buffer[tmp] == 'H' && buffer[tmp + 1] == 'T' && buffer[tmp + 2] == 'T' && buffer[tmp + 3] == 'P' && buffer[tmp + 4] == '/' && buffer[tmp + 5] == '1' && buffer[tmp + 6] == '.') { i += 7; p->i = i; p->chars += 7; request_set(&req->protocol_p, p, buffer); mk_http_set_minor_version(buffer[tmp + 7]); continue; } else { mk_http_error(MK_SERVER_HTTP_VERSION_UNSUP, req->session, req); return MK_HTTP_PARSER_ERROR; } } if (buffer[i] == '\r') { if (!req->protocol_p.data) { mark_end(); if (field_len() != 8) { mk_http_error(MK_SERVER_HTTP_VERSION_UNSUP, req->session, req); return MK_HTTP_PARSER_ERROR; } if (strncmp(buffer + p->start, "HTTP/1.", 7) != 0) { mk_http_error(MK_SERVER_HTTP_VERSION_UNSUP, req->session, req); return MK_HTTP_PARSER_ERROR; } request_set(&req->protocol_p, p, buffer); mk_http_set_minor_version(req->protocol_p.data[req->protocol_p.len - 1]); } /* Try to catch next LF */ if (i < len) { if (buffer[i+1] == '\n') { i++; p->i = i; p->level = REQ_LEVEL_CONTINUE; parse_next(); } } p->status = MK_ST_FIRST_FINALIZING; continue; } break; case MK_ST_FIRST_FINALIZING: /* New Line */ if (buffer[i] == '\n') { p->level = REQ_LEVEL_CONTINUE; parse_next(); } else { return MK_HTTP_PARSER_ERROR; } break; case MK_ST_BLOCK_END: if (buffer[i] == '\n') { return mk_http_parser_ok(req, p); } else { return MK_HTTP_PARSER_ERROR; } break; }; } else if (p->level == REQ_LEVEL_CONTINUE) { if (buffer[i] == '\r') { p->level = REQ_LEVEL_FIRST; p->status = MK_ST_BLOCK_END; continue; } else { p->level = REQ_LEVEL_HEADERS; p->status = MK_ST_HEADER_KEY; p->chars = 0; } } /* HEADERS: all headers stuff */ if (p->level == REQ_LEVEL_HEADERS) { /* Expect a Header key */ if (p->status == MK_ST_HEADER_KEY) { if (buffer[i] == '\r') { if (p->chars == 0) { p->level = REQ_LEVEL_END; parse_next(); } else { return MK_HTTP_PARSER_ERROR; } } if (p->chars == 0) { /* * We reach the start of a Header row, lets catch the most * probable header. * * The goal of this 'first row character lookup', is to define a * small range set of probable headers comparison once we catch * a header end. */ s = tolower(buffer[i]); switch (s) { case 'a': p->header_min = MK_HEADER_ACCEPT; p->header_max = MK_HEADER_AUTHORIZATION; break; case 'c': p->header_min = MK_HEADER_CACHE_CONTROL; p->header_max = MK_HEADER_CONTENT_TYPE; break; case 'h': header_scope_eq(p, MK_HEADER_HOST); break; case 'i': header_scope_eq(p, MK_HEADER_IF_MODIFIED_SINCE); break; case 'l': p->header_min = MK_HEADER_LAST_MODIFIED; p->header_max = MK_HEADER_LAST_MODIFIED_SINCE; break; case 'r': p->header_min = MK_HEADER_RANGE; p->header_max = MK_HEADER_REFERER; break; case 'u': p->header_min = MK_HEADER_UPGRADE; p->header_max = MK_HEADER_USER_AGENT; break; default: p->header_key = -1; p->header_sep = -1; p->header_min = -1; p->header_max = -1; }; p->header_key = i; continue; } /* Found key/value separator */ if (buffer[i] == ':') { /* Set the key/value middle point */ p->header_sep = i; /* validate length */ mark_end(); if (field_len() < 1) { return MK_HTTP_PARSER_ERROR; } /* Wait for a value */ p->status = MK_ST_HEADER_VALUE; parse_next(); } } /* Parsing the header value */ else if (p->status == MK_ST_HEADER_VALUE) { /* Trim left, set starts only when found something != ' ' */ if (buffer[i] == '\r' || buffer[i] == '\n') { return MK_HTTP_PARSER_ERROR; } else if (buffer[i] != ' ') { p->status = MK_ST_HEADER_VAL_STARTS; p->start = p->header_val = i; } continue; } /* New header row starts */ else if (p->status == MK_ST_HEADER_VAL_STARTS) { /* Maybe there is no more headers and we reach the end ? */ if (buffer[i] == '\r') { mark_end(); if (field_len() <= 0) { return MK_HTTP_PARSER_ERROR; } /* * A header row has ended, lets lookup the header and populate * our headers table index. */ ret = header_lookup(p, buffer); if (ret != 0) { if (ret < -1) { mk_http_error(-ret, req->session, req); } return MK_HTTP_PARSER_ERROR; } /* Try to catch next LF */ if (i < len) { if (buffer[i+1] == '\n') { i++; p->i = i; p->status = MK_ST_HEADER_KEY; p->chars = -1; parse_next(); } } p->status = MK_ST_HEADER_END; parse_next(); } else if (buffer[i] == '\n' && buffer[i - 1] != '\r') { return MK_HTTP_PARSER_ERROR; } continue; } else if (p->status == MK_ST_HEADER_END) { if (buffer[i] == '\n') { p->status = MK_ST_HEADER_KEY; p->chars = -1; parse_next(); } else { return MK_HTTP_PARSER_ERROR; } } } else if (p->level == REQ_LEVEL_END) { if (buffer[i] == '\n') { p->level = REQ_LEVEL_BODY; p->chars = -1; parse_next(); } else { return MK_HTTP_PARSER_ERROR; } } else if (p->level == REQ_LEVEL_BODY) { /* * Reaching this level can means two things: * * - A Pipeline Request * - A Body content (POST/PUT methods) */ if (p->header_content_length > 0) { p->body_received += (len - i); if (p->body_received == p->header_content_length) { return mk_http_parser_ok(req, p); } else { return MK_HTTP_PARSER_PENDING; } } return mk_http_parser_ok(req, p); } } /* * FIXME: the code above needs to be handled in a different way */ if (p->level == REQ_LEVEL_FIRST) { if (p->status == MK_ST_REQ_METHOD) { if (p->i > 10) { return MK_HTTP_PARSER_ERROR; } else { return MK_HTTP_PARSER_PENDING; } } } else if (p->level == REQ_LEVEL_HEADERS) { if (p->status == MK_ST_HEADER_KEY) { return MK_HTTP_PARSER_PENDING; } else if (p->status == MK_ST_HEADER_VALUE) { if (field_len() < 0) { return MK_HTTP_PARSER_PENDING; } } } else if (p->level == REQ_LEVEL_BODY) { if (p->header_content_length > 0) { p->body_received += (len - i); if (p->header_content_length == p->body_received) { return mk_http_parser_ok(req, p); } else { return MK_HTTP_PARSER_PENDING; } } if (p->header_content_length > 0 && p->body_received <= p->header_content_length) { return MK_HTTP_PARSER_PENDING; } else if (p->chars == 0) { return mk_http_parser_ok(req, p); } else { } } return MK_HTTP_PARSER_PENDING; }
/* * Read a tag from a string, returning the first character * after the end of the tag. * * If no tag is found, just returns the string. * */ char *read_tag(char *s, struct tag *t) { char *p, *end; int state = 0; int key_num = 0; /* * First let's make sure there's really a "tag" here. * */ if (strncasecmp(s, "<tag ", 5) == 0) { p = s + 5; t->start = 1; } else if (strncasecmp(s, "<tag>", 5) == 0) { p = s + 4; t->start = 1; } else if (strncasecmp(s, "</tag ", 6) == 0) { p = s + 6; t->start = 0; } else if (strncasecmp(s, "</tag>", 6) == 0) { p = s + 5; t->start = 0; } else { return s; }; /* * Now we can start parsing (after the "<tag "). * * We have a bit of a state machine here. We're either * parsing a key (state = 0) , or parsing a value (state = 1). * */ while (*p && *p != '>') { /* * Alternately parse out keys and values. * */ if (!state) { /* * Possible overflow of key space? * */ if (key_num >= MAX_KEYS) return s; /* * Otherwise parse out the key. * */ end = parse_next(p, t->keys[key_num].name, MAX_KEY_NAME); /* * Possibly it was badly-formed! * */ if (end == NULL) return s; } else { end = parse_next(p, t->keys[key_num].value, MAX_KEY_VALUE); /* * Possibly it was badly-formed! * */ if (end == NULL) return s; /* * Move on to the next key. * */ key_num++; }; /* * We're now in the other state. * */ state = !state; /* * Advance p * */ p = end; }; /* * Set the number of keys we found. * */ t->num_keys = key_num; /* * Advance past the >, if necessary. * */ if (*p == '>') p++; return p; };
static int status(char *nodes, opal_list_t *images) { char *query, *line, *ptr; FILE *fp; orcm_pvsn_provision_t *pvn, *pvnptr; opal_value_t *attr; int i, rc=ORTE_SUCCESS; int j; char **nodelist, **ranges; OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:status", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME))); /* if nodes is NULL, then get the info for all nodes. Note * that this could be a *lot* of info for a large cluster */ if (NULL == nodes) { (void)asprintf(&query, "%s provision print", cmd); } else { /* could be a comma-separated regex, so parse it */ ranges = opal_argv_split(nodes, ','); nodelist = NULL; for (i=0; NULL != ranges[i]; i++) { if (ORTE_SUCCESS != (rc = orte_regex_extract_node_names(ranges[i], &nodelist))) { ORTE_ERROR_LOG(rc); opal_argv_free(ranges); return rc; } } opal_argv_free(ranges); ptr = opal_argv_join(nodelist, ' '); opal_argv_free(nodelist); (void)asprintf(&query, "%s provision print %s", cmd, ptr); free(ptr); } if (NULL == (fp = popen(query, "r"))) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:avail query for provisioning status failed", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME))); free(query); return ORCM_ERROR; } free(query); while (NULL != (line = orcm_getline(fp))) { OPAL_OUTPUT_VERBOSE((5, orcm_pvsn_base_framework.framework_output, "%s pvsn:wwulf:status got input %s", ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), line)); /* if the line starts with #, it can be ignored */ if ('#' == line[0]) { free(line); continue; } /* we want the following sections of the output line: * 0 => node name * 1 => attribute * 3 => value */ ptr = line; j=0; while (NULL != (query = parse_next(ptr, &ptr))) { switch(j) { case 0: /* see if we already have this node */ pvn = NULL; OPAL_LIST_FOREACH(pvnptr, images, orcm_pvsn_provision_t) { if (0 == strcmp(pvnptr->nodes, query)) { pvn = pvnptr; break; } } if (NULL == pvn) { pvn = OBJ_NEW(orcm_pvsn_provision_t); opal_list_append(images, &pvn->super); pvn->nodes = strdup(query); /* need to come up with a naming scheme for images */ pvn->image.name = strdup(query); } break; case 1: attr = OBJ_NEW(opal_value_t); attr->key = strdup(query); opal_list_append(&pvn->image.attributes, &attr->super); break; case 3: attr->type = OPAL_STRING; attr->data.string = strdup(query); break; default: /* just ignore it */ break; } j++; } free(line); } pclose(fp); return ORCM_SUCCESS; }
//variabelen initialiseren en parse loop starten Parser::Parser(std::istream * in, std::ostream * out, QuestionList * ql) : in_(in), out_(out), ql_(ql) { parse_next(); }
/** * This function parses or tokenizes the whole buffer into a list. * It has to do some tricks to parse preprocessors. * * If output_text() were called immediately after, two things would happen: * - trailing whitespace are removed. * - leading space & tabs are converted to the appropriate format. * * All the tokens are inserted before ref. If ref is NULL, they are inserted * at the end of the list. Line numbers are relative to the start of the data. */ void tokenize(const deque<int>& data, chunk_t *ref) { tok_ctx ctx(data); chunk_t chunk; chunk_t *pc = NULL; chunk_t *rprev = NULL; struct parse_frame frm; bool last_was_tab = false; int prev_sp = 0; memset(&frm, 0, sizeof(frm)); while (ctx.more()) { chunk.reset(); if (!parse_next(ctx, chunk)) { LOG_FMT(LERR, "%s:%d Bailed before the end?\n", cpd.filename, ctx.c.row); cpd.error_count++; break; } /* Don't create an entry for whitespace */ if (chunk.type == CT_WHITESPACE) { last_was_tab = chunk.after_tab; prev_sp = chunk.orig_prev_sp; continue; } chunk.orig_prev_sp = prev_sp; prev_sp = 0; if (chunk.type == CT_NEWLINE) { last_was_tab = chunk.after_tab; chunk.after_tab = false; chunk.str.clear(); } else if (chunk.type == CT_NL_CONT) { last_was_tab = chunk.after_tab; chunk.after_tab = false; chunk.str = "\\\n"; } else { chunk.after_tab = last_was_tab; last_was_tab = false; } /* Strip trailing whitespace (for CPP comments and PP blocks) */ while ((chunk.str.size() > 0) && ((chunk.str[chunk.str.size() - 1] == ' ') || (chunk.str[chunk.str.size() - 1] == '\t'))) { // If comment contains backslash '\' followed by whitespace chars, keep last one; // this will prevent it from turning '\' into line continuation. if ((chunk.str.size() > 1) && (chunk.str[chunk.str.size() - 2] == '\\')) { break; } chunk.str.pop_back(); } /* Store off the end column */ chunk.orig_col_end = ctx.c.col; /* Add the chunk to the list */ rprev = pc; if (rprev != NULL) { chunk_flags_set(pc, rprev->flags & PCF_COPY_FLAGS); /* a newline can't be in a preprocessor */ if (pc->type == CT_NEWLINE) { chunk_flags_clr(pc, PCF_IN_PREPROC); } } if (ref != NULL) { chunk.flags |= PCF_INSERTED; } else { chunk.flags &= ~PCF_INSERTED; } pc = chunk_add_before(&chunk, ref); /* A newline marks the end of a preprocessor */ if (pc->type == CT_NEWLINE) // || (pc->type == CT_COMMENT_MULTI)) { cpd.in_preproc = CT_NONE; cpd.preproc_ncnl_count = 0; } /* Special handling for preprocessor stuff */ if (cpd.in_preproc != CT_NONE) { chunk_flags_set(pc, PCF_IN_PREPROC); /* Count words after the preprocessor */ if (!chunk_is_comment(pc) && !chunk_is_newline(pc)) { cpd.preproc_ncnl_count++; } /* Figure out the type of preprocessor for #include parsing */ if (cpd.in_preproc == CT_PREPROC) { if ((pc->type < CT_PP_DEFINE) || (pc->type > CT_PP_OTHER)) { set_chunk_type(pc, CT_PP_OTHER); } cpd.in_preproc = pc->type; } } else { /* Check for a preprocessor start */ if ((pc->type == CT_POUND) && ((rprev == NULL) || (rprev->type == CT_NEWLINE))) { set_chunk_type(pc, CT_PREPROC); pc->flags |= PCF_IN_PREPROC; cpd.in_preproc = CT_PREPROC; } } } /* Set the cpd.newline string for this file */ if ((cpd.settings[UO_newlines].le == LE_LF) || ((cpd.settings[UO_newlines].le == LE_AUTO) && (cpd.le_counts[LE_LF] >= cpd.le_counts[LE_CRLF]) && (cpd.le_counts[LE_LF] >= cpd.le_counts[LE_CR]))) { /* LF line ends */ cpd.newline = "\n"; LOG_FMT(LLINEENDS, "Using LF line endings\n"); } else if ((cpd.settings[UO_newlines].le == LE_CRLF) || ((cpd.settings[UO_newlines].le == LE_AUTO) && (cpd.le_counts[LE_CRLF] >= cpd.le_counts[LE_LF]) && (cpd.le_counts[LE_CRLF] >= cpd.le_counts[LE_CR]))) { /* CRLF line ends */ cpd.newline = "\r\n"; LOG_FMT(LLINEENDS, "Using CRLF line endings\n"); } else { /* CR line ends */ cpd.newline = "\r"; LOG_FMT(LLINEENDS, "Using CR line endings\n"); } } // tokenize
void parse_next(parse_struct * ps) { int type, c, p, d; char * tok1 = NULL; char * tok2 = NULL; parse_struct sub; ps->owner = 0; ps->name = NULL; ignore_whitespace(); p = type = getc(stdin); if (type == EOF) { ps->valid = 0; return; } ignore_whitespace(); c = getc(stdin); if (c != ':') error(EXPECTED_COLON, &p); p = c; ignore_whitespace(); c = getc(stdin); if (c != '[') error(EXPECTED_LBRACKET, &p); switch (type) { ////////// Parse Branches \\\\\\\\\\/ case 'b': ps->flags = FL_BRANCH; ps->u.dir.size = 0; do { if (tok1 != NULL) free(tok1); p = d = next_token(&tok1); if (tok1[0] == '#') { if (strcmp(tok1, "#protected") == 0) ps->flags |= FL_PROTECTED; else error(BAD_BRANCH_FLAG, tok1); } else { if (d != '=') error(EXPECTED_EQUALS, tok1); ignore_whitespace(); if (strcmp(tok1, "contents") == 0) { c = getc(stdin); if (c != '{') error(EXPECTED_LBRACE, NULL); ps->u.dir.contents = NULL; while (1) { ignore_whitespace(); c = getc(stdin); if (c == EOF) error(UNEXPECTED_EOF, NULL); if (c == '}') break; ungetc(c, stdin); parse_next(&sub); if (!sub.valid) error(UNEXPECTED_EOF, NULL); if (ps->u.dir.size % (0x20 * sizeof(parse_struct)) == 0) { ps->u.dir.contents = realloc(ps->u.dir.contents, ps->u.dir.size + (0x20 * sizeof(parse_struct))); } ps->u.dir.contents[ps->u.dir.size++] = sub; } ignore_whitespace(); d = getc(stdin); } else { if (tok2 != NULL) free(tok2); d = next_token(&tok2); if (strcmp(tok1, "name") == 0) { ps->name = tok2; tok2 = NULL; } else if (strcmp(tok1, "owner") == 0) { ps->owner = atoi(tok2); } else error(BAD_BRANCH_FIELD, tok1); } } } while (p != ']' && d != ']'); break; ////////// Parse Files \\\\\\\\\\/ case 'f': ps->flags = 0; do { if (tok1 != NULL) free(tok1); d = next_token(&tok1); if (tok1[0] == '#') { if (strcmp(tok1, "#protected") == 0) ps->flags |= FL_PROTECTED; else if (strcmp(tok1, "#executable") == 0) ps->flags |= FL_EXECUTABLE; else error(BAD_FILE_FLAG, tok1); } else { if (d != '=') error(EXPECTED_EQUALS, tok1); if (tok2 != NULL) free(tok2); d = next_token(&tok2); if (strcmp(tok1, "name") == 0) { ps->name = tok2; tok2 = NULL; } else if (strcmp(tok1, "owner") == 0) { ps->owner = atoi(tok2); } else if (strcmp(tok1, "actual") == 0) { ps->u.actual_file = tok2; tok2 = NULL; } else error(BAD_FILE_FIELD, tok1); } } while (p != ']' && d != ']'); break; ////////// Parse Indirects \\\\\\\\\\/ case 'i': ps->flags = FL_INDIRECT; break; default: error(UNRECOGNIZED_TYPE, &type); } if (tok1 == NULL) free(tok1); if (tok2 == NULL) free(tok2); ps->valid = 1; }