-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
csgrep --file-glob: expand glob patterns
... in the names of input files. If we have too many input files, we cannot rely on the glob expansion implemented in the shell because the expanded command might exceed the maximum command line length. Running `csgrep` for multiple batches of input files is difficult when we use a JSON-based output format, which cannot be concatenated as plain text. Related: csutils/csmock#187
- Loading branch information
Showing
7 changed files
with
99,875 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* This file is part of csdiff. | ||
* | ||
* csdiff is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
* | ||
* csdiff is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with csdiff. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "glob-expand.hh" | ||
|
||
#include <glob.h> | ||
|
||
bool globExpand(TStringList *pDst, const std::string &globPat) | ||
{ | ||
// use POSIX glob() to expand the glob pattern | ||
glob_t results; | ||
results.gl_pathc = 0U; | ||
const bool success = !glob(globPat.c_str(), 0, nullptr, &results); | ||
|
||
// store the results to *pDst | ||
for (unsigned i = 0U; i < results.gl_pathc; ++i) | ||
pDst->push_back(results.gl_pathv[i]); | ||
|
||
// free the original data structure returned by glob() | ||
globfree(&results); | ||
return success; | ||
} | ||
|
||
bool globExpand(TStringList *pDstSrc) | ||
{ | ||
bool success = true; | ||
|
||
// expand all globs one by one | ||
TStringList dst; | ||
for (const auto &globPat : *pDstSrc) | ||
success &= globExpand(&dst, globPat); | ||
|
||
// store the results to *pDstSrc | ||
*pDstSrc = std::move(dst); | ||
return success; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* This file is part of csdiff. | ||
* | ||
* csdiff is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
* | ||
* csdiff is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with csdiff. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef H_GUARD_GLOB_EXPAND_H | ||
#define H_GUARD_GLOB_EXPAND_H | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
using TStringList = std::vector<std::string>; | ||
|
||
/// expand the `globPat` pattern and store resulting file names to `*pDst` | ||
bool globExpand(TStringList *pDst, const std::string &globPat); | ||
|
||
/// expand all glob patterns in `*pDstSrc` and store results to `*pDstSrc` | ||
bool globExpand(TStringList *pDstSrc); | ||
|
||
#endif /* H_GUARD_GLOB_EXPAND_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--mode=json --file-glob "$PROJECT_ROOT/tests/csgrep/000[4-9]-*-stdin.txt" "$PROJECT_ROOT/tests/csgrep/000?-*-stdout.txt" |
Oops, something went wrong.