home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
net
/
mailmerge
/
mmerge.l
< prev
next >
Wrap
Text File
|
1987-04-02
|
3KB
|
115 lines
%{
/*
** MMERGE
** A quick and dirty "mail merge" program.
**
** Sloppy programming: we let exit() close all open files for us.
*/
#include <stdio.h>
#ifndef lint
static char RCS[] = "$Header: mmerge.c,v 1.1 87/04/02 10:35:34 rs REL $";
#endif /* lint */
#define LINESIZE 99
#define MARK '$'
#define NIL (char *)NULL
static int Lines;
static char LINES[] = "LINES: ";
static char **Text;
extern int errno;
extern int sys_nerr;
extern char *sys_errlist[];
extern char *calloc();
extern char *index();
static void
Yelp(a, b)
char *a;
char *b;
{
int e;
e = errno;
fprintf(stderr, "mmerge: ");
fprintf(stderr, a, b);
fprintf(stderr, " (errno = %d, %s).\n",
e, e < 0 || e > sys_nerr ? "oops" : sys_errlist[errno]);
exit(1);
}
main(ac, av)
int ac;
char *av[];
{
register FILE *Input;
register char *p;
register int i;
char buff[BUFSIZ];
/* Check usage. */
if (ac != 3)
Yelp("Bad usage", NIL);
/* Open files. */
if ((yyin = strcmp(av[1], "-") ? fopen(av[1], "r") : stdin) == NULL)
Yelp("Can't open %s as template file", av[1]);
if ((Input = strcmp(av[2], "-") ? fopen(av[2], "r") : stdin) == NULL)
Yelp("Can't open %s as input file", av[2]);
if (yyin == stdin && Input == stdin)
Yelp("Both files can't be from standard input", NIL);
/* First line of Input should be "LINES: #"; second should be blank. */
if (fgets(buff, sizeof buff, Input) == NULL
|| strncmp(buff, LINES, sizeof LINES - 1) != 0
|| (Lines = atoi(buff + sizeof LINES - 1)) == 0
|| fgets(buff, sizeof buff, Input) == NULL
|| buff[0] != '\n')
Yelp("Input file has bad format!", NIL);
/* Get space for each field. */
if ((Text = (char **)calloc((unsigned int)Lines+1, sizeof(char *))) == NULL)
Yelp("calloc failure #1", NIL);
for (i = Lines + 1; --i >= 0; )
if ((Text[i] = (char *)calloc(LINESIZE, 1)) == NULL)
Yelp("calloc failure #2", NIL);
while (!feof(Input)) {
/* Read a field array from the list file. */
for (i = 0; i <= Lines; i++) {
if (fgets(Text[i], LINESIZE, Input) == NULL) {
if (i)
Yelp("Premature end of input?", NIL);
exit(0);
}
if (p = index(Text[i], '\n'))
*p = '\0';
}
/* Read the template file, doing fill-ins as directed. */
(void)rewind(yyin);
yylex();
}
exit(0);
}
%}
%%
\$\$[0-9]+\$ {
int i;
i = atoi(&yytext[2]) - 1;
printf("%s", i >= 0 && i < Lines ? Text[i] : yytext);
}
. ECHO;
%%