+ case Word_String:
+ *dst++ = 'w';
+ break;
+ case Word_Phrase:
+ *dst++ = 'p';
+ break;
+ case Word_Numeric:
+ *dst++ = 'n';
+ }
+ for (i = 0; p->u.string[i] && i < IT_MAX_WORD-3; i++)
+ *dst++ = p->u.string[i];
+ *dst++ = '\0';
+
+ if (!diff)
+ {
+ memcpy (dst, &p->seqno, sizeof(p->seqno));
+ dst += sizeof(p->seqno);
+ }
+ reckeys.buf_used = dst - reckeys.buf;
+}
+
+static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
+ const char *databaseName)
+{
+ char attrSet = -1;
+ short attrUse = -1;
+ int seqno = 0;
+ int off = 0;
+
+ if (zebTargetInfo_curDatabase (zti, databaseName))
+ {
+ if (zebTargetInfo_newDatabase (zti, databaseName))
+ abort ();
+ }
+ while (off < reckeys->buf_used)
+ {
+ const char *src = reckeys->buf + off;
+ struct it_key key;
+ int lead, ch;
+
+ lead = *src++;
+
+ if (!(lead & 1))
+ {
+ memcpy (&attrSet, src, sizeof(attrSet));
+ src += sizeof(attrSet);
+ }
+ if (!(lead & 2))
+ {
+ memcpy (&attrUse, src, sizeof(attrUse));
+ src += sizeof(attrUse);
+ }
+ if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
+ key_flush ();
+ ++ptr_i;
+ key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
+
+ ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
+ if (ch < 0)
+ ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
+ assert (ch > 0);
+ ((char*) key_buf) [key_buf_used++] = ch;
+ while (*src)
+ ((char*)key_buf) [key_buf_used++] = *src++;
+ src++;
+ ((char*)key_buf) [key_buf_used++] = '\0';
+ ((char*) key_buf)[key_buf_used++] = cmd;
+
+ if (lead & 60)
+ seqno += ((lead>>2) & 15)-1;
+ else
+ {
+ memcpy (&seqno, src, sizeof(seqno));
+ src += sizeof(seqno);
+ }
+ key.seqno = seqno;
+ key.sysno = sysno;
+ memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
+ key_buf_used += sizeof(key);
+ off = src - reckeys->buf;
+ }
+ assert (off == reckeys->buf_used);
+}
+
+static const char **searchRecordKey (struct recKeys *reckeys,
+ int attrSetS, int attrUseS)
+{
+ static const char *ws[32];
+ int off = 0;
+ int startSeq = -1;
+ int i;
+
+ for (i = 0; i<32; i++)
+ ws[i] = NULL;
+
+ while (off < reckeys->buf_used)
+ {
+ const char *src = reckeys->buf + off;
+ char attrSet;
+ short attrUse;
+ int seqno;
+ const char *wstart;
+
+ memcpy (&attrSet, src, sizeof(attrSet));
+ src += sizeof(attrSet);
+
+ memcpy (&attrUse, src, sizeof(attrUse));
+ src += sizeof(attrUse);
+
+ wstart = src;
+ while (*src++)
+ ;
+
+ memcpy (&seqno, src, sizeof(seqno));
+ src += sizeof(seqno);
+
+#if 0
+ logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
+#endif
+ if (attrUseS == attrUse && attrSetS == attrSet)
+ {
+ int woff;
+
+
+ if (startSeq == -1)
+ startSeq = seqno;
+ woff = seqno - startSeq;
+ if (woff >= 0 && woff < 31)
+ ws[woff] = wstart;
+ }
+
+ off = src - reckeys->buf;
+ }
+ assert (off == reckeys->buf_used);
+ return ws;
+}
+
+struct file_read_info {
+ off_t file_max;
+ off_t file_offset;
+ off_t file_moffset;
+ int file_more;
+ int fd;
+};
+
+static struct file_read_info *file_read_start (int fd)
+{
+ struct file_read_info *fi = xmalloc (sizeof(*fi));
+
+ fi->fd = fd;
+ fi->file_max = 0;
+ fi->file_moffset = 0;
+ return fi;
+}
+
+static void file_read_stop (struct file_read_info *fi)
+{
+ assert (fi);
+ xfree (fi);
+}
+
+static off_t file_seek (void *handle, off_t offset)
+{
+ struct file_read_info *p = handle;
+ p->file_offset = offset;
+ return lseek (p->fd, offset, SEEK_SET);
+}
+
+static int file_read (void *handle, char *buf, size_t count)
+{
+ struct file_read_info *p = handle;
+ int fd = p->fd;
+ int r;
+ r = read (fd, buf, count);
+ if (r > 0)
+ {
+ p->file_offset += r;
+ if (p->file_offset > p->file_max)
+ p->file_max = p->file_offset;
+ }
+ return r;
+}
+
+static void file_begin (void *handle)
+{
+ struct file_read_info *p = handle;
+
+ p->file_offset = p->file_moffset;
+ if (p->file_moffset)
+ lseek (p->fd, p->file_moffset, SEEK_SET);
+ p->file_more = 0;
+}
+
+static void file_end (void *handle, off_t offset)
+{
+ struct file_read_info *p = handle;
+
+ assert (p->file_more == 0);
+ p->file_more = 1;
+ p->file_moffset = offset;
+}
+
+static int atois (const char **s)
+{
+ int val = 0, c;
+ while ( (c=**s) >= '0' && c <= '9')
+ {
+ val = val*10 + c - '0';
+ ++(*s);
+ }
+ return val;
+}
+
+static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
+ const char *fname,
+ const char *spec)
+{
+ static char dstBuf[2048];
+ char *dst = dstBuf;
+ const char *s = spec;
+ static const char **w;
+ int i;
+
+ while (1)
+ {
+ while (*s == ' ' || *s == '\t')
+ s++;
+ if (!*s)
+ break;
+ if (*s == '(')
+ {
+ char matchFlag[32];
+ int attrSet, attrUse;
+ int first = 1;
+
+ s++;
+ attrSet = atois (&s);
+ if (*s != ',')
+ {
+ logf (LOG_WARN, "Missing , in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ s++;
+ attrUse = atois (&s);
+ w = searchRecordKey (reckeys, attrSet, attrUse);
+ assert (w);
+
+ if (*s == ')')
+ {
+ for (i = 0; i<32; i++)
+ matchFlag[i] = 1;
+ }
+ else
+ {
+ logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ s++;
+
+ for (i = 0; i<32; i++)
+ if (matchFlag[i] && w[i])
+ {
+ if (first)
+ {
+ *dst++ = ' ';
+ first = 0;
+ }
+ strcpy (dst, w[i]);
+ dst += strlen(w[i]);
+ }
+ if (first)
+ {
+ logf (LOG_WARN, "Record didn't contain match"
+ " fields in (%d,%d)", attrSet, attrUse);
+ return NULL;
+ }
+ }
+ else if (*s == '$')
+ {
+ int spec_len;
+ char special[64];
+ const char *spec_src = NULL;
+ const char *s1 = ++s;
+ while (*s1 && *s1 != ' ' && *s1 != '\t')
+ s1++;
+
+ spec_len = s1 - s;
+ if (spec_len > 63)
+ spec_len = 63;
+ memcpy (special, s, spec_len);
+ special[spec_len] = '\0';
+ s = s1;
+
+ if (!strcmp (special, "group"))
+ spec_src = rGroup->groupName;
+ else if (!strcmp (special, "database"))
+ spec_src = rGroup->databaseName;
+ else if (!strcmp (special, "filename"))
+ spec_src = fname;
+ else if (!strcmp (special, "type"))
+ spec_src = rGroup->recordType;
+ else
+ spec_src = NULL;
+ if (spec_src)
+ {
+ strcpy (dst, spec_src);
+ dst += strlen (spec_src);
+ }
+ }
+ else if (*s == '\"' || *s == '\'')
+ {
+ int stopMarker = *s++;
+ char tmpString[64];
+ int i = 0;
+
+ while (*s && *s != stopMarker)
+ {
+ if (i < 63)
+ tmpString[i++] = *s++;
+ }
+ if (*s)
+ s++;
+ tmpString[i] = '\0';
+ strcpy (dst, tmpString);
+ dst += strlen (tmpString);
+ }
+ else
+ {
+ logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ *dst++ = 1;
+ }
+ if (dst == dstBuf)
+ {
+ logf (LOG_WARN, "No match criteria for record %s in group %s",
+ fname, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ return dstBuf;
+}
+
+struct recordLogInfo {
+ const char *fname;
+ char *op;
+ struct recordGroup *rGroup;
+};
+
+static void recordLogPreamble (int level, const char *msg, void *info)
+{
+ struct recordLogInfo *p = info;
+ FILE *outf = log_file ();
+
+ if (level & LOG_LOG)