Debian Codegraph @

libsrc/tw/tvput.c

raw
1 /* @(#)tvput.c 19.1 (ES0-DMD) 02/25/03 13:58:15 */
2 /*===========================================================================
3 Copyright (C) 1995 European Southern Observatory (ESO)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
18 MA 02139, USA.
19
20 Corresponding concerning ESO-MIDAS should be addressed as follows:
21 Internet e-mail: [email protected]
22 Postal address: European Southern Observatory
23 Data Management Division
24 Karl-Schwarzschild-Strasse 2
25 D 85748 Garching bei Muenchen
26 GERMANY
27 ===========================================================================*/
28
29 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
31 .TYPE Module
32 .IDENTIFICATION tvput.c
33 .AUTHOR Francois Ochsenbein [ESO-IPG]
34 .LANGUAGE C
35 .KEYWORDS Terminal Independant i/o Package.
36 .ENVIRONMENT TermWindows
37 .COMMENTS This module groups output and graphic char treatement.
38 \begin{TeX}
39 Used Capabilities:
40 $$\begin{tabular}{|lp{37em}|} \hline
41 {\tt se=} & Exit Stand-out mode \\
42 {\tt so=} & Start Stand-out mode \\
43 {\tt us=} & Start Underline mode \\
44 {\tt Va=} & Set keyboard to ASCII \\
45 {\tt Vg=} & Set keyboard to graphic mode \\
46 {\tt Vn=} & Set normal Video Attributes \\
47 {\tt VB=} & Set Bold Video Attributes \\
48 {\tt Vu=} & Set Underline Video Attributes \\
49 {\tt Vb=} & Set blink Video Attributes \\
50 {\tt Vr=} & Set reverse Video Attributes \\
51 \hline
52 \end{tabular}$$
53 \end{TeX}
54 .VERSION 1.0 07-Jul-1986: Creation
55 .VERSION 1.1 12-Aug-1986: Added tv_wa, writing a string with attributes.
56
57 .VERSION 1.2 13-Oct-1986 Some terminals (e.g. HP) need to resend the
58 attribute on each new line.
59 This feature incorporated with ChangedLine flag
60 in standout part of TERM structure.
61 .VERSION 2.0 03-Dec-1986 New terminal-independant with output buffering version.
62 .VERSION 2.1 11-Jun-1987 Version '2' of TermWindows.
63 Defaut input char is Control_Z.
64 Interfaces to xv routines in a seperate module.
65 Batch mode allowed.
66 .VERSION 2.2 23-Jun-1987 Implementation for UNIX and Minimal Version.
67 .VERSION 3.0 20-Mar-1988 Version '3' of TermWindows.
68 ----------------------------------------------------------------------------*/
69
70 #define DEBUG 0 /* For debugging */
71
72 #define PM_LEVEL LEVEL_TV
73
74 #define TW_MACROS 0 /* Don't use TermWindows Macros */
75 #define TW_import 0
76 #define TW_STRUCT 0 /* Do not use window Structures */
77 #include <twset.h>
78
79 MID_EXTERN TERM *terms ;
80 static char special;
81 MID_STATIC char gtr = 1;
82
83 #define SEND(p) tv_send(p,1)
84
85 #if DEBUG
86 # define TERMTEST if(!terms->version) tv_gopen();\
87 tv_ed(terms)
88 #else
89 # define TERMTEST if(!terms->version) tv_gopen()
90 #endif
91
92 #define ENTER_TV(x) int state_buffer; \
93 ENTER(x); TERMTEST; \
94 state_buffer = tv_buffer(1);
95 #define EXIT_TV(f) FIN: \
96 tv_buffer(state_buffer); \
97 EXIT(f)
98
99 /* Macros just to improve readibility */
100
101 #define curl (terms->pos[0])
102 #define curc (terms->pos[1])
103 #define diml (terms->dim[0])
104 #define dimc (terms->dim[1])
105 #define BUFS (terms->bufs)
106 #define Attr (terms->attr)
107 #define TTattr terms->tt_attr
108
109 #define Vn 0
110 #define VB 1
111 #define Vu 2
112 #define Vb 3
113 #define Vr 4
114 #define Vg 5
115 #define Va 6
116 #define IssueCorner(x) { special = CornerChar(x); \
117 tv_wg(&special, 1); }
118
119 /* MONITOR (ZTPUT); */ /* CG. status defined locally */
120
121 /*==========================================================================
122 * isgr0
123 *==========================================================================*/
124 static int isgr0(c)
125 /*+++
126 .PURPOSE Test non-graphic mode
127 .RETURNS 0 (FALSE) / 1 (TRUE)
128 .REMARKS Not traced
129 --------*/
130 char c; /* IN: char to test */
131 {
132 return((c == '+') || (c == '-') || (c == '|') || (c == '~'));
133 }
134
135 /*===========================================================================*/
136 static int strupper ( str )
137 /*+++++++++
138 .PURPOSE Converts (in place) a string to upper case
139 .RETURNS Length of string
140 --------*/
141 char *str; /* MOD: starting address */
142 {
143 register char *p;
144
145 for (p = str; *p; p++)
146 *p = toupper(*p);
147
148 return(p-str);
149 }
150
151 /*==========================================================================
152 * tv_attr
153 *==========================================================================*/
154 int tv_attr(val)
155 /*+++++++++
156 .PURPOSE Set attributes for the next output to screen
157 (using tv_write, tv_put or tv_puts)
158 .RETURNS OK / NOK
159 .REMARKS Attributes are defined as numbers to be OR'ed in TERMINAL.H
160 The cursor position remains unchanged.
161 To revert to the normal attributes, use tv_attr with
162 a value of 0.
163 ----------------*/
164 int val; /* IN: Desired screen attributes */
165 {
166 char *p;
167 int new, st, k;
168
169 ENTER_TV("tv_attr");
170
171 TRACE_ED_I("Asked attributes: ", val);
172
173 st = OK;
174 new = val &(_BLINK_|_UNDERSCORE_|_BOLD_|_REVERSE_|_GRAPHICS_);
175 new ^= terms->attr_init; /* Useful only if attr_init is not null */
176
177 if (terms->standout & ChangedLine) /* Current Attributes assumed to
178 * have changed. Reissue. */
179 { terms->standout &= ~ChangedLine;
180 tv_attr((int)terms->attr_init | (new & _GRAPHICS_));
181 }
182
183 if(Attr == new) FINISH; /* No change */
184
185 k = terms->standout & 7; /* k > 1 means `no or special standout' */
186 if ((new & _GRAPHICS_) != (Attr & _GRAPHICS_))
187 { if (new & _GRAPHICS_) p = TTattr[Vg];
188 else p = TTattr[Va];
189 if (p) if ( (st = SEND(p)) != OK) FINISH;
190 }
191
192 /* If only graphics changed, exit now */
193 if ((new & (~_GRAPHICS_)) == (Attr & (~_GRAPHICS_)))
194 { Attr = new;
195 FINISH;
196 }
197
198 /* Set first terminal to No Attributes ... */
199 if (Attr &(~_GRAPHICS_))
200 { p = TTattr[Vn];
201 if (p) if_not(st = SEND(p)) FINISH;
202 }
203 Attr = new;
204
205 /* ... if no other attribute is asked for, exit */
206 if ((Attr & (~_GRAPHICS_)) == 0) FINISH;
207
208 /* ... and send successively the asked attributes
209 * However, if no attributes exist at all, not necessary
210 * to search for non-existent capabilities.... */
211
212 if(k > 1) FINISH;
213
214 /* A loop is used for attributes */
215
216 for (k=1, new = 1; k <= _REVERSE_; k++, new <<= 1)
217 { if (Attr & new)
218 if_not(st = SEND(TTattr[k])) FINISH;
219 }
220
221 EXIT_TV(st);
222 }
223
224 /*==========================================================================
225 * tv_bell
226 *==========================================================================*/
227 int tv_bell()
228 /*
229 .PURPOSE Ring the terminal bell.
230 .RETURNS OK
231 .REMARKS Use `bl' capability --- or ASCII char `07' if such a capability
232 is not present.
233 -----------------------------------------------------------------------*/
234 {
235 register char *p;
236 int status;
237 MID_RSTATIC char ascii_bell = '\07';
238
239 ENTER_TV("tv_bell");
240
241 status = OK;
242 p = SearchCap("bl");
243 if (!p) p = SearchCap("vb");
244 if (!p) status = tv_out(&ascii_bell,1);
245 else status = SEND(p);
246 FINISH;
247
248 EXIT_TV(status);
249 }
250
251 /*======================================================================
252 * tv_box
253 *======================================================================*/
254 int tv_box(topl,topc,boxl,boxc)
255 /*+++
256 .PURPOSE Draw a box, and position the cursor at top left corner.
257 .RETURNS OK / NOK (impossible)
258 .REMARKS Value of 0 for box dimensions assume all usable space.
259 The cursor does not move if the box cannot be drawn.
260 Negative values for box dimensions mean right / down adjustments.
261 ---*/
262 int topl; /* IN: Line of upper-left corner (<0 if counted\
263 from bottom) */
264 int topc; /* IN: Column of upper-left corner (<0 if counted from
265 right) */
266 int boxl; /* IN: Line width of box (0 = whole space) */
267 int boxc; /* IN: Column width of box (0 = whole space) */
268 {
269 int status;
270 int lh, lv, old_attr;
271 short int b0[2], bd[2]; /* Final position & dimension of box */
272
273 ENTER_TV("tv_box");
274
275 #if DEBUG
276 TRACE_ED_I("Top line=",topl);
277 TRACE_ED_I("Top col.=",topc);
278 TRACE_ED_I("Ext line=",boxl);
279 TRACE_ED_I("Ext col.=",boxc);
280 #endif
281
282 b0[0] = topl; b0[1] = topc;
283 bd[0] = boxl; bd[1] = boxc;
284
285 /* Adjust box dimensions */
286
287 status = tv_rb(b0, bd, terms->dim);
288 if (status != OK) FINISH;
289
290 #if DEBUG
291 TRACE_ED_I("Kept Top line=",topl);
292 TRACE_ED_I("Kept Top col.=",topc);
293 TRACE_ED_I("Kept Ext line=",boxl);
294 TRACE_ED_I("Kept Ext col.=",boxc);
295 #endif
296
297 old_attr = Attr;
298 tv_attr(old_attr | _GRAPHICS_); /* All in Graphics Mode */
299
300 lh = bd[1] - 2; lv = bd[0] -2 ;
301
302 /* First, write the upperleft corner */
303 tv_goto(b0[0],b0[1]);
304 IssueCorner(_UPLEFT_);
305
306 /* Write now first horizontal line */
307 tv_rule(_RIGHT_,lh);
308
309 /* Write upperight corner */
310 IssueCorner(_UPRIGHT_);
311
312 /* Vertical lines */
313 tv_goto(b0[0]+1, b0[1] + bd[1] -1);
314 tv_rule(_DOWN_,lv);
315 IssueCorner(_DOWNRIGHT_);
316
317 tv_goto(b0[0]+1, b0[1]);
318 tv_rule(_DOWN_,lv);
319 IssueCorner(_DOWNLEFT_);
320
321 /* Bottom horizontal line */
322 tv_rule(_RIGHT_,lh);
323
324 /* Reset attributes, and go to top left corner */
325 tv_attr(old_attr);
326 tv_goto(b0[0]+1, b0[1]+1);
327
328 EXIT_TV(status);
329 }
330
331 /*======================================================================
332 * tv_graph
333 *======================================================================*/
334 int tv_graph(k)
335 /*+++
336 .PURPOSE Switch Translate or Not the Graphic Characters.
337 .RETURNS Previous state 0 (False) / 1 (True)
338 .REMARKS
339 ---*/
340 int k; /* IN: Status FALSE / TRUE */
341 {
342 register int s;
343
344 s = gtr;
345 gtr = k;
346
347 return(s);
348 }
349
350 /*======================================================================
351 * tv_gtr
352 *======================================================================*/
353 static int tv_gtr(str, len)
354 /*+++
355 .PURPOSE Translate the terminal-independant graphic chars into
356 terminal-dependant graphic characters
357 .RETURNS OK / NOK (there is at leat 1 non-graphic char)
358 .REMARKS *** For Internal Use ***
359 ---*/
360 char *str; /* MOD: String to translate */
361 int len; /* IN: Length of str */
362 {
363 int status;
364 char *p;
365 int i, k;
366
367 ENTER("tv_gtr");
368
369 status = OK;
370
371 if (gtr)
372 { for (p= str, i=len; --i>=0; p++)
373 { k = *p - 'a';
374 if ( (k < 0) || ( k >= ITEMS(terms->gchars)) ) k = _RUBBISH_;
375 *p = terms->gchars[k];
376 if (isgr0(*p)) status = NOK;
377 }
378 }
379
380 EXIT(status);
381 }
382
383 /*==========================================================================
384 * tv_line
385 *==========================================================================*/
386 int tv_line(str, len)
387 /*+++++++++++++++++
388 .PURPOSE Write a string made of normal characters to the terminal, on a
389 single line.
390 .RETURNS OK / NOK
391 .REMARKS The line is truncated if the right margin would be passed.
392 If the cursor was at the end of the line, issue a new line first.
393 If the terminal is hardcopy and stand-out mode is on, underline.
394 ----------------*/
395 char *str; /* IN: String to display on the terminal */
396 int len; /* IN: Length of String */
397 {
398 int status;
399 register int l, col;
400 register char *p;
401
402 ENTER_TV("tv_line");
403
404 #if DEBUG
405 tv_ed(terms);
406 #endif
407
408 status = OK;
409 if (len <= 0) FINISH; /* No output */
410
411 if(curc >= dimc) tv_nl();
412 col = curc;
413 p = str;
414
415 /* Send attributes if necessary */
416 if (terms->standout & ChangedLine)
417 { l = terms->attr;
418 terms->attr = terms->attr_init;
419 tv_attr(l);
420 }
421
422 /* Truncate length if necessary */
423 l = dimc - curc;
424 l = MIN ( l, len);
425
426 if(Attr)
427 { switch(terms->standout & 7)
428 { case 2: /* Write first the underline, if stand-out mode
429 * --- but not if only Graphics */
430 if (Attr & (~_GRAPHICS_))
431 { oscfill(BUFS,l,'_');
432 status = tv_out(BUFS,l);
433 curc += l;
434 tv_goto(curl,col); /* Go again to required column*/
435 }
436 default:
437 if (Attr & _GRAPHICS_) /* Translate graphic chars */
438 { p = BUFS;
439 oscopy(p, str, l);
440 if (tv_gtr(p, l) != OK) /* Note: if some non- */
441 { while (--l >= 0) /* graph. chars */
442 { if (isgr0(*p))
443 tv_attr((int)Attr & ~_GRAPHICS_);
444 else tv_attr((int)Attr | _GRAPHICS_);
445 tv_out(p, 1), p++, curc++;
446 }
447 tv_attr((int)Attr | _GRAPHICS_);
448 FINISH;
449 }
450 }
451 status = tv_out(p, l);
452 break;
453
454 case 3: /* Change Case -
455 * but not if Graphics! */
456 p = BUFS;
457 oscopy(p, str, l);
458 if (Attr & _GRAPHICS_) tv_gtr(p, l);
459 else strupper(p);
460 status = tv_out(p, l);
461 break;
462 }
463 }
464 else status = tv_out(p, l);
465
466 curc += l;
467
468 EXIT_TV(status);
469 }
470
471 /*==========================================================================
472 * tv_puts
473 *==========================================================================*/
474 int tv_puts(str)
475 /*+++++++++
476 .PURPOSE Write an EOS-terminated string to the terminal, followed by a newline.
477 .RETURNS OK / NOK
478 .REMARKS Control characters are automatically treated.
479 -----------*/
480 char *str; /* IN: EOS-terminated string to send to the terminal */
481 {
482 register int s; /* Status */
483
484 ENTER_TV("tv_puts");
485
486 s = tv_write(str,strlen(str), 1);
487 if (s == OK) s = tv_nl();
488 FINISH;
489
490 EXIT_TV(s);
491 }
492
493 /*======================================================================
494 * tv_rule
495 *======================================================================*/
496 int tv_rule(direction, len)
497 /*+++
498 .PURPOSE Writes a rule, horizontal or vertical
499 .RETURNS OK / NOK
500 .REMARKS Uses the graphic character if it exists.
501 .REMARKS On return, the cursor is just after (at the left, right, up or
502 down depending on the direction) the last issued character
503 ---*/
504 int direction; /* IN: The direction(_UP_, _LEFT_, etc) of the rule */
505 int len; /* IN: Rule length, in characters */
506 {
507 int i, l, dir, old_attr;
508 int status;
509
510 ENTER_TV("tv_rule");
511
512 status = OK;
513 old_attr = Attr;
514
515 dir = direction;
516 l = len;
517 if (len < 0) /* Change direction */
518 { dir = 1^direction;
519 l = -len;
520 }
521
522 l = MIN(l, dimc-curc); /* Not too many ! */
523 if (l == 0) FINISH;
524
525 i = dir >> 1; /* Direction horiz./vertical */
526 if (i > 1) /* Bad direction... */
527 { status = NOK;
528 ERR_ED_I("Bad rule orientation: ", direction);
529 FINISH;
530 }
531 special = RuleChar(i); /* Get the rule char */
532 tv_attr(old_attr | _GRAPHICS_);
533
534 /* Now, issue as many rule characters as required */
535
536 while(--l>=0) /* Write char + BS */
537 { status = tv_line(&special, 1); /* Note, cursor moves right */
538 if (status != OK) break;
539 if (dir != _RIGHT_)
540 { tv_mvc(_LEFT_,1);
541 tv_mvc(dir,1);
542 }
543 }
544
545 tv_attr(old_attr);
546
547 EXIT_TV(status);
548 }
549
550 /*==========================================================================
551 * tv_write
552 *==========================================================================*/
553 int tv_write(str, len, opt)
554 /*++++++++++++
555 .PURPOSE Write a string to the terminal, taking into account control chars
556 or replacing them with "rubbish" characters.
557 .RETURNS OK / NOK
558 .REMARKS If option is non-zero, the \n (newline), \b (backskpace),
559 \r (carriage-return), \f (form-feed) \t (tab) and
560 `Delete' control characters are automatically treated.
561 Other control chars (or all if option is zero) are issued as "rubbish".
562 ---------------*/
563 char *str; /* IN: String to display on the terminal */
564 int len; /* IN: Length of String */
565 int opt; /* IN: 0 to treat control chars, 1 otherwise */
566 {
567 char *p;
568 int i, l, old_attr;
569 int status;
570 char control_char;
571 MID_RSTATIC char rubbish_char = RubbishChar();
572
573 ENTER_TV("tv_write");
574
575 status = OK;
576 if (len <= 0) FINISH; /* No output */
577 TRACE_STR2(str, len);
578
579 /* Scan the string until a control char is found */
580
581 for (p = str, l = len, i = 0; l > 0; p += i, l -= i)
582 { if(curc >= dimc) tv_nl();
583 i = oscscan(p, l, _CNTRL_, main_ascii);
584 i = MIN(i, dimc - curc); /* Not more than one line */
585 /* Send the line */
586 if (i) /* Normal Characters */
587 { if (!(status = tv_line(p, i))) FINISH;
588 continue;
589 }
590
591 /* A control char was found ---- Process these chars */
592 control_char = *p;
593 if (opt == 0) /* Don't process, i.e. replace by something
594 * leading to `default' in following switch */
595 control_char = EOS;
596
597 switch(control_char)
598 { case '\n' : /* Start a new line */
599 tv_nl(); break;
600 case '\r' : /* go to beginning of line */
601 tv_goto(curl,0); break;
602 case '\f' : /* Clear screen */
603 tv_clear(_WHOLE_); break;
604 case '\b' : /* backspace */
605 tv_mvc(_LEFT_,1); break;
606 case DEL : /* delete: bs, then clear cursor */
607 tv_mvc(_LEFT_,1);
608 tv_cc(1); break;
609 case '\t' : /* Tabs are assumed to be 7,15,etc */
610 if(curc >= dimc) tv_nl();
611 /* Output the tab */
612 if ( (status = tv_line(p, 1)) != OK) FINISH;
613 curc++;
614 while (curc&7) curc++; /* Adjust the cursor position */
615 break;
616 default : /* Any other control, issue RUBBISH */
617 old_attr = Attr;
618 tv_attr(old_attr | _GRAPHICS_);
619 status = tv_line(&rubbish_char, 1);
620 tv_attr(old_attr);
621 if (status != OK) FINISH;
622 break;
623 }
624 i = 1;
625 }
626
627 EXIT_TV (status);
628 }
629
630 /*==========================================================================
631 * tv_wa
632 *==========================================================================*/
633 int tv_wa(str, len)
634 /*+++++++++++
635 .PURPOSE Write a string made of (attribute + char) to the terminal.
636 .RETURNS OK / NOK
637 .REMARKS All control characters are translated to rubbish.
638 ------------*/
639 ACHAR *str; /* IN: String to display on the terminal */
640 int len; /* IN: Length of String */
641 {
642 ACHAR *p;
643 int i, l;
644 char *pc;
645 ACHAR a;
646 char wbuf[81];
647 int status;
648
649 ENTER_TV("tv_wa");
650
651 status = OK;
652 if (len <= 0) FINISH; /* No output */
653
654 TRACE_STR2((char *)str, sizeof(ACHAR)*len);
655
656 for (p = str, l = len, i = 0; l > 0; l -= i)
657 { a = *p & 0xff00;
658 tv_attr((int)(a >> 8));
659 for (pc = wbuf, i = MIN(sizeof(wbuf), l);
660 (--i >= 0) && ((*p & 0xff00) == a);
661 p++, pc++) *pc = *p & HIGH;
662 i = pc - wbuf; /* String Length */
663 if_not(status = tv_write(wbuf,i, 0)) FINISH;
664 }
665
666 EXIT_TV(status);
667 }
668
669 /*==========================================================================
670 * tv_wg
671 *==========================================================================*/
672 int tv_wg(str, len)
673 /*++++++++
674 .PURPOSE Write graphic characters.
675 .RETURNS OK / NOK
676 .REMARKS All bad chars are translated into rubbish
677 -----------------------------------------------------------------------*/
678 char *str; /* IN: String to display on the terminal */
679 int len; /* IN: Length of String */
680 {
681 int old_attr;
682 int status;
683
684 ENTER_TV("tv_wg");
685
686 TRACE_ED_STR2("Output graphics: ", str, len);
687
688 old_attr = Attr;
689 tv_attr(old_attr | _GRAPHICS_);
690 status = tv_write(str, len, 0);
691 tv_attr(old_attr);
692 FINISH;
693
694 EXIT_TV(status);
695 }
696
697 /*==========================================================================
698 * tv_wsend
699 *==========================================================================*/
700 int tv_wsend(str,len)
701 /*
702 .PURPOSE Directly passes a string to OS-dependant routines
703 .RETURNS OK / NOK
704 .REMARKS The cursor is assumed not to move ...
705 -----------------------------------------------------------------------*/
706 char *str; /* IN: String to display on the terminal */
707 int len; /* IN: Length of String */
708 {
709 register int s; /* Status */
710
711 ENTER_TV("tv_wsend");
712
713 s = tv_out(str,len);
714 FINISH;
715
716 EXIT_TV(s);
717 }
718