/* Runs fn on the first two characters of option. */ bool compopt_short(bool (*fn)(const char *), const char *option) { char *short_opt = x_strndup(option, 2); bool retval = fn(short_opt); free(short_opt); return retval; }
static bool parse_line(const char *line, char **key, char **value, char **errmsg) { const char *p, *q; #define SKIP_WS(x) while (isspace(*x)) { ++x; } *key = NULL; *value = NULL; p = line; SKIP_WS(p); if (*p == '\0' || *p == '#') { return true; } q = p; while (isalpha(*q) || *q == '_') { ++q; } *key = x_strndup(p, q - p); p = q; SKIP_WS(p); if (*p != '=') { *errmsg = x_strdup("missing equal sign"); free(*key); *key = NULL; return false; } ++p; /* Skip leading whitespace. */ SKIP_WS(p); q = p; while (*q) { ++q; } /* Skip trailing whitespace. */ while (isspace(q[-1])) { --q; } *value = x_strndup(p, q - p); return true; #undef SKIP_WS }
bool conf_update_from_environment(struct conf *conf, char **errmsg) { char **p; char *q; char *key; char *errmsg2; const struct env_to_conf_item *env_to_conf_item; bool negate; size_t key_start; for (p = environ; *p; ++p) { if (!str_startswith(*p, "CCACHE_")) { continue; } q = strchr(*p, '='); if (!q) { continue; } if (str_startswith(*p + 7, "NO")) { negate = true; key_start = 9; } else { negate = false; key_start = 7; } key = x_strndup(*p + key_start, q - *p - key_start); ++q; /* Now points to the value. */ env_to_conf_item = find_env_to_conf(key); if (!env_to_conf_item) { free(key); continue; } if (!handle_conf_setting( conf, env_to_conf_item->conf_name, q, &errmsg2, true, negate, "environment")) { *errmsg = format("%s: %s", key, errmsg2); free(errmsg2); free(key); return false; } free(key); } return true; }
int reqs_parse(struct reqs_t *reqs) { register char *a, *z, *p; char *reqs_head = reqs->conn->reqs_head; char *reqs_line; /* [ take reqs_line */ a = reqs_head; while (*a == CR || *a == LF || *a == SP || *a == HT) { a++; } z = a; while (*z != CR && *z != LF && *z) { z++; } if (z == a) { return 0; } reqs_line = x_strndup(a, z - a); reqs->reqs_line = reqs_line; /* ] */ /* [ method_name */ reqs->method = HTTP_METHOD_UNKNOWN; a = reqs_line; z = a; while (*z != SP && *z) { z++; } switch (z - a) { case 0: case 1: case 2: break; case 3: if (str3equ(a, 'G', 'E', 'T')) { reqs->method = HTTP_METHOD_GET; } else if (str3equ(a, 'P', 'U', 'T')) { reqs->method = HTTP_METHOD_PUT; } break; case 4: if (str4equ(a, 'P', 'O', 'S', 'T')) { reqs->method = HTTP_METHOD_POST; } else if (str4equ(a, 'H', 'E', 'A', 'D')) { reqs->method = HTTP_METHOD_HEAD; } break; case 5: if (str5equ(a, 'T', 'R', 'A', 'C', 'E')) { reqs->method = HTTP_METHOD_TRACE; } break; case 6: if (str6equ(a, 'D', 'E', 'L', 'E', 'T', 'E')) { reqs->method = HTTP_METHOD_DELETE; } break; case 7: if (str7equ(a, 'O', 'P', 'T', 'I', 'O', 'N', 'S')) { reqs->method = HTTP_METHOD_OPTIONS; } else if (str7equ(a, 'C', 'O', 'N', 'N', 'E', 'C', 'T')) { reqs->method = HTTP_METHOD_CONNECT; } break; default: break; } reqs->method_name = http_method_names[reqs->method]; if (reqs->method == HTTP_METHOD_UNKNOWN) { reqs_throw_status(reqs, 501, ""); /* "501 Method Not Implemented" */ chtd_cry(reqs->htdx, "Method Not Implemented: [%s]", a); return 0; } /* ] */ /* [ URI */ #define is_valid_uri_char(c) ((c > 0x20) && (c != 0x7f) && (c != 0xff)) a = z; while (*a == SP) { a++; } z = a; while (*z != SP && *z) { z++; } if (a == z) { return 0; } reqs->uri = x_strndup(a, z - a); p = reqs->uri; for ( ; *p; p++) { if (!is_valid_uri_char(*p)) { return 0; } } /* ] */ /* [ http version */ reqs->http_version = HTTP_VERSION_0_9; a = z; for ( ; *a == SP; a++); z = strchr(a, 0); if (z - a == 8) { if (str8equ(a, 'H', 'T', 'T', 'P', '/', '1', '.', '0')) { reqs->http_version = HTTP_VERSION_1_0; } else if (str8equ(a, 'H', 'T', 'T', 'P', '/', '1', '.', '1')) { reqs->http_version = HTTP_VERSION_1_1; } } reqs->http_version_name = http_version_names[reqs->http_version]; /* ] */ /* [ parse request_path */ a = reqs->uri; z = a; while (*z != '?' && *z) { z++; } reqs->request_path = x_strndup(a, z - a); path_tidy(reqs->request_path); /* ] */ /* [ parse query_string */ a = strchr(reqs->uri, '?'); if (a) { reqs->query_string = strdup(a + 1); } else { reqs->query_string = calloc(1 , 1); } /* ] */ /* [ http headers */ parse_header(&reqs->re_headers, strchr(reqs_head, LF) + 1); /* ] */ reqs->content_length = atoi(get_http_header(reqs, "Content-Length")); reqs_parse_post(reqs); return 1; }