This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmdlib.c
548 lines (418 loc) · 8.26 KB
/
cmdlib.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
// cmdlib.c
#include "cmdlib.h"
#ifdef __NeXT__
/*
================
=
= filelength
=
================
*/
int filelength (int handle)
{
struct stat fileinfo;
if (fstat (handle,&fileinfo) == -1)
{
fprintf (stderr,"Error fstating");
exit (1);
}
return fileinfo.st_size;
}
int tell (int handle)
{
return lseek (handle, 0, L_INCR);
}
char *strupr (char *start)
{
char *in;
in = start;
while (*in)
{
*in = toupper(*in);
in++;
}
return start;
}
char *getcwd (char *path, int length)
{
return getwd(path);
}
#endif
/* globals for command line args */
#ifdef __NeXT__
extern int NXArgc;
extern char **NXArgv;
#define myargc NXArgc
#define myargv NXArgv
#endif
#ifdef __WATCOMC__
extern int _argc;
extern char **_argv;
#define myargc _argc
#define myargv _argv
#endif
/*
=============================================================================
MISC FUNCTIONS
=============================================================================
*/
/*
=================
=
= Error
=
= For abnormal program terminations
=
=================
*/
void Error (char *error, ...)
{
va_list argptr;
#ifndef __NeXT__
if ( *(byte *)0x449 == 0x13)
TextMode ();
#endif
va_start (argptr,error);
vprintf (error,argptr);
va_end (argptr);
printf ("\n");
exit (1);
}
/*
=================
=
= CheckParm
=
= Checks for the given parameter in the program's command line arguments
=
= Returns the argument number (1 to argc-1) or 0 if not present
=
=================
*/
int CheckParm (char *check)
{
int i;
char *parm;
for (i = 1;i<myargc;i++)
{
parm = myargv[i];
if ( !isalpha(*parm) ) // skip - / \ etc.. in front of parm
if (!*++parm)
continue; // parm was only one char
if ( !stricmp(check,parm) )
return i;
}
return 0;
}
int SafeOpenWrite (char *filename)
{
int handle;
handle = open(filename,O_RDWR | O_BINARY | O_CREAT | O_TRUNC
, 0666);
if (handle == -1)
Error ("Error opening %s: %s",filename,strerror(errno));
return handle;
}
int SafeOpenRead (char *filename)
{
int handle;
handle = open(filename,O_RDONLY | O_BINARY);
if (handle == -1)
Error ("Error opening %s: %s",filename,strerror(errno));
return handle;
}
void SafeRead (int handle, void *buffer, long count)
{
unsigned iocount;
while (count)
{
iocount = count > 0x8000 ? 0x8000 : count;
if (read (handle,buffer,iocount) != iocount)
Error ("File read failure");
buffer = (void *)( (byte *)buffer + iocount );
count -= iocount;
}
}
void SafeWrite (int handle, void *buffer, long count)
{
unsigned iocount;
while (count)
{
iocount = count > 0x8000 ? 0x8000 : count;
if (write (handle,buffer,iocount) != iocount)
Error ("File write failure");
buffer = (void *)( (byte *)buffer + iocount );
count -= iocount;
}
}
void *SafeMalloc (long size)
{
void *ptr;
ptr = malloc (size);
if (!ptr)
Error ("Malloc failure for %lu bytes",size);
return ptr;
}
/*
==============
=
= LoadFile
=
==============
*/
long LoadFile (char *filename, void **bufferptr)
{
int handle;
long length;
void *buffer;
handle = SafeOpenRead (filename);
length = filelength (handle);
buffer = SafeMalloc (length);
SafeRead (handle, buffer, length);
close (handle);
*bufferptr = buffer;
return length;
}
/*
==============
=
= SaveFile
=
==============
*/
void SaveFile (char *filename, void *buffer, long count)
{
int handle;
handle = SafeOpenWrite (filename);
SafeWrite (handle, buffer, count);
close (handle);
}
void DefaultExtension (char *path, char *extension)
{
char *src;
//
// if path doesn't have a .EXT, append extension
// (extension should include the .)
//
src = path + strlen(path) - 1;
while (*src != PATHSEPERATOR && src != path)
{
if (*src == '.')
return; // it has an extension
src--;
}
strcat (path, extension);
}
void DefaultPath (char *path, char *basepath)
{
char temp[128];
if (path[0] == PATHSEPERATOR)
return; // absolute path location
strcpy (temp,path);
strcpy (path,basepath);
strcat (path,temp);
}
void StripFilename (char *path)
{
int length;
length = strlen(path)-1;
while (length > 0 && path[length] != PATHSEPERATOR)
length--;
path[length] = 0;
}
void StripExtension (char *path)
{
int length;
length = strlen(path)-1;
while (length > 0 && path[length] != '.')
length--;
if (length)
path[length] = 0;
}
void ExtractFileBase (char *path, char *dest)
{
char *src;
int length;
src = path + strlen(path) - 1;
//
// back up until a \ or the start
//
while (src != path && *(src-1) != PATHSEPERATOR)
src--;
//
// copy up to eight characters
//
memset (dest,0,8);
length = 0;
while (*src && *src != '.')
{
if (++length == 9)
Error ("Filename base of %s >8 chars",path);
*dest++ = toupper(*src++);
}
}
/*
==============
=
= ParseNum / ParseHex
=
==============
*/
long ParseHex (char *hex)
{
char *str;
long num;
num = 0;
str = hex;
while (*str)
{
num <<= 4;
if (*str >= '0' && *str <= '9')
num += *str-'0';
else if (*str >= 'a' && *str <= 'f')
num += 10 + *str-'a';
else if (*str >= 'A' && *str <= 'F')
num += 10 + *str-'A';
else
Error ("Bad hex number: %s",hex);
str++;
}
return num;
}
long ParseNum (char *str)
{
if (str[0] == '$')
return ParseHex (str+1);
if (str[0] == '0' && str[1] == 'x')
return ParseHex (str+2);
return atol (str);
}
int GetKey (void)
{
#ifdef __NeXT__
return getchar ();
#else
return _bios_keybrd (_KEYBRD_READ)&0xff;
#endif
}
/*
============================================================================
BYTE ORDER FUNCTIONS
============================================================================
*/
#ifdef __BIG_ENDIAN__
short LittleShort (short l)
{
byte b1,b2;
b1 = l&255;
b2 = (l>>8)&255;
return (b1<<8) + b2;
}
short BigShort (short l)
{
return l;
}
long LittleLong (long l)
{
byte b1,b2,b3,b4;
b1 = l&255;
b2 = (l>>8)&255;
b3 = (l>>16)&255;
b4 = (l>>24)&255;
return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
}
long BigLong (long l)
{
return l;
}
#else
short BigShort (short l)
{
byte b1,b2;
b1 = l&255;
b2 = (l>>8)&255;
return (b1<<8) + b2;
}
short LittleShort (short l)
{
return l;
}
long BigLong (long l)
{
byte b1,b2,b3,b4;
b1 = l&255;
b2 = (l>>8)&255;
b3 = (l>>16)&255;
b4 = (l>>24)&255;
return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
}
long LittleLong (long l)
{
return l;
}
#endif
/*
============================================================================
BASIC GRAPHICS
============================================================================
*/
byte *screen;
#define PEL_WRITE_ADR 0x3c8
#define PEL_READ_ADR 0x3c7
#define PEL_DATA 0x3c9
#define PEL_MASK 0x3c6
/*
==============
=
= GetPalette
=
= Return an 8 bit / color palette
=
==============
*/
void GetPalette (byte *pal)
{
#ifndef __NeXT__
int i;
outp (PEL_READ_ADR,0);
for (i=0 ; i<768 ; i++)
pal[i] = inp (PEL_DATA)<<2;
#endif
}
/*
==============
=
= SetPalette
=
= Sets an 8 bit / color palette
=
==============
*/
void SetPalette (byte *pal)
{
#ifndef __NeXT__
int i;
outp (PEL_WRITE_ADR,0);
for (i=0 ; i<768 ; i++)
outp (PEL_DATA, pal[i]>>2);
#endif
}
void VGAMode (void)
{
#ifndef __NeXT__
union REGS regs; // scratch for int calls
regs.w.ax = 0x13;
int386 (0x10,(const union REGS *)®s,®s);
screen = (byte *)0xa0000;
#else
screen = malloc (0x10000);
#endif
}
void TextMode (void)
{
#ifndef __NeXT__
union REGS regs; // scratch for int calls
regs.w.ax = 0x3;
int386 (0x10,(const union REGS *)®s,®s);
#endif
}