home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BSRC_250.LZH / GET_LANG.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  16KB  |  408 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-91, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*               This module was written by Vince Perriello                 */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*             BinkleyTerm Language Compiler Raw Input Module               */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.250.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n343.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*--------------------------------------------------------------------------*/
  45.  
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #ifdef __TURBOC__
  50. #include <mem.h>
  51. #else
  52. #ifdef _MSC_VER_
  53. #include <memory.h>
  54. #else
  55. #include <string.h>
  56. #endif
  57. #endif
  58.  
  59. #ifdef OS_2
  60. #define OS_IDENT "OS2"
  61. #else
  62. #define OS_IDENT "DOS"
  63. #endif
  64.  
  65.  
  66. #include "language.h"
  67.  
  68. int parse_escapes (char *);
  69.  
  70. /*
  71.  *  get_language - read in BinkleyTerm language source
  72.  *
  73.  * Read lines into table, ignoring blanks and comments
  74.  * Store into a contiguous block of memory with the individual
  75.  *     members being referenced by an array of pointers
  76.  * Store number of lines read into pointer_size
  77.  * Store amount of memory used into memory_size
  78.  *
  79.  */
  80.  
  81. int get_language (char *name_of_file)
  82. {
  83.     int             len;                        /* length of current string  */
  84.     int             count_from_file;            /* no. of strings in file    */
  85.     int             file_version;               /* version of file           */
  86.     char           *p, *q;                      /* miscellaneous pointers    */
  87.     char           *storage;                    /* where we're storing now   */
  88.     char          **load_pointers;              /* pointer to pointer array  */
  89.     char            linebuf[255];               /* biggest line we'll allow  */
  90.     FILE           *fpt;                        /* stream pointer            */
  91.     int             internal_count;             /* How many strings we got   */
  92.     int             total_size;                 /* How big it all is         */
  93.     int             error;                      /* Internal error value      */
  94.     int             PrdctCode;                  /* Product Code              */
  95.     char           *PrdctNm;
  96.     char           *PrdctPtr = PrdctMem;
  97.     char           *n;
  98.     char           *LangStart;
  99.     unsigned int    ansival;                    /* Scanned ANSI keymap value */
  100.  
  101.     internal_count = 0;                         /* zero out internal counter */
  102.     count_from_file = 0;                        /* zero out internal counter */
  103.     total_size = 0;                             /* Initialize storage size   */
  104.     error = 0;                                  /* Initialize error value    */
  105.  
  106.     load_pointers = pointers;                   /* Start at the beginning    */
  107.     storage = memory;                           /* A very good place to start*/
  108.  
  109.     /*
  110.      * Open the file now. Then read in the appropriate table. First line of
  111.      * the file contains the number of lines we want Second line through end:
  112.      * ignore if it starts with a ; and store only up to ;
  113.      *
  114.      */
  115.  
  116.     fpt = fopen (name_of_file, "r");            /* Open the file             */
  117.     if (fpt == NULL)                            /* Were we successful?       */
  118.         {
  119.         (void) fprintf (stderr, "Can not open input file %s\n", name_of_file);
  120.         return (-1);                            /* Return failure to caller  */
  121.         }
  122.  
  123.     while (fgets (linebuf, 254, fpt) != NULL)   /* read a line in            */
  124.         {
  125.         p = q = linebuf;                        /* set up for scan           */
  126.  
  127. /*
  128.  * This label is only hit when a ? line is seen.
  129.  *
  130.  * The format of a ? line is:
  131.  *
  132.  *        ?xxx ....
  133.  *
  134.  * where xxx is a 3-character platform identifier. For DOS systems,
  135.  * the identifier is DOS and for OS/2 it is OS2. The text following
  136.  * ?xxx is the same format as any other language file line.
  137.  *
  138.  * When we see a ?, we compare the following string to the ID of our
  139.  * current platform. If it matches, we point p and q at the text following
  140.  * the expression, and (I'm sorry) jump back. If it doesn't match, we throw
  141.  * the line away.
  142.  */
  143.  
  144. re_cycle:
  145.  
  146.         switch (*p)
  147.             {
  148.  
  149.             case '?':
  150.  
  151.             if (strncmp (++p, OS_IDENT, 3) == 0)
  152.                {
  153.                q = p += 3;
  154.                goto re_cycle;
  155.                }
  156.             break;
  157.  
  158.             case ';':                           /* Comment                   */
  159.             case 'C':                           /* Comment                   */
  160.  
  161.             break;
  162.  
  163.             case 'L':                           /* Language Line             */
  164.  
  165.             LangStart = ++p;
  166.             (void) parse_escapes (p);
  167.             if ((len = strlen (p)) == 0)      /* Is anything there?        */
  168.                 continue;                     /* No -- ignore.             */
  169.  
  170.             if (!count_from_file)
  171.                 {
  172.                 (void) sscanf (LangStart,"%d %d",&count_from_file, &file_version);
  173.                 if (count_from_file <= pointer_size)
  174.                     continue;
  175.  
  176.                 (void) fprintf (stderr, 
  177.                     "Messages in file = %d, Pointer array size = %d\n",
  178.                         count_from_file, pointer_size);
  179.                 error = -2;
  180.                 break;
  181.                 }
  182.  
  183.             ++len;                                  /* Allow for the terminator  */
  184.             if (((total_size += len) < memory_size) /* Make sure it will fit     */
  185.             &&  (internal_count < pointer_size))
  186.                 {
  187.                 (void) memcpy (storage, LangStart, len); /* Copy it now (with term)*/
  188.                 *load_pointers++ = storage;         /* Point to start of string  */
  189.                 storage += len;                     /* Move pointer into memory  */                
  190.                 }
  191.  
  192.             ++internal_count;                       /* bump count */
  193.             break;
  194.  
  195.  
  196.             case 'A':                           /* ANSI key output map       */
  197.  
  198.             (void) sscanf (++p, "%4x", &ansival);
  199.             if (*(p += 4) != ' ')
  200.                break;
  201.             if (*++p == ' ')
  202.                break;
  203.             for (q = p; *q != '\0' && *q != ' '; q++)
  204.                ;
  205.             *q = '\0';
  206.  
  207.             q = AnsiMem + AnsiHdr.PoolSize;
  208.             len = parse_escapes (p);
  209.             *((unsigned int *)q) = ansival;
  210.             q += sizeof (unsigned int);
  211.             *q++ = (char) len;
  212.             strncpy (q, p, len);
  213.             AnsiHdr.ElemCnt++;
  214.             AnsiHdr.PoolSize = (int) ((q += len) - AnsiMem);
  215.             break;
  216.  
  217.             case 'P':                           /* Product Code              */
  218.                                                 /* Format: nnn ProductName   */
  219.             PrdctCode = (int) strtol (++p, &PrdctNm, 10);
  220.             while (' ' == *PrdctNm) ++PrdctNm;
  221.             n = PrdctNm + strlen (PrdctNm) - 1;
  222.             while ((PrdctNm <= n) && ((*n == ' ') || (*n == '\n')))
  223.                 *n-- = '\0';
  224.  
  225.             if (PrdctCode == -1)
  226.                 {
  227.                 strcpy (PrdctMem, PrdctNm);
  228.                 PrdctPtr = PrdctMem + strlen (PrdctMem) + 1;
  229.                 PrdctHdr.PoolSize += strlen (PrdctNm) + 1;
  230.                 }
  231.             else if ((0 <= PrdctCode) && (MAX_PRDCTS > PrdctCode))
  232.                 {
  233.                 switch (strlen (PrdctNm))
  234.                     {
  235.                     case 0:
  236.                         PrdctTbl[PrdctCode] = PrdctMem;
  237.                         break;
  238.                     default:
  239.                         PrdctTbl[PrdctCode] = PrdctPtr;
  240.                         strcpy (PrdctPtr, PrdctNm);
  241.                         PrdctPtr += strlen (PrdctNm) + 1;
  242.                         PrdctHdr.PoolSize += strlen (PrdctNm) + 1;
  243.                         break;
  244.                     }                /* end of switch (strlen (PrdctNm))   */
  245.                 }                    /* end of if (...)                    */
  246.             break;
  247.  
  248.             case 'T':                  /* Terminal Mode                      */
  249.                                        /* Format: KeyVal KeyXlate            */
  250.             (void) sscanf (++p,
  251.                            "%x %x",
  252.                            &TrmnlAccelTbl[TrmnlAccelCnt].ScanCode,
  253.                            &TrmnlAccelTbl[TrmnlAccelCnt].FncIdx);
  254.             ++TrmnlAccelCnt;
  255.             break;
  256.  
  257.             case 'U':                  /* Unattended Mode                    */
  258.                                        /* Format: KeyVal KeyXlate            */
  259.             (void) sscanf (++p,
  260.                            "%x %x",
  261.                            &UnattendedAccelTbl[UnattendedAccelCnt].ScanCode,
  262.                            &UnattendedAccelTbl[UnattendedAccelCnt].FncIdx);
  263.             ++UnattendedAccelCnt;
  264.             break;
  265.  
  266.             default:
  267.             break;
  268.             }                          /* end of switch (...)                */
  269.         }                              /* end of while (...)                 */
  270.     /*
  271.      * Close the file. Make sure the counts match and that memory size was
  272.      * not exceeded. If so, we have a winner! If not, snort and puke. 
  273.      *
  274.      */
  275.  
  276.     (void) fclose (fpt);
  277.  
  278.     if (internal_count > pointer_size)          /* Enough pointers?          */
  279.         {
  280.         (void) fprintf (stderr,
  281.             "%d messages read exceeds pointer array size of %d\n",
  282.                 internal_count, pointer_size);
  283.         error = -3;
  284.         }
  285.  
  286.     if (total_size > memory_size)               /* Did we fit?               */
  287.         {
  288.         (void) fprintf (stderr,
  289.             "Required memory of %d bytes exceeds %d bytes available\n",
  290.                 total_size, memory_size);
  291.         error = -4;
  292.         }
  293.  
  294.     if (count_from_file != internal_count)
  295.         {
  296.         (void) fprintf (stderr, 
  297.             "Count of %d lines does not match %d lines actually read\n",
  298.                 count_from_file, internal_count);
  299.         error = -5;
  300.         }
  301.  
  302.     if (!error)
  303.         {
  304.         pointer_size = internal_count;          /* Store final usage counts  */
  305.         memory_size = total_size;
  306.         *load_pointers = NULL;                  /* Terminate pointer table   */
  307.         }
  308.  
  309.     return (error);
  310. }
  311.  
  312. int parse_escapes (char *string)
  313. {
  314.    char c;
  315.    char *p, *q;
  316.    int escape_on = 0;
  317.  
  318.    p = q = string;
  319.  
  320.    while ((c = *p++) != '\0')
  321.        {
  322.        switch (c)
  323.            {
  324.            case ';':
  325.                if (escape_on)
  326.                    {
  327.                    *q++ = ';';
  328.                    --escape_on;
  329.                    break;
  330.                    }
  331.            /* Otherwise drop into newline code */
  332.  
  333.            case '\n':
  334.                *q = *p = '\0';
  335.                break;
  336.  
  337.            case '\\':
  338.                if (escape_on)
  339.                    {
  340.                    *q++ = '\\';
  341.                    --escape_on;
  342.                    }
  343.                else
  344.                    ++escape_on;
  345.                break;
  346.  
  347.            case 'n':
  348.                if (escape_on)
  349.                    {
  350.                    *q++ = '\n';
  351.                    --escape_on;
  352.                    }
  353.                else
  354.                    *q++ = c;
  355.                break;
  356.  
  357.            case 'r':
  358.                if (escape_on)
  359.                    {
  360.                    *q++ = '\r';
  361.                    --escape_on;
  362.                    }
  363.                else
  364.                    *q++ = c;
  365.                break;
  366.  
  367.            case 'b':
  368.                if (escape_on)
  369.                    {
  370.                    *q++ = ' ';
  371.                    --escape_on;
  372.                    }
  373.                else
  374.                    *q++ = c;
  375.                break;
  376.  
  377.  
  378.            case 'X':
  379.            case 'x':
  380.                if (escape_on)
  381.                    {
  382.                    *q++ = (char) strtol (p, &p, 16);
  383.                    --escape_on;
  384.                    }
  385.                else
  386.                    *q++ = c;
  387.                break;
  388.  
  389.            case '0':
  390.                if (escape_on)
  391.                    {
  392.                    *q++ = (char) strtol (p, &p, 8);
  393.                    --escape_on;
  394.                    }
  395.                else
  396.                    *q++ = c;
  397.                break;
  398.  
  399.            default:
  400.                *q++ = c;
  401.                escape_on = 0;
  402.                break;
  403.           }
  404.        }
  405.    return (int) (q - string);
  406. }
  407.  
  408.