malloc(2) of 0 bytes isn't defined, don't allocate anything
[nvi.git] / common / cut.c
blobc54b5a50f57ca377e73c58e1f22d8e2a56f8d496
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: cut.c,v 8.16 1993/12/29 10:48:43 bostic Exp $ (Berkeley) $Date: 1993/12/29 10:48:43 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <string.h>
20 #include "vi.h"
22 static int cb_line __P((SCR *, EXF *, recno_t, size_t, size_t, TEXT **));
23 static int cb_rotate __P((SCR *));
25 /*
26 * cut --
27 * Put a range of lines/columns into a buffer.
29 int
30 cut(sp, ep, name, fm, tm, lmode)
31 SCR *sp;
32 EXF *ep;
33 ARG_CHAR_T name;
34 int lmode;
35 MARK *fm, *tm;
37 CB *cbp;
38 TEXT *tp;
39 recno_t lno;
40 size_t len;
41 int append;
43 #if defined(DEBUG) && 0
44 TRACE(sp, "cut: from {%lu, %d}, to {%lu, %d}%s\n",
45 fm->lno, fm->cno, tm->lno, tm->cno, lmode ? " LINE MODE" : "");
46 #endif
48 * !!!
49 * The numeric buffers in historic vi offer us yet another opportunity
50 * for contemplation. First, the default buffer was the same as buffer
51 * '1'. Second, cutting into any numeric buffer caused buffers '1' to
52 * '8' to be rotated up one, and '9' to drop off the end. Finally,
53 * text cut into a numeric buffer other than '1' was always appended
54 * to the buffer (after the rotation), it was not a replacement.
56 append = 0;
57 if (isdigit(name)) {
58 (void)cb_rotate(sp);
59 if (name != '1')
60 append = 1;
64 * Upper-case buffer names map into lower-case buffers, but with
65 * append mode set so the buffer is appended to, not overwritten.
67 if (isupper(name))
68 append = 1;
69 CBNAME(sp, cbp, name);
72 * If this is a new buffer, create it, and add it into the list.
73 * Otherwise, if it's not an append, free its current contents.
75 if (cbp == NULL) {
76 CALLOC(sp, cbp, CB *, 1, sizeof(CB));
77 cbp->name = name;
78 LIST_INSERT_HEAD(&sp->gp->cutq, cbp, q);
79 CIRCLEQ_INIT(&cbp->textq);
80 } else if (!append) {
81 text_lfree(&cbp->textq);
82 cbp->len = 0;
83 cbp->flags = 0;
86 if (lmode) {
87 for (lno = fm->lno; lno <= tm->lno; ++lno) {
88 if (cb_line(sp, ep, lno, 0, 0, &tp))
89 goto mem;
90 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
91 cbp->len += tp->len;
93 cbp->flags |= CB_LMODE;
94 return (0);
97 /* Get the first line. */
98 len = fm->lno < tm->lno ? 0 : tm->cno - fm->cno;
99 if (cb_line(sp, ep, fm->lno, fm->cno, len, &tp))
100 goto mem;
102 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
103 cbp->len += tp->len;
105 for (lno = fm->lno; ++lno < tm->lno;) {
106 if (cb_line(sp, ep, lno, 0, 0, &tp))
107 goto mem;
108 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
109 cbp->len += tp->len;
112 if (tm->lno > fm->lno && tm->cno > 0) {
113 if (cb_line(sp, ep, lno, 0, tm->cno, &tp)) {
114 mem: if (append)
115 msgq(sp, M_ERR,
116 "Contents of %s buffer lost.",
117 charname(sp, name));
118 text_lfree(&cbp->textq);
119 cbp->len = 0;
120 cbp->flags = 0;
121 return (1);
123 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
124 cbp->len += tp->len;
126 return (0);
130 * cb_rotate --
131 * Rotate the numbered buffers up one.
133 static int
134 cb_rotate(sp)
135 SCR *sp;
137 CB *cbp, *del_cbp;
139 del_cbp = NULL;
140 for (cbp = sp->gp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next)
141 switch(cbp->name) {
142 case '1':
143 cbp->name = '2';
144 break;
145 case '2':
146 cbp->name = '3';
147 break;
148 case '3':
149 cbp->name = '4';
150 break;
151 case '4':
152 cbp->name = '5';
153 break;
154 case '5':
155 cbp->name = '6';
156 break;
157 case '6':
158 cbp->name = '7';
159 break;
160 case '7':
161 cbp->name = '8';
162 break;
163 case '8':
164 cbp->name = '9';
165 break;
166 case '9':
167 del_cbp = cbp;
168 break;
170 if (del_cbp != NULL) {
171 LIST_REMOVE(del_cbp, q);
172 text_lfree(&del_cbp->textq);
173 FREE(del_cbp, sizeof(CB));
175 return (0);
179 * cb_line --
180 * Cut a portion of a single line.
182 static int
183 cb_line(sp, ep, lno, fcno, clen, newp)
184 SCR *sp;
185 EXF *ep;
186 recno_t lno;
187 size_t fcno, clen;
188 TEXT **newp;
190 TEXT *tp;
191 size_t len;
192 char *p;
194 if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
195 GETLINE_ERR(sp, lno);
196 return (1);
199 if ((*newp = tp = text_init(sp, NULL, 0, len)) == NULL)
200 return (1);
203 * A length of zero means to cut from the MARK to the end
204 * of the line.
206 if (len != 0) {
207 if (clen == 0)
208 clen = len - fcno;
209 memmove(tp->lb, p + fcno, clen);
210 tp->len = clen;
212 return (0);
216 * text_init --
217 * Allocate a new TEXT structure.
219 TEXT *
220 text_init(sp, p, len, total_len)
221 SCR *sp;
222 const char *p;
223 size_t len, total_len;
225 TEXT *tp;
227 MALLOC(sp, tp, TEXT *, sizeof(TEXT));
228 if (tp == NULL)
229 return (NULL);
230 /* ANSI C doesn't define a call to malloc(2) for 0 bytes. */
231 if (tp->lb_len = total_len) {
232 MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len);
233 if (tp->lb == NULL) {
234 free(tp);
235 return (NULL);
237 if (p != NULL && len != 0)
238 memmove(tp->lb, p, len);
239 } else
240 tp->lb = NULL;
241 tp->len = len;
242 tp->ai = tp->insert = tp->offset = tp->owrite = 0;
243 tp->wd = NULL;
244 tp->wd_len = 0;
245 return (tp);
249 * text_lfree --
250 * Free a chain of text structures.
252 void
253 text_lfree(headp)
254 TEXTH *headp;
256 TEXT *tp;
258 while ((tp = headp->cqh_first) != (void *)headp) {
259 CIRCLEQ_REMOVE(headp, tp, q);
260 text_free(tp);
265 * text_free --
266 * Free a text structure.
268 void
269 text_free(tp)
270 TEXT *tp;
272 if (tp->lb != NULL)
273 FREE(tp->lb, tp->lb_len);
274 if (tp->wd != NULL)
275 FREE(tp->wd, tp->wd_len);
276 FREE(tp, sizeof(TEXT));
280 * put --
281 * Put text buffer contents into the file.
283 * !!!
284 * Historically, pasting into a file with no lines in vi would preserve
285 * the single blank line. This is almost certainly a result of the fact
286 * that historic vi couldn't deal with a file that had no lines in it.
287 * This implementation treats that as a bug, and does not retain the
288 * blank line.
291 put(sp, ep, name, cp, rp, append)
292 SCR *sp;
293 EXF *ep;
294 ARG_CHAR_T name;
295 MARK *cp, *rp;
296 int append;
298 CB *cbp;
299 TEXT *ltp, *tp;
300 recno_t lno;
301 size_t blen, clen, len;
302 int lmode;
303 char *bp, *p, *t;
305 CBEMPTY(sp, cbp, name);
307 tp = cbp->textq.cqh_first;
308 lmode = F_ISSET(cbp, CB_LMODE);
311 * It's possible to do a put into an empty file, meaning that the
312 * cut buffer simply becomes the file. It's a special case so
313 * that we can ignore it in general.
315 * Historical practice is that the cursor ends up on the first
316 * non-blank character of the first line inserted.
318 if (cp->lno == 1) {
319 if (file_lline(sp, ep, &lno))
320 return (1);
321 if (lno == 0) {
322 for (; tp != (void *)&cbp->textq;
323 ++lno, tp = tp->q.cqe_next)
324 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
325 return (1);
326 rp->lno = 1;
327 rp->cno = 0;
328 (void)nonblank(sp, ep, rp->lno, &rp->cno);
329 goto ret;
334 * If buffer was created in line mode, append each new line into the
335 * file.
337 if (lmode) {
338 lno = append ? cp->lno : cp->lno - 1;
339 rp->lno = lno + 1;
340 for (; tp != (void *)&cbp->textq; ++lno, tp = tp->q.cqe_next)
341 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
342 return (1);
343 rp->cno = 0;
344 (void)nonblank(sp, ep, rp->lno, &rp->cno);
345 goto ret;
349 * If buffer was cut in character mode, replace the current line with
350 * one built from the portion of the first line to the left of the
351 * split plus the first line in the CB. Append each intermediate line
352 * in the CB. Append a line built from the portion of the first line
353 * to the right of the split plus the last line in the CB.
355 * Get the first line.
357 lno = cp->lno;
358 if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
359 GETLINE_ERR(sp, lno);
360 return (1);
363 GET_SPACE_RET(sp, bp, blen, tp->len + len + 1);
364 t = bp;
366 /* Original line, left of the split. */
367 if (len > 0 && (clen = cp->cno + (append ? 1 : 0)) > 0) {
368 memmove(bp, p, clen);
369 p += clen;
370 t += clen;
373 /* First line from the CB. */
374 memmove(t, tp->lb, tp->len);
375 t += tp->len;
377 /* Calculate length left in original line. */
378 clen = len ? len - cp->cno - (append ? 1 : 0) : 0;
381 * If no more lines in the CB, append the rest of the original
382 * line and quit. Otherwise, build the last line before doing
383 * the intermediate lines, because the line changes will lose
384 * the cached line.
386 if (tp->q.cqe_next == (void *)&cbp->textq) {
388 * Historical practice is that if a non-line mode put
389 * is inside a single line, the cursor ends up on the
390 * last character inserted.
392 rp->lno = lno;
393 rp->cno = (t - bp) - 1;
395 if (clen > 0) {
396 memmove(t, p, clen);
397 t += clen;
399 if (file_sline(sp, ep, lno, bp, t - bp))
400 goto mem;
401 } else {
403 * Have to build both the first and last lines of the
404 * put before doing any sets or we'll lose the cached
405 * line. Build both the first and last lines in the
406 * same buffer, so we don't have to have another buffer
407 * floating around.
409 * Last part of original line; check for space, reset
410 * the pointer into the buffer.
412 ltp = cbp->textq.cqh_last;
413 len = t - bp;
414 ADD_SPACE_RET(sp, bp, blen, ltp->len + clen);
415 t = bp + len;
417 /* Add in last part of the CB. */
418 memmove(t, ltp->lb, ltp->len);
419 if (clen)
420 memmove(t + ltp->len, p, clen);
421 clen += ltp->len;
424 * Now: bp points to the first character of the first
425 * line, t points to the last character of the last
426 * line, t - bp is the length of the first line, and
427 * clen is the length of the last. Just figured you'd
428 * want to know.
430 * Output the line replacing the original line.
432 if (file_sline(sp, ep, lno, bp, t - bp))
433 goto mem;
436 * Historical practice is that if a non-line mode put
437 * covers multiple lines, the cursor ends up on the
438 * first character inserted. (Of course.)
440 rp->lno = lno;
441 rp->cno = (t - bp) - 1;
443 /* Output any intermediate lines in the CB. */
444 for (tp = tp->q.cqe_next;
445 tp->q.cqe_next != (void *)&cbp->textq;
446 ++lno, tp = tp->q.cqe_next)
447 if (file_aline(sp, ep, 1, lno, tp->lb, tp->len))
448 goto mem;
450 if (file_aline(sp, ep, 1, lno, t, clen)) {
451 mem: FREE_SPACE(sp, bp, blen);
452 return (1);
455 FREE_SPACE(sp, bp, blen);
457 /* Reporting... */
458 ret: sp->rptlines[L_PUT] += lno - cp->lno;
460 return (0);