-
Notifications
You must be signed in to change notification settings - Fork 9
/
aya5.cpp
507 lines (406 loc) · 11.2 KB
/
aya5.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//
// AYA version 5
//
// written by umeici. 2004
//
#if defined(WIN32) || defined(_WIN32_WCE)
#include "stdafx.h"
#include <io.h>
#include <fcntl.h>
#endif
#include <vector>
#include <ctime>
#include <locale>
#include <clocale>
#include <locale>
#include <stdio.h>
#include "aya5.h"
#include "basis.h"
#include "ayavm.h"
#include "ccct.h"
#include "manifest.h"
#include "messages.h"
#include "misc.h"
class CAyaVMWrapper;
static std::vector<CAyaVMWrapper*> vm;
static yaya::string_t modulename;
static std::vector<void (*)(const yaya::char_t *str, int mode, int id)> loghandler_list;
static size_t id_now=0;
static long logsend_hwnd = 0;
#if defined(WIN32)
void* g_hModule = NULL;
#endif
//////////DEBUG/////////////////////////
#ifdef _WINDOWS
#ifdef _DEBUG
#include <crtdbg.h>
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#endif
////////////////////////////////////////
class CAyaVMWrapper {
private:
CAyaVM *vm;
public:
CAyaVMWrapper(const yaya::string_t &path, yaya::global_t h, long len) {
vm = new CAyaVM();
if (logsend_hwnd != 0) {
SetLogRcvWnd(logsend_hwnd);
logsend_hwnd = 0;
}
vm->logger().Set_loghandler(loghandler_list[id_now]);
vm->basis().SetModuleName(modulename,L"",L"normal");
vm->load();
vm->basis().SetPath(h, len);
vm->basis().Configure();
if ( vm->basis().IsSuppress() ) {
vm->logger().Message(10,E_E);
CAyaVM *vme = new CAyaVM();
vme->logger().Set_loghandler(loghandler_list[id_now]);
vme->basis().SetModuleName(modulename,L"_emerg",L"emergency");
vme->load();
vme->basis().SetPath(h, len);
vme->basis().Configure();
vme->logger().Message(11,E_E);
if( ! vme->basis().IsSuppress() ) {
vme->logger().AppendErrorLogHistoryToBegin(std_move(vm->logger().GetErrorLogHistory())); //エラーログを引き継ぐ
std::swap(vm, vme);
}
delete vme;
}
vm->basis().ExecuteLoad();
}
virtual ~CAyaVMWrapper() {
vm->basis().Termination();
vm->unload();
delete vm;
}
void Set_loghandler(void (*loghandler)(const yaya::char_t *str, int mode, int id)){
vm->logger().Set_loghandler(loghandler);
}
bool IsSuppress(void) {
if ( ! vm ) { return true; }
return vm->basis().IsSuppress() != 0;
}
bool IsEmergency(void) {
if( ! vm ) { return false; }
return !wcscmp(vm->basis().GetModeName(),L"emergency");
}
yaya::global_t ExecuteRequest(yaya::global_t h, long *len, bool is_debug)
{
if ( ! vm ) { return NULL; }
vm->request_before();
yaya::global_t r = vm->basis().ExecuteRequest(h,len,is_debug);
vm->request_after();
return r;
}
void SetLogRcvWnd(long hwnd)
{
if ( ! vm ) { return; }
#if defined(WIN32)
vm->basis().SetLogRcvWnd(hwnd);
#endif
}
};
class CAyaVMPrepare {
public:
CAyaVMPrepare(void) {
vm.clear();
vm.emplace_back(nullptr); //0番VM=loadなど従来関数で使う標準
}
~CAyaVMPrepare(void) {
size_t n = vm.size();
for ( size_t i = 0 ; i < n ; ++i ) {
if ( vm[i] ) {
delete vm[i];
}
}
}
};
static CAyaVMPrepare prepare; //これはコンストラクタ・デストラクタ作動用
/* -----------------------------------------------------------------------
* DllMain
* -----------------------------------------------------------------------
*/
#if defined(WIN32)
static void AYA_InitModule(HMODULE hModule)
{
#ifdef _DEBUG
int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
tmpFlag |= _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF;
tmpFlag &= ~_CRTDBG_CHECK_CRT_DF;
_CrtSetDbgFlag( tmpFlag );
#endif
g_hModule = hModule;
if ( IsUnicodeAware() ) {
wchar_t path[MAX_PATH] = L"";
::GetModuleFileNameW(hModule, path, sizeof(path) / sizeof(path[0]));
wchar_t drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
_wsplitpath(path, drive, dir, fname, ext);
modulename = fname;
}
else {
char path[MAX_PATH] = "";
::GetModuleFileNameA(hModule, path, sizeof(path));
char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
_splitpath(path, drive, dir, fname, ext);
std::string mbmodulename = fname;
Ccct::MbcsToUcs2Buf(modulename, mbmodulename, CHARSET_DEFAULT);
}
Ccct::sys_setlocale(LC_ALL);
}
#endif //win32
#if !defined(AYA_MAKE_EXE)
#if defined(WIN32)
extern "C" BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
{
// モジュールの主ファイル名を取得
// NT系ではいきなりUNICODEで取得できるが、9x系を考慮してMBCSで取得してからUCS-2へ変換
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
AYA_InitModule(hModule);
}
return TRUE;
}
#endif //win32
#endif //aya_make_exe
inline void enlarge_loghandler_list(size_t size){
loghandler_list.reserve(size);
while(loghandler_list.size()<size)
loghandler_list.emplace_back(nullptr);
}
/* -----------------------------------------------------------------------
* load
* -----------------------------------------------------------------------
*/
extern "C" DLLEXPORT BOOL_TYPE FUNCATTRIB load(yaya::global_t h, long len)
{
if ( vm[0] ) { delete vm[0]; }
id_now=0;
enlarge_loghandler_list(1);
vm[0] = new CAyaVMWrapper(modulename,h,len);
#if defined(WIN32) || defined(_WIN32_WCE)
::GlobalFree(h);
#elif defined(POSIX)
free(h);
#endif
return 1;
}
extern "C" DLLEXPORT long FUNCATTRIB multi_load(yaya::global_t h, long len)
{
long id = 0;
long n = (long)vm.size();
for ( long i = 1 ; i < n ; ++i ) { //1から 0番は従来用
if ( vm[i] == NULL ) {
id = i;
}
}
if ( id <= 0 ) {
vm.emplace_back(nullptr);
id = (long)vm.size() - 1;
}
enlarge_loghandler_list(id+1);
id_now=id;
vm[id] = new CAyaVMWrapper(modulename,h,len);
#if defined(WIN32) || defined(_WIN32_WCE)
::GlobalFree(h);
#elif defined(POSIX)
free(h);
#endif
return id;
}
/* -----------------------------------------------------------------------
* unload
* -----------------------------------------------------------------------
*/
extern "C" DLLEXPORT BOOL_TYPE FUNCATTRIB unload()
{
if ( vm[0] ) {
delete vm[0];
vm[0] = NULL;
}
return 1;
}
extern "C" DLLEXPORT BOOL_TYPE FUNCATTRIB multi_unload(long id)
{
if ( id <= 0 || id > (long)vm.size() || vm[id] == NULL ) { //1から 0番は従来用
return 0;
}
delete vm[id];
vm[id] = NULL;
return 1;
}
/* -----------------------------------------------------------------------
* request
* -----------------------------------------------------------------------
*/
extern "C" DLLEXPORT yaya::global_t FUNCATTRIB request(yaya::global_t h, long *len)
{
if ( vm[0] ) {
return vm[0]->ExecuteRequest(h, len, false);
}
else {
return NULL;
}
}
extern "C" DLLEXPORT yaya::global_t FUNCATTRIB multi_request(long id, yaya::global_t h, long *len)
{
if ( id <= 0 || id > (long)vm.size() || vm[id] == NULL ) { //1から 0番は従来用
return 0;
}
if ( vm[id] ) {
return vm[id]->ExecuteRequest(h, len, false);
}
else {
return NULL;
}
}
/* -----------------------------------------------------------------------
* CI_check_failed
* -----------------------------------------------------------------------
*/
extern "C" DLLEXPORT BOOL_TYPE FUNCATTRIB CI_check_failed(void)
{
if( vm[0] ) {
return vm[0]->IsSuppress()||vm[0]->IsEmergency();
}
else {
return 0;
}
}
extern "C" DLLEXPORT BOOL_TYPE FUNCATTRIB multi_CI_check_failed(long id)//?
{
if( id <= 0 || id > (long)vm.size() || vm[id] == NULL ) { //1から 0番は従来用
return 0;
}
if( vm[id] ) {
return vm[id]->IsSuppress()||vm[id]->IsEmergency();
}
else {
return 0;
}
}
/* -----------------------------------------------------------------------
* Set_loghandler
* -----------------------------------------------------------------------
*/
extern "C" DLLEXPORT void FUNCATTRIB Set_loghandler(void (*loghandler)(const yaya::char_t *str, int mode, int id))
{
if(loghandler_list.size()<1)
loghandler_list.emplace_back(nullptr);
loghandler_list[0]=loghandler;
if( vm[0] ) {
vm[0]->Set_loghandler(loghandler);
}
else {
loghandler_list.reserve(1);
}
}
extern "C" DLLEXPORT void FUNCATTRIB multi_Set_loghandler(long id,void (*loghandler)(const yaya::char_t *str, int mode, int id))//?
{
if( id <= 0 || id > (long)vm.size() || vm[id] == NULL ) { //1から 0番は従来用
return;
}
enlarge_loghandler_list(id+1);
loghandler_list[id]=loghandler;
if( vm[id] ) {
vm[id]->Set_loghandler(loghandler);
}
}
/* -----------------------------------------------------------------------
* logsend(AYA固有 チェックツールから使用)
* -----------------------------------------------------------------------
*/
#if !defined(AYA_MAKE_EXE)
#if defined(WIN32)
extern "C" DLLEXPORT BOOL_TYPE FUNCATTRIB logsend(long hwnd)
{
if ( vm[0] ) {
vm[0]->SetLogRcvWnd(hwnd);
}
else if ( vm.size() >= 2 && vm[1] ) {
vm[1]->SetLogRcvWnd(hwnd);
}
else {
logsend_hwnd = hwnd;
}
return TRUE;
}
#endif //win32
#endif //aya_make_exe
/* -----------------------------------------------------------------------
* main (実行ファイル版のみ)
* -----------------------------------------------------------------------
*/
#if defined(AYA_MAKE_EXE)
int main( int argc, char *argv[ ], char *envp[ ] )
{
AYA_InitModule(NULL);
std::string bufstr;
_setmode( _fileno( stdin ), _O_BINARY );
_setmode( _fileno( stdout ), _O_BINARY );
while ( 1 ) {
bufstr.erase();
while ( 1 ) {
char buf[2];
fread(buf,1,1,stdin);
bufstr += static_cast<char>(buf[0]);
if ( bufstr.size() >= 2 ) {
if ( strcmp(bufstr.c_str() + bufstr.size() - 2,"\r\n") == 0 ) { //改行検出
break;
}
}
}
const char* bufptr = bufstr.c_str();
if ( strncmp(bufptr,"load:",5) == 0 ) {
bufptr += 5;
long size = atoi(bufptr);
if ( size > 0 ) {
char *read_ptr = (char*)::GlobalAlloc(GMEM_FIXED,size+1);
fread(read_ptr,1,size,stdin);
read_ptr[size] = 0;
char *p = strstr(read_ptr,"\r\n");
if ( p ) { *p = 0; size -= 2; }
load(read_ptr,size);
}
const char* result = "load:5\r\n1\r\n\r\n";
fwrite(result,1,strlen(result),stdout);
fflush(stdout);
}
else if ( strncmp(bufptr,"unload:",7) == 0 ) {
bufptr += 7;
long size = atoi(bufptr);
if ( size > 0 ) {
char *read_ptr = (char*)malloc(size);
fread(read_ptr,1,size,stdin);
free(read_ptr); //データまとめて破棄
}
unload();
const char* result = "unload:5\r\n1\r\n\r\n";
fwrite(result,1,strlen(result),stdout);
fflush(stdout);
break;
}
else if ( (strncmp(bufptr,"request:",8) == 0) ) {
bufptr += 8;
long size = atoi(bufptr);
if ( size > 0 ) {
char *read_ptr = (char*)::GlobalAlloc(GMEM_FIXED,size+1);
fread(read_ptr,1,size,stdin);
read_ptr[size] = 0;
yaya::global_t res = request(read_ptr,&size);
char write_header[64];
sprintf(write_header,"request:%d\r\n",size);
fwrite(write_header,1,strlen(write_header),stdout);
fwrite(res,1,size,stdout);
fflush(stdout);
::GlobalFree(res);
}
else {
const char* w = "request:0\r\n";
fwrite(w,1,strlen(w),stdout);
}
}
}
return 0;
}
#endif //aya_make_exe