From: Amos Jeffries Date: Mon, 27 Jul 2015 19:48:37 +0000 (-0700) Subject: purge: Convert squid.conf parser to std::regex X-Git-Tag: M-staged-PR71~362^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2056c6a6b3b59f54afb222f73bcf9486ab6051ff;p=thirdparty%2Fsquid.git purge: Convert squid.conf parser to std::regex --- diff --git a/tools/purge/conffile.cc b/tools/purge/conffile.cc index 84cfb4cc93..0959473272 100644 --- a/tools/purge/conffile.cc +++ b/tools/purge/conffile.cc @@ -47,6 +47,7 @@ #include #include #include +#include #include #include @@ -57,9 +58,6 @@ readConfigFile( CacheDirVector& cachedir, const char* fn, FILE* debug ) // fn (IN): file name of squid.conf to use // returns: number of entries, or negative to warn of errors { - static const char* expression = - "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)"; - // try to open file if ( debug ) fprintf( debug, "# trying to open %s\n", fn ? fn : "(null)" ); std::ifstream cfgin(fn); @@ -69,20 +67,15 @@ readConfigFile( CacheDirVector& cachedir, const char* fn, FILE* debug ) } // prepare regular expression for matching + static const char * expression = "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)"; if ( debug ) fprintf( debug, "# trying to compile \"%s\"\n", expression ); - regex_t rexp; - int result = regcomp( &rexp, expression, REG_EXTENDED ); - if ( result != 0 ) { - char buffer[256]; - regerror( result, &rexp, buffer, sizeof(buffer) ); - fprintf( stderr, "regular expression \"%s\": %s\n", expression, buffer ); - return -1; - } + + static const std::regex rexp(expression, std::regex::extended); // read line by line if ( debug ) fputs( "# trying to read lines\n", debug ); - regmatch_t subs[8]; + std::smatch subs; // set of std::string so we can use == char *s, line[1024]; CacheDir cd; while ( cfgin.getline( line, sizeof(line)) ) { @@ -95,93 +88,62 @@ readConfigFile( CacheDirVector& cachedir, const char* fn, FILE* debug ) if ( *line == '\0' || *line == '\n' ) continue; // test line - if ( (result=regexec( &rexp, line, 7, subs, 0 )) != 0 ) { - // error or no match - if ( result != REG_NOMATCH ) { - char buffer[256]; - regerror( result, &rexp, buffer, sizeof(buffer) ); - fprintf( stderr, "while matching \"%s\" against %s%s\n", - expression, line, buffer ); - regfree(&rexp); - cfgin.close(); - return -1; - } + std::string tmpLine(line); + if (!std::regex_search(tmpLine, subs, rexp)) + continue; + + // match, please record + memset( &cd, 0, sizeof(cd) ); + if ( debug ) fprintf( debug, "# match '%s' on line %s", subs[0].str().c_str(), line); + + // extract information. If 6th parenthesis is filled, this is + // a new squid with disk types, otherwise it is an older version + int offset = 2; + if (subs[6].str().empty()) { + // old version, disk type at position 2 is always UFS + cd.type = CacheDir::CDT_UFS; } else { - // match, please record - memset( &cd, 0, sizeof(cd) ); - if ( debug ) fprintf( debug, "# match from %d-%d on line %s", - (int)subs[0].rm_so, (int)subs[0].rm_eo, - line ); - - // terminate line after matched expression - line[ subs[0].rm_eo ] = '\0'; - - // extract information. If 6th parenthesis is filled, this is - // a new squid with disk types, otherwise it is an older version - int offset = 2; - if ( subs[6].rm_so == -1 ) { - // old version, disk type at position 2 is always UFS + // new version, disk type at position 2 + if ( debug ) fprintf( debug, "# match '%s' in \"%s\"\n", subs[offset].str().c_str(), subs[0].str().c_str()); + static const std::string ufsDir("ufs",3); + static const std::string aufsDir("aufs",4); + static const std::string asyncUfsDir("asyncufs",8); + static const std::string diskdDir("diskd",5); + if (subs[offset] == ufsDir) cd.type = CacheDir::CDT_UFS; - } else { - // new version, disk type at position 2 - line[ subs[offset].rm_eo ] = '\0'; - if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n", - (int)subs[offset].rm_so, - (int)subs[offset].rm_eo, - line+subs[offset].rm_so ); - if ( strcmp( line + subs[offset].rm_so, "ufs" ) == 0 ) - cd.type = CacheDir::CDT_UFS; - else if ( strcmp( line + subs[offset].rm_so, "asyncufs" ) == 0 ) - cd.type = CacheDir::CDT_AUFS; - else if ( strcmp( line + subs[offset].rm_so, "diskd" ) == 0 ) - cd.type = CacheDir::CDT_DISKD; - else - cd.type = CacheDir::CDT_OTHER; - ++offset; - } - - // extract base directory - line[ subs[offset].rm_eo ] = '\0'; - if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n", - (int)subs[offset].rm_so, - (int)subs[offset].rm_eo, - line+subs[offset].rm_so ); - cd.base = xstrdup( line+subs[offset].rm_so ); + else if (subs[offset] == aufsDir || subs[offset] == asyncUfsDir) + cd.type = CacheDir::CDT_AUFS; + else if (subs[offset] == diskdDir) + cd.type = CacheDir::CDT_DISKD; + else + cd.type = CacheDir::CDT_OTHER; ++offset; + } - // extract size information - line[ subs[offset].rm_eo ] = '\0'; - if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n", - (int)subs[offset].rm_so, - (int)subs[offset].rm_eo, - line+subs[offset].rm_so ); - cd.size = strtoul( line+subs[offset].rm_so, 0, 10 ); - ++offset; + // extract base directory + if ( debug ) fprintf( debug, "# match '%s' in \"%s\"\n", subs[offset].str().c_str(), subs[0].str().c_str()); + cd.base = xstrdup(subs[offset].str().c_str()); + ++offset; - // extract 1st level directories - line[ subs[offset].rm_eo ] = '\0'; - if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n", - (int)subs[offset].rm_so, - (int)subs[offset].rm_eo, - line+subs[offset].rm_so ); - cd.level[0] = strtoul( line+subs[offset].rm_so, 0, 10 ); - ++offset; + // extract size information + if ( debug ) fprintf( debug, "# match '%s' in \"%s\"\n", subs[offset].str().c_str(), subs[0].str().c_str()); + cd.size = strtoul(subs[offset].str().c_str(), 0, 10); + ++offset; - // extract 2nd level directories - line[ subs[offset].rm_eo ] = '\0'; - if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n", - (int)subs[offset].rm_so, - (int)subs[offset].rm_eo, - line+subs[offset].rm_so ); - cd.level[1] = strtoul( line+subs[offset].rm_so, 0, 10 ); - ++offset; + // extract 1st level directories + if ( debug ) fprintf( debug, "# match '%s' in \"%s\"\n", subs[offset].str().c_str(), subs[0].str().c_str()); + cd.level[0] = strtoul(subs[offset].str().c_str(), 0, 10); + ++offset; - cachedir.push_back( cd ); - } + // extract 2nd level directories + if ( debug ) fprintf( debug, "# match '%s' in \"%s\"\n", subs[offset].str().c_str(), subs[0].str().c_str()); + cd.level[1] = strtoul(subs[offset].str().c_str(), 0, 10); + ++offset; + + cachedir.push_back( cd ); } cfgin.close(); - regfree(&rexp); return cachedir.size(); }