1 /*	$OpenBSD: vs_split.c,v 1.10 2009/10/27 23:59:49 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/time.h>
17 
18 #include <bitstring.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "../common/common.h"
26 #include "vi.h"
27 
28 static SCR *vs_getbg(SCR *, char *);
29 
30 /*
31  * vs_split --
32  *	Create a new screen.
33  *
34  * PUBLIC: int vs_split(SCR *, SCR *, int);
35  */
36 int
vs_split(sp,new,ccl)37 vs_split(sp, new, ccl)
38 	SCR *sp, *new;
39 	int ccl;		/* Colon-command line split. */
40 {
41 	GS *gp;
42 	SMAP *smp;
43 	size_t half;
44 	int issmallscreen, splitup;
45 
46 	gp = sp->gp;
47 
48 	/* Check to see if it's possible. */
49 	/* XXX: The IS_ONELINE fix will change this, too. */
50 	if (sp->rows < 4) {
51 		msgq(sp, M_ERR,
52 		    "222|Screen must be larger than %d lines to split", 4 - 1);
53 		return (1);
54 	}
55 
56 	/* Wait for any messages in the screen. */
57 	vs_resolve(sp, NULL, 1);
58 
59 	half = sp->rows / 2;
60 	if (ccl && half > 6)
61 		half = 6;
62 
63 	/* Get a new screen map. */
64 	CALLOC(sp, _HMAP(new), SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
65 	if (_HMAP(new) == NULL)
66 		return (1);
67 	_HMAP(new)->lno = sp->lno;
68 	_HMAP(new)->coff = 0;
69 	_HMAP(new)->soff = 1;
70 
71 	/*
72 	 * Small screens: see vs_refresh.c section 6a.  Set a flag so
73 	 * we know to fix the screen up later.
74 	 */
75 	issmallscreen = IS_SMALL(sp);
76 
77 	/* The columns in the screen don't change. */
78 	new->cols = sp->cols;
79 
80 	/*
81 	 * Split the screen, and link the screens together.  If creating a
82 	 * screen to edit the colon command line or the cursor is in the top
83 	 * half of the current screen, the new screen goes under the current
84 	 * screen.  Else, it goes above the current screen.
85 	 *
86 	 * Recalculate current cursor position based on sp->lno, we're called
87 	 * with the cursor on the colon command line.  Then split the screen
88 	 * in half and update the shared information.
89 	 */
90 	splitup =
91 	    !ccl && (vs_sm_cursor(sp, &smp) ? 0 : (smp - HMAP) + 1) >= half;
92 	if (splitup) {				/* Old is bottom half. */
93 		new->rows = sp->rows - half;	/* New. */
94 		new->woff = sp->woff;
95 		sp->rows = half;		/* Old. */
96 		sp->woff += new->rows;
97 						/* Link in before old. */
98 		CIRCLEQ_INSERT_BEFORE(&gp->dq, sp, new, q);
99 
100 		/*
101 		 * If the parent is the bottom half of the screen, shift
102 		 * the map down to match on-screen text.
103 		 */
104 		memmove(_HMAP(sp), _HMAP(sp) + new->rows,
105 		    (sp->t_maxrows - new->rows) * sizeof(SMAP));
106 	} else {				/* Old is top half. */
107 		new->rows = half;		/* New. */
108 		sp->rows -= half;		/* Old. */
109 		new->woff = sp->woff + sp->rows;
110 						/* Link in after old. */
111 		CIRCLEQ_INSERT_AFTER(&gp->dq, sp, new, q);
112 	}
113 
114 	/* Adjust maximum text count. */
115 	sp->t_maxrows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
116 	new->t_maxrows = IS_ONELINE(new) ? 1 : new->rows - 1;
117 
118 	/*
119 	 * Small screens: see vs_refresh.c, section 6a.
120 	 *
121 	 * The child may have different screen options sizes than the parent,
122 	 * so use them.  Guarantee that text counts aren't larger than the
123 	 * new screen sizes.
124 	 */
125 	if (issmallscreen) {
126 		/* Fix the text line count for the parent. */
127 		if (splitup)
128 			sp->t_rows -= new->rows;
129 
130 		/* Fix the parent screen. */
131 		if (sp->t_rows > sp->t_maxrows)
132 			sp->t_rows = sp->t_maxrows;
133 		if (sp->t_minrows > sp->t_maxrows)
134 			sp->t_minrows = sp->t_maxrows;
135 
136 		/* Fix the child screen. */
137 		new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
138 		if (new->t_rows > new->t_maxrows)
139 			new->t_rows = new->t_maxrows;
140 		if (new->t_minrows > new->t_maxrows)
141 			new->t_minrows = new->t_maxrows;
142 	} else {
143 		sp->t_minrows = sp->t_rows = IS_ONELINE(sp) ? 1 : sp->rows - 1;
144 
145 		/*
146 		 * The new screen may be a small screen, even if the parent
147 		 * was not.  Don't complain if O_WINDOW is too large, we're
148 		 * splitting the screen so the screen is much smaller than
149 		 * normal.
150 		 */
151 		new->t_minrows = new->t_rows = O_VAL(sp, O_WINDOW);
152 		if (new->t_rows > new->rows - 1)
153 			new->t_minrows = new->t_rows =
154 			    IS_ONELINE(new) ? 1 : new->rows - 1;
155 	}
156 
157 	/* Adjust the ends of the new and old maps. */
158 	_TMAP(sp) = IS_ONELINE(sp) ?
159 	    _HMAP(sp) : _HMAP(sp) + (sp->t_rows - 1);
160 	_TMAP(new) = IS_ONELINE(new) ?
161 	    _HMAP(new) : _HMAP(new) + (new->t_rows - 1);
162 
163 	/* Reset the length of the default scroll. */
164 	if ((sp->defscroll = sp->t_maxrows / 2) == 0)
165 		sp->defscroll = 1;
166 	if ((new->defscroll = new->t_maxrows / 2) == 0)
167 		new->defscroll = 1;
168 
169 	/*
170 	 * Initialize the screen flags:
171 	 *
172 	 * If we're in vi mode in one screen, we don't have to reinitialize.
173 	 * This isn't just a cosmetic fix.  The path goes like this:
174 	 *
175 	 *	return into vi(), SC_SSWITCH set
176 	 *	call vs_refresh() with SC_STATUS set
177 	 *	call vs_resolve to display the status message
178 	 *	call vs_refresh() because the SC_SCR_VI bit isn't set
179 	 *
180 	 * Things go downhill at this point.
181 	 *
182 	 * Draw the new screen from scratch, and add a status line.
183 	 */
184 	F_SET(new,
185 	    SC_SCR_REFORMAT | SC_STATUS |
186 	    F_ISSET(sp, SC_EX | SC_VI | SC_SCR_VI | SC_SCR_EX));
187 	return (0);
188 }
189 
190 /*
191  * vs_discard --
192  *	Discard the screen, folding the real-estate into a related screen,
193  *	if one exists, and return that screen.
194  *
195  * PUBLIC: int vs_discard(SCR *, SCR **);
196  */
197 int
vs_discard(sp,spp)198 vs_discard(sp, spp)
199 	SCR *sp, **spp;
200 {
201 	SCR *nsp;
202 	dir_t dir;
203 
204 	/*
205 	 * Save the old screen's cursor information.
206 	 *
207 	 * XXX
208 	 * If called after file_end(), and the underlying file was a tmp
209 	 * file, it may have gone away.
210 	 */
211 	if (sp->frp != NULL) {
212 		sp->frp->lno = sp->lno;
213 		sp->frp->cno = sp->cno;
214 		F_SET(sp->frp, FR_CURSORSET);
215 	}
216 
217 	/*
218 	 * Add into a previous screen and then into a subsequent screen, as
219 	 * they're the closest to the current screen.  If that doesn't work,
220 	 * there was no screen to join.
221 	 */
222 	if ((nsp = CIRCLEQ_PREV(sp, q)) != CIRCLEQ_END(&sp->gp->dq)) {
223 		nsp->rows += sp->rows;
224 		sp = nsp;
225 		dir = FORWARD;
226 	} else if ((nsp = CIRCLEQ_NEXT(sp, q)) != CIRCLEQ_END(&sp->gp->dq)) {
227 		nsp->woff = sp->woff;
228 		nsp->rows += sp->rows;
229 		sp = nsp;
230 		dir = BACKWARD;
231 	} else {
232 		sp = NULL;
233 		dir = 0;	/* unused */
234 	}
235 
236 	if (spp != NULL)
237 		*spp = sp;
238 	if (sp == NULL)
239 		return (0);
240 
241 	/*
242 	 * Make no effort to clean up the discarded screen's information.  If
243 	 * it's not exiting, we'll do the work when the user redisplays it.
244 	 *
245 	 * Small screens: see vs_refresh.c section 6a.  Adjust text line info,
246 	 * unless it's a small screen.
247 	 *
248 	 * Reset the length of the default scroll.
249 	 */
250 	if (!IS_SMALL(sp))
251 		sp->t_rows = sp->t_minrows = sp->rows - 1;
252 	sp->t_maxrows = sp->rows - 1;
253 	sp->defscroll = sp->t_maxrows / 2;
254 	*(HMAP + (sp->t_rows - 1)) = *TMAP;
255 	TMAP = HMAP + (sp->t_rows - 1);
256 
257 	/*
258 	 * Draw the new screen from scratch, and add a status line.
259 	 *
260 	 * XXX
261 	 * We could play games with the map, if this were ever to be a
262 	 * performance problem, but I wrote the code a few times and it
263 	 * was never clean or easy.
264 	 */
265 	switch (dir) {
266 	case FORWARD:
267 		vs_sm_fill(sp, OOBLNO, P_TOP);
268 		break;
269 	case BACKWARD:
270 		vs_sm_fill(sp, OOBLNO, P_BOTTOM);
271 		break;
272 	default:
273 		abort();
274 	}
275 
276 	F_SET(sp, SC_STATUS);
277 	return (0);
278 }
279 
280 /*
281  * vs_fg --
282  *	Background the current screen, and foreground a new one.
283  *
284  * PUBLIC: int vs_fg(SCR *, SCR **, CHAR_T *, int);
285  */
286 int
vs_fg(sp,nspp,name,newscreen)287 vs_fg(sp, nspp, name, newscreen)
288 	SCR *sp, **nspp;
289 	CHAR_T *name;
290 	int newscreen;
291 {
292 	GS *gp;
293 	SCR *nsp;
294 
295 	gp = sp->gp;
296 
297 	if (newscreen)
298 		/* Get the specified background screen. */
299 		nsp = vs_getbg(sp, name);
300 	else
301 		/* Swap screens. */
302 		if (vs_swap(sp, &nsp, name))
303 			return (1);
304 
305 	if ((*nspp = nsp) == NULL) {
306 		msgq_str(sp, M_ERR, name,
307 		    name == NULL ?
308 		    "223|There are no background screens" :
309 		    "224|There's no background screen editing a file named %s");
310 		return (1);
311 	}
312 
313 	if (newscreen) {
314 		/* Remove the new screen from the background queue. */
315 		CIRCLEQ_REMOVE(&gp->hq, nsp, q);
316 
317 		/* Split the screen; if we fail, hook the screen back in. */
318 		if (vs_split(sp, nsp, 0)) {
319 			CIRCLEQ_INSERT_TAIL(&gp->hq, nsp, q);
320 			return (1);
321 		}
322 	} else {
323 		/* Move the old screen to the background queue. */
324 		CIRCLEQ_REMOVE(&gp->dq, sp, q);
325 		CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
326 	}
327 	return (0);
328 }
329 
330 /*
331  * vs_bg --
332  *	Background the screen, and switch to the next one.
333  *
334  * PUBLIC: int vs_bg(SCR *);
335  */
336 int
vs_bg(sp)337 vs_bg(sp)
338 	SCR *sp;
339 {
340 	GS *gp;
341 	SCR *nsp;
342 
343 	gp = sp->gp;
344 
345 	/* Try and join with another screen. */
346 	if (vs_discard(sp, &nsp))
347 		return (1);
348 	if (nsp == NULL) {
349 		msgq(sp, M_ERR,
350 		    "225|You may not background your only displayed screen");
351 		return (1);
352 	}
353 
354 	/* Move the old screen to the background queue. */
355 	CIRCLEQ_REMOVE(&gp->dq, sp, q);
356 	CIRCLEQ_INSERT_TAIL(&gp->hq, sp, q);
357 
358 	/* Toss the screen map. */
359 	free(_HMAP(sp));
360 	_HMAP(sp) = NULL;
361 
362 	/* Switch screens. */
363 	sp->nextdisp = nsp;
364 	F_SET(sp, SC_SSWITCH);
365 
366 	return (0);
367 }
368 
369 /*
370  * vs_swap --
371  *	Swap the current screen with a backgrounded one.
372  *
373  * PUBLIC: int vs_swap(SCR *, SCR **, char *);
374  */
375 int
vs_swap(sp,nspp,name)376 vs_swap(sp, nspp, name)
377 	SCR *sp, **nspp;
378 	char *name;
379 {
380 	GS *gp;
381 	SCR *nsp;
382 
383 	gp = sp->gp;
384 
385 	/* Get the specified background screen. */
386 	if ((*nspp = nsp = vs_getbg(sp, name)) == NULL)
387 		return (0);
388 
389 	/*
390 	 * Save the old screen's cursor information.
391 	 *
392 	 * XXX
393 	 * If called after file_end(), and the underlying file was a tmp
394 	 * file, it may have gone away.
395 	 */
396 	if (sp->frp != NULL) {
397 		sp->frp->lno = sp->lno;
398 		sp->frp->cno = sp->cno;
399 		F_SET(sp->frp, FR_CURSORSET);
400 	}
401 
402 	/* Switch screens. */
403 	sp->nextdisp = nsp;
404 	F_SET(sp, SC_SSWITCH);
405 
406 	/* Initialize terminal information. */
407 	VIP(nsp)->srows = VIP(sp)->srows;
408 
409 	/* Initialize screen information. */
410 	nsp->cols = sp->cols;
411 	nsp->rows = sp->rows;	/* XXX: Only place in vi that sets rows. */
412 	nsp->woff = sp->woff;
413 
414 	/*
415 	 * Small screens: see vs_refresh.c, section 6a.
416 	 *
417 	 * The new screens may have different screen options sizes than the
418 	 * old one, so use them.  Make sure that text counts aren't larger
419 	 * than the new screen sizes.
420 	 */
421 	if (IS_SMALL(nsp)) {
422 		nsp->t_minrows = nsp->t_rows = O_VAL(nsp, O_WINDOW);
423 		if (nsp->t_rows > sp->t_maxrows)
424 			nsp->t_rows = nsp->t_maxrows;
425 		if (nsp->t_minrows > sp->t_maxrows)
426 			nsp->t_minrows = nsp->t_maxrows;
427 	} else
428 		nsp->t_rows = nsp->t_maxrows = nsp->t_minrows = nsp->rows - 1;
429 
430 	/* Reset the length of the default scroll. */
431 	nsp->defscroll = nsp->t_maxrows / 2;
432 
433 	/* Allocate a new screen map. */
434 	CALLOC_RET(nsp, _HMAP(nsp), SMAP *, SIZE_HMAP(nsp), sizeof(SMAP));
435 	_TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);
436 
437 	/* Fill the map. */
438 	if (vs_sm_fill(nsp, nsp->lno, P_FILL))
439 		return (1);
440 
441 	/*
442 	 * The new screen replaces the old screen in the parent/child list.
443 	 * We insert the new screen after the old one.  If we're exiting,
444 	 * the exit will delete the old one, if we're foregrounding, the fg
445 	 * code will move the old one to the background queue.
446 	 */
447 	CIRCLEQ_REMOVE(&gp->hq, nsp, q);
448 	CIRCLEQ_INSERT_AFTER(&gp->dq, sp, nsp, q);
449 
450 	/*
451 	 * Don't change the screen's cursor information other than to
452 	 * note that the cursor is wrong.
453 	 */
454 	F_SET(VIP(nsp), VIP_CUR_INVALID);
455 
456 	/* Draw the new screen from scratch, and add a status line. */
457 	F_SET(nsp, SC_SCR_REDRAW | SC_STATUS);
458 	return (0);
459 }
460 
461 /*
462  * vs_resize --
463  *	Change the absolute size of the current screen.
464  *
465  * PUBLIC: int vs_resize(SCR *, long, adj_t);
466  */
467 int
vs_resize(sp,count,adj)468 vs_resize(sp, count, adj)
469 	SCR *sp;
470 	long count;
471 	adj_t adj;
472 {
473 	GS *gp;
474 	SCR *g, *s;
475 	size_t g_off, s_off;
476 
477 	gp = sp->gp;
478 
479 	/*
480 	 * Figure out which screens will grow, which will shrink, and
481 	 * make sure it's possible.
482 	 */
483 	if (count == 0)
484 		return (0);
485 	if (adj == A_SET) {
486 		if (sp->t_maxrows == count)
487 			return (0);
488 		if (sp->t_maxrows > count) {
489 			adj = A_DECREASE;
490 			count = sp->t_maxrows - count;
491 		} else {
492 			adj = A_INCREASE;
493 			count = count - sp->t_maxrows;
494 		}
495 	}
496 
497 	g_off = s_off = 0;
498 	if (adj == A_DECREASE) {
499 		if (count < 0)
500 			count = -count;
501 		s = sp;
502 		if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
503 			goto toosmall;
504 		if ((g = CIRCLEQ_PREV(sp, q)) == CIRCLEQ_END(&gp->dq)) {
505 			if ((g = CIRCLEQ_NEXT(sp, q)) == CIRCLEQ_END(&gp->dq))
506 				goto toobig;
507 			g_off = -count;
508 		} else
509 			s_off = count;
510 	} else {
511 		g = sp;
512 		if ((s = CIRCLEQ_NEXT(sp, q)) != CIRCLEQ_END(&gp->dq))
513 			if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count)
514 				s = NULL;
515 			else
516 				s_off = count;
517 		else
518 			s = NULL;
519 		if (s == NULL) {
520 			if ((s = CIRCLEQ_PREV(sp, q)) == CIRCLEQ_END(&gp->dq)) {
521 toobig:				msgq(sp, M_BERR, adj == A_DECREASE ?
522 				    "227|The screen cannot shrink" :
523 				    "228|The screen cannot grow");
524 				return (1);
525 			}
526 			if (s->t_maxrows < MINIMUM_SCREEN_ROWS + count) {
527 toosmall:			msgq(sp, M_BERR,
528 				    "226|The screen can only shrink to %d rows",
529 				    MINIMUM_SCREEN_ROWS);
530 				return (1);
531 			}
532 			g_off = -count;
533 		}
534 	}
535 
536 	/*
537 	 * Fix up the screens; we could optimize the reformatting of the
538 	 * screen, but this isn't likely to be a common enough operation
539 	 * to make it worthwhile.
540 	 */
541 	s->rows += -count;
542 	s->woff += s_off;
543 	g->rows += count;
544 	g->woff += g_off;
545 
546 	g->t_rows += count;
547 	if (g->t_minrows == g->t_maxrows)
548 		g->t_minrows += count;
549 	g->t_maxrows += count;
550 	_TMAP(g) += count;
551 	F_SET(g, SC_SCR_REFORMAT | SC_STATUS);
552 
553 	s->t_rows -= count;
554 	s->t_maxrows -= count;
555 	if (s->t_minrows > s->t_maxrows)
556 		s->t_minrows = s->t_maxrows;
557 	_TMAP(s) -= count;
558 	F_SET(s, SC_SCR_REFORMAT | SC_STATUS);
559 
560 	return (0);
561 }
562 
563 /*
564  * vs_getbg --
565  *	Get the specified background screen, or, if name is NULL, the first
566  *	background screen.
567  */
568 static SCR *
vs_getbg(sp,name)569 vs_getbg(sp, name)
570 	SCR *sp;
571 	char *name;
572 {
573 	GS *gp;
574 	SCR *nsp;
575 	char *p;
576 
577 	gp = sp->gp;
578 
579 	/* If name is NULL, return the first background screen on the list. */
580 	if (name == NULL) {
581 		nsp = CIRCLEQ_FIRST(&gp->hq);
582 		return (nsp == (void *)&gp->hq ? NULL : nsp);
583 	}
584 
585 	/* Search for a full match. */
586 	CIRCLEQ_FOREACH(nsp, &gp->hq, q)
587 		if (!strcmp(nsp->frp->name, name))
588 			break;
589 	if (nsp != (void *)&gp->hq)
590 		return (nsp);
591 
592 	/* Search for a last-component match. */
593 	CIRCLEQ_FOREACH(nsp, &gp->hq, q) {
594 		if ((p = strrchr(nsp->frp->name, '/')) == NULL)
595 			p = nsp->frp->name;
596 		else
597 			++p;
598 		if (!strcmp(p, name))
599 			break;
600 	}
601 	if (nsp != (void *)&gp->hq)
602 		return (nsp);
603 
604 	return (NULL);
605 }
606