home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / pgp2 / src / c / language < prev    next >
Encoding:
Text File  |  1995-06-06  |  10.1 KB  |  454 lines

  1. /*
  2.    language.c - Foreign language translation for PGP
  3.    Finds foreign language "subtitles" for English phrases
  4.    in external foriegn language text file.
  5.  
  6.    (c) Copyright 1990-1994 by Philip Zimmermann.  All rights reserved.
  7.    The author assumes no liability for damages resulting from the use
  8.    of this software, even if the damage results from defects in this
  9.    software.  No warranty is expressed or implied.
  10.  
  11.    Note that while most PGP source modules bear Philip Zimmermann's
  12.    copyright notice, many of them have been revised or entirely written
  13.    by contributors who frequently failed to put their names in their
  14.    code.  Code that has been incorporated into PGP from other authors
  15.    was either originally published in the public domain or is used with
  16.    permission from the various authors.
  17.  
  18.    PGP is available for free to the public under certain restrictions.
  19.    See the PGP User's Guide (included in the release package) for
  20.    important information about licensing, patent restrictions on
  21.    certain algorithms, trademarks, copyrights, and export controls.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include "usuals.h"
  29. #include "fileio.h"
  30. #include "language.h"
  31. #include "pgp.h"
  32. #include "charset.h"
  33. #include "armor.h"
  34.  
  35. #ifdef RISC_OS
  36. /* Sigh. No real extensions, short filenames, wrong separator.
  37.  * Isn't RISC OS great? -- GJM
  38.  */
  39. #define SUBTITLES_FILE    "lang/txt"
  40. #define LANG_INDEXFILE    "lang/idx"
  41. #else
  42. #define SUBTITLES_FILE    "language.txt"
  43. #define LANG_INDEXFILE    "language.idx"
  44. #endif
  45.  
  46. #define    STRBUFSIZE        2048
  47.  
  48. char language[16] = "en";    /* The language code, defaults to English */
  49. static char *strbuf;
  50. static char lang[16];        /* readstr sets this to the language id of
  51.                    the msg it last read */
  52. static int subtitles_available = 0;
  53. static int line = 0;
  54. /*      subtitles_available is used to determine if we know whether the special
  55.    subtitles_file exists.  subtitles_available has the following values:
  56.    0  = first time thru, we don't yet know if subtitles_file exists.
  57.    1  = we have already determined that subtitles_file exists.
  58.    -1 = we have already determined that subtitles_file does not exist.
  59.  */
  60.  
  61. #define    NEWLINE        0
  62. #define    COMMENT        1
  63. #define    INSTRING    2
  64. #define    ESCAPE        3
  65. #define    IDENT        4
  66. #define    DONE        5
  67. #define    ERROR        6
  68. #define    ERR1        7
  69.  
  70. /* Look for and return a quoted string from the file.
  71.  * If nlabort is true, return failure if we find a blank line
  72.  * before we find the opening quote.
  73.  */
  74. static char *
  75.  readstr(FILE * f, char *buf, int nlabort)
  76. {
  77.     int c, d;
  78.     char *p = buf;
  79.     int state = NEWLINE;
  80.     int i = 0;
  81.  
  82.     while ((c = getc(f)) != EOF) {
  83.     if (c == '\r')
  84.         continue;
  85.     /* line numbers are only incremented when creating index file */
  86.     if (line && c == '\n')
  87.         ++line;
  88.     switch (state) {
  89.     case NEWLINE:
  90.         switch (c) {
  91.         case '#':
  92.         state = COMMENT;
  93.         break;
  94.         case '"':
  95.         state = INSTRING;
  96.         break;
  97.         case '\n':
  98.         if (nlabort) {
  99.             *buf = '\0';
  100.             return buf;
  101.         }
  102.         default:
  103.         if (i == 0 && isalnum(c)) {
  104.             state = IDENT;
  105.             lang[i++] = c;
  106.             break;
  107.         }
  108.         if (!isspace(c)) {
  109.             fprintf(stderr, "language.txt:%d: syntax error\n", line);
  110.             state = ERROR;
  111.         }
  112.         }
  113.         break;
  114.     case COMMENT:
  115.         if (c == '\n')
  116.         state = NEWLINE;
  117.         break;
  118.     case INSTRING:
  119.         switch (c) {
  120.         case '\\':
  121.         state = ESCAPE;
  122.         break;
  123.         case '"':
  124.         state = DONE;
  125.         break;
  126.         default:
  127.         *p++ = c;
  128.         }
  129.         break;
  130.     case ESCAPE:
  131.         switch (c) {
  132.         case 'n':
  133.         *p++ = '\n';
  134.         break;
  135.         case 'r':
  136.         *p++ = '\r';
  137.         break;
  138.         case 't':
  139.         *p++ = '\t';
  140.         break;
  141.         case 'e':
  142.         *p++ = '\033';
  143.         break;
  144.         case 'a':
  145.         *p++ = '\007';
  146.         break;
  147.         case '#':
  148.         case '"':
  149.         case '\\':
  150.         *p++ = c;
  151.         break;
  152.         case '\n':
  153.         break;
  154.         case '0':
  155.         case '1':
  156.         case '2':
  157.         case '3':
  158.         case '4':
  159.         case '5':
  160.         case '6':
  161.         case '7':
  162.         d = c - '0';
  163.         while ((c = fgetc(f)) >= '0' && c <= '7')
  164.             d = 8 * d + c - '0';
  165.         *p++ = d;
  166.         ungetc(c, f);
  167.         break;
  168.         default:
  169.         fprintf(stderr,
  170.             "language.txt:%d: illegal escape sequence: '\\%c'\n",
  171.             line, c);
  172.         break;
  173.         }
  174.         state = INSTRING;
  175.         break;
  176.     case IDENT:        /* language identifier */
  177.         if (c == ':') {
  178.         state = NEWLINE;
  179.         break;
  180.         }
  181.         if (c == '\n' && strncmp(lang, "No translation", 14) == 0) {
  182.         i = 0;
  183.         state = NEWLINE;
  184.         break;
  185.         }
  186.         lang[i++] = c;
  187.         if (i == 15 || !isalnum(c) && !isspace(c)) {
  188.         lang[i] = '\0';
  189.         fprintf(stderr,
  190.             "language.txt:%d: bad language identifier: '%s'\n",
  191.             line, lang);
  192.         state = ERROR;
  193.         i = 0;
  194.         }
  195.         break;
  196.     case DONE:
  197.         if (c == '\n') {
  198.         lang[i] = '\0';
  199.         *p = '\0';
  200.         return buf;
  201.         }
  202.         if (!isspace(c)) {
  203.         fprintf(stderr,
  204.             "language.txt:%d: extra characters after '\"'\n",
  205.             line);
  206.         state = ERROR;
  207.         }
  208.         break;
  209.     case ERROR:
  210.         if (c == '\n')
  211.         state = ERR1;
  212.         break;
  213.     case ERR1:
  214.         state = (c == '\n' ? NEWLINE : ERROR);
  215.         break;
  216.     }
  217.     }
  218.     if (state != NEWLINE)
  219.     fprintf(stderr, "language.txt: unexpected EOF\n");
  220.     return NULL;
  221. }
  222.  
  223. #ifdef TEST
  224. main()
  225. {
  226.     char buf[2048];
  227.  
  228.     line = 1;
  229.     while (readstr(stdin, buf, 0)) {
  230.     printf("\nen: <%s>\n", buf);
  231.     while (readstr(stdin, buf, 1) && *buf != '\0')
  232.         printf("%s: <%s>\n", lang, buf);
  233.     }
  234.     exit(0);
  235. }
  236. #else
  237.  
  238. static struct indx_ent {
  239.     word32 crc;
  240.     long offset;
  241. } *indx_tbl = NULL;
  242.  
  243. static int max_msgs = 0;
  244. static int nmsg = 0;
  245.  
  246. static FILE *langf;
  247.  
  248. static void init_lang(void);
  249.  
  250. static int make_indexfile(char *);
  251.  
  252. /*
  253.  * uses 24-bit CRC function from armor.c
  254.  */
  255. static word32
  256.  message_crc(char *s)
  257. {
  258.     return crcbytes((byte *) s, strlen(s), (word32) 0);
  259. }
  260.  
  261. /*
  262.  * lookup file offset in indx_tbl
  263.  */
  264. static long lookup_offset(word32 crc)
  265. {
  266.     int i;
  267.  
  268.     for (i = 0; i < nmsg; ++i)
  269.     if (indx_tbl[i].crc == crc)
  270.         return indx_tbl[i].offset;
  271.     return -1;
  272. }
  273.  
  274.  
  275. /*
  276.  * return foreign translation of s
  277.  */
  278. char *
  279.  LANG(char *s)
  280. {
  281.     long filepos;
  282.  
  283.     if (subtitles_available == 0)
  284.     init_lang();
  285.     if (subtitles_available < 0)
  286.     return s;
  287.  
  288.     filepos = lookup_offset(message_crc(s));
  289.     if (filepos == -1) {
  290.     return s;
  291.     } else {
  292.     fseek(langf, filepos, SEEK_SET);
  293.     readstr(langf, strbuf, 1);
  294.     }
  295.  
  296.     if (strbuf[0] == '\0')
  297.     return s;
  298.  
  299.     for (s = strbuf; *s; ++s)
  300.     *s = EXT_C(*s);
  301.     return strbuf;
  302. }
  303.  
  304.  
  305. static struct {
  306.     long lang_fsize;        /* size of language.txt */
  307.     char lang[16];        /* language identifier */
  308.     int nmsg;            /* number of messages */
  309. } indx_hdr;
  310.  
  311.  
  312. /*
  313.  * initialize the index table: read it from language.idx or create
  314.  * a new one and write it to the index file. A new index file is
  315.  * created if the language set in config.pgp doesn't match the one
  316.  * in language.idx or if the size of language.txt has changed.
  317.  */
  318. static void init_lang()
  319. {
  320.     char indexfile[MAX_PATH];
  321.     char subtitles_file[MAX_PATH];
  322.     FILE *indexf;
  323.  
  324.     if (strcmp(language, "en") == 0) {
  325.     subtitles_available = -1;
  326.     return;            /* use default messages */
  327.     }
  328.     buildfilename(subtitles_file, SUBTITLES_FILE);
  329.     langf = fopen(subtitles_file, FOPRTXT);
  330.     if (langf == NULL) {
  331.     subtitles_available = -1;
  332.     return;
  333.     }
  334.     init_crc();
  335.     strbuf = (char *) malloc(STRBUFSIZE);
  336.     if (strbuf == NULL) {
  337.     fprintf(stderr, "Not enough memory for foreign subtitles\n");
  338.     fclose(langf);
  339.     subtitles_available = -1;
  340.     return;
  341.     }
  342.     buildfilename(indexfile, LANG_INDEXFILE);
  343.     indexf = fopen(indexfile, FOPRBIN);
  344.     if (indexf != NULL) {
  345.     if (fread(&indx_hdr, 1, sizeof(indx_hdr),
  346.           indexf) == sizeof(indx_hdr) &&
  347.         indx_hdr.lang_fsize == fsize(langf) &&
  348.         strcmp(indx_hdr.lang, language) == 0) {
  349.         nmsg = indx_hdr.nmsg;
  350.         indx_tbl = (struct indx_ent *) malloc(nmsg *
  351.                           sizeof(struct indx_ent));
  352.         if (indx_tbl == NULL) {
  353.         fprintf(stderr, "Not enough memory for foreign subtitles\n");
  354.         fclose(indexf);
  355.         fclose(langf);
  356.         subtitles_available = -1;
  357.         return;
  358.         }
  359.         if (fread(indx_tbl,
  360.               sizeof(struct indx_ent), nmsg, indexf) != nmsg) {
  361.         free(indx_tbl);    /* create a new one */
  362.         indx_tbl = NULL;
  363.         }
  364.     }
  365.     fclose(indexf);
  366.     }
  367.     if (indx_tbl == NULL && make_indexfile(indexfile) < 0) {
  368.     fclose(langf);
  369.     subtitles_available = -1;
  370.     } else {
  371.     subtitles_available = 1;
  372.     }
  373. }
  374.  
  375.  
  376. static int make_indexfile(char *indexfile)
  377. {
  378.     FILE *indexf;
  379.     long filepos;
  380.     int total_msgs = 0;
  381.     char *res;
  382.  
  383.     if (verbose)        /* must be set in config.pgp */
  384.     fprintf(stderr,
  385.         "Creating language index file '%s' for language \"%s\"\n",
  386.         indexfile, language);
  387.     rewind(langf);
  388.     indx_hdr.lang_fsize = fsize(langf);
  389.     strncpy(indx_hdr.lang, language, 15);
  390.     init_crc();
  391.     line = 1;
  392.     nmsg = 0;
  393.     while (readstr(langf, strbuf, 0)) {
  394.     if (nmsg == max_msgs) {
  395.         if (max_msgs) {
  396.         max_msgs *= 2;
  397.         indx_tbl = (struct indx_ent *) realloc(indx_tbl, max_msgs *
  398.                         sizeof(struct indx_ent));
  399.         } else {
  400.         max_msgs = 400;
  401.         indx_tbl = (struct indx_ent *) malloc(max_msgs *
  402.                         sizeof(struct indx_ent));
  403.         }
  404.         if (indx_tbl == NULL) {
  405.         fprintf(stderr, "Not enough memory for foreign subtitles\n");
  406.         return -1;
  407.         }
  408.     }
  409.     ++total_msgs;
  410.     indx_tbl[nmsg].crc = message_crc(strbuf);
  411.     if (lookup_offset(indx_tbl[nmsg].crc) != -1)
  412.         fprintf(stderr,
  413.             "language.txt:%d: Message CRC not unique: \"%s\"\n",
  414.             line, strbuf);
  415.     do {
  416.         filepos = ftell(langf);
  417.         res = readstr(langf, strbuf, 1);    /* Abort if find newline
  418.                            first */
  419.     } while (res && strbuf[0] != '\0' && strcmp(language, lang) != 0);
  420.  
  421.     if (res == NULL)
  422.         break;
  423.     if (strbuf[0] == '\0')    /* No translation */
  424.         continue;
  425.  
  426.     indx_tbl[nmsg].offset = filepos;
  427.     ++nmsg;
  428.     do
  429.         res = readstr(langf, strbuf, 1);    /* Abort if find newline
  430.                            first */
  431.     while (res && strbuf[0] != '\0');
  432.     }
  433.     line = 0;
  434.     indx_hdr.nmsg = nmsg;
  435.     if (nmsg == 0) {
  436.     fprintf(stderr, "No translations available for language \"%s\"\n\n",
  437.         language);
  438.     return -1;
  439.     }
  440.     if (verbose || total_msgs != nmsg)
  441.     fprintf(stderr, "%d messages, %d translations\n\n", total_msgs, nmsg);
  442.  
  443.     if ((indexf = fopen(indexfile, FOPWBIN)) == NULL) {
  444.     fprintf(stderr, "Cannot create %s\n", indexfile);
  445.     } else {
  446.     fwrite(&indx_hdr, 1, sizeof(indx_hdr), indexf);
  447.     fwrite(indx_tbl, sizeof(struct indx_ent), nmsg, indexf);
  448.     if (ferror(indexf) || fclose(indexf))
  449.         fprintf(stderr, "error writing %s\n", indexfile);
  450.     }
  451.     return 0;
  452. }
  453. #endif                /* TEST */
  454.