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
  
     | 
    
      /* 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
*/
#include <stdio.h>			/* sprintf */
#include <stdlib.h>			/* getenv  */
#include <string.h>			/* strchr */
#include <fcntl.h>
#include <dos.h>
/* 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[6144];	/* *** Maximum size of PROGRAM.%LANG% file *** */
struct catstring{
  char key1;
  char key2;
  char *text;
};
typedef struct catstring catstring_t;
catstring_t catpoints[128];		/* max 128 strings can be defined... */
/* 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); /* not meant for external use    */
/* external use would cause consistency problems if */
/* value or related file of the file handle changes */
int mystrtoul(char *, int, int);
/* Globals */
nl_catd _kitten_catalog = 0;	/* _kitten_catalog descriptor or 0 */
char catfile[128];		/* full path to _kitten_catalog */
char getlbuf[1024]; /* read buffer (better speed) -- 7/2004: 1k, not 8k */
char * getlp;       /* current point in buffer       */
int getlrem = -1;   /* remaining bytes in buffer     */
char lastcr = 0;    /* for 2byte CR LF sequences     */
/* DOS handle based file usage */
int dos_open(char *filename, int mode)
{
	union REGS r;
        struct SREGS s;
	if (mode);		/* mode ignored - readonly supported */
	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 : 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);
}
/* 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)
{
  int i = 0;
  
  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;
  
  /* 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 != NULL) {
      char *tok = strchr(nlsptr, ';');
      int toklen;
      if (tok == NULL)
        toklen = strlen(nlsptr); /* last segment */
      else
        toklen = (int)(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;
  /* 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; 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 {
    char *msg;
    char *key;
    int key1 = 0;
    int key2 = 0;
    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);
}
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)
{
	int ret = 0;
	
	for (; size > 0; size--)  {
	  int digit;
	  int ch = *src;
          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)
{
  char *src = line;
  char *dst = line; /* possible as dst is shorter than src */
  /* used when converting \xdd and \ddd (hex or octal) characters */
  char ch;
  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 */
          {
            int chx = mystrtoul(src, 16, 2); /* get value */
            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 */
	  {
            int chx = mystrtoul(src, 8, 3); /* get value */
            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) {
  int rval = -1;
  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 success = 0;
  int ch;
  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;
}
 
     |