Subversion Repositories SvarDOS

Rev

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

Rev Author Line No. Line
585 mateuszvis 1
/*
588 mateuszvis 2
 * Locales configuration for SvarDOS
585 mateuszvis 3
 *
588 mateuszvis 4
 * Copyright (C) Mateusz Viste 2015-2022
5
 *
592 mateuszvis 6
 * MIT license
585 mateuszvis 7
 *
592 mateuszvis 8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
585 mateuszvis 25
 */
26
 
27
#include <stdio.h>
28
#include <stdlib.h> /* atoi() */
29
#include <string.h> /* strchr */
30
 
603 mateuszvis 31
#include "svarlang.lib/svarlang.h"
32
 
585 mateuszvis 33
#include "country.h"
34
 
603 mateuszvis 35
#define PVER "20220203"
588 mateuszvis 36
#define PDATE "2015-2022"
585 mateuszvis 37
 
38
 
603 mateuszvis 39
enum NLS_STRINGS {
40
  NLS_HLP_VER           = 0x0000,
41
  NLS_HLP_DESC          = 0x0001,
42
  NLS_HLP_USAGE         = 0x0002,
43
  NLS_HLP_OPTIONS       = 0x0003,
44
  NLS_HLP_COUNTRY       = 0x000A,
45
  NLS_HLP_CP            = 0x000B,
46
  NLS_HLP_DECIM         = 0x000C,
47
  NLS_HLP_THOUS         = 0x000D,
48
  NLS_HLP_DATESEP       = 0x000E,
49
  NLS_HLP_DATEFMT       = 0x000F,
50
  NLS_HLP_TIMESEP       = 0x0010,
51
  NLS_HLP_TIMEFMT       = 0x0011,
52
  NLS_HLP_CURR          = 0x0012,
53
  NLS_HLP_CURRPOS0      = 0x0013,
54
  NLS_HLP_CURRPOS1      = 0x0014,
55
  NLS_HLP_CURRPOS2      = 0x0015,
56
  NLS_HLP_CURRSPC       = 0x0016,
57
  NLS_HLP_CURRPREC      = 0x0017,
58
  NLS_HLP_YESNO         = 0x0018,
59
  NLS_HLP_INFOLOC1      = 0x0032,
60
  NLS_HLP_INFOLOC2      = 0x0033,
61
 
62
  NLS_INFO_COUNTRY      = 0x0700,
63
  NLS_INFO_CODEPAGE     = 0x0701,
64
  NLS_INFO_DECSEP       = 0x0702,
65
  NLS_INFO_THOUSEP      = 0x0703,
66
  NLS_INFO_DATEFMT      = 0x0704,
67
  NLS_INFO_TIMEFMT      = 0x0705,
68
  NLS_INFO_YESNO        = 0x0706,
69
  NLS_INFO_CURREXAMPLE  = 0x0707,
70
  NLS_MAKESURE          = 0x0709,
71
 
72
  NLS_ERR_FILEPATHTWICE = 0x0900,
73
  NLS_ERR_BADPATH       = 0x0901,
74
  NLS_ERR_READFAIL      = 0x0902,
75
  NLS_ERR_INVPARAM      = 0x0903
76
};
77
 
78
 
606 mateuszvis 79
static void output(const char *s) {
80
  _asm {
81
    /* set cx to strlen(s) */
82
    push ds
83
    pop es
84
    mov di, s
85
    xor al, al
86
    cld
87
    mov cx, 0xff
88
    repne scasb  /* compare ES:DI with AL, inc DI until match */
89
    mov cx, di
90
    sub cx, s
91
    dec cx
92
    /* output via DOS */
93
    mov ah, 0x40  /* write to handle */
94
    mov bx, 1     /* 1=stdout */
95
    mov dx, s
96
    int 0x21
97
  }
603 mateuszvis 98
}
99
 
100
 
606 mateuszvis 101
static void crlf(void) {
102
  output("\r\n");
103
}
104
 
105
 
106
static void outputnl(const char *s) {
107
  output(s);
108
  crlf();
109
}
110
 
111
 
603 mateuszvis 112
static void nls_put(enum NLS_STRINGS id) {
606 mateuszvis 113
  output(svarlang_strid(id));
603 mateuszvis 114
}
115
 
116
 
606 mateuszvis 117
static void nls_puts(enum NLS_STRINGS id) {
118
  nls_put(id);
119
  crlf();
603 mateuszvis 120
}
121
 
122
 
585 mateuszvis 123
static void about(void) {
606 mateuszvis 124
  output("localcfg ");
603 mateuszvis 125
  nls_put(NLS_HLP_VER);
606 mateuszvis 126
  outputnl(" " PVER ", (C) " PDATE " Mateusz Viste");
603 mateuszvis 127
  nls_puts(NLS_HLP_DESC);
128
  crlf();
129
  nls_puts(NLS_HLP_USAGE);
130
  crlf();
131
  nls_puts(NLS_HLP_OPTIONS);
132
  crlf();
133
  nls_puts(NLS_HLP_COUNTRY);
134
  nls_puts(NLS_HLP_CP);
135
  nls_puts(NLS_HLP_DECIM);
136
  nls_puts(NLS_HLP_THOUS);
137
  nls_puts(NLS_HLP_DATESEP);
138
  nls_puts(NLS_HLP_DATEFMT);
139
  nls_puts(NLS_HLP_TIMESEP);
140
  nls_puts(NLS_HLP_TIMEFMT);
141
  nls_puts(NLS_HLP_CURR);
142
  nls_puts(NLS_HLP_CURRPOS0);
143
  nls_puts(NLS_HLP_CURRPOS1);
144
  nls_puts(NLS_HLP_CURRPOS2);
145
  nls_puts(NLS_HLP_CURRSPC);
146
  nls_puts(NLS_HLP_CURRPREC);
147
  nls_puts(NLS_HLP_YESNO);
148
  crlf();
149
  nls_puts(NLS_HLP_INFOLOC1);
150
  nls_puts(NLS_HLP_INFOLOC2);
585 mateuszvis 151
}
152
 
153
 
610 mateuszvis 154
static char *datestring(char *result, struct country *c) {
591 mateuszvis 155
  switch (c->CTYINFO.datefmt) {
585 mateuszvis 156
    case COUNTRY_DATE_MDY:
591 mateuszvis 157
      sprintf(result, "12%c31%c1990", c->CTYINFO.datesep[0], c->CTYINFO.datesep[0]);
585 mateuszvis 158
      break;
159
    case COUNTRY_DATE_DMY:
591 mateuszvis 160
      sprintf(result, "31%c12%c1990", c->CTYINFO.datesep[0], c->CTYINFO.datesep[0]);
585 mateuszvis 161
      break;
162
    case COUNTRY_DATE_YMD:
163
    default:
591 mateuszvis 164
      sprintf(result, "1990%c12%c31", c->CTYINFO.datesep[0], c->CTYINFO.datesep[0]);
585 mateuszvis 165
      break;
166
  }
167
  return(result);
168
}
169
 
170
 
610 mateuszvis 171
static char *timestring(char *result, struct country *c) {
591 mateuszvis 172
  if (c->CTYINFO.timefmt == COUNTRY_TIME12) {
173
    sprintf(result, "11%c59%c59 PM", c->CTYINFO.timesep[0], c->CTYINFO.timesep[0]);
585 mateuszvis 174
  } else {
591 mateuszvis 175
    sprintf(result, "23%c59%c59", c->CTYINFO.timesep[0], c->CTYINFO.timesep[0]);
585 mateuszvis 176
  }
177
  return(result);
178
}
179
 
180
 
610 mateuszvis 181
static char *currencystring(char *result, struct country *c) {
585 mateuszvis 182
  char decimalpart[16];
183
  char space[2] = {0, 0};
184
  char decsym[8];
185
  char cursym[8];
186
  decimalpart[0] = '1';
187
  decimalpart[1] = '2';
188
  decimalpart[2] = '3';
189
  decimalpart[3] = '4';
190
  decimalpart[4] = '5';
191
  decimalpart[5] = '6';
192
  decimalpart[6] = '7';
193
  decimalpart[7] = '8';
194
  decimalpart[8] = '9';
195
  decimalpart[9] = 0;
196
  /* prepare the decimal string first */
591 mateuszvis 197
  if (c->CTYINFO.currprec < 9) {
198
    decimalpart[c->CTYINFO.currprec] = 0;
585 mateuszvis 199
  }
200
  /* prepare the currency space string */
591 mateuszvis 201
  if (c->CTYINFO.currspace != 0) {
585 mateuszvis 202
    space[0] = ' ';
203
  }
204
  /* prepare the currency and decimal symbols */
591 mateuszvis 205
  if (c->CTYINFO.currdecsym != 0) { /* currency replaces the decimal point */
206
    sprintf(decsym, "%s", c->CTYINFO.currsym);
585 mateuszvis 207
    cursym[0] = 0;
208
  } else {
591 mateuszvis 209
    sprintf(decsym, "%c", c->CTYINFO.decimal[0]);
210
    sprintf(cursym, "%s", c->CTYINFO.currsym);
585 mateuszvis 211
  }
591 mateuszvis 212
  if (c->CTYINFO.currprec == 0) decsym[0] = 0;
585 mateuszvis 213
  /* compute the final string */
591 mateuszvis 214
  if (c->CTYINFO.currpos == 0) { /* currency precedes value */
585 mateuszvis 215
    sprintf(result, "%s%s99%s%s", cursym, space, decsym, decimalpart);
216
  } else { /* currency follows value or replaces decimal symbol */
217
    sprintf(result, "99%s%s%s%s", decsym, decimalpart, space, cursym);
218
  }
219
  return(result);
220
}
221
 
222
 
223
/* checks if str starts with prefix. returns 0 if so, non-zero otherwise. */
224
static int stringstartswith(char *str, char *prefix) {
225
  for (;;) {
226
    /* end of prefix means success */
227
    if (*prefix == 0) return(0);
228
    /* otherwise there is no match */
229
    if (*str != *prefix) return(-1);
230
    /* if match good so far, look at next char */
231
    str += 1;
232
    prefix += 1;
233
  }
234
}
235
 
236
 
237
/* processes an argument. returns 0 on success, non-zero otherwise. */
238
static int processarg(char *arg, struct country *c) {
239
  char *value;
240
  int intvalue;
241
  /* an option must start with a '/' */
242
  if (arg[0] != '/') return(-1);
243
  arg += 1; /* skip the slash */
244
  /* find where the value starts */
245
  value = strchr(arg, ':');
246
  /* if no value present, fail */
247
  if (value == NULL) return(-2);
248
  value += 1;
249
  if (*value == 0) return(-3);
250
  /* interpret the option now */
251
  if (stringstartswith(arg, "country:") == 0) {
252
    intvalue = atoi(value);
253
    if ((intvalue > 0) && (intvalue < 1000)) {
591 mateuszvis 254
      c->CTYINFO.id = intvalue;
585 mateuszvis 255
      return(0);
256
    }
257
  } else if (stringstartswith(arg, "cp:") == 0) {
258
    intvalue = atoi(value);
259
    if ((intvalue > 0) && (intvalue < 1000)) {
591 mateuszvis 260
      c->CTYINFO.codepage = intvalue;
585 mateuszvis 261
      return(0);
262
    }
263
  } else if (stringstartswith(arg, "decim:") == 0) {
264
    if (value[1] == 0) { /* value must be exactly one character */
591 mateuszvis 265
      c->CTYINFO.decimal[0] = *value;
585 mateuszvis 266
      return(0);
267
    }
268
  } else if (stringstartswith(arg, "thous:") == 0) {
269
    if (value[1] == 0) { /* value must be exactly one character */
591 mateuszvis 270
      c->CTYINFO.thousands[0] = *value;
585 mateuszvis 271
      return(0);
272
    }
273
  } else if (stringstartswith(arg, "datesep:") == 0) {
274
    if (value[1] == 0) { /* value must be exactly one character */
591 mateuszvis 275
      c->CTYINFO.datesep[0] = *value;
585 mateuszvis 276
      return(0);
277
    }
278
  } else if (stringstartswith(arg, "timesep:") == 0) {
279
    if (value[1] == 0) { /* value must be exactly one character */
591 mateuszvis 280
      c->CTYINFO.timesep[0] = *value;
585 mateuszvis 281
      return(0);
282
    }
283
  } else if (stringstartswith(arg, "datefmt:") == 0) {
284
    if (strcmp(value, "MDY") == 0) {
591 mateuszvis 285
      c->CTYINFO.datefmt = COUNTRY_DATE_MDY;
585 mateuszvis 286
      return(0);
287
    } else if (strcmp(value, "DMY") == 0) {
591 mateuszvis 288
      c->CTYINFO.datefmt = COUNTRY_DATE_DMY;
585 mateuszvis 289
      return(0);
290
    } else if (strcmp(value, "YMD") == 0) {
591 mateuszvis 291
      c->CTYINFO.datefmt = COUNTRY_DATE_YMD;
585 mateuszvis 292
      return(0);
293
    }
294
  } else if (stringstartswith(arg, "timefmt:") == 0) {
295
    if (value[1] == 0) {
296
      if ((value[0] >= '0') && (value[0] <= '1')) {
591 mateuszvis 297
        c->CTYINFO.timefmt = value[0] - '0';
585 mateuszvis 298
        return(0);
299
      }
300
    }
301
  } else if (stringstartswith(arg, "curr:") == 0) {
302
    if (strlen(value) <= 4) {
591 mateuszvis 303
      strcpy(c->CTYINFO.currsym, value);
585 mateuszvis 304
      return(0);
305
    }
306
  } else if (stringstartswith(arg, "currpos:") == 0) {
307
    if (value[1] == 0) {
308
      if (value[0] == '0') {
591 mateuszvis 309
        c->CTYINFO.currpos = 0;
585 mateuszvis 310
        return(0);
311
      } else if (value[0] == '1') {
591 mateuszvis 312
        c->CTYINFO.currpos = 1;
585 mateuszvis 313
        return(0);
314
      } else if (value[0] == '2') {
591 mateuszvis 315
        c->CTYINFO.currpos = 0;
316
        c->CTYINFO.currdecsym = 1;
585 mateuszvis 317
        return(0);
318
      }
319
    }
320
  } else if (stringstartswith(arg, "currspc:") == 0) {
321
    if (value[1] == 0) {
322
      if ((value[0] >= '0') && (value[0] <= '1')) {
591 mateuszvis 323
        c->CTYINFO.currspace = value[0] - '0';
585 mateuszvis 324
        return(0);
325
      }
326
    }
327
  } else if (stringstartswith(arg, "currprec:") == 0) {
328
    if (value[1] == 0) {
329
      if ((value[0] >= '0') && (value[0] <= '9')) {
591 mateuszvis 330
        c->CTYINFO.currprec = value[0] - '0';
585 mateuszvis 331
        return(0);
332
      }
333
    }
334
  } else if (stringstartswith(arg, "yesno:") == 0) {
335
    /* string must be exactly 2 characters long */
336
    if ((value[0] != 0) && (value[1] != 0) && (value[2] == 0)) {
591 mateuszvis 337
      c->YESNO.yes[0] = value[0];
338
      c->YESNO.no[0] = value[1];
585 mateuszvis 339
      return(0);
340
    }
341
  }
342
  /* if I'm here, something went wrong */
343
  return(-4);
344
}
345
 
346
 
588 mateuszvis 347
/* converts a path to its canonic representation, returns 0 on success
348
 * or DOS err on failure (invalid drive) */
349
static unsigned short file_truename(const char *dst, char *src) {
350
  unsigned short res = 0;
351
  _asm {
352
    push es
353
    mov ah, 0x60  /* query truename, DS:SI=src, ES:DI=dst */
354
    push ds
355
    pop es
356
    mov si, src
357
    mov di, dst
358
    int 0x21
359
    jnc DONE
360
    mov [res], ax
361
    DONE:
362
    pop es
363
  }
364
  return(res);
365
}
366
 
367
 
593 mateuszvis 368
static void default_country_path(char *s) {
369
  char *dosdir = getenv("DOSDIR");
370
  size_t dosdirlen;
371
  s[0] = 0;
372
  if (dosdir == NULL) return;
373
  dosdirlen = strlen(dosdir);
374
  if (dosdirlen == 0) return;
375
  /* drop trailing backslash if present */
376
  if (dosdir[dosdirlen - 1] == '\\') dosdirlen--;
377
  /* copy dosdir to s and append the rest of the path */
378
  memcpy(s, dosdir, dosdirlen);
379
  strcpy(s + dosdirlen, "\\CFG\\COUNTRY.SYS");
380
}
381
 
382
 
585 mateuszvis 383
int main(int argc, char **argv) {
384
  struct country cntdata;
385
  int changedflag;
386
  int x;
610 mateuszvis 387
  static char fname[130];
388
  static char buff[64];
585 mateuszvis 389
 
603 mateuszvis 390
  svarlang_autoload("localcfg");
391
 
593 mateuszvis 392
  /* scan argv looking for the path to country.sys */
393
  for (x = 1; x < argc; x++) {
394
    if (argv[x][0] != '/') {
395
      if (fname[0] != 0) {
603 mateuszvis 396
        nls_puts(NLS_ERR_FILEPATHTWICE);
593 mateuszvis 397
        return(1);
398
      }
399
      /* */
400
      if (file_truename(fname, argv[x]) != 0) {
603 mateuszvis 401
        nls_puts(NLS_ERR_BADPATH);
593 mateuszvis 402
        return(1);
403
      }
404
    } else if (strcmp(argv[x], "/?") == 0) { /* is it /? */
405
      about();
406
      return(1);
407
    }
585 mateuszvis 408
  }
409
 
593 mateuszvis 410
  /* if no file path provided, look into %DOSDIR%\CFG\COUNTRY.SYS */
411
  if (fname[0] == 0) default_country_path(fname);
585 mateuszvis 412
 
413
  x = country_read(&cntdata, fname);
414
  if (x != 0) {
603 mateuszvis 415
    nls_puts(NLS_ERR_READFAIL);
585 mateuszvis 416
    return(2);
417
  }
418
 
593 mateuszvis 419
  changedflag = 0;
585 mateuszvis 420
 
421
  /* process command line arguments */
593 mateuszvis 422
  for (x = 1; x < argc; x++) {
423
    if (argv[x][0] != '/') continue; /* skip country.sys filename (processed earlier) */
424
    changedflag++;
425
    if (processarg(argv[x], &cntdata) != 0) {
603 mateuszvis 426
      nls_puts(NLS_ERR_INVPARAM);
585 mateuszvis 427
      return(3);
428
    }
429
  }
430
 
603 mateuszvis 431
  nls_put(NLS_INFO_COUNTRY);
606 mateuszvis 432
  sprintf(buff, " %03d", cntdata.CTYINFO.id);
433
  outputnl(buff);
603 mateuszvis 434
  nls_put(NLS_INFO_CODEPAGE);
606 mateuszvis 435
  sprintf(buff, " %d", cntdata.CTYINFO.codepage);
436
  outputnl(buff);
603 mateuszvis 437
  nls_put(NLS_INFO_DECSEP);
606 mateuszvis 438
  sprintf(buff, " %c", cntdata.CTYINFO.decimal[0]);
439
  outputnl(buff);
603 mateuszvis 440
  nls_put(NLS_INFO_THOUSEP);
606 mateuszvis 441
  sprintf(buff, " %c", cntdata.CTYINFO.thousands[0]);
442
  outputnl(buff);
603 mateuszvis 443
  nls_put(NLS_INFO_DATEFMT);
610 mateuszvis 444
  output(" ");
445
  outputnl(datestring(buff, &cntdata));
603 mateuszvis 446
  nls_put(NLS_INFO_TIMEFMT);
610 mateuszvis 447
  output(" ");
448
  outputnl(timestring(buff, &cntdata));
603 mateuszvis 449
  nls_put(NLS_INFO_YESNO);
606 mateuszvis 450
  sprintf(buff, " %c/%c", cntdata.YESNO.yes[0], cntdata.YESNO.no[0]);
451
  outputnl(buff);
603 mateuszvis 452
  nls_put(NLS_INFO_CURREXAMPLE);
610 mateuszvis 453
  output(" ");
454
  outputnl(currencystring(buff, &cntdata));
585 mateuszvis 455
 
603 mateuszvis 456
  crlf();
457
  nls_puts(NLS_MAKESURE);
610 mateuszvis 458
  sprintf(buff, "COUNTRY=%03d,%03d,", cntdata.CTYINFO.id, cntdata.CTYINFO.codepage);
459
  output(buff);
460
  outputnl(fname);
585 mateuszvis 461
 
462
  /* if anything changed, write the new file */
463
  if (changedflag != 0) country_write(fname, &cntdata);
464
 
465
  return(0);
466
}