aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
blob: 57283237666a1f9aca5deb5402413827b575bbe3 (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
/**
 * @file misc.c
 * @author Joe Wingbermuehle
 * @date 2004-2006
 *
 * @brief Miscellaneous functions and macros.
 *
 */

#include "jwm.h"
#include "misc.h"

static int IsSpace(char ch);
static int IsSymbolic(char ch);
static char *GetSymbolName(const char *str);
static void ReplaceSymbol(char **str, const char *name, const char *value);

/** Determine if a character is a space character. */
int IsSpace(char ch) {
	switch(ch) {
	case ' ':
	case '\t':
	case '\n':
	case '\r':
		return 1;
	default:
		return 0;
	}
}

/** Determine if a character is a valid for a shell variable. */
int IsSymbolic(char ch) {

	if(ch >= 'A' && ch <= 'Z') {
		return 1;
	} else if(ch >= 'a' && ch <= 'z') {
		return 1;
	} else if(ch >= '0' && ch <= '9') {
		return 1;
	} else if(ch == '_') {
		return 1;
	} else {
		return 0;
	}

}

/** Get the name of a shell variable (returns a copy). */
char *GetSymbolName(const char *str) {

	char *temp;
	int stop;

	if(*str == '$') {
		temp = Allocate(2);
		temp[0] = '$';
		temp[1] = 0;
	} else {
		for(stop = 0; IsSymbolic(str[stop]); stop++);
		temp = Allocate(stop + 1);
		memcpy(temp, str, stop);
		temp[stop] = 0;
	}

	return temp;

}

/** Replace "name" with "value" in str (reallocates if needed). */
void ReplaceSymbol(char **str, const char *name, const char *value) {

	char *temp;
	int strLength;
	int nameLength;
	int valueLength;
	int x;

	Assert(str);
	Assert(name);

	strLength = strlen(*str);
	nameLength = strlen(name) + 1;
	if(value) {
		valueLength = strlen(value);
	} else {
		valueLength = 0;
	}

	if(valueLength > nameLength) {
		temp = Allocate(strLength - nameLength + valueLength + 1);
		strcpy(temp, *str);
		Release(*str);
		*str = temp;
	}

	temp = strstr(*str, name);
	Assert(temp);
	--temp; /* Account for the "$" */

	if(nameLength > valueLength) {

		/* Move left */
		for(x = 0; temp[x]; x++) {
			temp[x] = temp[x + nameLength - valueLength];
		}
		temp[x] = temp[x + nameLength - valueLength];

	} else if(nameLength < valueLength) {

		/* Move right */
		for(x = strlen(temp); x >= 0; x--) {
			temp[x + valueLength - nameLength] = temp[x];
		}

	}


	if(value) {
		memcpy(temp, value, valueLength);
	}

}

/** Perform shell-like macro path expansion. */
void ExpandPath(char **path) {

	char *name;
	char *value;
	int x;

	Assert(path);

	for(x = 0; (*path)[x]; x++) {

		if((*path)[x] == '$') {
			name = GetSymbolName(*path + x + 1);
			value = getenv(name);
			ReplaceSymbol(path, name, value);
			Release(name);
			if(value) {
				x += strlen(value) - 1;
			}
		}

	}

}

/** Trim leading and trailing whitespace from a string. */
void Trim(char *str) {

	int length;
	int start;
	int x;

	Assert(str);

	length = strlen(str);

	/* Determine how much to cut off of the left. */
	for(start = 0; IsSpace(str[start]); start++);

	/* Trim the left. */
	if(start > 0) {
		length -= start;
		for(x = 0; x < length + 1; x++) {
			str[x] = str[x + start];
		}
	}

	/* Trim the right. */
	while(IsSpace(str[length - 1])) {
		--length;
		str[length] = 0;
	}

}

/** Copy a string. */
char *CopyString(const char *str) {

	char *temp;
	int len;

	if(!str) {
		return NULL;
	}

	len = strlen(str) + 1;
	temp = Allocate(len);
	memcpy(temp, str, len);

	return temp;

}