Commit b42f3e1d15203927776a5b0954782287c901300f
1 parent
cc5485da
Move more code from qpdf.cc into QPDFJob
Showing
4 changed files
with
88 additions
and
115 deletions
include/qpdf/QPDFJob.hh
| @@ -43,6 +43,13 @@ class QPDFWriter; | @@ -43,6 +43,13 @@ class QPDFWriter; | ||
| 43 | class QPDFJob | 43 | class QPDFJob |
| 44 | { | 44 | { |
| 45 | public: | 45 | public: |
| 46 | + // Exit codes -- returned by getExitCode() after calling run() | ||
| 47 | + static int constexpr EXIT_ERROR = 2; | ||
| 48 | + static int constexpr EXIT_WARNING = 3; | ||
| 49 | + // For is-encrypted and requires-password | ||
| 50 | + static int constexpr EXIT_IS_NOT_ENCRYPTED = 2; | ||
| 51 | + static int constexpr EXIT_CORRECT_PASSWORD = 3; | ||
| 52 | + | ||
| 46 | // QPDFUsage is thrown if there are any usage-like errors when | 53 | // QPDFUsage is thrown if there are any usage-like errors when |
| 47 | // calling Config methods. | 54 | // calling Config methods. |
| 48 | QPDF_DLL | 55 | QPDF_DLL |
| @@ -345,17 +352,6 @@ class QPDFJob | @@ -345,17 +352,6 @@ class QPDFJob | ||
| 345 | QPDF_DLL | 352 | QPDF_DLL |
| 346 | std::shared_ptr<Config> config(); | 353 | std::shared_ptr<Config> config(); |
| 347 | 354 | ||
| 348 | - // Options for helping the qpdf CLI use the correct edit code and | ||
| 349 | - // properly report warnings. | ||
| 350 | - QPDF_DLL | ||
| 351 | - bool suppressWarnings(); | ||
| 352 | - QPDF_DLL | ||
| 353 | - bool warningsExitZero(); | ||
| 354 | - QPDF_DLL | ||
| 355 | - bool checkRequiresPassword(); | ||
| 356 | - QPDF_DLL | ||
| 357 | - bool checkIsEncrypted(); | ||
| 358 | - | ||
| 359 | // Execute the job | 355 | // Execute the job |
| 360 | QPDF_DLL | 356 | QPDF_DLL |
| 361 | void run(); | 357 | void run(); |
| @@ -364,7 +360,17 @@ class QPDFJob | @@ -364,7 +360,17 @@ class QPDFJob | ||
| 364 | // run() is called. | 360 | // run() is called. |
| 365 | 361 | ||
| 366 | QPDF_DLL | 362 | QPDF_DLL |
| 367 | - bool hasWarnings(); | 363 | + bool hasWarnings() const; |
| 364 | + | ||
| 365 | + // Return one of the EXIT_* constants defined at the top of the | ||
| 366 | + // class declaration. This may be called after run() when run() | ||
| 367 | + // did not throw an exception. Takes into consideration whether | ||
| 368 | + // isEncrypted or requiresPassword was called. Note that this | ||
| 369 | + // function does not know whether run() threw an exception, so | ||
| 370 | + // code that uses this to determine how to exit should explicitly | ||
| 371 | + // use EXIT_ERROR if run() threw an exception. | ||
| 372 | + QPDF_DLL | ||
| 373 | + int getExitCode() const; | ||
| 368 | 374 | ||
| 369 | // Return value is bitwise OR of values from qpdf_encryption_status_e | 375 | // Return value is bitwise OR of values from qpdf_encryption_status_e |
| 370 | QPDF_DLL | 376 | QPDF_DLL |
libqpdf/QPDFJob.cc
| @@ -629,10 +629,28 @@ QPDFJob::run() | @@ -629,10 +629,28 @@ QPDFJob::run() | ||
| 629 | { | 629 | { |
| 630 | this->m->warnings = true; | 630 | this->m->warnings = true; |
| 631 | } | 631 | } |
| 632 | + if (this->m->warnings && (! this->m->suppress_warnings)) | ||
| 633 | + { | ||
| 634 | + if (createsOutput()) | ||
| 635 | + { | ||
| 636 | + (*this->m->cerr) | ||
| 637 | + << this->m->message_prefix | ||
| 638 | + << ": operation succeeded with warnings;" | ||
| 639 | + << " resulting file may have some problems" | ||
| 640 | + << std::endl; | ||
| 641 | + } | ||
| 642 | + else | ||
| 643 | + { | ||
| 644 | + (*this->m->cerr) | ||
| 645 | + << this->m->message_prefix | ||
| 646 | + << ": operation succeeded with warnings" | ||
| 647 | + << std::endl; | ||
| 648 | + } | ||
| 649 | + } | ||
| 632 | } | 650 | } |
| 633 | 651 | ||
| 634 | bool | 652 | bool |
| 635 | -QPDFJob::hasWarnings() | 653 | +QPDFJob::hasWarnings() const |
| 636 | { | 654 | { |
| 637 | return this->m->warnings; | 655 | return this->m->warnings; |
| 638 | } | 656 | } |
| @@ -643,6 +661,51 @@ QPDFJob::createsOutput() const | @@ -643,6 +661,51 @@ QPDFJob::createsOutput() const | ||
| 643 | return ((m->outfilename != nullptr) || m->replace_input); | 661 | return ((m->outfilename != nullptr) || m->replace_input); |
| 644 | } | 662 | } |
| 645 | 663 | ||
| 664 | +int | ||
| 665 | +QPDFJob::getExitCode() const | ||
| 666 | +{ | ||
| 667 | + if (this->m->check_is_encrypted) | ||
| 668 | + { | ||
| 669 | + if (this->m->encryption_status & qpdf_es_encrypted) | ||
| 670 | + { | ||
| 671 | + QTC::TC("qpdf", "qpdf check encrypted encrypted"); | ||
| 672 | + return 0; | ||
| 673 | + } | ||
| 674 | + else | ||
| 675 | + { | ||
| 676 | + QTC::TC("qpdf", "qpdf check encrypted not encrypted"); | ||
| 677 | + return EXIT_IS_NOT_ENCRYPTED; | ||
| 678 | + } | ||
| 679 | + } | ||
| 680 | + else if (this->m->check_requires_password) | ||
| 681 | + { | ||
| 682 | + if (this->m->encryption_status & qpdf_es_encrypted) | ||
| 683 | + { | ||
| 684 | + if (this->m->encryption_status & qpdf_es_password_incorrect) | ||
| 685 | + { | ||
| 686 | + QTC::TC("qpdf", "qpdf check password password incorrect"); | ||
| 687 | + return 0; | ||
| 688 | + } | ||
| 689 | + else | ||
| 690 | + { | ||
| 691 | + QTC::TC("qpdf", "qpdf check password password correct"); | ||
| 692 | + return EXIT_CORRECT_PASSWORD; | ||
| 693 | + } | ||
| 694 | + } | ||
| 695 | + else | ||
| 696 | + { | ||
| 697 | + QTC::TC("qpdf", "qpdf check password not encrypted"); | ||
| 698 | + return EXIT_IS_NOT_ENCRYPTED; | ||
| 699 | + } | ||
| 700 | + } | ||
| 701 | + | ||
| 702 | + if (this->m->warnings && (! this->m->warnings_exit_zero)) | ||
| 703 | + { | ||
| 704 | + return EXIT_WARNING; | ||
| 705 | + } | ||
| 706 | + return 0; | ||
| 707 | +} | ||
| 708 | + | ||
| 646 | void | 709 | void |
| 647 | QPDFJob::checkConfiguration() | 710 | QPDFJob::checkConfiguration() |
| 648 | { | 711 | { |
| @@ -726,30 +789,6 @@ QPDFJob::checkConfiguration() | @@ -726,30 +789,6 @@ QPDFJob::checkConfiguration() | ||
| 726 | } | 789 | } |
| 727 | } | 790 | } |
| 728 | 791 | ||
| 729 | -bool | ||
| 730 | -QPDFJob::suppressWarnings() | ||
| 731 | -{ | ||
| 732 | - return this->m->suppress_warnings; | ||
| 733 | -} | ||
| 734 | - | ||
| 735 | -bool | ||
| 736 | -QPDFJob::warningsExitZero() | ||
| 737 | -{ | ||
| 738 | - return this->m->warnings_exit_zero; | ||
| 739 | -} | ||
| 740 | - | ||
| 741 | -bool | ||
| 742 | -QPDFJob::checkRequiresPassword() | ||
| 743 | -{ | ||
| 744 | - return this->m->check_requires_password; | ||
| 745 | -} | ||
| 746 | - | ||
| 747 | -bool | ||
| 748 | -QPDFJob::checkIsEncrypted() | ||
| 749 | -{ | ||
| 750 | - return this->m->check_is_encrypted; | ||
| 751 | -} | ||
| 752 | - | ||
| 753 | unsigned long | 792 | unsigned long |
| 754 | QPDFJob::getEncryptionStatus() | 793 | QPDFJob::getEncryptionStatus() |
| 755 | { | 794 | { |
qpdf/qpdf.cc
| 1 | #include <qpdf/QPDFJob.hh> | 1 | #include <qpdf/QPDFJob.hh> |
| 2 | -#include <qpdf/QTC.hh> | ||
| 3 | #include <qpdf/QUtil.hh> | 2 | #include <qpdf/QUtil.hh> |
| 4 | #include <qpdf/QPDFUsage.hh> | 3 | #include <qpdf/QPDFUsage.hh> |
| 5 | 4 | ||
| @@ -8,13 +7,6 @@ | @@ -8,13 +7,6 @@ | ||
| 8 | #include <cstring> | 7 | #include <cstring> |
| 9 | #include <iostream> | 8 | #include <iostream> |
| 10 | 9 | ||
| 11 | -static int constexpr EXIT_ERROR = 2; | ||
| 12 | -static int constexpr EXIT_WARNING = 3; | ||
| 13 | - | ||
| 14 | -// For is-encrypted and requires-password | ||
| 15 | -static int constexpr EXIT_IS_NOT_ENCRYPTED = 2; | ||
| 16 | -static int constexpr EXIT_CORRECT_PASSWORD = 3; | ||
| 17 | - | ||
| 18 | static char const* whoami = 0; | 10 | static char const* whoami = 0; |
| 19 | 11 | ||
| 20 | static void usageExit(std::string const& msg) | 12 | static void usageExit(std::string const& msg) |
| @@ -33,7 +25,7 @@ static void usageExit(std::string const& msg) | @@ -33,7 +25,7 @@ static void usageExit(std::string const& msg) | ||
| 33 | << " " << whoami << "--help general help and a topic list" | 25 | << " " << whoami << "--help general help and a topic list" |
| 34 | << std::endl | 26 | << std::endl |
| 35 | << std::endl; | 27 | << std::endl; |
| 36 | - exit(EXIT_ERROR); | 28 | + exit(QPDFJob::EXIT_ERROR); |
| 37 | } | 29 | } |
| 38 | 30 | ||
| 39 | int realmain(int argc, char* argv[]) | 31 | int realmain(int argc, char* argv[]) |
| @@ -48,8 +40,6 @@ int realmain(int argc, char* argv[]) | @@ -48,8 +40,6 @@ int realmain(int argc, char* argv[]) | ||
| 48 | } | 40 | } |
| 49 | 41 | ||
| 50 | QPDFJob j; | 42 | QPDFJob j; |
| 51 | - | ||
| 52 | - bool errors = false; | ||
| 53 | try | 43 | try |
| 54 | { | 44 | { |
| 55 | // See "HOW TO ADD A COMMAND-LINE ARGUMENT" in README-maintainer. | 45 | // See "HOW TO ADD A COMMAND-LINE ARGUMENT" in README-maintainer. |
| @@ -63,74 +53,9 @@ int realmain(int argc, char* argv[]) | @@ -63,74 +53,9 @@ int realmain(int argc, char* argv[]) | ||
| 63 | catch (std::exception& e) | 53 | catch (std::exception& e) |
| 64 | { | 54 | { |
| 65 | std::cerr << whoami << ": " << e.what() << std::endl; | 55 | std::cerr << whoami << ": " << e.what() << std::endl; |
| 66 | - errors = true; | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - bool warnings = j.hasWarnings(); | ||
| 70 | - if (warnings) | ||
| 71 | - { | ||
| 72 | - if (! j.suppressWarnings()) | ||
| 73 | - { | ||
| 74 | - if (j.createsOutput()) | ||
| 75 | - { | ||
| 76 | - std::cerr << whoami << ": operation succeeded with warnings;" | ||
| 77 | - << " resulting file may have some problems" | ||
| 78 | - << std::endl; | ||
| 79 | - } | ||
| 80 | - else | ||
| 81 | - { | ||
| 82 | - std::cerr << whoami << ": operation succeeded with warnings" | ||
| 83 | - << std::endl; | ||
| 84 | - } | ||
| 85 | - } | ||
| 86 | - // Still return with warning code even if warnings were | ||
| 87 | - // suppressed, so leave warnings == true unless we've been | ||
| 88 | - // specifically instructed to do otherwise. | ||
| 89 | - if (j.warningsExitZero()) | ||
| 90 | - { | ||
| 91 | - warnings = false; | ||
| 92 | - } | 56 | + return QPDFJob::EXIT_ERROR; |
| 93 | } | 57 | } |
| 94 | - | ||
| 95 | - unsigned long encryption_status = j.getEncryptionStatus(); | ||
| 96 | - if (j.checkIsEncrypted()) | ||
| 97 | - { | ||
| 98 | - if (encryption_status & qpdf_es_encrypted) | ||
| 99 | - { | ||
| 100 | - QTC::TC("qpdf", "qpdf check encrypted encrypted"); | ||
| 101 | - return 0; | ||
| 102 | - } | ||
| 103 | - else | ||
| 104 | - { | ||
| 105 | - QTC::TC("qpdf", "qpdf check encrypted not encrypted"); | ||
| 106 | - return EXIT_IS_NOT_ENCRYPTED; | ||
| 107 | - } | ||
| 108 | - } | ||
| 109 | - else if (j.checkRequiresPassword()) | ||
| 110 | - { | ||
| 111 | - if (encryption_status & qpdf_es_encrypted) | ||
| 112 | - { | ||
| 113 | - if (encryption_status & qpdf_es_password_incorrect) | ||
| 114 | - { | ||
| 115 | - QTC::TC("qpdf", "qpdf check password password incorrect"); | ||
| 116 | - return 0; | ||
| 117 | - } | ||
| 118 | - else | ||
| 119 | - { | ||
| 120 | - QTC::TC("qpdf", "qpdf check password password correct"); | ||
| 121 | - return EXIT_CORRECT_PASSWORD; | ||
| 122 | - } | ||
| 123 | - } | ||
| 124 | - else | ||
| 125 | - { | ||
| 126 | - QTC::TC("qpdf", "qpdf check password not encrypted"); | ||
| 127 | - return EXIT_IS_NOT_ENCRYPTED; | ||
| 128 | - } | ||
| 129 | - } | ||
| 130 | - | ||
| 131 | - return (errors ? EXIT_ERROR : | ||
| 132 | - warnings ? EXIT_WARNING : | ||
| 133 | - 0); | 58 | + return j.getExitCode(); |
| 134 | } | 59 | } |
| 135 | 60 | ||
| 136 | #ifdef WINDOWS_WMAIN | 61 | #ifdef WINDOWS_WMAIN |
qpdf/test_driver.cc
| @@ -3182,6 +3182,9 @@ static void test_84(QPDF& pdf, char const* arg2) | @@ -3182,6 +3182,9 @@ static void test_84(QPDF& pdf, char const* arg2) | ||
| 3182 | ->objectStreams("preserve") | 3182 | ->objectStreams("preserve") |
| 3183 | ->checkConfiguration(); | 3183 | ->checkConfiguration(); |
| 3184 | j.run(); | 3184 | j.run(); |
| 3185 | + assert(j.getExitCode() == 0); | ||
| 3186 | + assert(! j.hasWarnings()); | ||
| 3187 | + assert(j.getEncryptionStatus() == 0); | ||
| 3185 | } | 3188 | } |
| 3186 | 3189 | ||
| 3187 | std::cout << "error caught by check" << std::endl; | 3190 | std::cout << "error caught by check" << std::endl; |