1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_tty_filter_c[] = "$Id$";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
24 /* The tty & plain filters: */
26 /****************************************************************************/
27 #define HAVE_TERMCAP_H
32 #include "new_memory.h"
33 #include "new_string.h"
34 #include "string_dictionary_aux.h"
35 #include "formatter.h"
39 /***************************************************************************/
40 #ifndef HAVE_TERMCAP_H
42 extern char *tgetstr(),*getenv();
44 extern speed_t ospeed;
51 /* Dictionary naming convention:
53 B.xxx is the termcap sequence to begin environment xxx.
54 E.xxx is the termcap sequence to end environment xxx.
58 static string_dictionary termcap_dict;
59 static char code_buf[10240], *code_buf_pos = code_buf, *code;
61 /* Define the following commands:
63 (Hopefully) shared with all devices:
67 @em Emphasis. User underline if available, else reverse video.
68 @bold Bold letters. If not available, reverse video, else underline.
69 @beep "bl" termcap entry, else "^G"
73 @blink "mb"/"me" termcap entry, else nothing.
74 @rv "so"/"se" termcap entry.
75 @u "us"/"ue" termcap entry.
78 #define TD_SET(k,v) (string_dictionary_Define(termcap_dict, (k), &ex)->value \
80 #define EXPAND(k) (code = code_buf_pos, tputs(tmp, 1, tty_outc), \
81 *code_buf_pos++ = 0, TD_SET(k, code))
92 tty_filter_init(char *drivername,
97 static char st_buf[128];
98 char tc_buf[1024], *p = st_buf, *tmp, *term;
100 string_dictionary_binding *b;
101 int isrealtty = string_Eq(drivername, "tty");
102 #ifdef HAVE_TERMIOS_H
105 ospeed = (tcgetattr(STDIN_FILENO, &tbuf) == 0) ? cfgetospeed(&tbuf) : 2400;
107 struct sgttyb sgttyb;
109 ospeed = (ioctl(0, TIOCGETP, &sgttyb) == 0) ? sgttyb.sg_ospeed : 2400;
112 if (termcap_dict == (string_dictionary) NULL)
113 termcap_dict = string_dictionary_Create(7);
115 if (!(term = getenv("TERM"))) { /* Only use termcap if $TERM. */
116 if (isrealtty && !notfirst)
117 /* only complain if initializing tty mode, and would be first
119 ERROR("$TERM not set. tty mode will be plain.\n");
123 * This is a temporary KLUDGE to get around the problem where some people
124 * might start zwgc in their ~/.startup.X and it hangs on the RISC/6000.
125 * Apparently, the call to tgetent() with the Athena console window causes
126 * the process to get stopped on tty access. Since the terminal type is
127 * "dumb" (set by tcsh), we can pretty much assume there isn't anything
128 * to setup from the termcap information.
130 else if (!strcmp(term, "dumb")) { }
133 tgetent(tc_buf, term);
135 /* Step 1: get all of {rv,bold,u,bell,blink} that are available. */
137 /* We cheat here, and ignore the padding (if any) specified for
138 the mode-change strings (it's a real pain to do "right") */
140 tmp = tgetstr("pc", &p);
141 PC = (tmp) ? *tmp : 0;
142 if (tmp = tgetstr("md",&p)) { /* bold ? */
144 tmp = tgetstr("me",&p);
147 if (tmp = tgetstr("mr",&p)) { /* reverse video? */
149 tmp = tgetstr("me",&p);
152 if (tmp = tgetstr("bl",&p)) { /* Bell ? */
154 TD_SET("E.bell", NULL);
156 if (tmp = tgetstr("mb",&p)) { /* Blink ? */
158 tmp = tgetstr("me",&p);
161 if (tmp = tgetstr("us",&p)) { /* Underline ? */
163 tmp = tgetstr("ue",&p);
166 if (tmp = tgetstr("so",&p)) { /* Standout ? */
168 tmp = tgetstr("se",&p);
172 /* Step 2: alias others to the nearest substitute */
174 /* Bold = so, else rv, else ul */
175 if (NULL == string_dictionary_Lookup(termcap_dict,"B.bold")) {
176 if(b = string_dictionary_Lookup(termcap_dict,"B.so")) {
177 TD_SET("B.bold",b->value);
179 string_dictionary_Lookup(termcap_dict,"E.so")->value);
180 } else if (b = string_dictionary_Lookup(termcap_dict,"B.rv")) {
181 TD_SET("B.bold",b->value);
183 string_dictionary_Lookup(termcap_dict,"E.rv")->value);
184 } else if (b = string_dictionary_Lookup(termcap_dict,"B.u")) {
185 TD_SET("B.bold",b->value);
187 string_dictionary_Lookup(termcap_dict,"E.u")->value);
192 if (NULL == string_dictionary_Lookup(termcap_dict,"B.bell")) {
193 TD_SET("B.bell","\007");
194 TD_SET("E.bell",NULL);
197 /* Underline -> nothing */
198 /* Blink -> nothing */
203 /***************************************************************************/
209 fixed_string_eq(string pattern,
213 while (*pattern && text_length>0 && *pattern == *text) {
219 return(!*pattern && !text_length);
222 typedef struct _tty_str_info {
223 struct _tty_str_info *next;
228 char alignment; /* 'l', 'c', 'r', or ' ' to indicate newline */
229 unsigned int bold_p : 1;
230 unsigned int italic_p : 1;
231 unsigned int bell_p : 1;
232 unsigned int ignore: 1;
236 free_info(tty_str_info *info)
238 tty_str_info *next_info;
241 next_info = info->next;
248 do_mode_change(tty_str_info *current_mode_p,
252 /* alignment commands: */
253 if (fixed_string_eq("left", text, text_length) ||
254 fixed_string_eq("l", text, text_length))
255 current_mode_p->alignment = 'l';
256 else if (fixed_string_eq("center", text, text_length) ||
257 fixed_string_eq("c", text, text_length))
258 current_mode_p->alignment = 'c';
259 else if (fixed_string_eq("right", text, text_length) ||
260 fixed_string_eq("r", text, text_length))
261 current_mode_p->alignment = 'r';
264 else if (fixed_string_eq("bold", text, text_length) ||
265 fixed_string_eq("b", text, text_length))
266 current_mode_p->bold_p = 1;
267 else if (fixed_string_eq("italic", text, text_length) ||
268 fixed_string_eq("i", text, text_length))
269 current_mode_p->italic_p = 1;
270 else if (fixed_string_eq("roman", text, text_length)) {
271 current_mode_p->bold_p = 0;
272 current_mode_p->italic_p = 0;
273 } else if (fixed_string_eq("beep", text, text_length)) {
274 current_mode_p->bell_p = 1;
278 /* commands ignored in tty mode: */
279 else if (fixed_string_eq("color", text, text_length) ||
280 fixed_string_eq("font", text, text_length)) {
281 current_mode_p->ignore = 1;
286 static tty_str_info *
287 convert_desc_to_tty_str_info(desctype *desc)
290 tty_str_info *result = NULL;
291 tty_str_info *last_result_block = NULL;
292 int isbeep, did_beep = 0;
294 #if !defined(SABER) && defined(__STDC__)
295 tty_str_info current_mode = { NULL, "", 0, 'l', 0 , 0, 0, 0};
297 /* This is needed due to a bug in saber, and lack of pre-ANSI support. */
298 tty_str_info current_mode;
300 current_mode.next = NULL;
301 current_mode.str = "";
302 current_mode.len = 0;
303 current_mode.alignment = 'l';
304 current_mode.bold_p = 0;
305 current_mode.italic_p = 0;
306 current_mode.bell_p = 0;
307 current_mode.ignore = 0;
310 for (; desc->code!=DT_EOF; desc=desc->next) {
312 /* Handle environments: */
313 if (desc->code == DT_ENV) {
315 temp = (tty_str_info *)malloc(sizeof(struct _tty_str_info));
316 *temp = current_mode;
317 current_mode.next = temp;
319 isbeep = do_mode_change(¤t_mode, desc->str, desc->len);
320 if (!isbeep || did_beep)
321 continue; /* process one beep, ignore other envs */
322 } else if (desc->code == DT_END) {
324 temp = current_mode.next;
325 current_mode = *temp;
330 /* Add new block (call it temp) to result: */
331 temp = (tty_str_info *)malloc(sizeof(struct _tty_str_info));
332 *temp = current_mode;
333 if (last_result_block) {
334 last_result_block->next = temp;
335 last_result_block = temp;
338 last_result_block = temp;
342 /* special processing: need to insert a bell */
343 string_dictionary_binding *b;
344 b = string_dictionary_Lookup(termcap_dict,"B.bell");
346 temp->str = b->value;
347 temp->len = string_Length(temp->str);
349 /* shouldn't get here! */
354 if (desc->code == DT_STR) {
355 /* just combine string info with current mode: */
356 temp->str = desc->str;
357 temp->len = desc->len;
358 } else if (desc->code == DT_NL) {
359 /* make the new block a ' ' alignment block with an empty string */
360 temp->alignment = ' ';
366 if (last_result_block)
367 last_result_block->next = NULL;
372 #define max(a,b) ((a)>(b)?(a):(b))
375 line_width(int left_width,
379 if (center_width>0) {
380 if (left_width==0 && right_width==0)
381 return(center_width);
382 return(center_width+2+max(left_width,right_width)*2);
384 if (left_width && right_width)
385 return(1+left_width+right_width);
387 return(left_width+right_width);
392 calc_max_line_width(tty_str_info *info)
394 int max_line_width = 0;
399 for (; info; info=info->next) {
402 switch (info->alignment) {
418 printf("width: %d %d %d = %d\n", left, center, right,
419 line_width(left, center, right));
421 max_line_width = max(max_line_width,
422 line_width(left, center, right));
423 left = center = right = 0;
430 printf("width: %d %d %d = %d\n", left, center, right,
431 line_width(left, center, right));
433 max_line_width = max(max_line_width,
434 line_width(left, center, right));
436 return(max_line_width);
440 tty_filter(string text,
443 string text_copy = string_Copy(text);
444 string result_so_far = string_Copy("");
448 tty_str_info *info, *info_head;
451 desc = disp_get_cmds(text_copy, &number_of_strs, &number_of_lines);
452 info_head = info = convert_desc_to_tty_str_info(desc);
458 for (ptr=info; ptr; ptr=ptr->next) {
459 printf("%c: %s %s %s <%s>\n", ptr->alignment,
460 ptr->bold_p ? "(bold)" : "",
461 ptr->italic_p ? "(italic)" : "",
462 ptr->bell_p ? "(bell)" : "",
463 string_CreateFromData(ptr->str, ptr->len));
468 max_line_width = calc_max_line_width(info);
469 dprintf1("max width = %d\n", max_line_width);
472 string left, center, right;
473 int left_width, center_width, right_width;
476 left_width = center_width = right_width = 0;
477 left = string_Copy("");
478 center = string_Copy("");
479 right = string_Copy("");
481 for (; info && info->alignment!=' '; info=info->next) {
487 item = string_Copy("");
489 if (info->bold_p && use_fonts) {
490 if (temp = string_dictionary_Fetch(termcap_dict, "B.bold"))
491 item = string_Concat2(item, temp);
492 } else if (info->italic_p && use_fonts) {
493 if (temp = string_dictionary_Fetch(termcap_dict, "B.u"))
494 item = string_Concat2(item, temp);
496 temp = string_CreateFromData(info->str, info->len);
497 item = string_Concat2(item, temp);
500 if (info->bold_p && use_fonts) {
501 if (temp = string_dictionary_Fetch(termcap_dict, "E.bold"))
502 item = string_Concat2(item, temp);
503 } else if (info->italic_p && use_fonts) {
504 if (temp = string_dictionary_Fetch(termcap_dict, "E.u"))
505 item = string_Concat2(item, temp);
508 switch (info->alignment) {
511 left = string_Concat2(left, item);
512 left_width += info->len;
516 center = string_Concat2(center, item);
517 center_width += info->len;
521 right = string_Concat2(right, item);
522 right_width += info->len;
528 result_so_far = string_Concat2(result_so_far, left);
530 while (left_width < (max_line_width-center_width)/2 ) {
531 result_so_far = string_Concat2(result_so_far, " ");
534 result_so_far = string_Concat2(result_so_far, center);
535 left_width += center_width;
538 while (left_width<max_line_width-right_width) {
539 result_so_far = string_Concat2(result_so_far, " ");
542 result_so_far = string_Concat2(result_so_far, right);
543 free(left); free(center); free(right);
545 if (info && info->alignment == ' ') {
547 result_so_far = string_Concat2(result_so_far, "\r\n");
551 free_info(info_head);
553 if (number_of_lines &&
554 (result_so_far[string_Length(result_so_far)-1] != '\n'))
555 /* CRLF-terminate all results */
556 result_so_far = string_Concat2(result_so_far, "\r\n");
557 return(result_so_far);