Annotation of src/usr.bin/mg/line.c, Revision 1.58
1.58 ! mmcc 1: /* $OpenBSD: line.c,v 1.57 2015/09/29 02:07:49 guenther Exp $ */
1.23 kjell 2:
3: /* This file is in the public domain. */
1.5 niklas 4:
1.1 deraadt 5: /*
6: * Text line handling.
1.8 mickey 7: *
1.4 millert 8: * The functions in this file are a general set of line management
1.8 mickey 9: * utilities. They are the only routines that touch the text. They
10: * also touch the buffer and window structures to make sure that the
1.51 lum 11: * necessary updating gets done.
1.1 deraadt 12: *
1.8 mickey 13: * Note that this code only updates the dot and mark values in the window
14: * list. Since all the code acts on the current window, the buffer that
15: * we are editing must be displayed, which means that "b_nwnd" is non-zero,
16: * which means that the dot and mark values in the buffer headers are
1.1 deraadt 17: * nonsense.
18: */
19:
1.55 bcallah 20: #include <sys/queue.h>
1.54 guenther 21: #include <limits.h>
1.55 bcallah 22: #include <signal.h>
23: #include <stdio.h>
1.32 kjell 24: #include <stdlib.h>
25: #include <string.h>
1.55 bcallah 26:
27: #include "def.h"
1.32 kjell 28:
1.4 millert 29: /*
1.9 vincent 30: * Allocate a new line of size `used'. lrealloc() can be called if the line
31: * ever needs to grow beyond that.
1.1 deraadt 32: */
1.28 deraadt 33: struct line *
1.9 vincent 34: lalloc(int used)
1.3 millert 35: {
1.28 deraadt 36: struct line *lp;
1.1 deraadt 37:
1.22 db 38: if ((lp = malloc(sizeof(*lp))) == NULL)
39: return (NULL);
1.9 vincent 40: lp->l_text = NULL;
41: lp->l_size = 0;
42: lp->l_used = used; /* XXX */
43: if (lrealloc(lp, used) == FALSE) {
44: free(lp);
1.22 db 45: return (NULL);
1.1 deraadt 46: }
1.22 db 47: return (lp);
1.1 deraadt 48: }
49:
1.9 vincent 50: int
1.28 deraadt 51: lrealloc(struct line *lp, int newsize)
1.1 deraadt 52: {
1.9 vincent 53: char *tmp;
1.1 deraadt 54:
1.18 millert 55: if (lp->l_size < newsize) {
56: if ((tmp = realloc(lp->l_text, newsize)) == NULL)
1.22 db 57: return (FALSE);
1.18 millert 58: lp->l_text = tmp;
59: lp->l_size = newsize;
60: }
1.22 db 61: return (TRUE);
1.1 deraadt 62: }
63:
64: /*
1.4 millert 65: * Delete line "lp". Fix all of the links that might point to it (they are
1.8 mickey 66: * moved to offset 0 of the next line. Unlink the line from whatever buffer
67: * it might be in, and release the memory. The buffers are updated too; the
1.4 millert 68: * magic conditions described in the above comments don't hold here.
1.1 deraadt 69: */
1.6 art 70: void
1.28 deraadt 71: lfree(struct line *lp)
1.3 millert 72: {
1.28 deraadt 73: struct buffer *bp;
74: struct mgwin *wp;
1.1 deraadt 75:
1.3 millert 76: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 77: if (wp->w_linep == lp)
78: wp->w_linep = lp->l_fp;
1.3 millert 79: if (wp->w_dotp == lp) {
80: wp->w_dotp = lp->l_fp;
81: wp->w_doto = 0;
1.1 deraadt 82: }
83: if (wp->w_markp == lp) {
84: wp->w_markp = lp->l_fp;
85: wp->w_marko = 0;
86: }
87: }
1.3 millert 88: for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
1.1 deraadt 89: if (bp->b_nwnd == 0) {
1.3 millert 90: if (bp->b_dotp == lp) {
1.1 deraadt 91: bp->b_dotp = lp->l_fp;
92: bp->b_doto = 0;
93: }
94: if (bp->b_markp == lp) {
95: bp->b_markp = lp->l_fp;
96: bp->b_marko = 0;
97: }
98: }
99: }
100: lp->l_bp->l_fp = lp->l_fp;
101: lp->l_fp->l_bp = lp->l_bp;
1.58 ! mmcc 102: free(lp->l_text);
1.9 vincent 103: free(lp);
1.1 deraadt 104: }
105:
106: /*
1.8 mickey 107: * This routine is called when a character changes in place in the current
108: * buffer. It updates all of the required flags in the buffer and window
109: * system. The flag used is passed as an argument; if the buffer is being
110: * displayed in more than 1 window we change EDIT to HARD. Set MODE if the
1.4 millert 111: * mode line needs to be updated (the "*" has to be set).
1.1 deraadt 112: */
1.6 art 113: void
1.15 vincent 114: lchange(int flag)
1.3 millert 115: {
1.28 deraadt 116: struct mgwin *wp;
1.1 deraadt 117:
1.4 millert 118: /* update mode lines if this is the first change. */
119: if ((curbp->b_flag & BFCHG) == 0) {
120: flag |= WFMODE;
1.1 deraadt 121: curbp->b_flag |= BFCHG;
122: }
1.3 millert 123: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 124: if (wp->w_bufp == curbp) {
1.47 kjell 125: wp->w_rflag |= flag;
1.3 millert 126: if (wp != curwp)
1.47 kjell 127: wp->w_rflag |= WFFULL;
1.1 deraadt 128: }
129: }
1.20 vincent 130: }
131:
132: /*
1.8 mickey 133: * Insert "n" copies of the character "c" at the current location of dot.
134: * In the easy case all that happens is the text is stored in the line.
135: * In the hard case, the line has to be reallocated. When the window list
136: * is updated, take special care; I screwed it up once. You always update
1.4 millert 137: * dot in the current window. You update mark and a dot in another window
138: * if it is greater than the place where you did the insert. Return TRUE
1.1 deraadt 139: * if all is well, and FALSE on errors.
140: */
1.4 millert 141: int
1.15 vincent 142: linsert(int n, int c)
1.1 deraadt 143: {
1.28 deraadt 144: struct line *lp1;
145: struct mgwin *wp;
1.4 millert 146: RSIZE i;
147: int doto;
1.45 kjell 148: int s;
1.1 deraadt 149:
1.20 vincent 150: if (!n)
151: return (TRUE);
152:
1.45 kjell 153: if ((s = checkdirty(curbp)) != TRUE)
154: return (s);
155:
1.15 vincent 156: if (curbp->b_flag & BFREADONLY) {
1.53 lum 157: dobeep();
1.15 vincent 158: ewprintf("Buffer is read only");
1.22 db 159: return (FALSE);
1.15 vincent 160: }
161:
1.1 deraadt 162: lchange(WFEDIT);
1.4 millert 163:
164: /* current line */
165: lp1 = curwp->w_dotp;
1.15 vincent 166:
1.4 millert 167: /* special case for the end */
1.41 kjell 168: if (lp1 == curbp->b_headp) {
1.28 deraadt 169: struct line *lp2, *lp3;
1.11 deraadt 170:
1.4 millert 171: /* now should only happen in empty buffer */
1.1 deraadt 172: if (curwp->w_doto != 0) {
1.53 lum 173: dobeep();
1.1 deraadt 174: ewprintf("bug: linsert");
1.22 db 175: return (FALSE);
1.1 deraadt 176: }
1.4 millert 177: /* allocate a new line */
1.9 vincent 178: if ((lp2 = lalloc(n)) == NULL)
1.22 db 179: return (FALSE);
1.4 millert 180: /* previous line */
181: lp3 = lp1->l_bp;
182: /* link in */
183: lp3->l_fp = lp2;
1.1 deraadt 184: lp2->l_fp = lp1;
185: lp1->l_bp = lp2;
186: lp2->l_bp = lp3;
1.3 millert 187: for (i = 0; i < n; ++i)
1.1 deraadt 188: lp2->l_text[i] = c;
1.3 millert 189: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 190: if (wp->w_linep == lp1)
191: wp->w_linep = lp2;
192: if (wp->w_dotp == lp1)
193: wp->w_dotp = lp2;
194: if (wp->w_markp == lp1)
195: wp->w_markp = lp2;
196: }
1.16 vincent 197: undo_add_insert(lp2, 0, n);
1.1 deraadt 198: curwp->w_doto = n;
1.22 db 199: return (TRUE);
1.1 deraadt 200: }
1.4 millert 201: /* save for later */
202: doto = curwp->w_doto;
1.9 vincent 203:
204: if ((lp1->l_used + n) > lp1->l_size) {
205: if (lrealloc(lp1, lp1->l_used + n) == FALSE)
1.22 db 206: return (FALSE);
1.11 deraadt 207: }
1.9 vincent 208: lp1->l_used += n;
1.11 deraadt 209: if (lp1->l_used != n)
1.9 vincent 210: memmove(&lp1->l_text[doto + n], &lp1->l_text[doto],
211: lp1->l_used - n - doto);
212:
1.4 millert 213: /* Add the characters */
214: for (i = 0; i < n; ++i)
1.9 vincent 215: lp1->l_text[doto + i] = c;
1.3 millert 216: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 217: if (wp->w_dotp == lp1) {
1.3 millert 218: if (wp == curwp || wp->w_doto > doto)
1.1 deraadt 219: wp->w_doto += n;
220: }
221: if (wp->w_markp == lp1) {
222: if (wp->w_marko > doto)
223: wp->w_marko += n;
224: }
225: }
1.16 vincent 226: undo_add_insert(curwp->w_dotp, doto, n);
1.22 db 227: return (TRUE);
1.1 deraadt 228: }
229:
1.44 kjell 230: /*
231: * Do the work of inserting a newline at the given line/offset.
232: * If mark is on the current line, we may have to move the markline
233: * to keep line numbers in sync.
234: * lnewline_at assumes the current buffer is writable. Checking for
235: * this fact should be done by the caller.
236: */
1.4 millert 237: int
1.28 deraadt 238: lnewline_at(struct line *lp1, int doto)
1.1 deraadt 239: {
1.28 deraadt 240: struct line *lp2;
241: struct mgwin *wp;
1.52 florian 242: int nlen, tcurwpdotline;
1.1 deraadt 243:
1.39 kjell 244: lchange(WFFULL);
1.14 vincent 245:
1.44 kjell 246: curwp->w_bufp->b_lines++;
247: /* Check if mark is past dot (even on current line) */
248: if (curwp->w_markline > curwp->w_dotline ||
249: (curwp->w_dotline == curwp->w_markline &&
250: curwp->w_marko >= doto))
251: curwp->w_markline++;
1.52 florian 252:
253: tcurwpdotline = curwp->w_dotline;
1.44 kjell 254:
1.35 kjell 255: /* If start of line, allocate a new line instead of copying */
1.4 millert 256: if (doto == 0) {
257: /* new first part */
1.35 kjell 258: if ((lp2 = lalloc(0)) == NULL)
259: return (FALSE);
1.1 deraadt 260: lp2->l_bp = lp1->l_bp;
261: lp1->l_bp->l_fp = lp2;
262: lp2->l_fp = lp1;
263: lp1->l_bp = lp2;
1.52 florian 264: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.3 millert 265: if (wp->w_linep == lp1)
266: wp->w_linep = lp2;
1.52 florian 267: if (wp->w_dotline >= tcurwpdotline)
268: wp->w_dotline++;
269: }
1.46 kjell 270: undo_add_boundary(FFRAND, 1);
1.35 kjell 271: undo_add_insert(lp2, 0, 1);
1.46 kjell 272: undo_add_boundary(FFRAND, 1);
1.35 kjell 273: return (TRUE);
1.1 deraadt 274: }
1.4 millert 275:
276: /* length of new part */
277: nlen = llength(lp1) - doto;
278:
279: /* new second half line */
1.35 kjell 280: if ((lp2 = lalloc(nlen)) == NULL)
281: return (FALSE);
1.3 millert 282: if (nlen != 0)
283: bcopy(&lp1->l_text[doto], &lp2->l_text[0], nlen);
1.1 deraadt 284: lp1->l_used = doto;
285: lp2->l_bp = lp1;
286: lp2->l_fp = lp1->l_fp;
287: lp1->l_fp = lp2;
288: lp2->l_fp->l_bp = lp2;
1.4 millert 289: /* Windows */
290: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 291: if (wp->w_dotp == lp1 && wp->w_doto >= doto) {
292: wp->w_dotp = lp2;
293: wp->w_doto -= doto;
1.52 florian 294: wp->w_dotline++;
295: } else if (wp->w_dotline > tcurwpdotline)
296: wp->w_dotline++;
1.1 deraadt 297: if (wp->w_markp == lp1 && wp->w_marko >= doto) {
298: wp->w_markp = lp2;
299: wp->w_marko -= doto;
300: }
301: }
1.46 kjell 302: undo_add_boundary(FFRAND, 1);
1.24 kjell 303: undo_add_insert(lp1, llength(lp1), 1);
1.46 kjell 304: undo_add_boundary(FFRAND, 1);
1.35 kjell 305: return (TRUE);
1.1 deraadt 306: }
307:
308: /*
1.21 vincent 309: * Insert a newline into the buffer at the current location of dot in the
310: * current window.
311: */
312: int
313: lnewline(void)
314: {
1.45 kjell 315: int s;
316:
317: if ((s = checkdirty(curbp)) != TRUE)
318: return (s);
1.21 vincent 319: if (curbp->b_flag & BFREADONLY) {
1.53 lum 320: dobeep();
1.21 vincent 321: ewprintf("Buffer is read only");
1.22 db 322: return (FALSE);
1.21 vincent 323: }
1.22 db 324: return (lnewline_at(curwp->w_dotp, curwp->w_doto));
1.21 vincent 325: }
326:
327: /*
1.36 kjell 328: * This function deletes "n" bytes, starting at dot. (actually, n+1, as the
329: * newline is included) It understands how to deal with end of lines, etc.
330: * It returns TRUE if all of the characters were deleted, and FALSE if
331: * they were not (because dot ran into the end of the buffer).
332: * The "kflag" indicates either no insertion, or direction of insertion
333: * into the kill buffer.
1.1 deraadt 334: */
1.4 millert 335: int
1.15 vincent 336: ldelete(RSIZE n, int kflag)
1.3 millert 337: {
1.28 deraadt 338: struct line *dotp;
1.36 kjell 339: RSIZE chunk;
1.28 deraadt 340: struct mgwin *wp;
1.36 kjell 341: int doto;
342: char *cp1, *cp2;
343: size_t len;
1.49 millert 344: char *sv = NULL;
1.36 kjell 345: int end;
1.45 kjell 346: int s;
1.49 millert 347: int rval = FALSE;
1.1 deraadt 348:
1.45 kjell 349: if ((s = checkdirty(curbp)) != TRUE)
350: return (s);
1.15 vincent 351: if (curbp->b_flag & BFREADONLY) {
1.53 lum 352: dobeep();
1.15 vincent 353: ewprintf("Buffer is read only");
1.49 millert 354: goto out;
1.15 vincent 355: }
1.32 kjell 356: len = n;
357: if ((sv = calloc(1, len + 1)) == NULL)
1.49 millert 358: goto out;
1.32 kjell 359: end = 0;
1.15 vincent 360:
1.48 kjell 361: undo_add_delete(curwp->w_dotp, curwp->w_doto, n, (kflag & KREG));
1.15 vincent 362:
1.1 deraadt 363: while (n != 0) {
364: dotp = curwp->w_dotp;
365: doto = curwp->w_doto;
1.4 millert 366: /* Hit the end of the buffer */
1.41 kjell 367: if (dotp == curbp->b_headp)
1.49 millert 368: goto out;
1.4 millert 369: /* Size of the chunk */
370: chunk = dotp->l_used - doto;
1.20 vincent 371:
1.1 deraadt 372: if (chunk > n)
373: chunk = n;
1.4 millert 374: /* End of line, merge */
375: if (chunk == 0) {
1.42 kjell 376: if (dotp == blastlp(curbp))
1.49 millert 377: goto out;
1.39 kjell 378: lchange(WFFULL);
1.32 kjell 379: if (ldelnewline() == FALSE)
1.49 millert 380: goto out;
1.32 kjell 381: end = strlcat(sv, "\n", len + 1);
1.1 deraadt 382: --n;
383: continue;
384: }
385: lchange(WFEDIT);
1.4 millert 386: /* Scrunch text */
387: cp1 = &dotp->l_text[doto];
1.32 kjell 388: memcpy(&sv[end], cp1, chunk);
389: end += chunk;
390: sv[end] = '\0';
1.20 vincent 391: for (cp2 = cp1 + chunk; cp2 < &dotp->l_text[dotp->l_used];
392: cp2++)
393: *cp1++ = *cp2;
1.4 millert 394: dotp->l_used -= (int)chunk;
1.3 millert 395: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
396: if (wp->w_dotp == dotp && wp->w_doto >= doto) {
1.1 deraadt 397: wp->w_doto -= chunk;
398: if (wp->w_doto < doto)
399: wp->w_doto = doto;
400: }
1.3 millert 401: if (wp->w_markp == dotp && wp->w_marko >= doto) {
1.1 deraadt 402: wp->w_marko -= chunk;
403: if (wp->w_marko < doto)
404: wp->w_marko = doto;
405: }
406: }
407: n -= chunk;
408: }
1.34 kjell 409: if (kchunk(sv, (RSIZE)len, kflag) != TRUE)
1.49 millert 410: goto out;
411: rval = TRUE;
412: out:
1.32 kjell 413: free(sv);
1.49 millert 414: return (rval);
1.1 deraadt 415: }
416:
417: /*
1.8 mickey 418: * Delete a newline and join the current line with the next line. If the next
1.4 millert 419: * line is the magic header line always return TRUE; merging the last line
1.8 mickey 420: * with the header line can be thought of as always being a successful
421: * operation. Even if nothing is done, this makes the kill buffer work
1.44 kjell 422: * "right". If the mark is past the dot (actually, markline > dotline),
423: * decrease the markline accordingly to keep line numbers in sync.
424: * Easy cases can be done by shuffling data around. Hard cases
1.8 mickey 425: * require that lines be moved about in memory. Return FALSE on error and
1.40 kjell 426: * TRUE if all looks ok. We do not update w_dotline here, as deletes are done
427: * after moves.
1.1 deraadt 428: */
1.4 millert 429: int
1.15 vincent 430: ldelnewline(void)
1.3 millert 431: {
1.28 deraadt 432: struct line *lp1, *lp2, *lp3;
433: struct mgwin *wp;
1.45 kjell 434: int s;
1.1 deraadt 435:
1.45 kjell 436: if ((s = checkdirty(curbp)) != TRUE)
437: return (s);
1.15 vincent 438: if (curbp->b_flag & BFREADONLY) {
1.53 lum 439: dobeep();
1.15 vincent 440: ewprintf("Buffer is read only");
1.22 db 441: return (FALSE);
1.15 vincent 442: }
443:
1.1 deraadt 444: lp1 = curwp->w_dotp;
445: lp2 = lp1->l_fp;
1.4 millert 446: /* at the end of the buffer */
1.41 kjell 447: if (lp2 == curbp->b_headp)
1.22 db 448: return (TRUE);
1.44 kjell 449: /* Keep line counts in sync */
1.40 kjell 450: curwp->w_bufp->b_lines--;
1.44 kjell 451: if (curwp->w_markline > curwp->w_dotline)
452: curwp->w_markline--;
1.1 deraadt 453: if (lp2->l_used <= lp1->l_size - lp1->l_used) {
454: bcopy(&lp2->l_text[0], &lp1->l_text[lp1->l_used], lp2->l_used);
1.3 millert 455: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
1.1 deraadt 456: if (wp->w_linep == lp2)
457: wp->w_linep = lp1;
458: if (wp->w_dotp == lp2) {
1.3 millert 459: wp->w_dotp = lp1;
1.1 deraadt 460: wp->w_doto += lp1->l_used;
461: }
462: if (wp->w_markp == lp2) {
1.3 millert 463: wp->w_markp = lp1;
1.1 deraadt 464: wp->w_marko += lp1->l_used;
465: }
466: }
467: lp1->l_used += lp2->l_used;
468: lp1->l_fp = lp2->l_fp;
469: lp2->l_fp->l_bp = lp1;
1.37 kjell 470: free(lp2);
1.22 db 471: return (TRUE);
1.1 deraadt 472: }
1.3 millert 473: if ((lp3 = lalloc(lp1->l_used + lp2->l_used)) == NULL)
1.22 db 474: return (FALSE);
1.1 deraadt 475: bcopy(&lp1->l_text[0], &lp3->l_text[0], lp1->l_used);
476: bcopy(&lp2->l_text[0], &lp3->l_text[lp1->l_used], lp2->l_used);
477: lp1->l_bp->l_fp = lp3;
478: lp3->l_fp = lp2->l_fp;
479: lp2->l_fp->l_bp = lp3;
480: lp3->l_bp = lp1->l_bp;
1.3 millert 481: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
482: if (wp->w_linep == lp1 || wp->w_linep == lp2)
1.1 deraadt 483: wp->w_linep = lp3;
484: if (wp->w_dotp == lp1)
1.3 millert 485: wp->w_dotp = lp3;
1.1 deraadt 486: else if (wp->w_dotp == lp2) {
1.3 millert 487: wp->w_dotp = lp3;
1.1 deraadt 488: wp->w_doto += lp1->l_used;
489: }
490: if (wp->w_markp == lp1)
1.3 millert 491: wp->w_markp = lp3;
1.1 deraadt 492: else if (wp->w_markp == lp2) {
1.3 millert 493: wp->w_markp = lp3;
1.1 deraadt 494: wp->w_marko += lp1->l_used;
495: }
496: }
1.37 kjell 497: free(lp1);
498: free(lp2);
1.22 db 499: return (TRUE);
1.1 deraadt 500: }
1.9 vincent 501:
1.1 deraadt 502: /*
1.8 mickey 503: * Replace plen characters before dot with argument string. Control-J
504: * characters in st are interpreted as newlines. There is a casehack
505: * disable flag (normally it likes to match case of replacement to what
1.4 millert 506: * was there).
1.1 deraadt 507: */
1.4 millert 508: int
1.27 kjell 509: lreplace(RSIZE plen, char *st)
1.4 millert 510: {
511: RSIZE rlen; /* replacement length */
1.45 kjell 512: int s;
1.15 vincent 513:
1.45 kjell 514: if ((s = checkdirty(curbp)) != TRUE)
515: return (s);
1.15 vincent 516: if (curbp->b_flag & BFREADONLY) {
1.53 lum 517: dobeep();
1.15 vincent 518: ewprintf("Buffer is read only");
1.22 db 519: return (FALSE);
1.15 vincent 520: }
1.46 kjell 521: undo_boundary_enable(FFRAND, 0);
1.25 deraadt 522:
1.6 art 523: (void)backchar(FFARG | FFRAND, (int)plen);
1.24 kjell 524: (void)ldelete(plen, KNONE);
1.15 vincent 525:
1.1 deraadt 526: rlen = strlen(st);
1.24 kjell 527: region_put_data(st, rlen);
1.39 kjell 528: lchange(WFFULL);
1.1 deraadt 529:
1.46 kjell 530: undo_boundary_enable(FFRAND, 1);
1.29 kjell 531: return (TRUE);
1.38 kjell 532: }
533:
534: /*
535: * Allocate and return the supplied line as a C string
536: */
537: char *
538: linetostr(const struct line *ln)
539: {
1.50 kjell 540: int len;
1.38 kjell 541: char *line;
542:
543: len = llength(ln);
1.50 kjell 544: if (len == INT_MAX) /* (len + 1) overflow */
1.38 kjell 545: return (NULL);
546:
547: if ((line = malloc(len + 1)) == NULL)
548: return (NULL);
549:
550: (void)memcpy(line, ltext(ln), len);
551: line[len] = '\0';
552:
553: return (line);
1.1 deraadt 554: }