home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / util2src / t2bm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-24  |  5.5 KB  |  158 lines

  1. /*============================================================================*
  2.  * main() module: t2bm.c - Convert text stream to BookMaster stream.
  3.  *
  4.  * (C)Copyright IBM Corporation, 1990, 1991, 1992.        Brian E. Yoder
  5.  *
  6.  * This program converts an input ASCII text stream to a BookMaster output
  7.  * stream.  Specific characters (see text2bm.c) are translated to their
  8.  * BookMaster symbol equivalents.  For example, a colon ":" is translated
  9.  * into "&colon." so that it won't be interpreted as the start of a GML tag.
  10.  * Other translations are performed for characters that don't map directly
  11.  * into EBCDIC and therefore don't translate correctly uploaded to VM/MVS.
  12.  *
  13.  * It was initially developed to test the text2bm() subroutine in the
  14.  * text2bm.c source module.  However, it is also a useful utility program
  15.  * in its own right.
  16.  *
  17.  * 07/19/90 - Created.
  18.  * 07/19/90 - Initial version of the test program.
  19.  * 01/28/91 - Allow stdin/stdout, as well as specific file names.  Also,
  20.  *            don't enclose output within :gdoc/:xmp. and /:exmp/:egdoc tags.
  21.  * 04/03/91 - Ported from AIX to DOS C/2.
  22.  * 09/11/91 - Explicitly open files in text mode, for DOS, OS/2.
  23.  * 07/24/92 - Changed "rt" to "r" for fopen(). C Set/2 doesn't allow "rt".
  24.  *============================================================================*/
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <sys/types.h>
  29.  
  30. #include "util.h"
  31.  
  32. #define LINELEN 1024               /* Maximum input file line length allowed */
  33. static  char line[LINELEN + 1];    /* Input file line buffer */
  34.  
  35. /*============================================================================*
  36.  * Define internal functions to allow forward access
  37.  *============================================================================*/
  38.  
  39. static void syntax();
  40.  
  41. /*============================================================================*
  42.  * Main Program Entry Point
  43.  *============================================================================*/
  44.  
  45. main(argc, argv)
  46.  
  47. int argc;           /* arg count */
  48. char *argv[];       /* arg pointers */
  49.  
  50. {
  51.   int numargs;      /* No. of arguments processed (not including -flags) */
  52.  
  53.   int  rc;          /* Return code store area */
  54.   char cf;          /* Current flag character (if any) */
  55.  
  56.   char *inname;     /* Pointers to arguments */
  57.   char *outname;
  58.  
  59.   FILE *infile;
  60.   FILE *outfile;
  61.  
  62.   int   i;          /* Integer index */
  63.   char *text;       /* Pointer to text string */
  64.  
  65.   argv++;                 /* Ignore 1st argument (program name) */
  66.   argc--;
  67.  
  68.   inname = argv[0];       /* Store pointers to arguments */
  69.   outname = argv[1];
  70.  
  71.   rc = 0;                 /* Initialize return code value */
  72.  
  73. /*============================================================================*
  74.  * Open input file and create output file
  75.  *============================================================================*/
  76.  
  77.   switch (argc)
  78.   {
  79.      case 1:                        /* One argument: -i expected: */
  80.         if (strcmp(inname, "-i") == 0)   /* If -i: */
  81.         {
  82.            infile = stdin;                    /* Set input file to stdin, */
  83.            outfile = stdout;                  /* Set output file to stdout */
  84.         }
  85.         else
  86.            syntax();
  87.         break;
  88.  
  89.      case 2:                        /* Two arguments: Filenames expected: */
  90.         infile = fopen(inname, "r");
  91.         if (infile == NULL)
  92.         {
  93.            fprintf(stderr, "Cannot access input file: '%s'.\n", inname);
  94.            return(1);
  95.         }
  96.  
  97.         outfile = fopen(outname, "w");
  98.         if (outfile == NULL)
  99.         {
  100.            fprintf(stderr, "Cannot create output file: '%s'.\n", outname);
  101.            return(1);
  102.         }
  103.         break;
  104.  
  105.      default:                       /* Other no. of args: Display syntax: */
  106.         syntax();
  107.         break;
  108.   }
  109.  
  110. /*============================================================================*
  111.  * Loop to process the input file line-by-line
  112.  *============================================================================*/
  113.  
  114. /*fprintf(outfile, ":gdoc.\n:xmp keep=5.\n");  Removed 1/28/91 */
  115.  
  116.   for (;;)
  117.   {
  118.      text = fgets(line, LINELEN,
  119.                   infile);
  120.      if (text == NULL)
  121.         break;
  122.  
  123.      rc = text2bm(line, outfile);
  124.      if (rc != 0)
  125.      {
  126.        fprintf(stderr, "--- Error detected by text2bm().\n");
  127.        break;
  128.      }
  129.   }
  130.  
  131. /*fprintf(outfile, ":exmp.\n:egdoc.\n");       Removed 1/28/91 */
  132.  
  133. /*============================================================================*
  134.  * Done
  135.  *============================================================================*/
  136.  
  137.   return(rc);                       /* Done with main(): Return */
  138.  
  139. } /* end of main() */
  140.  
  141. /*============================================================================*
  142.  * syntax() - Display command syntax and exit to operating system!
  143.  *============================================================================*/
  144. static void syntax()
  145. {
  146.   fprintf(stderr, "Usage: t2bm TextFile BookMasterFile\n");
  147.   fprintf(stderr, "   or: t2bm -i\n");
  148.   fprintf(stderr, "\n");
  149.   fprintf(stderr, "This program copies the text file to the BookMaster file,\n");
  150.   fprintf(stderr, "converting special characters into BookMaster symbols.\n");
  151.   fprintf(stderr, "\n");
  152.   fprintf(stderr, "If -i is specified instead of the two filenames, then this\n");
  153.   fprintf(stderr, "program reads the text from stdin and writes the output file\n");
  154.   fprintf(stderr, "to stdout.\n");
  155.   fprintf(stderr, "\n");
  156.   exit(1);
  157. }
  158.