2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Copyright 2013 Tobias Klauser.
5 * Subject to the GPL, version 2.
12 #include <sys/ioctl.h>
19 #define term_trailing_size 5
20 #define term_starting_size 3
22 #define term_curr_size (get_tty_size() - term_trailing_size)
24 static char buffer
[1024];
26 static volatile size_t buffer_use
= 0;
28 static struct spinlock buffer_lock
;
30 static int get_tty_size(void)
33 struct ttysize ts
= {0};
35 return (ioctl(0, TIOCGSIZE
, &ts
) == 0 ? ts
.ts_cols
: DEFAULT_TTY_SIZE
);
36 #elif defined(TIOCGWINSZ)
39 return (ioctl(0, TIOCGWINSZ
, &ts
) == 0 ? ts
.ws_col
: DEFAULT_TTY_SIZE
);
41 return DEFAULT_TTY_SIZE
;
45 static inline void __tprintf_flush_newline(void)
50 for (i
= 0; i
< term_starting_size
; ++i
)
54 static inline int __tprintf_flush_skip(char *buf
, int i
)
58 if (val
== ' ' || val
== ',')
64 static void __tprintf_flush(void)
67 static ssize_t line_count
= 0;
68 ssize_t term_len
= term_curr_size
;
69 size_t color_open
= 0;
71 for (i
= 0; i
< buffer_use
; ++i
) {
72 if (buffer
[i
] == '\n') {
73 term_len
= term_curr_size
;
77 /* Start of an color escape sequence? */
78 if (buffer
[i
] == 033) {
79 if ((i
+ 1) < buffer_use
&& buffer
[i
+ 1] == '[')
83 if (color_open
== 0 && line_count
>= term_len
) {
84 __tprintf_flush_newline();
85 line_count
= term_starting_size
;
87 while (i
< buffer_use
&&
88 __tprintf_flush_skip(buffer
, i
))
92 /* End of the color escape sequence? */
93 if (color_open
> 0 && buffer
[i
] == 'm')
96 fputc(buffer
[i
], stdout
);
104 void tprintf_flush(void)
106 spinlock_lock(&buffer_lock
);
108 spinlock_unlock(&buffer_lock
);
111 void tprintf_init(void)
113 spinlock_init(&buffer_lock
);
115 setvbuf(stdout
, NULL
, _IONBF
, 0);
116 setvbuf(stderr
, NULL
, _IONBF
, 0);
119 void tprintf_cleanup(void)
122 spinlock_destroy(&buffer_lock
);
125 void tprintf(char *msg
, ...)
131 spinlock_lock(&buffer_lock
);
133 avail
= sizeof(buffer
) - buffer_use
;
137 ret
= vsnprintf(buffer
+ buffer_use
, avail
, msg
, vl
);
141 panic("vsnprintf screwed up in tprintf!\n");
142 if ((size_t) ret
> sizeof(buffer
))
143 panic("No mem in tprintf left!\n");
147 avail
= sizeof(buffer
) - buffer_use
;
151 ret
= vsnprintf(buffer
+ buffer_use
, avail
, msg
, vl
);
155 panic("vsnprintf screwed up in tprintf!\n");
160 spinlock_unlock(&buffer_lock
);
163 void tputchar_safe(int c
)
165 unsigned char ch
= (unsigned char)(c
& 0xff);
170 tprintf("\\0x%02x", ch
);
173 void tputs_safe(const char *str
, size_t len
)