summaryrefslogtreecommitdiffstats
path: root/runtime/string.c
blob: 21a6c70e23ac6db8031222d2eeee99c6ed36080a (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* -*- linux-c -*- 
 * String Functions
 * Copyright (C) 2005, 2006 Red Hat Inc.
 *
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */
#ifndef _STRING_C_
#define _STRING_C_

#include <linux/config.h>
#include "string.h"

/** @file string.c
 * @brief Implements String type.
 */
/** @addtogroup string String Functions
 *
 * @{
 */

/** Initialize a String for our use.
 * This grabs one of the global Strings for our temporary use.
 *
 * @param num Number of the preallocated String to use. 
 * #STP_NUM_STRINGS are statically allocated for our use. The
 * translator (or author) should be sure to grab a free one.
 * @returns An empty String.
 */

String _stp_string_init (int num)
{
	String str;

	if (num >= STP_NUM_STRINGS || num < 0) {
		_stp_error ("_stp_string_init internal error: requested string exceeded allocated number or was negative");
		return NULL;
	}
	str = &_stp_string[num][smp_processor_id()];
	str->len = 0;
	str->buf[0] = 0;
	return str;
}


/** Sprintf into a String.
 * Like printf, except output goes into a String.
 * Safe because overflowing the buffer is not allowed.
 * Size is limited by length of String, #STP_STRING_SIZE.
 *
 * @param str String
 * @param fmt A printf-style format string followed by a 
 * variable number of args.
 */
void _stp_sprintf (String str, const char *fmt, ...)
{
	int num;
	va_list args;
	if (str == _stp_stdout) {
		int cpu = smp_processor_id();
		char *buf = &_stp_pbuf[cpu][STP_PRINT_BUF_START] + _stp_pbuf_len[cpu];
		int size = STP_PRINT_BUF_LEN -_stp_pbuf_len[cpu] + 1;
		va_start(args, fmt);
		num = _stp_vsnprintf(buf, size, fmt, args);
		va_end(args);
		if (unlikely(num >= size)) { 
			/* overflowed the buffer */
			if (_stp_pbuf_len[cpu] == 0) {
				_stp_pbuf_len[cpu] = STP_PRINT_BUF_LEN;
				_stp_print_flush();
			} else {
				*buf ='\0';
				_stp_print_flush();
				va_start(args, fmt);
				_stp_vsprintf(_stp_stdout, fmt, args); 
				va_end(args);
			}
		} else {
			_stp_pbuf_len[cpu] += num;
		}

	} else {
		va_start(args, fmt);
		num = _stp_vscnprintf(str->buf + str->len, STP_STRING_SIZE - str->len, fmt, args);
		va_end(args);
		if (likely(num > 0))
			str->len += num;
	}
}

/** Vsprintf into a String
 * Use this if your function already has a va_list.
 * You probably want _stp_sprintf().
 */
void _stp_vsprintf (String str, const char *fmt, va_list args)
{
	int num;
	if (str == _stp_stdout) {
		int cpu = smp_processor_id();
		char *buf = &_stp_pbuf[cpu][STP_PRINT_BUF_START] + _stp_pbuf_len[cpu];
		int size = STP_PRINT_BUF_LEN -_stp_pbuf_len[cpu] + 1;
		num = _stp_vsnprintf(buf, size, fmt, args);
		if (num < size)
			_stp_pbuf_len[cpu] += num;
		else {
			_stp_pbuf_len[cpu] = STP_PRINT_BUF_LEN;
			_stp_print_flush();
		}
	} else {
		num = _stp_vscnprintf(str->buf + str->len, STP_STRING_SIZE - str->len, fmt, args);
		if (num > 0)
			str->len += num;
	}
}

/** ConCATenate (append) a C string to a String.
 * Like strcat().
 * @param str1 String
 * @param str2 C string (char *)
 * @sa _stp_string_cat
 */
void _stp_string_cat_cstr (String str1, const char *str2)
{
	int num = strlen (str2);
	if (str1 == _stp_stdout) {
		char *buf;
		int cpu = smp_processor_id();
		int size = STP_PRINT_BUF_LEN -_stp_pbuf_len[cpu];
		if (num >= size) {
			_stp_print_flush();
			if (num > STP_PRINT_BUF_LEN)
				num = STP_PRINT_BUF_LEN;
		}
		buf = &_stp_pbuf[cpu][STP_PRINT_BUF_START] + _stp_pbuf_len[cpu];
		memcpy (buf, str2, num);
		_stp_pbuf_len[cpu] += num;
	} else {
		int size = STP_STRING_SIZE - str1->len - 1; 
		if (num > size)
			num = size;
		memcpy (str1->buf + str1->len, str2, num);
		str1->len += num;
		str1->buf[str1->len] = 0;
	}
}

/** ConCATenate (append) a String to a String.
 * Like strcat().
 * @param str1 String
 * @param str2 String
 * @sa _stp_string_cat
 */
void _stp_string_cat_string (String str1, String str2)
{
	if (str2->len)
		_stp_string_cat_cstr (str1, str2->buf);
}


void _stp_string_cat_char (String str1, const char c)
{
	if (str1 == _stp_stdout) {
		char *buf;
		int cpu = smp_processor_id();
		int size = STP_PRINT_BUF_LEN -_stp_pbuf_len[cpu];
		if (1 >= size)
			_stp_print_flush();
		buf = &_stp_pbuf[cpu][STP_PRINT_BUF_START] + _stp_pbuf_len[cpu];
		buf[0] = c;
		buf[1] = 0;
		_stp_pbuf_len[cpu] ++;
	} else {
		int size = STP_STRING_SIZE - str1->len - 1; 
		if (size > 0) {
			char *buf = str1->buf + str1->len;
			buf[0] = c;
			buf[1] = 0;
			str1->len ++;
		}
	}
}

/** Get a pointer to String's buffer
 * For rare cases when a C string is needed and you have a String.
 * One example is when you want to print a String with _stp_printf().
 * @param str String
 * @returns A C string (char *)
 * @note Readonly. Don't write to this pointer or it will mess up
 * the internal String state and probably mess up your output or crash something.
 */
char * _stp_string_ptr (String str)
{
	return str->buf;
}

/** ConCATenate (append) a String or C string to a String.
 * This macro selects the proper function to call.
 * @param str1 A String
 * @param str2 A String or C string (char *)
 * @sa _stp_string_cat_cstr _stp_string_cat_string
 */
#define _stp_string_cat(str1, str2)					\
  ({                                                            \
	  if (__builtin_types_compatible_p (typeof (str2), char[])) {	\
		  char *x = (char *)str2;				\
		  _stp_string_cat_cstr(str1,x);				\
	  } else {							\
		  String x = (String)str2;				\
		  _stp_string_cat_string(str1,x);			\
	  }								\
  })

#include "loc2c-runtime.h"

/** Return a printable text string.
 *
 * Takes a string, and any ASCII characters that are not printable are
 * replaced by the corresponding escape sequence in the returned
 * string.
 *
 * @param outstr Output string pointer
 * @param in Input string pointer
 * @param len Maximum length of string to return not including terminating 0.
 * 0 means MAXSTRINGLEN.
 * @param quoted Put double quotes around the string. If input string is truncated
 * in will have "..." after the second quote.
 * @param user Set this to indicate the input string pointer is a userspace pointer.
 */
void _stp_text_str(char *outstr, char *in, int len, int quoted, int user)
{
	const int length = len;
	char c, *out = outstr;

	if (len == 0 || len > STP_STRING_SIZE-1)
		len = STP_STRING_SIZE-1;
	if (quoted) {
		len -= 2;
		*out++ = '\"';
	}

	if (user)
		c = deref(1,(char __user *)in);
	else
		c = *in;

	while (c && len > 0) {
		int num = 1;
		if (isprint(c) && isascii(c))
			*out++ = c;
		else {
			switch (c) {
			case '\a':
			case '\b':
			case '\f':
			case '\n':
			case '\r':
			case '\t':
			case '\v':
				num = 2;
				break;
			default:
				if (c > 077)
					num = 4;
				else if (c > 07)
					num = 3;
				else
					num = 2;
				break;
			}
			
			if (len < num)
				break;

			*out++ = '\\';
			switch (c) {
			case '\a':
				*out++ = 'a';
				break;
			case '\b':
				*out++ = 'b';
				break;
			case '\f':
				*out++ = 'f';
				break;
			case '\n':
				*out++ = 'n';
				break;
			case '\r':
				*out++ = 'r';
				break;
			case '\t':
				*out++ = 't';
				break;
			case '\v':
				*out++ = 'v';
				break;
			default:                  /* output octal representation */
				if (c > 077)
					*out++ = to_oct_digit(c >> 6);
				if (c > 07)
					*out++ = to_oct_digit((c & 070) >> 3);
				*out++ = to_oct_digit(c & 07);
				break;
			}
		}
		len -= num;
		in++;
		if (user)
			c = deref(1,(char __user *)in);
		else
			c = *in;
	}

	if (quoted) {
		if (c) {
			out = out - 3 + len;
			*out++ = '\"';
			*out++ = '.';
			*out++ = '.';
			*out++ = '.';
		} else
			*out++ = '\"';
	}
	*out = '\0';
	return;
deref_fault:
	strlcpy (outstr, "<unknown>", len);
}

/** @} */
#endif /* _STRING_C_ */