home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / net / mailmerge / mmerge.l < prev    next >
Text File  |  1987-04-02  |  3KB  |  115 lines

  1.  
  2. %{
  3. /*
  4. **  MMERGE
  5. **  A quick and dirty "mail merge" program.
  6. **
  7. **  Sloppy programming:  we let exit() close all open files for us.
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #ifndef    lint
  13. static char     RCS[] = "$Header: mmerge.c,v 1.1 87/04/02 10:35:34 rs REL $";
  14. #endif    /* lint */
  15.  
  16. #define LINESIZE    99
  17. #define MARK        '$'
  18. #define NIL        (char *)NULL
  19.  
  20. static int      Lines;
  21. static char      LINES[] = "LINES: ";
  22. static char    **Text;
  23.  
  24. extern int     errno;
  25. extern int     sys_nerr;
  26. extern char    *sys_errlist[];
  27. extern char    *calloc();
  28. extern char    *index();
  29.  
  30.  
  31. static void
  32. Yelp(a, b)
  33.     char    *a;
  34.     char    *b;
  35. {
  36.     int         e;
  37.  
  38.     e = errno;
  39.     fprintf(stderr, "mmerge:  ");
  40.     fprintf(stderr, a, b);
  41.     fprintf(stderr, " (errno = %d, %s).\n",
  42.         e, e < 0 || e > sys_nerr ? "oops" : sys_errlist[errno]);
  43.     exit(1);
  44. }
  45.  
  46.  
  47. main(ac, av)
  48.     int              ac;
  49.     char         *av[];
  50. {
  51.     register FILE     *Input;
  52.     register char     *p;
  53.     register int      i;
  54.     char          buff[BUFSIZ];
  55.  
  56.     /* Check usage. */
  57.     if (ac != 3)
  58.     Yelp("Bad usage", NIL);
  59.  
  60.     /* Open files. */
  61.     if ((yyin = strcmp(av[1], "-") ? fopen(av[1], "r") : stdin) == NULL)
  62.     Yelp("Can't open %s as template file", av[1]);
  63.     if ((Input = strcmp(av[2], "-") ? fopen(av[2], "r") : stdin) == NULL)
  64.     Yelp("Can't open %s as input file", av[2]);
  65.     if (yyin == stdin && Input == stdin)
  66.     Yelp("Both files can't be from standard input", NIL);
  67.  
  68.     /* First line of Input should be "LINES: #"; second should be blank. */
  69.     if (fgets(buff, sizeof buff, Input) == NULL
  70.      || strncmp(buff, LINES, sizeof LINES - 1) != 0
  71.      || (Lines = atoi(buff + sizeof LINES - 1)) == 0
  72.      || fgets(buff, sizeof buff, Input) == NULL
  73.      || buff[0] != '\n')
  74.     Yelp("Input file has bad format!", NIL);
  75.  
  76.     /* Get space for each field. */
  77.     if ((Text = (char **)calloc((unsigned int)Lines+1, sizeof(char *))) == NULL)
  78.     Yelp("calloc failure #1", NIL);
  79.     for (i = Lines + 1; --i >= 0; )
  80.     if ((Text[i] = (char *)calloc(LINESIZE, 1)) == NULL)
  81.         Yelp("calloc failure #2", NIL);
  82.  
  83.     while (!feof(Input)) {
  84.     /* Read a field array from the list file. */
  85.     for (i = 0; i <= Lines; i++) {
  86.         if (fgets(Text[i], LINESIZE, Input) == NULL) {
  87.         if (i)
  88.             Yelp("Premature end of input?", NIL);
  89.         exit(0);
  90.         }
  91.         if (p = index(Text[i], '\n'))
  92.         *p = '\0';
  93.     }
  94.  
  95.     /* Read the template file, doing fill-ins as directed. */
  96.     (void)rewind(yyin);
  97.     yylex();
  98.     }
  99.  
  100.     exit(0);
  101. }
  102. %}
  103.  
  104. %%
  105.  
  106. \$\$[0-9]+\$    {
  107.             int     i;
  108.  
  109.             i = atoi(&yytext[2]) - 1;
  110.             printf("%s", i >= 0 && i < Lines ? Text[i] : yytext);
  111.         }
  112. .        ECHO;
  113.  
  114. %%
  115.