home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / bts310b5 / langload.c < prev    next >
C/C++ Source or Header  |  1993-05-17  |  7KB  |  273 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-90, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                            */
  12. /*                                                                            */
  13. /*                 This module was written by Vince Perriello                 */
  14. /*                                                                            */
  15. /*                                                                            */
  16. /*                      BinkleyTerm Language File Loader                        */
  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.240.    */
  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:132/491, 1:141/491    */
  37. /* P.O. Box 460398                  AlterNet 7:491/0                            */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                  Internet f491.n132.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. #ifdef __TOS__
  49. #include <ext.h>
  50. #else
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #endif
  54. #include <string.h>
  55.  
  56. #ifdef __TURBOC__
  57. #ifndef __TOS__
  58. #include <alloc.h>
  59. #endif
  60. #else
  61. #ifndef LATTICE
  62. #include <malloc.h>
  63. #endif
  64. #endif
  65.  
  66.  
  67. #include "bink.h"
  68. #include "msgs.h"
  69.  
  70. #define LANGFILE "BINKLEY.LNG"
  71. #define DEVICEDRIVER "BINKLEY.IO"
  72.  
  73.  
  74. /*
  75.  * Read the compiled BinkleyTerm Language file.
  76.  *
  77.  */
  78.  
  79. int load_language (void)
  80. {
  81.     int pointer_size;
  82.     char *memory;
  83.     size_t memory_size;
  84.     char *malloc_target;
  85.     char *envptr;
  86.     char LANGpath[128];
  87.     int error;
  88.     int i;
  89.     size_t read;
  90.     struct stat stbuf;
  91.     FILE           *fpt;                         /* stream pointer             */
  92.  
  93.    envptr = getenv ("BINKLEY");                  /* get path from environment*/
  94.    if ((envptr != NULL)                          /* If there was one, and     */
  95.        && (!dexists (LANGFILE)))                 /* No local language file,  */
  96.       {
  97.       (void) strcpy (LANGpath, envptr);          /* use BINKLEY as our path  */
  98.       (void) add_backslash (LANGpath);
  99.       }
  100.    else
  101.       LANGpath[0] = '\0';
  102.  
  103.    strcat (LANGpath, LANGFILE);
  104.  
  105.    /*
  106.     * Get some info about the file
  107.     */
  108.  
  109.     error = stat (LANGpath, &stbuf);
  110.     if (error != 0)
  111.         {
  112.         fprintf (stderr, "Cannot get information on file %s\n",LANGpath);
  113.         if (reset_port) exit_port();
  114.         exit (250);
  115.         }
  116.  
  117.    /*
  118.     * Allocate space for the raw character array and for the
  119.     * pointer and fixup arrays
  120.     *
  121.     */
  122.  
  123.     memory_size = stbuf.st_size;
  124.  
  125.     malloc_target = malloc (memory_size);
  126.     if (malloc_target == NULL)
  127.         {
  128.         fprintf (stderr, "Unable to allocate string memory\n");
  129.         if (reset_port) exit_port();
  130.         exit (250);
  131.         }
  132.  
  133.    /*
  134.     * Open the input file
  135.     *
  136.     */
  137.  
  138.     fpt = fopen (LANGpath, read_binary);                /* Open the file             */
  139.     if (fpt == NULL)                            /* Were we successful?         */
  140.         {
  141.         fprintf (stderr, "Can not open input file %s\n", LANGpath);
  142.         if (reset_port) exit_port();
  143.         exit (250);
  144.         }
  145.  
  146.    /*
  147.     * Read the entire file into memory now.
  148.     *
  149.     */
  150.  
  151.     read = fread (malloc_target, 1, memory_size, fpt);
  152.     if (read != memory_size)
  153.         {
  154.         fprintf (stderr, "Could not read language data from file %s\n",LANGpath);
  155.         fclose (fpt);
  156.         if (reset_port) exit_port();
  157.         exit (250);
  158.         }
  159.  
  160.    /*
  161.     * Close the file.
  162.     *
  163.     */
  164.  
  165.     error = fclose (fpt);
  166.     if (error != 0)
  167.         {
  168.         fprintf (stderr, "Unable to close language file %s\n",LANGpath);
  169.         if (reset_port) exit_port();
  170.         exit (250);
  171.         }
  172.  
  173.    /*
  174.     * Do fixups on the string pointer array as follows:
  175.     *
  176.     * 1. Find the NULL pointer in the mess here.
  177.     * 2. Start of the string memory is the "following pointer".
  178.     * 3. Apply arithmetic correction to entire array.
  179.     *
  180.     */
  181.  
  182.     msgtxt = (char **) malloc_target;
  183.     for (i = 0; msgtxt[i] != NULL; i++)         /* Find NULL marker in array */
  184.         ;
  185.  
  186.     pointer_size = i - 1;                        /* Count of elements w/o NULL*/
  187.     if (pointer_size != X_TOTAL_MSGS)
  188.         {
  189. #ifdef NEW
  190.         fprintf(stderr, "Incompatible language file %s\n", LANGFILE);
  191.         fprintf(stderr,    "It has %d messages instead of %d\n",
  192.                     pointer_size, X_TOTAL_MSGS);
  193.         fprintf(stderr, "Recompile it by using BTLNG.TOS\n");
  194. #else
  195.         fprintf (stderr, "Count of %d from file does not match %d required\n",
  196.                     pointer_size, X_TOTAL_MSGS);
  197. #endif
  198.         if (reset_port) exit_port();
  199.         exit (250);
  200.         }
  201.  
  202.     memory = (char *) &msgtxt[++i];             /* Text starts after NULL     */
  203.  
  204.     for (i = 1; i <= pointer_size; i++)
  205.         {
  206.         msgtxt[i] = memory + (msgtxt[i] - msgtxt[0]);
  207.         }
  208.  
  209.     msgtxt[0] = memory;
  210.  
  211.  
  212. /* Read Device-Driver */
  213.  
  214.  
  215.    envptr = getenv ("BINKLEY");                  /* get path from environment*/
  216.    if ((envptr != NULL)                          /* If there was one, and     */
  217.        && (!dexists (DEVICEDRIVER)))                 /* No local language file,  */
  218.       {
  219.       (void) strcpy (LANGpath, envptr);          /* use BINKLEY as our path  */
  220.       (void) add_backslash (LANGpath);
  221.       }
  222.    else
  223.       LANGpath[0] = '\0';
  224.  
  225.    strcat (LANGpath, DEVICEDRIVER);
  226.  
  227.     error = stat (LANGpath, &stbuf);
  228.     if (error != 0)
  229.         {    fprintf (stderr, "Cannot get information on file %s\n",LANGpath);
  230.             if (reset_port) exit_port();
  231.             exit (250);
  232.         }
  233.     else
  234.         {    memory_size = stbuf.st_size;
  235.  
  236.             malloc_target = malloc (memory_size);
  237.             if (malloc_target == NULL)
  238.                 {
  239.                 fprintf (stderr, "Unable to allocate devicedriver memory\n");
  240.                 if (reset_port) exit_port();
  241.                 exit (250);
  242.                 }
  243.         
  244.             fpt = fopen (LANGpath, read_binary);                /* Open the file             */
  245.             if (fpt == NULL)                            /* Were we successful?         */
  246.                 {
  247.                 fprintf (stderr, "Can not open input file %s\n", LANGpath);
  248.                 if (reset_port) exit_port();
  249.                 exit (250);
  250.                 }
  251.         
  252.             read = fread (malloc_target, 1, memory_size, fpt);
  253.             if (read != memory_size)
  254.                 {
  255.                 fprintf (stderr, "Could not read devicedriver data from file %s\n",LANGpath);
  256.                 fclose (fpt);
  257.                 if (reset_port) exit_port();
  258.                 exit (250);
  259.                 }
  260.         
  261.             error = fclose (fpt);
  262.             if (error != 0)
  263.                 {
  264.                 fprintf (stderr, "Unable to close devicedriver file %s\n",LANGpath);
  265.                 if (reset_port) exit_port();
  266.                 exit (250);
  267.                 }
  268.             device_addr = (long) malloc_target;
  269.         }
  270.  
  271.     return (1);
  272. }
  273.