From: hno <> Date: Sat, 7 Sep 2002 20:55:24 +0000 (+0000) Subject: Some reshuffling of code to where it belongs X-Git-Tag: SQUID_3_0_PRE1~777 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d860a1aad3b46542307ffd97a5d6d7c1426083ea;p=thirdparty%2Fsquid.git Some reshuffling of code to where it belongs strtokFile from acl.c to cache_cf.c strwordtok/strwordquote from external_acl.c to tools.c prototypes for the same. --- diff --git a/src/cache_cf.cc b/src/cache_cf.cc index d9e122f4a5..3636aec801 100644 --- a/src/cache_cf.cc +++ b/src/cache_cf.cc @@ -1,6 +1,6 @@ /* - * $Id: cache_cf.cc,v 1.411 2002/09/01 15:16:33 hno Exp $ + * $Id: cache_cf.cc,v 1.412 2002/09/07 14:55:24 hno Exp $ * * DEBUG: section 3 Configuration File Parsing * AUTHOR: Harvest Derived @@ -75,7 +75,6 @@ static int parseTimeUnits(const char *unit); static void parseTimeLine(time_t * tptr, const char *units); static void parse_ushort(u_short * var); static void parse_string(char **); -void parse_wordlist(wordlist **); static void default_all(void); static void defaults_if_none(void); static int parse_line(char *); @@ -2465,3 +2464,54 @@ requirePathnameExists(const char *name, const char *path) if (stat(path, &sb) < 0) fatalf("%s: %s", path, xstrerror()); } + +char * +strtokFile(void) +{ + char *t, *fn; + LOCAL_ARRAY(char, buf, 256); + + strtok_again: + if (!aclFromFile) { + t = (strtok(NULL, w_space)); + if (!t || *t == '#') { + return NULL; + } else if (*t == '\"' || *t == '\'') { + /* quote found, start reading from file */ + fn = ++t; + while (*t && *t != '\"' && *t != '\'') + t++; + *t = '\0'; + if ((aclFile = fopen(fn, "r")) == NULL) { + debug(28, 0) ("strtokFile: %s not found\n", fn); + return (NULL); + } +#if defined(_SQUID_MSWIN_) || defined(_SQUID_CYGWIN_) + setmode(fileno(aclFile), O_TEXT); +#endif + aclFromFile = 1; + } else { + return t; + } + } + /* aclFromFile */ + if (fgets(buf, 256, aclFile) == NULL) { + /* stop reading from file */ + fclose(aclFile); + aclFromFile = 0; + goto strtok_again; + } else { + t = buf; + /* skip leading and trailing white space */ + t += strspn(buf, w_space); + t[strcspn(t, w_space)] = '\0'; + /* skip comments */ + if (*t == '#') + goto strtok_again; + /* skip blank lines */ + if (!*t) + goto strtok_again; + return t; + } +} + diff --git a/src/external_acl.cc b/src/external_acl.cc index d26a460148..2af23e6dcf 100644 --- a/src/external_acl.cc +++ b/src/external_acl.cc @@ -1,6 +1,6 @@ /* - * $Id: external_acl.cc,v 1.12 2002/09/07 14:39:52 hno Exp $ + * $Id: external_acl.cc,v 1.13 2002/09/07 14:55:24 hno Exp $ * * DEBUG: section 82 External ACL * AUTHOR: Henrik Nordstrom, MARA Systems AB @@ -57,9 +57,6 @@ static void external_acl_cache_delete(external_acl * def, external_acl_entry * e static int external_acl_entry_expired(external_acl * def, external_acl_entry * entry); static void external_acl_cache_touch(external_acl * def, external_acl_entry * entry); -extern char *strtokFile(void); -static void strwordquote(MemBuf * mb, const char *str); - /******************************************************************* * external_acl cache entry * Used opaqueue in the interface @@ -654,73 +651,6 @@ free_externalAclState(void *data) cbdataReferenceDone(state->def); } -/* FIXME: This should be moved to tools.c */ -static char * -strwordtok(char *buf, char **t) -{ - unsigned char *word = NULL; - unsigned char *p = (unsigned char *) buf; - unsigned char *d; - unsigned char ch; - int quoted = 0; - if (!p) - p = (unsigned char *) *t; - if (!p) - goto error; - while (*p && isspace(*p)) - p++; - if (!*p) - goto error; - word = d = p; - while ((ch = *p)) { - switch (ch) { - case '\\': - p++; - *d++ = ch = *p; - if (ch) - p++; - break; - case '"': - quoted = !quoted; - p++; - default: - if (!quoted && isspace(*p)) { - p++; - goto done; - } - *d++ = *p++; - break; - } - } - done: - *d++ = '\0'; - error: - *t = (char *) p; - return (char *) word; -} - -static void -strwordquote(MemBuf * mb, const char *str) -{ - int quoted = 0; - if (strchr(str, ' ')) { - quoted = 1; - memBufAppend(mb, "\"", 1); - } - while (*str) { - int l = strcspn(str, "\"\\"); - memBufAppend(mb, str, l); - str += l; - while (*str == '"' || *str == '\\') { - memBufAppend(mb, "\\", 1); - memBufAppend(mb, str, 1); - str++; - } - } - if (quoted) - memBufAppend(mb, "\"", 1); -} - /* * The helper program receives queries on stdin, one * per line, and must return the result on on stdout as diff --git a/src/protos.h b/src/protos.h index 89226c8c27..56214bab0f 100644 --- a/src/protos.h +++ b/src/protos.h @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.442 2002/07/20 12:30:04 hno Exp $ + * $Id: protos.h,v 1.443 2002/09/07 14:55:24 hno Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -1159,6 +1159,9 @@ extern int isPowTen(int); extern void parseEtcHosts(void); extern int getMyPort(void); +static void strwordquote(MemBuf * mb, const char *str); +void strwordquote(MemBuf * mb, const char *str); + #if USE_HTCP extern void htcpInit(void); extern void htcpQuery(StoreEntry * e, request_t * req, peer * p); @@ -1334,5 +1337,6 @@ typedef void EAH(void *data, void *result); extern void externalAclLookup(aclCheck_t * ch, void *acl_data, EAH * handler, void *data); extern void externalAclInit(void); extern void externalAclShutdown(void); +extern char *strtokFile(void); #endif /* SQUID_PROTOS_H */ diff --git a/src/tools.cc b/src/tools.cc index 244577029f..a784c8ea03 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -1,6 +1,6 @@ /* - * $Id: tools.cc,v 1.221 2002/09/01 15:16:35 hno Exp $ + * $Id: tools.cc,v 1.222 2002/09/07 14:55:24 hno Exp $ * * DEBUG: section 21 Misc Functions * AUTHOR: Harvest Derived @@ -1008,3 +1008,76 @@ getMyPort(void) fatal("No port defined"); return 0; /* NOT REACHED */ } + +/* + * Similar to strtok, but has some rudimentary knowledge + * of quoting + */ +static char * +strwordtok(char *buf, char **t) +{ + unsigned char *word = NULL; + unsigned char *p = (unsigned char *) buf; + unsigned char *d; + unsigned char ch; + int quoted = 0; + if (!p) + p = (unsigned char *) *t; + if (!p) + goto error; + while (*p && isspace(*p)) + p++; + if (!*p) + goto error; + word = d = p; + while ((ch = *p)) { + switch (ch) { + case '\\': + p++; + *d++ = ch = *p; + if (ch) + p++; + break; + case '"': + quoted = !quoted; + p++; + default: + if (!quoted && isspace(*p)) { + p++; + goto done; + } + *d++ = *p++; + break; + } + } + done: + *d++ = '\0'; + error: + *t = (char *) p; + return (char *) word; +} + +/* + * Inverse of strwordtok. Quotes a word if needed + */ +static void +strwordquote(MemBuf * mb, const char *str) +{ + int quoted = 0; + if (strchr(str, ' ')) { + quoted = 1; + memBufAppend(mb, "\"", 1); + } + while (*str) { + int l = strcspn(str, "\"\\"); + memBufAppend(mb, str, l); + str += l; + while (*str == '"' || *str == '\\') { + memBufAppend(mb, "\\", 1); + memBufAppend(mb, str, 1); + str++; + } + } + if (quoted) + memBufAppend(mb, "\"", 1); +}