1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/******************************************************************************
*
* Project: GDAL
* Purpose: CLI front-end
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2024, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdalalgorithm.h"
#include "commonutils.h"
#include "cpl_error.h"
#include "gdal.h"
#include <cassert>
#include <utility>
// #define DEBUG_COMPLETION
/************************************************************************/
/* EmitCompletion() */
/************************************************************************/
/** Return on stdout a space-separated list of choices for bash completion */
static void EmitCompletion(std::unique_ptr<GDALAlgorithm> rootAlg,
const std::vector<std::string> &argsIn,
bool lastWordIsComplete)
{
#ifdef DEBUG_COMPLETION
for (size_t i = 0; i < argsIn.size(); ++i)
fprintf(stderr, "arg[%d]='%s'\n", static_cast<int>(i),
argsIn[i].c_str());
#endif
std::vector<std::string> args = argsIn;
std::string ret;
const auto addSpace = [&ret]()
{
if (!ret.empty())
ret += " ";
};
if (!args.empty() &&
(args.back() == "--config" ||
STARTS_WITH(args.back().c_str(), "--config=") ||
(args.size() >= 2 && args[args.size() - 2] == "--config")))
{
if (args.back() == "--config=" || args.back().back() != '=')
{
CPLStringList aosConfigOptions(CPLGetKnownConfigOptions());
for (const char *pszOpt : cpl::Iterate(aosConfigOptions))
{
addSpace();
ret += pszOpt;
ret += '=';
}
printf("%s", ret.c_str());
}
return;
}
for (const auto &choice : rootAlg->GetAutoComplete(
args, lastWordIsComplete, /*showAllOptions = */ true))
{
addSpace();
ret += CPLString(choice).replaceAll(" ", "\\ ");
}
#ifdef DEBUG_COMPLETION
fprintf(stderr, "ret = '%s'\n", ret.c_str());
#endif
if (!ret.empty())
printf("%s", ret.c_str());
}
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(argc, argv)
{
const bool bIsCompletion = argc >= 3 && strcmp(argv[1], "completion") == 0;
if (bIsCompletion)
{
CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
EarlySetConfigOptions(argc, argv);
}
else
{
EarlySetConfigOptions(argc, argv);
for (int i = 1; i < argc; ++i)
{
// Used by gdal raster tile --parallel-method=spawn to pass
// config options in a stealth way
if (strcmp(argv[i], "--config-options-in-stdin") == 0)
{
std::string line;
constexpr int LINE_SIZE = 10 * 1024;
line.resize(LINE_SIZE);
while (fgets(line.data(), LINE_SIZE, stdin))
{
if (strcmp(line.c_str(), "--config\n") == 0 &&
fgets(line.data(), LINE_SIZE, stdin))
{
std::string osLine(line.c_str());
if (!osLine.empty() && osLine.back() == '\n')
{
osLine.pop_back();
char *pszUnescaped = CPLUnescapeString(
osLine.c_str(), nullptr, CPLES_URL);
char *pszKey = nullptr;
const char *pszValue =
CPLParseNameValue(pszUnescaped, &pszKey);
if (pszKey && pszValue)
{
CPLSetConfigOption(pszKey, pszValue);
}
CPLFree(pszKey);
CPLFree(pszUnescaped);
}
}
else if (strcmp(line.c_str(), "END_CONFIG\n") == 0)
{
break;
}
}
break;
}
}
}
auto alg = GDALGlobalAlgorithmRegistry::GetSingleton().Instantiate(
GDALGlobalAlgorithmRegistry::ROOT_ALG_NAME);
assert(alg);
// Register GDAL drivers
GDALAllRegister();
alg->SetCalledFromCommandLine();
if (bIsCompletion)
{
const bool bLastWordIsComplete =
EQUAL(argv[argc - 1], "last_word_is_complete=true");
if (STARTS_WITH(argv[argc - 1], "last_word_is_complete="))
--argc;
else if (argc >= 2 && STARTS_WITH(argv[argc - 2], "prev=") &&
STARTS_WITH(argv[argc - 1], "cur="))
{
const char *pszPrevVal = argv[argc - 2] + strlen("prev=");
const char *pszCurVal = argv[argc - 1] + strlen("cur=");
std::string osCurVal;
const bool bIsPrevValEqual = (strcmp(pszPrevVal, "=") == 0);
if (bIsPrevValEqual)
{
osCurVal = std::string("=").append(pszCurVal);
pszCurVal = osCurVal.c_str();
}
int iMatch = 0;
for (int i = 3; i < argc - 1; ++i)
{
if (bIsPrevValEqual ? (strstr(argv[i], pszCurVal) != nullptr)
: (strcmp(argv[i], pszCurVal) == 0))
{
if (iMatch == 0)
iMatch = i;
else
iMatch = -1;
}
}
if (iMatch > 0)
argc = iMatch + 1;
else
argc -= 2;
}
// Process lines like "gdal completion gdal raster last_word_is_complete=true|false"
EmitCompletion(std::move(alg),
std::vector<std::string>(argv + 3, argv + argc),
bLastWordIsComplete);
return 0;
}
// Prevent GDALGeneralCmdLineProcessor() to process --format XXX, unless
// "gdal" is invoked only with it. Cf #12411
std::vector<std::pair<char **, char *>> apOrigFormat;
constexpr const char *pszFormatReplaced = "--format-XXXX";
if (!(argc == 3 && strcmp(argv[1], "--format") == 0))
{
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "--format") == 0)
{
apOrigFormat.emplace_back(argv + i, argv[i]);
argv[i] = const_cast<char *>(pszFormatReplaced);
}
}
}
// Process generic cmomand options
argc = GDALGeneralCmdLineProcessor(
argc, &argv, GDAL_OF_RASTER | GDAL_OF_VECTOR | GDAL_OF_MULTIDIM_RASTER);
for (auto &pair : apOrigFormat)
{
*(pair.first) = pair.second;
}
if (argc < 1)
return (-argc);
std::vector<std::string> args;
for (int i = 1; i < argc; ++i)
args.push_back(strcmp(argv[i], pszFormatReplaced) == 0 ? "--format"
: argv[i]);
CSLDestroy(argv);
if (!alg->ParseCommandLineArguments(args))
{
if (strstr(CPLGetLastErrorMsg(), "Do you mean") == nullptr &&
strstr(CPLGetLastErrorMsg(), "Should be one among") == nullptr &&
strstr(CPLGetLastErrorMsg(), "Potential values for argument") ==
nullptr &&
strstr(CPLGetLastErrorMsg(),
"Single potential value for argument") == nullptr)
{
fprintf(stderr, "%s", alg->GetUsageForCLI(true).c_str());
}
return 1;
}
{
const auto stdoutArg =
alg->GetActualAlgorithm().GetArg(GDAL_ARG_NAME_STDOUT);
if (stdoutArg && stdoutArg->GetType() == GAAT_BOOLEAN)
stdoutArg->Set(true);
}
GDALProgressFunc pfnProgress =
alg->IsProgressBarRequested() ? GDALTermProgress : nullptr;
void *pProgressData = nullptr;
int ret = (alg->Run(pfnProgress, pProgressData) && alg->Finalize()) ? 0 : 1;
const auto outputArg =
alg->GetActualAlgorithm().GetArg(GDAL_ARG_NAME_OUTPUT_STRING);
if (outputArg && outputArg->GetType() == GAAT_STRING &&
outputArg->IsOutput())
{
printf("%s", outputArg->Get<std::string>().c_str());
}
const auto retCodeArg = alg->GetActualAlgorithm().GetArg("return-code");
if (retCodeArg && retCodeArg->GetType() == GAAT_INTEGER &&
retCodeArg->IsOutput())
{
ret = retCodeArg->Get<int>();
}
return ret;
}
MAIN_END
|