2008-01-16 07:11:50 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: Firebird utilities interface
|
|
|
|
* MODULE: UtilSvc.cpp
|
2008-12-05 02:20:14 +01:00
|
|
|
* DESCRIPTION: Interface making it possible to use same code
|
2008-01-16 07:11:50 +01:00
|
|
|
* 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 "iberror.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2010-05-06 15:42:20 +02:00
|
|
|
namespace {
|
|
|
|
void outputFile(FILE* f, const void* 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void outputFile(FILE* std, const char* text)
|
|
|
|
{
|
|
|
|
outputFile(std, text, strlen(text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-16 07:11:50 +01:00
|
|
|
class StandaloneUtilityInterface : public UtilSvc
|
|
|
|
{
|
|
|
|
public:
|
2014-04-25 12:59:34 +02:00
|
|
|
StandaloneUtilityInterface(int ac, char** av)
|
2008-01-16 07:11:50 +01:00
|
|
|
{
|
|
|
|
while (ac--)
|
|
|
|
{
|
|
|
|
argv.push(*av++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 15:42:20 +02:00
|
|
|
void outputVerbose(const char* text)
|
2009-01-16 16:42:33 +01:00
|
|
|
{
|
2010-05-06 15:42:20 +02:00
|
|
|
outputFile(usvcDataMode ? stderr : stdout, text);
|
|
|
|
}
|
2010-05-08 04:25:58 +02:00
|
|
|
|
2010-05-06 15:42:20 +02:00
|
|
|
void outputError(const char* text)
|
|
|
|
{
|
|
|
|
outputFile(stderr, text);
|
2009-01-16 16:42:33 +01:00
|
|
|
}
|
|
|
|
|
2014-07-17 20:48:46 +02:00
|
|
|
void outputData(const void* data, FB_SIZE_T size)
|
2010-05-06 15:42:20 +02:00
|
|
|
{
|
|
|
|
fb_assert(usvcDataMode);
|
|
|
|
outputFile(stdout, data, size);
|
|
|
|
}
|
2010-05-08 04:25:58 +02:00
|
|
|
|
2010-05-06 15:42:20 +02:00
|
|
|
virtual void printf(bool err, const SCHAR* format, ...)
|
2008-01-16 07:11:50 +01:00
|
|
|
{
|
|
|
|
va_list arglist;
|
|
|
|
va_start(arglist, format);
|
2010-05-06 15:42:20 +02:00
|
|
|
int rc = ::vfprintf((usvcDataMode || err) ? stderr : stdout, format, arglist);
|
2008-01-16 07:11:50 +01:00
|
|
|
va_end(arglist);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
2010-05-06 15:42:20 +02:00
|
|
|
system_call_failed::raise("StandaloneUtilityInterface::printf()/vfprintf()");
|
2008-01-16 07:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void hidePasswd(ArgvType& argv, int pos)
|
|
|
|
{
|
|
|
|
const size_t l = strlen(argv[pos]);
|
|
|
|
char* data = FB_NEW(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool isService()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void checkService()
|
|
|
|
{
|
2008-07-03 14:02:54 +02:00
|
|
|
status_exception::raise(Arg::Gds(isc_utl_trusted_switch));
|
2008-01-16 07:11:50 +01:00
|
|
|
}
|
2008-09-05 13:41:00 +02:00
|
|
|
|
2011-10-04 14:51:57 +02:00
|
|
|
virtual unsigned int getAuthBlock(const unsigned char** bytes)
|
|
|
|
{
|
|
|
|
// Utility has no auth block
|
|
|
|
*bytes = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-16 07:11:50 +01:00
|
|
|
// do nothing for non-service
|
|
|
|
virtual void started() { }
|
|
|
|
virtual void putLine(char, const char*) { }
|
|
|
|
virtual void putSLong(char, SLONG) { }
|
|
|
|
virtual void putChar(char, char) { }
|
2014-07-17 20:48:46 +02:00
|
|
|
virtual void putBytes(const UCHAR*, FB_SIZE_T) { }
|
2012-06-20 13:47:30 +02:00
|
|
|
virtual ULONG getBytes(UCHAR*, ULONG) { return 0; }
|
2008-09-11 13:51:25 +02:00
|
|
|
virtual void setServiceStatus(const ISC_STATUS*) { }
|
|
|
|
virtual void setServiceStatus(const USHORT, const USHORT, const MsgFormat::SafeArg&) { }
|
2009-12-18 13:42:26 +01:00
|
|
|
virtual const ISC_STATUS* getStatus() { return 0; }
|
2012-03-15 16:05:31 +01:00
|
|
|
virtual void fillDpb(ClumpletWriter&) { }
|
2014-05-21 15:11:43 +02:00
|
|
|
virtual bool finished() { return false; }
|
2009-12-18 13:42:26 +01:00
|
|
|
virtual void initStatus() { }
|
2014-05-21 15:11:43 +02:00
|
|
|
virtual bool utf8FileNames() { return false; }
|
2008-01-16 07:11:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
UtilSvc* UtilSvc::createStandalone(int ac, char** av)
|
|
|
|
{
|
2008-02-02 19:16:04 +01:00
|
|
|
return new StandaloneUtilityInterface(ac, av);
|
2008-01-16 07:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Firebird
|