Subversion Repositories SvarDOS

Rev

Rev 420 | Rev 423 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
421 mateuszvis 1
/* This file is part of the SvarCOM project and is published under the terms
2
 * of the MIT license.
3
 *
4
 * Copyright (C) 2021 Mateusz Viste
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
23
 */
24
 
352 mateuszvis 25
/*
26
 * a variety of helper functions
27
 * Copyright (C) 2021 Mateusz Viste
28
 */
29
 
396 mateuszvis 30
#include <i86.h>    /* MK_FP() */
420 mateuszvis 31
#include <stdio.h>  /* sprintf() */
396 mateuszvis 32
 
352 mateuszvis 33
#include "helpers.h"
34
 
420 mateuszvis 35
 
352 mateuszvis 36
/* case-insensitive comparison of strings, returns non-zero on equality */
37
int imatch(const char *s1, const char *s2) {
38
  for (;;) {
39
    char c1, c2;
40
    c1 = *s1;
41
    c2 = *s2;
42
    if ((c1 >= 'a') && (c1 <= 'z')) c1 -= ('a' - 'A');
43
    if ((c2 >= 'a') && (c2 <= 'z')) c2 -= ('a' - 'A');
44
    /* */
45
    if (c1 != c2) return(0);
46
    if (c1 == 0) return(1);
47
    s1++;
48
    s2++;
49
  }
50
}
51
 
52
 
53
/* returns zero if s1 starts with s2 */
54
int strstartswith(const char *s1, const char *s2) {
55
  while (*s2 != 0) {
56
    if (*s1 != *s2) return(-1);
57
    s1++;
58
    s2++;
59
  }
60
  return(0);
61
}
369 mateuszvis 62
 
63
 
64
/* outputs a NULL-terminated string to stdout */
65
void output_internal(const char *s, unsigned short nl) {
66
  _asm {
67
    mov ah, 0x02 /* AH=9 - write character in DL to stdout */
68
    mov si, s
69
    cld          /* clear DF so lodsb increments SI */
70
    NEXTBYTE:
71
    lodsb /* load byte from DS:SI into AL, SI++ */
72
    mov dl, al
73
    or al, 0  /* is al == 0? */
74
    jz DONE
75
    int 0x21
76
    jmp NEXTBYTE
77
    DONE:
78
    or nl, 0
79
    jz FINITO
80
    /* print out a CR/LF trailer if nl set */
81
    mov dl, 0x0D /* CR */
82
    int 0x21
83
    mov dl, 0x0A /* LF */
84
    int 0x21
85
    FINITO:
86
  }
87
}
388 mateuszvis 88
 
89
 
90
/* find first matching files using a FindFirst DOS call
91
 * returns 0 on success or a DOS err code on failure */
92
unsigned short findfirst(struct DTA *dta, const char *pattern, unsigned short attr) {
93
  unsigned short res = 0;
94
  _asm {
95
    /* set DTA location */
96
    mov ah, 0x1a
97
    mov dx, dta
98
    int 0x21
99
    /* */
100
    mov ah, 0x4e    /* FindFirst */
101
    mov dx, pattern
102
    mov cx, attr
103
    int 0x21        /* CF set on error + err code in AX, DTA filled with FileInfoRec on success */
104
    jnc DONE
105
    mov [res], ax
106
    DONE:
107
  }
108
  return(res);
109
}
110
 
111
 
112
/* find next matching, ie. continues an action intiated by findfirst() */
113
unsigned short findnext(struct DTA *dta) {
114
  unsigned short res = 0;
115
  _asm {
116
    mov ah, 0x4f    /* FindNext */
117
    mov dx, dta
118
    int 0x21        /* CF set on error + err code in AX, DTA filled with FileInfoRec on success */
119
    jnc DONE
120
    mov [res], ax
121
    DONE:
122
  }
123
  return(res);
124
}
392 mateuszvis 125
 
126
 
127
/* print s string and wait for a single key press from stdin. accepts only
128
 * key presses defined in the c ASCIIZ string. returns offset of pressed key
129
 * in string. keys in c MUST BE UPPERCASE! */
130
unsigned short askchoice(const char *s, const char *c) {
131
  unsigned short res;
132
  char key = 0;
133
 
134
  AGAIN:
135
  output(s);
136
  output(" ");
137
 
138
  _asm {
139
    push ax
140
    push dx
141
 
142
    mov ax, 0x0c01 /* clear input buffer and execute getchar (INT 21h,AH=1) */
143
    int 0x21
144
    /* if AL == 0 then this is an extended character */
145
    test al, al
146
    jnz GOTCHAR
147
    mov ah, 0x08   /* read again to flush extended char from input buffer */
148
    int 0x21
149
    xor al, al     /* all extended chars are ignored */
150
    GOTCHAR:       /* received key is in AL now */
151
    mov [key], al  /* save key */
152
 
153
    /* print a cr/lf */
154
    mov ah, 0x02
155
    mov dl, 0x0D
156
    int 0x21
157
    mov dl, 0x0A
158
    int 0x21
159
 
160
    pop dx
161
    pop ax
162
  }
163
 
164
  /* ucase() result */
165
  if ((key >= 'a') && (key <= 'z')) key -= ('a' - 'A');
166
 
167
  /* is there a match? */
168
  for (res = 0; c[res] != 0; res++) if (c[res] == key) return(res);
169
 
170
  goto AGAIN;
171
}
172
 
173
 
399 mateuszvis 174
/* converts a path to its canonic representation, returns 0 on success
175
 * or DOS err on failure (invalid drive) */
176
unsigned short file_truename(const char *src, char *dst) {
177
  unsigned short res = 0;
392 mateuszvis 178
  _asm {
399 mateuszvis 179
    push es
392 mateuszvis 180
    mov ah, 0x60  /* query truename, DS:SI=src, ES:DI=dst */
181
    push ds
182
    pop es
183
    mov si, src
184
    mov di, dst
185
    int 0x21
399 mateuszvis 186
    jnc DONE
187
    mov [res], ax
188
    DONE:
189
    pop es
392 mateuszvis 190
  }
399 mateuszvis 191
  return(res);
392 mateuszvis 192
}
193
 
194
 
195
/* returns DOS attributes of file, or -1 on error */
196
int file_getattr(const char *fname) {
197
  int res = -1;
198
  _asm {
199
    mov ax, 0x4300  /* query file attributes, fname at DS:DX */
200
    mov dx, fname
201
    int 0x21        /* CX=attributes if CF=0, otherwise AX=errno */
202
    jc DONE
203
    mov [res], cx
204
    DONE:
205
  }
206
  return(res);
207
}
396 mateuszvis 208
 
209
 
210
/* returns screen's width (in columns) */
211
unsigned short screen_getwidth(void) {
212
  /* BIOS 0040:004A = word containing screen width in text columns */
213
  unsigned short far *scrw = MK_FP(0x40, 0x4a);
214
  return(*scrw);
215
}
216
 
217
 
218
/* returns screen's height (in rows) */
219
unsigned short screen_getheight(void) {
220
  /* BIOS 0040:0084 = byte containing maximum valid row value (EGA ONLY) */
221
  unsigned char far *scrh = MK_FP(0x40, 0x84);
222
  if (*scrh == 0) return(25);  /* pre-EGA adapter */
223
  return(*scrh + 1);
224
}
225
 
226
 
227
/* displays the "Press any key to continue" msg and waits for a keypress */
228
void press_any_key(void) {
229
  output("Press any key to continue...");
230
  _asm {
231
    mov ah, 0x08  /* no echo console input */
232
    int 0x21      /* pressed key in AL now (0 for extended keys) */
233
    test al, al
234
    jnz DONE
235
    int 0x21      /* executed ah=8 again to read the rest of extended key */
236
    DONE:
237
    /* output CR/LF */
238
    mov ah, 0x02
239
    mov dl, 0x0D
240
    int 0x21
241
    mov dl, 0x0A
242
    int 0x21
243
  }
244
}
399 mateuszvis 245
 
246
 
247
/* validate a drive (A=0, B=1, etc). returns 1 if valid, 0 otherwise */
248
int isdrivevalid(unsigned char drv) {
249
  _asm {
250
    mov ah, 0x19  /* query default (current) disk */
251
    int 0x21      /* drive in AL (0=A, 1=B, etc) */
252
    mov ch, al    /* save current drive to ch */
253
    /* try setting up the drive as current */
254
    mov ah, 0x0E   /* select default drive */
255
    mov dl, [drv]  /* 0=A, 1=B, etc */
256
    int 0x21
257
    /* this call does not set CF on error, I must check cur drive to look for success */
258
    mov ah, 0x19  /* query default (current) disk */
259
    int 0x21      /* drive in AL (0=A, 1=B, etc) */
260
    mov [drv], 1  /* preset result as success */
261
    cmp al, dl    /* is eq? */
262
    je DONE
263
    mov [drv], 0  /* fail */
264
    jmp FAILED
265
    DONE:
266
    /* set current drive back to what it was initially */
267
    mov ah, 0x0E
268
    mov dl, ch
269
    int 0x21
270
    FAILED:
271
  }
272
  return(drv);
273
}
406 mateuszvis 274
 
275
 
276
/* converts a 8+3 filename into 11-bytes FCB format (MYFILE  EXT) */
277
void file_fname2fcb(char *dst, const char *src) {
278
  unsigned short i;
279
 
280
  /* fill dst with 11 spaces and a NULL terminator */
420 mateuszvis 281
  for (i = 0; i < 11; i++) dst[i] = ' ';
282
  dst[11] = 0;
406 mateuszvis 283
 
284
  /* copy fname until dot (.) or 8 characters */
285
  for (i = 0; i < 8; i++) {
286
    if ((src[i] == '.') || (src[i] == 0)) break;
287
    dst[i] = src[i];
288
  }
289
 
290
  /* advance src until extension or end of string */
291
  src += i;
292
  for (;;) {
293
    if (*src == '.') {
294
      src++; /* next character is extension */
295
      break;
296
    }
297
    if (*src == 0) break;
298
  }
299
 
300
  /* copy extension to dst (3 chars max) */
301
  dst += 8;
302
  for (i = 0; i < 3; i++) {
303
    if (src[i] == 0) break;
304
    dst[i] = src[i];
305
  }
306
}
307
 
308
 
309
/* converts a 11-bytes FCB filename (MYFILE  EXT) into 8+3 format (MYFILE.EXT) */
310
void file_fcb2fname(char *dst, const char *src) {
311
  unsigned short i, end = 0;
312
 
313
  for (i = 0; i < 8; i++) {
314
    dst[i] = src[i];
315
    if (dst[i] != ' ') end = i + 1;
316
  }
317
 
318
  /* is there an extension? */
319
  if (src[8] == ' ') {
320
    dst[end] = 0;
321
  } else { /* found extension: copy it until first space */
322
    dst[end++] = '.';
323
    for (i = 8; i < 11; i++) {
324
      if (src[i] == ' ') break;
325
      dst[end++] = src[i];
326
    }
327
    dst[end] = 0;
328
  }
329
}
410 mateuszvis 330
 
331
 
332
/* converts an ASCIIZ string into an unsigned short. returns 0 on success. */
333
int atouns(unsigned short *r, const char *s) {
334
  int err = 0;
335
 
336
  _asm {
337
    mov si, s
338
    xor ax, ax  /* general purpose register */
339
    xor cx, cx  /* contains the result */
340
    mov bx, 10  /* used as a multiplicative step */
341
 
342
    NEXTBYTE:
343
    xchg cx, ax /* move result into cx temporarily */
344
    lodsb  /* AL = DS:[SI++] */
345
    /* is AL 0? if so we're done */
346
    test al, al
347
    jz DONE
348
    /* validate that AL is in range '0'-'9' */
349
    sub al, '0'
350
    jc FAIL   /* neg result */
351
    cmp al, 9
352
    jg FAIL
353
    /* restore result into AX (CX contains the new digit) */
354
    xchg cx, ax
355
    /* multiply result by 10 and add cl */
356
    mul bx    /* DX AX = AX * BX(10) */
357
    jc FAIL   /* overflow */
358
    add ax, cx
359
    /* if CF then overflow occured (overflow part lands in DX) */
360
    jnc NEXTBYTE
361
 
362
    FAIL:
363
    inc [err]
364
 
365
    DONE: /* save result (CX) into indirect memory address r */
366
    mov bx, [r]
367
    mov [bx], cx
368
  }
369
  return(err);
370
}
415 mateuszvis 371
 
372
 
373
/* appends a backslash if path is a directory
374
 * returns the (possibly updated) length of path */
375
unsigned short path_appendbkslash_if_dir(char *path) {
376
  unsigned short len;
377
  int attr;
378
  for (len = 0; path[len] != 0; len++);
379
  if (len == 0) return(0);
380
  if (path[len - 1] == '\\') return(len);
381
  /* */
382
  attr = file_getattr(path);
383
  if ((attr > 0) && (attr & DOS_ATTR_DIR)) {
384
    path[len++] = '\\';
385
    path[len] = 0;
386
  }
387
  return(len);
388
}
416 mateuszvis 389
 
390
 
391
/* get current path drive d (A=1, B=2, etc - 0 is "current drive")
392
 * returns 0 on success, doserr otherwise */
393
unsigned short curpathfordrv(char *buff, unsigned char d) {
394
  unsigned short r = 0;
395
 
396
  _asm {
397
    /* is d == 0? then I need to resolve current drive */
398
    cmp byte ptr [d], 0
399
    jne GETCWD
400
    /* resolve cur drive */
401
    mov ah, 0x19  /* get current default drive */
402
    int 0x21      /* al = drive (00h = A:, 01h = B:, etc) */
403
    inc al        /* convert to 1=A, 2=B, etc */
404
    mov [d], al
405
 
406
    GETCWD:
407
    /* prepend buff with drive:\ */
408
    mov si, buff
409
    mov dl, [d]
410
    mov [si], dl
411
    add byte ptr [si], 'A' - 1
412
    inc si
413
    mov [si], ':'
414
    inc si
415
    mov [si], '\\'
416
    inc si
417
 
418
    mov ah, 0x47      /* get current directory of drv DL into DS:SI */
419
    int 0x21
420
    jnc DONE
421
    mov [r], ax       /* copy result from ax */
422
 
423
    DONE:
424
  }
425
 
426
  return(r);
427
}
420 mateuszvis 428
 
429
 
430
/* fills a nls_patterns struct with current NLS patterns, returns 0 on success, DOS errcode otherwise */
431
unsigned short nls_getpatterns(struct nls_patterns *p) {
432
  unsigned short r = 0;
433
 
434
  _asm {
435
    mov ax, 0x3800  /* DOS 2+ -- Get Country Info for current country */
436
    mov dx, p       /* DS:DX points to the CountryInfoRec buffer */
437
    int 0x21
438
    jnc DONE
439
    mov [r], ax     /* copy DOS err code to r */
440
    DONE:
441
  }
442
 
443
  return(r);
444
}
445
 
446
 
447
/* computes a formatted date based on NLS patterns found in p
448
 * returns length of result */
449
unsigned short nls_format_date(char *s, unsigned short yr, unsigned char mo, unsigned char dy, const struct nls_patterns *p) {
450
  unsigned short items[3];
451
  /* preset date/month/year in proper order depending on date format */
452
  switch (p->dateformat) {
453
    case 0:  /* USA style: m d y */
454
      items[0] = mo;
455
      items[1] = dy;
456
      items[2] = yr;
457
      break;
458
    case 1:  /* EU style: d m y */
459
      items[0] = dy;
460
      items[1] = mo;
461
      items[2] = yr;
462
      break;
463
    case 2:  /* Japan-style: y m d */
464
    default:
465
      items[0] = yr;
466
      items[1] = mo;
467
      items[2] = dy;
468
      break;
469
  }
470
  /* compute the string */
471
  return(sprintf(s, "%02u%s%02u%s%02u", items[0], p->datesep, items[1], p->datesep, items[2]));
472
}
473
 
474
 
475
/* computes a formatted time based on NLS patterns found in p
476
 * returns length of result */
477
unsigned short nls_format_time(char *s, unsigned char ho, unsigned char mn, const struct nls_patterns *p) {
478
  char ampm[2] = {0, 0};
479
  const char *fmt = "%02u%s%02u%s";
480
  if (p->timefmt == 0) {
481
    if (ho == 12) {
482
      ampm[0] = 'p';
483
    } else if (ho > 12) {
484
      ho -= 12;
485
      ampm[0] = 'p';
486
    } else { /* ho < 12 */
487
      if (ho == 0) ho = 12;
488
      ampm[0] = 'a';
489
    }
490
    fmt = "%2u%s%02u%s";
491
  }
492
  return(sprintf(s, fmt, ho, p->timesep, mn, ampm));
493
}
494
 
495
 
496
/* computes a formatted integer number based on NLS patterns found in p
497
 * returns length of result */
498
unsigned short nls_format_number(char *s, long num, const struct nls_patterns *p) {
499
  unsigned short sl = 0, res, i;
500
  unsigned char thcount = 0;
501
 
502
  /* handle negative values */
503
  if (num < 0) {
504
    s[sl++] = '-';
505
    num = 0 - num;
506
  }
507
 
508
  /* write the absolute value now */
509
  do {
510
    if ((thcount == 3) && (p->thousep[0] != 0)) {
511
      s[sl++] = p->thousep[0];
512
      thcount = 0;
513
    }
514
    s[sl++] = '0' + num % 10;
515
    num /= 10;
516
    thcount++;
517
  } while (num > 0);
518
 
519
  /* terminate the string and remember its size */
520
  s[sl] = 0;
521
  res = sl;
522
 
523
  /* reverse the string now (it has been build in reverse) */
524
  if (s[0] == '-') {
525
    sl--;
526
    s++;
527
  }
528
  for (i = 0; i <= (sl / 2); i++) {
529
    thcount = s[i];
530
    s[i] = s[sl - (i + 1)];
531
    s[sl - (i + 1)] = thcount;
532
  }
533
 
534
  return(res);
535
}