#include "config.h"
#include <sys/types.h>
#include <sys/queue.h>
#include <bitstring.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../common/common.h"
int
ex_copy(SCR *sp, EXCMD *cmdp)
{
CB cb;
MARK fm1, fm2, m, tm;
recno_t cnt;
int rval;
rval = 0;
NEEDFILE(sp, cmdp);
fm1 = cmdp->addr1;
fm2 = cmdp->addr2;
memset(&cb, 0, sizeof(cb));
TAILQ_INIT(&cb.textq);
for (cnt = fm1.lno; cnt <= fm2.lno; ++cnt)
if (cut_line(sp, cnt, 0, CUT_LINE_TO_EOL, &cb)) {
rval = 1;
goto err;
}
cb.flags |= CB_LMODE;
tm.lno = cmdp->lineno;
tm.cno = 0;
if (put(sp, &cb, NULL, &tm, &m, 1, 1))
rval = 1;
else {
cnt = (fm2.lno - fm1.lno) + 1;
sp->lno = m.lno + (cnt - 1);
sp->cno = 0;
}
err: text_lfree(&cb.textq);
return (rval);
}
int
ex_move(SCR *sp, EXCMD *cmdp)
{
LMARK *lmp;
MARK fm1, fm2;
recno_t cnt, diff, fl, tl, mfl, mtl;
size_t blen, len;
int mark_reset;
char *bp, *p;
NEEDFILE(sp, cmdp);
fm1 = cmdp->addr1;
fm2 = cmdp->addr2;
if (cmdp->lineno >= fm1.lno && cmdp->lineno <= fm2.lno) {
msgq(sp, M_ERR, "Destination line is inside move range");
return (1);
}
fl = fm1.lno;
tl = cmdp->lineno;
mark_reset = 0;
LIST_FOREACH(lmp, &sp->ep->marks, q)
if (lmp->name != ABSMARK1 &&
lmp->lno >= fl && lmp->lno <= tl) {
mark_reset = 1;
F_CLR(lmp, MARK_USERSET);
(void)log_mark(sp, lmp);
}
GET_SPACE_RET(sp, bp, blen, 256);
diff = (fm2.lno - fm1.lno) + 1;
if (tl > fl) {
mfl = tl - diff;
mtl = tl;
for (cnt = diff; cnt--;) {
if (db_get(sp, fl, DBG_FATAL, &p, &len))
return (1);
BINC_RET(sp, bp, blen, len);
memcpy(bp, p, len);
if (db_append(sp, 1, tl, bp, len))
return (1);
if (mark_reset)
LIST_FOREACH(lmp, &sp->ep->marks, q)
if (lmp->name != ABSMARK1 &&
lmp->lno == fl)
lmp->lno = tl + 1;
if (db_delete(sp, fl))
return (1);
}
} else {
mfl = tl;
mtl = tl + diff;
for (cnt = diff; cnt--;) {
if (db_get(sp, fl, DBG_FATAL, &p, &len))
return (1);
BINC_RET(sp, bp, blen, len);
memcpy(bp, p, len);
if (db_append(sp, 1, tl++, bp, len))
return (1);
if (mark_reset)
LIST_FOREACH(lmp, &sp->ep->marks, q)
if (lmp->name != ABSMARK1 &&
lmp->lno == fl)
lmp->lno = tl;
++fl;
if (db_delete(sp, fl))
return (1);
}
}
FREE_SPACE(sp, bp, blen);
sp->lno = tl;
sp->cno = 0;
if (mark_reset)
LIST_FOREACH(lmp, &sp->ep->marks, q)
if (lmp->name != ABSMARK1 &&
lmp->lno >= mfl && lmp->lno <= mtl)
(void)log_mark(sp, lmp);
sp->rptlines[L_MOVED] += diff;
return (0);
}