+ if (p->more_l && p->more_r)
+ cmp = (*kctrl->cmp)(p->buf_l, p->buf_r);
+ else if (p->more_l)
+ cmp = -rfd->rset->scope;
+ else
+ cmp = rfd->rset->scope;
+#if RSET_DEBUG
+ logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
+ rfd, p->more_l, p->more_r, cmp, p->tail);
+ (*kctrl->log_item)(LOG_DEBUG, p->buf_l, "left ");
+ (*kctrl->log_item)(LOG_DEBUG, p->buf_r, "right ");
+#endif
+ if (!cmp)
+ { /* cmp==0 */
+ memcpy (buf, p->buf_l, kctrl->key_size);
+ if (term)
+ *term=p->term_l;
+ p->more_l = rset_read (p->rfd_l, p->buf_l, &p->term_l);
+ p->tail = 1;
+ }
+ else if ( (cmp>0) && (cmp<rfd->rset->scope))
+ { /* typically cmp == 1 */
+ memcpy (buf, p->buf_r, kctrl->key_size);
+ if (term)
+ *term=p->term_r;
+ p->more_r = rset_read (p->rfd_r, p->buf_r, &p->term_r);
+ p->tail = 1;
+#if RSET_DEBUG
+ logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
+ rfd, p->more_l, p->more_r, cmp);
+ key_logdump(LOG_DEBUG,buf);
+ (*kctrl->log_item)(LOG_DEBUG, buf, "");
+#endif
+ p->hits++;
+ return 1;
+ }
+ else if ( (cmp<0) && (-cmp<rfd->rset->scope))
+ { /* cmp == -1 */
+ memcpy (buf, p->buf_l, kctrl->key_size);
+ if (term)
+ *term=p->term_l;
+ p->more_l = rset_read (p->rfd_l, p->buf_l,&p->term_l);
+ p->tail = 1;
+#if RSET_DEBUG
+ logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
+ rfd, p->more_l, p->more_r, cmp);
+ (*kctrl->log_item)(LOG_DEBUG, buf, "");
+#endif
+ p->hits++;
+ return 1;
+ }
+ else if (cmp >= rfd->rset->scope )
+ { /* cmp == 2 */
+ if (p->tail)
+ {
+ memcpy (buf, p->buf_r, kctrl->key_size);
+ if (term)
+ *term=p->term_r;
+ p->more_r = rset_read (p->rfd_r, p->buf_r, &p->term_r);
+ if (!p->more_r || (*kctrl->cmp)(p->buf_r, buf) > 1)
+ p->tail = 0;
+#if RSET_DEBUG
+ logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
+ rfd, p->more_l, p->more_r, cmp);
+ (*kctrl->log_item)(LOG_DEBUG, buf, "");
+#endif
+ p->hits++;
+ return 1;
+ }
+ else
+ {
+#if RSET_DEBUG
+ logf (LOG_DEBUG, "r_read_and [%p] about to forward R "
+ "m=%d/%d c=%d",
+ rfd, p->more_l, p->more_r, cmp);
+#endif
+ if (p->more_r && p->more_l)
+ p->more_r = rset_forward( p->rfd_r, p->buf_r,
+ &p->term_r, p->buf_l);
+ else
+ return 0; /* no point in reading further */
+ }
+ }
+ else
+ { /* cmp == -2 */
+ if (p->tail)
+ {
+ memcpy (buf, p->buf_l, kctrl->key_size);
+ if (term)
+ *term=p->term_l;
+ p->more_l = rset_read (p->rfd_l, p->buf_l, &p->term_l);
+ if (!p->more_l || (*kctrl->cmp)(p->buf_l, buf) > 1)
+ p->tail = 0;
+#if RSET_DEBUG
+ logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
+ rfd, p->more_l, p->more_r, cmp);
+ (*kctrl->log_item)(LOG_DEBUG, buf, "");
+#endif
+ p->hits++;
+ return 1;
+ }
+ else
+ {
+#if RSET_DEBUG
+ logf (LOG_DEBUG, "r_read_and [%p] about to forward L "
+ "m=%d/%d c=%d",
+ rfd, p->more_l, p->more_r, cmp);
+#endif
+ if (p->more_r && p->more_l)
+ p->more_l = rset_forward(p->rfd_l, p->buf_l,
+ &p->term_l, p->buf_r);
+ else
+ return 0; /* no point in reading further */
+ }
+ }
+ }
+#if RSET_DEBUG
+ logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
+#endif
+ return 0;