home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / dev / misc / flexcat / src / flexcat.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  32KB  |  1,540 lines

  1. /*
  2.     FlexCat.c:    The flexible catalog creator                V 1.01
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.  
  20.     Ok, this is nothing special. It grabs a catalog translation and a
  21.     catalog description file and produces catalogs and the source to
  22.     handle 'em. What is it else than lots of other programs?
  23.  
  24.     The difference is, that YOU determine what source FlexCat produces.
  25.     Another file is scanned by FlexCat to produce code. This file contains
  26.     some c-string like special characters (%v for version for example)
  27.     You can edit this file and modify it as you want. So FlexCat can produce
  28.     C source as well as Assembler, Oberon, Modula 2, E, ...
  29.  
  30.     Command line syntax:
  31.  
  32.     FlexCat CDFILE/A,CTFILE,NEWCTFILE/K,CATALOG/K,ASSEMBLER/S,SOURCES/M
  33.  
  34.     CDFILE    - the name of the catalog description file; see CatComp for
  35.         syntax
  36.     CTFILE    - the name of a catalog translation file; see CatComp for
  37.         syntax
  38.     NEWCTFILE - the name of a catalog transcription file to be created
  39.         (will be a copy of CDFILE except that the strings remain
  40.         empty and will be put as comments behind the string
  41.         definition)
  42.     CATALOG   - the name of the catalog to be created; default is the
  43.         basename of the CDFILE and .catalog appended
  44.     SOURCES   - the sourcefiles to be created. These arguments take a special
  45.         form: sourcefile=templatefile where sourcefile is the
  46.         name of the file to create and templatefile is the name of
  47.         a template file containing some special patterns. The
  48.         template file will be scanned and written to the source
  49.         file with these special patterns replaced by certain
  50.         strings. Possible patterns are:
  51.  
  52.             %b        basename of CDFILE (for example "file", if
  53.                 CDFILE is "file.cd")
  54.             %v        version
  55.             %l        language (of CDFILE)
  56.             %n        number of catalog strings
  57.             %%        the percent sign itself
  58.  
  59.         The following patterns indicate, that the line should be
  60.         repeated for each catalog string.
  61.             %i        the string's ID descriptor
  62.             %d        the string's ID
  63.             %s        the string itself
  64.             %(...)  indicates that anything between the brackets
  65.                 should appear for any line except the last
  66.                 (Oberon and Modula need a "," to separate array
  67.                 entries, but the last entry must not be
  68.                 terminated by a ",". So you would terminate an
  69.                 array entry by a %(,).)
  70.  
  71.     Example: Let the string description be
  72.  
  73.     MSG_HELLO (5//)
  74.     Hello!
  75.  
  76.     Then the special characters produce
  77.     %i = MSG_HELLO
  78.     %d = 5
  79.     %s = "Hello!"
  80.  
  81.     Additionally the following characters may appear (as well as in the
  82.     catalog description and catalog translation file)
  83.     \b    = Backspace (Ascii 8)
  84.     \c    = Control sequence introducer (Ascii 155)
  85.     \e    = Escape (Ascii 27)
  86.     \f    = Form feed (Ascii 12)
  87.     \g    = Display beep (Ascii 7)
  88.     \n    = line feed, newline (Ascii 10)
  89.     \r    = carriage return (Ascii 13)
  90.     \t    = Tab (Ascii 9)
  91.     \v    = Vertical tab (Ascii 11)
  92.     \)    = the character ')' (Needed inside %(..))
  93.     \\    = the backslash itself
  94.     \xHH  = Ascii code HH (where HH are hex digits)
  95.     \OOO  = Ascii code OOO (where OOO are octal digits)
  96.  
  97.     A backslash at the end of a line indicates that the line should be
  98.     concatenated with the next one.
  99.  
  100.  
  101.     Computer:    Amiga 1200            Compiler: Dice, 2.07.54R
  102.  
  103.     Author:    V 1.0        31.06.1993        Jochen Wiedmann
  104.                         Am Eisteich 9
  105.                       72555 Metzingen
  106.                         Tel. 07123 / 14881
  107.  
  108.                 Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  109.  
  110.         V1.01        Fixed a bug: The length of the source string
  111.                 was used to check for the stringlen instead of
  112.                 the real stringlen.
  113. */
  114.  
  115.  
  116.  
  117. #include <stdlib.h>
  118. #include <stdio.h>
  119. #include <string.h>
  120. #include <ctype.h>
  121. #ifdef AMIGA
  122. #include <exec/types.h>
  123. #include <clib/exec_protos.h>
  124. #include "FlexCat_cat.h"
  125. #ifdef AZTEC_C
  126. #include <pragmas/exec_lib.h>
  127. #endif
  128. #endif
  129.  
  130. #ifdef _DCC
  131. #define index strchr
  132. #define rindex strrchr
  133. #endif
  134.  
  135. #ifndef FALSE
  136. #define FALSE 0
  137. #endif
  138. #ifndef TRUE
  139. #define TRUE (!FALSE)
  140. #endif
  141.  
  142.  
  143. #define TYPE_C         0  /*    Produce C strings            */
  144. #define TYPE_ASSEMBLER     1  /*    Produce Assembler strings        */
  145. #define TYPE_OBERON     2  /*    Produce Oberon strings            */
  146. #define TYPE_NONE     3  /*    Simple strings                */
  147.  
  148. #define OutputMode_None  0  /*    Nothing written yet            */
  149. #define OutputMode_Bin     1  /*    Last character written was binary   */
  150. #define OutputMode_Ascii 2  /*    Last character written was Ascii    */
  151.  
  152. struct CatString
  153. { struct CatString *Next;
  154.   char *CD_Str;
  155.   char *CT_Str;
  156.   char *ID_Str;
  157.   int MinLen, MaxLen, ID;
  158. };
  159.  
  160. struct CDLine
  161. { struct CDLine *Next;
  162.   char *Line;
  163. };
  164.  
  165.  
  166. struct CatString *FirstCatString = NULL;  /*  First catalog string    */
  167. struct CDLine *FirstCDLine = NULL;  /*    First catalog description line    */
  168.  
  169. char *BaseName = "";                /*  Basename of catalog description */
  170. char *Language = "english";         /*  Language of catalog description */
  171. int CatVersion = 0;            /*    Version of catalog to be opened */
  172. int LengthBytes = 0;            /*    Number of bytes to preceed a    */
  173.                     /*    created string and containing    */
  174.                     /*    its length.            */
  175. char *CatLanguage = NULL;        /*    Language of catalog translation */
  176. char *CatVersionString = NULL;        /*    version string of catalog    */
  177.                     /*    translation            */
  178. int CodeSet = 0;            /*    Codeset of catalog translation    */
  179. int NumStrings = 0;            /*    Number of catalog strings    */
  180. int LongStrings = TRUE;         /*    Generate long or short strings    */
  181.  
  182.  
  183. char *ScanFile;             /*    File currently scanned        */
  184. int ScanLine;                /*    Line currently scanned        */
  185.  
  186. struct Library *LocaleBase = NULL;
  187.  
  188.  
  189.  
  190. /*
  191.     This terminates the program.
  192. */
  193. void Cleanup(int result)
  194.  
  195. {
  196. #ifdef AMIGA
  197.   CloseFlexCatCatalog();
  198.   if (LocaleBase)
  199.   { CloseLibrary(LocaleBase);
  200.   }
  201. #endif
  202.   exit(result);
  203. }
  204.  
  205. /*
  206.     This shows an error message and terminates
  207. */
  208. struct RDArgs *Args;
  209. void ShowError(const STRPTR msg, ...)
  210.  
  211. { fprintf(stderr, (char *) msg, (&msg)[1], (&msg)[2], (&msg)[3], (&msg)[4]);
  212.   putc('\n', stderr);
  213.   Cleanup(10);
  214. }
  215.  
  216.  
  217.  
  218. /*
  219.     This shows the message: Memory error.
  220. */
  221. void MemError(void)
  222.  
  223. { ShowError(GetFlexCatString(msgMemoryError), NULL);
  224. }
  225.  
  226.  
  227.  
  228.  
  229. /*
  230.     This shows a warning
  231. */
  232. void ShowWarn(STRPTR msg, ...)
  233.  
  234. { fprintf(stderr, (char *) GetFlexCatString(msgWarning), ScanFile, ScanLine);
  235.   fprintf(stderr, (char *) msg, (&msg)[1], (&msg)[2], (&msg)[3], (&msg)[4]);
  236.   putc('\n', stderr);
  237. }
  238.  
  239.  
  240.  
  241.  
  242. /*
  243.     This allocates a string
  244. */
  245. char *AllocString(const char *str)
  246.  
  247. { char *ptr;
  248.  
  249.   if (!(ptr = malloc(strlen(str)+1)))
  250.   { MemError();
  251.   }
  252.   strcpy(ptr, str);
  253.   return(ptr);
  254. }
  255.  
  256.  
  257.  
  258.  
  259. /*
  260.     This translates a hex character.
  261. */
  262. int gethex(int c)
  263.  
  264. {
  265.   if (c >= '0'  &&  c <= '9')
  266.   { return(c - '0');
  267.   }
  268.   else if (c >= 'a'  &&  c <= 'f')
  269.   { return(c - 'a' + 10);
  270.   }
  271.   else if (c >= 'A'  &&  c <= 'F')
  272.   { return(c - 'A' + 10);
  273.   }
  274.   ShowWarn(GetFlexCatString(msgExpectedHex));
  275.   return(0);
  276. }
  277.  
  278.  
  279.  
  280.  
  281. /*
  282.     This translates an octal digit.
  283. */
  284. int getoctal(int c)
  285.  
  286. {
  287.   if (c >= '0'  &&  c <= '7')
  288.   { return(c - '0');
  289.   }
  290.   ShowWarn(GetFlexCatString(msgExpectedOctal));
  291.   return(0);
  292. }
  293.  
  294.  
  295.  
  296.  
  297. /*
  298.     Reading a line is somewhat compicated in order to allow lines of any
  299.     length.
  300.  
  301.     Inputs: fp         - the file, where the input comes from
  302.         AllowComment - TRUE, if a leading semicolon should force to
  303.                interpret the line as a comment line
  304. */
  305. #define BUFSIZE 4096
  306. char *ReadLine(FILE *fp, int AllowComment)
  307.  
  308. { char *OldLine, *NewLine = NULL;
  309.   int c = '\0';
  310.   int Len = 0, LineLen = 0;
  311.   int CommentLine = FALSE, FirstChar = TRUE;
  312.   int BackslashSeen = FALSE;
  313.  
  314.   while (c != EOF)
  315.   { if (Len+10 > LineLen)
  316.