home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume4 / fmtr / fmtr.c < prev    next >
C/C++ Source or Header  |  1986-11-30  |  3KB  |  158 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: fmtr.c,v 1.4 86/05/05 14:09:41 root Exp $";
  3. #endif
  4.  
  5. /*  fmtr, a formatter for roff source text respecting no fill mode.
  6.  *  The code is derived from a translation to C of Kernighan and
  7.  *  Plaugher's format program.
  8.  */
  9.  
  10. #include "fmtr.h"
  11.  
  12. int optind, opterr;
  13. char *optarg, *progname;        /* program name */
  14.  
  15. main(argc, argv)
  16. int argc;
  17. char *argv[];
  18. {
  19.     int i, c;
  20.     char *sarray, *earray, *getenv();
  21.     FILE *efopen(), *fp;
  22.  
  23.     progname = argv[0];
  24.     outp = outbuf;
  25.     sarray = earray = (char *) NULL;
  26.     llength = 72;    /* default */
  27.  
  28.     sarray = getenv("FMTR_S");
  29.     check(sarray, "environmental variable FMTR_S");
  30.     earray = getenv("FMTR_E");
  31.     check(earray, "environmental variable FMTR_E");
  32.  
  33.     mk_table(sarray, earray);    /* make table of commands from env var */
  34.                 /* we call mk_table whether the arrays */
  35.                 /* null or not */
  36.     sarray = earray = (char *) 0;
  37.  
  38.     while ((c = getopt(argc, argv, "zw:s:e:")) != EOF)
  39.     switch (c) {
  40.         case 'z':
  41.         z_flag = 1;
  42.         break;
  43.         case 'w':
  44.         llength = atoi(optarg);
  45.         if (llength <= 0 || llength >= BUFSIZ - 2) {
  46.             fprintf(stderr,"%s: bad line length: %s\n", progname, optarg);
  47.             exit (1);
  48.         }
  49.         break;
  50.         case 's':
  51.         sarray = optarg;
  52.         check(sarray, "-s flag");
  53.         break;
  54.         case 'e':
  55.         earray = optarg;
  56.         check(earray, "-e flag");
  57.         break;
  58.         case '?':
  59.         usage();
  60.         exit(1);
  61.     }
  62.  
  63.     mk_table(sarray, earray);    /* make table of commands from flags */
  64.                 /* again, always call */
  65.  
  66.     argc -= optind;
  67.     argv += optind;
  68.     
  69.     fp = stdin;
  70.     i = 0;
  71.     do {
  72.     if (argc > 0)
  73.         fp = efopen(*argv, "r");
  74.     do_fmt(fp);        /* does real work */
  75.     argv++;
  76.     } while (++i < argc);
  77.  
  78.     n_brk();
  79. }
  80.  
  81. check(string, origin)    /* check format of string of macros */
  82. char *string, *origin;
  83. {
  84.     if (string == NULL)
  85.     return;
  86.     if ((string[0] != '.') || strlen(string) % 3 != 0) {
  87.     fprintf(stderr,
  88.     "%s: list of macros '%s' supplied with %s is not in correct format\n",
  89.     progname, string, origin);
  90.     exit(1);
  91.     }
  92. }
  93.  
  94. /*  do_fmt is where work is done, or rather assigned.  Each line
  95.  *  read has the terminatig newline, and any trailing blanks, removed.
  96.  *  Then it is sent to either command() or text(), depending on whether
  97.  *  it appears to be a command or not.
  98.  */
  99.  
  100. do_fmt(fp)
  101. FILE *fp;
  102. {
  103.     char *cp, line[10*BUFSIZ];
  104.     int length;
  105.  
  106.     while (length = fgetsmod(line, sizeof line, fp)) {
  107.     for (cp = &line[length - 2]; *cp == ' ' && cp >= line; cp--)
  108.         ;
  109.     *++cp = '\0';
  110.         if (*line == '.' || *line == '\'' ||
  111.       (z_flag && strncmp(line, "\\&.", 3) == 0) ||
  112.       (z_flag && strncmp(line, "\\&'", 3) == 0))
  113.             command(line);
  114.         else
  115.             text(line);
  116.     }
  117. }
  118.  
  119. /*  text() checks for leading blanks or blank line, in which case causes
  120.  *  break.  Blank lines output as is, others broken into words by getword()
  121.  *  with words sent to outbuf by putword().
  122.  */
  123.  
  124. text(line)
  125. char *line;
  126. {
  127.     char *pline, wordbuf[BUFSIZ], *getword();
  128.  
  129.     if (ul_val || ce_val || nf_val) {
  130.     puts(line);
  131.     if (ul_val)
  132.         ul_val--;
  133.     if (ce_val)
  134.         ce_val--;
  135.     return;
  136.     }
  137.  
  138.     if (*line == ' ' || *line == '\0')
  139.     leadbl(line);
  140.     if (*line == '\0')
  141.     put(line);
  142.     else {        /* main case */
  143.     pline = line;
  144.     do {
  145.         pline = getword(pline, wordbuf);
  146.         if (pline)
  147.             putword(wordbuf);
  148.     } while (pline);
  149.     }
  150. }
  151.  
  152. usage()
  153. {
  154.     fprintf(stderr,
  155. "usage: %s [ -z ] [ -wn ] [ -s start_string ] [ -e end_string] [ file ... ]\n",
  156.     progname);
  157. }
  158.