-
Notifications
You must be signed in to change notification settings - Fork 0
/
client
executable file
·1572 lines (1351 loc) · 37.3 KB
/
client
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
#!/usr/bin/perl
# $Id: client,v 1.1 1999/08/10 21:18:18 rsh Exp $
use strict qw(vars subs);
# we want nice terminal I/O handling
use Term::ReadKey;
# and cool option processing
use Getopt::Long;
require 'sock.pl';
# set up variables
my $termwidth = (GetTerminalSize())[0] - 4; # for pprint with nice right margin
my $speedwalking = 0; # not currently speedwalking
my $hostname = ''; # from cmd line
my $service = ''; # from cmd line
my $helpfile = 'client.hlp'; # pathname for client help file
my $cmdfile = ''; # command file read on startup (from -f cmdline option)
my $cmdchar = '#'; # client commands must be immediately preceded by this
my $cmdsep = ';;'; # separates multiple commands on the same input line
my $histsize = 50; # max number of commands in history list
my $histnext = 1; # offset into history list of next command
my %history = (); # empty history to start
my %toggles = (
dosubs => 1, # do substitutions
doactions => 1, # do actions
showvar => 1, # show variable creation/deletion msgs
showalias => 1, # show alias creation/deletion msgs
showaction => 1, # show action creation/deletion msgs
showsub => 1, # show substitution creation/deletion msgs
showtog => 1, # show toggle messages
speedwalk => 1, # speedwalking
prettyprint => 1, # pretty printing for long lines from client (lines from
# server are printed as-is
# to add a new toggle, just stick in the key and it's initial
# state (0 or 1). The just reference it whenever you want the value.
);
my %log = (
active => 0, # currently logging to file?
file => '' # file we're logging to
);
my %textin = (
active => 0, # currently textin'ing?
file => '', # file we're reading from
regexp => '', # when get stuff matching this, read and do next cmd
prefix => '' # prefix all read string with this before processing
);
my $totline = ''; # the big line resulting from a bunch of '\'-ending lines
my $sockstatus = ''; # return status of read on socket
my %variables = (); # currently no variables
my %substitutions = (); # no subs at the moment
my %actions = (); # none at the moment
my %aliases = (); # none yet
my $cursession = ''; # currently no open connection
my @clientcmds = # a list of the client commands. This list is used to
# dispatch calls to the functions whose names are
# Client<cmdname>(@params). i.e. there is a ClientAction
# function.
qw(action alias evaluate help history killall log nop substitute textin
toggle unaction unalias unsubstitute unvariable variable write loop math);
# Clean shutdown on error
sub abort
{
die "### Connection closed at host side.\n";
} # abort
# Handle signals that could kill us
sub handler
{
my $sig = $_[0];
print "### Caught SIG$sig--shutting down.\n";
exit 1;
} # handler
# handle SIGWINCH for when terminal changes size
sub handlewinch
{
$termwidth = (GetTerminalSize())[0] - 4;
} # handlewinch
# Always called at program termination
sub END
{
ReadMode 0; # return to normal terminal I/O
close TEXTIN if $textin{'active'};
close LOGFILE if $log{'active'};
sock'close_all();
exit 0;
} # END
# Takes a single string. Returns the first whitespace- OR brace-surrounded
# word and sets its parameter to the remainder of the string. If the next
# word begins with a $cmdsep, return nothing and do not modify the params.
# That means it'll only get words belonging to the current command.
# If returning a brace-surrounded word, the braces are present.
# e.g.
# #eval {print "Weelah.\n";};;lastcmd
# returns "#eval" and param contains {"print "Weelah.\n";};;lastcmd"
sub GetWord
{
my $arg;
my $word;
$arg = $_[0];
$arg =~ s/^\s+//; # kill leading whitespace
if ($arg eq '')
{
$_[0] = '';
return '';
}
if ($arg =~ /^$cmdsep/o) # if at a $cmdsep, don't grab anymore words
{
return '';
}
if ($arg =~ /^{/) # opening brace?
{
$word = "{".GetBraceStuff($arg)."}";
$_[0] = $arg;
}
else # anything else
{
$arg =~ /(.+?)(\s|$cmdsep|$)(.*)/o;
$word = $1;
$_[0] = $2.$3;
}
$_[0] =~ s/^\s+(.*)/$1/; # kill leading whitespace now...yes it has
# to be done here AND above
$word;
} # GetWord
# Takes a single parameter: string surrounded by braces. Returns the contents
# of the braces, and the param passed in holds the remainder of the
# string (the part without the braces). Assumes first non-space character
# in the string is an opening brace.
# Also unquotes all top-level quoted braces within the first brace set.
# Throws out leading whitespace.
# e.g.
# {#eval {print "You\}\{ typed ";};;\};;#eval {print "blah.\n";}}
# is returned as
# #eval {print "You\}\{ typed ";};;};;#eval {print "blah.\n";}
sub GetBraceStuff
{
my ($arg, $nest, $p) = ($_[0], 1, 1);
my $ch;
my $ret = '';
$arg =~ s/^\s+//; # kill leading whitespace
if ($arg eq '')
{
$_[0] = '';
return '';
}
while (($ch = substr($arg, $p, 1) , $p) < length($arg) && $nest > 0)
{
if ($ch =~ /{/)
{
$nest++;
}
elsif ($ch =~ /}/)
{
$nest--;
}
elsif ($ch =~ /\\/) # quoting next char
{
my $t = substr($arg, $p+1, 1); # grab quoted char
if ($t =~ /{|}/) # brace?
{
if ($nest < 2) # top-level?
{
$ret .= $t; # unquote it
$p += 2; # go to next true char
next;
}
}
# output quote and char
$ret .= "\\$t";
$p += 2;
next;
}
$ret .= $ch; # tack on the character
$p++;
}
# braces didn't match
if ($nest > 0)
{
print "### Unmatched brace error.\n";
$_[0] = '';
return '';
}
chop $ret; # kill the brace matching the 1st one on line
$_[0] = substr($arg, $p); # parameter is replaced with remainder of line
$ret;
} # GetBraceStuff
# Reads the next line from the textin file and processes it. If the file is
# is done, close it and reset the textin state variables. Assumes everything
# is set up and ready to go for the next read (i.e. file is open, etc.).
sub DoTextinLine
{
my $tmp = <TEXTIN>;
if (!defined($tmp))
{
close TEXTIN;
# get rid of notification variable now that we're done textin
delete $variables{'textin'};
pprint("### Done reading from \"$textin{'file'}\".\n", '### ', '###\s*');
$textin{'active'} = 0;
$textin{'file'} = '';
$textin{'regexp'} = '';
$textin{'prefix'} = '';
}
else
{
# just like it was typed, but with any prefix attached
DoCommand($textin{'prefix'}.$tmp);
}
} # DoTextinLine
# Takes a line of text and does any actions that match on the line.
sub DoActions
{
my $line = $_[0];
my $i;
my @found;
foreach $i (sort(keys %actions))
{
#if ($line =~ /$i/)
if (@found = ($line =~ /$i/))
{
my $j = 0;
my @vars = ();
# transfer positional parameters that matched in the action
# into the variables hash.
# Note which variables we set as well, so we can pull out
# the $cmdseps in them before we do the action.
if ($1 ne '')
{
do
{
$j++;
push(@vars, $j); # save name of the variable
$variables{$j} = ${$j};
} until (${$j} eq $+);
}
# remove any $cmdseps from the variables we got to prevent
# other people from abusing user's triggers
foreach $j (@vars)
{
$variables{$j} =~ s/$cmdsep//go;
}
# pretend it was a typed command
DoCommand($actions{$i});
}
} # foreach
} # DoActions
# Called whenever data is read from socket.
# Assumes its only parameter is the string read from the server.
# Do matching actions and substitutions.
# Strips out ANSI color sequences before action-matching is performed, so you
# don't have to put in groupings to match the color codes in your regexps.
sub FromServer
{
my $line = $_[0];
my $pureline = $line;
my $i;
my $linedead = 0;
# strip out ANSI color
if ($pureline =~ /\033/)
{
$pureline =~ s/\033\[.*?m//g;
}
# do subs on $line (logged lines are not substituted), unless ignoring them
if ($toggles{'dosubs'})
{
foreach $i (sort(keys %substitutions))
{
$line =~ s/$i/$substitutions{$i}/g; # change all matches
$linedead = 1 if ($line =~ /^$/); # empty line now?
}
}
# do any actions that match this string, unless ignoring them
DoActions($pureline) if ($toggles{'doactions'});
#display data, unless we substituted it all away
print $line unless $linedead;
# if we're doing a textin, spew out the next command if the regexp matches
# what we just got. If you do not give a regular
# expression for the textin cmd, all lines in the file will be shot out
# at once (see the polling i/o loop).
DoTextinLine() if ($textin{'active'} && $textin{'regexp'} ne '' &&
$line =~ /$textin{'regexp'}/);
print LOGFILE $pureline if ($log{'active'}); # log the line WITHOUT color
} # FromServer
# Take care of variable interpolation. Variables are denoted by
# "$$varname", where varname is a collection of digits, numbers, or '_'.
# If $$varname is a number, it is a position parameter.
# $$1 signifies the first grouping in a regexp, $$2, the second,
# etc. Returns the interpolated line.
sub DoVariables
{
my $line = $_[0];
my $oldline = $line;
my @varnames = ();
my $var = 0;
# one the first pass, we create an array of variable names that we
# are interpolating. Then, on the second pass, we replace all of them.
# We don't use the s///g regexp operator since that will also interpolate
# any variables whose values are the name of a variable.
# We don't interpolate variable that refer to variables.
while (defined($line) && $line =~ /\$\$(\w+)/) # for each variable in the line
{
# replace it with nothing...we're just picking the names out
$var = $1;
$line =~ s/\$\$$var//g;
push(@varnames, $var);
}
# now that we have the names, replace them with values
my $i;
foreach $i (@varnames)
{
$oldline =~ s/\$\$$i/$variables{$i}/g;
}
$oldline; # return the modified line
} # DoVariables
# Adds the single parameter to the next position in the history list.
sub AddToHistory
{
my $temp = $_[0];
chomp($temp);
if ($temp ne '')
{
$history{$histnext} = $temp; # use an assoc array
$histnext++;
$histnext = 1 if ($histnext > $histsize); # wrap around if full
}
} # AddToHistory
# Takes single param that is assumed to be a single command with no
# leading or trailing whitespace or $cmdsep's. If it is
# a bang-command, replace the bang stuff with the command
# from the history list, set the param to the modified line, and
# return 1, else return undef and don't modify the param.
sub GetHistoryCommand
{
my $line = $_[0];
# first char must be a bang
if ($line =~ /^!/)
{
# doubled bang means most recent cmd
if ($line =~ /^!!/)
{
# replace the bang stuff with the right command
my $temp = ($histnext - 1 < 1) ? $history{$histsize} : $history{$histnext - 1};
$_[0] = $temp.substr($line, 2);
return 1;
}
# bang followed by only digits
elsif ($line =~ /^!(\d+)/)
{
$_[0] = $history{$1}.substr($line, 1 + length($1));
return 1;
}
# bang followed by stuff
elsif ($line =~ /^!(.+)/)
{
# compare $1 to the start of each line in the history and
# use the one that matches a more recent line
my $i = $histnext - 1;
my $j = 1;
my $text = KillBraces($1);
while ($j <= $histsize)
{
$i--;
$i = $histsize if ($i < 1);
last if ($history{$i} =~ /^$text/);
$j++;
}
$_[0] = $history{$i}.substr($line, 1 + length($text));
}
elsif ($line =~ /^!{(.*)}/)
# bang with stuff in braces
{
my $i = $histnext - 1;
my $j = 1;
my $text = $1;
while ($j <= $histsize)
{
$i--;
$i = $histsize if ($i < 1);
last if ($history{$i} =~ /^$text/);
$j++;
}
$_[0] = $history{$i}.substr($line, 3 + length($text));
}
# anything else will go through as a server command
}
# not a history command
return '';
} # GetHistoryCommand
# Takes a sequence of two parameters (any more and they are ignored).
# The first is a regular
# expression and the second is a command line to execute when the host
# sends a line matching the regular expression.
sub ClientAction
{
my $regexp = KillBraces($_[0]);
my $cmds = KillBraces($_[1]);
# add/modify action
if ($regexp ne '' && $cmds ne '')
{
# put into assoc array
$actions{$regexp} = $cmds;
pprint("### {$regexp} now triggers {$cmds}\n", '### ', '###\s*') if ($toggles{'showaction'});
}
elsif ($cmds eq '' && $regexp ne '')
# remove one based on the regexp
{
if (delete($actions{$regexp}) ne '')
{
pprint("### {$regexp} is no longer an action.\n", '### ', '###\s*') if ($toggles{'showaction'});
}
else
{
pprint("### {$regexp} is not an action.\n", '### ', '###\s*');
}
}
elsif ($regexp eq '' && $cmds ne '')
{
print("### You can't have an empty action.\n");
}
else
# display existing actions
{
print "### You have the following actions:\n".
"###################################\n";
my $j = 0;
my $i;
foreach $i (sort(keys %actions))
{
pprint(sprintf("### %3d. {$i} -> {$actions{$i}}\n", $j), '###', '###\s*\d+\.\s*');
$j++;
}
print "###################################\n";
}
} # ClientAction
# Takes two parameters (any more and they're ignored). The first is a
# regular expression that (when a match is entered), the second parameter
# (a sequence of commands) is executed as though the user had typed it
# instead. Note that regexp grouping is applied, so that $$1 will refer to
# the value of the first regexp group, $$2 to the second.
sub ClientAlias
{
my $regexp = KillBraces($_[0]);
my $cmds = KillBraces($_[1]);
# display existing aliases
if ($regexp eq '' && $cmds eq '')
{
print "### You have the following aliases:\n".
"###################################\n";
my $j = 0;
my $i;
foreach $i (sort(keys %aliases))
{
pprint(sprintf("### %3d. {$i} -> {$aliases{$i}}\n", $j), '###', '###\s*\d+\.\s*');
$j++;
}
print "###################################\n";
}
# they refer to the same thing (infinite loop at execution time)
# Note that we quote the regexp so that we can compare a regexp to it.
# Also note that it's case insensitive cuz that's how aliases are.
elsif ($regexp =~ /^\Q$cmds\E$/i)
{
pprint("### {$regexp} would be aliased to itself. Don't do that.\n", '### ', '###\s*');
}
# add/modify alias
elsif ($regexp ne '' && $cmds ne '')
{
# store in assoc array
$aliases{$regexp} = $cmds;
pprint("### {$regexp} now aliases {$cmds}\n", '### ', '###\s*') if ($toggles{'showalias'});
}
elsif ($regexp ne '' && $cmds eq '')
# remove one based on regexp
{
if (delete($aliases{$regexp}) ne '')
{
pprint("### {$regexp} is no longer an alias.\n", '### ', '###\s*') if ($toggles{'showalias'});
}
else
{
pprint("### {$regexp} is not an alias.\n", '### ', '###\s*');
}
}
elsif ($regexp eq '' && $cmds ne '')
{
print "### You can't have an empty alias.\n";
}
} # ClientAlias
# Takes two params (any more and they're ignored). 1st param is the toggle
# key, the second is (optional) "on" or "off".
sub ClientToggle
{
my $which = KillBraces($_[0]);
my $switch = KillBraces($_[1]);
# no param, so display toggle status
if ($which eq '' && $switch eq '')
{
my $i;
print "### Toggle status:\n".
"##################\n";
foreach $i (sort(keys %toggles))
{
printf("### %15s : %s\n", $i, $toggles{$i} ? "on" : "off");
}
print "##################\n";
}
elsif ($which eq '' && $switch ne '')
{
print "### Toggle which state?\n";
}
elsif ($which ne '' && $switch eq '')
# toggle the state
{
my $i;
my $found = 0;
foreach $i (sort(keys %toggles))
{
# valid toggle prefix?
if ($i =~ /^$which/i)
{
$found = 1;
$toggles{$i} ^= 1; # do the toggle
if ($toggles{'showtog'})
{
print "### \"$i\" is now ", $toggles{$i} ? "on.\n" : "off.\n";
}
}
}
print "### \"$which\" is not a valid toggle.\n" if (!$found);
}
else
{
my $i;
foreach $i (sort(keys %toggles))
{
# valid toggle?
if ($i =~ /^$which/i)
{
if ($switch =~ /^on$/i)
{
# use ourselves to do the work by first turning it off then
# making us toggle it
$toggles{$i} = 0;
ClientToggle($i);
return;
}
elsif ($switch =~ /^off$/i)
{
$toggles{$i} = 1;
ClientToggle($i);
return;
}
else
{
print "### You can only use \"on\" or \"off\".\n";
return;
}
}
}
print "### \"$which\" is not a valid toggle.\n";
}
} # ClientToggle
# Takes one parameter (any more are ignored). Evaluates the parameter
# as though it were perl code.
sub ClientEvaluate
{
# interpolate any variables
my $expr = DoVariables(KillBraces($_[0]));
eval $expr if ($expr ne '');
} # ClientEvaluate
# Takes one parameter (others are ignored) as a client command and displays
# the help text associated with that command.
sub ClientHelp
{
# get it in lowercase for comparisons
my $cmd = lc(KillBraces($_[0]));
my $showing = 0; # not currently showing a topic
my $found = 0;
$cmd =~ s/^$cmdchar*//; # kill any leading $cmdchars
# parse each line in the help file. When a line matching ^<$cmd>$
# is found, everything up until the line matching ^<\/$cmd>$ is
# displayed. Note that the complete command name is necessary.
# matches are case-insensitive.
if (open(HELPFILE, "<$helpfile"))
{
print "##########\n";
while (<HELPFILE>)
{
s/#/$cmdchar/go; # replace all #'s with the correct cmdchar
s/;;/$cmdsep/go; # replace all ;; with correct cmdsep
last if (/^<\/$cmd>$/); # end of topic
print "### $_" if ($showing);
$showing = 1 if (/^<$cmd>$/);
}
print "##########\n";
close HELPFILE;
}
else
{
print "### Can't file helpfile \"$helpfile\".\n";
}
} # ClientHelp
# Takes no parameters (ignores all). Displays the history list.
sub ClientHistory
{
my $i = $histnext;
my $j = 1;
my $s;
print "### Command history:\n".
"####################\n";
while ($j <= $histsize)
{
if ($history{$i} ne '')
{
$s = sprintf("### %3d %s\n", $i, $history{$i});
pprint($s, '###', '###\s*\d+ ');
}
$i++;
$i = 1 if ($i > $histsize);
$j++;
}
print "####################\n";
} # ClientHistory
# Takes no parameters. Wipes out all aliases, substitutions, actions, and
# variables.
sub ClientKillall
{
undef %variables;
undef %actions;
undef %aliases;
undef %substitutions;
print "### All actions, aliases, substitutions, and variables deleted.\n";
} # ClientKillall
# Takes a single parameter (others are ignored). It is assumed to be the name
# of a file to which the session is logged. A timestamp is first written.
sub ClientLog
{
my $file = KillBraces($_[0]);
# if we're logging, shut it off
if ($log{'active'})
{
$log{'active'} = 0;
print "### Logging to \"$log{'file'}\" is off.\n";
$log{'file'} = '';
close LOGFILE;
# if no file was given and we were logging, don't start a new log,
# just return.
return if ($file eq '');
}
# log to new file
if ($file ne '')
{
if (!open(LOGFILE, ">$file"))
{
print "### Can't open \"$file\" for output.\n";
}
else
{
print LOGFILE scalar(localtime())."\n";
$log{'active'} = 1;
$log{'file'} = $file;
print "### Logging to \"$log{'file'}\" is on.\n";
}
}
else
# no file given and we're not logging
{
print "### You're not logging anything.\n";
}
} # ClientLog
sub ClientNop
{
# do nothing
;
} # ClientNop
# Takes two parameters (others ignored). The first is a regular expression
# that, when received from the server, is replaced with the second parameter.
sub ClientSubstitute
{
# interpolate variables for both params
my $regexp = DoVariables(KillBraces($_[0]));
my $text = DoVariables(KillBraces($_[1]));
# print the current list of substitutions
if ($regexp eq '' && $text eq '')
{
print "### You have the following substitutions:\n".
"#########################################\n";
my $j = 0;
my $i;
foreach $i (sort(keys %substitutions))
{
pprint(sprintf("### %3d. {$i} -> {$substitutions{$i}}\n", $j), '###', '###\s*\d+\.\s*');
$j++;
}
print "#########################################\n";
}
# substituting same stuff is stupid, so don't do it
elsif ($regexp eq $text)
{
pprint("### $regexp would substitute itself. Why bother?\n", '### ', '###\s*');
}
# add/modify substitution
elsif ($regexp ne '')
{
# store them in an assoc array
$substitutions{$regexp} = $text;
pprint("### {$regexp} is replaced with {$text}\n", '### ', '###\s*') if ($toggles{'showsub'});
}
elsif ($regexp eq '' && $text ne '')
{
print "### You can't substitute something for nothing.\n";
}
} # ClientSubstitute
# Takes three params (others ignored). First param is file to read from,
# second is the regexp to match, 3rd is prefix.
sub ClientTextin
{
# the params get interpolated
my $file = DoVariables(KillBraces($_[0]));
my $regexp = DoVariables(KillBraces($_[1]));
my $prefix = DoVariables(KillBraces($_[2]));
# if already textin'ing, shut it off
if ($textin{'active'})
{
print "### ${cmdchar}textin from $textin{'file'} stopped.\n";
close TEXTIN;
$textin{'active'} = 0;
$textin{'file'} = '';
$textin{'regexp'} = '';
$textin{'prefix'} = '';
# get rid of notification variable now that we're done textin
delete $variables{'textin'};
}
else
{
if ($file ne '')
{
if (!open(TEXTIN, "<$file"))
{
print "### Can't open \"$file\"for input.\n";
return;
}
$textin{'active'} = 1;
$textin{'file'} = $file;
$textin{'regexp'} = $regexp;
$textin{'prefix'} = $prefix;
# set notification variable to show we're doing a textin
$variables{'textin'} = 'active';
DoTextinLine(); # do one to get started
}
}
} # ClientTextin
# Takes a single params: offset into the list of # entries, denoting which
# one to remove. If param is 'all', remove them all.
sub ClientUnaction
{
my $which = $_[0];
my $i;
my $j = 0;
return if ($which eq '');
if ($which =~ /^all$/i)
{
undef %actions;
print "### All actions deleted.\n" if ($toggles{'showaction'});
return;
}
foreach $i (sort(keys %actions))
{
if ($which == $j)
{
pprint("### {$i} is no longer an action.\n", '### ', '###\s*') if ($toggles{'showaction'});
delete $actions{$i};
return;
}
$j++;
}
} # ClientUnaction
# Takes a single params: offset into the list of # entries, denoting which
# one to remove. If param is 'all', remove them all.
sub ClientUnalias
{
my $which = $_[0];
my $i;
my $j = 0;
return if ($which eq '');
if ($which =~ /^all$/i)
{
undef %aliases;
print "### All aliases deleted.\n" if ($toggles{'showalias'});
return;
}
foreach $i (sort(keys %aliases))
{
if ($which == $j)
{
pprint("### {$i} is no longer an alias.\n", '### ', '###\s*') if ($toggles{'showalias'});
delete $aliases{$i};
return;
}
$j++;
}
} # ClientUnalias
# Takes a single params: offset into the list of # entries, denoting which
# one to remove. If param is 'all', remove them all.
sub ClientUnsubstitute
{
my $which = $_[0];
my $i;
my $j = 0;
return if ($which eq '');
if ($which =~ /^all$/i)
{
undef %substitutions;
print "### All substitutions deleted.\n" if ($toggles{'showsub'});
return;
}
foreach $i (sort(keys %substitutions))
{
if ($which == $j)
{
pprint("### {$i} is no longer a substitution.\n", '### ', '###\s*') if ($toggles{'showsub'});
delete $substitutions{$i};
return;
}
$j++;
}
} # ClientUnsubstitute
# Takes a single params: offset into the list of # entries, denoting which
# one to remove. If param is 'all', remove them all.
sub ClientUnvariable
{
my $which = $_[0];
my $i;
my $j = 0;
return if ($which eq '');
if ($which =~ /^all$/i)
{
undef %variables;
print "### All variables deleted.\n" if ($toggles{'showvar'});
return;
}
foreach $i (sort(keys %variables))
{
if ($which == $j)
{
pprint("### {$i} is no longer a variable.\n", '### ', '###\s*') if ($toggles{'showvar'});
delete $variables{$i};
return;
}
$j++;
}
} # ClientUnvariable
# Takes two params (others ignored). First param is variable name, second
# is value.
sub ClientVariable
{
my $name = DoVariables(KillBraces($_[0]));
my $value = DoVariables(KillBraces($_[1]));
# add/modify variable
if ($name ne '' && $value ne '')
{
# store in assoc array
$variables{$name} = $value;
pprint("### {$name} now has the value {$value}\n", '### ', '###\s*') if ($toggles{'showvar'});
}
elsif ($name eq '' && $value eq '')
# print list of current variables
{
print "### You have the following variables:\n".
"#####################################\n";
my $j = 0;
my $i;