summaryrefslogtreecommitdiffstats
path: root/tapset/string.stp
blob: 59ba74ee89294b80f0f78dd9944d77ed5ac97606 (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
// Standard string functions tapset.
// Copyright (C) 2009 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.

/**
 * sfunction strlen - Returns the length of a string.
 * @s: the string
 *
 * Description: Returns the lenght of the string, which can be zero up
 * to MAXSTRINGLEN.
 */
function strlen:long(s:string) %{ /* pure */ /* unprivileged */
        THIS->__retvalue = strlen(THIS->s);
%}

/**
 * sfunction substr - Returns a substring.
 * @str: The string to take a substring from
 * @start: Starting position.  0 = start of the string.
 * @length: Length of string to return.
 *
 * Description: Returns the substring of the up to the given length
 * starting at the given start position.
 */
function substr:string(str:string,start:long, length:long) %{ /* pure */ /* unprivileged */
	int length = THIS->length >= MAXSTRINGLEN ? MAXSTRINGLEN : THIS->length + 1;
	if (THIS->start >= 0 && length > 0 && THIS->start < strlen(THIS->str))
		strlcpy(THIS->__retvalue, THIS->str + THIS->start, length);
%}

/**
 * sfunction stringat - Returns the char at a given position in the string.
 * @str: The string to fetch the character from.
 * @pos: The position to get the character from.  0 = start of the string.
 *
 * Returns the character at a given position in the string or zero if the
 * string doesn't have as many characters.
 */
function stringat:long(str:string, pos:long) %{ /* pure */ /* unprivileged */
	if (THIS->pos >= 0 && THIS->pos < strlen(THIS->str))
		THIS->__retvalue = THIS->str[THIS->pos];
	else
		THIS->__retvalue = 0;
%}

/**
 * sfunction isinstr - Returns whether a string is a substring of another string.
 * @s1: String to search in.
 * @s2: Substring to find.
 *
 * Description: Returns 1 if s2 is in s1, otherwise 0.
 */
function isinstr:long(s1:string,s2:string) %{ /* pure */ /* unprivileged */
	if (strstr(THIS->s1,THIS->s2) != NULL)
		THIS->__retvalue = 1;
	else
		THIS->__retvalue = 0;
%}

/**
 * sfunction text_str - Escape any non-printable chars in a string.
 * @input: The string to escape.
 *
 * Description Takes a string, and any ASCII characters that are not
 * printable are replaced by the corresponding escape sequence in the
 * returned string.
 */
function text_str:string(input:string) 
%{ /* pure */ /* unprivileged */
	_stp_text_str(THIS->__retvalue, THIS->input, 0, 0, 0);
%}

/**
 * sfunction text_strn - Escape any non-printable chars in a string.
 * @input: The string to escape.
 * @len: Maximum length of string to return. 0 means MAXSTRINGLEN.
 * @quoted: Put double quotes around the string. If input string is
 * truncated it will have "..." after the second quote.
 *
 * Description Takes a string, and any ASCII characters that are not
 * printable are replaced by the corresponding escape sequence in the
 * returned string.
 */
function text_strn:string(input:string, len:long, quoted:long)
%{ /* pure */ /* unprivileged */
	_stp_text_str(THIS->__retvalue, THIS->input, THIS->len, THIS->quoted, 0);
%}

/**
 * sfunction tokenize - Return the next non-empty token in a string.
 * @input: String to tokenize. If NULL, returns the next non-empty token
 * in the string passed in the previous call to tokenize().
 * @delim: Token delimiter. Set of characters that delimit the tokens.
 * 
 * Description: Given a string and a token delimiter, return the next
 * non-empty token in the string or blank when no more non-empty tokens
 * are left.
 */
function tokenize:string(input:string, delim:string)
%{ /* unprivileged */
	static char str[MAXSTRINGLEN];
	static char *str_start;
	static char *str_end;
	char *token = NULL;
	char *token_end = NULL;

	if (THIS->input[0]) {
		strncpy(str, THIS->input, MAXSTRINGLEN);
		str_start = &str[0];
		str_end = &str[0] + strlen(str);
	}
	do {
		token = strsep(&str_start, THIS->delim);
	} while (token && !token[0]);
	if (token) {
		token_end = (str_start ? str_start - 1 : str_end);
		memcpy(THIS->__retvalue, token, token_end - token + 1);
	}
%}

/**
 * sfunction - str_replace Replaces all instances of a substring with another.
 * @prnt_str: The string to search and replace in.
 * @srch_str: The substring which is used to search in prnt_str string.
 * @rplc_str: The substring which is used to replace srch_str.
 *
 * Description: Returns the given string with substrings replaced.
 */
function str_replace:string (prnt_str:string, srch_str:string, rplc_str:string)
%{ /* pure */ /* unprivileged */
	char *ptr = THIS->prnt_str;
	char *ptr_base = THIS->prnt_str;
	int strlen_srch_str = strlen(THIS->srch_str);

	if(strlen_srch_str == 0) {
		strlcat(THIS->__retvalue, ptr_base, MAXSTRINGLEN);
		return;
	}

	while((ptr = strstr(ptr, THIS->srch_str)) != NULL) {

		*ptr = '\0';
		strlcat(THIS->__retvalue, ptr_base, MAXSTRINGLEN);
		strlcat(THIS->__retvalue, THIS->rplc_str, MAXSTRINGLEN);
		ptr = ptr + strlen_srch_str;
		ptr_base = ptr;
	}

	strlcat(THIS->__retvalue, ptr_base, MAXSTRINGLEN);
	return;
%}

/**
 * sfunction - strtol - Convert a string to a long.
 * @str: String to convert.
 * @base: The base to use
 */
function strtol:long(str:string, base:long)
%{ /* pure */ /* unprivileged */
	THIS->__retvalue = simple_strtol(THIS->str, NULL, THIS->base);
%}