Debian Codegraph @

listen/utils.c

raw
1 /*
2 * Copyright 1996, 1997 Heikki Hannikainen OH7LZB <[email protected]>
3 *
4 * Portions and ideas (like the ibm character mapping) from
5 * Tomi Manninen OH2BNS <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <curses.h>
12 #include <netdb.h>
13 #include <netinet/in.h>
14 #include <string.h>
15
16 #include "listen.h"
17
18 int color = 0; /* Colorized? */
19 int sevenbit = 1; /* Are we on a 7-bit terminal? */
20 int ibmhack = 0; /* IBM mapping? */
21
22 /* mapping of IBM codepage 437 chars 128-159 to ISO latin1 equivalents
23 * (158 and 159 are mapped to space)
24 */
25
26 static unsigned char ibm_map[32] = {
27 199, 252, 233, 226, 228, 224, 229, 231,
28 234, 235, 232, 239, 238, 236, 196, 197,
29 201, 230, 198, 244, 246, 242, 251, 249,
30 255, 214, 220, 162, 163, 165, 32, 32
31 };
32
33 /*
34 * Printf in Technicolor (TM) (available in selected theatres only)
35 */
36
37 void lprintf(int dtype, char *fmt, ...)
38 {
39 va_list args;
40 char str[1024];
41 chtype ch;
42 char *p;
43
44 va_start(args, fmt);
45 vsnprintf(str, 1024, fmt, args);
46 va_end(args);
47
48 if (color) {
49 for (p = str; *p != '\0'; p++) {
50 ch = *p;
51
52 if (sevenbit && ch > 127)
53 ch = '.';
54
55 if ((ch > 127 && ch < 160) && ibmhack)
56 ch = ibm_map[ch - 128] | A_BOLD;
57 else if ((ch < 32) && (ch != '\n'))
58 ch = (ch + 64) | A_REVERSE;
59
60 if ((dtype == T_ADDR) || (dtype == T_PROTOCOL)
61 || (dtype == T_AXHDR) || (dtype == T_IPHDR)
62 || (dtype == T_ROSEHDR) || (dtype == T_PORT)
63 || (dtype == T_TIMESTAMP))
64 ch |= A_BOLD;
65
66 ch |= COLOR_PAIR(dtype);
67
68 addch(ch);
69 }
70 } else {
71 for (p = str; *p != '\0'; p++)
72 if ((*p < 32 && *p != '\n')
73 || (*p > 126 && (unsigned char) *p < 160 && sevenbit))
74 *p = '.';
75 if (fputs(str, stdout) == -1)
76 exit(1);
77 if (fflush(stdout) == -1)
78 exit(1);
79 }
80 }
81
82 int initcolor(void)
83 {
84 initscr(); /* Start ncurses */
85 if (!has_colors()) {
86 endwin();
87 fprintf(stderr, "Your terminal does not support color\n");
88 exit(1);
89 }
90 start_color(); /* Initialize color support */
91 refresh(); /* Clear screen */
92 noecho(); /* Don't echo */
93 wattrset(stdscr, 0); /* Clear attributes */
94 scrollok(stdscr, TRUE); /* Like a scrolling Stone... */
95 leaveok(stdscr, TRUE); /* Cursor position doesn't really matter */
96 idlok(stdscr, TRUE); /* Use hardware ins/del of the terminal */
97 nodelay(stdscr, TRUE); /* Make getch() nonblocking */
98
99 /* Pick colors for each type */
100 init_pair(T_PORT, COLOR_GREEN, COLOR_BLACK);
101 init_pair(T_DATA, COLOR_WHITE, COLOR_BLACK);
102 init_pair(T_ERROR, COLOR_RED, COLOR_BLACK);
103 init_pair(T_PROTOCOL, COLOR_CYAN, COLOR_BLACK);
104 init_pair(T_AXHDR, COLOR_WHITE, COLOR_BLACK);
105 init_pair(T_IPHDR, COLOR_WHITE, COLOR_BLACK);
106 init_pair(T_ADDR, COLOR_GREEN, COLOR_BLACK);
107 init_pair(T_ROSEHDR, COLOR_WHITE, COLOR_BLACK);
108 init_pair(T_TIMESTAMP, COLOR_YELLOW, COLOR_BLACK);
109 init_pair(T_KISS, COLOR_MAGENTA, COLOR_BLACK);
110 init_pair(T_BPQ, COLOR_MAGENTA, COLOR_BLACK);
111 init_pair(T_TCPHDR, COLOR_BLUE, COLOR_BLACK);
112 init_pair(T_FLEXNET, COLOR_BLUE, COLOR_BLACK);
113 init_pair(T_OPENTRAC, COLOR_YELLOW, COLOR_BLACK);
114
115 return 1;
116 }
117
118 char *servname(int port, char *proto)
119 {
120 struct servent *serv;
121 static char str[6];
122
123 if ((serv = getservbyport(htons(port), proto)))
124 return serv->s_name;
125 else
126 snprintf(str, sizeof(str), "%i", port);
127
128 return str;
129 }