-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpowmain.c
1530 lines (1298 loc) · 39.3 KB
/
powmain.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* powwow -- mud client with telnet protocol
*
* Copyright (C) 1998 by Massimiliano Ghilardi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
* History:
*
* Initially inspired to the Tintin client by Peter Unold,
* Powwow contains no Tintin code.
* The original program Cancan, written by Mattias Engdegård (Yorick)
* (f91-men@nada.kth.se) 1992-94,
* was greatly improved upon by Vivriel, Thuzzle and Ilie and then
* transformed from Cancan into Powwow by Cosmos who worked
* to make it yet more powerful.
* AmigaDOS porting attempt by Fror.
* Many new features added by Dain.
* As usual, all the developers are in debt to countless users
* for suggestions and debugging.
*/
/*
* Set this to whatever you like
*
* #define POWWOW_DIR "/home/gustav/powwow"
*/
#define POWWOW_VERSION "1.2.5, Copyright 2000-2001 by Cosmos\n(contributions by Yorick, Vivriel, Thuzzle, Ilie, Fror, Dain)"
#define HELPNAME "powwow.help"
#define COPYNAME "COPYING"
#ifndef POWWOW_DIR
# define POWWOW_DIR ".\\"
#endif
#include <stdio.h>
#include <io.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
/* are these really needed? */
extern int errno;
//extern int select();
# include "malloc.h"
# include "regex.h"
#include "powdefines.h"
#include "powmain.h"
#include "powutils.h"
#include "powtty.h"
#include "powbeam.h"
#include "powcmd.h"
#include "powcmd2.h"
#include "powedit.h"
#include "powmap.h"
#include "powlist.h"
#include "poweval.h"
#include "powlog.h"
#include "powhook.h"
#include "putty.h"
/* local function declarations */
#ifdef MOTDFILE
static void printmotd (void);
#endif
static void mainloop (void);
static void exec_delays (void);
static void prompt_reset_iac (void);
static int search_action_or_prompt (char *line, char clearline, char copyprompt);
#define search_action(line, clearline) search_action_or_prompt((line), (clearline), 0)
#define search_prompt(line, copyprompt) search_action_or_prompt((line), 0, (copyprompt)+1)
static void set_params (char *line, int *match_s, int *match_e);
static void parse_commands (char *command, char *arg);
static int subst_param (ptr *buf, char *src);
static int jit_subst_vars (ptr *buf, char *src);
/* GLOBALS */
static char *copyname = COPYNAME;
static char typed[CAPLEN];
char inserted_next[BUFSIZE];
char identified = 0; /* 1 after #identify */
volatile char confirm = 0; /* 1 if just tried to quit */
int history_done = 0; /* number of recursive #history commands */
int prompt_status = 0; /* prompt status: 0 = ready -> nothing to do;
* 1 if internal echo -> must redraw;
* -1 if something sent to MUD -> waiting for it.
*/
int line_status = 0; /* input line status: 0 = ready -> nothing to do;
* 1 if printed something -> must redraw.
*/
int limit_mem = 0; /* if !=0, max len of a string or text */
char echo_ext = 1; /* 1 if text sent to MUD must be echoed */
char echo_key = 1; /* 1 if binds must be echoed */
char echo_int = 1; /* 0 if internal messages are suppressed */
char opt_exit = 0; /* 1 to autoquit when closing last conn. */
char opt_history; /* 1 if to save also history */
char opt_words = 0; /* 1 if to save also word completion list */
char opt_compact = 0; /* 1 if to clear prompt between remote messages */
char opt_debug = 0; /* 1 if to echo every line before executing it */
char opt_speedwalk = 0; /* 1 = speedwalk on */
char opt_wrap = 1; /* 1 = word wrap active */
char opt_autoprint = 0; /* 1 = automatically #print lines matched by actions */
char opt_reprint = 0; /* 1 = reprint sent commands when we get a prompt */
char opt_sendsize = 0; /* 1 = send term size upon connect */
char opt_autoclear = 1; /* 1 = clear input line before executing commands
* from spawned programs.
* if 0, spawned progs must #clear before printing
*/
char hostname[BUFSIZE];
int portnumber;
static char powwow_dir[BUFSIZE]; /* default path to definition files */
char deffile[BUFSIZE]; /* name and path of definition file */
char helpfile[BUFSIZE]; /* name and path of help file */
char powcopyfile[BUFSIZE]; /* name and path of copyright file */
aliasnode *aliases[MAX_HASH]; /* head of alias hash list */
aliasnode *sortedaliases; /* head of (ASCII) sorted alias list */
actionnode *actions; /* head of action list */
promptnode *prompts; /* head of prompt list */
marknode *markers; /* head of mark list */
int a_nice = 0; /* default priority of new actions/marks */
keynode *keydefs; /* head of key binding list */
delaynode *delays; /* head of delayed commands list */
delaynode *dead_delays; /* head of dead-delayed commands list */
varnode *named_vars[2][MAX_HASH]; /* head of named variables hash list */
varnode *sortednamed_vars[2]; /* head of (ASCII) sorted named variables list */
int max_named_vars = 100; /* max number of named vars (grows as needed) */
int num_named_vars[2]; /* number of named variables actually used */
static param_stack paramstk; /* stack of local unnamed vars */
static unnamedvar global_var[NUMTOT]; /* global unnamed vars */
vars *var; /* vector of all vars */
ptr globptr[2]; /* global ptr buffer */
char globptrok = 1|2; /* x&i = 0 if globptr[i] is in use */
varnode *prompt; /* $prompt is always set */
ptr marked_prompt; /* $prompt with marks added */
static varnode *last_line; /* $line is always set to
* the last line processed */
time_t now; /* current time */
time_t start_time; /* time of powwow timer startup */
time_t ref_time; /* time corresponding to timer == 0 */
function_str last_edit_cmd; /* GH: keep track of for repeated cmds */
time_t start_clock, cpu_clock;
char initstr[BUFSIZE]; /* initial string to send on connect */
int linemode = 0; /* line mode flags (LM_* in main.h) */
/* for line editing */
int cols=10, lines=24; /* screen size */
int cols_1=79; /* == cols if tty_wrapglitch, == cols-1 otherwise */
int olines; /* previous screen size */
int col0; /* input line offset (= printstrlen of prompt) */
int line0; /* screen line where the input line starts */
char edbuf[BUFSIZE]; /* line editing buffer */
int edlen; /* length of current input line */
int pos = 0; /* cursor position in line */
char surely_isprompt = 0; /* !=0 if last #prompt set #isprompt */
char verbatim = 0; /* 1 = don't expand aliases or process semicolons */
char prefixstr[BUFSIZE]; /* inserted in the editing buffer each time */
char edattrbeg[CAPLEN]; /* starting input line attributes */
char edattrend[CAPLEN]; /* ending input line attributes */
int edattrbg; /* input line attributes do change bg color */
char CMDSEP='|';
/* signals handling */
volatile int sig_pending, sig_winch_got, sig_chld_got;
/* GH: different ID characters for different action types */
/*
* Cosmos: they are hardcoded in cmd.c, funtion parse_action()
* so don't change them.
*/
char action_chars[ACTION_TYPES] = { '>', '%' };
/* GH: different delimeter modes */
char *delim_list[] = { " |", " <>!=(),|\"'{}[]+-/*%", 0 };
int delim_len [] = { 2 , 21 , 0 };
char *delim_name[] = { "normal", "program", "custom" };
int delim_mode = DELIM_NORMAL;
int input_chars=0;
void powmain ()
{
char *p;
int i;
/* initializations */
initstr[0] = 0;
if ((p = cfg.powwowdir)) {
strcpy(powwow_dir, p);
if (powwow_dir[strlen(powwow_dir) - 1] != '\\')
strcat(powwow_dir, "\\");
} else
powwow_dir[0] = '\0';
if (powwow_dir[0])
strcpy(powcopyfile, powwow_dir);
else
strcpy(powcopyfile, POWWOW_DIR);
if (helpfile[strlen(helpfile) - 1] != '\\')
strcat(powcopyfile, "\\");
strcat(powcopyfile, copyname);
if (access(powcopyfile, 04) == -1 && !access(copyname, 04))
strcpy(powcopyfile, copyname);
/* initialize variables */
if ((var = (vars *)malloc(sizeof(vars)*(NUMTOT+max_named_vars)))) {
for (i=0; i<NUMTOT; i++) {
var[i].num = &global_var[i].num;
var[i].str = &global_var[i].str;
}
}
/* stack is empty */
paramstk.curr = 0;
/* allocate permanent variables */
if ((prompt = add_varnode("prompt", 1))
&& (prompt->str = ptrnew(PARAMLEN))
&& (marked_prompt = ptrnew(PARAMLEN))
&& (last_line = add_varnode("last_line", 1))
&& (last_line->str = ptrnew(PARAMLEN))
&& (globptr[0] = ptrnew(PARAMLEN))
&& (globptr[1] = ptrnew(PARAMLEN)));
/* BILLJ:
* set the file from the global config struct
* the subroutine will magically figure out if it's in the 'working dir' or not
*/
tty_add_initial_binds();
tty_add_walk_binds();
if (cfg.powwowcfg) {
if (strlen(cfg.powwowcfg)>0) {
set_deffile(cfg.powwowcfg);
if (read_settings()<0) {
sprintf(errtext,"#could not read definitions file.\r\n");
from_backend(0,errtext,strlen(errtext));
} else if (*initstr){
parse_instruction(initstr, 0, 0, 1);
}
}
}
// fix the timer, etc
update_now();
ref_time = start_time = movie_last = now;
// tty_start();
parse_user_input("#identify",0);
// so word completions will work
edit_bootstrap();
return;
}
#define IAC_N 1024
static char *iac_v[IAC_N];
static int iac_f, iac_l;
static void prompt_reset_iac (void)
{
iac_f = iac_l = 0;
}
void prompt_set_iac (char *p)
{
if (iac_f == iac_l)
iac_f = iac_l = 0;
if (iac_l < IAC_N)
iac_v[iac_l++] = p;
}
static char *prompt_get_iac (void)
{
return iac_l > iac_f ? iac_v[iac_f] : NULL;
}
static int grab_prompt (char *linestart, int len, int islast)
{
char *p;
int is_iac_prompt = surely_isprompt = 0;
/* recognize IAC GA as end-of-prompt marker */
if ((p = prompt_get_iac()) && p > linestart && p <= linestart+len)
iac_f++, is_iac_prompt = len = p - linestart;
else if (!islast)
return 0;
/*
* We may get a prompt in the middle of a bunch of lines, so
* match #prompts. They usually have no #print, so we print manually
* if islast is not set and a #prompt matches.
*/
if ((is_iac_prompt || islast || printstrlen(linestart) < cols) &&
((search_prompt(linestart, 1) && surely_isprompt) || is_iac_prompt)) {
char *reprint;
/*
* the line starts with a prompt.
* #isprompt placed the actual prompt in $prompt,
* we must still process the rest of the line.
*/
if (surely_isprompt > 0 && surely_isprompt <= len) {
len = surely_isprompt;
prompt_status = 1;
} else if (!surely_isprompt && is_iac_prompt) {
prompt->str = ptrmcpy(prompt->str, linestart, len = surely_isprompt = is_iac_prompt);
if (MEM_ERROR) { promptzero(); errmsg("malloc(prompt)"); return 0; }
prompt_status = 1;
}
/*
* following data may be the reply to a previously sent command,
* so we may have to reprint that command.
*/
if ((reprint = reprint_getline()) && *reprint) {
smart_print(promptstr, 0);
status(-1);
sprintf(errtext,"(%s)\n", reprint);
from_backend(0,errtext,strlen(errtext));
} else if (!islast)
smart_print(promptstr, 1);
} else if (islast) {
prompt->str = ptrmcpy(prompt->str, linestart, len);
if (MEM_ERROR) { promptzero(); errmsg("malloc(prompt)"); return 0; }
prompt_status = 1; /* good, we got what to redraw */
} else
len = 0;
return len;
}
/*
* process remote input one line at time. stop at "\n".
*/
static void process_singleline (char **pbuf, int *psize)
{
/*
if (!search_action(*pbuf, 0)) {
//from_backend(0,*pbuf,*psize);
smart_print(*pbuf,0);
}
*psize=0;
*/
int size, len = 0;
char *wasn = 0, *buf, *linestart = *pbuf, *lineend, *end = *pbuf + *psize;
if ((lineend = memchr(linestart, '\n', *psize))) {
/* ok, there is a newline */
*(wasn = lineend) = '\0';
buf = lineend + 1; /* start of next line */
}
if (!lineend)
/* line continues till end of buffer, no trailing \n */
buf = lineend = end;
size = buf - linestart;
#ifdef DEBUGCODE_2
/* debug code to see in detail what codes come from the server */
{
char c;
char *t;
tty_putc('{');
for (t = linestart; t < lineend && (c = *t); t++) {
if (c < ' ' || c > '~')
tty_printf("[%d]", (int)c);
else
tty_putc(c);
}
tty_puts("}\n");
}
#endif
/*
* Try to guess where is the prompt... really not much more than
* a guess. Again, do it only on main connection: we do not want
* output from other connections to mess with the prompt
* of main connection :)
*
* Since we now have #prompt, behave more restrictively:
* if no #prompts match or a #prompt matches but does not set #isprompt
* (i.e. recognize it for not being a prompt),
* we check for #actions on it when the \n arrives.
* if a #prompt matches and sets #isprompt, then it is REALLY a prompt
* so never match #actions on it.
*/
if (lineend == end && printstrlen(linestart) < cols) {
/*
* The last line in the chunk we received has no trailing \n
* Assume it is a prompt.
*/
if (surely_isprompt && promptlen && prompt_status == 1) {
draw_prompt(); tty_printf("\r\n"); col0 = 0;
}
surely_isprompt = 0;
promptzero();
if (lineend > linestart && (len = grab_prompt(linestart, lineend-linestart, 1)))
size = len;
} else {
surely_isprompt = 0;
promptzero();
if (linestart[0]) {
/* set $last_line */
last_line->str = ptrmcpy(last_line->str, linestart, strlen(linestart));
if (MEM_ERROR) { print_error(error); return; }
if (lineend > linestart && (len = grab_prompt(linestart, lineend-linestart, 0)))
size = len;
}
if (!len && (!*linestart || (!search_action(linestart, 0) || opt_autoprint))) {
if (line0 < lines - 1)
line0++;
smart_print(linestart, 1);
}
}
/*
* search_prompt and search_action above
* might set error: clear it to avoid troubles.
*/
error = 0;
if (wasn) *wasn = '\n';
*pbuf += size;
*psize -= size;
}
/*
* Code to merge lines from host that were splitted
* into different packets: it is a horrible kludge (sigh)
* and can be used only on one connection at time.
* We currently do it on main connection.
*
* Note that this code also works for _prompts_ splitted into
* different packets, as long as no #prompts execute #isprompt
* on an incomplete prompt (as stated in the docs).
*/
int process_first_fragment (char *buf, int got)
{
int processed = 0;
/*
* Don't merge if the first part of the line was intercepted
* by a #prompt action which executed #isprompt
* (to avoid intercepting it twice)
*/
if (*buf == '\n') {
char deleteprompt = 0, matched = 0;
if (opt_compact) {
/* in compact mode, skip the first \n */
deleteprompt = 1;
processed++;
}
/*
* the prompt was actually a complete line.
* no need to put it on the top of received data.
* unless #isprompt was executed, demote it to a regular line,
* match #actions on it, copy it in last_line.
*/
if (!surely_isprompt) {
last_line->str = ptrcpy(last_line->str, prompt->str);
if (MEM_ERROR) { print_error(error); return 0; }
/*
* Kludge for kludge: don't delete the old prompt immediately.
* Instead, match actions on it first.
* If it matches, clear the line before running the action
* (done by the `1' in search_action() )
* If it doesn't match, delete it only if opt_compact != 0
*/
matched = search_action(promptstr, 1);
}
if (!matched)
// clear_input_line(deleteprompt);
status(-1);
} else {
/*
* try to merge the prompt with the first line in buf
* (assuming we have a line splitted into those parts)
* then clear the prompt.
*/
char *lineend, *spinning = NULL;
/* find the end of the first line. include the final newline. */
if ((lineend = strchr(buf, '\n')))
lineend++;
else
lineend = buf + got;
if (surely_isprompt || (spinning = memchr(buf, '\b', lineend - buf))) {
/*
* either #isprompt _was_ executed,
* or we got a MUME spinning bar.
* in both cases, don't try to merge.
*
* print a newline (to keep the old prompt on screen)
* only if !opt_compact and we didn't get a MUME spinning bar.
*/
// clear_input_line(opt_compact);
if (!spinning && !opt_compact)
tty_putc('\n'), col0 = 0;
promptzero();
} else {
ptr *pp, p = (ptr)0;
char *dummy;
int dummyint;
/* ok, merge this junk with the prompt */
TAKE_PTR(pp, p);
*pp = ptrcpy(*pp, prompt->str);
*pp = ptrmcat(*pp, buf, lineend - buf);
if (MEM_ERROR) { print_error(error); return 0; }
if (!*pp)
return 0;
dummy = ptrdata(*pp);
dummyint = ptrlen(*pp);
/* this also sets last_line or prompt->str : */
// clear_input_line(1);
process_singleline(&dummy, &dummyint);
processed = lineend - buf;
}
}
return processed;
}
/*
* process input from remote host:
* detect special sequences, trigger actions, locate prompt,
* word-wrap, print to tty
*/
void process_remote_input (char *buf, int size)
{
// if (promptlen && tcp_fd == tcp_main_fd)
promptzero(); /* discard the prompt, we look for another one */
prompt_reset_iac();
status(1);
do {
process_singleline(&buf, &size);
} while (size > 0);
}
static void common_clear (int newline)
{
clear_input_line(opt_compact);
if (newline) {
tty_putc('\n'); col0 = 0; status(1);
}
}
/*
* match action containing &1..&9 and $1..$9 and return actual params start/end
* in match_s/match_e - return 1 if matched, 0 if not
*/
static int match_weak_action (char *pat, char *line, int *match_s, int *match_e)
{
char mpat[BUFSIZE], *npat=0, *npat2=0, *src=line, *nsrc=0, c;
ptr *pbuf, buf = (ptr)0;
char *tmp, *realpat = pat;
int mbeg = 0, mword = 0, prm = -1, p;
TAKE_PTR(pbuf, buf);
if (jit_subst_vars(pbuf, pat))
pat = ptrdata(*pbuf);
if (REAL_ERROR) {
print_error(error);
DROP_PTR(pbuf);
return 0;
}
unescape(pat);
for (p = 0; p < NUMPARAM; p++)
match_s[p] = match_e[p] = 0;
p = 0;
if (*pat == '^') {
pat++;
mbeg = 1; /* anchor match at line start */
}
if (*pat == '&' || *pat == '$')
mbeg = - mbeg - 1; /* pattern starts with '&' or '$' */
while (pat && *pat) {
if (((c=*pat) == '&' || c == '$')) {
/* &x matches a string */
/* $x matches a single word */
tmp = pat + 1;
if (isdigit(*tmp)) {
p = 0;
while (isdigit(*tmp) && p < NUMPARAM) {
p *= 10;
p += *tmp++ - '0';
}
if (p <= 0 || p >= NUMPARAM) {
DROP_PTR(pbuf);
return 0;
}
prm = p;
pat = tmp;
if (c == '$')
mword = 1;
} else {
sprintf(errtext,"#error: bad action pattern `%s\'\n#missing digit after `%c\'\n",
realpat, *pat);
from_backend(0,errtext,strlen(errtext));
DROP_PTR(pbuf);
return 0;
}
}
npat = first_valid(pat, '&');
npat2 = first_valid(pat, '$');
if (npat2 < npat) npat = npat2;
if (!*npat) npat = 0;
if (npat) {
my_strncpy(mpat, pat, npat-pat);
/* mpat[npat - pat] = 0; */
} else
strcpy(mpat, pat);
if (*mpat) {
nsrc = strstr(src, mpat);
if (!nsrc) {
DROP_PTR(pbuf);
return 0;
}
if (mbeg > 0) {
if (nsrc != src) {
DROP_PTR(pbuf);
return 0;
}
mbeg = 0; /* reset mbeg to stop further start match */
}
if (prm != -1) {
match_s[prm] = src - line;
match_e[prm] = nsrc - line;
}
} else if (prm != -1) {
/* end of pattern space */
match_s[prm] = src - line;
match_e[prm] = strlen(line);
}
/* post-processing of param */
if (prm != -1 && match_e[prm] && mword) {
if (mbeg == -1) {
/* unanchored '$' start, take last word */
if ((tmp = memrchrs(line + match_s[prm],
match_e[prm] - match_s[prm],
DELIM, DELIM_LEN))) {
match_s[prm] = tmp - line + 1;
}
} else if (!*pat) {
/* '$' at end of pattern, take first word */
if ((tmp = memchrs(line + match_s[prm],
match_e[prm] - match_s[prm],
DELIM, DELIM_LEN)))
match_e[prm] = tmp - line;
} else {
/* match only if param is single-worded */
if (memchrs(line + match_s[prm],
match_e[prm] - match_s[prm],
DELIM, DELIM_LEN)) {
DROP_PTR(pbuf);
return 0;
}
}
}
if (prm != -1 && match_e[prm])
mbeg = mword = 0; /* reset match flags */
src = nsrc + strlen(mpat);
pat = npat;
}
DROP_PTR(pbuf);
match_s[0] = 0; match_e[0] = strlen(line);
return 1;
}
/*
* Search for #actions or #prompts to trigger on an input line.
* The line can't be trashed since we want to print it on the screen later.
* Return 1 if line matched to some #action, 0 otherwise
*
* Optionally clear the input line before running the trigger command.
*/
static int search_action_or_prompt (char *line, char clearline, char onprompt)
{
/*
* we need actionnode and promptnode to be the same `triggernode' type
*/
triggernode *p;
int ret = 0;
int match_s[NUMPARAM], match_e[NUMPARAM];
for (p = onprompt ? prompts : actions; p; p = p->next) {
if (p->active &&
((p->type == ACTION_WEAK && match_weak_action(p->pattern, line, match_s, match_e))
))
{
push_params(); if (error) return 0;
ret = 1; error = 0;
set_params(line, match_s, match_e); if (error) return 0;
if (onprompt == 2) {
prompt->str = ptrmcpy(prompt->str, line, strlen(line));
}
// if (clearline)
// clear_input_line(1);
parse_instruction(p->command, 0, 1, 1);
history_done = 0;
if (error!=DYN_STACK_UND_ERROR && error!=DYN_STACK_OV_ERROR)
pop_params();
break;
}
}
if (error) return 0;
return ret;
}
/*
* split str into words separated by DELIM, and place in
* VAR[1].str ... VAR[9].str -
* the whole str is put in VAR[0].str
*/
static char *split_words (char *str)
{
int i;
char *end;
ptr *prm;
*VAR[0].str = ptrmcpy(*VAR[0].str, str, strlen(str));
for (i = 1; i < NUMPARAM; i++) {
*VAR[i].num = 0;
prm = VAR[i].str;
/* skip multiple span of DELIM */
while (*str && strchr(DELIM, *str))
str++;
end = str;
while (*end && !strchr(DELIM, *end))
end++;
*prm = ptrmcpy(*prm, str, end-str);
str = end;
if (MEM_ERROR) { print_error(error); return NULL; }
}
return str;
}
/*
* free the whole stack and reset it to empty
*/
static void free_allparams (void)
{
int i,j;
paramstk.curr = 0; /* reset stack to empty */
for (i=1; i<MAX_STACK; i++) {
for (j=0; j<NUMPARAM; j++) {
ptrdel(paramstk.p[i][j].str);
paramstk.p[i][j].str = (ptr)0;
}
}
for (j=0; j<NUMPARAM; j++) {
VAR[j].num = ¶mstk.p[0][j].num;
VAR[j].str = ¶mstk.p[0][j].str;
}
}
void push_params (void)
{
int i;
if (paramstk.curr < MAX_STACK - 1)
paramstk.curr++;
else {
print_error(error=DYN_STACK_OV_ERROR);
free_allparams();
return;
}
for (i=0; i<NUMPARAM; i++) {
*(VAR[i].num = ¶mstk.p[paramstk.curr][i].num) = 0;
ptrzero(*(VAR[i].str = ¶mstk.p[paramstk.curr][i].str));
}
}
void pop_params (void)
{
int i;
if (paramstk.curr > 0)
paramstk.curr--;
else {
print_error(error=DYN_STACK_UND_ERROR);
free_allparams();
return;
}
for (i=0; i<NUMPARAM; i++) {
ptrdel(*VAR[i].str); *VAR[i].str = (ptr)0;
VAR[i].num = ¶mstk.p[paramstk.curr][i].num;
VAR[i].str = ¶mstk.p[paramstk.curr][i].str;
}
}
static void set_params (char *line, int *match_s, int *match_e)
{
int i;
for (i=0; i<NUMPARAM; i++) {
*VAR[i].num = 0;
if (match_e[i] > match_s[i]) {
*VAR[i].str = ptrmcpy(*VAR[i].str, line + match_s[i],
match_e[i] - match_s[i]);
if (MEM_ERROR) {
print_error(error);
return;
}
} else
ptrzero(*VAR[i].str);
}
}
/*
* substitute $0..$9 and @0..@9 in a string
* (unless $ or @ is escaped with backslash)
*
* return 0 if dst not filled. if returned 0 and not error,
* there was nothing to substitute.
*/
static int subst_param (ptr *buf, char *src)
{
int done, i;
char *dst, *tmp, kind;
if (!strchr(src, '$') && !strchr(src, '@'))
return 0;
i = strlen(src);
if (!*buf || ptrlen(*buf) < i) {
*buf = ptrsetlen(*buf, i);
if (REAL_ERROR)
return 0;
}
dst = ptrdata(*buf);
while (*src) {
while (*src && *src != '$' && *src != '@' && *src != ESC)
*dst++ = *src++;
if (*src == ESC) {
while (*src == ESC)
*dst++ = *src++;
if (*src)
*dst++ = *src++;
}
done = 0;
if (*src == '$' || *src == '@') {
kind = *src == '$' ? 1 : 0;
tmp = src + 1;
if (isdigit(*tmp)) {
i = atoi(tmp);
while (isdigit(*tmp))
tmp++;
if (i < NUMPARAM) {
int max = 0, n;
char *data = NULL, buf2[LONGLEN];
done = 1;
src = tmp;
/* now the actual substitution */
if (kind) {
if (*VAR[i].str && (data = ptrdata(*VAR[i].str)))
max = ptrlen(*VAR[i].str);
} else {
sprintf(data = buf2, "%ld", *VAR[i].num);
max = strlen(buf2);
}
if (data && max) {
n = dst - ptrdata(*buf);
*buf = ptrpad(*buf, max);
if (REAL_ERROR)
return 0;
dst = ptrdata(*buf) + n;
memcpy(dst, data, max);
dst += max;
}
}
}
}
if (!done && (*src == '$' || *src == '@'))
*dst++ = *src++;
}
*dst = '\0';
return 1;
}
/*
* just-in-time substitution:
* substitute ${name}, @{name} and #{expression} in a string
* (unless `${', `@{' or `#{' are escaped with backslash)
*
* return 0 if dst not filled. if returned 0 and not error,
* there was nothing to substitute.
*/
static int jit_subst_vars (ptr *buf, char *src)
{
int i, done, kind;
char *tmp, *name, *dst, c;
varnode *named_var;
if (!strstr(src, "${") && !strstr(src, "@{") && !strstr(src, "#{"))
return 0;
i = strlen(src);
if (!*buf || ptrlen(*buf) < i) {
*buf = ptrsetlen(*buf, i);
if (REAL_ERROR)
return 0;
}
dst = ptrdata(*buf);
while (*src) {
while (*src && *src != '$' && *src != '@' && *src != '#' && *src != ESC)