aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-02-01 19:49:11 +0100
committerJay Berkenbilt <ejb@ql.org>2022-02-01 19:50:58 +0100
commit42bff9f4584362f2084033795896c2e891274631 (patch)
tree4c317d9db160535848b036f47dd646884e1d6ab0
parenta0d9d9923ce397d46680a9b180f253b39135ece2 (diff)
downloadqpdf-42bff9f4584362f2084033795896c2e891274631.tar.zst
QPDFJob: let initializeFromArgv just take argv, not argc
Let argv be a null-terminated array. There is already code that assumes this, and it makes it easier to construct the arguments.
-rw-r--r--examples/qpdf-job.cc2
-rw-r--r--examples/qpdfjob-c.c2
-rw-r--r--include/qpdf/QPDFJob.hh41
-rw-r--r--include/qpdf/qpdfjob-c.h10
-rw-r--r--libqpdf/QPDFJob_argv.cc7
-rw-r--r--libqpdf/qpdfjob-c.cc16
-rw-r--r--qpdf/qpdf.cc2
-rw-r--r--qpdf/qpdfjob-ctest.c4
8 files changed, 49 insertions, 35 deletions
diff --git a/examples/qpdf-job.cc b/examples/qpdf-job.cc
index d1760897..29f23a34 100644
--- a/examples/qpdf-job.cc
+++ b/examples/qpdf-job.cc
@@ -82,7 +82,7 @@ int main(int argc, char* argv[])
nullptr
};
QPDFJob j;
- j.initializeFromArgv(9, new_argv);
+ j.initializeFromArgv(new_argv);
j.run();
std::cout << "out2 status: " << j.getExitCode() << std::endl;
}
diff --git a/examples/qpdfjob-c.c b/examples/qpdfjob-c.c
index da53001a..ac58b8ae 100644
--- a/examples/qpdfjob-c.c
+++ b/examples/qpdfjob-c.c
@@ -57,6 +57,6 @@ int main(int argc, char* argv[])
* qpdfjob_run_from_json instead and pass the json string as a
* single char const* argument.
*/
- r = qpdfjob_run_from_argv(5, new_argv);
+ r = qpdfjob_run_from_argv(new_argv);
return r;
}
diff --git a/include/qpdf/QPDFJob.hh b/include/qpdf/QPDFJob.hh
index 44ec723c..df51828b 100644
--- a/include/qpdf/QPDFJob.hh
+++ b/include/qpdf/QPDFJob.hh
@@ -57,27 +57,28 @@ class QPDFJob
// SETUP FUNCTIONS
- // Initialize a QPDFJob object from argv. The progname_env
- // argument is the name of an environment variable which, if set,
- // overrides the name of the executable for purposes of generating
- // the --completion options. See QPDFArgParser for details. If a
- // null pointer is passed in, the default value of
- // "QPDF_EXECUTABLE" is used. This is used by the QPDF cli, which
- // just initializes a QPDFJob from argv, calls run(), and handles
- // errors and exit status issues. You can perform much of the cli
- // functionality programmatically in this way rather than using
- // the regular API. This is exposed in the C API, which makes it
- // easier to get certain high-level qpdf functionality from other
- // languages. If there are any command-line errors, this method
- // will throw QPDFUsage which is derived from std::runtime_error.
- // Other exceptions may be thrown in some cases. Note that argc,
- // and argv should be UTF-8 encoded. If you are calling this from
- // a Windows Unicode-aware main (wmain), see
- // QUtil::call_main_from_wmain for information about converting
- // arguments to UTF-8. This method will mutate arguments that are
- // passed to it.
+ // Initialize a QPDFJob object from argv, which must be a
+ // null-terminated array of null-terminated UTF-8-encoded C
+ // strings. The progname_env argument is the name of an
+ // environment variable which, if set, overrides the name of the
+ // executable for purposes of generating the --completion options.
+ // See QPDFArgParser for details. If a null pointer is passed in,
+ // the default value of "QPDF_EXECUTABLE" is used. This is used by
+ // the QPDF cli, which just initializes a QPDFJob from argv, calls
+ // run(), and handles errors and exit status issues. You can
+ // perform much of the cli functionality programmatically in this
+ // way rather than using the regular API. This is exposed in the C
+ // API, which makes it easier to get certain high-level qpdf
+ // functionality from other languages. If there are any
+ // command-line errors, this method will throw QPDFUsage which is
+ // derived from std::runtime_error. Other exceptions may be thrown
+ // in some cases. Note that argc, and argv should be UTF-8
+ // encoded. If you are calling this from a Windows Unicode-aware
+ // main (wmain), see QUtil::call_main_from_wmain for information
+ // about converting arguments to UTF-8. This method will mutate
+ // arguments that are passed to it.
QPDF_DLL
- void initializeFromArgv(int argc, char const* const argv[],
+ void initializeFromArgv(char const* const argv[],
char const* progname_env = nullptr);
// Initialize a QPDFJob from json. Passing partial = true prevents
diff --git a/include/qpdf/qpdfjob-c.h b/include/qpdf/qpdfjob-c.h
index 3fc3ec30..0a390877 100644
--- a/include/qpdf/qpdfjob-c.h
+++ b/include/qpdf/qpdfjob-c.h
@@ -47,12 +47,12 @@ extern "C" {
#endif
/* This function does the equivalent of running the qpdf
* command-line with the given arguments and returns the exit code
- * that qpdf would use. Note that arguments must be UTF8-encoded.
- * If calling this from wmain on Windows, use
- * qpdfjob_run_from_wide_argv instead.
+ * that qpdf would use. argv must be a null-terminated array of
+ * null-terminated UTF8-encoded strings. If calling this from
+ * wmain on Windows, use qpdfjob_run_from_wide_argv instead.
*/
QPDF_DLL
- int qpdfjob_run_from_argv(int argc, char const* const argv[]);
+ int qpdfjob_run_from_argv(char const* const argv[]);
#ifndef QPDF_NO_WCHAR_T
/* This function is the same as qpdfjob_run_from_argv except argv
@@ -60,7 +60,7 @@ extern "C" {
* calling from a Windows wmain function.
*/
QPDF_DLL
- int qpdfjob_run_from_wide_argv(int argc, wchar_t const* const argv[]);
+ int qpdfjob_run_from_wide_argv(wchar_t const* const argv[]);
#endif /* QPDF_NO_WCHAR_T */
/* This function runs QPDFJob from a job JSON file. See the "QPDF
diff --git a/libqpdf/QPDFJob_argv.cc b/libqpdf/QPDFJob_argv.cc
index e2870309..e3d4a47b 100644
--- a/libqpdf/QPDFJob_argv.cc
+++ b/libqpdf/QPDFJob_argv.cc
@@ -465,13 +465,18 @@ ArgParser::parseOptions()
}
void
-QPDFJob::initializeFromArgv(int argc, char const* const argv[],
+QPDFJob::initializeFromArgv(char const* const argv[],
char const* progname_env)
{
if (progname_env == nullptr)
{
progname_env = "QPDF_EXECUTABLE";
}
+ int argc = 0;
+ for (auto k = argv; *k; ++k)
+ {
+ ++argc;
+ }
QPDFArgParser qap(argc, argv, progname_env);
setMessagePrefix(qap.getProgname());
ArgParser ap(qap, config());
diff --git a/libqpdf/qpdfjob-c.cc b/libqpdf/qpdfjob-c.cc
index e1c76477..10e13043 100644
--- a/libqpdf/qpdfjob-c.cc
+++ b/libqpdf/qpdfjob-c.cc
@@ -7,7 +7,7 @@
#include <cstdio>
#include <cstring>
-int qpdfjob_run_from_argv(int argc, char const* const argv[])
+int qpdfjob_run_from_argv(char const* const argv[])
{
auto whoami_p = QUtil::make_shared_cstr(argv[0]);
auto whoami = QUtil::getWhoami(whoami_p.get());
@@ -16,7 +16,7 @@ int qpdfjob_run_from_argv(int argc, char const* const argv[])
QPDFJob j;
try
{
- j.initializeFromArgv(argc, argv);
+ j.initializeFromArgv(argv);
j.run();
}
catch (std::exception& e)
@@ -28,9 +28,17 @@ int qpdfjob_run_from_argv(int argc, char const* const argv[])
}
#ifndef QPDF_NO_WCHAR_T
-int qpdfjob_run_from_wide_argv(int argc, wchar_t const* const argv[])
+int qpdfjob_run_from_wide_argv(wchar_t const* const argv[])
{
- return QUtil::call_main_from_wmain(argc, argv, qpdfjob_run_from_argv);
+ int argc = 0;
+ for (auto k = argv; *k; ++k)
+ {
+ ++argc;
+ }
+ return QUtil::call_main_from_wmain(
+ argc, argv, [](int, char const* const new_argv[]) {
+ return qpdfjob_run_from_argv(new_argv);
+ });
}
#endif // QPDF_NO_WCHAR_T
diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc
index d2c576a4..b6805691 100644
--- a/qpdf/qpdf.cc
+++ b/qpdf/qpdf.cc
@@ -43,7 +43,7 @@ int realmain(int argc, char* argv[])
try
{
// See "HOW TO ADD A COMMAND-LINE ARGUMENT" in README-maintainer.
- j.initializeFromArgv(argc, argv);
+ j.initializeFromArgv(argv);
j.run();
}
catch (QPDFUsage& e)
diff --git a/qpdf/qpdfjob-ctest.c b/qpdf/qpdfjob-ctest.c
index 73605f8a..43935333 100644
--- a/qpdf/qpdfjob-ctest.c
+++ b/qpdf/qpdfjob-ctest.c
@@ -13,7 +13,7 @@ static void wide_test()
argv[2] = L"a.pdf";
argv[3] = L"--static-id";
argv[4] = NULL;
- assert(qpdfjob_run_from_wide_argv(4, argv) == 0);
+ assert(qpdfjob_run_from_wide_argv(argv) == 0);
printf("wide test passed\n");
}
#endif // QPDF_NO_WCHAR_T
@@ -28,7 +28,7 @@ static void run_tests()
argv[2] = "a.pdf";
argv[3] = "--deterministic-id";
argv[4] = NULL;
- assert(qpdfjob_run_from_argv(4, argv) == 0);
+ assert(qpdfjob_run_from_argv(argv) == 0);
printf("argv test passed\n");
assert(qpdfjob_run_from_json("{\n\