-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_put.c
1310 lines (1206 loc) · 25.7 KB
/
ex_put.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This code contains changes by
* Gunnar Ritter, Freiburg i. Br., Germany, 2002. All rights reserved.
*
* Conditions 1, 2, and 4 and the no-warranty notice below apply
* to these changes.
*
*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
* Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Redistributions of source code and documentation must retain the
* above copyright notice, this list of conditions and the following
* disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed or owned by Caldera
* International, Inc.
* Neither the name of Caldera International, Inc. nor the names of
* other contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
* INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
* LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
#ifdef DOSCCS
static char sccsid[] = "@(#)ex_put.c 1.32 (gritter) 2/17/05";
#endif
#endif
/* from ex_put.c 7.9.1 (2.11BSD GTE) 12/9/94 */
#include "ex.h"
#include "ex_tty.h"
#include "ex_vis.h"
/*
* Terminal driving and line formatting routines.
* Basic motion optimizations are done here as well
* as formatting of lines (printing of control characters,
* line numbering and the like).
*/
/*
* The routines outchar, putchar and pline are actually
* variables, and these variables point at the current definitions
* of the routines. See the routine setflav.
* We sometimes make outchar be routines which catch the characters
* to be printed, e.g. if we want to see how long a line is.
* During open/visual, outchar and putchar will be set to
* routines in the file ex_vput.c (vputchar, vinschar, etc.).
*/
int (*Outchar)(int) = termchar;
int (*Putchar)(int) = normchar;
void (*Pline)(int) = normline;
int (*
setlist(int t))(int)
{
register int (*P)(int);
listf = t;
P = Putchar;
Putchar = t ? listchar : normchar;
return (P);
}
void (*
setnumb(int t))(int)
{
register void (*P)(int);
numberf = t;
P = Pline;
Pline = t ? numbline : normline;
return (P);
}
/*
* Format c for list mode; leave things in common
* with normal print mode to be done by normchar.
*/
int
listchar(int c)
{
if (c & MULTICOL) {
c &= ~MULTICOL;
if (c == 0)
return MULTICOL;
}
c &= (TRIM|QUOTE);
switch (c) {
case '\t':
case '\b':
c = ctlof(c);
outchar('^');
break;
case '\n':
break;
default:
if (c == ('\n' | QUOTE))
outchar('$');
if (c & QUOTE)
break;
#ifndef BIT8
if (c < ' ' && c != '\n')
outchar('^'), c = ctlof(c);
#else /* !BIT8 */
if (!printable(c) && c != '\n' || c == DELETE)
c = printof(c);
#endif
break;
}
return normchar(c);
}
/*
* Format c for printing. Handle funnies of upper case terminals
* and crocky hazeltines which don't have ~.
*/
int
normchar(register int c)
{
int u;
#ifdef UCVISUAL
register char *colp;
if (c == '~' && xHZ) {
normchar('\\');
c = '^';
}
#endif
if (c & MULTICOL) {
c &= ~MULTICOL;
if (c == 0)
return MULTICOL;
}
c &= (TRIM|QUOTE);
u = c & TRIM;
if (c & QUOTE) {
if (c == (' ' | QUOTE) || c == ('\b' | QUOTE))
/*EMPTY*/;
else if (c == QUOTE)
return c;
else
c &= TRIM;
}
#ifdef BIT8
else {
if (!printable(c) && (u != '\b' || !OS) &&
u != '\n' && u != '\t')
c = printof(u);
else {
c = u;
if (0)
/*EMPTY*/;
#else /* !BIT8 */
else if (c < ' ' && (c != '\b' || !OS) && c != '\n' && c != '\t')
putchar('^'), c = ctlof(c);
#endif /* !BIT8 */
#ifdef UCVISUAL
else if (UPPERCASE)
if (xisupper(c)) {
outchar('\\');
c = tolower(c);
} else {
colp = "({)}!|^~'`";
while (*colp++)
if (c == *colp++) {
outchar('\\');
c = colp[-2];
break;
}
}
#endif /* UCVISUAL */
#ifdef BIT8
}
}
#endif
outchar(c);
return c;
}
/*
* Given c at the beginning of a line, determine whether
* the printing of the line will erase or otherwise obliterate
* the prompt which was printed before. If it won't, do it now.
*/
void
slobber(int c)
{
shudclob = 0;
switch (c) {
case '\t':
if (Putchar == listchar)
return;
break;
default:
return;
case ' ':
case 0:
break;
}
if (OS)
return;
flush();
putch(' ');
if (BC)
tputs(BC, 0, putch);
else
putch('\b');
}
/*
* Print a line with a number.
*/
void
numbline(int i)
{
if (shudclob)
slobber(' ');
printf("%6d ", i);
normline(0);
}
/*
* Normal line output, no numbering.
*/
/*ARGSUSED*/
void
normline(int unused)
{
register char *cp;
int c, n;
if (shudclob)
slobber(linebuf[0]);
/* pdp-11 doprnt is not reentrant so can't use "printf" here
in case we are tracing */
cp = linebuf;
vcolbp = cp;
while (*cp) {
vcolbp = cp;
nextc(c, cp, n);
cp += n;
putchar(c);
}
if (!inopen) {
putchar('\n' | QUOTE);
}
}
/*
* The output buffer is initialized with a useful error
* message so we don't have to keep it in data space.
*/
static char linb[66+MB_LEN_MAX];
char *linp = linb;
/*
* Phadnl records when we have already had a complete line ending with \n.
* If another line starts without a flush, and the terminal suggests it,
* we switch into -nl mode so that we can send lineffeeds to avoid
* a lot of spacing.
*/
static bool phadnl;
/*
* Indirect to current definition of putchar.
*/
int
putchar(int c)
{
if (c & MULTICOL) {
c &= ~MULTICOL;
if (c == 0)
return MULTICOL;
}
(*Putchar)(c);
return c;
}
/*
* Termchar routine for command mode.
* Watch for possible switching to -nl mode.
* Otherwise flush into next level of buffering when
* small buffer fills or at a newline.
*/
int
termchar(int c)
{
if (pfast == 0 && phadnl)
pstart();
if (c == '\n')
phadnl = 1;
else if (linp >= &linb[63])
flush1();
#ifdef MB
if (mb_cur_max > 1 && c & ~(wchar_t)0177) {
char mb[MB_LEN_MAX];
int i, n;
n = wctomb(mb, c&TRIM);
for (i = 0; i < n; i++)
*linp++ = mb[i];
} else
#endif /* MB */
*linp++ = c;
if (linp >= &linb[63]) {
fgoto();
flush1();
}
return c;
}
void
flush2(void)
{
fgoto();
flusho();
pstop();
}
void
flush(void)
{
flush1();
flush2();
}
/*
* Flush from small line buffer into output buffer.
* Work here is destroying motion into positions, and then
* letting fgoto do the optimized motion.
*/
void
flush1(void)
{
register char *lp;
int c, n;
*linp = 0;
lp = linb;
while (*lp) {
nextc(c, lp, n);
lp += n;
switch (c) {
case '\r':
destline += destcol / TCOLUMNS;
destcol = 0;
continue;
case '\b':
if (destcol)
destcol--;
continue;
case ' ':
destcol++;
continue;
case '\t':
destcol += value(TABSTOP) - destcol % value(TABSTOP);
continue;
case '\n':
destline += destcol / TCOLUMNS + 1;
if (destcol != 0 && destcol % TCOLUMNS == 0)
destline--;
destcol = 0;
continue;
default:
fgoto();
for (;;) {
if (AM == 0 && outcol == TCOLUMNS)
fgoto();
c &= TRIM;
putch(c);
if (c == '\b') {
outcol--;
destcol--;
#ifndef BIT8
} else if ( c >= ' ' && c != DELETE) {
#else
} else if (printable(c)) {
#endif
#ifdef MB
n = colsc(c);
outcol += n;
destcol += n;
#else /* !MB */
outcol++;
destcol++;
#endif /* !MB */
if (XN && outcol % TCOLUMNS == 0)
putch('\r'), putch('\n');
}
nextc(c, lp, n);
lp += n;
#ifndef BIT8
if (c <= ' ')
#else
if (c == ' ' || !printable(c))
#endif
break;
}
--lp;
continue;
}
}
linp = linb;
}
static int plodcnt, plodflg;
/*
* Move (slowly) to destination.
* Hard thing here is using home cursor on really deficient terminals.
* Otherwise just use cursor motions, hacking use of tabs and overtabbing
* and backspace.
*/
int
plodput(int c)
{
if (plodflg)
plodcnt--;
else
putch(c);
return c;
}
int
plod(int cnt)
{
register int i, j, k = 0;
register int soutcol, soutline;
plodcnt = plodflg = cnt;
soutcol = outcol;
soutline = outline;
/*
* Consider homing and moving down/right from there, vs moving
* directly with local motions to the right spot.
*/
if (HO) {
/*
* i is the cost to home and tab/space to the right to
* get to the proper column. This assumes ND space costs
* 1 char. So i+destcol is cost of motion with home.
*/
if (GT)
i = (destcol / value(HARDTABS)) + (destcol % value(HARDTABS));
else
i = destcol;
/*
* j is cost to move locally without homing
*/
if (destcol >= outcol) { /* if motion is to the right */
j = destcol / value(HARDTABS) - outcol / value(HARDTABS);
if (GT && j)
j += destcol % value(HARDTABS);
else
j = destcol - outcol;
} else
/* leftward motion only works if we can backspace. */
if (outcol - destcol <= i && (BS || BC))
i = j = outcol - destcol; /* cheaper to backspace */
else
j = i + 1; /* impossibly expensive */
/* k is the absolute value of vertical distance */
k = outline - destline;
if (k < 0)
k = -k;
j += k;
/*
* Decision. We may not have a choice if no UP.
*/
if (i + destline < j || (!UP && destline < outline)) {
/*
* Cheaper to home. Do it now and pretend it's a
* regular local motion.
*/
tputs(HO, 0, plodput);
outcol = outline = 0;
} else if (LL) {
/*
* Quickly consider homing down and moving from there.
* Assume cost of LL is 2.
*/
k = (TLINES - 1) - destline;
if (i + k + 2 < j && (k<=0 || UP)) {
tputs(LL, 0, plodput);
outcol = 0;
outline = TLINES - 1;
}
}
} else
/*
* No home and no up means it's impossible, so we return an
* incredibly big number to make cursor motion win out.
*/
if (!UP && destline < outline)
return (500);
if (GT)
i = destcol % value(HARDTABS)
+ destcol / value(HARDTABS);
else
i = destcol;
/*
if (BT && outcol > destcol && (j = (((outcol+7) & ~7) - destcol - 1) >> 3)) {
j *= (k = strlen(BT));
if ((k += (destcol&7)) > 4)
j += 8 - (destcol&7);
else
j += k;
} else
*/
j = outcol - destcol;
/*
* If we will later need a \n which will turn into a \r\n by
* the system or the terminal, then don't bother to try to \r.
*/
if ((NONL || !pfast) && outline < destline)
goto dontcr;
/*
* If the terminal will do a \r\n and there isn't room for it,
* then we can't afford a \r.
*/
if (NC && outline >= destline)
goto dontcr;
/*
* If it will be cheaper, or if we can't back up, then send
* a return preliminarily.
*/
if (j > i + 1 || outcol > destcol && !BS && !BC) {
/*
* BUG: this doesn't take the (possibly long) length
* of xCR into account.
*/
if (ospeed != B0) {
if (xCR)
tputs(xCR, 0, plodput);
else
plodput('\r');
}
if (NC) {
if (xNL)
tputs(xNL, 0, plodput);
else
plodput('\n');
outline++;
}
outcol = 0;
}
dontcr:
/* Move down, if necessary, until we are at the desired line */
while (outline < destline) {
j = destline - outline;
if (j > costDP && DOWN_PARM) {
/* Win big on Tek 4025 */
tputs(tgoto(DOWN_PARM, 0, j), j, plodput);
outline += j;
}
else {
outline++;
if (xNL && pfast)
tputs(xNL, 0, plodput);
else
plodput('\n');
}
if (plodcnt < 0)
goto out;
if (NONL || pfast == 0)
outcol = 0;
}
if (BT)
k = strlen(BT); /* should probably be cost(BT) and moved out */
/* Move left, if necessary, to desired column */
while (outcol > destcol) {
if (plodcnt < 0)
goto out;
if (BT && !insmode && outcol - destcol > 4+k) {
tputs(BT, 0, plodput);
outcol--;
outcol -= outcol % value(HARDTABS); /* outcol &= ~7; */
continue;
}
j = outcol - destcol;
if (j > costLP && LEFT_PARM) {
tputs(tgoto(LEFT_PARM, 0, j), j, plodput);
outcol -= j;
}
else {
outcol--;
if (BC)
tputs(BC, 0, plodput);
else
plodput('\b');
}
}
/* Move up, if necessary, to desired row */
while (outline > destline) {
j = outline - destline;
if (UP_PARM && j > 1) {
/* Win big on Tek 4025 */
tputs(tgoto(UP_PARM, 0, j), j, plodput);
outline -= j;
}
else {
outline--;
tputs(UP, 0, plodput);
}
if (plodcnt < 0)
goto out;
}
/*
* Now move to the right, if necessary. We first tab to
* as close as we can get.
*/
if (GT && !insmode && destcol - outcol > 1) {
/* tab to right as far as possible without passing col */
for (;;) {
i = tabcol(outcol, value(HARDTABS));
if (i > destcol)
break;
if (TA)
tputs(TA, 0, plodput);
else
plodput('\t');
outcol = i;
}
/* consider another tab and then some backspaces */
if (destcol - outcol > 4 && i < TCOLUMNS && (BC || BS)) {
if (TA)
tputs(TA, 0, plodput);
else
plodput('\t');
outcol = i;
/*
* Back up. Don't worry about LEFT_PARM because
* it's never more than 4 spaces anyway.
*/
while (outcol > destcol) {
outcol--;
if (BC)
tputs(BC, 0, plodput);
else
plodput('\b');
}
}
}
/*
* We've tabbed as much as possible. If we still need to go
* further (not exact or can't tab) space over. This is a
* very common case when moving to the right with space.
*/
while (outcol < destcol) {
j = destcol - outcol;
if (j > costRP && RIGHT_PARM) {
/*
* This probably happens rarely, if at all.
* It seems mainly useful for ANSI terminals
* with no hardware tabs, and I don't know
* of any such terminal at the moment.
*/
tputs(tgoto(RIGHT_PARM, 0, j), j, plodput);
outcol += j;
}
else {
/*
* move one char to the right. We don't use ND space
* because it's better to just print the char we are
* moving over. There are various exceptions, however.
* If !inopen, vtube contains garbage. If the char is
* a null or a tab we want to print a space. Other
* random chars we use space for instead, too.
*/
if (!inopen || vtube[outline]==NULL ||
#ifndef BIT8
((i=vtube[outline][outcol]) < ' ')
#else
((i=vtube[outline][outcol]) == 0)
|| (i!=MULTICOL && !printable(i&~INVBIT&~MULTICOL))
#endif
)
i = ' ';
if((i & (QUOTE|INVBIT)) == QUOTE) /* mjm: no sign
extension on 3B */
i = ' ';
if ((insmode || i == MULTICOL) && ND)
tputs(ND, 0, plodput);
else if (i == MULTICOL) {
if (BS && BC)
tputs(BC, 0, plodput);
else
plodput('\b');
plodput(vtube[outline][outcol-1]);
} else
plodput(i);
outcol += i == MULTICOL ? 1 : colsc(i & ~MULTICOL);
}
if (plodcnt < 0)
goto out;
}
out:
if (plodflg) {
outcol = soutcol;
outline = soutline;
}
return(plodcnt);
}
/*
* Sync the position of the output cursor.
* Most work here is rounding for terminal boundaries getting the
* column position implied by wraparound or the lack thereof and
* rolling up the screen to get destline on the screen.
*/
void
fgoto(void)
{
register int l, c;
if (destcol > TCOLUMNS - 1) {
destline += destcol / TCOLUMNS;
destcol %= TCOLUMNS;
}
if (outcol > TCOLUMNS - 1) {
l = (outcol + 1) / TCOLUMNS;
outline += l;
outcol %= TCOLUMNS;
if (AM == 0) {
while (l > 0) {
if (pfast && ospeed != B0)
if (xCR)
tputs(xCR, 0, putch);
else
putch('\r');
if (xNL)
tputs(xNL, 0, putch);
else
putch('\n');
l--;
}
outcol = 0;
}
if (outline > TLINES - 1) {
destline -= outline - (TLINES - 1);
outline = TLINES - 1;
}
}
if (destline > TLINES - 1) {
l = destline;
destline = TLINES - 1;
if (outline < TLINES - 1) {
c = destcol;
if (pfast == 0 && (!CA || holdcm))
destcol = 0;
fgoto();
destcol = c;
}
while (l > TLINES - 1) {
/*
* The following linefeed (or simulation thereof)
* is supposed to scroll up the screen, since we
* are on the bottom line. We make the assumption
* that linefeed will scroll. If ns is in the
* capability list this won't work. We should
* probably have an sc capability but sf will
* generally take the place if it works.
*
* Superbee glitch: in the middle of the screen we
* have to use esc B (down) because linefeed screws up
* in "Efficient Paging" (what a joke) mode (which is
* essential in some SB's because CRLF mode puts garbage
* in at end of memory), but you must use linefeed to
* scroll since down arrow won't go past memory end.
* I turned this off after recieving Paul Eggert's
* Superbee description which wins better.
*/
if (xNL /* && !XB */ && pfast)
tputs(xNL, 0, putch);
else
putch('\n');
l--;
if (pfast == 0)
outcol = 0;
}
}
if (destline < outline && !(CA && !holdcm || UP != NOSTR))
destline = outline;
if (CA && !holdcm)
if (plod(costCM) > 0)
plod(0);
else
tputs(tgoto(CM, destcol, destline), 0, putch);
else
plod(0);
outline = destline;
outcol = destcol;
}
/*
* Tab to column col by flushing and then setting destcol.
* Used by "set all".
*/
void
tab(int col)
{
flush1();
destcol = col;
}
/*
* An input line arrived.
* Calculate new (approximate) screen line position.
* Approximate because kill character echoes newline with
* no feedback and also because of long input lines.
*/
void
noteinp(void)
{
outline++;
if (outline > TLINES - 1)
outline = TLINES - 1;
destline = outline;
destcol = outcol = 0;
}
/*
* Something weird just happened and we
* lost track of whats happening out there.
* Since we cant, in general, read where we are
* we just reset to some known state.
* On cursor addressible terminals setting to unknown
* will force a cursor address soon.
*/
void
termreset(void)
{
endim();
if (TI) /* otherwise it flushes anyway, and 'set tty=dumb' vomits */
putpad(TI); /*adb change -- emit terminal initial sequence */
destcol = 0;
destline = TLINES - 1;
if (CA) {
outcol = UKCOL;
outline = UKCOL;
} else {
outcol = destcol;
outline = destline;
}
}
/*
* Low level buffering, with the ability to drain
* buffered output without printing it.
*/
char *obp = obuf;
void
draino(void)
{
obp = obuf;
}
void
flusho(void)
{
if (obp != obuf) {
write(1, obuf, obp - obuf);
obp = obuf;
}
}
void
putnl(void)
{
putchar('\n');
}
void
putS(char *cp)
{
if (cp == NULL)
return;
while (*cp)
putch(*cp++);
}
int
putch(int c)
{
#ifdef OLD3BTTY /* mjm */
if(c == '\n') /* mjm: Fake "\n\r" for '\n' til fix in 3B firmware */
putch('\r'); /* mjm: vi does "stty -icanon" => -onlcr !! */
#endif
if (c & MULTICOL) {
c &= ~MULTICOL;
if (c == 0)
return MULTICOL;
}
c &= ~INVBIT; /* strip '~' | INVBIT multicolumn filler */
#ifdef MB
if (mb_cur_max > 1 && c & ~(wchar_t)0177) {
char mb[MB_LEN_MAX];
int i, n;
n = wctomb(mb, c&TRIM);
for (i = 0; i < n; i++) {
*obp++ = mb[i];
if (obp >= &obuf[sizeof obuf])
flusho();
}
} else
#endif /* MB */
*obp++ = c & TRIM;
if (obp >= &obuf[sizeof obuf])
flusho();
return c;
}
/*
* Miscellaneous routines related to output.
*/