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