forked from oe5hpm/openBCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ad_win32.cpp
506 lines (444 loc) · 13.4 KB
/
ad_win32.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
/***************************************************************
BayCom(R) Packet-Radio fuer IBM PC
OpenBCM-Mailbox
-------------------------------
Anpassungen fuer WIN32-Umgebung
-------------------------------
Copyright (c) Florian Radlherr
Taubenbergstr. 32
83627 Warngau
Alle Rechte vorbehalten / All Rights Reserved
***************************************************************/
//19980831 OE3DZW added setenv for Win32
//19980914 DG9MHZ,OE3DZW fixes to oshell32
//19980921 OE3DZW fixed free_disk
//19980922 DG9MHZ,OE3DZW finaly fixed oshell32
#include "baycom.h"
#ifdef _WIN32
#include <fcntl.h>
/*---------------------------------------------------------------------------*/
int bioskey (int mode)
//*************************************************************************
//
//*************************************************************************
{
return 0;
}
/*---------------------------------------------------------------------------*/
int random_max (int max)
//*************************************************************************
//
//*************************************************************************
{
return rand()%max;
}
/*---------------------------------------------------------------------------*/
// struct _finddata_t {
// unsigned attrib;
// time_t time_create; /* -1 for FAT file systems */
// time_t time_access; /* -1 for FAT file systems */
// time_t time_write;
// _fsize_t size;
// char name[260];
// };
/*---------------------------------------------------------------------------*/
DIR *opendir (char *file)
//*************************************************************************
//
//*************************************************************************
{
char fn[80];
sprintf(fn, "%s/*.*", file);
DIR *tmpdir = (DIR *) t_malloc(sizeof(DIR), "odir");
tmpdir->handle = _findfirst(fn, &(tmpdir->ft));
if (tmpdir->handle == -1)
{
t_free(tmpdir);
return NULL;
}
else
return tmpdir;
}
/*---------------------------------------------------------------------------*/
struct dirent *readdir (DIR *d)
//*************************************************************************
//
//*************************************************************************
{
static struct dirent di;
if (d == NULL)
return NULL;
if (d->ft.name[0] == 0)
{
if (_findnext(d->handle, &(d->ft)) == -1)
{
return NULL;
}
}
strcpy(di.d_name, d->ft.name);
d->ft.name[0] = 0;
return &di;
}
/*---------------------------------------------------------------------------*/
void closedir (DIR *d)
//*************************************************************************
//
//*************************************************************************
{
if (d != NULL)
{
_findclose(d->handle);
t_free(d);
}
}
/*---------------------------------------------------------------------------*/
unsigned long dfree (char *path, int total)
//*************************************************************************
//
// returns free space in kbytes (max. 4 GBytes)
//
//*************************************************************************
{
static char cpath[80];
char *pcpath = cpath;
union _ULARGE_INTEGER a, b, c;
strcpy(cpath, path);
if (cpath[1] == ':')
{
cpath[2] = '\\';
cpath[3] = 0;
}
else
pcpath=NULL;
if (! GetDiskFreeSpaceEx(pcpath, &a, &b, &c))
return 1024L*1024L; //dummy 1MByte
if (total)
return (unsigned long) (b.QuadPart>>10);
else
return (unsigned long) (a.QuadPart>>10);
}
/*---------------------------------------------------------------------------*/
char *memfree (int swapfree)
//*************************************************************************
//
//*************************************************************************
{
static char ret[20];
static char ret2[20];
MEMORYSTATUS st;
st.dwLength=sizeof(st);
GlobalMemoryStatus(&st);
if(swapfree)
{
sprintf(ret, "%ld", (st.dwTotalPageFile-st.dwAvailPageFile) >> 10);
return ret;
}
else
{
sprintf(ret2, "%ld", (st.dwAvailPhys) >> 10);
return ret2;
}
}
/*---------------------------------------------------------------------------*/
void task_idle (int active)
//*************************************************************************
//
//*************************************************************************
{
//static long lasttic=0;
if (active == 0)
Sleep(20);
// if (systic != lasttic)
// {
call_l2(active == 2);
// lasttic = systic;
// }
}
/*---------------------------------------------------------------------------*/
void coldstart (void)
//*************************************************************************
//
//*************************************************************************
{
strcpy(stopreason, "WATCHDOG");
exit(1);
}
typedef struct { char *befbuf;
char *buffer;
HANDLE hProcess;
int hStdOutPipe[2];
int hStdInPipe[2];
} shellthread_t;
#define OUT_BUFF_SIZE 256
#define READ_HANDLE 0
#define WRITE_HANDLE 1
/*---------------------------------------------------------------------------*/
void oshell95 (void *p)
//*************************************************************************
//
//*************************************************************************
{
int retwert = 0;
int nExitCode = STILL_ACTIVE;
char tmp_out[20];
char tmp_err[20];
shellthread_t *s = (shellthread_t*)p;
strcpy(tmp_out, time2filename(0));
strcpy(tmp_err, time2filename(0));
if (s->befbuf[0])
{
char *commandcom = getenv("COMSPEC");
if (commandcom == NULL)
commandcom = "command";
sprintf(s->buffer, "/c %s >%s", s->befbuf, tmp_out);
freopen(tmp_err, "wt", stderr);
s->hProcess = (HANDLE) spawnl(P_NOWAIT, commandcom,
commandcom, s->buffer, NULL);
cdhome();
freopen("con", "wt", stderr);
sprintf(s->befbuf+1, "%s %s", tmp_err, tmp_out);
s->befbuf[0] = 0;
while (nExitCode == STILL_ACTIVE)
{
if (! GetExitCodeProcess(s->hProcess, (unsigned long*) &nExitCode))
ExitThread(4);
Sleep(100);
}
ExitThread(nExitCode);
}
ExitThread(retwert);
}
/*---------------------------------------------------------------------------*/
void oshell32 (void *p)
//*************************************************************************
//
//*************************************************************************
{
shellthread_t *s = (shellthread_t*)p;
int hStdOut;
int hStdIn;
int hStdErr;
int nExitCode = STILL_ACTIVE;
char *cmdline;
// Create the pipe
if (_pipe(s->hStdOutPipe, 512, O_BINARY | O_NOINHERIT) == -1 ||
_pipe(s->hStdInPipe, 512, O_BINARY | O_NOINHERIT) == -1)
ExitThread(1);
// Duplicate stdout handle (next line will close original)
hStdOut = _dup(_fileno(stdout));
hStdIn = _dup(_fileno(stdin));
hStdErr = _dup(_fileno(stderr));
// Duplicate write end of pipe to stdout handle
if (_dup2(s->hStdOutPipe[WRITE_HANDLE], _fileno(stdout)) != 0 ||
_dup2(s->hStdInPipe[READ_HANDLE], _fileno(stdin)) != 0 ||
_dup2(s->hStdOutPipe[WRITE_HANDLE], _fileno(stderr)) != 0)
ExitThread(2);
// Close original write end of pipe
close(s->hStdOutPipe[WRITE_HANDLE]);
close(s->hStdInPipe[READ_HANDLE]);
cmdline = getenv("COMSPEC");
if (cmdline == NULL)
cmdline = "cmd.exe";
if (s->befbuf && s->befbuf[0])
sprintf(s->buffer, "/c%s", s->befbuf);
else
s->buffer[0] = 0;
// Spawn process
s->hProcess = (HANDLE) spawnl(P_NOWAIT, cmdline,
cmdline, s->buffer, NULL);
// Duplicate copy of original stdout back into stdout
if (_dup2(hStdOut, _fileno(stdout)) != 0 ||
_dup2(hStdIn, _fileno(stdin)) != 0 ||
_dup2(hStdErr, _fileno(stderr)) != 0)
ExitThread(3);
// Close duplicate copy of original stdout
close(hStdOut);
close(hStdIn);
close(hStdErr);
while (nExitCode == STILL_ACTIVE)
{
if (! GetExitCodeProcess(s->hProcess, (unsigned long*) &nExitCode))
ExitThread(4);
Sleep(100);
}
close(s->hStdOutPipe[READ_HANDLE]);
close(s->hStdInPipe[WRITE_HANDLE]);
CloseHandle(s->hProcess);
//ExitThread(nExitCode);
_endthread();
}
/*---------------------------------------------------------------------------*/
void kill (void *p)
//*************************************************************************
//
//*************************************************************************
{
TerminateProcess((HANDLE)p, -1);
_endthread();
}
/*---------------------------------------------------------------------------*/
int oshell (char *befbuf, shellinput_t input)
//*************************************************************************
//
//*************************************************************************
{
HANDLE hdl;
OSVERSIONINFO OsVer;
shellthread_t s;
int ProcessExitCode = STILL_ACTIVE;
int ThreadExitCode = STILL_ACTIVE;
int nOutRead;
char buffer[OUT_BUFF_SIZE+1];
s.befbuf = befbuf;
s.buffer = buffer;
s.hProcess = 0;
OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OsVer);
switch (OsVer.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
if (! befbuf || ! befbuf[0])
putf("Type EXIT to return to " STD_BOXHEADER "...\n");
if ((hdl = (HANDLE)_beginthread(oshell32, 0, (void*)&s)) != 0)
{
wdelay(500);
if (s.hProcess)
{
t->childpid=(int) s.hProcess;
while (ProcessExitCode == STILL_ACTIVE && ThreadExitCode == STILL_ACTIVE)
{
if (getvalid())
{
getline(buffer, OUT_BUFF_SIZE - 2, 1);
strcat(buffer, "\n");
write(s.hStdInPipe[WRITE_HANDLE], buffer, strlen(buffer));
}
while (! eof(s.hStdOutPipe[READ_HANDLE]))
{
nOutRead = read(s.hStdOutPipe[READ_HANDLE], buffer, OUT_BUFF_SIZE);
if (nOutRead)
{
buffer[nOutRead]=0;
putf("%s", buffer);
putflush();
}
}
wdelay(11);
if (! GetExitCodeProcess(s.hProcess, (unsigned long*) &ProcessExitCode))
return 4;
if (! GetExitCodeThread(hdl, (unsigned long*) &ThreadExitCode))
return 5;
}
}
putv(LF);
}
t->childpid=0;
_beginthread(kill, 0, (void*)s.hProcess);
break;
default:
if ((hdl = (HANDLE)_beginthread(oshell95, 0, (void*)&s)) != 0)
{
wdelay(500);
if (s.hProcess)
{
t->childpid=(int) s.hProcess;
while (ThreadExitCode == STILL_ACTIVE)
{
if (! GetExitCodeThread(hdl, (unsigned long*) &ThreadExitCode))
return 5;
wdelay(100);
}
if (! befbuf[0])
{
befbuf++;
char *s = strchr(befbuf, 32);
if (s)
{
s[0]=0;
s++;
}
if (! access(befbuf, 0))
{
fileio_text fio;
fio.usefile(befbuf);
fio.doerase();
fio.tx();
}
if (s && !access(s, 0))
{
fileio_text fio;
fio.usefile(s);
fio.doerase();
fio.tx();
}
}
t->childpid=0;
}
}
}
return ThreadExitCode;
}
/*---------------------------------------------------------------------------*/
void setenv (char *lpname, char *lpvalue, int dummy)
//*************************************************************************
//
// replacement for Linux setenv under Win32
//
//*************************************************************************
{
SetEnvironmentVariable(lpname, lpvalue);
}
/*---------------------------------------------------------------------------*/
char *cpuinfo (void)
//*************************************************************************
//
//*************************************************************************
{
static char retbuf[256];
char cputype[40];
char osname[40];
char *s = retbuf;
strcpy(cputype, "??");
strcpy(osname, "??");
SYSTEM_INFO si;
OSVERSIONINFO OsVer;
OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OsVer);
if (OsVer.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
GetSystemInfo(&si);
switch (si.dwProcessorType)
{
case PROCESSOR_INTEL_386:
sprintf(cputype, "I386");
break;
case PROCESSOR_INTEL_486:
sprintf(cputype, "I486");
break;
case PROCESSOR_INTEL_PENTIUM:
sprintf(cputype, "Pentium");
break;
}
s += sprintf(s, "CPU: %s ", cputype);
}
if (GetSystemMetrics(SM_SLOWMACHINE))
s += sprintf(s, "Processor too slow!");
switch (OsVer.dwPlatformId)
{
case VER_PLATFORM_WIN32s:
sprintf(osname, "WIN32s");
break;
case VER_PLATFORM_WIN32_WINDOWS:
sprintf(osname, "Windows 95");
break;
case VER_PLATFORM_WIN32_NT:
sprintf(osname, "Windows NT");
break;
}
s += sprintf(s, "\nOS: %s V%d.%d Build %d %s",
osname, OsVer.dwMajorVersion, OsVer.dwMinorVersion,
OsVer.dwBuildNumber, OsVer.szCSDVersion);
return retbuf;
}
#endif
/*---------------------------------------------------------------------------*/