-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymsvr.pl
executable file
·377 lines (334 loc) · 6.39 KB
/
symsvr.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use IO::Handle;
###### config
my $DEF_PORT = 20000;
my $INST_NO = 0;
my $DEBUG = $ENV{DEBUG};
my $IS_MSWIN = $^O eq 'MSWin32';
my $SYMFIND = $ENV{SYMFIND} || 'symfind';
my $SYMSCAN = $ENV{SYMSCAN} || 'symscan.pl';
my $DEF_REPO = 'tags.repo.gz';
# update frequency: e.g. "2h" - 2hours; "30"/"30m" - 30min; "30s"
my $UPDATE = $ENV{P_UPDATE} || "2h";
###### global
my $g_tgtpid;
my $g_isclient = 0;
my $g_sig = ''; # update/quit/""
my $g_updateCmd;
my $g_repo;
###### toolkit {{{
sub mychop
{
$_[0] =~ s/[ \r\n]+$//;
}
sub msg # ($msg, [$force=0])
{
print STDERR $_[0] if $DEBUG || $_[1];
}
sub mydie # ($msg)
{
msg("*** error: $_[0]\n", 1);
kill 9, $g_tgtpid if $g_tgtpid;
exit(-1);
}
sub getTcpPort # ()
{
my $p = $ENV{TCP_PORT} || $DEF_PORT + $INST_NO;
msg("=== TCP_PORT=$p\n", 1) if !$g_isclient && !$g_sig;
return $p;
}
package CommInet; # {{{
our $TCP_PORT;
use IO::Socket::INET;
sub new # ({isclient=>0})
{
my $clsName = shift;
my %opt = @_;
my $this = bless {
type => 'inet',
isclient => $opt{isclient}
}, $clsName;
$TCP_PORT = main::getTcpPort();
if ($this->{isclient}) {
$this->{sock} = IO::Socket::INET->new (
PeerAddr => '127.0.0.1',
PeerPort => $TCP_PORT,
Proto => 'tcp',
) or main::mydie("cannot connect to server (port=$TCP_PORT).");
}
else {
$this->{sock} = IO::Socket::INET->new (
#LocalAddr => '127.0.0.1',
LocalPort => $TCP_PORT,
Reuse => !$IS_MSWIN,
# ReuseAddr => 1,
# ReusePort => 1,
Proto => 'tcp',
Listen => 1,
) or main::mydie("cannot open socket.");
}
$this->{sock}->autoflush(1);
$this;
}
sub destroy
{
my $this = shift;
if ($this->{session}) {
$this->{session}->close();
}
$this->{sock}->close();
}
sub put # ($line)
{
my $this = shift;
my $line = $_[0];
my $sck = $this->{isclient}? $this->{sock}: $this->{session} ;
print $sck $line;
}
# return: content or '<timeout>' for recv timeout
sub get # (\%opt={timeout})
{
my $this = shift;
my ($opt) = @_;
my $sck;
local $_;
if ($opt->{timeout}) {
$this->{sock}->timeout($opt->{timeout});
}
if ($this->{isclient}) {
$sck = $this->{sock};
}
else {
if ($this->{session}) {
$this->{session}->close();
delete $this->{session};
}
$sck = $this->{session} = $this->{sock}->accept();
return '<timeout>' if !defined $sck; # timeout
}
$_ = <$sck>;
}
# sub accept
# {
# my $this = shift;
# if ($this->{session}) {
# $this->{session}->close();
# delete $this->{session};
# }
# $this->{session} = $this->{sock}->accept();
# $this->{session}->autoflush(1);
# 1;
# }
#}}}
package main;
#}}}
###### function {{{
sub showHelp
{
print "Usage: symsvr [repo=$DEF_REPO] [:{instance_no=0}]
e.g.
symsvr
symsvr 1.repo.gz
symsvr myprj.gz :1
Show help:
symsvr -h
Run as client:
symsvr -c {cmd} [:{instance_no=0}]
e.g.
symsvr -c \"f dbm\" :1
";
}
sub runClient # ($cmds)
{
my ($cmds) = @_;
my $comm = CommInet->new(isclient=> 1) or die "*** cannot start client!";
$comm->put("$cmds\n");
my $line;
while(defined ($line = $comm->get()))
{
print $line;
}
$comm->destroy();
}
sub parseRetLine # ($comm, $line, $hideout)
{
my ($comm, $line, $hideout) = @_;
$comm->put("$line\n") if $comm && !$hideout;
}
sub execCmd # ($comm, $cmd, [$hideout=0])
{
my ($comm, $cmd, $hideout) = @_;
if (defined $cmd)
{
print MAIN_WR "$cmd\n";
msg("(cmd) '$cmd'\n");
}
while(<MAIN_RD>)
{
mychop($_);
if(/^>/) {
return 1;
}
msg(">>> '$_'\n");
parseRetLine($comm, $_, $hideout);
}
return; # undef - quit server
}
sub logtm
{
my @a = localtime(time);
sprintf("%d/%02d/%02d %02d:%02d:%02d", $a[5]+1900,$a[4]+1, $a[3], $a[2], $a[1], $a[0]);
}
sub getUpdateTm # ()
{
if ($UPDATE !~ /^(\d+)([hms])?$/) {
$UPDATE = "2h"; # default: 2h
return 2*3600;
}
if (!defined($2) || $2 eq 'm') {
return $1 * 60;
}
elsif ($2 eq 'h') {
return $1 * 3600;
}
else {
return $1 + 0;
}
}
# ret: $thr
sub updateProc # ()
{
use threads;
use threads::shared;
use File::stat;
checkUpdateRepo();
share($g_sig);
my $thr = threads->create(sub {
while (1) {
my $sec = checkUpdateRepo();
sleep($sec);
}
});
$thr->detach();
return $thr;
}
# return: seconds to sleep
sub checkUpdateRepo # ()
{
my $sec = getUpdateTm();
if ($sec == 0) {
return 1;
}
my $diff = time() - stat($g_repo)->mtime;
if ($diff < $sec) {
# print "plan=$sec, diff=$diff, sleep " . ($sec - $diff) . "\n";;
return ($sec - $diff);
}
# scan
system($g_updateCmd);
print "=== Repo is updated.\n";
$g_sig = 'update';
# runClient("q");
return ($sec+1);
}
#}}}
###### main routine
### parse args {{{
my $params = '';
my @argv;
for (@ARGV) {
if (/^:(\d+)/) {
my $p = $1 +0;
if ($p < 1000) {
$INST_NO = $p;
}
else {
$ENV{TCP_PORT} = $p;
}
}
elsif ($_ eq '-c') {
$g_isclient = 1;
}
elsif ($_ eq '-h') {
showHelp();
exit;
}
else {
$params .= $_ . ' ';
push @argv, $_;
}
}
$g_repo = $argv[0] || $DEF_REPO;
chop $params if $params;
if ($g_isclient) {
runClient($params);
exit;
}
if (@argv == 0)
{
unless (-f $DEF_REPO) {
print "!!! no repo file. scan current folder ...\n";
system("$SYMSCAN .");
}
unless (-f $DEF_REPO) {
print "*** cannot find symbol repo: $DEF_REPO.";
exit(-1);
}
$params = $DEF_REPO;
push @argv, $DEF_REPO;
}
for my $repo (@argv) {
unless (-f $repo) {
print "*** cannot find repo file '$repo'\n";
exit;
}
}
#}}}
my $cmd = "$SYMFIND $params";
$g_updateCmd = "$SYMSCAN \"$g_repo\"";
$ENV{SYM_SVR} =1;
my $thrUpdate = updateProc();
# Ctrl-C for graceful quit
$SIG{INT} = sub {
$g_sig = 'quit';
};
again:
use IPC::Open3;
$g_tgtpid = open3(\*MAIN_WR, \*MAIN_RD, \*MAIN_RD, $cmd)
or die("start symfind error: $!\n");
select(MAIN_WR); $| = 1; # make unbuffered
my $comm = CommInet->new();
execCmd(undef, undef); # just process init output
msg ("=== [" . logtm() . "] server is ready. (update=$UPDATE)\n", 1);
exit if !$IS_MSWIN && fork != 0;
$g_sig = '';
while(1)
{
local $_ = $comm->get({timeout=>1});
last if $g_sig;
next if $_ eq '<timeout>';
mychop($_);
if (/\.debug=(\d)/ ) {
$DEBUG = $1;
next;
}
if (/^u$/) {
system($g_updateCmd);
$comm->put("update done.\n");
$g_sig = 'update';
last;
}
my $hideout = 0; # s/^@//;
my $rv = execCmd($comm, $_, $hideout);
last unless defined $rv;
}
select STDOUT;
$comm->destroy();
print MAIN_WR "q\n"; # quit the symfind-process
waitpid($g_tgtpid, 0);
if ($g_sig eq 'update') {
goto again;
}
# vim: set foldmethod=marker :