Subversion Repositories planix.SVN

Rev

Rev 118 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
105 7u83 1
/*
2
 * Copyright (c) 1980 Regents of the University of California.
3
 * All rights reserved.  The Berkeley software License Agreement
4
 * specifies the terms and conditions for redistribution.
5
 */
6
 
7
 
8
#include "ex.h"
9
#include "ex_tty.h"
10
#include "ex_vis.h"
11
 
118 7u83 12
 
13
#include "mach.h"
14
 
105 7u83 15
/*
16
 * Terminal driving and line formatting routines.
17
 * Basic motion optimizations are done here as well
18
 * as formatting of lines (printing of control characters,
19
 * line numbering and the like).
20
 */
21
 
22
/*
23
 * The routines outchar, putchar and pline are actually
24
 * variables, and these variables point at the current definitions
25
 * of the routines.  See the routine setflav.
26
 * We sometimes make outchar be routines which catch the characters
27
 * to be printed, e.g. if we want to see how long a line is.
28
 * During open/visual, outchar and putchar will be set to
29
 * routines in the file ex_vput.c (vputchar, vinschar, etc.).
30
 */
115 7u83 31
int	(*Outchar)() = termchar;
32
int	(*Putchar)() = normchar;
105 7u83 33
int	(*Pline)() = normline;
34
 
115 7u83 35
int (*
105 7u83 36
setlist(t))()
37
	bool t;
38
{
115 7u83 39
	register int (*P)();
105 7u83 40
 
41
	listf = t;
42
	P = Putchar;
43
	Putchar = t ? listchar : normchar;
44
	return (P);
45
}
46
 
47
int (*
48
setnumb(t))()
49
	bool t;
50
{
51
	register int (*P)();
52
 
53
	numberf = t;
54
	P = Pline;
55
	Pline = t ? numbline : normline;
56
	return (P);
57
}
58
 
59
/*
60
 * Format c for list mode; leave things in common
61
 * with normal print mode to be done by normchar.
62
 */
115 7u83 63
listchar(c)
64
	register short c;
105 7u83 65
{
66
 
67
	c &= (TRIM|QUOTE);
68
	switch (c) {
69
 
70
	case '\t':
71
	case '\b':
72
		outchar('^');
73
		c = ctlof(c);
74
		break;
75
 
76
	case '\n':
77
		break;
78
 
79
	case '\n' | QUOTE:
80
		outchar('$');
81
		break;
82
 
83
	default:
84
		if (c & QUOTE)
85
			break;
86
		if (c < ' ' && c != '\n' || c == DELETE)
87
			outchar('^'), c = ctlof(c);
88
		break;
89
	}
90
	normchar(c);
91
}
92
 
93
/*
94
 * Format c for printing.
95
 */
115 7u83 96
normchar(c)
97
	register short c;
105 7u83 98
{
99
	register char *colp;
100
 
101
	c &= (TRIM|QUOTE);
102
	if (c & QUOTE)
103
		switch (c) {
104
 
105
		case ' ' | QUOTE:
106
		case '\b' | QUOTE:
107
			break;
108
 
109
		case QUOTE:
110
			return;
111
 
112
		default:
113
#ifdef	ISO
114
			if (niso(c))
115
#endif
116
			c &= TRIM;
117
		}
118
	else if (c < ' ' && (c != '\b' || !OS) && c != '\n' && c != '\t' || c == DELETE)
119
		putchar('^'), c = ctlof(c);
120
	outchar(c);
121
}
122
 
123
/*
124
 * Print a line with a number.
125
 */
126
numbline(i)
127
	int i;
128
{
129
 
130
	if (shudclob)
131
		slobber(' ');
132
	printf("%6d  ", i);
133
	normline();
134
}
135
 
136
/*
137
 * Normal line output, no numbering.
138
 */
139
normline()
140
{
141
	register char *cp;
142
 
143
	if (shudclob)
144
		slobber(linebuf[0]);
145
	/* pdp-11 doprnt is not reentrant so can't use "printf" here
146
	   in case we are tracing */
147
	for (cp = linebuf; *cp;)
148
		putchar(*cp++);
149
	if (!inopen)
150
		putchar('\n' | QUOTE);
151
}
152
 
153
/*
154
 * Given c at the beginning of a line, determine whether
155
 * the printing of the line will erase or otherwise obliterate
156
 * the prompt which was printed before.  If it won't, do it now.
157
 */
158
slobber(c)
159
	int c;
160
{
161
 
162
	shudclob = 0;
163
	switch (c) {
164
 
165
	case '\t':
166
		if (Putchar == listchar)
167
			return;
168
		break;
169
 
170
	default:
171
		return;
172
 
173
	case ' ':
174
	case 0:
175
		break;
176
	}
177
	if (OS)
178
		return;
179
	flush();
180
	putch(' ');
181
	if (BC)
182
		tputs(BC, 0, putch);
183
	else
184
		putch('\b');
185
}
186
 
187
/*
188
 * The output buffer is initialized with a useful error
189
 * message so we don't have to keep it in data space.
190
 */
191
static	char linb[66];
192
char *linp = linb;
193
 
194
/*
195
 * Phadnl records when we have already had a complete line ending with \n.
196
 * If another line starts without a flush, and the terminal suggests it,
197
 * we switch into -nl mode so that we can send lineffeeds to avoid
198
 * a lot of spacing.
199
 */
200
static	bool phadnl;
201
 
202
/*
203
 * Indirect to current definition of putchar.
204
 */
205
putchar(c)
206
	int c;
207
{
208
 
209
	(*Putchar)(c);
210
}
211
 
212
/*
213
 * Termchar routine for command mode.
214
 * Watch for possible switching to -nl mode.
215
 * Otherwise flush into next level of buffering when
216
 * small buffer fills or at a newline.
217
 */
218
termchar(c)
219
	int c;
220
{
221
 
222
	if (pfast == 0 && phadnl)
223
		pstart();
224
	if (c == '\n')
225
		phadnl = 1;
226
	else if (linp >= &linb[63])
227
		flush1();
228
	*linp++ = c;
229
	if (linp >= &linb[63]) {
230
		fgoto();
231
		flush1();
232
	}
233
}
234
 
116 7u83 235
void 
236
flush(void)
105 7u83 237
{
238
 
239
	flush1();
240
	flush2();
118 7u83 241
 
242
	fflush(stdout);
105 7u83 243
}
244
 
245
/*
246
 * Flush from small line buffer into output buffer.
247
 * Work here is destroying motion into positions, and then
248
 * letting fgoto do the optimized motion.
249
 */
250
flush1()
251
{
252
	register char *lp;
253
	register short c;
254
 
255
	*linp = 0;
256
	lp = linb;
257
	while (*lp)
258
		switch (c = *lp++) {
259
 
260
		case '\r':
261
			destline += destcol / COLUMNS;
262
			destcol = 0;
263
			continue;
264
 
265
		case '\b':
266
			if (destcol)
267
				destcol--;
268
			continue;
269
 
270
		case ' ':
271
			destcol++;
272
			continue;
273
 
274
		case '\t':
275
			destcol += value(TABSTOP) - destcol % value(TABSTOP);
276
			continue;
277
 
278
		case '\n':
279
			destline += destcol / COLUMNS + 1;
280
			if (destcol != 0 && destcol % COLUMNS == 0)
281
				destline--;
282
			destcol = 0;
283
			continue;
284
 
285
		default:
286
			fgoto();
287
			for (;;) {
288
				if (AM == 0 && outcol == COLUMNS)
289
					fgoto();
290
#ifdef	ISO
291
				if (niso(c))
292
#endif
293
				c &= TRIM;
294
				putch(c);
295
				if (c == '\b') {
296
					outcol--;
297
					destcol--;
298
				} else if (
299
#ifdef	ISO
300
					!(c & QUOTE) &&
301
#endif
302
						c >= ' ' && c != DELETE) {
303
					outcol++;
304
					destcol++;
305
					if (XN && outcol % COLUMNS == 0)
306
						putch('\r'), putch('\n');
307
				}
308
				c = *lp++;
309
				if (
310
#ifdef ISO
311
					!(c & 0200) &&
312
#endif
313
					c <= ' ')
314
					break;
315
			}
316
			--lp;
317
			continue;
318
		}
319
	linp = linb;
320
}
321
 
322
flush2()
323
{
324
 
325
	fgoto();
326
	flusho();
327
	pstop();
328
}
329
 
330
/*
331
 * Sync the position of the output cursor.
332
 * Most work here is rounding for terminal boundaries getting the
333
 * column position implied by wraparound or the lack thereof and
334
 * rolling up the screen to get destline on the screen.
335
 */
336
fgoto()
337
{
338
	register int l, c;
339
 
340
	if (destcol > COLUMNS - 1) {
341
		destline += destcol / COLUMNS;
342
		destcol %= COLUMNS;
343
	}
344
	if (outcol > COLUMNS - 1) {
345
		l = (outcol + 1) / COLUMNS;
346
		outline += l;
347
		outcol %= COLUMNS;
348
		if (AM == 0) {
349
			while (l > 0) {
350
				if (pfast)
351
					if (xCR)
352
						tputs(xCR, 0, putch);
353
					else
354
						putch('\r');
355
				if (xNL)
356
					tputs(xNL, 0, putch);
357
				else
358
					putch('\n');
359
				l--;
360
			}
361
			outcol = 0;
362
		}
363
		if (outline > LINES - 1) {
364
			destline -= outline - (LINES - 1);
365
			outline = LINES - 1;
366
		}
367
	}
368
	if (destline > LINES - 1) {
369
		l = destline;
370
		destline = LINES - 1;
371
		if (outline < LINES - 1) {
372
			c = destcol;
373
			if (pfast == 0 && (!CA || holdcm))
374
				destcol = 0;
375
			fgoto();
376
			destcol = c;
377
		}
378
		while (l > LINES - 1) {
379
			/*
380
			 * The following linefeed (or simulation thereof)
381
			 * is supposed to scroll up the screen, since we
382
			 * are on the bottom line.  We make the assumption
383
			 * that linefeed will scroll.  If ns is in the
384
			 * capability list this won't work.  We should
385
			 * probably have an sc capability but sf will
386
			 * generally take the place if it works.
387
			 *
388
			 * Superbee glitch:  in the middle of the screen we
389
			 * have to use esc B (down) because linefeed screws up
390
			 * in "Efficient Paging" (what a joke) mode (which is
391
			 * essential in some SB's because CRLF mode puts garbage
392
			 * in at end of memory), but you must use linefeed to
393
			 * scroll since down arrow won't go past memory end.
394
			 * I turned this off after recieving Paul Eggert's
395
			 * Superbee description which wins better.
396
			 */
397
			if (xNL /* && !XB */ && pfast)
398
				tputs(xNL, 0, putch);
399
			else
400
				putch('\n');
401
			l--;
402
			if (pfast == 0)
403
				outcol = 0;
404
		}
405
	}
406
	if (destline < outline && !(CA && !holdcm || UP != NOSTR))
407
		destline = outline;
408
	if (CA && !holdcm)
409
		if (plod(costCM) > 0)
410
			plod(0);
411
		else
412
			tputs(tgoto(CM, destcol, destline), 0, putch);
413
	else
414
		plod(0);
415
	outline = destline;
416
	outcol = destcol;
417
}
418
 
419
/*
420
 * Tab to column col by flushing and then setting destcol.
421
 * Used by "set all".
422
 */
423
tab(col)
424
	int col;
425
{
426
 
427
	flush1();
428
	destcol = col;
429
}
430
 
431
/*
432
 * Move (slowly) to destination.
433
 * Hard thing here is using home cursor on really deficient terminals.
434
 * Otherwise just use cursor motions, hacking use of tabs and overtabbing
435
 * and backspace.
436
 */
437
 
438
static int plodcnt, plodflg;
439
 
440
plodput(c)
441
{
442
 
443
	if (plodflg)
444
		plodcnt--;
445
	else
446
		putch(c);
447
}
448
 
449
plod(cnt)
450
{
451
	register int i, j, k;
452
	register int soutcol, soutline;
453
 
454
	plodcnt = plodflg = cnt;
455
	soutcol = outcol;
456
	soutline = outline;
457
	/*
458
	 * Consider homing and moving down/right from there, vs moving
459
	 * directly with local motions to the right spot.
460
	 */
461
	if (HO) {
462
		/*
463
		 * i is the cost to home and tab/space to the right to
464
		 * get to the proper column.  This assumes ND space costs
465
		 * 1 char.  So i+destcol is cost of motion with home.
466
		 */
467
		if (GT)
468
			i = (destcol / value(HARDTABS)) + (destcol % value(HARDTABS));
469
		else
470
			i = destcol;
471
		/*
472
		 * j is cost to move locally without homing
473
		 */
474
		if (destcol >= outcol) {	/* if motion is to the right */
475
			j = destcol / value(HARDTABS) - outcol / value(HARDTABS);
476
			if (GT && j)
477
				j += destcol % value(HARDTABS);
478
			else
479
				j = destcol - outcol;
480
		} else
481
			/* leftward motion only works if we can backspace. */
482
			if (outcol - destcol <= i && (BS || BC))
483
				i = j = outcol - destcol; /* cheaper to backspace */
484
			else
485
				j = i + 1; /* impossibly expensive */
486
 
487
		/* k is the absolute value of vertical distance */
488
		k = outline - destline;
489
		if (k < 0)
490
			k = -k;
491
		j += k;
492
 
493
		/*
494
		 * Decision.  We may not have a choice if no UP.
495
		 */
496
		if (i + destline < j || (!UP && destline < outline)) {
497
			/*
498
			 * Cheaper to home.  Do it now and pretend it's a
499
			 * regular local motion.
500
			 */
501
			tputs(HO, 0, plodput);
502
			outcol = outline = 0;
503
		} else if (LL) {
504
			/*
505
			 * Quickly consider homing down and moving from there.
506
			 * Assume cost of LL is 2.
507
			 */
508
			k = (LINES - 1) - destline;
509
			if (i + k + 2 < j && (k<=0 || UP)) {
510
				tputs(LL, 0, plodput);
511
				outcol = 0;
512
				outline = LINES - 1;
513
			}
514
		}
515
	} else
516
	/*
517
	 * No home and no up means it's impossible, so we return an
518
	 * incredibly big number to make cursor motion win out.
519
	 */
520
		if (!UP && destline < outline)
521
			return (500);
522
	if (GT)
523
		i = destcol % value(HARDTABS)
524
		    + destcol / value(HARDTABS);
525
	else
526
		i = destcol;
527
/*
528
	if (BT && outcol > destcol && (j = (((outcol+7) & ~7) - destcol - 1) >> 3)) {
529
		j *= (k = strlen(BT));
530
		if ((k += (destcol&7)) > 4)
531
			j += 8 - (destcol&7);
532
		else
533
			j += k;
534
	} else
535
*/
536
		j = outcol - destcol;
537
	/*
538
	 * If we will later need a \n which will turn into a \r\n by
539
	 * the system or the terminal, then don't bother to try to \r.
540
	 */
541
	if ((NONL || !pfast) && outline < destline)
542
		goto dontcr;
543
	/*
544
	 * If the terminal will do a \r\n and there isn't room for it,
545
	 * then we can't afford a \r.
546
	 */
547
	if (NC && outline >= destline)
548
		goto dontcr;
549
	/*
550
	 * If it will be cheaper, or if we can't back up, then send
551
	 * a return preliminarily.
552
	 */
553
	if (j > i + 1 || outcol > destcol && !BS && !BC) {
554
		/*
555
		 * BUG: this doesn't take the (possibly long) length
556
		 * of xCR into account.
557
		 */
558
		if (xCR)
559
			tputs(xCR, 0, plodput);
560
		else
561
			plodput('\r');
562
		if (NC) {
563
			if (xNL)
564
				tputs(xNL, 0, plodput);
565
			else
566
				plodput('\n');
567
			outline++;
568
		}
569
		outcol = 0;
570
	}
571
dontcr:
572
	/* Move down, if necessary, until we are at the desired line */
573
	while (outline < destline) {
574
		j = destline - outline;
575
		if (j > costDP && DOWN_PARM) {
576
			/* Win big on Tek 4025 */
577
			tputs(tgoto(DOWN_PARM, 0, j), j, plodput);
578
			outline += j;
579
		}
580
		else {
581
			outline++;
582
			if (xNL && pfast)
583
				tputs(xNL, 0, plodput);
584
			else
585
				plodput('\n');
586
		}
587
		if (plodcnt < 0)
588
			goto out;
589
		if (NONL || pfast == 0)
590
			outcol = 0;
591
	}
592
	if (BT)
593
		k = strlen(BT);	/* should probably be cost(BT) and moved out */
594
	/* Move left, if necessary, to desired column */
595
	while (outcol > destcol) {
596
		if (plodcnt < 0)
597
			goto out;
598
		if (BT && !insmode && outcol - destcol > 4+k) {
599
			tputs(BT, 0, plodput);
600
			outcol--;
601
			outcol -= outcol % value(HARDTABS); /* outcol &= ~7; */
602
			continue;
603
		}
604
		j = outcol - destcol;
605
		if (j > costLP && LEFT_PARM) {
606
			tputs(tgoto(LEFT_PARM, 0, j), j, plodput);
607
			outcol -= j;
608
		}
609
		else {
610
			outcol--;
611
			if (BC)
612
				tputs(BC, 0, plodput);
613
			else
614
				plodput('\b');
615
		}
616
	}
617
	/* Move up, if necessary, to desired row */
618
	while (outline > destline) {
619
		j = outline - destline;
620
		if (UP_PARM && j > 1) {
621
			/* Win big on Tek 4025 */
622
			tputs(tgoto(UP_PARM, 0, j), j, plodput);
623
			outline -= j;
624
		}
625
		else {
626
			outline--;
627
			tputs(UP, 0, plodput);
628
		}
629
		if (plodcnt < 0)
630
			goto out;
631
	}
632
	/*
633
	 * Now move to the right, if necessary.  We first tab to
634
	 * as close as we can get.
635
	 */
636
	if (GT && !insmode && destcol - outcol > 1) {
637
		/* tab to right as far as possible without passing col */
638
		for (;;) {
639
			i = tabcol(outcol, value(HARDTABS));
640
			if (i > destcol)
641
				break;
642
			if (TA)
643
				tputs(TA, 0, plodput);
644
			else
645
				plodput('\t');
646
			outcol = i;
647
		}
648
		/* consider another tab and then some backspaces */
649
		if (destcol - outcol > 4 && i < COLUMNS && (BC || BS)) {
650
			if (TA)
651
				tputs(TA, 0, plodput);
652
			else
653
				plodput('\t');
654
			outcol = i;
655
			/*
656
			 * Back up.  Don't worry about LEFT_PARM because
657
			 * it's never more than 4 spaces anyway.
658
			 */
659
			while (outcol > destcol) {
660
				outcol--;
661
				if (BC)
662
					tputs(BC, 0, plodput);
663
				else
664
					plodput('\b');
665
			}
666
		}
667
	}
668
	/*
669
	 * We've tabbed as much as possible.  If we still need to go
670
	 * further (not exact or can't tab) space over.  This is a
671
	 * very common case when moving to the right with space.
672
	 */
673
	while (outcol < destcol) {
674
		j = destcol - outcol;
675
		if (j > costRP && RIGHT_PARM) {
676
			/*
677
			 * This probably happens rarely, if at all.
678
			 * It seems mainly useful for ANSI terminals
679
			 * with no hardware tabs, and I don't know
680
			 * of any such terminal at the moment.
681
			 */
682
			tputs(tgoto(RIGHT_PARM, 0, j), j, plodput);
683
			outcol += j;
684
		}
685
		else {
686
			/*
687
			 * move one char to the right.  We don't use ND space
688
			 * because it's better to just print the char we are
689
			 * moving over.  There are various exceptions, however.
690
			 * If !inopen, vtube contains garbage.  If the char is
691
			 * a null or a tab we want to print a space.  Other
692
			 * random chars we use space for instead, too.
693
			 */
694
			if (!inopen || vtube[outline]==NULL ||
695
				((i=vtube[outline][outcol]) < ' ')
696
#ifdef	ISO
697
					&& !(i & QUOTE)
698
#endif
699
					)
700
				i = ' ';
701
#ifndef	ISO
702
			if(i & QUOTE)	/* mjm: no sign extension on 3B */
703
#else
704
			if (niso(i))
705
#endif
706
				i = ' ';
707
			if (insmode && ND)
708
				tputs(ND, 0, plodput);
709
			else
710
				plodput(i);
711
			outcol++;
712
		}
713
		if (plodcnt < 0)
714
			goto out;
715
	}
716
out:
717
	if (plodflg) {
718
		outcol = soutcol;
719
		outline = soutline;
720
	}
721
	return(plodcnt);
722
}
723
 
724
/*
725
 * An input line arrived.
726
 * Calculate new (approximate) screen line position.
727
 * Approximate because kill character echoes newline with
728
 * no feedback and also because of long input lines.
729
 */
730
noteinp()
731
{
732
 
733
	outline++;
734
	if (outline > LINES - 1)
735
		outline = LINES - 1;
736
	destline = outline;
737
	destcol = outcol = 0;
738
}
739
 
740
/*
741
 * Something weird just happened and we
742
 * lost track of whats happening out there.
743
 * Since we cant, in general, read where we are
744
 * we just reset to some known state.
745
 * On cursor addressible terminals setting to unknown
746
 * will force a cursor address soon.
747
 */
748
termreset()
749
{
750
 
751
	endim();
752
	if (TI)	/* otherwise it flushes anyway, and 'set tty=dumb' vomits */
753
		putpad(TI);	 /*adb change -- emit terminal initial sequence */
754
	destcol = 0;
755
	destline = LINES - 1;
756
	if (CA) {
757
		outcol = UKCOL;
758
		outline = UKCOL;
759
	} else {
760
		outcol = destcol;
761
		outline = destline;
762
	}
763
}
764
 
765
/*
766
 * Low level buffering, with the ability to drain
767
 * buffered output without printing it.
768
 */
769
char	*obp = obuf;
770
 
771
draino()
772
{
773
 
774
	obp = obuf;
775
}
776
 
777
flusho()
778
{
779
 
780
	if (obp != obuf) {
781
		write(1, obuf, obp - obuf);
782
		obp = obuf;
783
	}
784
}
785
 
786
putnl()
787
{
788
 
789
	putchar('\n');
790
}
791
 
792
putS(cp)
793
	char *cp;
794
{
795
 
796
	if (cp == NULL)
797
		return;
798
	while (*cp)
799
		putch(*cp++);
800
}
801
 
802
 
803
putch(c)
804
	int c;
805
{
806
 
807
#ifdef OLD3BTTY		/* mjm */
808
	if(c == '\n')	/* mjm: Fake "\n\r" for '\n' til fix in 3B firmware */
809
		putch('\r');	/* mjm: vi does "stty -icanon" => -onlcr !! */
810
#endif
811
#ifndef	ISO
812
	*obp++ = c & TRIM;
813
#else
814
	if (niso(c)) {
815
		if (c == ('\a' | QUOTE))
816
			*obp++ = ' ';
817
		else
818
			*obp++ = c & TRIM;
819
	} else {
820
		*obp++ = c;
821
	}
822
#endif
823
	if (obp >= &obuf[sizeof obuf])
824
		flusho();
825
}
826
 
827
/*
828
 * Miscellaneous routines related to output.
829
 */
830
 
831
/*
832
 * Put with padding
833
 */
834
putpad(cp)
835
	char *cp;
836
{
837
 
838
	flush();
839
	tputs(cp, 0, putch);
840
}
841
 
842
/*
843
 * Set output through normal command mode routine.
844
 */
845
setoutt()
846
{
847
 
848
	Outchar = termchar;
849
}
850
 
851
/*
852
 * Printf (temporarily) in list mode.
853
 */
854
/*VARARGS2*/
855
lprintf(cp, dp)
856
	char *cp, *dp;
857
{
115 7u83 858
	register int (*P)();
105 7u83 859
 
860
	P = setlist(1);
861
	printf(cp, dp);
862
	Putchar = P;
863
}
864
 
865
/*
866
 * Newline + flush.
867
 */
116 7u83 868
void
869
putNFL(void)
105 7u83 870
{
871
	putnl();
872
	flush();
873
}
874
 
875
/*
876
 * Try to start -nl mode.
877
 */
878
pstart()
879
{
880
 
881
	if (NONL)
882
		return;
883
 	if (!value(OPTIMIZE))
884
		return;
885
	if (ruptible == 0 || pfast)
886
		return;
887
	fgoto();
888
	flusho();
889
	pfast = 1;
890
	normtty++;
891
#ifndef USG3TTY
892
	tty.sg_flags = normf & ~(ECHO|XTABS|CRMOD);
893
#else
894
	tty = normf;
895
	tty.c_oflag &= ~(ONLCR|TAB3);
896
	tty.c_lflag &= ~ECHO;
897
#endif
898
	sTTY(1);
899
}
900
 
901
/*
902
 * Stop -nl mode.
903
 */
904
pstop()
905
{
906
 
907
	if (inopen)
908
		return;
909
	phadnl = 0;
910
	linp = linb;
911
	draino();
912
	normal(normf);
913
	pfast &= ~1;
914
}
915
 
916
/*
917
 * Prep tty for open mode.
918
 */
919
ttymode
920
ostart()
921
{
922
	ttymode f;
923
 
924
	if (!intty)
925
		error("Open and visual must be used interactively");
926
	gTTY(1);
927
	normtty++;
928
#ifndef USG3TTY
929
	f = tty.sg_flags;
930
	tty.sg_flags = (normf &~ (ECHO|XTABS|CRMOD)) |
931
# ifdef CBREAK
932
							CBREAK;
933
# else
934
							RAW;
935
# endif
936
# ifdef TIOCGETC
937
	ttcharoff();
938
# endif
939
#else
940
	f = tty;
941
	tty = normf;
942
	tty.c_iflag &= ~ICRNL;
943
	tty.c_lflag &= ~(ECHO|ICANON);
944
	tty.c_oflag &= ~(TAB3|ONLCR);
945
	tty.c_cc[VMIN] = 1;
946
	tty.c_cc[VTIME] = 1;
947
	ttcharoff();
948
#endif
949
	sTTY(1);
950
	tostart();
951
	pfast |= 2;
952
	return (f);
953
}
954
 
955
/* actions associated with putting the terminal in open mode */
956
tostart()
957
{
958
	putpad(VS);
959
	putpad(KS);
960
	if (!value(MESG)) {
961
		if (ttynbuf[0] == 0) {
962
			register char *tn;
963
			if ((tn=ttyname(2)) == NULL &&
964
			    (tn=ttyname(1)) == NULL &&
965
			    (tn=ttyname(0)) == NULL)
966
				ttynbuf[0] = 1;
967
			else
968
				strcpy(ttynbuf, tn);
969
		}
970
		if (ttynbuf[0] != 1) {
971
			struct stat sbuf;
972
			stat(ttynbuf, &sbuf);
973
			ttymesg = sbuf.st_mode & 0777;
974
			chmod(ttynbuf,
975
#ifdef UCBV7
976
	/*
977
	 * This applies to the UCB V7 Pdp-11 system with the
978
	 * -u write option only.
979
	 */
980
					0611	/* 11 = urgent only allowed */
981
#else
982
					0600
983
#endif
984
						);
985
		}
986
	}
987
}
988
 
989
/*
990
 * Turn off start/stop chars if they aren't the default ^S/^Q.
991
 * This is so idiots who make esc their start/stop don't lose.
992
 * We always turn off quit since datamedias send ^\ for their
993
 * right arrow key.
994
 */
995
#ifdef TIOCGETC
996
ttcharoff()
997
{
998
	nttyc.t_quitc = '\377';
999
	if (nttyc.t_startc != CTRL('q'))
1000
		nttyc.t_startc = '\377';
1001
	if (nttyc.t_stopc != CTRL('s'))
1002
		nttyc.t_stopc = '\377';
1003
# ifdef TIOCLGET
1004
	nlttyc.t_suspc = '\377';	/* ^Z */
1005
	nlttyc.t_dsuspc = '\377';	/* ^Y */
1006
	nlttyc.t_flushc = '\377';	/* ^O */
1007
	nlttyc.t_lnextc = '\377';	/* ^V */
1008
# endif
1009
}
1010
#endif
1011
 
1012
#ifdef USG3TTY
1013
ttcharoff()
1014
{
1015
	tty.c_cc[VQUIT] = '\377';
1016
# ifdef VSTART
1017
	/*
1018
	 * The following is sample code if USG ever lets people change
1019
	 * their start/stop chars.  As long as they can't we can't get
1020
	 * into trouble so we just leave them alone.
1021
	 */
1022
	if (tty.c_cc[VSTART] != CTRL('q'))
1023
		tty.c_cc[VSTART] = '\377';
1024
	if (tty.c_cc[VSTOP] != CTRL('s'))
1025
		tty.c_cc[VSTOP] = '\377';
1026
# endif
1027
}
1028
#endif
1029
 
1030
/*
1031
 * Stop open, restoring tty modes.
1032
 */
1033
ostop(f)
1034
	ttymode f;
1035
{
1036
 
1037
#ifndef USG3TTY
1038
	pfast = (f & CRMOD) == 0;
1039
#else
1040
	pfast = (f.c_oflag & ONLCR) == 0;
1041
#endif
1042
	termreset(), fgoto(), flusho();
1043
	normal(f);
1044
	tostop();
1045
}
1046
 
1047
/* Actions associated with putting the terminal in the right mode. */
1048
tostop()
1049
{
1050
	putpad(VE);
1051
	putpad(KE);
1052
	if (!value(MESG) && ttynbuf[0]>1)
1053
		chmod(ttynbuf, ttymesg);
1054
}
1055
 
1056
#ifndef CBREAK
1057
/*
1058
 * Into cooked mode for interruptibility.
1059
 */
1060
vcook()
1061
{
118 7u83 1062
	disable_raw();
1063
//	tty.sg_flags &= ~RAW;
1064
//	sTTY(1);
105 7u83 1065
}
1066
 
1067
/*
1068
 * Back into raw mode.
1069
 */
1070
vraw()
1071
{
118 7u83 1072
	enable_raw();
105 7u83 1073
 
118 7u83 1074
//	tty.sg_flags |= RAW;
1075
//	sTTY(1);
105 7u83 1076
}
1077
#endif
1078
 
1079
/*
1080
 * Restore flags to normal state f.
1081
 */
1082
normal(f)
1083
	ttymode f;
1084
{
1085
 
1086
	if (normtty > 0) {
1087
		setty(f);
1088
		normtty--;
1089
	}
1090
}
1091
 
1092
/*
1093
 * Straight set of flags to state f.
1094
 */
1095
ttymode
1096
setty(f)
1097
	ttymode f;
1098
{
1099
#ifndef USG3TTY
1100
	register int ot = tty.sg_flags;
1101
#else
1102
	ttymode ot;
1103
	ot = tty;
1104
#endif
1105
 
1106
#ifndef USG3TTY
1107
	if (f == normf) {
1108
		nttyc = ottyc;
1109
# ifdef TIOCLGET
1110
		nlttyc = olttyc;
1111
# endif
1112
	} else
1113
		ttcharoff();
1114
	tty.sg_flags = f;
1115
#else
1116
	if (tty.c_lflag & ICANON)
1117
		ttcharoff();
1118
	tty = f;
1119
#endif
1120
	sTTY(1);
1121
	return (ot);
1122
}
1123
 
1124
gTTY(i)
1125
	int i;
1126
{
119 7u83 1127
/* XXX
105 7u83 1128
#ifndef USG3TTY
1129
	ignore(gtty(i, &tty));
1130
# ifdef TIOCGETC
1131
	ioctl(i, TIOCGETC, &ottyc);
1132
	nttyc = ottyc;
1133
# endif
1134
# ifdef TIOCGLTC
1135
	ioctl(i, TIOCGLTC, &olttyc);
1136
	nlttyc = olttyc;
1137
# endif
1138
#else
1139
	ioctl(i, TCGETA, &tty);
1140
#endif
106 7u83 1141
*/
105 7u83 1142
}
1143
 
1144
/*
1145
 * sTTY: set the tty modes on file descriptor i to be what's
1146
 * currently in global "tty".  (Also use nttyc if needed.)
1147
 */
1148
sTTY(i)
1149
	int i;
1150
{
1151
 
1152
#ifndef USG3TTY
1153
# ifdef USG
1154
	/* Bug in USG tty driver, put out a DEL as a patch. */
1155
	if (tty.sg_ospeed >= B1200)
1156
		write(1, "\377", 1);
1157
# endif
1158
 
1159
# ifdef TIOCSETN
1160
	/* Don't flush typeahead if we don't have to */
1161
	ioctl(i, TIOCSETN, &tty);
1162
# else
1163
	/* We have to.  Too bad. */
1164
	stty(i, &tty);
1165
# endif
1166
 
1167
# ifdef TIOCGETC
1168
	/* Update the other random chars while we're at it. */
1169
	ioctl(i, TIOCSETC, &nttyc);
1170
# endif
1171
# ifdef TIOCSLTC
1172
	ioctl(i, TIOCSLTC, &nlttyc);
1173
# endif
1174
 
1175
#else
1176
	/* USG 3 very simple: just set everything */
106 7u83 1177
/*	ioctl(i, TCSETAW, &tty);*/
105 7u83 1178
#endif
1179
}
1180
 
1181
/*
1182
 * Print newline, or blank if in open/visual
1183
 */
116 7u83 1184
void noonl(void)
105 7u83 1185
{
1186
	putchar(Outchar != termchar ? ' ' : '\n');
1187
}