summaryrefslogtreecommitdiff
path: root/tprintf.c
blob: 0ca2375be503d780290a16421628116fd42a3704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * netsniff-ng - the packet sniffing beast
 * Copyright 2009, 2010 Daniel Borkmann.
 * Copyright 2013 Tobias Klauser.
 * Subject to the GPL, version 2.
 */

#define _BSD_SOURCE
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/ioctl.h>

#include "tprintf.h"
#include "die.h"
#include "locking.h"
#include "built_in.h"

#define term_trailing_size	5
#define term_starting_size	3

#define term_curr_size		(get_tty_size() - term_trailing_size)

static char buffer[1024];

static volatile size_t buffer_use = 0;

static struct spinlock buffer_lock;

static int get_tty_size(void)
{
#ifdef TIOCGSIZE
	struct ttysize ts = {0};

	return (ioctl(0, TIOCGSIZE, &ts) == 0 ?	ts.ts_cols : DEFAULT_TTY_SIZE);
#elif defined(TIOCGWINSZ)
	struct winsize ts;

	return (ioctl(0, TIOCGWINSZ, &ts) == 0 ? ts.ws_col : DEFAULT_TTY_SIZE);
#else
	return DEFAULT_TTY_SIZE;
#endif
}

static inline void __tprintf_flush_newline(void)
{
	int i;

	fputc('\n', stdout);
	for (i = 0; i < term_starting_size; ++i)
		fputc(' ', stdout);
}

static inline int __tprintf_flush_skip(char *buf, int i)
{
	int val = buf[i];

	if (val == ' ' || val == ',')
		return 1;

	return 0;
}

static void __tprintf_flush(void)
{
	size_t i;
	static ssize_t line_count = 0;
	ssize_t term_len = term_curr_size;

	for (i = 0; i < buffer_use; ++i) {
		if (buffer[i] == '\n') {
			term_len = term_curr_size;
			line_count = -1;
		}

		if (line_count == term_len) {
			__tprintf_flush_newline();
			line_count = term_starting_size;

			while (i < buffer_use &&
			       __tprintf_flush_skip(buffer, i))
				i++;
		}

		fputc(buffer[i], stdout);
		line_count++;
	}

	fflush(stdout);
	buffer_use = 0;
}

void tprintf_flush(void)
{
	spinlock_lock(&buffer_lock);
	__tprintf_flush();
	spinlock_unlock(&buffer_lock);
}

void tprintf_init(void)
{
	spinlock_init(&buffer_lock);

	setvbuf(stdout, NULL, _IONBF, 0);
	setvbuf(stderr, NULL, _IONBF, 0);
}

void tprintf_cleanup(void)
{
	tprintf_flush();
	spinlock_destroy(&buffer_lock);
}

void tprintf(char *msg, ...)
{
	ssize_t ret;
	ssize_t avail;
	va_list vl;

	spinlock_lock(&buffer_lock);

	avail = sizeof(buffer) - buffer_use;
	bug_on(avail < 0);

	va_start(vl, msg);
	ret = vsnprintf(buffer + buffer_use, avail, msg, vl);
	va_end(vl);

	if (ret < 0)
		panic("vsnprintf screwed up in tprintf!\n");
	if ((size_t) ret > sizeof(buffer))
		panic("No mem in tprintf left!\n");
	if (ret >= avail) {
		__tprintf_flush();

		avail = sizeof(buffer) - buffer_use;
		bug_on(avail < 0);

		va_start(vl, msg);
		ret = vsnprintf(buffer + buffer_use, avail, msg, vl);
		va_end(vl);

		if (ret < 0)
			panic("vsnprintf screwed up in tprintf!\n");
	}

	buffer_use += ret;

	spinlock_unlock(&buffer_lock);
}

void tputchar_safe(int c)
{
	unsigned char ch = (unsigned char)(c & 0xff);

	if (isprint(ch))
		tprintf("%c", ch);
	else
		tprintf("\\0x%02x", ch);
}

void tputs_safe(const char *str, size_t len)
{
	while (len--) {
		tputchar_safe(*str);
		str++;
	}
}
?h=nds-private-remove&id=d07830db1bdb254e4b50d366010b219286b8c937'>plain -rw-r--r--asoundef.h17098logplain -rw-r--r--atmel-abdac.h639logplain -rw-r--r--atmel-ac97c.h1342logplain -rw-r--r--compress_driver.h6772logplain -rw-r--r--control.h8704logplain -rw-r--r--core.h14380logplain -rw-r--r--cs35l33.h1034logplain -rw-r--r--cs35l34.h887logplain -rw-r--r--cs4231-regs.h8480logplain -rw-r--r--cs4271.h1417logplain -rw-r--r--cs42l52.h738logplain -rw-r--r--cs42l56.h1192logplain -rw-r--r--cs42l73.h507logplain -rw-r--r--cs8403.h8833logplain -rw-r--r--cs8427.h10649logplain -rw-r--r--da7213.h1178logplain -rw-r--r--da7218.h2681logplain -rw-r--r--da7219-aad.h2476logplain -rw-r--r--da7219.h1064logplain -rw-r--r--da9055.h914logplain -rw-r--r--designware_i2s.h2249logplain -rw-r--r--dmaengine_pcm.h6157logplain -rw-r--r--emu10k1.h91396logplain -rw-r--r--emu10k1_synth.h1382logplain -rw-r--r--emu8000.h4109logplain -rw-r--r--emu8000_reg.h10459logplain -rw-r--r--emux_legacy.h5503logplain -rw-r--r--emux_synth.h7649logplain -rw-r--r--es1688.h3618logplain -rw-r--r--gus.h20691logplain -rw-r--r--hda_chmap.h2621logplain -rw-r--r--hda_hwdep.h1412logplain -rw-r--r--hda_i915.h1645logplain -rw-r--r--hda_register.h9475logplain -rw-r--r--hda_regmap.h6714logplain -rw-r--r--hda_verbs.h17130logplain -rw-r--r--hdaudio.h18455logplain -rw-r--r--hdaudio_ext.h7119logplain -rw-r--r--hdmi-codec.h2290logplain -rw-r--r--hwdep.h2624logplain -rw-r--r--i2c.h3555logplain -rw-r--r--info.h7584logplain