home *** CD-ROM | disk | FTP | other *** search
-
- BEGIN {
- # recall variable L has been setup.
- # first, we swallow the file L into the (integer indexed) array
- # "let_lines". The variable "line" contains the number of
- # lines in this file, at the end.
- line = 0;
- do {
- eof = getline s < L;
- if (eof > 0) {
- line++;
- let_lines[line] = s;
- }
- } while (eof > 0);
- if (line == 0) {
- print "mailmerge.awk: File " L " does not exist." > "/dev/tty";
- goch = 1; exit 1;
- }
- }
-
- {taken = 0}
- # "This line has not yet been dealt with.
-
- (NF == 0) {taken = 1}
- # Ignore blank lines (i.e., those with no fields).
-
- # Now catch the LAYOUT line:
- /^#LAYOUT/ {
- taken = 1;
- if (NF == 1) {
- print "mailmerge.awk: LAYOUT must contain some fields!" > "/dev/tty";
- goch = 1; exit 1;
- }
- for (i=2; i<=NF; i++) position[$i] = i-1;
- fnum = NF-1;
- # just gobble layout into "position" associative array.
- gotlayout = 1;
- }
-
- # Finally, if you have a line with taken == 0, it's a normal DB line.
- (taken == 0) {
- if (gotlayout == 0) {
- print "mailmerge.awk: Database file must contain a LAYOUT line before any real data." > "/dev/tty";
- goch = 1; exit 1;
- }
-
- print "";
- print "Input line = " substr($0, 1, 50) " ...";
-
- n = split($0, words, "|");
- if (n != fnum) {
- print "mailmerge.awk: Error on Line " NR " of database" > "/dev/tty";
- print " (Expect " fnum " fields, got " n ")" > "/dev/tty";
- }
-
- # Now we're all set to generate code.
- # First generate filename.
- ofn = "mm_" L "." (NR-1);
- print "Generating file named " ofn;
-
- # run over lines of letter, doing find-replace.
- for (i=1; i<=line; i++) {
- s = let_lines[i];
- for (v in position)
- if (0 != index(s, v)) # only try replace if it matches
- gsub(v, words[position[v]], s);
- print s > ofn;
- }
- }
-
- END {
- if (goch == 1) exit 1;
- }
-