Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / pcvt / vttest / esc.c
1 #include "header.h"
2
3 println(s) char *s; {
4   printf("%s\n", s);
5 }
6
7 esc(s) char *s; {
8   printf("%c%s", 27, s);
9 }
10
11 esc2(s1, s2) char s1, s2; {
12   printf("%c%s%s", 27, s1, s2);
13 }
14
15 brcstr(ps, c) char *ps, c; {
16   printf("%c[%s%c", 27, ps, c);
17 }
18
19 brc(pn,c) int pn; char c; {
20   printf("%c[%d%c", 27, pn, c);
21 }
22
23 brc2(pn1, pn2 ,c) int pn1, pn2; char c; {
24   printf("%c[%d;%d%c", 27, pn1, pn2, c);
25 }
26
27 cub(pn) int pn; {  /* Cursor Backward */
28   brc(pn,'D');
29 }
30 cud(pn) int pn; {  /* Cursor Down */
31   brc(pn,'B');
32 }
33 cuf(pn) int pn; {  /* Cursor Forward */
34   brc(pn,'C');
35 }
36 cup(pn1, pn2) int pn1, pn2; {  /* Cursor Position */
37   brc2(pn1, pn2, 'H');
38 }
39 cuu(pn) int pn; {  /* Cursor Up */
40   brc(pn,'A');
41 }
42 da() {  /* Device Attributes */
43   brc(0,'c');
44 }
45 decaln() {  /* Screen Alignment Display */
46   esc("#8");
47 }
48 decdhl(lower) int lower; {  /* Double Height Line (also double width) */
49   if (lower) esc("#4");
50   else       esc("#3");
51 }
52 decdwl() {  /* Double Wide Line */
53   esc("#6");
54 }
55 deckpam() {  /* Keypad Application Mode */
56   esc("=");
57 }
58 deckpnm() {  /* Keypad Numeric Mode */
59   esc(">");
60 }
61 decll(ps) char *ps; {  /* Load LEDs */
62   brcstr(ps, 'q');
63 }
64 decrc() {  /* Restore Cursor */
65   esc("8");
66 }
67 decreqtparm(pn) int pn; {  /* Request Terminal Parameters */
68   brc(pn,'x');
69 }
70 decsc() {  /* Save Cursor */
71   esc("7");
72 }
73 decstbm(pn1, pn2) int pn1, pn2; {  /* Set Top and Bottom Margins */
74   if (pn1 || pn2) brc2(pn1, pn2, 'r');
75   else            esc("[r");
76   /* Good for >24-line terminals */
77 }
78 decswl() {  /* Single With Line */
79   esc("#5");
80 }
81 dectst(pn) int pn; {  /* Invoke Confidence Test */
82   brc2(2, pn, 'y');
83 }
84 dsr(pn) int pn; {  /* Device Status Report */
85   brc(pn, 'n');
86 }
87 ed(pn) int pn; {  /* Erase in Display */
88   brc(pn, 'J');
89 }
90 el(pn) int pn; {  /* Erase in Line */
91   brc(pn,'K');
92 }
93 hts() {  /* Horizontal Tabulation Set */
94   esc("H");
95 }
96 hvp(pn1, pn2) int pn1, pn2; {  /* Horizontal and Vertical Position */
97   brc2(pn1, pn2, 'f');
98 }
99 ind() {  /* Index */
100   esc("D");
101 }
102 nel() {  /* Next Line */
103   esc("E");
104 }
105 ri() {  /* Reverse Index */
106   esc("M");
107 }
108 ris() { /*  Reset to Initial State */
109   esc("c");
110 }
111 rm(ps) char *ps; {  /* Reset Mode */
112   brcstr(ps, 'l');
113 }
114 scs(g,c) int g; char c; {  /* Select character Set */
115   printf("%c%c%c%c%c%c%c", 27, g ? ')' : '(', c,
116                            27, g ? '(' : ')', 'B',
117                            g ? 14 : 15);
118 }
119 sgr(ps) char *ps; {  /* Select Graphic Rendition */
120   brcstr(ps, 'm');
121 }
122 sm(ps) char *ps; {  /* Set Mode */
123   brcstr(ps, 'h');
124 }
125 tbc(pn) int pn; {  /* Tabulation Clear */
126   brc(pn, 'g');
127 }
128
129 vt52cup(l,c) int l,c; {
130   printf("%cY%c%c", 27, l + 31, c + 31);
131 }
132
133 char inchar() {
134
135   /*
136    *   Wait until a character is typed on the terminal
137    *   then read it, without waiting for CR.
138    */
139
140 #ifdef UNIX
141   int lval, waittime, getpid(); static int val; char ch;
142
143   fflush(stdout);
144   lval = val;
145   brkrd = 0;
146   reading = 1;
147   read(0,&ch,1);
148   reading = 0;
149   if (brkrd)
150     val = 0177;
151   else
152     val = ch;
153   if ((val==0177) && (val==lval))
154     kill(getpid(), (int) SIGTERM);
155 #endif
156 #ifdef SARG10
157   int val, waittime;
158
159   waittime = 0;
160   while(!uuo(051,2,&val)) {             /* TTCALL 2, (INCHRS)   */
161     zleep(100);                         /* Wait 0.1 seconds     */
162     if ((waittime += ttymode) > 600)    /* Time-out, in case    */
163       return('\177');                   /* of hung in ttybin(1) */
164   }
165 #endif
166 #ifdef SARG20   /* try to fix a time-out function */
167   int val, waittime;
168
169   waittime = 0;
170   while(jsys(SIBE,2,_PRIIN) == 0) {     /* Is input empty? */
171     zleep(100);
172     if ((waittime += ttymode) > 600)
173       return('\177');
174   }
175   ejsys(BIN,_PRIIN);
176   val = jsac[2];
177 #endif
178   return(val);
179 }
180
181 char *instr() {
182
183   /*
184    *   Get an unfinished string from the terminal:
185    *   wait until a character is typed on the terminal,
186    *   then read it, and all other available characters.
187    *   Return a pointer to that string.
188    */
189
190
191   int i, val, crflag; long l1; char ch;
192   static char result[80];
193
194   i = 0;
195   result[i++] = inchar();
196 /* Wait 0.1 seconds (1 second in vanilla UNIX) */
197 #ifdef SARG10
198   if (trmop(01031,0) < 5) zleep(500); /* wait longer if low speed */
199   else                    zleep(100);
200 #else
201   zleep(100);
202 #endif
203 #ifdef UNIX
204   fflush(stdout);
205 #ifdef XENIX
206   while(rdchk(0)) {
207     read(0,result+i,1);
208     if (i++ == 78) break;
209   }
210 #else
211 #ifdef SIII
212   while(read(2,result+i,1) == 1)
213     if (i++ == 78) break;
214 #else
215   while(ioctl(0,FIONREAD,&l1), l1 > 0L) {
216     while(l1-- > 0L) {
217       read(0,result+i,1);
218       if (i++ == 78) goto out1;
219     }
220   }
221 out1:
222 #endif
223 #endif
224 #endif
225 #ifdef SARG10
226   while(uuo(051,2,&val)) {      /* TTCALL 2, (INCHRS)  */
227     if (!(val == '\012' && crflag))     /* TOPS-10 adds LF to CR */
228       result[i++] = val;
229     crflag = val == '\015';
230     if (i == 79) break;
231     zleep(50);          /* Wait 0.05 seconds */
232   }
233 #endif
234 #ifdef SARG20
235   while(jsys(SIBE,2,_PRIIN) != 0) {     /* read input until buffer is empty */
236     ejsys(BIN,_PRIIN);
237     result[i++] = jsac[2];
238     if (i == 79) break;
239     zleep(50);          /* Wait 0.05 seconds */
240   }
241 #endif
242   result[i] = '\0';
243   return(result);
244 }
245
246 ttybin(bin) int bin; {
247 #ifdef SARG10
248   #define OPEN 050
249   #define IO_MOD 0000017
250   #define _IOPIM 2
251   #define _IOASC 0
252   #define _TOPAG 01021
253   #define _TOSET 01000
254
255   int v;
256   static int arglst[] = {
257     _IOPIM,
258     `TTY`,
259     0
260   };
261   arglst[0] = bin ? _IOPIM : _IOASC;
262   v = uuo(OPEN, 1, &arglst[0]);
263   if (!v) { printf("OPEN failed"); exit(); }
264   trmop(_TOPAG + _TOSET, bin ? 0 : 1);
265   ttymode = bin;
266 #endif
267 #ifdef SARG20
268   /*    TTYBIN will set the line in BINARY/ASCII mode
269    *    BINARY mode is needed to send control characters
270    *    Bit 28 must be 0 (we don't flip it).
271    *    Bit 29 is used for the mode change.
272    */
273
274   #define _TTASC 0000100
275   #define _MOXOF 0000043
276
277   int v;
278
279   ejsys(RFMOD,_CTTRM);
280   v = ejsys(SFMOD,_CTTRM, bin ? (~_TTASC & jsac[2]) : (_TTASC | jsac[2]));
281   if (v) { printf("SFMOD failed"); exit(); }
282   v = ejsys(MTOPR,_CTTRM,_MOXOF,0);
283   if (v) { printf("MTOPR failed"); exit(); }
284 #endif
285 }
286
287 #ifdef SARG20
288 /*
289  *      SUPERBIN turns off/on all input character interrupts
290  *      This affects ^C, ^O, ^T
291  *      Beware where and how you use it !!!!!!!
292  */
293
294 superbin(bin) int bin; {
295   int v;
296
297   v = ejsys(STIW,(0//-5), bin ? 0 : -1);
298   if (v) { printf("STIW superbinary setting failed"); exit(); }
299   ttymode = bin;
300 }
301
302 /*
303  *      PAGE affects the ^S/^Q handshake.
304  *      Set bit 34 to turn it on. Clear it for off.
305  */
306
307 page(bin) int bin; {
308   int v;
309
310   #define TT_PGM 0000002
311
312   ejsys(RFMOD,_CTTRM);  /* Get the current terminal status */
313   v = ejsys(STPAR,_CTTRM, bin ? (TT_PGM | jsac[2]) : (~TT_PGM & jsac[2]));
314   if (v) { printf("STPAR failed"); exit(); }
315 }
316 #endif
317
318 trmop(fc,arg) int fc, arg; {
319 #ifdef SARG10
320   int retvalp;
321   int arglst[3];
322
323   /* TRMOP is a TOPS-10 monitor call that does things to the terminal. */
324
325   /* Find out TTY nbr (PA1050 barfs if TRMOP get -1 instead of udx)    */
326   /* A TRMNO monitor call returns the udx (Universal Device Index)     */
327
328   arglst[0] = fc;               /* function code        */
329   arglst[1] = calli(0115, -1);  /* udx, TRMNO. UUO      */
330   arglst[2] = arg;              /* Optional argument    */
331
332   if (calli(0116, 3 // &arglst[0], &retvalp))           /* TRMOP. UUO */
333   return (retvalp);
334   else {
335     printf("?Error return in TRMOP.");
336     exit();
337   }
338 #endif
339 }
340
341 inputline(s) char *s; {
342   scanf("%s",s);
343 #ifdef SARG10
344   readnl();
345 #endif
346 #ifdef SARG20
347   readnl();
348 #endif
349 }
350
351 inflush() {
352
353   /*
354    *   Flush input buffer, make sure no pending input character
355    */
356
357   int val;
358
359 #ifdef UNIX
360 #ifdef XENIX
361   while(rdchk(0)) read(0,&val,1);
362 #else
363 #ifdef SIII
364   while(read(2,&val,1));
365 #else
366   long l1;
367   ioctl (0, FIONREAD, &l1);
368   while(l1-- > 0L) read(0,&val,1);
369 #endif
370 #endif
371 #endif
372 #ifdef SARG10
373   while(uuo(051,2,&val))        /* TTCALL 2, (INCHRS)  */
374     ;
375 #endif
376 #ifdef SARG20
377   ejsys(CFIBF,_PRIIN);          /* Clear input buffer */
378 #endif
379 }
380
381 zleep(t) int t; {
382
383 /*
384  *    Sleep and do nothing (don't waste CPU) for t milliseconds
385  */
386
387 #ifdef SARG10
388   calli(072,t);         /* (HIBER) t milliseconds */
389 #endif
390 #ifdef SARG20
391   ejsys(DISMS,t);       /* DISMISS for t milliseconds */
392 #endif
393 #ifdef UNIX
394   t = t / 1000;
395   if (t == 0) t = 1;
396   sleep(t);             /* UNIX can only sleep whole seconds */
397 #endif
398 }