xref: /netbsd-src/lib/libcurses/cr_put.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: cr_put.c,v 1.32 2017/01/06 13:53:18 roy Exp $	*/
2 
3 /*
4  * Copyright (c) 1981, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)cr_put.c	8.3 (Berkeley) 5/4/94";
36 #else
37 __RCSID("$NetBSD: cr_put.c,v 1.32 2017/01/06 13:53:18 roy Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include <string.h>
42 
43 #include "curses.h"
44 #include "curses_private.h"
45 
46 #define	HARDTABS	8
47 
48 /*
49  * Terminal driving and line formatting routines.  Basic motion optimizations
50  * are done here as well as formatting lines (printing of control characters,
51  * line numbering and the like).
52  */
53 
54 /* Stub function for the users. */
55 int
56 mvcur(int ly, int lx, int y, int x)
57 {
58 	return (__mvcur(ly, lx, y, x, 0));
59 }
60 
61 static void fgoto(int);
62 static int plod(int, int);
63 static int plodput(int);
64 static int tabcol(int, int);
65 
66 static int outcol, outline, destcol, destline;
67 
68 /*
69  * Sync the position of the output cursor.  Most work here is rounding for
70  * terminal boundaries getting the column position implied by wraparound or
71  * the lack thereof and rolling up the screen to get destline on the screen.
72  */
73 int
74 __mvcur(int ly, int lx, int y, int x, int in_refresh)
75 {
76 #ifdef DEBUG
77 	__CTRACE(__CTRACE_OUTPUT,
78 	    "mvcur: moving cursor from (%d, %d) to (%d, %d)\n", ly, lx, y, x);
79 #endif
80 	destcol = x;
81 	destline = y;
82 	outcol = lx;
83 	outline = ly;
84 	fgoto(in_refresh);
85 	return (OK);
86 }
87 
88 static void
89 fgoto(int in_refresh)
90 {
91 	int	 c, l;
92 	char	*cgp;
93 
94 #ifdef DEBUG
95 	__CTRACE(__CTRACE_OUTPUT, "fgoto: in_refresh=%d\n", in_refresh);
96 #endif /* DEBUG */
97 	if (destcol >= COLS) {
98 		destline += destcol / COLS;
99 		destcol %= COLS;
100 	}
101 	if (outcol >= COLS) {
102 		l = (outcol + 1) / COLS;
103 		outline += l;
104 		outcol %= COLS;
105 		if (auto_left_margin == 0) {
106 			while (l > 0) {
107 				if (__pfast) {
108 					if (carriage_return)
109 						tputs(carriage_return,
110 							0, __cputchar);
111 					else
112 						__cputchar('\r');
113 				}
114 				if (cursor_down)
115 					tputs(cursor_down, 0, __cputchar);
116 				else
117 					__cputchar('\n');
118 				l--;
119 			}
120 			outcol = 0;
121 		}
122 		if (outline > LINES - 1) {
123 			destline -= outline - (LINES - 1);
124 			outline = LINES - 1;
125 		}
126 	}
127 	if (destline >= LINES) {
128 		l = destline;
129 		destline = LINES - 1;
130 		if (outline < LINES - 1) {
131 			c = destcol;
132 			if (__pfast == 0 && !cursor_address)
133 				destcol = 0;
134 			fgoto(in_refresh);
135 			destcol = c;
136 		}
137 		while (l >= LINES) {
138 			/* The following linefeed (or simulation thereof) is
139 			 * supposed to scroll up the screen, since we are on
140 			 * the bottom line.  We make the assumption that
141 			 * linefeed will scroll.  If ns is in the capability
142 			 * list this won't work.  We should probably have an
143 			 * sc capability but sf will generally take the place
144 			 * if it works.
145 			 *
146 			 * Superbee glitch: in the middle of the screen have to
147 			 * use esc B (down) because linefeed screws up in
148 			 * "Efficient Paging" (what a joke) mode (which is
149 			 * essential in some SB's because CRLF mode puts
150 			 * garbage in at end of memory), but you must use
151 			 * linefeed to scroll since down arrow won't go past
152 			 * memory end. I turned this off after recieving Paul
153 			 * Eggert's Superbee description which wins better. */
154 			if (cursor_down /* && !__tc_xb */ && __pfast)
155 				tputs(cursor_down, 0, __cputchar);
156 			else
157 				__cputchar('\n');
158 			l--;
159 			if (__pfast == 0)
160 				outcol = 0;
161 		}
162 	}
163 	if (destline < outline && !(cursor_address || cursor_up))
164 		destline = outline;
165 
166 	if (cursor_address &&
167 	    (cgp = tiparm(cursor_address, destline, destcol)))
168 	{
169 		/*
170 		 * Need this condition due to inconsistent behavior
171 		 * of backspace on the last column.
172 		 */
173 #ifdef DEBUG
174 		__CTRACE(__CTRACE_OUTPUT, "fgoto: cgp=%s\n", cgp);
175 #endif /* DEBUG */
176 		if (outcol != COLS - 1 &&
177 		    plod((int) strlen(cgp), in_refresh) > 0)
178 			plod(0, in_refresh);
179 		else
180 			tputs(cgp, 0, __cputchar);
181 	} else
182 		plod(0, in_refresh);
183 	outline = destline;
184 	outcol = destcol;
185 }
186 
187 /*
188  * Move (slowly) to destination.
189  * Hard thing here is using home cursor on really deficient terminals.
190  * Otherwise just use cursor motions, hacking use of tabs and overtabbing
191  * and backspace.
192  *
193  * XXX this needs to be revisited for wide characters since we may output
194  * XXX more than one byte for a character.
195  */
196 
197 static int plodcnt, plodflg;
198 
199 static int
200 plodput(int c)
201 {
202 	if (plodflg)
203 		--plodcnt;
204 	else
205 		__cputchar(c);
206 	return 0;
207 }
208 
209 static int
210 plod(int cnt, int in_refresh)
211 {
212 	int	 i, j, k, soutcol, soutline;
213 
214 #ifdef DEBUG
215 	__CTRACE(__CTRACE_OUTPUT, "plod: cnt=%d, in_refresh=%d\n",
216 	    cnt, in_refresh);
217 #endif /* DEBUG */
218 	plodcnt = plodflg = cnt;
219 	soutcol = outcol;
220 	soutline = outline;
221 
222 	/*
223 	 * Consider homing and moving down/right from there, vs. moving
224 	 * directly with local motions to the right spot.
225 	 */
226 	if (cursor_home) {
227 		/*
228 		 * i is the cost to home and tab/space to the right to get to
229 		 * the proper column.  This assumes nd space costs 1 char.  So
230 		 * i + destcol is cost of motion with home.
231 		 */
232 		if (__GT)
233 			i = (destcol / HARDTABS) + (destcol % HARDTABS);
234 		else
235 			i = destcol;
236 
237 		/* j is cost to move locally without homing. */
238 		if (destcol >= outcol) {	/* if motion is to the right */
239 			j = destcol / HARDTABS - outcol / HARDTABS;
240 			if (__GT && j)
241 				j += destcol % HARDTABS;
242 			else
243 				j = destcol - outcol;
244 		} else
245 			/* leftward motion only works if we can backspace. */
246 			if (outcol - destcol <= i)
247 				/* Cheaper to backspace. */
248 				i = j = outcol - destcol;
249 			else
250 				/* Impossibly expensive. */
251 				j = i + 1;
252 
253 		/* k is the absolute value of vertical distance. */
254 		k = outline - destline;
255 		if (k < 0)
256 			k = -k;
257 		j += k;
258 
259 		/* Decision.  We may not have a choice if no up. */
260 		if (i + destline < j || (!cursor_up && destline < outline)) {
261 			/*
262 			 * Cheaper to home.  Do it now and pretend it's a
263 			 * regular local motion.
264 			 */
265 			tputs(cursor_home, 0, plodput);
266 			outcol = outline = 0;
267 		} else
268 			if (cursor_to_ll) {
269 				/*
270 				 * Quickly consider homing down and moving from
271 				 * there.  Assume cost of ll is 2.
272 				 */
273 				k = (LINES - 1) - destline;
274 				if (i + k + 2 < j && (k <= 0 || cursor_up)) {
275 					tputs(cursor_to_ll, 0, plodput);
276 					outcol = 0;
277 					outline = LINES - 1;
278 				}
279 			}
280 	} else
281 		/* No home and no up means it's impossible. */
282 		if (!cursor_up && destline < outline)
283 			return (-1);
284 	if (__GT)
285 		i = destcol % HARDTABS + destcol / HARDTABS;
286 	else
287 		i = destcol;
288 #ifdef notdef
289 	if (back_tab && outcol > destcol &&
290 	    (j = (((outcol + 7) & ~7) - destcol - 1) >> 3)) {
291 		j *= (k = strlen(back_tab));
292 		if ((k += (destcol & 7)) > 4)
293 			j += 8 - (destcol & 7);
294 		else
295 			j += k;
296 	} else
297 #endif
298 		j = outcol - destcol;
299 
300 	/*
301 	 * If we will later need a \n which will turn into a \r\n by the
302 	 * system or the terminal, then don't bother to try to \r.
303 	 */
304 	if ((__NONL || !__pfast) && outline < destline)
305 		goto dontcr;
306 
307 	/*
308 	 * If the terminal will do a \r\n and there isn't room for it, then
309 	 * we can't afford a \r.
310 	 */
311 	if (!carriage_return && outline >= destline)
312 		goto dontcr;
313 
314 	/*
315 	 * If it will be cheaper, or if we can't back up, then send a return
316 	 * preliminarily.
317 	 */
318 	if (j > i + 1 || outcol > destcol) {
319 		/*
320 		 * BUG: this doesn't take the (possibly long) length of cr
321 		 * into account.
322 		 */
323 		if (carriage_return)
324 			tputs(carriage_return, 0, plodput);
325 		else
326 			plodput('\r');
327 		if (!carriage_return) {
328 			if (cursor_down)
329 				tputs(cursor_down, 0, plodput);
330 			else
331 				plodput('\n');
332 			outline++;
333 		}
334 		outcol = 0;
335 	}
336 dontcr:while (outline < destline) {
337 		outline++;
338 		if (cursor_down)
339 			tputs(cursor_down, 0, plodput);
340 		else
341 			plodput('\n');
342 		if (plodcnt < 0)
343 			goto out;
344 		if (__NONL || __pfast == 0)
345 			outcol = 0;
346 	}
347 	if (back_tab)
348 		k = (int) strlen(back_tab);
349 	while (outcol > destcol) {
350 		if (plodcnt < 0)
351 			goto out;
352 #ifdef notdef
353 		if (back_tab && outcol - destcol > k + 4) {
354 			tputs(back_tab, 0, plodput);
355 			outcol--;
356 			outcol &= ~7;
357 			continue;
358 		}
359 #endif
360 		outcol--;
361 		if (cursor_left)
362 			tputs(cursor_left, 0, plodput);
363 		else
364 			plodput('\b');
365 	}
366 	while (outline > destline) {
367 		outline--;
368 		tputs(cursor_up, 0, plodput);
369 		if (plodcnt < 0)
370 			goto out;
371 	}
372 	if (__GT && destcol - outcol > 1) {
373 		for (;;) {
374 			i = tabcol(outcol, HARDTABS);
375 			if (i > destcol)
376 				break;
377 			if (tab)
378 				tputs(tab, 0, plodput);
379 			else
380 				plodput('\t');
381 			outcol = i;
382 		}
383 		if (destcol - outcol > 4 && i < COLS) {
384 			if (tab)
385 				tputs(tab, 0, plodput);
386 			else
387 				plodput('\t');
388 			outcol = i;
389 			while (outcol > destcol) {
390 				outcol--;
391 				if (cursor_left)
392 					tputs(cursor_left, 0, plodput);
393 				else
394 					plodput('\b');
395 			}
396 		}
397 	}
398 	while (outcol < destcol) {
399 		/*
400 		 * Move one char to the right.  We don't use nd space because
401 		 * it's better to just print the char we are moving over.
402 		 */
403 		if (in_refresh)
404 			if (plodflg)	/* Avoid a complex calculation. */
405 				plodcnt--;
406 			else {
407 #ifndef HAVE_WCHAR
408 				i = curscr->alines[outline]->line[outcol].ch
409 				    & __CHARTEXT;
410 				if (curscr->alines[outline]->line[outcol].attr
411 				    == curscr->wattr)
412 					__cputchar(i);
413 #else
414 				if ((curscr->alines[outline]->line[outcol].attr
415 				    & WA_ATTRIBUTES)
416 				    == curscr->wattr) {
417 					switch (WCOL(curscr->alines[outline]->line[outcol])) {
418 					case 1:
419 						__cputwchar(curscr->alines[outline]->line[outcol].ch);
420 						__cursesi_putnsp(curscr->alines[outline]->line[outcol].nsp,
421 								outline,
422 								outcol);
423 #ifdef DEBUG
424 						__CTRACE(__CTRACE_OUTPUT,
425 						    "plod: (%d,%d)WCOL(%d), "
426 						    "putwchar(%x)\n",
427 						    outline, outcol,
428 						    WCOL(curscr->alines[outline]->line[outcol]),
429 						    curscr->alines[outline]->line[outcol].ch);
430 #endif /* DEBUG */
431 					/*FALLTHROUGH*/
432 					case 0:
433 						break;
434 					default:
435 						goto nondes;
436 					}
437 				}
438 #endif /* HAVE_WCHAR */
439 				else
440 					goto nondes;
441 			}
442 		else
443 	nondes:	if (cursor_right)
444 			tputs(cursor_right, 0, plodput);
445 		else
446 			plodput(' ');
447 		outcol++;
448 		if (plodcnt < 0)
449 			goto out;
450 	}
451 
452 out:	if (plodflg) {
453 		outcol = soutcol;
454 		outline = soutline;
455 	}
456 #ifdef DEBUG
457 	__CTRACE(__CTRACE_OUTPUT, "plod: returns %d\n", plodcnt);
458 #endif /* DEBUG */
459 	return plodcnt;
460 }
461 
462 /*
463  * Return the column number that results from being in column col and
464  * hitting a tab, where tabs are set every ts columns.  Work right for
465  * the case where col > COLS, even if ts does not divide COLS.
466  */
467 static int
468 tabcol(int col, int ts)
469 {
470 	int	 offset;
471 
472 	if (col >= COLS) {
473 		offset = COLS * (col / COLS);
474 		col -= offset;
475 	} else
476 		offset = 0;
477 	return (col + ts - (col % ts) + offset);
478 }
479