- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathUtilSvc.cpp
158 lines (137 loc) · 4.11 KB
/
UtilSvc.cpp
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
/*
* PROGRAM: Firebird utilities interface
* MODULE: UtilSvc.cpp
* DESCRIPTION: Interface making it possible to use same code
* as both utility or service
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Alex Peshkov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2007 Alex Peshkov <peshkoff at mail dot ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
*/
#include"firebird.h"
#include"../common/UtilSvc.h"
#include"../common/classes/alloc.h"
#include"../common/StatusArg.h"
#include"iberror.h"
#include<string.h>
#include<stdarg.h>
namespaceFirebird {
namespace {
voidoutputFile(FILE* f, constvoid* text, size_t len)
{
if (::fwrite(text, 1, len, f) != len)
{
// ASF: If the console is configured to UTF-8 (chcp 65001) with TrueType font, the MSVC
// runtime returns the number of characters (instead of bytes) written and make
// ferror(stdout) return true. So lets not check for errors here.
#ifndef WIN_NT
Firebird::system_call_failed::raise("StandaloneUtilityInterface::output()/fwrite()");
#endif
}
}
voidoutputFile(FILE* std, constchar* text)
{
outputFile(std, text, strlen(text));
fflush(std);
}
}
classStandaloneUtilityInterface : publicUtilSvc
{
public:
StandaloneUtilityInterface(int ac, char** av)
{
while (ac--)
{
argv.push(*av++);
}
}
voidoutputVerbose(constchar* text) override
{
outputFile(usvcDataMode ? stderr : stdout, text);
}
voidoutputError(constchar* text) override
{
outputFile(stderr, text);
}
voidoutputData(constvoid* data, FB_SIZE_T size) override
{
fb_assert(usvcDataMode);
outputFile(stdout, data, size);
}
voidprintf(bool err, const SCHAR* format, ...) override
{
va_list arglist;
va_start(arglist, format);
int rc = ::vfprintf((usvcDataMode || err) ? stderr : stdout, format, arglist);
va_end(arglist);
if (rc < 0)
{
system_call_failed::raise("StandaloneUtilityInterface::printf()/vfprintf()");
}
}
voidhidePasswd(ArgvType& argv, int pos) override
{
constsize_t l = strlen(argv[pos]);
char* data = FB_NEW_POOL(getPool()) char[l + 1];
memcpy(data, argv[pos], l);
data[l] = 0;
// here const-correctness is violated to make the rest 99.9%
// places of code much more clear
char* hide = const_cast<char*>(argv[pos]);
argv[pos] = data;
memset(hide, '*', l);
}
boolisService() override
{
returnfalse;
}
voidcheckService() override
{
status_exception::raise(Arg::Gds(isc_utl_trusted_switch));
}
unsignedintgetAuthBlock(constunsignedchar** bytes) override
{
// Utility has no auth block
*bytes = NULL;
return0;
}
// do nothing for non-service
voidstarted() override { }
voidputLine(char, constchar*) override { }
voidputSLong(char, SLONG) override { }
voidputSInt64(char, SINT64) override { }
voidputChar(char, char) override { }
voidputBytes(const UCHAR*, FB_SIZE_T) override { }
ULONG getBytes(UCHAR*, ULONG) override { return0; }
voidsetServiceStatus(const ISC_STATUS*) override { }
voidsetServiceStatus(const USHORT, const USHORT, const MsgFormat::SafeArg&) override { }
StatusAccessor getStatusAccessor() override { returnStatusAccessor(); }
voidfillDpb(ClumpletWriter&) override { }
boolfinished() override { returnfalse; }
boolutf8FileNames() override { returnfalse; }
Firebird::ICryptKeyCallback* getCryptCallback() override { returnNULL; }
intgetParallelWorkers() override { return0; };
};
UtilSvc* UtilSvc::createStandalone(int ac, char** av)
{
return FB_NEW StandaloneUtilityInterface(ac, av);
}
} // namespace Firebird