1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
6 *
7 * See the LICENSE file for redistribution information.
8 */
9
10 #include "config.h"
11
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "common.h"
26
27 static void cb_rotate(SCR *);
28
29 /*
30 * cut --
31 * Put a range of lines/columns into a TEXT buffer.
32 *
33 * There are two buffer areas, both found in the global structure. The first
34 * is the linked list of all the buffers the user has named, the second is the
35 * unnamed buffer storage. There is a pointer, too, which is the current
36 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
37 * depending on into what buffer the last text was cut. Logically, in both
38 * delete and yank operations, if the user names a buffer, the text is cut
39 * into it. If it's a delete of information on more than a single line, the
40 * contents of the numbered buffers are rotated up one, the contents of the
41 * buffer named '9' are discarded, and the text is cut into the buffer named
42 * '1'. The text is always cut into the unnamed buffer.
43 *
44 * In all cases, upper-case buffer names are the same as lower-case names,
45 * with the exception that they cause the buffer to be appended to instead
46 * of replaced. Note, however, that if text is appended to a buffer, the
47 * default buffer only contains the appended text, not the entire contents
48 * of the buffer.
49 *
50 * !!!
51 * The contents of the default buffer would disappear after most operations
52 * in historic vi. It's unclear that this is useful, so we don't bother.
53 *
54 * When users explicitly cut text into the numeric buffers, historic vi became
55 * genuinely strange. I've never been able to figure out what was supposed to
56 * happen. It behaved differently if you deleted text than if you yanked text,
57 * and, in the latter case, the text was appended to the buffer instead of
58 * replacing the contents. Hopefully it's not worth getting right, and here
59 * we just treat the numeric buffers like any other named buffer.
60 *
61 * PUBLIC: int cut(SCR *, CHAR_T *, MARK *, MARK *, int);
62 */
63 int
cut(SCR * sp,CHAR_T * namep,MARK * fm,MARK * tm,int flags)64 cut(SCR *sp, CHAR_T *namep, MARK *fm, MARK *tm, int flags)
65 {
66 CB *cbp;
67 CHAR_T name = '\0';
68 recno_t lno;
69 int append, copy_one, copy_def;
70
71 /*
72 * If the user specified a buffer, put it there. (This may require
73 * a copy into the numeric buffers. We do the copy so that we don't
74 * have to reference count and so we don't have to deal with things
75 * like appends to buffers that are used multiple times.)
76 *
77 * Otherwise, if it's supposed to be put in a numeric buffer (usually
78 * a delete) put it there. The rules for putting things in numeric
79 * buffers were historically a little strange. There were three cases.
80 *
81 * 1: Some motions are always line mode motions, which means
82 * that the cut always goes into the numeric buffers.
83 * 2: Some motions aren't line mode motions, e.g. d10w, but
84 * can cross line boundaries. For these commands, if the
85 * cut crosses a line boundary, it goes into the numeric
86 * buffers. This includes most of the commands.
87 * 3: Some motions aren't line mode motions, e.g. d`<char>,
88 * but always go into the numeric buffers, regardless. This
89 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
90 *
91 * Otherwise, put it in the unnamed buffer.
92 */
93 append = copy_one = copy_def = 0;
94 if (namep != NULL) {
95 name = *namep;
96 if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
97 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
98 copy_one = 1;
99 cb_rotate(sp);
100 }
101 if ((append = isupper(name))) {
102 if (!copy_one)
103 copy_def = 1;
104 name = tolower(name);
105 }
106 namecb: CBNAME(sp, cbp, name);
107 } else if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
108 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
109 name = '1';
110 cb_rotate(sp);
111 goto namecb;
112 } else
113 cbp = &sp->gp->dcb_store;
114
115 copyloop:
116 /*
117 * If this is a new buffer, create it and add it into the list.
118 * Otherwise, if it's not an append, free its current contents.
119 */
120 if (cbp == NULL) {
121 CALLOC_RET(sp, cbp, 1, sizeof(CB));
122 cbp->name = name;
123 TAILQ_INIT(cbp->textq);
124 SLIST_INSERT_HEAD(sp->gp->cutq, cbp, q);
125 } else if (!append) {
126 text_lfree(cbp->textq);
127 cbp->len = 0;
128 cbp->flags = 0;
129 }
130
131
132 /* In line mode, it's pretty easy, just cut the lines. */
133 if (LF_ISSET(CUT_LINEMODE)) {
134 cbp->flags |= CB_LMODE;
135 for (lno = fm->lno; lno <= tm->lno; ++lno)
136 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
137 goto cut_line_err;
138 } else {
139 /*
140 * Get the first line. A length of ENTIRE_LINE causes
141 * cut_line to cut from the MARK to the end of the line.
142 */
143 if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
144 ENTIRE_LINE : (tm->cno - fm->cno) + 1, cbp))
145 goto cut_line_err;
146
147 /* Get the intermediate lines. */
148 for (lno = fm->lno; ++lno < tm->lno;)
149 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
150 goto cut_line_err;
151
152 /* Get the last line. */
153 if (tm->lno != fm->lno &&
154 cut_line(sp, lno, 0, tm->cno + 1, cbp))
155 goto cut_line_err;
156 }
157
158 append = 0; /* Only append to the named buffer. */
159 sp->gp->dcbp = cbp; /* Repoint the default buffer on each pass. */
160
161 if (copy_one) { /* Copy into numeric buffer 1. */
162 name = '1';
163 CBNAME(sp, cbp, name);
164 copy_one = 0;
165 goto copyloop;
166 }
167 if (copy_def) { /* Copy into the default buffer. */
168 cbp = &sp->gp->dcb_store;
169 copy_def = 0;
170 goto copyloop;
171 }
172 return (0);
173
174 cut_line_err:
175 text_lfree(cbp->textq);
176 cbp->len = 0;
177 cbp->flags = 0;
178 return (1);
179 }
180
181 /*
182 * cb_rotate --
183 * Rotate the numbered buffers up one.
184 */
185 static void
cb_rotate(SCR * sp)186 cb_rotate(SCR *sp)
187 {
188 CB *cbp, *del_cbp = NULL, *pre_cbp = NULL;
189
190 SLIST_FOREACH(cbp, sp->gp->cutq, q) {
191 switch(cbp->name) {
192 case '1': case '2': case '3':
193 case '4': case '5': case '6':
194 case '7': case '8':
195 cbp->name += 1;
196 break;
197 case '9':
198 if (cbp == SLIST_FIRST(sp->gp->cutq))
199 SLIST_REMOVE_HEAD(sp->gp->cutq, q);
200 else
201 SLIST_REMOVE_AFTER(pre_cbp, q);
202 del_cbp = cbp;
203 break;
204 }
205 pre_cbp = cbp;
206 }
207 if (del_cbp != NULL) {
208 text_lfree(del_cbp->textq);
209 free(del_cbp);
210 }
211 }
212
213 /*
214 * cut_line --
215 * Cut a portion of a single line.
216 *
217 * PUBLIC: int cut_line(SCR *, recno_t, size_t, size_t, CB *);
218 */
219 int
cut_line(SCR * sp,recno_t lno,size_t fcno,size_t clen,CB * cbp)220 cut_line(SCR *sp, recno_t lno, size_t fcno, size_t clen, CB *cbp)
221 {
222 TEXT *tp;
223 size_t len;
224 CHAR_T *p;
225
226 /* Get the line. */
227 if (db_get(sp, lno, DBG_FATAL, &p, &len))
228 return (1);
229
230 /* Create a TEXT structure that can hold the entire line. */
231 if ((tp = text_init(sp, NULL, 0, len)) == NULL)
232 return (1);
233
234 /*
235 * If the line isn't empty and it's not the entire line,
236 * copy the portion we want, and reset the TEXT length.
237 */
238 if (len != 0) {
239 if (clen == ENTIRE_LINE)
240 clen = len - fcno;
241 MEMCPY(tp->lb, p + fcno, clen);
242 tp->len = clen;
243 }
244
245 /* Append to the end of the cut buffer. */
246 TAILQ_INSERT_TAIL(cbp->textq, tp, q);
247 cbp->len += tp->len;
248
249 return (0);
250 }
251
252 /*
253 * cut_close --
254 * Discard all cut buffers.
255 *
256 * PUBLIC: void cut_close(GS *);
257 */
258 void
cut_close(GS * gp)259 cut_close(GS *gp)
260 {
261 CB *cbp;
262
263 /* Free cut buffer list. */
264 while ((cbp = SLIST_FIRST(gp->cutq)) != NULL) {
265 if (!TAILQ_EMPTY(cbp->textq))
266 text_lfree(cbp->textq);
267 SLIST_REMOVE_HEAD(gp->cutq, q);
268 free(cbp);
269 }
270
271 /* Free default cut storage. */
272 cbp = &gp->dcb_store;
273 if (!TAILQ_EMPTY(cbp->textq))
274 text_lfree(cbp->textq);
275 }
276
277 /*
278 * text_init --
279 * Allocate a new TEXT structure.
280 *
281 * PUBLIC: TEXT *text_init(SCR *, const CHAR_T *, size_t, size_t);
282 */
283 TEXT *
text_init(SCR * sp,const CHAR_T * p,size_t len,size_t total_len)284 text_init(SCR *sp, const CHAR_T *p, size_t len, size_t total_len)
285 {
286 TEXT *tp;
287
288 CALLOC(sp, tp, 1, sizeof(TEXT));
289 if (tp == NULL)
290 return (NULL);
291 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
292 if ((tp->lb_len = total_len * sizeof(CHAR_T)) != 0) {
293 MALLOC(sp, tp->lb, tp->lb_len);
294 if (tp->lb == NULL) {
295 free(tp);
296 return (NULL);
297 }
298 if (p != NULL && len != 0)
299 MEMCPY(tp->lb, p, len);
300 }
301 tp->len = len;
302 return (tp);
303 }
304
305 /*
306 * text_lfree --
307 * Free a chain of text structures.
308 *
309 * PUBLIC: void text_lfree(TEXTH *);
310 */
311 void
text_lfree(TEXTH * headp)312 text_lfree(TEXTH *headp)
313 {
314 TEXT *tp;
315
316 while ((tp = TAILQ_FIRST(headp)) != NULL) {
317 TAILQ_REMOVE(headp, tp, q);
318 text_free(tp);
319 }
320 }
321
322 /*
323 * text_free --
324 * Free a text structure.
325 *
326 * PUBLIC: void text_free(TEXT *);
327 */
328 void
text_free(TEXT * tp)329 text_free(TEXT *tp)
330 {
331 free(tp->lb);
332 free(tp);
333 }
334