-
Notifications
You must be signed in to change notification settings - Fork 4
/
beye.c
749 lines (708 loc) · 22.7 KB
/
beye.c
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/**
* @namespace biew
* @file biew.c
* @brief This file contains entry point of BIEW project.
* @version -
* @remark this source file is part of Binary vIEW project (BIEW).
* The Binary vIEW (BIEW) is copyright (C) 1995 Nickols_K.
* All rights reserved. This software is redistributable under the
* licence given in the file "Licence.en" ("Licence.ru" in russian
* translation) distributed in the BIEW archive.
* @note Requires POSIX compatible development system
*
* @author Nickols_K
* @since 1995
* @note Development, fixes and improvements
* @author Kostya Nosov <[email protected]>
* @date 27.11.2000
* @note Changing technology recognition of new-exe files
**/
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <signal.h>
#include <limits.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_RESOURCE
#include <sys/resource.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include "bconfig.h"
#include "bconsole.h"
#include "colorset.h"
#include "bmfile.h"
#include "bin_util.h"
#include "codeguid.h"
#include "editor.h"
#include "tstrings.h"
#include "reg_form.h"
#include "beyeutil.h"
#include "search.h"
#include "setup.h"
#include "libbeye/file_ini.h"
#include "libbeye/kbd_code.h"
#include "libbeye/beyelib.h"
#include "libbeye/pmalloc.h"
unsigned ArgCount;
char ** ArgVector;
unsigned ListFileCount;
static char **ListFile;
static char *LastOpenFileName;
__filesize_t LastOffset;
static tBool UseIniFile=True;
char biew_help_name[FILENAME_MAX+1] = "";
char biew_skin_name[FILENAME_MAX+1] = "";
char biew_syntax_name[FILENAME_MAX+1] = "";
char biew_codepage[256] = "CP866";
extern char last_skin_error[];
char biew_scheme_name[256] = "Built-in";
static char biew_ini_ver[32];
unsigned long biew_vioIniFlags = 0L;
unsigned long biew_twinIniFlags = 0L;
unsigned long biew_kbdFlags = 0L;
tBool iniSettingsAnywhere = False;
tBool fioUseMMF = False;
tBool iniPreserveTime = False;
tBool iniUseExtProgs = False;
__filesize_t headshift = 0L;
char *ini_name;
TWindow * MainWnd = 0,*HelpWnd = 0,*TitleWnd = 0,*ErrorWnd = 0;
#define SHORT_PATH_LEN __TVIO_MAXSCREENWIDTH-54
char shortname[SHORT_PATH_LEN + 1];
extern REGISTRY_BIN binTable;
extern REGISTRY_BIN rmTable;
extern REGISTRY_BIN movTable;
extern REGISTRY_BIN mp3Table;
extern REGISTRY_BIN mpegTable;
extern REGISTRY_BIN jpegTable;
extern REGISTRY_BIN wavTable;
extern REGISTRY_BIN aviTable;
extern REGISTRY_BIN asfTable;
extern REGISTRY_BIN bmpTable;
extern REGISTRY_BIN neTable;
extern REGISTRY_BIN peTable;
extern REGISTRY_BIN leTable;
extern REGISTRY_BIN lxTable;
extern REGISTRY_BIN nlm386Table;
extern REGISTRY_BIN elf386Table;
extern REGISTRY_BIN jvmTable;
extern REGISTRY_BIN coff386Table;
extern REGISTRY_BIN archTable;
extern REGISTRY_BIN aoutTable;
extern REGISTRY_BIN OldPharLapTable;
extern REGISTRY_BIN PharLapTable;
extern REGISTRY_BIN rdoffTable;
extern REGISTRY_BIN rdoff2Table;
extern REGISTRY_BIN sisTable;
extern REGISTRY_BIN sisxTable;
extern REGISTRY_BIN lmfTable;
extern REGISTRY_BIN mzTable;
extern REGISTRY_BIN dossysTable;
static REGISTRY_BIN *mainBinTable[] =
{
&neTable,
&peTable,
&leTable,
&lxTable,
&nlm386Table,
&elf386Table,
&jvmTable,
&coff386Table,
&archTable,
&aoutTable,
&OldPharLapTable,
&PharLapTable,
&rdoffTable,
&rdoff2Table,
&lmfTable,
&mzTable,
&dossysTable,
&sisTable,
&sisxTable,
&aviTable,
&asfTable,
&bmpTable,
&mpegTable,
&jpegTable,
&wavTable,
&movTable,
&rmTable,
&mp3Table,
&binTable
};
REGISTRY_BIN *detectedFormat = 0;
extern REGISTRY_MODE binMode;
extern REGISTRY_MODE textMode;
extern REGISTRY_MODE hexMode;
extern REGISTRY_MODE disMode;
static REGISTRY_MODE *mainModeTable[] =
{
&textMode,
&binMode,
&hexMode,
&disMode
};
REGISTRY_MODE *activeMode;
static size_t LastMode = sizeof(mainModeTable)/sizeof(REGISTRY_BIN *)+10;
static unsigned defMainModeSel = 0;
tBool SelectMode( void )
{
const char *modeName[sizeof(mainModeTable)/sizeof(REGISTRY_MODE *)];
size_t i,nModes;
int retval;
nModes = sizeof(mainModeTable)/sizeof(REGISTRY_MODE *);
for(i = 0;i < nModes;i++) modeName[i] = mainModeTable[i]->name;
retval = SelBoxA(modeName,nModes," Select translation mode: ",defMainModeSel);
if(retval != -1)
{
if(activeMode->term) activeMode->term();
activeMode = mainModeTable[retval];
if(activeMode->init) activeMode->init();
defMainModeSel = retval;
return True;
}
return False;
}
static void __NEAR__ __FASTCALL__ init_modes( hIniProfile *ini )
{
if(activeMode->init) activeMode->init();
if(activeMode->read_ini) activeMode->read_ini(ini);
}
static void __NEAR__ __FASTCALL__ term_modes( void )
{
if(activeMode->term) activeMode->term();
}
static void __NEAR__ __FASTCALL__ __init_biew( void )
{
LastOpenFileName = PMalloc(4096);
ListFile = PMalloc((ArgCount-1)*sizeof(char *));
if((!LastOpenFileName) || (!ListFile))
{
printm("BIEW initialization failed! Out of memory!");
exit(EXIT_FAILURE);
}
}
static void __NEAR__ __FASTCALL__ __term_biew( void )
{
PFREE(LastOpenFileName);
PFREE(ListFile);
}
void QuickSelectMode( void )
{
unsigned nModes;
nModes = sizeof(mainModeTable)/sizeof(REGISTRY_MODE *);
if(defMainModeSel < nModes - 1) defMainModeSel++;
else defMainModeSel = 0;
if(activeMode->term) activeMode->term();
activeMode = mainModeTable[defMainModeSel];
if(activeMode->init) activeMode->init();
}
static void __NEAR__ __FASTCALL__ MakeShortName( void )
{
unsigned l;
unsigned slen = twGetClientWidth(TitleWnd)-54;
l = strlen(ArgVector[1]);
if(l <= slen) strcpy(shortname,ArgVector[1]);
else
{
strncpy(shortname,ArgVector[1],slen/2 - 3);
shortname[slen/2-4] = 0;
strcat(shortname,"...");
strcat(shortname,&ArgVector[1][l - slen/2]);
}
__nls_CmdlineToOem((unsigned char *)shortname,strlen(shortname));
}
__filesize_t IsNewExe()
{
__filesize_t ret;
char id[2];
bmReadBufferEx(id,sizeof(id),0,BM_SEEK_SET);
#if 0
/*
It is well documented technology, but it correctly working
only with normal stubs, i.e. when New EXE header is located at
offset > 0x40. However, in PC world exists files with wrong
stubs, which are normal for Host OS. Hence biew must recognize
them as normal New EXE files, despite the fact that DOS can
not execute ones.
Fixed by Kostya Nosov <[email protected]>.
*/
if(!( id[0] == 'M' && id[1] == 'Z' &&
bmReadWordEx(0x18,BM_SEEK_SET) >= 0x40 &&
(ret=bmReadDWordEx(0x3C,BM_SEEK_SET)) > 0x40L)) ret = 0;
#endif
if(!( id[0] == 'M' && id[1] == 'Z' &&
(ret=bmReadDWordEx(0x3C,BM_SEEK_SET)) > 0x02L)) ret = 0;
return (__filesize_t)ret;
}
static void __NEAR__ __FASTCALL__ AutoDetectMode( void )
{
int i,n;
n = sizeof(mainModeTable) / sizeof(REGISTRY_MODE *);
for(i = 0;i < n;i++)
{
if(mainModeTable[i]->detect())
{
defMainModeSel = i;
break;
}
}
activeMode = mainModeTable[i];
BMSeek(0,BM_SEEK_SET);
}
struct tagbiewArg
{
const char key[4];
const char *prompt;
}biewArg[] =
{
{ "-a", "autodetect mode (default)" },
{ "-b", "view file in binary mode" },
{ "-d", "view file in disassembler mode" },
{ "-h", "view file in hexadecimal mode" },
{ "-t", "view file in text mode" },
{ "-s", "change size of file to NNN bytes (create, if file does not exist)" },
{ "-i", "ignore .ini file (create new)" },
{ "-?", "display this screen" }
};
static int __NEAR__ __FASTCALL__ queryKey(char *arg)
{
int ret = -1;
size_t i;
for(i = 0;i < sizeof(biewArg)/sizeof(struct tagbiewArg);i++)
{
if(strcmp(arg,biewArg[i].key) == 0) { ret = i; break; }
}
return ret;
}
static unsigned int biew_mode = UINT_MAX;
static __filesize_t new_file_size = FILESIZE_MAX;
static void __NEAR__ __FASTCALL__ ParseCmdLine( void )
{
unsigned i;
ListFileCount = 0;
for(i = 1;i < ArgCount;i++)
{
int biew_key;
biew_key = queryKey(ArgVector[i]);
switch(biew_key)
{
case 0: biew_mode = UINT_MAX; break;
case 1: biew_mode = 1; break;
case 2: biew_mode = 3; break;
case 3: biew_mode = 2; break;
case 4: biew_mode = 0; break;
case 5:
#if (__WORDSIZE >= 32) && !defined(__QNX4__)
new_file_size = strtoull(ArgVector[++i],NULL,10);
#else
new_file_size = strtoul(ArgVector[++i],NULL,10);
#endif
break;
case 6: UseIniFile = False; break;
case 7: ListFileCount = 0; return;
default: ListFile[ListFileCount++] = ArgVector[i];
}
}
if(ListFileCount) ArgVector[1] = ListFile[0];
}
static tBool __NEAR__ __FASTCALL__ LoadInfo( void )
{
MakeShortName();
if(new_file_size != FILESIZE_MAX)
{
bhandle_t handle;
if(__IsFileExists(ArgVector[1]) == False) handle = __OsCreate(ArgVector[1]);
else
{
handle = __OsOpen(ArgVector[1],FO_READWRITE | SO_DENYNONE);
if(handle == NULL_HANDLE) handle = __OsOpen(ArgVector[1],FO_READWRITE | SO_COMPAT);
}
if(handle != NULL_HANDLE)
{
__OsChSize(handle,new_file_size);
__OsClose(handle);
}
else
{
errnoMessageBox(OPEN_FAIL,NULL,errno);
return False;
}
}
if(BMOpen(ArgVector[1]) != 0) return False;
if(biew_mode != UINT_MAX)
{
defMainModeSel = biew_mode;
activeMode = mainModeTable[defMainModeSel];
}
else
{
if(LastMode >= sizeof(mainModeTable)/sizeof(REGISTRY_MODE *) || !isValidIniArgs()) AutoDetectMode();
else
{
defMainModeSel = LastMode;
activeMode = mainModeTable[defMainModeSel];
}
}
return True;
}
static void __NEAR__ __FASTCALL__ __detectBinFmt( void )
{
unsigned i;
if(!bmGetFLength())
{
detectedFormat = &binTable;
return;
}
for(i = 0;i < sizeof(mainBinTable)/sizeof(REGISTRY_BIN *);i++)
{
if(mainBinTable[i]->check_format())
{
detectedFormat = mainBinTable[i];
if(detectedFormat->init) detectedFormat->init();
break;
}
}
/* Special case: mz initialization */
mzTable.check_format();
}
void PaintTitle( void )
{
twUseWin(TitleWnd);
twFreezeWin(TitleWnd);
twGotoXY(1,1);
twClrEOL();
twPrintF("File : %s",shortname);
twGotoXY(twGetClientWidth(TitleWnd)-43,1);
twPrintF("Size : %8llu bytes",BMGetFLength());
twRefreshWin(TitleWnd);
}
static void MyAtExit( void )
{
if(MainWnd) CloseWnd(MainWnd);
if(HelpWnd) CloseWnd(HelpWnd);
if(TitleWnd) CloseWnd(TitleWnd);
if(ErrorWnd) CloseWnd(ErrorWnd);
termBConsole();
__term_biew();
__term_sys();
}
tBool isValidIniArgs( void )
{
return iniSettingsAnywhere ? True :
ArgVector[1] ?
strcmp(ArgVector[1],LastOpenFileName) == 0 ?
biew_mode != UINT_MAX && biew_mode != LastMode ?
False : True : False : False;
}
static hIniProfile * __NEAR__ __FASTCALL__ load_ini_info( void )
{
char tmp[20], buf[20];
hIniProfile *ini;
ini_name = getenv("BIEW_INI");
if(!ini_name) ini_name = __get_ini_name("biew");
ini = UseIniFile ? iniOpenFile(ini_name,NULL) : NULL;
biewReadProfileString(ini,"Biew","Setup","HelpName","",biew_help_name,sizeof(biew_help_name));
biewReadProfileString(ini,"Biew","Setup","SkinName","",biew_skin_name,sizeof(biew_skin_name));
biewReadProfileString(ini,"Biew","Setup","SyntaxName","",biew_syntax_name,sizeof(biew_syntax_name));
biewReadProfileString(ini,"Biew","Search","String","",(char *)search_buff,sizeof(search_buff));
search_len = strlen((char *)search_buff);
biewReadProfileString(ini,"Biew","Search","Case","off",tmp,sizeof(tmp));
biewSearchFlg = stricmp(tmp,"on") == 0 ? SF_CASESENS : SF_NONE;
biewReadProfileString(ini,"Biew","Search","Word","off",tmp,sizeof(tmp));
if(stricmp(tmp,"on") == 0) biewSearchFlg |= SF_WORDONLY;
biewReadProfileString(ini,"Biew","Search","Backward","off",tmp,sizeof(tmp));
if(stricmp(tmp,"on") == 0) biewSearchFlg |= SF_REVERSE;
biewReadProfileString(ini,"Biew","Search","Template","off",tmp,sizeof(tmp));
if(stricmp(tmp,"on") == 0) biewSearchFlg |= SF_WILDCARDS;
biewReadProfileString(ini,"Biew","Search","UsePlugin","off",tmp,sizeof(tmp));
if(stricmp(tmp,"on") == 0) biewSearchFlg |= SF_PLUGINS;
biewReadProfileString(ini,"Biew","Search","AsHex","off",tmp,sizeof(tmp));
if(stricmp(tmp,"on") == 0) biewSearchFlg |= SF_ASHEX;
biewReadProfileString(ini,"Biew","Browser","LastOpen","",LastOpenFileName,4096);
sprintf(buf,"%u",LastMode); /* [dBorca] so that src and dst won't overlap for strncpy */
biewReadProfileString(ini,"Biew","Browser","LastMode",buf,tmp,sizeof(tmp));
LastMode = (size_t)strtoul(tmp,NULL,10);
biewReadProfileString(ini,"Biew","Browser","Offset","0",tmp,sizeof(tmp));
#if (__WORDSIZE >= 32) && !defined(__QNX4__)
LastOffset = atoll(tmp);
#else
LastOffset = atol(tmp); /** by watcom */
#endif
biewReadProfileString(ini,"Biew","Setup","Version","",biew_ini_ver,sizeof(biew_ini_ver));
biewReadProfileString(ini,"Biew","Setup","DirectConsole","yes",tmp,sizeof(tmp));
if(stricmp(tmp,"yes") == 0) biew_vioIniFlags = __TVIO_FLG_DIRECT_CONSOLE_ACCESS;
biewReadProfileString(ini,"Biew","Setup","ForceMono","no",tmp,sizeof(tmp));
if(stricmp(tmp,"yes") == 0) biew_twinIniFlags = TWIF_FORCEMONO;
biewReadProfileString(ini,"Biew","Setup","Force7Bit","no",tmp,sizeof(tmp));
if(stricmp(tmp,"yes") == 0) biew_vioIniFlags |= __TVIO_FLG_USE_7BIT;
biewReadProfileString(ini,"Biew","Setup","MouseSens","yes",tmp,sizeof(tmp));
if(stricmp(tmp,"yes") == 0) biew_kbdFlags = KBD_NONSTOP_ON_MOUSE_PRESS;
biewReadProfileString(ini,"Biew","Setup","IniSettingsAnywhere","no",tmp,sizeof(tmp));
if(stricmp(tmp,"yes") == 0) iniSettingsAnywhere = True;
biewReadProfileString(ini,"Biew","Setup","FioUseMMF","no",tmp,sizeof(tmp));
if(stricmp(tmp,"yes") == 0) fioUseMMF = True;
if(!__mmfIsWorkable()) fioUseMMF = False;
biewReadProfileString(ini,"Biew","Setup","PreserveTimeStamp","no",tmp,sizeof(tmp));
if(stricmp(tmp,"yes") == 0) iniPreserveTime = True;
biewReadProfileString(ini,"Biew","Setup","UseExternalProgs","no",tmp,sizeof(tmp));
if(stricmp(tmp,"yes") == 0) iniUseExtProgs = True;
biewReadProfileString(ini,"Biew","Setup","Codepage","CP866",biew_codepage,sizeof(biew_codepage));
return ini;
}
static void __NEAR__ __FASTCALL__ save_ini_info( void )
{
char tmp[20];
hIniProfile *ini;
search_buff[search_len] = 0;
ini = iniOpenFile(ini_name,NULL);
biewWriteProfileString(ini,"Biew","Setup","HelpName",biew_help_name);
biewWriteProfileString(ini,"Biew","Setup","SkinName",biew_skin_name);
biewWriteProfileString(ini,"Biew","Setup","SyntaxName",biew_syntax_name);
biewWriteProfileString(ini,"Biew","Setup","Version",BIEW_VERSION);
biewWriteProfileString(ini,"Biew","Search","String",(char *)search_buff);
biewWriteProfileString(ini,"Biew","Search","Case",biewSearchFlg & SF_CASESENS ? "on" : "off");
biewWriteProfileString(ini,"Biew","Search","Word",biewSearchFlg & SF_WORDONLY ? "on" : "off");
biewWriteProfileString(ini,"Biew","Search","Backward",biewSearchFlg & SF_REVERSE ? "on" : "off");
biewWriteProfileString(ini,"Biew","Search","Template",biewSearchFlg & SF_WILDCARDS ? "on" : "off");
biewWriteProfileString(ini,"Biew","Search","UsePlugin",biewSearchFlg & SF_PLUGINS ? "on" : "off");
biewWriteProfileString(ini,"Biew","Search","AsHex",biewSearchFlg & SF_ASHEX ? "on" : "off");
biewWriteProfileString(ini,"Biew","Browser","LastOpen",ArgVector[1]);
sprintf(tmp,"%u",defMainModeSel);
biewWriteProfileString(ini,"Biew","Browser","LastMode",tmp);
#if (__WORDSIZE >= 32) && !defined(__QNX4__)
sprintf(tmp,"%llu",LastOffset);
#else
sprintf(tmp,"%lu",LastOffset);
#endif
biewWriteProfileString(ini,"Biew","Browser","Offset",tmp);
strcpy(tmp,biew_vioIniFlags & __TVIO_FLG_DIRECT_CONSOLE_ACCESS ? "yes" : "no");
biewWriteProfileString(ini,"Biew","Setup","DirectConsole",tmp);
strcpy(tmp,biew_twinIniFlags & TWIF_FORCEMONO ? "yes" : "no");
biewWriteProfileString(ini,"Biew","Setup","ForceMono",tmp);
strcpy(tmp,(biew_vioIniFlags & __TVIO_FLG_USE_7BIT) == __TVIO_FLG_USE_7BIT ? "yes" : "no");
biewWriteProfileString(ini,"Biew","Setup","Force7Bit",tmp);
strcpy(tmp,biew_kbdFlags & KBD_NONSTOP_ON_MOUSE_PRESS ? "yes" : "no");
biewWriteProfileString(ini,"Biew","Setup","MouseSens",tmp);
strcpy(tmp,iniSettingsAnywhere ? "yes" : "no");
biewWriteProfileString(ini,"Biew","Setup","IniSettingsAnywhere",tmp);
strcpy(tmp,fioUseMMF ? "yes" : "no");
biewWriteProfileString(ini,"Biew","Setup","FioUseMMF",tmp);
strcpy(tmp,iniPreserveTime ? "yes" : "no");
biewWriteProfileString(ini,"Biew","Setup","PreserveTimeStamp",tmp);
strcpy(tmp,iniUseExtProgs ? "yes" : "no");
biewWriteProfileString(ini,"Biew","Setup","UseExternalProgs",tmp);
biewWriteProfileString(ini,"Biew","Setup","Codepage",biew_codepage);
if(activeMode->save_ini) activeMode->save_ini(ini);
udnTerm(ini);
iniCloseFile(ini);
}
static FTime ftim;
static tBool ftim_ok = False;
static void __FASTCALL__ ShowUsage(void) {
unsigned evt,i,nln,h,y;
TWindow *win;
nln = sizeof(biewArg)/sizeof(struct tagbiewArg);
h = nln+4;
y = tvioHeight/2-h/2;
win = WindowOpen(2,y,tvioWidth-1,y+h,TWS_NONE | TWS_NLSOEM);
if(!win) goto done;
twSetTitleAttr(win,BIEW_VER_MSG,TW_TMODE_CENTER,error_cset.border);
twCentredWin(win,NULL);
twSetColorAttr(error_cset.main);
twSetFrameAttr(win,TW_DOUBLE_FRAME,error_cset.border);
twSetFooterAttr(win," Press [ ESC ] to quit ",TW_TMODE_RIGHT,error_cset.border);
twClearWin();
twGotoXY(1,1);
twPutS(" Usage: biew [OPTIONS] file...");
for(i = 0;i < nln;i++)
{
twGotoXY(1,4+i);
twPrintF(" %s %s\n",biewArg[i].key,biewArg[i].prompt);
}
twShowWin(win);
do {
evt = GetEvent(NULL,NULL,ErrorWnd);
}while(!(evt == KE_ESCAPE || evt == KE_F(10) || evt == KE_ENTER));
twDestroyWin(win);
done:
termBConsole();
}
int main( int argc, char *argv[] )
{
hIniProfile *ini;
tBool skin_err;
int retval;
#ifndef NDEBUG
#ifdef RLIMIT_CORE
{
/* on many systems default coresize is 0.
Enable any coresize here. */
struct rlimit rl;
getrlimit(RLIMIT_CORE,&rl);
rl.rlim_cur = rl.rlim_max;
setrlimit(RLIMIT_CORE,&rl);
}
#endif
#endif
ArgCount = argc;
ArgVector = argv;
__init_sys();
__init_biew();
ini = load_ini_info();
skin_err = csetReadIniFile(biew_skin_name);
initBConsole(biew_vioIniFlags,biew_twinIniFlags);
if(ArgCount < 2) goto show_usage;
ParseCmdLine();
if(!ListFileCount)
{
/** print usage message */
size_t i;
show_usage:
ShowUsage();
printm("\n"BIEW_VER_MSG"\n");
printm(" Usage: biew [OPTIONS] file...\n\n");
for(i = 0;i < sizeof(biewArg)/sizeof(struct tagbiewArg);i++)
{
printm(" %s\t%s\n",biewArg[i].key,biewArg[i].prompt);
}
printm("\n");
return EXIT_FAILURE;
}
udnInit(ini);
ErrorWnd = WindowOpen(1,1,50,16,TWS_NONE | TWS_NLSOEM);
if(ErrorWnd) twSetTitleAttr(ErrorWnd," Error ",TW_TMODE_CENTER,error_cset.border);
else { printm("fatal error: can't create window"); return EXIT_FAILURE; }
twCentredWin(ErrorWnd,NULL);
twSetColorAttr(error_cset.main);
twSetFrameAttr(ErrorWnd,TW_DOUBLE_FRAME,error_cset.border);
HelpWnd = WindowOpen(1,tvioHeight,tvioWidth,tvioHeight,TWS_NLSOEM);
twSetColorAttr(prompt_cset.digit);
twClearWin();
twShowWin(HelpWnd);
if(strcmp(biew_ini_ver,BIEW_VERSION) != 0) Setup();
TitleWnd = WindowOpen(1,1,tvioWidth,1,TWS_NONE);
twSetColorAttr(title_cset.main);
twClearWin();
twShowWin(TitleWnd);
activeMode = mainModeTable[1];
atexit(MyAtExit);
retval = EXIT_SUCCESS;
if(skin_err)
{
char sout[256];
sprintf(sout,"Error in skin file detected: '%s'",last_skin_error);
ErrMessageBox(sout,NULL);
}
/* We must do it before opening a file because of some RTL has bug
when are trying to open already open file with no sharing access */
ftim_ok = __OsGetFTime(ArgVector[1],&ftim);
if(!LoadInfo())
{
if(ini) iniCloseFile(ini);
retval = EXIT_FAILURE;
goto Bye;
}
__detectBinFmt();
init_modes(ini);
init_addons();
init_sysinfo();
if(ini) iniCloseFile(ini);
MainWnd = WindowOpen(1,2,tvioWidth,tvioHeight-1,TWS_NONE);
twSetColorAttr(browser_cset.main);
twClearWin();
PaintTitle();
if(!isValidIniArgs() || LastOffset > BMGetFLength()) LastOffset = 0;
twShowWin(MainWnd);
MainLoop();
LastOffset = BMGetCurrFilePos();
save_ini_info();
term_sysinfo();
term_addons();
term_modes();
if(detectedFormat->destroy) detectedFormat->destroy();
BMClose();
if(iniPreserveTime && ftim_ok) __OsSetFTime(ArgVector[1],&ftim);
Bye:
return retval;
}
tBool NewSource( void )
{
int i;
tBool ret;
unsigned j,freq;
static int prev_file;
char ** nlsListFile;
nlsListFile = (char **)PMalloc(ListFileCount*sizeof(char *));
if(nlsListFile)
{
for(j = 0;j < ListFileCount;j++)
{
nlsListFile[j] = PMalloc(strlen(ListFile[j])+1);
if(!nlsListFile[j]) break;
}
}
else { MemOutBox("Initializing List of File\n"); return 0; }
for(freq = 0;freq < j;freq++)
{
unsigned ls;
ls = strlen(ListFile[freq]);
memcpy(nlsListFile[freq],ListFile[freq],ls+1);
__nls_CmdlineToOem((unsigned char *)nlsListFile[freq],ls);
}
i = SelBoxA((const char **)nlsListFile,j," Select new file: ",prev_file);
ret = 0;
for(freq = 0;freq < j;freq++) PFree(nlsListFile[freq]);
PFree(nlsListFile);
if(i != -1)
{
if(iniPreserveTime && ftim_ok) __OsSetFTime(ArgVector[1],&ftim);
BMClose();
ftim_ok = __OsGetFTime(ListFile[i],&ftim);
if(BMOpen(ListFile[i]) == 0)
{
ArgVector[1] = ListFile[i];
if(detectedFormat->destroy) detectedFormat->destroy();
if(activeMode->term) activeMode->term();
MakeShortName();
__detectBinFmt();
if(activeMode->init) activeMode->init();
ret = True;
}
else
{
if(BMOpen(ArgVector[1]) != 0)
{
exit(EXIT_FAILURE);
}
if(detectedFormat->destroy) detectedFormat->destroy();
if(activeMode->term) activeMode->term();
MakeShortName();
__detectBinFmt();
if(activeMode->init) activeMode->init();
ret = False;
}
}
return ret;
}
unsigned __FASTCALL__ biewReadProfileString(hIniProfile *ini,
const char *section,
const char *subsection,
const char *_item,
const char *def_value,
char *buffer,
unsigned cbBuffer)
{
return UseIniFile ? iniReadProfileString(ini,section,subsection,
_item,def_value,buffer,cbBuffer)
: 1;
}
tBool __FASTCALL__ biewWriteProfileString(hIniProfile *ini,
const char *section,
const char *subsection,
const char *item,
const char *value)
{
return iniWriteProfileString(ini,section,subsection,item,value);
}