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 /****************************************************************************/
28 #include "new_memory.h"
29 #include "new_string.h"
30 #include "string_dictionary_aux.h"
31 #include "formatter.h"
35 /***************************************************************************/
38 extern char *tgetstr(),*getenv();
40 extern speed_t ospeed;
46 /* Dictionary naming convention:
48 B.xxx is the termcap sequence to begin environment xxx.
49 E.xxx is the termcap sequence to end environment xxx.
53 static string_dictionary termcap_dict;
54 static char code_buf[10240], *code_buf_pos = code_buf, *code;
56 /* Define the following commands:
58 (Hopefully) shared with all devices:
62 @em Emphasis. User underline if available, else reverse video.
63 @bold Bold letters. If not available, reverse video, else underline.
64 @beep "bl" termcap entry, else "^G"
68 @blink "mb"/"me" termcap entry, else nothing.
69 @rv "so"/"se" termcap entry.
70 @u "us"/"ue" termcap entry.
73 #define TD_SET(k,v) (string_dictionary_Define(termcap_dict, (k), &ex)->value \
75 #define EXPAND(k) (code = code_buf_pos, tputs(tmp, 1, tty_outc), \
76 *code_buf_pos++ = 0, TD_SET(k, code))
87 int tty_filter_init(drivername, notfirst, pargc, argv)
93 static char st_buf[128];
94 char tc_buf[1024], *p = st_buf, *tmp, *term;
96 string_dictionary_binding *b;
97 int isrealtty = string_Eq(drivername, "tty");
101 ospeed = (tcgetattr(STDIN_FILENO, &tbuf) == 0) ? cfgetospeed(&tbuf) : 2400;
103 struct sgttyb sgttyb;
105 ospeed = (ioctl(0, TIOCGETP, &sgttyb) == 0) ? sgttyb.sg_ospeed : 2400;
108 if (termcap_dict == (string_dictionary) NULL)
109 termcap_dict = string_dictionary_Create(7);
111 if (!(term = getenv("TERM"))) { /* Only use termcap if $TERM. */
112 if (isrealtty && !notfirst)
113 /* only complain if initializing tty mode, and would be first
115 ERROR("$TERM not set. tty mode will be plain.\n");
119 * This is a temporary KLUDGE to get around the problem where some people
120 * might start zwgc in their ~/.startup.X and it hangs on the RISC/6000.
121 * Apparently, the call to tgetent() with the Athena console window causes
122 * the process to get stopped on tty access. Since the terminal type is
123 * "dumb" (set by tcsh), we can pretty much assume there isn't anything
124 * to setup from the termcap information.
126 else if (!strcmp(term, "dumb")) { }
129 tgetent(tc_buf, term);
131 /* Step 1: get all of {rv,bold,u,bell,blink} that are available. */
133 /* We cheat here, and ignore the padding (if any) specified for
134 the mode-change strings (it's a real pain to do "right") */
136 tmp = tgetstr("pc", &p);
137 PC = (tmp) ? *tmp : 0;
138 if (tmp = tgetstr("md",&p)) { /* bold ? */
140 tmp = tgetstr("me",&p);
143 if (tmp = tgetstr("mr",&p)) { /* reverse video? */
145 tmp = tgetstr("me",&p);
148 if (tmp = tgetstr("bl",&p)) { /* Bell ? */
150 TD_SET("E.bell", NULL);
152 if (tmp = tgetstr("mb",&p)) { /* Blink ? */
154 tmp = tgetstr("me",&p);
157 if (tmp = tgetstr("us",&p)) { /* Underline ? */
159 tmp = tgetstr("ue",&p);
162 if (tmp = tgetstr("so",&p)) { /* Standout ? */
164 tmp = tgetstr("se",&p);
168 /* Step 2: alias others to the nearest substitute */
170 /* Bold = so, else rv, else ul */
171 if (NULL == string_dictionary_Lookup(termcap_dict,"B.bold")) {
172 if(b = string_dictionary_Lookup(termcap_dict,"B.so")) {
173 TD_SET("B.bold",b->value);
175 string_dictionary_Lookup(termcap_dict,"E.so")->value);
176 } else if (b = string_dictionary_Lookup(termcap_dict,"B.rv")) {
177 TD_SET("B.bold",b->value);
179 string_dictionary_Lookup(termcap_dict,"E.rv")->value);
180 } else if (b = string_dictionary_Lookup(termcap_dict,"B.u")) {
181 TD_SET("B.bold",b->value);
183 string_dictionary_Lookup(termcap_dict,"E.u")->value);
188 if (NULL == string_dictionary_Lookup(termcap_dict,"B.bell")) {
189 TD_SET("B.bell","\007");
190 TD_SET("E.bell",NULL);
193 /* Underline -> nothing */
194 /* Blink -> nothing */
199 /***************************************************************************/
204 static int fixed_string_eq(pattern, text, text_length)
209 while (*pattern && text_length>0 && *pattern == *text) {
215 return(!*pattern && !text_length);
218 typedef struct _tty_str_info {
219 struct _tty_str_info *next;
224 char alignment; /* 'l', 'c', 'r', or ' ' to indicate newline */
225 unsigned int bold_p : 1;
226 unsigned int italic_p : 1;
227 unsigned int bell_p : 1;
228 unsigned int ignore: 1;
231 static void free_info(info)
234 tty_str_info *next_info;
237 next_info = info->next;
243 static int do_mode_change(current_mode_p, text, text_length)
244 tty_str_info *current_mode_p;
248 /* alignment commands: */
249 if (fixed_string_eq("left", text, text_length) ||
250 fixed_string_eq("l", text, text_length))
251 current_mode_p->alignment = 'l';
252 else if (fixed_string_eq("center", text, text_length) ||
253 fixed_string_eq("c", text, text_length))
254 current_mode_p->alignment = 'c';
255 else if (fixed_string_eq("right", text, text_length) ||
256 fixed_string_eq("r", text, text_length))
257 current_mode_p->alignment = 'r';
260 else if (fixed_string_eq("bold", text, text_length) ||
261 fixed_string_eq("b", text, text_length))
262 current_mode_p->bold_p = 1;
263 else if (fixed_string_eq("italic", text, text_length) ||
264 fixed_string_eq("i", text, text_length))
265 current_mode_p->italic_p = 1;
266 else if (fixed_string_eq("roman", text, text_length)) {
267 current_mode_p->bold_p = 0;
268 current_mode_p->italic_p = 0;
269 } else if (fixed_string_eq("beep", text, text_length)) {
270 current_mode_p->bell_p = 1;
274 /* commands ignored in tty mode: */
275 else if (fixed_string_eq("color", text, text_length) ||
276 fixed_string_eq("font", text, text_length)) {
277 current_mode_p->ignore = 1;
282 static tty_str_info *convert_desc_to_tty_str_info(desc)
286 tty_str_info *result = NULL;
287 tty_str_info *last_result_block = NULL;
288 int isbeep, did_beep = 0;
290 #if !defined(SABER) && defined(__STDC__)
291 tty_str_info current_mode = { NULL, "", 0, 'l', 0 , 0, 0, 0};
293 /* This is needed due to a bug in saber, and lack of pre-ANSI support. */
294 tty_str_info current_mode;
296 current_mode.next = NULL;
297 current_mode.str = "";
298 current_mode.len = 0;
299 current_mode.alignment = 'l';
300 current_mode.bold_p = 0;
301 current_mode.italic_p = 0;
302 current_mode.bell_p = 0;
303 current_mode.ignore = 0;
306 for (; desc->code!=DT_EOF; desc=desc->next) {
308 /* Handle environments: */
309 if (desc->code == DT_ENV) {
311 temp = (tty_str_info *)malloc(sizeof(struct _tty_str_info));
312 *temp = current_mode;
313 current_mode.next = temp;
315 isbeep = do_mode_change(¤t_mode, desc->str, desc->len);
316 if (!isbeep || did_beep)
317 continue; /* process one beep, ignore other envs */
318 } else if (desc->code == DT_END) {
320 temp = current_mode.next;
321 current_mode = *temp;
326 /* Add new block (call it temp) to result: */
327 temp = (tty_str_info *)malloc(sizeof(struct _tty_str_info));
328 *temp = current_mode;
329 if (last_result_block) {
330 last_result_block->next = temp;
331 last_result_block = temp;
334 last_result_block = temp;
338 /* special processing: need to insert a bell */
339 string_dictionary_binding *b;
340 b = string_dictionary_Lookup(termcap_dict,"B.bell");
342 temp->str = b->value;
343 temp->len = string_Length(temp->str);
345 /* shouldn't get here! */
350 if (desc->code == DT_STR) {
351 /* just combine string info with current mode: */
352 temp->str = desc->str;
353 temp->len = desc->len;
354 } else if (desc->code == DT_NL) {
355 /* make the new block a ' ' alignment block with an empty string */
356 temp->alignment = ' ';
362 if (last_result_block)
363 last_result_block->next = NULL;
368 #define max(a,b) ((a)>(b)?(a):(b))
370 static int line_width(left_width, center_width, right_width)
375 if (center_width>0) {
376 if (left_width==0 && right_width==0)
377 return(center_width);
378 return(center_width+2+max(left_width,right_width)*2);
380 if (left_width && right_width)
381 return(1+left_width+right_width);
383 return(left_width+right_width);
387 static int calc_max_line_width(info)
390 int max_line_width = 0;
395 for (; info; info=info->next) {
398 switch (info->alignment) {
414 printf("width: %d %d %d = %d\n", left, center, right,
415 line_width(left, center, right));
417 max_line_width = max(max_line_width,
418 line_width(left, center, right));
419 left = center = right = 0;
426 printf("width: %d %d %d = %d\n", left, center, right,
427 line_width(left, center, right));
429 max_line_width = max(max_line_width,
430 line_width(left, center, right));
432 return(max_line_width);
435 string tty_filter(text, use_fonts)
439 string text_copy = string_Copy(text);
440 string result_so_far = string_Copy("");
444 tty_str_info *info, *info_head;
447 desc = disp_get_cmds(text_copy, &number_of_strs, &number_of_lines);
448 info_head = info = convert_desc_to_tty_str_info(desc);
454 for (ptr=info; ptr; ptr=ptr->next) {
455 printf("%c: %s %s %s <%s>\n", ptr->alignment,
456 ptr->bold_p ? "(bold)" : "",
457 ptr->italic_p ? "(italic)" : "",
458 ptr->bell_p ? "(bell)" : "",
459 string_CreateFromData(ptr->str, ptr->len));
464 max_line_width = calc_max_line_width(info);
465 dprintf1("max width = %d\n", max_line_width);
468 string left, center, right;
469 int left_width, center_width, right_width;
472 left_width = center_width = right_width = 0;
473 left = string_Copy("");
474 center = string_Copy("");
475 right = string_Copy("");
477 for (; info && info->alignment!=' '; info=info->next) {
483 item = string_Copy("");
485 if (info->bold_p && use_fonts) {
486 if (temp = string_dictionary_Fetch(termcap_dict, "B.bold"))
487 item = string_Concat2(item, temp);
488 } else if (info->italic_p && use_fonts) {
489 if (temp = string_dictionary_Fetch(termcap_dict, "B.u"))
490 item = string_Concat2(item, temp);
492 temp = string_CreateFromData(info->str, info->len);
493 item = string_Concat2(item, temp);
496 if (info->bold_p && use_fonts) {
497 if (temp = string_dictionary_Fetch(termcap_dict, "E.bold"))
498 item = string_Concat2(item, temp);
499 } else if (info->italic_p && use_fonts) {
500 if (temp = string_dictionary_Fetch(termcap_dict, "E.u"))
501 item = string_Concat2(item, temp);
504 switch (info->alignment) {
507 left = string_Concat2(left, item);
508 left_width += info->len;
512 center = string_Concat2(center, item);
513 center_width += info->len;
517 right = string_Concat2(right, item);
518 right_width += info->len;
524 result_so_far = string_Concat2(result_so_far, left);
526 while (left_width < (max_line_width-center_width)/2 ) {
527 result_so_far = string_Concat2(result_so_far, " ");
530 result_so_far = string_Concat2(result_so_far, center);
531 left_width += center_width;
534 while (left_width<max_line_width-right_width) {
535 result_so_far = string_Concat2(result_so_far, " ");
538 result_so_far = string_Concat2(result_so_far, right);
539 free(left); free(center); free(right);
541 if (info && info->alignment == ' ') {
543 result_so_far = string_Concat2(result_so_far, "\r\n");
547 free_info(info_head);
549 if (number_of_lines &&
550 (result_so_far[string_Length(result_so_far)-1] != '\n'))
551 /* CRLF-terminate all results */
552 result_so_far = string_Concat2(result_so_far, "\r\n");
553 return(result_so_far);