remove log and exp from gamecommand again as log is dangerous for now
[divverent/nexuiz.git] / data / qcsrc / common / gamecommand.qc
1 #define MAX_RPN_STACK 16
2 float rpn_db;
3 float rpn_error;
4 float rpn_sp;
5 string rpn_stack[MAX_RPN_STACK];
6 string rpn_pop() {
7         if(rpn_sp > 0) {
8                 --rpn_sp;
9                 return rpn_stack[rpn_sp];
10         } else {
11                 print("rpn: stack underflow\n");
12                 rpn_error = TRUE;
13                 return "";
14         }
15 }
16 void rpn_push(string s) {
17         if(rpn_sp < MAX_RPN_STACK) {
18                 rpn_stack[rpn_sp] = s;
19                 ++rpn_sp;
20         } else {
21                 print("rpn: stack overflow\n");
22                 rpn_error = TRUE;
23         }
24 }
25 string rpn_get() {
26         if(rpn_sp > 0) {
27                 return rpn_stack[rpn_sp - 1];
28         } else {
29                 print("rpn: empty stack\n");
30                 rpn_error = TRUE;
31                 return "";
32         }
33 }
34 void rpn_set(string s) {
35         if(rpn_sp > 0) {
36                 rpn_stack[rpn_sp - 1] = s;
37         } else {
38                 print("rpn: empty stack\n");
39                 rpn_error = TRUE;
40         }
41 }
42 float rpn_getf() { return stof(rpn_get()); }
43 float rpn_popf() { return stof(rpn_pop()); }
44 void rpn_pushf(float f) { return rpn_push(ftos(f)); }
45 void rpn_setf(float f) { return rpn_set(ftos(f)); }
46
47 #define NUM_MARKUPS     41
48 float markup_init;
49 string markup_from[NUM_MARKUPS];
50 string markup_to[NUM_MARKUPS];
51 void GameCommand_MarkupInit()
52 {
53         float i;
54         if (markup_init)
55                 return;
56         markup_init = 1;
57         i = 0;
58         markup_from[i] = "&alien"; markup_to[i] = "\x12"; ++i;
59         markup_from[i] = "&:-)"; markup_to[i] = "\x13"; ++i;
60         markup_from[i] = "&:-("; markup_to[i] = "\x14"; ++i;
61         markup_from[i] = "&x-P"; markup_to[i] = "\x15"; ++i;
62         markup_from[i] = "&:-/"; markup_to[i] = "\x16"; ++i;
63         markup_from[i] = "&:-D"; markup_to[i] = "\x17"; ++i;
64         markup_from[i] = "&<<"; markup_to[i] = "\x18"; ++i;
65         markup_from[i] = "&>>"; markup_to[i] = "\x19"; ++i;
66         markup_from[i] = "&dot"; markup_to[i] = "\x1a"; ++i;
67         markup_from[i] = "&^_"; markup_to[i] = "\x1b"; ++i;
68         markup_from[i] = "&ysplat"; markup_to[i] = "\x1c"; ++i;
69         markup_from[i] = "&-]"; markup_to[i] = "\x1d"; ++i;
70         markup_from[i] = "&--"; markup_to[i] = "\x1e"; ++i;
71         markup_from[i] = "&[-"; markup_to[i] = "\x1f"; ++i;
72         markup_from[i] = "&s<"; markup_to[i] = "\x2c"; ++i;
73         markup_from[i] = "&s>"; markup_to[i] = "\x2e"; ++i;
74         markup_from[i] = "&<-"; markup_to[i] = "\x7f"; ++i;
75         markup_from[i] = "&[="; markup_to[i] = "\x80"; ++i;
76         markup_from[i] = "&=="; markup_to[i] = "\x81"; ++i;
77         markup_from[i] = "&=]"; markup_to[i] = "\x82"; ++i;
78         markup_from[i] = "&r!"; markup_to[i] = "\x84"; ++i;
79         markup_from[i] = "&|o|"; markup_to[i] = "\x85"; ++i;
80         markup_from[i] = "&|u|"; markup_to[i] = "\x86"; ++i;
81         markup_from[i] = "&|i|"; markup_to[i] = "\x87"; ++i;
82         markup_from[i] = "&|c|"; markup_to[i] = "\x88"; ++i;
83         markup_from[i] = "&[c]"; markup_to[i] = "\x89"; ++i;
84         markup_from[i] = "&[n]"; markup_to[i] = "\x8a"; ++i;
85         markup_from[i] = "&[]"; markup_to[i] = "\x8b"; ++i;
86         markup_from[i] = "&r?"; markup_to[i] = "\x8c"; ++i;
87         markup_from[i] = "&|>"; markup_to[i] = "\x8d"; ++i;
88         markup_from[i] = "&splat0"; markup_to[i] = "\x8e"; ++i;
89         markup_from[i] = "&splat1"; markup_to[i] = "\x8f"; ++i;
90         markup_from[i] = "&[["; markup_to[i] = "\x90"; ++i;
91         markup_from[i] = "&]]"; markup_to[i] = "\x91"; ++i;
92         markup_from[i] = "&splat2"; markup_to[i] = "\x9a"; ++i;
93         markup_from[i] = "&)("; markup_to[i] = "\x9b"; ++i;
94         markup_from[i] = "&splat3"; markup_to[i] = "\x9c"; ++i;
95         markup_from[i] = "&(."; markup_to[i] = "\x9d"; ++i;
96         markup_from[i] = "&.."; markup_to[i] = "\x9e"; ++i;
97         markup_from[i] = "&.)"; markup_to[i] = "\x9f"; ++i;
98         markup_from[i] = "&<|"; markup_to[i] = "\xff"; ++i;
99 }
100
101 string GameCommand_Markup(string s2)
102 {
103         float red, ccase, i, j;
104         string s, s3;
105
106         GameCommand_MarkupInit();
107
108         s = "";
109
110         red = 0;
111         ccase = 0;
112         for(i = 0; i < strlen(s2); ++i)
113         {
114                 for(j = 0; j < NUM_MARKUPS; ++j)
115                 {
116                         s3 = substring(s2, i, strlen(markup_from[j]));
117                         if (s3 == markup_from[j])
118                         {
119                                 s = strcat(s, markup_to[j]);
120                                 i += strlen(markup_from[j]) - 1;
121                                 break;
122                         }
123                 }
124
125                 if(j == NUM_MARKUPS)
126                 {
127                         if(substring(s2, i, 2) == "&&")
128                         {
129                                 s = strcat(s, strconv(ccase, red, red, "&"));
130                                 ++i;
131                         }
132                         else if(substring(s2, i, 2) == "&d")
133                         {
134                                 red = 2;
135                                 ccase = 0;
136                                 ++i;
137                         }
138                         else if(substring(s2, i, 2) == "&a")
139                         {
140                                 red = 2;
141                                 ccase = 2;
142                                 ++i;
143                         }
144                         else if(substring(s2, i, 2) == "&n")
145                         {
146                                 red = 0;
147                                 ccase = 0;
148                                 ++i;
149                         }
150                         else
151                                 s = strcat(s, strconv(ccase, red, red, substring(s2, i, 1)));
152                 }
153         }
154
155         return s;
156 }
157
158 float GameCommand_Generic(string command)
159 {
160         float argc;
161         float i, j, f, n;
162         vector rgb;
163         string s, s2, c;
164         argc = tokenize_console(command);
165         if(argv(0) == "help")
166         {
167                 print("  rpn EXPRESSION... - a RPN calculator.\n");
168                 print("    Operator description (x: string, s: set, f: float):\n");
169                 print("    x pop ----------------------------->     : removes the top\n");
170                 print("    x dup -----------------------------> x x : duplicates the top\n");
171                 print("    x x exch --------------------------> x x : swap the top two\n");
172                 print("    /cvarname load --------------------> x   : loads a cvar\n");
173                 print("    /cvarname x def ------------------->     : writes to a cvar\n");
174                 print("    f f add|sub|mul|div|mod|max|min ---> f   : adds/... two numbers\n");
175                 print("    f f eq|ne|gt|ge|lt|le -------------> f   : compares two numbers\n");
176                 print("    f neg|abs|sgn|rand|floor|ceil------> f   : negates/... a number\n");
177                 print("    f f f bound -----------------------> f   : bounds the middle number\n");
178                 print("    f1 f2 b when ----------------------> f   : f1 if b, f2 otherwise\n");
179                 print("    s s union|intersection|difference -> s   : set operations\n");
180                 print("    s shuffle -------------------------> s   : randomly arrange elements\n");
181                 print("    x dbpush -------------------------->     : pushes the top onto the database\n");
182                 print("    dbpop|dbget -----------------------> x   : removes/reads DB's top\n");
183                 print("    dblen|dbat ------------------------> f   : gets the DB's size/cursor pos\n");
184                 print("    dbclr ----------------------------->     : clear the DB\n");
185                 print("    s dbsave|dbload-------------------->     : save/load the DB to/from a file\n");
186                 print("    x dbins --------------------------->     : moves the top into the DB\n");
187                 print("    dbext|dbread ----------------------> x   : extract/get from the DB's cursor\n");
188                 print("    f dbmov|dbgoto -------------------->     : move or set the DB's cursor\n");
189                 print("    s localtime -----------------------> s   : formats the current local time\n");
190                 print("    s gmtime --------------------------> s   : formats the current UTC time\n");
191                 print("    time ------------------------------> f   : seconds since VM start\n");
192                 print("    Set operations operate on 'such''strings'.\n");
193                 print("    Unknown tokens insert their cvar value.\n");
194                 print("  maplist add map\n");
195                 print("  maplist remove map\n");
196                 print("  maplist shuffle\n");
197                 print("  maplist cleanup\n");
198                 print("  maplist maplist\n");
199                 print("  maplist lsmaps\n");
200                 print("  addtolist variable addedvalue\n");
201                 print("  records\n");
202                 return TRUE;
203         }
204         
205         if(argv(0) == "maplist")
206         {
207                 if(argv(1) == "add" && argc == 3)
208                 {
209                         f = fopen(strcat("maps/", argv(2), ".bsp"), FILE_READ);
210                         if(f != -1)
211                                 fclose(f);
212                         else {
213                                 print("maplist: ERROR: ", argv(2), " does not exist!\n");
214                                 return TRUE;
215                         }
216                         if(cvar_string("g_maplist") == "")
217                                 cvar_set("g_maplist", argv(2));
218                         else
219                                 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
220                         return TRUE;
221                 }
222                 else if(argv(1) == "remove" && argc == 3)
223                 {
224                         s = argv(2);
225                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
226                         s2 = "";
227                         for(i = 0; i < n; ++i)
228                                 if(argv(i) != s)
229                                         s2 = strcat(s2, " ", argv(i));
230                         s2 = substring(s2, 1, strlen(s2) - 1);
231                         cvar_set("g_maplist", s2);
232                         return TRUE;
233                 }
234                 else if(argv(1) == "shuffle" && argc == 2)
235                 {
236                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
237                         return TRUE;
238                 }
239                 else if(argv(1) == "cleanup")
240                 {
241                         MapInfo_Enumerate();
242                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
243                         n = tokenizebyseparator(cvar_string("g_maplist"), " ");
244                         s2 = "";
245                         for(i = 0; i < n; ++i)
246                                 if(MapInfo_CheckMap(argv(i)))
247                                         s2 = strcat(s2, " ", argv(i));
248                         s2 = substring(s2, 1, strlen(s2) - 1);
249                         cvar_set("g_maplist", s2);
250                         return TRUE;
251                 }
252                 else if(argv(1) == "maplist") {
253                         print(maplist_reply);
254                         return TRUE;
255                 }
256                 else if(argv(1) == "lsmaps") {
257                         print(lsmaps_reply);
258                         return TRUE;
259                 }
260         }
261         else if(argc >= 3 && argv(0) == "red")
262         {
263                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
264                 localcmd(strcat(argv(1), " ", GameCommand_Markup(s)));
265                 return TRUE;
266         }
267         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
268         {
269                 // other test case
270                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
271
272                 n = floor(random() * 6 + 2);
273
274                 s2 = "";
275                 for(i = 0; i < n; ++i)
276                 {
277                         s2 = strcat(s2, "AH");
278                 }
279
280                 if(random() < 0.1)
281                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
282
283                 if(s == "")
284                         s = s2;
285                 else
286                         if(random() < 0.8)
287                                 s = strcat(s, " ", s2);
288                         else
289                                 s = strcat(s2, " ", s);
290
291                 s2 = substring(s, strlen(s) - 2, 2);
292                 if(s2 == "AH" || s2 == "AY")
293                         s = strcat(s, "))");
294                 else
295                         s = strcat(s, " ))");
296
297                 if(random() < 0.1)
298                         s = substring(s, 0, strlen(s) - 1);
299
300                 if(random() < 0.1)
301                         s = strconv(1, 0, 0, s);
302
303                 localcmd(strcat(argv(1), " ", s));
304
305                 return TRUE;
306         }
307         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55970)
308         {
309                 // test case for terrencehill's color codes
310                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
311                 s2 = "";
312                 
313                 n = strlen(s);
314                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
315                 f = random() * 6;
316
317                 for(i = 0; i < n; ++i)
318                 {
319                         c = substring(s, i, 1);
320
321                         if(c == ";")
322                                 c = ":";
323                         else if(c == "^")
324                         {
325                                 c = "^^";
326                                 if(substring(s, i+1, 1) == "^")
327                                         ++i;
328                         }
329
330                         if(c != " ")
331                         {
332                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
333                                 c = strcat(rgb_to_hexcolor(rgb), c);
334                         }
335                         s2 = strcat(s2, c);
336                 }
337
338                 localcmd(strcat(argv(1), " ", s2));
339
340                 return TRUE;
341         }
342         else if(argv(0) == "rpn")
343         {
344                 if(!rpn_db)
345                 {
346                         rpn_db = db_create();
347                         db_put(rpn_db, "stack.pointer", "0");
348                         db_put(rpn_db, "stack.pos", "-1");
349                 }
350                 if(argc >= 2)
351                 {
352                         float rpnpos;
353                         string rpncmd;
354                         float f2, f3;
355                         rpn_sp = 0;
356                         rpn_error = FALSE;
357                         for(rpnpos = 1; rpnpos < argc; ++rpnpos)
358                         {
359                                 rpncmd = argv(rpnpos);
360                                 f = strlen(rpncmd);
361                                 if(rpncmd == "") {
362                                 } else if(stof(substring(rpncmd, 0, 1)) > 0) {
363                                         rpn_push(rpncmd);
364                                 } else if(substring(rpncmd, 0, 1) == "0") {
365                                         rpn_push(rpncmd);
366                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "+") {
367                                         rpn_push(rpncmd);
368                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "-") {
369                                         rpn_push(rpncmd);
370                                 } else if(f >= 2 && substring(rpncmd, 0, 1) == "/") {
371                                         rpn_push(substring(rpncmd, 1, strlen(rpncmd) - 1));
372                                 } else if(rpncmd == "clear") {
373                                         rpn_sp = 0;
374                                 } else if(rpncmd == "def" || rpncmd == "=") {
375                                         s = rpn_pop();
376                                         s2 = rpn_pop();
377
378                                         if(s2 != "")
379                                         {
380 #ifdef MENUQC
381                                                 registercvar(s2, "", 0);
382 #else
383                                                 registercvar(s2, "");
384 #endif
385                                                 if(!rpn_error) // don't change cvars if a stack error had happened!
386                                                         cvar_set(s2, s);
387                                         }
388                                         else
389                                         {
390                                                 print("rpn: empty cvar name for 'def'\n");
391                                                 rpn_error = TRUE;
392                                         }
393                                 } else if(rpncmd == "defs" || rpncmd == "@") {
394                                         s = "";
395                                         i = rpn_popf();
396                                         j = (i == 0);
397                                         while(rpn_sp > 1 && (j || i > 0))
398                                         {
399                                                 s = strcat("/", rpn_pop(), " ", s);
400                                                 --i;
401                                         }
402                                         s2 = rpn_pop();
403                                         if(s2 != "")
404                                         {
405 #ifdef MENUQC
406                                                 registercvar(s2, "", 0);
407 #else
408                                                 registercvar(s2, "");
409 #endif
410                                                 if(!rpn_error) // don't change cvars if a stack error had happened!
411                                                         cvar_set(s2, s);
412                                         }
413                                         else
414                                         {
415                                                 print("rpn: empty cvar name for 'defs'\n");
416                                                 rpn_error = TRUE;
417                                         }
418                                 } else if(rpncmd == "load") {
419                                         rpn_set(cvar_string(rpn_get()));
420                                 } else if(rpncmd == "exch") {
421                                         s = rpn_pop();
422                                         s2 = rpn_get();
423                                         rpn_set(s);
424                                         rpn_push(s2);
425                                 } else if(rpncmd == "dup") {
426                                         rpn_push(rpn_get());
427                                 } else if(rpncmd == "pop") {
428                                         rpn_pop();
429                                 } else if(rpncmd == "add" || rpncmd == "+") {
430                                         f = rpn_popf();
431                                         rpn_setf(rpn_getf() + f);
432                                 } else if(rpncmd == "sub" || rpncmd == "-") {
433                                         f = rpn_popf();
434                                         rpn_setf(rpn_getf() - f);
435                                 } else if(rpncmd == "mul" || rpncmd == "*") {
436                                         f = rpn_popf();
437                                         rpn_setf(rpn_getf() * f);
438                                 } else if(rpncmd == "div" || rpncmd == "/") {
439                                         f = rpn_popf();
440                                         rpn_setf(rpn_getf() / f);
441                                 } else if(rpncmd == "mod" || rpncmd == "%") {
442                                         f = rpn_popf();
443                                         f2 = rpn_getf();
444                                         rpn_setf(f2 - f * floor(f2 / f));
445                                 } else if(rpncmd == "abs") {
446                                         rpn_setf(fabs(rpn_getf()));
447                                 } else if(rpncmd == "sgn") {
448                                         f = rpn_getf();
449                                         if(f < 0)
450                                                 rpn_set("-1");
451                                         else if(f > 0)
452                                                 rpn_set("1");
453                                         else
454                                                 rpn_set("0");
455                                 } else if(rpncmd == "neg" || rpncmd == "~") {
456                                         rpn_setf(-rpn_getf());
457                                 } else if(rpncmd == "floor" || rpncmd == "f") {
458                                         rpn_setf(floor(rpn_getf()));
459                                 } else if(rpncmd == "ceil" || rpncmd == "c") {
460                                         rpn_setf(ceil(rpn_getf()));
461                                 } else if(rpncmd == "max") {
462                                         f = rpn_popf();
463                                         f2 = rpn_getf();
464                                         rpn_setf(max(f2, f));
465                                 } else if(rpncmd == "min") {
466                                         f = rpn_popf();
467                                         f2 = rpn_getf();
468                                         rpn_setf(min(f2, f));
469                                 } else if(rpncmd == "bound") {
470                                         f = rpn_popf();
471                                         f2 = rpn_popf();
472                                         f3 = rpn_getf();
473                                         rpn_setf(bound(f3, f2, f));
474                                 } else if(rpncmd == "when") {
475                                         f = rpn_popf();
476                                         f2 = rpn_popf();
477                                         f3 = rpn_getf();
478                                         if(f)
479                                                 rpn_setf(f3);
480                                         else
481                                                 rpn_setf(f2);
482                                 } else if(rpncmd == ">" || rpncmd == "gt") {
483                                         f = rpn_popf();
484                                         rpn_setf(rpn_getf() > f);
485                                 } else if(rpncmd == "<" || rpncmd == "lt") {
486                                         f = rpn_popf();
487                                         rpn_setf(rpn_getf() < f);
488                                 } else if(rpncmd == "==" || rpncmd == "eq") {
489                                         f = rpn_popf();
490                                         rpn_setf(rpn_getf() == f);
491                                 } else if(rpncmd == ">=" || rpncmd == "ge") {
492                                         f = rpn_popf();
493                                         rpn_setf(rpn_getf() >= f);
494                                 } else if(rpncmd == "<=" || rpncmd == "le") {
495                                         f = rpn_popf();
496                                         rpn_setf(rpn_getf() <= f);
497                                 } else if(rpncmd == "!=" || rpncmd == "ne") {
498                                         f = rpn_popf();
499                                         rpn_setf(rpn_getf() != f);
500                                 } else if(rpncmd == "rand") {
501                                         rpn_setf(ceil(random() * rpn_getf()) - 1);
502                                 } else if(rpncmd == "crc16") {
503                                         rpn_setf(crc16(FALSE, rpn_get()));
504                                 } else if(rpncmd == "dbpush") {
505                                         s = rpn_pop();
506                                         if(!rpn_error)
507                                         {
508                                                 i = stof(db_get(rpn_db, "stack.pointer"));
509                                                 db_put(rpn_db, "stack.pointer", ftos(i+1));
510                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
511                                         }
512                                         if(!i)
513                                                 db_put(rpn_db, "stack.pos", "0");
514                                 } else if(rpncmd == "dbpop") {
515                                         i = stof(db_get(rpn_db, "stack.pointer"));
516                                         if(i)
517                                         {
518                                                 s = ftos(i-1);
519                                                 db_put(rpn_db, "stack.pointer", s);
520                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
521                                                 j = stof(db_get(rpn_db, "stack.pos"));
522                                                 if(j >= i)
523                                                         db_put(rpn_db, "stack.pos", ftos(i-2));
524                                         } else {
525                                                 rpn_error = 1;
526                                                 print("rpn: database underflow\n");
527                                         }
528                                 } else if(rpncmd == "dbget") {
529                                         
530                                         i = stof(db_get(rpn_db, "stack.pointer"));
531                                         if(i)
532                                         {
533                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i-1))));
534                                         } else {
535                                                 rpn_error = 1;
536                                                 print("rpn: database empty\n");
537                                         }
538                                 } else if(rpncmd == "dblen") {
539                                         rpn_push(db_get(rpn_db, "stack.pointer"));
540                                 } else if(rpncmd == "dbclr") {
541                                         db_close(rpn_db);
542                                         rpn_db = db_create();
543                                         db_put(rpn_db, "stack.pointer", "0");
544                                         db_put(rpn_db, "stack.pos", "-1");
545                                 } else if(rpncmd == "dbsave") {
546                                         s = rpn_pop();
547                                         if(!rpn_error)
548                                                 db_save(rpn_db, s);
549                                 } else if(rpncmd == "dbload") {
550                                         s = rpn_pop();
551                                         if(!rpn_error)
552                                         {
553                                                 db_close(rpn_db);
554                                                 rpn_db = db_load(s);
555                                         }
556                                 } else if(rpncmd == "dbins") {
557                                         s = rpn_pop();
558                                         if(!rpn_error)
559                                                 //if(rpn_sp > 0)
560                                         {
561                                                 j = stof(db_get(rpn_db, "stack.pointer"));
562                                                 i = stof(db_get(rpn_db, "stack.pos"));
563                                                 
564                                                 if(i < 0)
565                                                 {
566                                                         i = 0;
567                                                         db_put(rpn_db, "stack.pos", "0");
568                                                 }
569                                                 
570                                                 db_put(rpn_db, "stack.pointer", ftos(j+1));
571                                                 for(--j; j >= i; --j)
572                                                 {
573                                                         db_put(rpn_db, strcat("stack.", ftos(j+1)),
574                                                                db_get(rpn_db, (strcat("stack.", ftos(j))))
575                                                                 );
576                                                 }
577                                                 db_put(rpn_db, strcat("stack.", ftos(i)), s);
578                                         }
579                                 } else if(rpncmd == "dbext") {
580                                         j = stof(db_get(rpn_db, "stack.pointer"));
581                                         i = stof(db_get(rpn_db, "stack.pos"));
582                                         if(!j)
583                                         {
584                                                 rpn_error = TRUE;
585                                                 print("rpn: empty database\n");
586                                         } else {
587                                                 --j;
588                                                 rpn_push(db_get(rpn_db, strcat("stack.", ftos(i))));
589                                                 db_put(rpn_db, "stack.pointer", ftos(j));
590                                                 if(i == j)
591                                                 {
592                                                         db_put(rpn_db, "stack.pos", ftos(j-1));
593                                                 } else {
594                                                         while(i < j)
595                                                         {
596                                                                 db_put(rpn_db, strcat("stack.", ftos(i)),
597                                                                        db_get(rpn_db, (strcat("stack.", ftos(i+1))))
598                                                                         );
599                                                                 ++i;
600                                                         }
601                                                 }
602                                         }
603                                 } else if(rpncmd == "dbread") {
604                                         s = db_get(rpn_db, "stack.pos");
605                                         if(stof(s) >= 0)
606                                         {
607                                                 rpn_push(db_get(rpn_db, strcat("stack.", s)));
608                                         } else {
609                                                 rpn_error = 1;
610                                                 print("rpn: empty database\n");
611                                         }
612                                 } else if(rpncmd == "dbat") {
613                                         rpn_push(db_get(rpn_db, "stack.pos"));
614                                 } else if(rpncmd == "dbmov") {
615                                         j = stof(db_get(rpn_db, "stack.pointer"));
616                                         i = stof(db_get(rpn_db, "stack.pos"));
617                                         i += rpn_popf();
618                                         if(!rpn_error)
619                                         {
620                                                 if(i < 0 || i >= j)
621                                                 {
622                                                         print("rpn: database cursor out of bounds\n");
623                                                         rpn_error = TRUE;
624                                                 }
625                                                 if(!rpn_error)
626                                                 {
627                                                         db_put(rpn_db, "stack.pos", ftos(i));
628                                                 }
629                                         }
630                                 } else if(rpncmd == "dbgoto") {
631                                         s = rpn_pop();
632                                         j = stof(db_get(rpn_db, "stack.pointer"));
633                                         if(!j)
634                                         {
635                                                 rpn_error = TRUE;
636                                                 print("rpn: empty database, cannot move cursor\n");
637                                         }
638                                         if(!rpn_error)
639                                         {
640                                                 if(s == "end")
641                                                         i = stof(db_get(rpn_db, "stack.pointer"))-1;
642                                                 else if(s == "beg")
643                                                         i = 0;
644                                                 else
645                                                         i = stof(s);
646                                                 
647                                                 j = stof(db_get(rpn_db, "stack.pointer"));
648                                                 if(i < 0 || i >= j)
649                                                 {
650                                                         print("rpn: database cursor destination out of bounds\n");
651                                                         rpn_error = TRUE;
652                                                 }
653                                                 if(!rpn_error)
654                                                 {
655                                                         db_put(rpn_db, "stack.pos", ftos(i));
656                                                 }
657                                         }
658                                 } else if(rpncmd == "union") {
659                                         // s s2 union
660                                         s2 = rpn_pop();
661                                         s = rpn_get();
662                                         f = tokenize_console(s);
663                                         f2 = tokenize_console(strcat(s, " ", s2));
664                                         // tokens 0..(f-1) represent s
665                                         // tokens f..f2 represent s2
666                                         // UNION: add all tokens to s that are in s2 but not in s
667                                         s = "";
668                                         for(i = 0; i < f; ++i)  
669                                                 s = strcat(s, " ", argv(i));
670                                         for(i = f; i < f2; ++i) {
671                                                 for(j = 0; j < f; ++j)
672                                                         if(argv(i) == argv(j))
673                                                                 goto skip_union;
674                                                 s = strcat(s, " ", argv(i));
675 :skip_union
676                                         }
677                                         if(substring(s, 0, 1) == " ")
678                                                 s = substring(s, 1, 99999);
679                                         rpn_set(s);
680                                         tokenize_console(command);
681                                 } else if(rpncmd == "intersection") {
682                                         // s s2 intersection
683                                         s2 = rpn_pop();
684                                         s = rpn_get();
685                                         f = tokenize_console(s);
686                                         f2 = tokenize_console(strcat(s, " ", s2));
687                                         // tokens 0..(f-1) represent s
688                                         // tokens f..f2 represent s2
689                                         // INTERSECTION: keep only the tokens from s that are also in s2
690                                         s = "";
691                                         for(i = 0; i < f; ++i) {
692                                                 for(j = f; j < f2; ++j)
693                                                         if(argv(i) == argv(j))
694                                                         {
695                                                                 s = strcat(s, " ", argv(i));
696                                                                 break;
697                                                         }
698                                         }
699                                         if(substring(s, 0, 1) == " ")
700                                                 s = substring(s, 1, 99999);
701                                         rpn_set(s);
702                                         tokenize_console(command);
703                                 } else if(rpncmd == "difference") {
704                                         // s s2 difference
705                                         s2 = rpn_pop();
706                                         s = rpn_get();
707                                         f = tokenize_console(s);
708                                         f2 = tokenize_console(strcat(s, " ", s2));
709                                         // tokens 0..(f-1) represent s
710                                         // tokens f..f2 represent s2
711                                         // DIFFERENCE: keep only the tokens from s that are not in s2
712                                         s = "";
713                                         for(i = 0; i < f; ++i) {
714                                                 for(j = f; j < f2; ++j)
715                                                         if(argv(i) == argv(j))
716                                                                 goto skip_difference;
717                                                 s = strcat(s, " ", argv(i));
718 :skip_difference
719                                         }
720                                         if(substring(s, 0, 1) == " ")
721                                                 s = substring(s, 1, 99999);
722                                         rpn_set(s);
723                                         tokenize_console(command);
724                                 } else if(rpncmd == "shuffle") {
725                                         // s shuffle
726                                         s = rpn_get();
727                                         f = tokenize_console(s);
728
729                                         for(i = 0; i < f - 1; ++i) {
730                                                 // move a random item from i..f-1 to position i
731                                                 s = "";
732                                                 f2 = floor(random() * (f - i) + i);
733                                                 for(j = 0; j < i; ++j)
734                                                         s = strcat(s, " ", argv(j));
735                                                 s = strcat(s, " ", argv(f2));
736                                                 for(j = i; j < f; ++j)
737                                                         if(j != f2)
738                                                                 s = strcat(s, " ", argv(j));
739                                                 f = tokenize_console(s);
740                                         }
741
742                                         if(substring(s, 0, 1) == " ")
743                                                 s = substring(s, 1, 99999);
744                                         rpn_set(s);
745                                         tokenize_console(command);
746                                 } else if(rpncmd == "fexists_assert") {
747                                         s = rpn_pop();
748                                         if(!rpn_error)
749                                         {
750                                                 f = fopen(s, FILE_READ);
751                                                 if(f != -1)
752                                                         fclose(f);
753                                                 else {
754                                                         print("rpn: ERROR: ", s, " does not exist!\n");
755                                                         rpn_error = TRUE;
756                                                 }
757                                         }
758                                 } else if(rpncmd == "fexists") {
759                                         s = rpn_get();
760                                         if(!rpn_error)
761                                         {
762                                                 f = fopen(s, FILE_READ);
763                                                 if(f != -1) {
764                                                         fclose(f);
765                                                         rpn_setf(1);
766                                                 } else {
767                                                         rpn_setf(0);
768                                                 }
769                                         }
770                                 } else if(rpncmd == "localtime") {
771                                         rpn_set(strftime(TRUE, rpn_get()));
772                                 } else if(rpncmd == "gmtime") {
773                                         rpn_set(strftime(FALSE, rpn_get()));
774                                 } else if(rpncmd == "time") {
775                                         rpn_pushf(time);
776                                 } else {
777                                         rpn_push(cvar_string(rpncmd));
778                                 }
779                                 if(rpn_error)
780                                         break;
781                         }
782                         while(rpn_sp > 0)
783                         {
784                                 s = rpn_pop();
785                                 print("rpn: still on stack: ", s, "\n");
786                         }
787                         return TRUE;
788                 }
789         } else if(argv(0) == "addtolist") {
790                 if(argc >= 2)
791                 {
792                         s = argv(1);
793                         s2 = argv(2);
794                         if(cvar_string(s) == "")
795                                 cvar_set(s, s2);
796                         else {
797                                 n = tokenizebyseparator(cvar_string(s), " ");
798                                 for(i = 0; i < n; ++i)
799                                         if(argv(i) == s2)
800                                                 return TRUE; // already in list
801                                 cvar_set(s, strcat(s2, " ", cvar_string(s)));
802                         }
803                 }
804                 return TRUE;
805         }
806         else if(argv(0) == "records") {
807                 print(records_reply);
808                 return TRUE;
809 #ifdef MENUQC
810         } else if(argv(0) == "cp") {
811                 if(argc >= 2)
812                 {
813                         s = argv(1);
814                         for(i = 2; i < argc; ++i)
815                                 s = strcat(s, " ", argv(i));
816                         centerprint(unescape(s));
817                 }
818                 return TRUE;
819 #endif
820         }
821
822         return FALSE;
823 }