/* * put -- * Put text buffer contents into the file. * * PUBLIC: int put __P((SCR *, CB *, CHAR_T *, MARK *, MARK *, int)); */ int put(SCR *sp, CB *cbp, ARG_CHAR_T *namep, MARK *cp, MARK *rp, int append) { ARG_CHAR_T name; TEXT *ltp, *tp; db_recno_t lno; size_t blen, clen, len; int rval; CHAR_T *bp, *t; CHAR_T *p; if (cbp == NULL) { if (namep == NULL) { cbp = sp->wp->dcbp; if (cbp == NULL) { msgq(sp, M_ERR, "053|The default buffer is empty"); return (1); } } else { name = *namep; CBNAME(sp, cbp, name); if (cbp == NULL) { msgq(sp, M_ERR, "054|Buffer %s is empty", KEY_NAME(sp, name)); return (1); } } } tp = cbp->textq.cqh_first; /* * It's possible to do a put into an empty file, meaning that the cut * buffer simply becomes the file. It's a special case so that we can * ignore it in general. * * !!! * Historically, pasting into a file with no lines in vi would preserve * the single blank line. This is surely a result of the fact that the * historic vi couldn't deal with a file that had no lines in it. This * implementation treats that as a bug, and does not retain the blank * line. * * Historical practice is that the cursor ends at the first character * in the file. */ if (cp->lno == 1) { if (db_last(sp, &lno)) return (1); if (lno == 0) { for (; tp != (void *)&cbp->textq; ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next) if (db_append(sp, 1, lno, tp->lb, tp->len)) return (1); rp->lno = 1; rp->cno = 0; return (0); } } /* If a line mode buffer, append each new line into the file. */ if (F_ISSET(cbp, CB_LMODE)) { lno = append ? cp->lno : cp->lno - 1; rp->lno = lno + 1; for (; tp != (void *)&cbp->textq; ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next) if (db_append(sp, 1, lno, tp->lb, tp->len)) return (1); rp->cno = 0; (void)nonblank(sp, rp->lno, &rp->cno); return (0); } /* * If buffer was cut in character mode, replace the current line with * one built from the portion of the first line to the left of the * split plus the first line in the CB. Append each intermediate line * in the CB. Append a line built from the portion of the first line * to the right of the split plus the last line in the CB. * * Get the first line. */ lno = cp->lno; if (db_get(sp, lno, DBG_FATAL, &p, &len)) return (1); GET_SPACE_RETW(sp, bp, blen, tp->len + len + 1); t = bp; /* Original line, left of the split. */ if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) { MEMCPYW(bp, p, clen); p += clen; t += clen; } /* First line from the CB. */ if (tp->len != 0) { MEMCPYW(t, tp->lb, tp->len); t += tp->len; } /* Calculate length left in the original line. */ clen = len == 0 ? 0 : len - (cp->cno + (append ? 1 : 0)); /* * !!! * In the historical 4BSD version of vi, character mode puts within * a single line have two cursor behaviors: if the put is from the * unnamed buffer, the cursor moves to the character inserted which * appears last in the file. If the put is from a named buffer, * the cursor moves to the character inserted which appears first * in the file. In System III/V, it was changed at some point and * the cursor always moves to the first character. In both versions * of vi, character mode puts that cross line boundaries leave the * cursor on the first character. Nvi implements the System III/V * behavior, and expect POSIX.2 to do so as well. */ rp->lno = lno; rp->cno = len == 0 ? 0 : sp->cno + (append && tp->len ? 1 : 0); /* * If no more lines in the CB, append the rest of the original * line and quit. Otherwise, build the last line before doing * the intermediate lines, because the line changes will lose * the cached line. */ if (tp->q.cqe_next == (void *)&cbp->textq) { if (clen > 0) { MEMCPYW(t, p, clen); t += clen; } if (db_set(sp, lno, bp, t - bp)) goto err; if (sp->rptlchange != lno) { sp->rptlchange = lno; ++sp->rptlines[L_CHANGED]; } } else { /* * Have to build both the first and last lines of the * put before doing any sets or we'll lose the cached * line. Build both the first and last lines in the * same buffer, so we don't have to have another buffer * floating around. * * Last part of original line; check for space, reset * the pointer into the buffer. */ ltp = cbp->textq.cqh_last; len = t - bp; ADD_SPACE_RETW(sp, bp, blen, ltp->len + clen); t = bp + len; /* Add in last part of the CB. */ MEMCPYW(t, ltp->lb, ltp->len); if (clen) MEMCPYW(t + ltp->len, p, clen); clen += ltp->len; /* * Now: bp points to the first character of the first * line, t points to the last character of the last * line, t - bp is the length of the first line, and * clen is the length of the last. Just figured you'd * want to know. * * Output the line replacing the original line. */ if (db_set(sp, lno, bp, t - bp)) goto err; if (sp->rptlchange != lno) { sp->rptlchange = lno; ++sp->rptlines[L_CHANGED]; } /* Output any intermediate lines in the CB. */ for (tp = tp->q.cqe_next; tp->q.cqe_next != (void *)&cbp->textq; ++lno, ++sp->rptlines[L_ADDED], tp = tp->q.cqe_next) if (db_append(sp, 1, lno, tp->lb, tp->len)) goto err; if (db_append(sp, 1, lno, t, clen)) goto err; ++sp->rptlines[L_ADDED]; } rval = 0; if (0) err: rval = 1; FREE_SPACEW(sp, bp, blen); return (rval); }
/* * shift -- * Ex shift support. */ static int shift(SCR *sp, EXCMD *cmdp, enum which rl) { db_recno_t from, to; size_t blen, len, newcol, newidx, oldcol, oldidx, sw; int curset; CHAR_T *p; CHAR_T *bp, *tbp; NEEDFILE(sp, cmdp); if (O_VAL(sp, O_SHIFTWIDTH) == 0) { msgq(sp, M_INFO, "152|shiftwidth option set to 0"); return (0); } /* Copy the lines being shifted into the unnamed buffer. */ if (cut(sp, NULL, &cmdp->addr1, &cmdp->addr2, CUT_LINEMODE)) return (1); /* * The historic version of vi permitted the user to string any number * of '>' or '<' characters together, resulting in an indent of the * appropriate levels. There's a special hack in ex_cmd() so that * cmdp->argv[0] points to the string of '>' or '<' characters. * * Q: What's the difference between the people adding features * to vi and the Girl Scouts? * A: The Girl Scouts have mint cookies and adult supervision. */ for (p = cmdp->argv[0]->bp, sw = 0; *p == '>' || *p == '<'; ++p) sw += O_VAL(sp, O_SHIFTWIDTH); GET_SPACE_RETW(sp, bp, blen, 256); curset = 0; for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) { if (db_get(sp, from, DBG_FATAL, &p, &len)) goto err; if (!len) { if (sp->lno == from) curset = 1; continue; } /* * Calculate the old indent amount and the number of * characters it used. */ for (oldidx = 0, oldcol = 0; oldidx < len; ++oldidx) if (p[oldidx] == ' ') ++oldcol; else if (p[oldidx] == '\t') oldcol += O_VAL(sp, O_TABSTOP) - oldcol % O_VAL(sp, O_TABSTOP); else break; /* Calculate the new indent amount. */ if (rl == RIGHT) newcol = oldcol + sw; else { newcol = oldcol < sw ? 0 : oldcol - sw; if (newcol == oldcol) { if (sp->lno == from) curset = 1; continue; } } /* Get a buffer that will hold the new line. */ ADD_SPACE_RETW(sp, bp, blen, newcol + len); /* * Build a new indent string and count the number of * characters it uses. */ tbp = bp; newidx = 0; if (!O_ISSET(sp, O_EXPANDTAB)) { for (; newcol >= O_VAL(sp, O_TABSTOP); ++newidx) { *tbp++ = '\t'; newcol -= O_VAL(sp, O_TABSTOP); } } for (; newcol > 0; --newcol, ++newidx) *tbp++ = ' '; /* Add the original line. */ MEMCPYW(tbp, p + oldidx, len - oldidx); /* Set the replacement line. */ if (db_set(sp, from, bp, (tbp + (len - oldidx)) - bp)) { err: FREE_SPACEW(sp, bp, blen); return (1); } /* * !!! * The shift command in historic vi had the usual bizarre * collection of cursor semantics. If called from vi, the * cursor was repositioned to the first non-blank character * of the lowest numbered line shifted. If called from ex, * the cursor was repositioned to the first non-blank of the * highest numbered line shifted. Here, if the cursor isn't * part of the set of lines that are moved, move it to the * first non-blank of the last line shifted. (This makes * ":3>>" in vi work reasonably.) If the cursor is part of * the shifted lines, it doesn't get moved at all. This * permits shifting of marked areas, i.e. ">'a." shifts the * marked area twice, something that couldn't be done with * historic vi. */ if (sp->lno == from) { curset = 1; if (newidx > oldidx) sp->cno += newidx - oldidx; else if (sp->cno >= oldidx - newidx) sp->cno -= oldidx - newidx; } } if (!curset) { sp->lno = to; sp->cno = 0; (void)nonblank(sp, to, &sp->cno); } FREE_SPACEW(sp, bp, blen); sp->rptlines[L_SHIFT] += cmdp->addr2.lno - cmdp->addr1.lno + 1; return (0); }
/* * del -- * Delete a range of text. * * PUBLIC: int del __P((SCR *, MARK *, MARK *, int)); */ int del( SCR *sp, MARK *fm, MARK *tm, int lmode) { recno_t lno; size_t blen, len, nlen, tlen; CHAR_T *bp, *p; int eof, rval; bp = NULL; /* Case 1 -- delete in line mode. */ if (lmode) { for (lno = tm->lno; lno >= fm->lno; --lno) { if (db_delete(sp, lno)) return (1); ++sp->rptlines[L_DELETED]; if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp)) break; } goto done; } /* * Case 2 -- delete to EOF. This is a special case because it's * easier to pick it off than try and find it in the other cases. */ if (db_last(sp, &lno)) return (1); if (tm->lno >= lno) { if (tm->lno == lno) { if (db_get(sp, lno, DBG_FATAL, &p, &len)) return (1); eof = tm->cno != ENTIRE_LINE && tm->cno >= len ? 1 : 0; } else eof = 1; if (eof) { for (lno = tm->lno; lno > fm->lno; --lno) { if (db_delete(sp, lno)) return (1); ++sp->rptlines[L_DELETED]; if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp)) break; } if (db_get(sp, fm->lno, DBG_FATAL, &p, &len)) return (1); GET_SPACE_RETW(sp, bp, blen, fm->cno); MEMCPY(bp, p, fm->cno); if (db_set(sp, fm->lno, bp, fm->cno)) return (1); goto done; } } /* Case 3 -- delete within a single line. */ if (tm->lno == fm->lno) { if (db_get(sp, fm->lno, DBG_FATAL, &p, &len)) return (1); GET_SPACE_RETW(sp, bp, blen, len); if (fm->cno != 0) MEMCPY(bp, p, fm->cno); MEMCPY(bp + fm->cno, p + (tm->cno + 1), len - (tm->cno + 1)); if (db_set(sp, fm->lno, bp, len - ((tm->cno - fm->cno) + 1))) goto err; goto done; } /* * Case 4 -- delete over multiple lines. * * Copy the start partial line into place. */ if ((tlen = fm->cno) != 0) { if (db_get(sp, fm->lno, DBG_FATAL, &p, NULL)) return (1); GET_SPACE_RETW(sp, bp, blen, tlen + 256); MEMCPY(bp, p, tlen); } /* Copy the end partial line into place. */ if (db_get(sp, tm->lno, DBG_FATAL, &p, &len)) goto err; if (len != 0 && tm->cno != len - 1) { /* * XXX * We can overflow memory here, if the total length is greater * than SIZE_T_MAX. The only portable way I've found to test * is depending on the overflow being less than the value. */ nlen = (len - (tm->cno + 1)) + tlen; if (tlen > nlen) { msgq(sp, M_ERR, "002|Line length overflow"); goto err; } if (tlen == 0) { GET_SPACE_RETW(sp, bp, blen, nlen); } else ADD_SPACE_RETW(sp, bp, blen, nlen); MEMCPY(bp + tlen, p + (tm->cno + 1), len - (tm->cno + 1)); tlen += len - (tm->cno + 1); } /* Set the current line. */ if (db_set(sp, fm->lno, bp, tlen)) goto err; /* Delete the last and intermediate lines. */ for (lno = tm->lno; lno > fm->lno; --lno) { if (db_delete(sp, lno)) goto err; ++sp->rptlines[L_DELETED]; if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp)) break; } done: rval = 0; if (0) err: rval = 1; if (bp != NULL) FREE_SPACEW(sp, bp, blen); return (rval); }
/* * sscr_exec -- * Take a line and hand it off to the shell. * * PUBLIC: int sscr_exec __P((SCR *, db_recno_t)); */ int sscr_exec(SCR *sp, db_recno_t lno) { SCRIPT *sc; db_recno_t last_lno; size_t blen, len, last_len, tlen; int isempty, matchprompt, rval; ssize_t nw; CHAR_T *bp = NULL; CHAR_T *p; /* If there's a prompt on the last line, append the command. */ if (db_last(sp, &last_lno)) return (1); if (db_get(sp, last_lno, DBG_FATAL, &p, &last_len)) return (1); if (sscr_matchprompt(sp, p, last_len, &tlen) && tlen == 0) { matchprompt = 1; GET_SPACE_RETW(sp, bp, blen, last_len + 128); MEMMOVEW(bp, p, last_len); } else matchprompt = 0; /* Get something to execute. */ if (db_eget(sp, lno, &p, &len, &isempty)) { if (isempty) goto empty; goto err1; } /* Empty lines aren't interesting. */ if (len == 0) goto empty; /* Delete any prompt. */ if (sscr_matchprompt(sp, p, len, &tlen)) { if (tlen == len) { empty: msgq(sp, M_BERR, "151|No command to execute"); goto err1; } p += (len - tlen); len = tlen; } /* Push the line to the shell. */ sc = sp->script; if ((size_t)(nw = write(sc->sh_master, p, len)) != len) goto err2; rval = 0; if (write(sc->sh_master, "\n", 1) != 1) { err2: if (nw == 0) errno = EIO; msgq(sp, M_SYSERR, "shell"); goto err1; } if (matchprompt) { ADD_SPACE_RETW(sp, bp, blen, last_len + len); MEMMOVEW(bp + last_len, p, len); if (db_set(sp, last_lno, bp, last_len + len)) err1: rval = 1; } if (matchprompt) FREE_SPACEW(sp, bp, blen); return (rval); }