Subversion Repositories SvarDOS

Rev

Rev 1997 | Rev 2195 | 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
 *
1629 mateusz.vi 4
 * Copyright (C) 2021-2024 Mateusz Viste
421 mateuszvis 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
 */
28
 
396 mateuszvis 29
#include <i86.h>    /* MK_FP() */
420 mateuszvis 30
#include <stdio.h>  /* sprintf() */
437 mateuszvis 31
#include <string.h> /* memcpy() */
396 mateuszvis 32
 
968 mateusz.vi 33
#include "svarlang.lib\svarlang.h"
34
 
437 mateuszvis 35
#include "env.h"
1881 mateusz.vi 36
#include "rmodinit.h"
437 mateuszvis 37
 
352 mateuszvis 38
#include "helpers.h"
39
 
420 mateuszvis 40
 
965 mateusz.vi 41
 
1823 mateusz.vi 42
void dos_get_date(unsigned short *y, unsigned char *m, unsigned char *d) {
43
  /* get cur date */
44
  _asm {
45
    mov ah, 0x2a  /* DOS 1+ -- Query DOS Date */
46
    int 0x21      /* CX=year DH=month DL=day */
47
    mov bx, y
48
    mov [bx], cx
49
    mov bx, m
50
    mov [bx], dh
51
    mov bx, d
52
    mov [bx], dl
53
  }
54
}
55
 
56
 
57
void dos_get_time(unsigned char *h, unsigned char *m, unsigned char *s) {
58
  _asm {
59
    mov ah, 0x2c  /* DOS 1+ -- Query DOS Time */
60
    int 0x21      /* CH=hour CL=minutes DH=seconds DL=1/100sec */
61
    mov bx, h
62
    mov [bx], ch
63
    mov bx, m
64
    mov [bx], cl
65
    mov bx, s
66
    mov [bx], dh
67
  }
68
}
69
 
70
 
530 mateuszvis 71
/* case-insensitive comparison of strings, compares up to maxlen characters.
72
 * returns non-zero on equality. */
73
int imatchlim(const char *s1, const char *s2, unsigned short maxlen) {
74
  while (maxlen--) {
352 mateuszvis 75
    char c1, c2;
76
    c1 = *s1;
77
    c2 = *s2;
78
    if ((c1 >= 'a') && (c1 <= 'z')) c1 -= ('a' - 'A');
79
    if ((c2 >= 'a') && (c2 <= 'z')) c2 -= ('a' - 'A');
80
    /* */
81
    if (c1 != c2) return(0);
530 mateuszvis 82
    if (c1 == 0) break;
352 mateuszvis 83
    s1++;
84
    s2++;
85
  }
530 mateuszvis 86
  return(1);
352 mateuszvis 87
}
88
 
89
 
90
/* returns zero if s1 starts with s2 */
91
int strstartswith(const char *s1, const char *s2) {
92
  while (*s2 != 0) {
93
    if (*s1 != *s2) return(-1);
94
    s1++;
95
    s2++;
96
  }
97
  return(0);
98
}
369 mateuszvis 99
 
100
 
538 mateuszvis 101
/* outputs a NULL-terminated string to handle (1=stdout 2=stderr) */
102
void output_internal(const char *s, unsigned char nl, unsigned char handle) {
103
  const static unsigned char *crlf = "\r\n";
369 mateuszvis 104
  _asm {
445 mateuszvis 105
    push ds
106
    pop es         /* make sure es=ds (scasb uses es) */
107
    /* get length of s into CX */
108
    mov ax, 0x4000 /* ah=DOS "write to file" and AL=0 for NULL matching */
109
    mov dx, s      /* set dx to string (required for later) */
110
    mov di, dx     /* set di to string (for NULL matching) */
111
    mov cx, 0xffff /* preset cx to 65535 (-1) */
112
    cld            /* clear DF so scasb increments DI */
113
    repne scasb    /* cmp al, es:[di], inc di, dec cx until match found */
114
    /* CX contains (65535 - strlen(s)) now */
115
    not cx         /* reverse all bits so I get (strlen(s) + 1) */
116
    dec cx         /* this is CX length */
117
    jz WRITEDONE   /* do nothing for empty strings */
118
 
119
    /* output by writing to stdout */
120
    /* mov ah, 0x40 */  /* DOS 2+ -- write to file via handle */
538 mateuszvis 121
    xor bh, bh
122
    mov bl, handle /* set handle (1=stdout 2=stderr) */
445 mateuszvis 123
    /* mov cx, xxx */ /* write CX bytes */
124
    /* mov dx, s   */ /* DS:DX is the source of bytes to "write" */
369 mateuszvis 125
    int 0x21
445 mateuszvis 126
    WRITEDONE:
127
 
128
    /* print out a CR/LF trailer if nl set */
538 mateuszvis 129
    test byte ptr [nl], 0xff
369 mateuszvis 130
    jz FINITO
538 mateuszvis 131
    /* bx still contains handle */
132
    mov ah, 0x40 /* "write to file" */
133
    mov cx, 2
445 mateuszvis 134
    mov dx, crlf
369 mateuszvis 135
    int 0x21
136
    FINITO:
137
  }
138
}
388 mateuszvis 139
 
140
 
542 mateuszvis 141
void nls_output_internal(unsigned short id, unsigned char nl, unsigned char handle) {
538 mateuszvis 142
  const char *NOTFOUND = "NLS_STRING_NOT_FOUND";
968 mateusz.vi 143
  const char *ptr = svarlang_strid(id);
985 mateusz.vi 144
  if ((ptr == NULL) || (ptr[0]) == 0) ptr = NOTFOUND;
542 mateuszvis 145
  output_internal(ptr, nl, handle);
538 mateuszvis 146
}
147
 
148
 
959 mateusz.vi 149
/* output DOS error e to stdout, if stdout is redirected then *additionally*
150
 * also to stderr */
538 mateuszvis 151
void nls_outputnl_doserr(unsigned short e) {
152
  static char errstr[16];
153
  const char *ptr = NULL;
959 mateusz.vi 154
  unsigned char redirflag = 0;
538 mateuszvis 155
  /* find string in nls block */
968 mateusz.vi 156
  if (e < 0xff) ptr = svarlang_strid(0xff00 | e);
538 mateuszvis 157
  /* if not found, use a fallback */
1046 mateusz.vi 158
  if ((ptr == NULL) || (ptr[0] == 0)) {
538 mateuszvis 159
    sprintf(errstr, "DOS ERR %u", e);
160
    ptr = errstr;
161
  }
959 mateusz.vi 162
 
163
  /* display to stdout */
164
  output_internal(ptr, 1, hSTDOUT);
165
 
166
  /* is stdout redirected? */
167
  _asm {
168
    push bx
169
    push dx
170
 
171
    mov ax, 0x4400   /* query device flags */
172
    mov bx, 1        /* stdout */
173
    int 0x21
174
    /* CF set on error and AX filled with DOS error,
175
     * returns flags in DX on succes:
176
     *  bit 7 reset if handle points to a file, set if handle points to a device  */
177
    jc FAIL
178
    mov redirflag, dl
179
    and redirflag, 128
180
 
181
    FAIL:
182
    pop dx
183
    pop bx
184
  }
185
 
186
  if (redirflag == 0) output_internal(ptr, 1, hSTDERR);
538 mateuszvis 187
}
188
 
189
 
388 mateuszvis 190
/* find first matching files using a FindFirst DOS call
191
 * returns 0 on success or a DOS err code on failure */
192
unsigned short findfirst(struct DTA *dta, const char *pattern, unsigned short attr) {
193
  unsigned short res = 0;
194
  _asm {
195
    /* set DTA location */
196
    mov ah, 0x1a
197
    mov dx, dta
198
    int 0x21
199
    /* */
200
    mov ah, 0x4e    /* FindFirst */
201
    mov dx, pattern
202
    mov cx, attr
203
    int 0x21        /* CF set on error + err code in AX, DTA filled with FileInfoRec on success */
204
    jnc DONE
205
    mov [res], ax
206
    DONE:
207
  }
208
  return(res);
209
}
210
 
211
 
212
/* find next matching, ie. continues an action intiated by findfirst() */
213
unsigned short findnext(struct DTA *dta) {
214
  unsigned short res = 0;
215
  _asm {
216
    mov ah, 0x4f    /* FindNext */
217
    mov dx, dta
218
    int 0x21        /* CF set on error + err code in AX, DTA filled with FileInfoRec on success */
219
    jnc DONE
220
    mov [res], ax
221
    DONE:
222
  }
223
  return(res);
224
}
392 mateuszvis 225
 
226
 
1997 mateusz.vi 227
static unsigned char _dos_getkey_noecho(void);
228
#pragma aux _dos_getkey_noecho = \
229
"mov ax, 0x0c08" /* clear input buffer and execute getchar (INT 21h,AH=8) */  \
230
"int 0x21"                                                                    \
231
"test al, al"    /* if AL == 0 then this is an extended character */          \
232
"jnz GOTCHAR"                                                                 \
233
"mov ah, 0x08"   /* read again to flush extended char from input buffer */    \
234
"int 0x21"                                                                    \
235
"xor al, al"     /* all extended chars are ignored */                         \
236
"GOTCHAR:"       /* received key is in AL now */                              \
237
modify [ah]                                                                   \
238
value [al]
239
 
240
 
392 mateuszvis 241
/* print s string and wait for a single key press from stdin. accepts only
242
 * key presses defined in the c ASCIIZ string. returns offset of pressed key
1997 mateusz.vi 243
 * in string. keys in c MUST BE UPPERCASE! ENTER chooses the FIRST choice */
392 mateuszvis 244
unsigned short askchoice(const char *s, const char *c) {
245
  unsigned short res;
1001 mateusz.vi 246
  char cstr[2] = {0,0};
392 mateuszvis 247
  char key = 0;
248
 
249
  output(s);
250
  output(" ");
1001 mateusz.vi 251
  output("(");
252
  for (res = 0; c[res] != 0; res++) {
253
    if (res != 0) output("/");
254
    cstr[0] = c[res];
255
    output(cstr);
256
  }
257
  output(") ");
392 mateuszvis 258
 
1997 mateusz.vi 259
  AGAIN:
260
  key = _dos_getkey_noecho();
261
  if (key == '\r') key = c[0]; /* ENTER is synonym for the first key */
392 mateuszvis 262
 
263
  /* ucase() result */
264
  if ((key >= 'a') && (key <= 'z')) key -= ('a' - 'A');
265
 
266
  /* is there a match? */
1997 mateusz.vi 267
  for (res = 0; c[res] != 0; res++) {
268
    if (c[res] == key) {
269
      cstr[0] = key;
270
      output(cstr);
271
      output("\r\n");
272
      return(res);
273
    }
274
  }
392 mateuszvis 275
 
276
  goto AGAIN;
277
}
278
 
279
 
399 mateuszvis 280
/* converts a path to its canonic representation, returns 0 on success
281
 * or DOS err on failure (invalid drive) */
282
unsigned short file_truename(const char *src, char *dst) {
283
  unsigned short res = 0;
392 mateuszvis 284
  _asm {
399 mateuszvis 285
    push es
392 mateuszvis 286
    mov ah, 0x60  /* query truename, DS:SI=src, ES:DI=dst */
287
    push ds
288
    pop es
289
    mov si, src
290
    mov di, dst
291
    int 0x21
399 mateuszvis 292
    jnc DONE
293
    mov [res], ax
294
    DONE:
295
    pop es
392 mateuszvis 296
  }
399 mateuszvis 297
  return(res);
392 mateuszvis 298
}
299
 
300
 
301
/* returns DOS attributes of file, or -1 on error */
302
int file_getattr(const char *fname) {
303
  int res = -1;
304
  _asm {
305
    mov ax, 0x4300  /* query file attributes, fname at DS:DX */
306
    mov dx, fname
307
    int 0x21        /* CX=attributes if CF=0, otherwise AX=errno */
308
    jc DONE
309
    mov [res], cx
310
    DONE:
311
  }
312
  return(res);
313
}
396 mateuszvis 314
 
315
 
316
/* returns screen's width (in columns) */
317
unsigned short screen_getwidth(void) {
318
  /* BIOS 0040:004A = word containing screen width in text columns */
319
  unsigned short far *scrw = MK_FP(0x40, 0x4a);
320
  return(*scrw);
321
}
322
 
323
 
324
/* returns screen's height (in rows) */
325
unsigned short screen_getheight(void) {
326
  /* BIOS 0040:0084 = byte containing maximum valid row value (EGA ONLY) */
327
  unsigned char far *scrh = MK_FP(0x40, 0x84);
328
  if (*scrh == 0) return(25);  /* pre-EGA adapter */
329
  return(*scrh + 1);
330
}
331
 
332
 
333
/* displays the "Press any key to continue" msg and waits for a keypress */
334
void press_any_key(void) {
437 mateuszvis 335
  nls_output(15, 1); /* Press any key to continue... */
396 mateuszvis 336
  _asm {
337
    mov ah, 0x08  /* no echo console input */
338
    int 0x21      /* pressed key in AL now (0 for extended keys) */
339
    test al, al
340
    jnz DONE
341
    int 0x21      /* executed ah=8 again to read the rest of extended key */
342
    DONE:
343
    /* output CR/LF */
344
    mov ah, 0x02
345
    mov dl, 0x0D
346
    int 0x21
347
    mov dl, 0x0A
348
    int 0x21
349
  }
350
}
399 mateuszvis 351
 
352
 
353
/* validate a drive (A=0, B=1, etc). returns 1 if valid, 0 otherwise */
354
int isdrivevalid(unsigned char drv) {
355
  _asm {
356
    mov ah, 0x19  /* query default (current) disk */
357
    int 0x21      /* drive in AL (0=A, 1=B, etc) */
358
    mov ch, al    /* save current drive to ch */
359
    /* try setting up the drive as current */
360
    mov ah, 0x0E   /* select default drive */
361
    mov dl, [drv]  /* 0=A, 1=B, etc */
362
    int 0x21
363
    /* this call does not set CF on error, I must check cur drive to look for success */
364
    mov ah, 0x19  /* query default (current) disk */
365
    int 0x21      /* drive in AL (0=A, 1=B, etc) */
366
    mov [drv], 1  /* preset result as success */
367
    cmp al, dl    /* is eq? */
368
    je DONE
369
    mov [drv], 0  /* fail */
370
    jmp FAILED
371
    DONE:
372
    /* set current drive back to what it was initially */
373
    mov ah, 0x0E
374
    mov dl, ch
375
    int 0x21
376
    FAILED:
377
  }
378
  return(drv);
379
}
406 mateuszvis 380
 
381
 
382
/* converts a 8+3 filename into 11-bytes FCB format (MYFILE  EXT) */
383
void file_fname2fcb(char *dst, const char *src) {
384
  unsigned short i;
385
 
386
  /* fill dst with 11 spaces and a NULL terminator */
420 mateuszvis 387
  for (i = 0; i < 11; i++) dst[i] = ' ';
388
  dst[11] = 0;
406 mateuszvis 389
 
390
  /* copy fname until dot (.) or 8 characters */
391
  for (i = 0; i < 8; i++) {
392
    if ((src[i] == '.') || (src[i] == 0)) break;
393
    dst[i] = src[i];
394
  }
395
 
396
  /* advance src until extension or end of string */
397
  src += i;
398
  for (;;) {
399
    if (*src == '.') {
400
      src++; /* next character is extension */
401
      break;
402
    }
403
    if (*src == 0) break;
404
  }
405
 
406
  /* copy extension to dst (3 chars max) */
407
  dst += 8;
408
  for (i = 0; i < 3; i++) {
409
    if (src[i] == 0) break;
410
    dst[i] = src[i];
411
  }
412
}
413
 
414
 
415
/* converts a 11-bytes FCB filename (MYFILE  EXT) into 8+3 format (MYFILE.EXT) */
416
void file_fcb2fname(char *dst, const char *src) {
417
  unsigned short i, end = 0;
418
 
419
  for (i = 0; i < 8; i++) {
420
    dst[i] = src[i];
421
    if (dst[i] != ' ') end = i + 1;
422
  }
423
 
424
  /* is there an extension? */
425
  if (src[8] == ' ') {
426
    dst[end] = 0;
427
  } else { /* found extension: copy it until first space */
428
    dst[end++] = '.';
429
    for (i = 8; i < 11; i++) {
430
      if (src[i] == ' ') break;
431
      dst[end++] = src[i];
432
    }
433
    dst[end] = 0;
434
  }
435
}
410 mateuszvis 436
 
437
 
430 mateuszvis 438
/* converts an ASCIIZ string into an unsigned short. returns 0 on success.
439
 * on error, result will contain all valid digits that were read until
440
 * error occurred (0 on overflow or if parsing failed immediately) */
426 mateuszvis 441
int atous(unsigned short *r, const char *s) {
410 mateuszvis 442
  int err = 0;
443
 
444
  _asm {
445
    mov si, s
446
    xor ax, ax  /* general purpose register */
447
    xor cx, cx  /* contains the result */
448
    mov bx, 10  /* used as a multiplicative step */
449
 
450
    NEXTBYTE:
451
    xchg cx, ax /* move result into cx temporarily */
452
    lodsb  /* AL = DS:[SI++] */
453
    /* is AL 0? if so we're done */
454
    test al, al
455
    jz DONE
456
    /* validate that AL is in range '0'-'9' */
457
    sub al, '0'
430 mateuszvis 458
    jc FAIL   /* invalid character detected */
410 mateuszvis 459
    cmp al, 9
430 mateuszvis 460
    jg FAIL   /* invalid character detected */
410 mateuszvis 461
    /* restore result into AX (CX contains the new digit) */
462
    xchg cx, ax
463
    /* multiply result by 10 and add cl */
464
    mul bx    /* DX AX = AX * BX(10) */
430 mateuszvis 465
    jc OVERFLOW  /* overflow */
410 mateuszvis 466
    add ax, cx
430 mateuszvis 467
    /* if CF is set then overflow occurred (overflow part lands in DX) */
410 mateuszvis 468
    jnc NEXTBYTE
469
 
430 mateuszvis 470
    OVERFLOW:
471
    xor cx, cx  /* make sure result is zeroed in case overflow occured */
472
 
410 mateuszvis 473
    FAIL:
474
    inc [err]
475
 
476
    DONE: /* save result (CX) into indirect memory address r */
477
    mov bx, [r]
478
    mov [bx], cx
479
  }
480
  return(err);
481
}
415 mateuszvis 482
 
483
 
484
/* appends a backslash if path is a directory
485
 * returns the (possibly updated) length of path */
486
unsigned short path_appendbkslash_if_dir(char *path) {
487
  unsigned short len;
488
  int attr;
489
  for (len = 0; path[len] != 0; len++);
490
  if (len == 0) return(0);
491
  if (path[len - 1] == '\\') return(len);
492
  /* */
493
  attr = file_getattr(path);
494
  if ((attr > 0) && (attr & DOS_ATTR_DIR)) {
495
    path[len++] = '\\';
496
    path[len] = 0;
497
  }
498
  return(len);
499
}
416 mateuszvis 500
 
501
 
502
/* get current path drive d (A=1, B=2, etc - 0 is "current drive")
503
 * returns 0 on success, doserr otherwise */
504
unsigned short curpathfordrv(char *buff, unsigned char d) {
505
  unsigned short r = 0;
506
 
507
  _asm {
508
    /* is d == 0? then I need to resolve current drive */
509
    cmp byte ptr [d], 0
510
    jne GETCWD
511
    /* resolve cur drive */
512
    mov ah, 0x19  /* get current default drive */
513
    int 0x21      /* al = drive (00h = A:, 01h = B:, etc) */
514
    inc al        /* convert to 1=A, 2=B, etc */
515
    mov [d], al
516
 
517
    GETCWD:
518
    /* prepend buff with drive:\ */
519
    mov si, buff
520
    mov dl, [d]
521
    mov [si], dl
522
    add byte ptr [si], 'A' - 1
523
    inc si
524
    mov [si], ':'
525
    inc si
526
    mov [si], '\\'
527
    inc si
528
 
529
    mov ah, 0x47      /* get current directory of drv DL into DS:SI */
530
    int 0x21
531
    jnc DONE
532
    mov [r], ax       /* copy result from ax */
533
 
534
    DONE:
535
  }
536
 
537
  return(r);
538
}
420 mateuszvis 539
 
540
 
541
/* fills a nls_patterns struct with current NLS patterns, returns 0 on success, DOS errcode otherwise */
542
unsigned short nls_getpatterns(struct nls_patterns *p) {
543
  unsigned short r = 0;
544
 
545
  _asm {
546
    mov ax, 0x3800  /* DOS 2+ -- Get Country Info for current country */
547
    mov dx, p       /* DS:DX points to the CountryInfoRec buffer */
548
    int 0x21
549
    jnc DONE
550
    mov [r], ax     /* copy DOS err code to r */
551
    DONE:
552
  }
553
 
554
  return(r);
555
}
556
 
557
 
558
/* computes a formatted date based on NLS patterns found in p
559
 * returns length of result */
560
unsigned short nls_format_date(char *s, unsigned short yr, unsigned char mo, unsigned char dy, const struct nls_patterns *p) {
561
  unsigned short items[3];
562
  /* preset date/month/year in proper order depending on date format */
563
  switch (p->dateformat) {
564
    case 0:  /* USA style: m d y */
565
      items[0] = mo;
566
      items[1] = dy;
567
      items[2] = yr;
568
      break;
569
    case 1:  /* EU style: d m y */
570
      items[0] = dy;
571
      items[1] = mo;
572
      items[2] = yr;
573
      break;
574
    case 2:  /* Japan-style: y m d */
575
    default:
576
      items[0] = yr;
577
      items[1] = mo;
578
      items[2] = dy;
579
      break;
580
  }
581
  /* compute the string */
582
  return(sprintf(s, "%02u%s%02u%s%02u", items[0], p->datesep, items[1], p->datesep, items[2]));
583
}
584
 
585
 
426 mateuszvis 586
/* computes a formatted time based on NLS patterns found in p, sc are ignored if set 0xff
420 mateuszvis 587
 * returns length of result */
426 mateuszvis 588
unsigned short nls_format_time(char *s, unsigned char ho, unsigned char mn, unsigned char sc, const struct nls_patterns *p) {
589
  char ampm = 0;
590
  unsigned short res;
591
 
420 mateuszvis 592
  if (p->timefmt == 0) {
593
    if (ho == 12) {
426 mateuszvis 594
      ampm = 'p';
420 mateuszvis 595
    } else if (ho > 12) {
596
      ho -= 12;
426 mateuszvis 597
      ampm = 'p';
420 mateuszvis 598
    } else { /* ho < 12 */
599
      if (ho == 0) ho = 12;
426 mateuszvis 600
      ampm = 'a';
420 mateuszvis 601
    }
426 mateuszvis 602
    res = sprintf(s, "%2u", ho);
603
  } else {
604
    res = sprintf(s, "%02u", ho);
420 mateuszvis 605
  }
426 mateuszvis 606
 
607
  /* append separator and minutes */
608
  res += sprintf(s + res, "%s%02u", p->timesep, mn);
609
 
610
  /* if seconds provided, append them, too */
611
  if (sc != 0xff) res += sprintf(s + res, "%s%02u", p->timesep, sc);
612
 
613
  /* finally append AM/PM char */
614
  if (ampm != 0) s[res++] = ampm;
615
  s[res] = 0;
616
 
617
  return(res);
420 mateuszvis 618
}
619
 
620
 
621
/* computes a formatted integer number based on NLS patterns found in p
622
 * returns length of result */
423 mateuszvis 623
unsigned short nls_format_number(char *s, unsigned long num, const struct nls_patterns *p) {
624
  unsigned short sl = 0, i;
420 mateuszvis 625
  unsigned char thcount = 0;
626
 
423 mateuszvis 627
  /* write the value (reverse) with thousand separators (if any defined) */
420 mateuszvis 628
  do {
629
    if ((thcount == 3) && (p->thousep[0] != 0)) {
630
      s[sl++] = p->thousep[0];
631
      thcount = 0;
632
    }
633
    s[sl++] = '0' + num % 10;
634
    num /= 10;
635
    thcount++;
636
  } while (num > 0);
637
 
423 mateuszvis 638
  /* terminate the string */
420 mateuszvis 639
  s[sl] = 0;
640
 
423 mateuszvis 641
  /* reverse the string now (has been built in reverse) */
642
  for (i = sl / 2 + (sl & 1); i < sl; i++) {
420 mateuszvis 643
    thcount = s[i];
423 mateuszvis 644
    s[i] = s[sl - (i + 1)];   /* abc'de  if i=3 then ' <-> c */
420 mateuszvis 645
    s[sl - (i + 1)] = thcount;
646
  }
647
 
423 mateuszvis 648
  return(sl);
420 mateuszvis 649
}
437 mateuszvis 650
 
651
 
1137 mateusz.vi 652
/* capitalize an ASCIZ string following country-dependent rules */
653
void nls_strtoup(char *buff) {
654
  unsigned short errcode = 0;
655
  /* requires DOS 4+ */
656
  _asm {
657
    push ax
658
    push dx
659
 
660
    mov ax, 0x6522 /* country-dependent capitalize string (DOS 4+) */
661
    mov dx, buff   /* DS:DX -> string to capitalize */
662
    int 0x21
663
    jnc DONE
664
 
665
    mov errcode, ax /* set errcode on failure */
666
    DONE:
667
 
668
    pop dx
669
    pop ax
670
  }
671
 
672
  /* rely on OpenWatcom's strupr() if DOS has no NLS support */
673
  if (errcode != 0) strupr(buff);
674
}
675
 
676
 
1881 mateusz.vi 677
/* reload nls ressources from svarcom.lng into svarlang_mem and rmod */
678
void nls_langreload(char *buff, unsigned short rmodseg) {
1629 mateusz.vi 679
  const char far *dosdir;
968 mateusz.vi 680
  const char far *lang;
965 mateusz.vi 681
  static unsigned short lastlang;
1629 mateusz.vi 682
  unsigned short dosdirlen;
1881 mateusz.vi 683
  unsigned short rmodenvseg = *(unsigned short far *)MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
684
  unsigned char far *rmodcritmsg = MK_FP(rmodseg, RMOD_OFFSET_CRITMSG);
685
  int i;
437 mateuszvis 686
 
687
  /* look up the LANG env variable, upcase it and copy to lang */
1881 mateusz.vi 688
  lang = env_lookup_val(rmodenvseg, "LANG");
968 mateusz.vi 689
  if ((lang == NULL) || (lang[0] == 0)) return;
690
  _fmemcpy(buff, lang, 2);
437 mateuszvis 691
  buff[2] = 0;
692
 
693
  /* check if there is need to reload at all */
968 mateusz.vi 694
  if (memcmp(&lastlang, buff, 2) == 0) return;
437 mateuszvis 695
 
968 mateusz.vi 696
  buff[4] = 0;
1881 mateusz.vi 697
  dosdir = env_lookup_val(rmodenvseg, "DOSDIR");
1629 mateusz.vi 698
  if (dosdir == NULL) return;
437 mateuszvis 699
 
1629 mateusz.vi 700
  _fstrcpy(buff + 4, dosdir);
701
  dosdirlen = strlen(buff + 4);
702
  if (buff[4 + dosdirlen - 1] == '\\') dosdirlen--;
703
  memcpy(buff + 4 + dosdirlen, "\\SVARCOM.LNG", 13);
437 mateuszvis 704
 
1629 mateusz.vi 705
  /* try loading %DOSDIR%\SVARCOM.LNG */
706
  if (svarlang_load(buff + 4, buff) != 0) {
1881 mateusz.vi 707
    /* failed! try %DOSDIR%\BIN\SVARCOM.LNG */
1629 mateusz.vi 708
    memcpy(buff + 4 + dosdirlen, "\\BIN\\SVARCOM.LNG", 17);
709
    if (svarlang_load(buff + 4, buff) != 0) return;
710
  }
711
 
968 mateusz.vi 712
  _fmemcpy(&lastlang, lang, 2);
1881 mateusz.vi 713
 
714
  /* update RMOD's critical handler with new strings */
2161 mateusz.vi 715
  for (i = 0; i < 9; i++) {
1881 mateusz.vi 716
    int len;
717
    len = strlen(svarlang_str(3, i));
718
    if (len > 15) len = 15;
719
    _fmemcpy(rmodcritmsg + (i * 16), svarlang_str(3, i), len);
720
    _fmemcpy(rmodcritmsg + (i * 16) + len, "$", 1);
721
  }
722
  /* The ARIF string is special: always 4 bytes long and no $ terminator */
2161 mateusz.vi 723
  _fmemcpy(rmodcritmsg + (9 * 16), svarlang_str(3,9), 4);
437 mateuszvis 724
}
571 mateuszvis 725
 
726
 
727
/* locates executable fname in path and fill res with result. returns 0 on success,
728
 * -1 on failed match and -2 on failed match + "don't even try with other paths"
729
 * extptr is filled with a ptr to the extension in fname (NULL if no extension) */
730
int lookup_cmd(char *res, const char *fname, const char *path, const char **extptr) {
731
  unsigned short lastbslash = 0;
732
  unsigned short i, len;
733
  unsigned char explicitpath = 0;
1072 mateusz.vi 734
  const char *exec_ext[] = {"COM", "EXE", "BAT", NULL};
571 mateuszvis 735
 
736
  /* does the original fname has an explicit path prefix or explicit ext? */
737
  *extptr = NULL;
738
  for (i = 0; fname[i] != 0; i++) {
739
    switch (fname[i]) {
740
      case ':':
741
      case '\\':
742
        explicitpath = 1;
743
        *extptr = NULL; /* extension is the last dot AFTER all path delimiters */
744
        break;
745
      case '.':
746
        *extptr = fname + i + 1;
747
        break;
748
    }
749
  }
750
 
1072 mateusz.vi 751
  /* if explicit ext found, make sure it is executable */
752
  if (*extptr != NULL) {
753
    for (i = 0; exec_ext[i] != NULL; i++) if (imatch(*extptr, exec_ext[i])) break;
754
    if (exec_ext[i] == NULL) return(-2); /* bad extension - don't try running it ever */
755
  }
756
 
571 mateuszvis 757
  /* normalize filename */
758
  if (file_truename(fname, res) != 0) return(-2);
759
 
760
  /* printf("truename: %s\r\n", res); */
761
 
1072 mateusz.vi 762
  /* figure out where the command starts */
571 mateuszvis 763
  for (len = 0; res[len] != 0; len++) {
764
    switch (res[len]) {
765
      case '?':   /* abort on any wildcard character */
766
      case '*':
767
        return(-2);
768
      case '\\':
769
        lastbslash = len;
770
        break;
771
    }
772
  }
773
 
774
  /* printf("lastbslash=%u\r\n", lastbslash); */
775
 
776
  /* if no path prefix was found in fname (no colon or backslash) AND we have
777
   * a path arg, then assemble path+filename */
778
  if ((!explicitpath) && (path != NULL) && (path[0] != 0)) {
779
    i = strlen(path);
780
    if (path[i - 1] != '\\') i++; /* add a byte for inserting a bkslash after path */
781
    /* move the filename at the place where path will end */
782
    memmove(res + i, res + lastbslash + 1, len - lastbslash);
783
    /* copy path in front of the filename and make sure there is a bkslash sep */
784
    memmove(res, path, i);
785
    res[i - 1] = '\\';
786
  }
787
 
788
  /* if no extension was initially provided, try matching COM, EXE, BAT */
789
  if (*extptr == NULL) {
1072 mateusz.vi 790
    int attr;
571 mateuszvis 791
    len = strlen(res);
1072 mateusz.vi 792
    res[len++] = '.';
793
    for (i = 0; exec_ext[i] != NULL; i++) {
794
      strcpy(res + len, exec_ext[i]);
571 mateuszvis 795
      /* printf("? '%s'\r\n", res); */
1072 mateusz.vi 796
      *extptr = exec_ext[i];
797
      attr = file_getattr(res);
798
      if (attr < 0) continue; /* file not found */
799
      if (attr & DOS_ATTR_DIR) continue; /* this is a directory */
800
      if (attr & DOS_ATTR_VOL) continue; /* this is a volume */
801
      return(0);
571 mateuszvis 802
    }
803
  } else { /* try finding it as-is */
804
    /* printf("? '%s'\r\n", res); */
1072 mateusz.vi 805
    int attr = file_getattr(res);
806
    if ((attr >= 0) &&  /* file exists */
807
        ((attr & DOS_ATTR_DIR) == 0) && /* is not a directory */
808
        ((attr & DOS_ATTR_VOL) == 0)) { /* is not a volume */
809
      return(0);
810
    }
571 mateuszvis 811
  }
812
 
813
  /* not found */
814
  if (explicitpath) return(-2); /* don't bother trying other paths, the caller had its own path preset anyway */
815
  return(-1);
816
}
817
 
818
 
819
/* fills fname with the path and filename to the linkfile related to the
820
 * executable link "linkname". returns 0 on success. */
821
int link_computefname(char *fname, const char *linkname, unsigned short env_seg) {
1044 mateusz.vi 822
  unsigned short pathlen, doserr = 0;
571 mateuszvis 823
 
824
  /* fetch %DOSDIR% */
825
  pathlen = env_lookup_valcopy(fname, 128, env_seg, "DOSDIR");
1988 mateusz.vi 826
  if (pathlen == 0) return(-1);
571 mateuszvis 827
 
828
  /* prep filename: %DOSDIR%\LINKS\PKG.LNK */
829
  if (fname[pathlen - 1] == '\\') pathlen--;
1043 mateusz.vi 830
  pathlen += sprintf(fname + pathlen, "\\LINKS");
1044 mateusz.vi 831
  /* create \LINKS if not exists */
832
  if (file_getattr(fname) < 0) {
833
    _asm {
834
      push dx
835
      mov ah, 0x39
836
      mov dx, fname
837
      int 0x21
838
      jnc DONE
839
      mov doserr, ax
840
      DONE:
841
      pop dx
842
    }
843
    if (doserr) {
844
      output(fname);
845
      output(" - ");
846
      nls_outputnl(255, doserr);
847
      return(-1);
848
    }
849
  }
850
  /* quit early if dir does not exist (or is not a dir) */
1043 mateusz.vi 851
  if (file_getattr(fname) != DOS_ATTR_DIR) {
852
    output(fname);
853
    output(" - ");
854
    nls_outputnl(255,3); /* path not found */
855
    return(-1);
856
  }
857
  sprintf(fname + pathlen, "\\%s.LNK", linkname);
571 mateuszvis 858
 
859
  return(0);
860
}