forked from kilobyte/kbtin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.c
363 lines (340 loc) · 10.6 KB
/
vars.c
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "tintin.h"
#include "protos/action.h"
#include "protos/files.h"
#include "protos/glob.h"
#include "protos/globals.h"
#include "protos/print.h"
#include "protos/parse.h"
#include "protos/utils.h"
#include "protos/string.h"
#include "protos/alias.h"
#include "protos/chinese.h"
#include "protos/hash.h"
#include "protos/eval.h"
#include "protos/math.h"
#include "protos/path.h"
#include "protos/session.h"
#include "protos/ticks.h"
#include "protos/unicode.h"
#include "protos/vars.h"
/*****************************************/
/* delete ; and unpaired/underflowing {} */
/*****************************************/
static int defang_var(char *result, const char *var)
{
int obraces=0, level=0;
for (const char *p=var; *p; p++)
{
if (*p == '{')
obraces++, level++;
else if (*p == '}')
if (level>0)
level--;
}
obraces -= level; /* # of unpaired */
level = 0;
char *r = result;
for (const char *p=var; *p; p++)
{
if (*p == ';')
; /* no replacement */
else if (*p == '{')
{
*r++ = (obraces-->0)? '{' : '(';
level++;
}
else if (*p == '}')
{
if (level > 0)
*r++ = '}', level--;
else
*r++ = ')';
}
else
*r++ = *p;
}
return r - result;
}
/*************************************************************************/
/* copy the arg text into the result-space, but substitute the variables */
/* %0..%9 with the real variables */
/*************************************************************************/
static void substitute_ivars(const char *arg, char *result)
{
int nest = 0;
int numands, n;
const char *ARG=arg;
int valuelen, len=strlen(arg);
if (!pvars)
{
strcpy(result, arg);
return;
}
while (*arg)
{
if (*arg == '%')
{ /* substitute variable */
numands = 1; /* at least one */
while (*(arg + numands) == '%')
numands++;
if (isadigit(*(arg + numands)) && numands == (nest + 1))
{
n = *(arg + numands) - '0';
valuelen=strlen((*pvars)[n]);
if ((len+=valuelen-numands-1) > BUFFER_SIZE-10)
{
len-=valuelen-numands-1;
if (!aborting)
{
tintin_eprintf(0, "#ERROR: command+vars too long in {%s}.", ARG);
aborting=true;
}
goto novar1;
}
strcpy(result, (*pvars)[n]);
arg = arg + numands + 1;
result += valuelen;
}
else
{
novar1:
memcpy(result, arg, numands + 1);
arg += numands + 1;
result += numands + 1;
}
in_alias=false; /* not a simple alias */
}
else if (*arg == '$')
{ /* substitute variable */
numands = 1; /* at least one */
while (*(arg + numands) == '$')
numands++;
if (isadigit(*(arg + numands)) && numands == (nest + 1))
{
n = *(arg + numands) - '0';
valuelen=strlen((*pvars)[n]);
if ((len+=valuelen-numands-1) > BUFFER_SIZE-10)
{
len-=valuelen-numands-1;
if (!aborting)
{
tintin_eprintf(0, "#ERROR: command+vars too long in {%s}.", ARG);
aborting=true;
}
goto novar2;
}
result += defang_var(result, (*pvars)[n]);
arg = arg + numands + 1;
}
else
{
novar2:
memcpy(result, arg, numands);
arg += numands;
result += numands;
}
in_alias=false; /* not a simple alias */
}
else if (*arg == BRACE_OPEN)
{
nest++;
*result++ = *arg++;
}
else if (*arg == BRACE_CLOSE)
{
nest--;
*result++ = *arg++;
}
else if (*arg == '\\' && nest == 0)
{
while (*arg == '\\')
*result++ = *arg++;
if (*arg == '%')
{
result--;
*result++ = *arg++;
if (*arg)
*result++ = *arg++;
}
}
else
*result++ = *arg++;
}
*result = '\0';
}
void set_variable(const char *left, const char *right, struct session *ses)
{
set_hash(ses->myvars, left, right);
varnum++; /* we don't care for exactness of this */
if (ses->mesvar[MSG_VARIABLE])
tintin_printf(ses, "#Ok. $%s is now set to {%s}.", left, right);
}
static int builtin_var(const char *varname, char *value, struct session *ses)
{
if (!strcmp(varname, "secstotick"))
sprintf(value, "%lld", timetilltick(ses)/NANO);
else if (!strcmp(varname, "TIMETOTICK"))
{
timens_t tt = timetilltick(ses);
sprintf(value, "%lld.%09ld", tt/NANO, labs(tt%NANO));
}
else if (!strcmp(varname, "LINES"))
sprintf(value, "%d", LINES);
else if (!strcmp(varname, "COLS"))
sprintf(value, "%d", COLS);
else if (!strcmp(varname, "PATH"))
path2var(value, ses);
else if (!strcmp(varname, "IDLETIME"))
nsecstr(value, current_time()-ses->idle_since);
else if (!strcmp(varname, "SERVERIDLE"))
nsecstr(value, current_time()-ses->server_idle_since);
else if (!strcmp(varname, "USERIDLE"))
nsecstr(value, current_time()-idle_since);
else if (!strcmp(varname, "LINENUM"))
sprintf(value, "%llu", ses->linenum);
else if (_ && (!strcmp(varname, "LINE")
|| !strcmp(varname, "_")))
strcpy(value, _);
else if (!strcmp(varname, "SESSION"))
strcpy(value, ses->name);
else if (!strcmp(varname, "SESSIONS"))
seslist(value);
else if (!strcmp(varname, "ASESSION"))
strcpy(value, activesession->name);
else if (!strcmp(varname, "LOGFILE"))
strcpy(value, ses->logfile?ses->logname:"");
else if (!strcmp(varname, "RANDOM") || !strcmp(varname, "_random"))
sprintf(value, "%d", rand());
else if (!strcmp(varname, "_time"))
{
timens_t age = current_time() - start_time;
sprintf(value, "%lld", age/NANO);
}
else if (!strcmp(varname, "STARTTIME"))
sprintf(value, "%lld.%09lld", start_time/NANO, start_time%NANO);
else if (!strcmp(varname, "TIME"))
{
timens_t ct = current_time();
sprintf(value, "%lld.%09lld", ct/NANO, ct%NANO);
}
else if (!strcmp(varname, "_clock"))
sprintf(value, "%ld", (long int)time(0));
else if (!strcmp(varname, "_msec"))
{
timens_t age = current_time() - start_time;
sprintf(value, "%lld", age / (NANO/1000));
}
else if (!strcmp(varname, "HOME"))
{
const char *v=getenv("HOME");
if (v)
snprintf(value, BUFFER_SIZE, "%s", v);
else
*value=0;
}
else
return 0;
return 1;
}
void substitute_myvars(const char *arg, char *result, struct session *ses, int recur)
{
char varname[BUFFER_SIZE], value[BUFFER_SIZE];
int nest = 0;
int len=strlen(arg);
while (*arg)
{
if (*arg == '$')
{ /* substitute variable */
bool specvar;
int counter = 0, varlen = 0;
while (*(arg + counter) == '$')
counter++;
/* ${name} code added by Sverre Normann */
if (*(arg+counter) != BRACE_OPEN)
{
/* ordinary variable whose name contains no spaces and special characters */
specvar = false;
while (isalpha(*(arg+varlen+counter))||
(*(arg+varlen+counter)=='_')||
isadigit(*(arg+varlen+counter)))
varlen++;
if (varlen>0)
memcpy(varname, arg+counter, varlen);
*(varname + varlen) = '\0';
}
else
{
/* variable with name containing non-alpha characters e.g ${a b} */
specvar = true;
get_arg_in_braces(arg + counter, varname, 0);
varlen=strlen(varname);
if (recur < 8)
{
substitute_ivars(varname, value);
/* RECURSIVE CALL */
substitute_myvars(value, varname, ses, recur + 1);
}
}
if (specvar) varlen += 2; /* 2*DELIMITERS e.g. {} */
char *v = 0;
if (counter == nest + 1)
{
v = get_hash(ses->myvars, varname);
if (!v && builtin_var(varname, value, ses))
v = value;
}
if (v)
{
int valuelen = strlen(v);
if ((len+=valuelen-counter-varlen) > BUFFER_SIZE-10)
{
if (!aborting)
{
tintin_eprintf(ses, "#ERROR: command+variables too long while substituting $%s%s%s.",
specvar?"{":"", varname, specvar?"}":"");
aborting=true;
}
len-=valuelen-counter-varlen;
v = 0;
}
else
{
strcpy(result, v);
result += valuelen;
arg += counter + varlen;
}
}
if (!v)
{
memcpy(result, arg, counter + varlen);
result += varlen + counter;
arg += varlen + counter;
}
}
else if (*arg == BRACE_OPEN)
{
nest++;
*result++ = *arg++;
}
else if (*arg == BRACE_CLOSE)
{
nest--;
*result++ = *arg++;
}
else if (*arg == '\\' && *(arg + 1) == '$' && nest == 0)
{
arg++;
*result++ = *arg++;
len--;
}
else
*result++ = *arg++;
}
*result = '\0';
}
void substitute_vars(const char *string, char *result, struct session *ses)
{
char arg[BUFFER_SIZE];
substitute_ivars(string, arg);
substitute_myvars(arg, result, ses, 0);
}