aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qpdf.cc
blob: bb57e88b2b590fa9b2046498b7443ab800779c18 (plain) (blame)
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
#include <qpdf/QPDFJob.hh>
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

static int constexpr EXIT_ERROR = 2;
static int constexpr EXIT_WARNING = 3;

// For is-encrypted and requires-password
static int constexpr EXIT_IS_NOT_ENCRYPTED = 2;
static int constexpr EXIT_CORRECT_PASSWORD = 3;

static char const* whoami = 0;

static void usageExit(std::string const& msg)
{
    std::cerr
	<< std::endl
	<< whoami << ": " << msg << std::endl
	<< std::endl
	<< "For detailed help, run " << whoami << " --help" << std::endl
	<< std::endl;
    exit(EXIT_ERROR);
}

int realmain(int argc, char* argv[])
{
    whoami = QUtil::getWhoami(argv[0]);
    QUtil::setLineBuf(stdout);

    // Remove prefix added by libtool for consistency during testing.
    if (strncmp(whoami, "lt-", 3) == 0)
    {
	whoami += 3;
    }

    QPDFJob j;

    bool errors = false;
    try
    {
        j.initializeFromArgv(argc, argv);
        j.run();
    }
    catch (QPDFArgParser::Usage& e)
    {
        usageExit(e.what());
    }
    catch (std::exception& e)
    {
	std::cerr << whoami << ": " << e.what() << std::endl;
	errors = true;
    }

    bool warnings = j.hasWarnings();
    if (warnings)
    {
        if (! j.suppressWarnings())
        {
            if (j.createsOutput())
            {
                std::cerr << whoami << ": operation succeeded with warnings;"
                          << " resulting file may have some problems"
                          << std::endl;
            }
            else
            {
                std::cerr << whoami << ": operation succeeded with warnings"
                          << std::endl;
            }
        }
        // Still return with warning code even if warnings were
        // suppressed, so leave warnings == true unless we've been
        // specifically instructed to do otherwise.
        if (j.warningsExitZero())
        {
            warnings = false;
        }
    }

    unsigned long encryption_status = j.getEncryptionStatus();
    if (j.checkIsEncrypted())
    {
        if (encryption_status & qpdf_es_encrypted)
        {
            QTC::TC("qpdf", "qpdf check encrypted encrypted");
            return 0;
        }
        else
        {
            QTC::TC("qpdf", "qpdf check encrypted not encrypted");
            return EXIT_IS_NOT_ENCRYPTED;
        }
    }
    else if (j.checkRequiresPassword())
    {
        if (encryption_status & qpdf_es_encrypted)
        {
            if (encryption_status & qpdf_es_password_incorrect)
            {
                QTC::TC("qpdf", "qpdf check password password incorrect");
                return 0;
            }
            else
            {
                QTC::TC("qpdf", "qpdf check password password correct");
                return EXIT_CORRECT_PASSWORD;
            }
        }
        else
        {
            QTC::TC("qpdf", "qpdf check password not encrypted");
            return EXIT_IS_NOT_ENCRYPTED;
        }
    }

    return (errors ? EXIT_ERROR :
            warnings ? EXIT_WARNING :
            0);
}

#ifdef WINDOWS_WMAIN

extern "C"
int wmain(int argc, wchar_t* argv[])
{
    return QUtil::call_main_from_wmain(argc, argv, realmain);
}

#else

int main(int argc, char* argv[])
{
    return realmain(argc, argv);
}

#endif