aboutsummaryrefslogtreecommitdiff
path: root/kitten.c
blob: f63cd96ace719ff5ba0654e3fc6f491fe7a47f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/* Functions that emulate UNIX catgets */

/* Copyright (C) 1999,2000,2001 Jim Hall <jhall@freedos.org> */
/* Kitten version 2003 by Tom Ehlert, heavily modified by Eric Auer 2003 */

/*
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef NO_KITTEN

#include <stdio.h>		/* sprintf */
#ifndef _MICROC_
#include <stdlib.h>		/* getenv  */
#include <string.h>		/* strchr */
#include <dos.h>
#ifndef __PACIFIC__
#include <fcntl.h>
#else
#define O_RDONLY 0
#define O_TEXT 0
#endif
#else
#include <intr.h>
#include <file.h>
#define O_RDONLY READONLY
#define O_TEXT 0
#endif
/* assert we are running in small model */
/* else pointer below has to be done correctly */
/* char verify_small_pointers[sizeof(void*) == 2 ? 1 : -1]; */

#include "kitten.h"

char catcontents[8192];

struct catstring
{
  char key1;
  char key2;
  char *text;
};

/* Micro-C does not support typedef */
#define catstring_t struct catstring

catstring_t catpoints[128];


/* Local prototypes */

int catread (char *catfile);	/* Reads a catfile into the hash */
char *processEscChars (char *line);	/* Converts c escape sequences to chars */

int get_char (int file);	/* not meant for external use    */
/* external use would cause consistency problems if */
/* value or related file of the file handle changes */

int mystrtoul (char *src, int base, int size);


/* Globals */

nl_catd _kitten_catalog = 0;	/* _kitten_catalog descriptor or 0 */
char catfile[128];		/* full path to _kitten_catalog */

char getlbuf[8192];		/* read buffer for better speed  */
char *getlp;			/* current point in buffer       */
int getlrem = -1;		/* remaining bytes in buffer     */
char lastcr = 0;		/* for 2byte CR LF sequences     */


#ifndef _MICROC_
#ifndef __DJGPP__

/* DOS handle based file usage */

int
dos_open (char *filename)
{
  union REGS r;
  struct SREGS s;

  r.h.ah = 0x3d;
  r.h.al = 0;			/* read mode only supoported now !! */
  r.x.dx = FP_OFF (filename);
  s.ds = FP_SEG (filename);
  intdosx (&r, &r, &s);
  return ((r.x.cflag) ? -1 : (int) r.x.ax);
}


int
dos_read (int file, void *ptr, unsigned count)
{
  union REGS r;
  struct SREGS s;
  r.h.ah = 0x3f;
  r.x.bx = file;
  r.x.cx = count;
  r.x.dx = FP_OFF (ptr);
  s.ds = FP_SEG (ptr);
  intdosx (&r, &r, &s);
  return ((r.x.cflag) ? 0 : r.x.ax);
}


int
dos_write (int file, void *ptr, unsigned count)
{
  union REGS r;
  struct SREGS s;
  r.h.ah = 0x40;
  r.x.bx = file;
  r.x.cx = count;
  r.x.dx = FP_OFF (ptr);
  s.ds = FP_SEG (ptr);
  intdosx (&r, &r, &s);
  return ((r.x.cflag) ? 0 : r.x.ax);
}


void
dos_close (int file)
{
  union REGS r;
  r.h.ah = 0x3e;
  r.x.bx = file;
  intdos (&r, &r);
}

#endif /*DJGPP*/
#endif /*Micro-C */
/* Functions */
/**
 * On success, catgets() returns a pointer to an internal
 * buffer area containing the null-terminated message string.
 * On failure, catgets() returns the value 'message'.
 */
char *
kittengets (int setnum, int msgnum, char *message)
{
/* In Micro-C, variables must be defined at the start of the
 * function and may not be immediately assigned a value
 */
#ifdef _MICROC_
  int i;
  i = 0;
#else
  int i = 0;
#endif

  while ((catpoints[i].key1 != setnum) || (catpoints[i].key2 != msgnum))
    {
      if ((catpoints[i].text == NULL) || (i > 127))	/* at EOF */
	return message;
      i++;
    }

  if (catpoints[i].text == NULL)
    return message;
  else
    return (catpoints[i].text);
}


/**
 * Initialize kitten for program (name).
 */

nl_catd
kittenopen (char *name)
{
  /* catopen() returns a message _kitten_catalog descriptor  *
   * of type nl_catd on success.  On failure, it returns -1. */

  char catlang[3];		/* from LANG environment var. */
  char *nlsptr;			/* ptr to NLSPATH */
  char *lang;			/* ptr to LANG */
  int i;
#ifdef _MICROC_
  char *tok;
  int toklen;
#endif

  /* Open the _kitten_catalog file */
  /* The value of `_kitten_catalog' will be set based on catread */

  if (_kitten_catalog)
    {				/* Already one open */
      write (1, "cat already open\r\n", strlen ("cat already open\r\n"));
      return (-1);
    }

  for (i = 0; i < 128; i++)
    catpoints[i].text = NULL;

  if (strchr (name, '\\'))
    {
      /* unusual case: 'name' is a filename */
      write (1, "found \\\r\n", 9);
      _kitten_catalog = catread (name);
      if (_kitten_catalog)
	return (_kitten_catalog);
    }

  /* If the message _kitten_catalog file name does not contain a directory *
   * separator, then we need to try to locate the message _kitten_catalog. */

  /* We will need the value of LANG, and may need a 2-letter abbrev of
     LANG later on, so get it now. */

  lang = getenv ("LANG");

  if (lang == NULL)
    {
      /* printf("no lang= found\n"); *//* not fatal, though */
      /* Return failure - we won't be able to locate the cat file */
      return (-1);
    }

  if ((strlen (lang) < 2) || ((strlen (lang) > 2) && (lang[2] != '-')))
    {
      /* Return failure - we won't be able to locate the cat file */
      return (-1);
    }

  memcpy (catlang, lang, 2);
  /* we copy the full LANG value or the part before "-" if "-" found */
  catlang[2] = '\0';

  /* step through NLSPATH */

  nlsptr = getenv ("NLSPATH");

  if (nlsptr == NULL)
    {
      /* printf("no NLSPATH= found\n"); *//* not fatal either */
      /* Return failure - we won't be able to locate the cat file */
      return (-1);
    }

  catfile[0] = '\0';

  while (nlsptr && nlsptr[0])
    {
#ifdef _MICROC_
      tok = strchr (nlsptr, ';');
#else
      char *tok = strchr (nlsptr, ';');
      int toklen;
#endif

      if (tok == NULL)
	toklen = strlen (nlsptr);	/* last segment */
      else
	toklen = tok - nlsptr;	/* segment terminated by ';' */

      /* catfile = malloc(toklen+1+strlen(name)+1+strlen(lang)+1); */
      /* Try to find the _kitten_catalog file in each path from NLSPATH */

      if ((toklen + 6 + strlen (name)) > sizeof (catfile))
	{
	  write (1, "NLSPATH overflow\r\n", strlen ("NLSPATH overflow\r\n"));
	  return 0;		/* overflow in NLSPATH, should never happen */
	}

      /* Rule #1: %NLSPATH%\%LANG%\cat */

      memcpy (catfile, nlsptr, toklen);
      strcpy (catfile + toklen, "\\");
      strcat (catfile, catlang);
      strcat (catfile, "\\");
      strcat (catfile, name);
      _kitten_catalog = catread (catfile);
      if (_kitten_catalog)
	return (_kitten_catalog);

      /* Rule #2: %NLSPATH%\cat.%LANG% */

      /* memcpy(catfile, nlsptr, toklen); */
      strcpy (catfile + toklen, "\\");
      strcat (catfile, name);
      strcat (catfile, ".");
      strcat (catfile, catlang);
      _kitten_catalog = catread (catfile);
      if (_kitten_catalog)
	return (_kitten_catalog);

      /* Grab next tok for the next while iteration */

      nlsptr = tok;
      if (nlsptr)
	nlsptr++;

    }				/* while tok */

  /* We could not find it.  Return failure. */

  return (-1);
}


/**
 * Load a message catalog into memory.
 */

int
catread (char *catfile)
{
  int file;			/* pointer to the catfile */
  int i;
  char *where;
  char *tok;
#ifdef _MICROC_
  char *msg;
  char *key;
  int key1;
  int key2;
#endif

  /* Get the whole catfile into a buffer and parse it */

  file = open (catfile, O_RDONLY | O_TEXT);
  if (file < 0)
    /* Cannot open the file.  Return failure */
    return 0;

  for (i = 0; i < 128; i++)
    catpoints[i].text = NULL;

  for (i = 0; (unsigned int) i < sizeof (catcontents); i++)
    catcontents[i] = '\0';

  /* Read the file into memory */
  i = read (file, catcontents, sizeof (catcontents) - 1);

  if ((i == sizeof (catcontents) - 1) || (i < 1))
    return 0;			/* file was too big or too small */

  where = catcontents;
  i = 0;			/* catpoints entry */

  do
    {
#ifndef _MICROC_
      char *msg;
      char *key;
      int key1 = 0;
      int key2 = 0;
#else
      key1 = 0;
      key2 = 0;
#endif

      tok = strchr (where, '\n');

      if (tok == NULL)
	{			/* done? */
	  close (file);
	  return 1;		/* success */
	}

      tok[0] = '\0';		/* terminate here */
      tok--;			/* guess: \r before \n */
      if (tok[0] != '\r')
	tok++;			/* if not, go back */
      else
	{
	  tok[0] = '\0';	/* terminate here already */
	  tok++;
	}
      tok++;			/* this is where the next line starts */

      if ((where[0] >= '0') && (where[0] <= '9') &&
	  ((msg = strchr (where, ':')) != NULL))
	{
	  /* Skip everything which starts with # or with no digit */
	  /* Entries look like "1.2:This is a message" */

	  msg[0] = '\0';	/* remove : */
	  msg++;		/* go past the : */

	  if ((key = strchr (where, '.')) != NULL)
	    {
	      key[0] = '\0';	/* turn . into terminator */
	      key++;		/* go past the . */
	      key1 = mystrtoul (where, 10, strlen (where));
	      key2 = mystrtoul (key, 10, strlen (key));

	      if ((key1 >= 0) && (key2 >= 0))
		{
		  catpoints[i].key1 = key1;
		  catpoints[i].key2 = key2;
		  catpoints[i].text = processEscChars (msg);
		  if (catpoints[i].text == NULL)	/* ESC parse error */
		    catpoints[i].text = msg;
		  i++;		/* next entry! */
		}		/* valid keys */

	    }			/* . found */

	}			/* : and digit found */

      where = tok;		/* go to next line */

    }
  while (1);
#ifdef __PACIFIC__
  return 0;
#endif
}


void
kittenclose (void)
{
  /* close a message _kitten_catalog */
  _kitten_catalog = 0;
}


/**
 * Parse a string that represents an unsigned integer.
 * Returns -1 if an error is found. The first size
 * chars of the string are parsed.
 */

int
mystrtoul (char *src, int base, int size)
{
#ifdef _MICROC_
  int ret;
  int digit;
  int ch;
  ret = 0;
#else
  int ret = 0;
#endif

  for (; size > 0; size--)
    {
#ifdef _MICROC_
      ch = *src;
#else
      int digit;
      int ch = *src;
#endif
      src++;

      if (ch >= '0' && ch <= '9')
	digit = ch - '0';
      else if (ch >= 'A' && ch <= 'Z')
	digit = ch - 'A' + 10;
      else if (ch >= 'a' && ch <= 'z')
	digit = ch - 'a' + 10;
      else
	return -1;

      if (digit >= base)
	return -1;

      ret = ret * base + digit;
    }				/* for */

  return ret;
}


/**
 * Process strings, converting \n, \t, \v, \b, \r, \f, \\,
 * \ddd, \xdd and \x0dd to actual chars.
 * (Note: \x is an extension to support hexadecimal)
 * This is used to allow the messages to use c escape sequences.
 * Modifies the line in-place (always same size or shorter).
 * Returns a pointer to input string.
 */

char *
processEscChars (char *line)
{
  /* used when converting \xdd and \ddd (hex or octal) characters */
  char ch;
#ifdef _MICROC_
  char *src;
  char *dst;
  int chx;
  src = line;
  dst = line;
#else
  char *src = line;
  char *dst = line;		/* possible as dst is shorter than src */
#endif

  if (line == NULL)
    return line;

  /* cycle through copying characters, except when a \ is encountered. */
  while (*src != '\0')
    {
      ch = *src;
      src++;

      if (ch == '\\')
	{
	  ch = *src;		/* what follows slash? */
	  src++;

	  switch (ch)
	    {
	    case '\\':		/* a single slash */
	      *dst = '\\';
	      dst++;
	      break;
	    case 'n':		/* a newline (linefeed) */
	      *dst = '\n';
	      dst++;
	      break;
	    case 'r':		/* a carriage return */
	      *dst = '\r';
	      dst++;
	      break;
	    case 't':		/* a horizontal tab */
	      *dst = '\t';
	      dst++;
	      break;
	    case 'v':		/* a vertical tab */
	      *dst = '\v';
	      dst++;
	      break;
	    case 'b':		/* a backspace */
	      *dst = '\b';
	      dst++;
	      break;
	    case 'a':		/* alert */
	      *dst = '\a';
	      dst++;
	      break;
	    case 'f':		/* formfeed */
	      *dst = '\f';
	      dst++;
	      break;
	    case 'x':		/* extension supporting hex numbers \xdd or \x0dd */
	      {
#ifdef _MICROC_
		chx = mystrtoul (src, 16, 2);	/* get value */
#else
		int chx = mystrtoul (src, 16, 2);	/* get value */
#endif
		if (chx >= 0)
		  {		/* store character */
		    *dst = chx;
		    dst++;
		    src += 2;
		  }
		else		/* error so just store x (loose slash) */
		  {
		    *dst = *src;
		    dst++;
		  }
	      }
	      break;
	    default:		/* just store letter (loose slash) or handle octal */
	      {
#ifdef _MICROC_
		chx = mystrtoul (src, 8, 3);	/* get value */
#else
		int chx = mystrtoul (src, 8, 3);	/* get value */
#endif
		if (chx >= 0)
		  {		/* store character */
		    *dst = chx;
		    dst++;
		    src += 3;
		  }
		else
		  {
		    *dst = *src;
		    dst++;
		  }
	      }
	      break;
	    }			/* switch */
	}			/* if backslash */
      else
	{
	  *dst = ch;
	  dst++;
	}
    }				/* while */

  /* ensure '\0' terminated */
  *dst = '\0';

  return line;
}


int
get_char (int file)
{
#ifdef _MICROC_
  int rval;
  rval = -1;
#else
  int rval = -1;
#endif

  if (getlrem <= 0)
    {				/* (re)init buffer */
      getlrem = read (file, getlbuf, sizeof (getlbuf));
      if (getlrem <= 0)
	return -1;		/* fail: read error / EOF */
      getlp = getlbuf;		/* init pointer */
    }

  if (getlrem > 0)
    {				/* consume byte from buffer */
      rval = getlp[0];
      getlp++;
      getlrem--;
    }

  return rval;
}


/**
 * Read a line of text from file. You must call this with
 * a null buffer or null size to flush buffers when you are
 * done with a file before using it on the next file. Cannot
 * be used for 2 files at the same time.
 */

int
get_line (int file, char *str, int size)
{
  int ch;
#ifdef _MICROC_
  int success;
  success = 0;
#else
  int success = 0;
#endif

  if ((size == 0) || (str == NULL))
    {				/* re-init get_line buffers */
      getlp = getlbuf;
      getlrem = -1;
      lastcr = 0;
      return 0;
    }

  str[0] = '\0';

  while ((size > 0) && (success == 0))
    {
      ch = get_char (file);
      if (ch < 0)
	break;			/* (can cause fail if no \n found yet) */

      if (ch == '\r')
	ch = get_char (file);	/* ignore \r */

      str[0] = ch;

      if ((ch == '\n') || (ch == '\r'))
	{			/* done? */
	  str[0] = '\0';
	  return 1;		/* success */
	}

      str++;
      size--;

    }				/* while */

  str[0] = '\0';		/* terminate buffer */

  return success;

}

#endif /*NO_KITTEN */