home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 183_01 / letter.c < prev    next >
C/C++ Source or Header  |  1986-02-04  |  21KB  |  817 lines

  1. /*
  2.  *  letter      A short, savage, document processor designed to serve one
  3.  *              function; format a one page letter.
  4.  *
  5.  *              I don't know about anybody else, but I write about 25 one-
  6.  *              pagers a week and I like my letters to look pretty. This
  7.  *              requires vertical centering and I've not found a word
  8.  *              processor yet that can do it (although qnxs' document
  9.  *              processor 'doc' did provide sufficient primitives to
  10.  *              write a REALLY complex macro that did and UNIXs' nroff and
  11.  *              a whole bunch of shell commands could do the job as well).
  12.  *
  13.  *              letter will vertically center a one page letter, will
  14.  *              divert the address to a file (suitable for printing on
  15.  *              an envelope by address) and support a small set of document
  16.  *              processing functions. It is weak on error checking and
  17.  *              semantic analysis (so don't push it!). See the separate
  18.  *              document 'letter.doc' for more detail.
  19.  *
  20.  *              This program took less than six hours to write. It is
  21.  *              probably far from perfect. I'm not offering this as a
  22.  *              replacement for nroff (or even WordStar), but it does
  23.  *              what I need acceptably.
  24.  *
  25.  *              This is thrown freely into the public domain and can be
  26.  *              used, modified, and distributed by any person or agency
  27.  *              that wants to. I would appreciate it if the original
  28.  *              author credit remains and would also request that no
  29.  *              compensation be charged by anyone passing this turkey
  30.  *              around.
  31.  *
  32.  *      This program was written by Jon Simkins and tested using the DeSmet C
  33.  *      compiler and MS-DOS 3.0.
  34.  *
  35.  *  Revision Notes
  36.  *
  37.  *      Date        Revision
  38.  *      --------------------
  39.  *      86.02.04    Added UL_ON and BF_ON control strings for printers that
  40.  *                  directly support underlining and boldface (most if them
  41.  *                  these days).
  42.  *                  Changed setDate() function for Wizard C and recompiled. js.
  43.  *      
  44.  *      Copyright (c) 1985 SoftFocus
  45.  *                         1343 Stanbury Dr.,
  46.  *                         Oakville, Ontario, Canada
  47.  *                         L6L 2J5
  48.  *                         (416)825-0903
  49.  *
  50.  *                      Compuserve: 76566, 1172
  51.  */
  52. #include <stdio.h>
  53. #include <bdos.h>      /* for getdate() structure                     */
  54.  
  55. #ifndef TRUE
  56. #define TRUE 1
  57. #define FALSE 0
  58. #endif
  59.  
  60. #define MAXLINE 200     /* maximum input line. Assumed larger than     */
  61.                         /* maximum print line.                         */
  62. #define LPP     66      /* maximum lines per page.                     */
  63. #define ERROR -1
  64. #define HEAD 6       /* lines left at top of page when paper is loaded */
  65.  
  66. /*
  67.  *  program defaults and printer controls.
  68.  *  If your printer cannot backspace one character, don't use underline or
  69.  *  boldface.
  70.  */
  71. #define DEFIN           0       /* default indent                          */
  72. #define DEFRM           65      /* default right margin                    */
  73. #define DEFPO           5      /* default page offset                     */
  74. #define DEFJU           FALSE   /* justification off                       */
  75. #define DEFFI           TRUE    /* fill on                                 */
  76.  
  77. #define FF              "\014"  /* string to formfeed the printer          */
  78. #define BACKSPACE       8       /* character to backspace the printer      */
  79. /*
  80.  *  Printer Control Strings
  81.  *  -----------------------
  82.  *  If your printer supports such niceties as automatic underlining and
  83.  *  boldface, then fill in the following defines in the logical manner.
  84.  *  If they are left undefined, then letter will underline by backspacing
  85.  *  and printing a '_' and boldface by doublestriking.
  86.  *  The given examples are for a Brother HR-35 daisywheel. This is disabled
  87.  *  in the distribution version.
  88.  */
  89. /* #define UL_ON    "\033E" *//* enable auto underline                  */
  90. /* #define UL_OFF   "\033R" *//* disable auto underline                 */
  91. /* #define BF_ON    "\033O" *//* enable auto boldface                   */
  92. /* #define BF_OFF   "\033&" *//* disable auto boldface                  */
  93.  
  94. FILE *fd;               /* main input file */
  95. FILE *fdAdd;            /* address file    */
  96. FILE *printer;          /* printer device  */
  97. int hardCopy;           /* true if printed */
  98. #define PRINTER "LPT1"  /* hardcopy output */
  99. #define CONSOLE "CON"   /* console output  */
  100.  
  101. /*
  102.  *  letter array, line buffer, and counters
  103.  */                  
  104. char *lines[LPP];           /* build the page here                      */
  105. int lineCount;
  106. char pLine[MAXLINE];        /* build the current line here              */
  107. int pCount;                                                              
  108. int saveJu, saveFi;         
  109.  
  110. /*
  111.  *  command array & defines
  112.  */
  113. #define COMCOUNT 13
  114. char *commands[] = {". ", "in", "po", "ju", "rm", "ti", "br",
  115.                     "as", "ae", "sp", "dt", "sg", "ce", "fi"};
  116. #define COMMENT     0
  117. #define INDENT      1
  118. #define PAGEOFF     2
  119. #define JUSTIFY     3
  120. #define RM          4
  121. #define TI          5
  122. #define BR          6
  123. #define AS          7
  124. #define AE          8
  125. #define SP          9
  126. #define DT          10
  127. #define SG          11
  128. #define CENTER      12
  129. #define FILL        13
  130.  
  131. /*
  132.  *  margins, switches.
  133.  *  There is currently no way to disable the interpretation of the characters
  134.  *  listed below.
  135.  */
  136. #define BC '^'      /* boldface toggle  */
  137. #define UC '_'      /* underline toggle */
  138. #define HS '~'      /* 'hard' space (not spread by justification)          */
  139.  
  140. int rm, in, ti, ju, po, fi, br, sp, ce;
  141. int addFlag;                    /* whether address redirection in effect   */
  142. int ul, bf;                     /* underline, boldface currently selected  */
  143.  
  144.  
  145. /*
  146.  *  off and away
  147.  */
  148. main(argc, argv)
  149. int argc;
  150. char *argv[];
  151. {
  152.     char buff[MAXLINE], word[MAXLINE];
  153.     int index, len;
  154.     char *pr;
  155.     register j;
  156.  
  157.     /*
  158.      *  check argument count
  159.      */
  160.     if (argc > 3 || argc < 2)
  161.         erExit("usage: letter [-p] <fName>");
  162.  
  163.     /*
  164.      *  get command line option(s)
  165.      */
  166.     j=1;
  167.     if (argv[j][0] == '-' && argv[j][1] == 'p') {
  168.         pr = PRINTER;
  169.         hardCopy = TRUE;
  170.         j++;
  171.     }
  172.     else {
  173.         pr = CONSOLE;
  174.         hardCopy = FALSE;
  175.     }
  176.  
  177.     /*
  178.      *  open input file
  179.      */
  180.     if ( (fd=fopen(argv[j], "r")) == 0) {
  181.         printf("Could not open '%s'\n", argv[j]);
  182.         exit(ERROR);
  183.     }
  184.  
  185.     /*
  186.      *  set defaults
  187.      */
  188.     ul = bf = addFlag = FALSE;
  189.     in = DEFIN;
  190.     rm = DEFRM;
  191.     ju = DEFJU;
  192.     fi = DEFFI;
  193.     if (hardCopy)
  194.         po = DEFPO;
  195.     else
  196.         po = 0;
  197.  
  198.     ti = ce = lineCount = 0;
  199.  
  200.     /*
  201.      *  input/process loop
  202.      */
  203.     pCount = 0;
  204.     pLine[0] = '\0';
  205.     while( fgets(buff, MAXLINE, fd) != NULL) {
  206.         trim(buff);
  207.         /*
  208.          *  blank line?
  209.          */
  210.         if (buff[0] == '\0') {
  211.             pFlush(FALSE);
  212.             pCount=1;
  213.             pFlush(FALSE);
  214.             continue;
  215.         }
  216.  
  217.         /*
  218.          *  check for any command lines
  219.          */
  220.         if (buff[0] == '.') {
  221.             dotComm(buff);
  222.             continue;
  223.         }
  224.  
  225.         /*
  226.          *  if fill mode is off, just pass the line on through.
  227.          */
  228.         if ( !(fi)) {
  229.             strcpy(pLine, buff);
  230.             pCount = lenCal(pLine);
  231.             pFlush(ju);
  232.         }
  233.         else {
  234.         /*
  235.          *  must be text; add it (word at a time) to the current print
  236.          *  line.
  237.          */
  238.             index=0;
  239.             do { 
  240.                 index = getWord(index, buff, word);
  241.                 if (index == 0)
  242.                     continue;
  243.                 len = lenCal(word);
  244.                 /*
  245.                  *  if there's still room, add word to current line
  246.                  */
  247.