Subversion Repositories SvarDOS

Rev

Rev 603 | Rev 610 | 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
 
154
static char *datestring(struct country *c) {
155
  static char result[16];
591 mateuszvis 156
  switch (c->CTYINFO.datefmt) {
585 mateuszvis 157
    case COUNTRY_DATE_MDY:
591 mateuszvis 158
      sprintf(result, "12%c31%c1990", c->CTYINFO.datesep[0], c->CTYINFO.datesep[0]);
585 mateuszvis 159
      break;
160
    case COUNTRY_DATE_DMY:
591 mateuszvis 161
      sprintf(result, "31%c12%c1990", c->CTYINFO.datesep[0], c->CTYINFO.datesep[0]);
585 mateuszvis 162
      break;
163
    case COUNTRY_DATE_YMD:
164
    default:
591 mateuszvis 165
      sprintf(result, "1990%c12%c31", c->CTYINFO.datesep[0], c->CTYINFO.datesep[0]);
585 mateuszvis 166
      break;
167
  }
168
  return(result);
169
}
170
 
171
 
172
static char *timestring(struct country *c) {
173
  static char result[16];
591 mateuszvis 174
  if (c->CTYINFO.timefmt == COUNTRY_TIME12) {
175
    sprintf(result, "11%c59%c59 PM", c->CTYINFO.timesep[0], c->CTYINFO.timesep[0]);
585 mateuszvis 176
  } else {
591 mateuszvis 177
    sprintf(result, "23%c59%c59", c->CTYINFO.timesep[0], c->CTYINFO.timesep[0]);
585 mateuszvis 178
  }
179
  return(result);
180
}
181
 
182
 
183
static char *currencystring(struct country *c) {
184
  static char result[16];
185
  char decimalpart[16];
186
  char space[2] = {0, 0};
187
  char decsym[8];
188
  char cursym[8];
189
  decimalpart[0] = '1';
190
  decimalpart[1] = '2';
191
  decimalpart[2] = '3';
192
  decimalpart[3] = '4';
193
  decimalpart[4] = '5';
194
  decimalpart[5] = '6';
195
  decimalpart[6] = '7';
196
  decimalpart[7] = '8';
197
  decimalpart[8] = '9';
198
  decimalpart[9] = 0;
199
  /* prepare the decimal string first */
591 mateuszvis 200
  if (c->CTYINFO.currprec < 9) {
201
    decimalpart[c->CTYINFO.currprec] = 0;
585 mateuszvis 202
  }
203
  /* prepare the currency space string */
591 mateuszvis 204
  if (c->CTYINFO.currspace != 0) {
585 mateuszvis 205
    space[0] = ' ';
206
  }
207
  /* prepare the currency and decimal symbols */
591 mateuszvis 208
  if (c->CTYINFO.currdecsym != 0) { /* currency replaces the decimal point */
209
    sprintf(decsym, "%s", c->CTYINFO.currsym);
585 mateuszvis 210
    cursym[0] = 0;
211
  } else {
591 mateuszvis 212
    sprintf(decsym, "%c", c->CTYINFO.decimal[0]);
213
    sprintf(cursym, "%s", c->CTYINFO.currsym);
585 mateuszvis 214
  }
591 mateuszvis 215
  if (c->CTYINFO.currprec == 0) decsym[0] = 0;
585 mateuszvis 216
  /* compute the final string */
591 mateuszvis 217
  if (c->CTYINFO.currpos == 0) { /* currency precedes value */
585 mateuszvis 218
    sprintf(result, "%s%s99%s%s", cursym, space, decsym, decimalpart);
219
  } else { /* currency follows value or replaces decimal symbol */
220
    sprintf(result, "99%s%s%s%s", decsym, decimalpart, space, cursym);
221
  }
222
  return(result);
223
}
224
 
225
 
226
/* checks if str starts with prefix. returns 0 if so, non-zero otherwise. */
227
static int stringstartswith(char *str, char *prefix) {
228
  for (;;) {
229
    /* end of prefix means success */
230
    if (*prefix == 0) return(0);
231
    /* otherwise there is no match */
232
    if (*str != *prefix) return(-1);
233
    /* if match good so far, look at next char */
234
    str += 1;
235
    prefix += 1;
236
  }
237
}
238
 
239
 
240
/* processes an argument. returns 0 on success, non-zero otherwise. */
241
static int processarg(char *arg, struct country *c) {
242
  char *value;
243
  int intvalue;
244
  /* an option must start with a '/' */
245
  if (arg[0] != '/') return(-1);
246
  arg += 1; /* skip the slash */
247
  /* find where the value starts */
248
  value = strchr(arg, ':');
249
  /* if no value present, fail */
250
  if (value == NULL) return(-2);
251
  value += 1;
252
  if (*value == 0) return(-3);
253
  /* interpret the option now */
254
  if (stringstartswith(arg, "country:") == 0) {
255
    intvalue = atoi(value);
256
    if ((intvalue > 0) && (intvalue < 1000)) {
591 mateuszvis 257
      c->CTYINFO.id = intvalue;
585 mateuszvis 258
      return(0);
259
    }
260
  } else if (stringstartswith(arg, "cp:") == 0) {
261
    intvalue = atoi(value);
262
    if ((intvalue > 0) && (intvalue < 1000)) {
591 mateuszvis 263
      c->CTYINFO.codepage = intvalue;
585 mateuszvis 264
      return(0);
265
    }
266
  } else if (stringstartswith(arg, "decim:") == 0) {
267
    if (value[1] == 0) { /* value must be exactly one character */
591 mateuszvis 268
      c->CTYINFO.decimal[0] = *value;
585 mateuszvis 269
      return(0);
270
    }
271
  } else if (stringstartswith(arg, "thous:") == 0) {
272
    if (value[1] == 0) { /* value must be exactly one character */
591 mateuszvis 273
      c->CTYINFO.thousands[0] = *value;
585 mateuszvis 274
      return(0);
275
    }
276
  } else if (stringstartswith(arg, "datesep:") == 0) {
277
    if (value[1] == 0) { /* value must be exactly one character */
591 mateuszvis 278
      c->CTYINFO.datesep[0] = *value;
585 mateuszvis 279
      return(0);
280
    }
281
  } else if (stringstartswith(arg, "timesep:") == 0) {
282
    if (value[1] == 0) { /* value must be exactly one character */
591 mateuszvis 283
      c->CTYINFO.timesep[0] = *value;
585 mateuszvis 284
      return(0);
285
    }
286
  } else if (stringstartswith(arg, "datefmt:") == 0) {
287
    if (strcmp(value, "MDY") == 0) {
591 mateuszvis 288
      c->CTYINFO.datefmt = COUNTRY_DATE_MDY;
585 mateuszvis 289
      return(0);
290
    } else if (strcmp(value, "DMY") == 0) {
591 mateuszvis 291
      c->CTYINFO.datefmt = COUNTRY_DATE_DMY;
585 mateuszvis 292
      return(0);
293
    } else if (strcmp(value, "YMD") == 0) {
591 mateuszvis 294
      c->CTYINFO.datefmt = COUNTRY_DATE_YMD;
585 mateuszvis 295
      return(0);
296
    }
297
  } else if (stringstartswith(arg, "timefmt:") == 0) {
298
    if (value[1] == 0) {
299
      if ((value[0] >= '0') && (value[0] <= '1')) {
591 mateuszvis 300
        c->CTYINFO.timefmt = value[0] - '0';
585 mateuszvis 301
        return(0);
302
      }
303
    }
304
  } else if (stringstartswith(arg, "curr:") == 0) {
305
    if (strlen(value) <= 4) {
591 mateuszvis 306
      strcpy(c->CTYINFO.currsym, value);
585 mateuszvis 307
      return(0);
308
    }
309
  } else if (stringstartswith(arg, "currpos:") == 0) {
310
    if (value[1] == 0) {
311
      if (value[0] == '0') {
591 mateuszvis 312
        c->CTYINFO.currpos = 0;
585 mateuszvis 313
        return(0);
314
      } else if (value[0] == '1') {
591 mateuszvis 315
        c->CTYINFO.currpos = 1;
585 mateuszvis 316
        return(0);
317
      } else if (value[0] == '2') {
591 mateuszvis 318
        c->CTYINFO.currpos = 0;
319
        c->CTYINFO.currdecsym = 1;
585 mateuszvis 320
        return(0);
321
      }
322
    }
323
  } else if (stringstartswith(arg, "currspc:") == 0) {
324
    if (value[1] == 0) {
325
      if ((value[0] >= '0') && (value[0] <= '1')) {
591 mateuszvis 326
        c->CTYINFO.currspace = value[0] - '0';
585 mateuszvis 327
        return(0);
328
      }
329
    }
330
  } else if (stringstartswith(arg, "currprec:") == 0) {
331
    if (value[1] == 0) {
332
      if ((value[0] >= '0') && (value[0] <= '9')) {
591 mateuszvis 333
        c->CTYINFO.currprec = value[0] - '0';
585 mateuszvis 334
        return(0);
335
      }
336
    }
337
  } else if (stringstartswith(arg, "yesno:") == 0) {
338
    /* string must be exactly 2 characters long */
339
    if ((value[0] != 0) && (value[1] != 0) && (value[2] == 0)) {
591 mateuszvis 340
      c->YESNO.yes[0] = value[0];
341
      c->YESNO.no[0] = value[1];
585 mateuszvis 342
      return(0);
343
    }
344
  }
345
  /* if I'm here, something went wrong */
346
  return(-4);
347
}
348
 
349
 
588 mateuszvis 350
/* converts a path to its canonic representation, returns 0 on success
351
 * or DOS err on failure (invalid drive) */
352
static unsigned short file_truename(const char *dst, char *src) {
353
  unsigned short res = 0;
354
  _asm {
355
    push es
356
    mov ah, 0x60  /* query truename, DS:SI=src, ES:DI=dst */
357
    push ds
358
    pop es
359
    mov si, src
360
    mov di, dst
361
    int 0x21
362
    jnc DONE
363
    mov [res], ax
364
    DONE:
365
    pop es
366
  }
367
  return(res);
368
}
369
 
370
 
593 mateuszvis 371
static void default_country_path(char *s) {
372
  char *dosdir = getenv("DOSDIR");
373
  size_t dosdirlen;
374
  s[0] = 0;
375
  if (dosdir == NULL) return;
376
  dosdirlen = strlen(dosdir);
377
  if (dosdirlen == 0) return;
378
  /* drop trailing backslash if present */
379
  if (dosdir[dosdirlen - 1] == '\\') dosdirlen--;
380
  /* copy dosdir to s and append the rest of the path */
381
  memcpy(s, dosdir, dosdirlen);
382
  strcpy(s + dosdirlen, "\\CFG\\COUNTRY.SYS");
383
}
384
 
385
 
585 mateuszvis 386
int main(int argc, char **argv) {
387
  struct country cntdata;
388
  int changedflag;
389
  int x;
606 mateuszvis 390
  char fname[130];
391
  char buff[128];
585 mateuszvis 392
 
603 mateuszvis 393
  svarlang_autoload("localcfg");
394
 
593 mateuszvis 395
  /* scan argv looking for the path to country.sys */
396
  for (x = 1; x < argc; x++) {
397
    if (argv[x][0] != '/') {
398
      if (fname[0] != 0) {
603 mateuszvis 399
        nls_puts(NLS_ERR_FILEPATHTWICE);
593 mateuszvis 400
        return(1);
401
      }
402
      /* */
403
      if (file_truename(fname, argv[x]) != 0) {
603 mateuszvis 404
        nls_puts(NLS_ERR_BADPATH);
593 mateuszvis 405
        return(1);
406
      }
407
    } else if (strcmp(argv[x], "/?") == 0) { /* is it /? */
408
      about();
409
      return(1);
410
    }
585 mateuszvis 411
  }
412
 
593 mateuszvis 413
  /* if no file path provided, look into %DOSDIR%\CFG\COUNTRY.SYS */
414
  if (fname[0] == 0) default_country_path(fname);
585 mateuszvis 415
 
416
  x = country_read(&cntdata, fname);
417
  if (x != 0) {
603 mateuszvis 418
    nls_puts(NLS_ERR_READFAIL);
585 mateuszvis 419
    return(2);
420
  }
421
 
593 mateuszvis 422
  changedflag = 0;
585 mateuszvis 423
 
424
  /* process command line arguments */
593 mateuszvis 425
  for (x = 1; x < argc; x++) {
426
    if (argv[x][0] != '/') continue; /* skip country.sys filename (processed earlier) */
427
    changedflag++;
428
    if (processarg(argv[x], &cntdata) != 0) {
603 mateuszvis 429
      nls_puts(NLS_ERR_INVPARAM);
585 mateuszvis 430
      return(3);
431
    }
432
  }
433
 
603 mateuszvis 434
  nls_put(NLS_INFO_COUNTRY);
606 mateuszvis 435
  sprintf(buff, " %03d", cntdata.CTYINFO.id);
436
  outputnl(buff);
603 mateuszvis 437
  nls_put(NLS_INFO_CODEPAGE);
606 mateuszvis 438
  sprintf(buff, " %d", cntdata.CTYINFO.codepage);
439
  outputnl(buff);
603 mateuszvis 440
  nls_put(NLS_INFO_DECSEP);
606 mateuszvis 441
  sprintf(buff, " %c", cntdata.CTYINFO.decimal[0]);
442
  outputnl(buff);
603 mateuszvis 443
  nls_put(NLS_INFO_THOUSEP);
606 mateuszvis 444
  sprintf(buff, " %c", cntdata.CTYINFO.thousands[0]);
445
  outputnl(buff);
603 mateuszvis 446
  nls_put(NLS_INFO_DATEFMT);
606 mateuszvis 447
  sprintf(buff, " %s", datestring(&cntdata));
448
  outputnl(buff);
603 mateuszvis 449
  nls_put(NLS_INFO_TIMEFMT);
606 mateuszvis 450
  sprintf(buff, " %s", timestring(&cntdata));
451
  outputnl(buff);
603 mateuszvis 452
  nls_put(NLS_INFO_YESNO);
606 mateuszvis 453
  sprintf(buff, " %c/%c", cntdata.YESNO.yes[0], cntdata.YESNO.no[0]);
454
  outputnl(buff);
603 mateuszvis 455
  nls_put(NLS_INFO_CURREXAMPLE);
606 mateuszvis 456
  sprintf(" %s", currencystring(&cntdata));
457
  outputnl(buff);
585 mateuszvis 458
 
603 mateuszvis 459
  crlf();
460
  nls_puts(NLS_MAKESURE);
606 mateuszvis 461
  sprintf(buff, "COUNTRY=%03d,%03d,%s", cntdata.CTYINFO.id, cntdata.CTYINFO.codepage, fname);
462
  outputnl(buff);
585 mateuszvis 463
 
464
  /* if anything changed, write the new file */
465
  if (changedflag != 0) country_write(fname, &cntdata);
466
 
467
  return(0);
468
}