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