/* Returns the appropriate handler, or NULL if the string isn't an URL. */ static const struct hFILE_scheme_handler *find_scheme_handler(const char *s) { static const struct hFILE_scheme_handler unknown_scheme = { hopen_unknown_scheme, hfile_always_local, "built-in", 0 }; char scheme[12]; int i; for (i = 0; i < sizeof scheme; i++) if (isalnum_c(s[i]) || s[i] == '+' || s[i] == '-' || s[i] == '.') scheme[i] = tolower_c(s[i]); else if (s[i] == ':') break; else return NULL; // 1 byte schemes are likely windows C:/foo pathnames if (i <= 1 || i >= sizeof scheme) return NULL; scheme[i] = '\0'; pthread_mutex_lock(&plugins_lock); if (! schemes) load_hfile_plugins(); pthread_mutex_unlock(&plugins_lock); khint_t k = kh_get(scheme_string, schemes, scheme); return (k != kh_end(schemes))? kh_value(schemes, k) : &unknown_scheme; }
char * span_var_expr(char *t) { int parenthesis = 0; int brackets = 0; while (*t && (isalnum_c(*t) || strchr(VALIDCHARS, *t))) switch (*t++) { case '[': brackets++; break; case '(': parenthesis++; break; case ']': if (brackets <= 0) return t-1; if (--brackets <= 0) return t; break; case ')': if (parenthesis <= 0) return t-1; if (--parenthesis <= 0) return t; break; default: break; } return t; }
static int is_dns_compliant(const char *s0, const char *slim) { int has_nondigit = 0, len = 0; const char *s; for (s = s0; s < slim; len++, s++) if (islower_c(*s)) has_nondigit = 1; else if (*s == '-') { has_nondigit = 1; if (s == s0 || s+1 == slim) return 0; } else if (isdigit_c(*s)) ; else if (*s == '.') { if (s == s0 || ! isalnum_c(s[-1])) return 0; if (s+1 == slim || ! isalnum_c(s[1])) return 0; } else return 0; return has_nondigit && len >= 3 && len <= 63; }
/* Evaluate a variable. */ wordlist * vareval(char *string) { struct variable *v; wordlist *wl; char buf[BSIZE_SP], *s; char *oldstring = copy(string); char *range = NULL; int i, up, low; cp_wstrip(string); if ((s = strchr(string, '[')) != NULL) { *s = '\0'; range = s + 1; } switch (*string) { case '$': wl = wl_cons(tprintf("%d", getpid()), NULL); tfree(oldstring); return (wl); case '<': (void) fflush(cp_out); if (!fgets(buf, BSIZE_SP, cp_in)) { clearerr(cp_in); (void) strcpy(buf, "EOF"); } for (s = buf; *s && (*s != '\n'); s++) ; *s = '\0'; wl = cp_lexer(buf); /* This is a hack. */ if (!wl->wl_word) wl->wl_word = copy(""); tfree(oldstring); return (wl); case '?': string++; for (v = variables; v; v = v->va_next) if (eq(v->va_name, string)) break; if (!v) v = cp_enqvar(string); wl = wl_cons(copy(v ? "1" : "0"), NULL); tfree(oldstring); return (wl); case '#': string++; for (v = variables; v; v = v->va_next) if (eq(v->va_name, string)) break; if (!v) v = cp_enqvar(string); if (!v) { fprintf(cp_err, "Error: %s: no such variable.\n", string); tfree(oldstring); return (NULL); } if (v->va_type == CP_LIST) for (v = v->va_vlist, i = 0; v; v = v->va_next) i++; else i = (v->va_type != CP_BOOL); wl = wl_cons(tprintf("%d", i), NULL); tfree(oldstring); return (wl); case '\0': wl = wl_cons(copy("$"), NULL); tfree(oldstring); return (wl); } /* The notation var[stuff] has two meanings... If this is a real * variable, then the [] denotes range, but if this is a strange * (e.g, device parameter) variable, it could be anything... */ for (v = variables; v; v = v->va_next) if (eq(v->va_name, string)) break; if (!v && isdigit_c(*string)) { for (v = variables; v; v = v->va_next) if (eq(v->va_name, "argv")) break; range = string; } if (!v) { range = NULL; string = oldstring; v = cp_enqvar(string); } if (!v && (s = getenv(string)) != NULL) { wl = wl_cons(copy(s), NULL); tfree(oldstring); return (wl); } if (!v) { fprintf(cp_err, "Error: %s: no such variable.\n", string); tfree(oldstring); return (NULL); } wl = cp_varwl(v); /* Now parse and deal with 'range' ... */ if (range) { /* rather crude fix when range itself is a $expression */ wordlist *r = NULL; if (*range == '$') { char *t = ++range; if (*t == '&') t++; while (isalnum_c(*t)) t++; *t = '\0'; r = vareval(range); if (!r || r->wl_next) { fprintf(cp_err, "Error: %s: illegal index.\n", string); tfree(oldstring); wl_free(r); return NULL; } range = r->wl_word; } for (low = 0; isdigit_c(*range); range++) low = low * 10 + *range - '0'; if ((*range == '-') && isdigit_c(range[1])) for (up = 0, range++; isdigit_c(*range); range++) up = up * 10 + *range - '0'; else if (*range == '-') up = wl_length(wl); else up = low; up--, low--; wl = wl_range(wl, low, up); wl_free(r); } tfree(oldstring); return (wl); }