Subversion Repositories SvarDOS

Rev

Rev 1823 | Rev 1988 | 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
 
227
/* print s string and wait for a single key press from stdin. accepts only
228
 * key presses defined in the c ASCIIZ string. returns offset of pressed key
229
 * in string. keys in c MUST BE UPPERCASE! */
230
unsigned short askchoice(const char *s, const char *c) {
231
  unsigned short res;
1001 mateusz.vi 232
  char cstr[2] = {0,0};
392 mateuszvis 233
  char key = 0;
234
 
235
  AGAIN:
236
  output(s);
237
  output(" ");
1001 mateusz.vi 238
  output("(");
239
  for (res = 0; c[res] != 0; res++) {
240
    if (res != 0) output("/");
241
    cstr[0] = c[res];
242
    output(cstr);
243
  }
244
  output(") ");
392 mateuszvis 245
 
246
  _asm {
247
    push ax
248
    push dx
249
 
250
    mov ax, 0x0c01 /* clear input buffer and execute getchar (INT 21h,AH=1) */
251
    int 0x21
252
    /* if AL == 0 then this is an extended character */
253
    test al, al
254
    jnz GOTCHAR
255
    mov ah, 0x08   /* read again to flush extended char from input buffer */
256
    int 0x21
257
    xor al, al     /* all extended chars are ignored */
258
    GOTCHAR:       /* received key is in AL now */
259
    mov [key], al  /* save key */
260
 
261
    /* print a cr/lf */
262
    mov ah, 0x02
263
    mov dl, 0x0D
264
    int 0x21
265
    mov dl, 0x0A
266
    int 0x21
267
 
268
    pop dx
269
    pop ax
270
  }
271
 
272
  /* ucase() result */
273
  if ((key >= 'a') && (key <= 'z')) key -= ('a' - 'A');
274
 
275
  /* is there a match? */
276
  for (res = 0; c[res] != 0; res++) if (c[res] == key) return(res);
277
 
278
  goto AGAIN;
279
}
280
 
281
 
399 mateuszvis 282
/* converts a path to its canonic representation, returns 0 on success
283
 * or DOS err on failure (invalid drive) */
284
unsigned short file_truename(const char *src, char *dst) {
285
  unsigned short res = 0;
392 mateuszvis 286
  _asm {
399 mateuszvis 287
    push es
392 mateuszvis 288
    mov ah, 0x60  /* query truename, DS:SI=src, ES:DI=dst */
289
    push ds
290
    pop es
291
    mov si, src
292
    mov di, dst
293
    int 0x21
399 mateuszvis 294
    jnc DONE
295
    mov [res], ax
296
    DONE:
297
    pop es
392 mateuszvis 298
  }
399 mateuszvis 299
  return(res);
392 mateuszvis 300
}
301
 
302
 
303
/* returns DOS attributes of file, or -1 on error */
304
int file_getattr(const char *fname) {
305
  int res = -1;
306
  _asm {
307
    mov ax, 0x4300  /* query file attributes, fname at DS:DX */
308
    mov dx, fname
309
    int 0x21        /* CX=attributes if CF=0, otherwise AX=errno */
310
    jc DONE
311
    mov [res], cx
312
    DONE:
313
  }
314
  return(res);
315
}
396 mateuszvis 316
 
317
 
318
/* returns screen's width (in columns) */
319
unsigned short screen_getwidth(void) {
320
  /* BIOS 0040:004A = word containing screen width in text columns */
321
  unsigned short far *scrw = MK_FP(0x40, 0x4a);
322
  return(*scrw);
323
}
324
 
325
 
326
/* returns screen's height (in rows) */
327
unsigned short screen_getheight(void) {
328
  /* BIOS 0040:0084 = byte containing maximum valid row value (EGA ONLY) */
329
  unsigned char far *scrh = MK_FP(0x40, 0x84);
330
  if (*scrh == 0) return(25);  /* pre-EGA adapter */
331
  return(*scrh + 1);
332
}
333
 
334
 
335
/* displays the "Press any key to continue" msg and waits for a keypress */
336
void press_any_key(void) {
437 mateuszvis 337
  nls_output(15, 1); /* Press any key to continue... */
396 mateuszvis 338
  _asm {
339
    mov ah, 0x08  /* no echo console input */
340
    int 0x21      /* pressed key in AL now (0 for extended keys) */
341
    test al, al
342
    jnz DONE
343
    int 0x21      /* executed ah=8 again to read the rest of extended key */
344
    DONE:
345
    /* output CR/LF */
346
    mov ah, 0x02
347
    mov dl, 0x0D
348
    int 0x21
349
    mov dl, 0x0A
350
    int 0x21
351
  }
352
}
399 mateuszvis 353
 
354
 
355
/* validate a drive (A=0, B=1, etc). returns 1 if valid, 0 otherwise */
356
int isdrivevalid(unsigned char drv) {
357
  _asm {
358
    mov ah, 0x19  /* query default (current) disk */
359
    int 0x21      /* drive in AL (0=A, 1=B, etc) */
360
    mov ch, al    /* save current drive to ch */
361
    /* try setting up the drive as current */
362
    mov ah, 0x0E   /* select default drive */
363
    mov dl, [drv]  /* 0=A, 1=B, etc */
364
    int 0x21
365
    /* this call does not set CF on error, I must check cur drive to look for success */
366
    mov ah, 0x19  /* query default (current) disk */
367
    int 0x21      /* drive in AL (0=A, 1=B, etc) */
368
    mov [drv], 1  /* preset result as success */
369
    cmp al, dl    /* is eq? */
370
    je DONE
371
    mov [drv], 0  /* fail */
372
    jmp FAILED
373
    DONE:
374
    /* set current drive back to what it was initially */
375
    mov ah, 0x0E
376
    mov dl, ch
377
    int 0x21
378
    FAILED:
379
  }
380
  return(drv);
381
}
406 mateuszvis 382
 
383
 
384
/* converts a 8+3 filename into 11-bytes FCB format (MYFILE  EXT) */
385
void file_fname2fcb(char *dst, const char *src) {
386
  unsigned short i;
387
 
388
  /* fill dst with 11 spaces and a NULL terminator */
420 mateuszvis 389
  for (i = 0; i < 11; i++) dst[i] = ' ';
390
  dst[11] = 0;
406 mateuszvis 391
 
392
  /* copy fname until dot (.) or 8 characters */
393
  for (i = 0; i < 8; i++) {
394
    if ((src[i] == '.') || (src[i] == 0)) break;
395
    dst[i] = src[i];
396
  }
397
 
398
  /* advance src until extension or end of string */
399
  src += i;
400
  for (;;) {
401
    if (*src == '.') {
402
      src++; /* next character is extension */
403
      break;
404
    }
405
    if (*src == 0) break;
406
  }
407
 
408
  /* copy extension to dst (3 chars max) */
409
  dst += 8;
410
  for (i = 0; i < 3; i++) {
411
    if (src[i] == 0) break;
412
    dst[i] = src[i];
413
  }
414
}
415
 
416
 
417
/* converts a 11-bytes FCB filename (MYFILE  EXT) into 8+3 format (MYFILE.EXT) */
418
void file_fcb2fname(char *dst, const char *src) {
419
  unsigned short i, end = 0;
420
 
421
  for (i = 0; i < 8; i++) {
422
    dst[i] = src[i];
423
    if (dst[i] != ' ') end = i + 1;
424
  }
425
 
426
  /* is there an extension? */
427
  if (src[8] == ' ') {
428
    dst[end] = 0;
429
  } else { /* found extension: copy it until first space */
430
    dst[end++] = '.';
431
    for (i = 8; i < 11; i++) {
432
      if (src[i] == ' ') break;
433
      dst[end++] = src[i];
434
    }
435
    dst[end] = 0;
436
  }
437
}
410 mateuszvis 438
 
439
 
430 mateuszvis 440
/* converts an ASCIIZ string into an unsigned short. returns 0 on success.
441
 * on error, result will contain all valid digits that were read until
442
 * error occurred (0 on overflow or if parsing failed immediately) */
426 mateuszvis 443
int atous(unsigned short *r, const char *s) {
410 mateuszvis 444
  int err = 0;
445
 
446
  _asm {
447
    mov si, s
448
    xor ax, ax  /* general purpose register */
449
    xor cx, cx  /* contains the result */
450
    mov bx, 10  /* used as a multiplicative step */
451
 
452
    NEXTBYTE:
453
    xchg cx, ax /* move result into cx temporarily */
454
    lodsb  /* AL = DS:[SI++] */
455
    /* is AL 0? if so we're done */
456
    test al, al
457
    jz DONE
458
    /* validate that AL is in range '0'-'9' */
459
    sub al, '0'
430 mateuszvis 460
    jc FAIL   /* invalid character detected */
410 mateuszvis 461
    cmp al, 9
430 mateuszvis 462
    jg FAIL   /* invalid character detected */
410 mateuszvis 463
    /* restore result into AX (CX contains the new digit) */
464
    xchg cx, ax
465
    /* multiply result by 10 and add cl */
466
    mul bx    /* DX AX = AX * BX(10) */
430 mateuszvis 467
    jc OVERFLOW  /* overflow */
410 mateuszvis 468
    add ax, cx
430 mateuszvis 469
    /* if CF is set then overflow occurred (overflow part lands in DX) */
410 mateuszvis 470
    jnc NEXTBYTE
471
 
430 mateuszvis 472
    OVERFLOW:
473
    xor cx, cx  /* make sure result is zeroed in case overflow occured */
474
 
410 mateuszvis 475
    FAIL:
476
    inc [err]
477
 
478
    DONE: /* save result (CX) into indirect memory address r */
479
    mov bx, [r]
480
    mov [bx], cx
481
  }
482
  return(err);
483
}
415 mateuszvis 484
 
485
 
486
/* appends a backslash if path is a directory
487
 * returns the (possibly updated) length of path */
488
unsigned short path_appendbkslash_if_dir(char *path) {
489
  unsigned short len;
490
  int attr;
491
  for (len = 0; path[len] != 0; len++);
492
  if (len == 0) return(0);
493
  if (path[len - 1] == '\\') return(len);
494
  /* */
495
  attr = file_getattr(path);
496
  if ((attr > 0) && (attr & DOS_ATTR_DIR)) {
497
    path[len++] = '\\';
498
    path[len] = 0;
499
  }
500
  return(len);
501
}
416 mateuszvis 502
 
503
 
504
/* get current path drive d (A=1, B=2, etc - 0 is "current drive")
505
 * returns 0 on success, doserr otherwise */
506
unsigned short curpathfordrv(char *buff, unsigned char d) {
507
  unsigned short r = 0;
508
 
509
  _asm {
510
    /* is d == 0? then I need to resolve current drive */
511
    cmp byte ptr [d], 0
512
    jne GETCWD
513
    /* resolve cur drive */
514
    mov ah, 0x19  /* get current default drive */
515
    int 0x21      /* al = drive (00h = A:, 01h = B:, etc) */
516
    inc al        /* convert to 1=A, 2=B, etc */
517
    mov [d], al
518
 
519
    GETCWD:
520
    /* prepend buff with drive:\ */
521
    mov si, buff
522
    mov dl, [d]
523
    mov [si], dl
524
    add byte ptr [si], 'A' - 1
525
    inc si
526
    mov [si], ':'
527
    inc si
528
    mov [si], '\\'
529
    inc si
530
 
531
    mov ah, 0x47      /* get current directory of drv DL into DS:SI */
532
    int 0x21
533
    jnc DONE
534
    mov [r], ax       /* copy result from ax */
535
 
536
    DONE:
537
  }
538
 
539
  return(r);
540
}
420 mateuszvis 541
 
542
 
543
/* fills a nls_patterns struct with current NLS patterns, returns 0 on success, DOS errcode otherwise */
544
unsigned short nls_getpatterns(struct nls_patterns *p) {
545
  unsigned short r = 0;
546
 
547
  _asm {
548
    mov ax, 0x3800  /* DOS 2+ -- Get Country Info for current country */
549
    mov dx, p       /* DS:DX points to the CountryInfoRec buffer */
550
    int 0x21
551
    jnc DONE
552
    mov [r], ax     /* copy DOS err code to r */
553
    DONE:
554
  }
555
 
556
  return(r);
557
}
558
 
559
 
560
/* computes a formatted date based on NLS patterns found in p
561
 * returns length of result */
562
unsigned short nls_format_date(char *s, unsigned short yr, unsigned char mo, unsigned char dy, const struct nls_patterns *p) {
563
  unsigned short items[3];
564
  /* preset date/month/year in proper order depending on date format */
565
  switch (p->dateformat) {
566
    case 0:  /* USA style: m d y */
567
      items[0] = mo;
568
      items[1] = dy;
569
      items[2] = yr;
570
      break;
571
    case 1:  /* EU style: d m y */
572
      items[0] = dy;
573
      items[1] = mo;
574
      items[2] = yr;
575
      break;
576
    case 2:  /* Japan-style: y m d */
577
    default:
578
      items[0] = yr;
579
      items[1] = mo;
580
      items[2] = dy;
581
      break;
582
  }
583
  /* compute the string */
584
  return(sprintf(s, "%02u%s%02u%s%02u", items[0], p->datesep, items[1], p->datesep, items[2]));
585
}
586
 
587
 
426 mateuszvis 588
/* computes a formatted time based on NLS patterns found in p, sc are ignored if set 0xff
420 mateuszvis 589
 * returns length of result */
426 mateuszvis 590
unsigned short nls_format_time(char *s, unsigned char ho, unsigned char mn, unsigned char sc, const struct nls_patterns *p) {
591
  char ampm = 0;
592
  unsigned short res;
593
 
420 mateuszvis 594
  if (p->timefmt == 0) {
595
    if (ho == 12) {
426 mateuszvis 596
      ampm = 'p';
420 mateuszvis 597
    } else if (ho > 12) {
598
      ho -= 12;
426 mateuszvis 599
      ampm = 'p';
420 mateuszvis 600
    } else { /* ho < 12 */
601
      if (ho == 0) ho = 12;
426 mateuszvis 602
      ampm = 'a';
420 mateuszvis 603
    }
426 mateuszvis 604
    res = sprintf(s, "%2u", ho);
605
  } else {
606
    res = sprintf(s, "%02u", ho);
420 mateuszvis 607
  }
426 mateuszvis 608
 
609
  /* append separator and minutes */
610
  res += sprintf(s + res, "%s%02u", p->timesep, mn);
611
 
612
  /* if seconds provided, append them, too */
613
  if (sc != 0xff) res += sprintf(s + res, "%s%02u", p->timesep, sc);
614
 
615
  /* finally append AM/PM char */
616
  if (ampm != 0) s[res++] = ampm;
617
  s[res] = 0;
618
 
619
  return(res);
420 mateuszvis 620
}
621
 
622
 
623
/* computes a formatted integer number based on NLS patterns found in p
624
 * returns length of result */
423 mateuszvis 625
unsigned short nls_format_number(char *s, unsigned long num, const struct nls_patterns *p) {
626
  unsigned short sl = 0, i;
420 mateuszvis 627
  unsigned char thcount = 0;
628
 
423 mateuszvis 629
  /* write the value (reverse) with thousand separators (if any defined) */
420 mateuszvis 630
  do {
631
    if ((thcount == 3) && (p->thousep[0] != 0)) {
632
      s[sl++] = p->thousep[0];
633
      thcount = 0;
634
    }
635
    s[sl++] = '0' + num % 10;
636
    num /= 10;
637
    thcount++;
638
  } while (num > 0);
639
 
423 mateuszvis 640
  /* terminate the string */
420 mateuszvis 641
  s[sl] = 0;
642
 
423 mateuszvis 643
  /* reverse the string now (has been built in reverse) */
644
  for (i = sl / 2 + (sl & 1); i < sl; i++) {
420 mateuszvis 645
    thcount = s[i];
423 mateuszvis 646
    s[i] = s[sl - (i + 1)];   /* abc'de  if i=3 then ' <-> c */
420 mateuszvis 647
    s[sl - (i + 1)] = thcount;
648
  }
649
 
423 mateuszvis 650
  return(sl);
420 mateuszvis 651
}
437 mateuszvis 652
 
653
 
1137 mateusz.vi 654
/* capitalize an ASCIZ string following country-dependent rules */
655
void nls_strtoup(char *buff) {
656
  unsigned short errcode = 0;
657
  /* requires DOS 4+ */
658
  _asm {
659
    push ax
660
    push dx
661
 
662
    mov ax, 0x6522 /* country-dependent capitalize string (DOS 4+) */
663
    mov dx, buff   /* DS:DX -> string to capitalize */
664
    int 0x21
665
    jnc DONE
666
 
667
    mov errcode, ax /* set errcode on failure */
668
    DONE:
669
 
670
    pop dx
671
    pop ax
672
  }
673
 
674
  /* rely on OpenWatcom's strupr() if DOS has no NLS support */
675
  if (errcode != 0) strupr(buff);
676
}
677
 
678
 
1881 mateusz.vi 679
/* reload nls ressources from svarcom.lng into svarlang_mem and rmod */
680
void nls_langreload(char *buff, unsigned short rmodseg) {
1629 mateusz.vi 681
  const char far *dosdir;
968 mateusz.vi 682
  const char far *lang;
965 mateusz.vi 683
  static unsigned short lastlang;
1629 mateusz.vi 684
  unsigned short dosdirlen;
1881 mateusz.vi 685
  unsigned short rmodenvseg = *(unsigned short far *)MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
686
  unsigned char far *rmodcritmsg = MK_FP(rmodseg, RMOD_OFFSET_CRITMSG);
687
  int i;
437 mateuszvis 688
 
689
  /* look up the LANG env variable, upcase it and copy to lang */
1881 mateusz.vi 690
  lang = env_lookup_val(rmodenvseg, "LANG");
968 mateusz.vi 691
  if ((lang == NULL) || (lang[0] == 0)) return;
692
  _fmemcpy(buff, lang, 2);
437 mateuszvis 693
  buff[2] = 0;
694
 
695
  /* check if there is need to reload at all */
968 mateusz.vi 696
  if (memcmp(&lastlang, buff, 2) == 0) return;
437 mateuszvis 697
 
968 mateusz.vi 698
  buff[4] = 0;
1881 mateusz.vi 699
  dosdir = env_lookup_val(rmodenvseg, "DOSDIR");
1629 mateusz.vi 700
  if (dosdir == NULL) return;
437 mateuszvis 701
 
1629 mateusz.vi 702
  _fstrcpy(buff + 4, dosdir);
703
  dosdirlen = strlen(buff + 4);
704
  if (buff[4 + dosdirlen - 1] == '\\') dosdirlen--;
705
  memcpy(buff + 4 + dosdirlen, "\\SVARCOM.LNG", 13);
437 mateuszvis 706
 
1629 mateusz.vi 707
  /* try loading %DOSDIR%\SVARCOM.LNG */
708
  if (svarlang_load(buff + 4, buff) != 0) {
1881 mateusz.vi 709
    /* failed! try %DOSDIR%\BIN\SVARCOM.LNG */
1629 mateusz.vi 710
    memcpy(buff + 4 + dosdirlen, "\\BIN\\SVARCOM.LNG", 17);
711
    if (svarlang_load(buff + 4, buff) != 0) return;
712
  }
713
 
968 mateusz.vi 714
  _fmemcpy(&lastlang, lang, 2);
1881 mateusz.vi 715
 
716
  /* update RMOD's critical handler with new strings */
717
  for (i = 0; i < 7; i++) {
718
    int len;
719
    len = strlen(svarlang_str(3, i));
720
    if (len > 15) len = 15;
721
    _fmemcpy(rmodcritmsg + (i * 16), svarlang_str(3, i), len);
722
    _fmemcpy(rmodcritmsg + (i * 16) + len, "$", 1);
723
  }
724
  /* The ARIF string is special: always 4 bytes long and no $ terminator */
725
  _fmemcpy(rmodcritmsg + (7 * 16), svarlang_str(3,9), 4);
437 mateuszvis 726
}
571 mateuszvis 727
 
728
 
729
/* locates executable fname in path and fill res with result. returns 0 on success,
730
 * -1 on failed match and -2 on failed match + "don't even try with other paths"
731
 * extptr is filled with a ptr to the extension in fname (NULL if no extension) */
732
int lookup_cmd(char *res, const char *fname, const char *path, const char **extptr) {
733
  unsigned short lastbslash = 0;
734
  unsigned short i, len;
735
  unsigned char explicitpath = 0;
1072 mateusz.vi 736
  const char *exec_ext[] = {"COM", "EXE", "BAT", NULL};
571 mateuszvis 737
 
738
  /* does the original fname has an explicit path prefix or explicit ext? */
739
  *extptr = NULL;
740
  for (i = 0; fname[i] != 0; i++) {
741
    switch (fname[i]) {
742
      case ':':
743
      case '\\':
744
        explicitpath = 1;
745
        *extptr = NULL; /* extension is the last dot AFTER all path delimiters */
746
        break;
747
      case '.':
748
        *extptr = fname + i + 1;
749
        break;
750
    }
751
  }
752
 
1072 mateusz.vi 753
  /* if explicit ext found, make sure it is executable */
754
  if (*extptr != NULL) {
755
    for (i = 0; exec_ext[i] != NULL; i++) if (imatch(*extptr, exec_ext[i])) break;
756
    if (exec_ext[i] == NULL) return(-2); /* bad extension - don't try running it ever */
757
  }
758
 
571 mateuszvis 759
  /* normalize filename */
760
  if (file_truename(fname, res) != 0) return(-2);
761
 
762
  /* printf("truename: %s\r\n", res); */
763
 
1072 mateusz.vi 764
  /* figure out where the command starts */
571 mateuszvis 765
  for (len = 0; res[len] != 0; len++) {
766
    switch (res[len]) {
767
      case '?':   /* abort on any wildcard character */
768
      case '*':
769
        return(-2);
770
      case '\\':
771
        lastbslash = len;
772
        break;
773
    }
774
  }
775
 
776
  /* printf("lastbslash=%u\r\n", lastbslash); */
777
 
778
  /* if no path prefix was found in fname (no colon or backslash) AND we have
779
   * a path arg, then assemble path+filename */
780
  if ((!explicitpath) && (path != NULL) && (path[0] != 0)) {
781
    i = strlen(path);
782
    if (path[i - 1] != '\\') i++; /* add a byte for inserting a bkslash after path */
783
    /* move the filename at the place where path will end */
784
    memmove(res + i, res + lastbslash + 1, len - lastbslash);
785
    /* copy path in front of the filename and make sure there is a bkslash sep */
786
    memmove(res, path, i);
787
    res[i - 1] = '\\';
788
  }
789
 
790
  /* if no extension was initially provided, try matching COM, EXE, BAT */
791
  if (*extptr == NULL) {
1072 mateusz.vi 792
    int attr;
571 mateuszvis 793
    len = strlen(res);
1072 mateusz.vi 794
    res[len++] = '.';
795
    for (i = 0; exec_ext[i] != NULL; i++) {
796
      strcpy(res + len, exec_ext[i]);
571 mateuszvis 797
      /* printf("? '%s'\r\n", res); */
1072 mateusz.vi 798
      *extptr = exec_ext[i];
799
      attr = file_getattr(res);
800
      if (attr < 0) continue; /* file not found */
801
      if (attr & DOS_ATTR_DIR) continue; /* this is a directory */
802
      if (attr & DOS_ATTR_VOL) continue; /* this is a volume */
803
      return(0);
571 mateuszvis 804
    }
805
  } else { /* try finding it as-is */
806
    /* printf("? '%s'\r\n", res); */
1072 mateusz.vi 807
    int attr = file_getattr(res);
808
    if ((attr >= 0) &&  /* file exists */
809
        ((attr & DOS_ATTR_DIR) == 0) && /* is not a directory */
810
        ((attr & DOS_ATTR_VOL) == 0)) { /* is not a volume */
811
      return(0);
812
    }
571 mateuszvis 813
  }
814
 
815
  /* not found */
816
  if (explicitpath) return(-2); /* don't bother trying other paths, the caller had its own path preset anyway */
817
  return(-1);
818
}
819
 
820
 
821
/* fills fname with the path and filename to the linkfile related to the
822
 * executable link "linkname". returns 0 on success. */
823
int link_computefname(char *fname, const char *linkname, unsigned short env_seg) {
1044 mateusz.vi 824
  unsigned short pathlen, doserr = 0;
571 mateuszvis 825
 
826
  /* fetch %DOSDIR% */
827
  pathlen = env_lookup_valcopy(fname, 128, env_seg, "DOSDIR");
828
  if (pathlen == 0) {
1043 mateusz.vi 829
    nls_outputnl(29,5); /* "%DOSDIR% not defined" */
571 mateuszvis 830
    return(-1);
831
  }
832
 
833
  /* prep filename: %DOSDIR%\LINKS\PKG.LNK */
834
  if (fname[pathlen - 1] == '\\') pathlen--;
1043 mateusz.vi 835
  pathlen += sprintf(fname + pathlen, "\\LINKS");
1044 mateusz.vi 836
  /* create \LINKS if not exists */
837
  if (file_getattr(fname) < 0) {
838
    _asm {
839
      push dx
840
      mov ah, 0x39
841
      mov dx, fname
842
      int 0x21
843
      jnc DONE
844
      mov doserr, ax
845
      DONE:
846
      pop dx
847
    }
848
    if (doserr) {
849
      output(fname);
850
      output(" - ");
851
      nls_outputnl(255, doserr);
852
      return(-1);
853
    }
854
  }
855
  /* quit early if dir does not exist (or is not a dir) */
1043 mateusz.vi 856
  if (file_getattr(fname) != DOS_ATTR_DIR) {
857
    output(fname);
858
    output(" - ");
859
    nls_outputnl(255,3); /* path not found */
860
    return(-1);
861
  }
862
  sprintf(fname + pathlen, "\\%s.LNK", linkname);
571 mateuszvis 863
 
864
  return(0);
865
}