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
|
#-*- perl -*-
#
# Copyright (C) 2001,2002 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Switch.pm,v 1.53 2002/02/02 15:24:18 fukachan Exp $
#
package FML::Process::Switch;
use strict;
use Carp;
use vars qw($debug);
use File::Spec;
=head1 NAME
FML::Process::Switch - dispatch the suitable module
=head1 SYNOPSIS
used in C<libexec/loader>.
package main;
use FML::Process::Switch;
&Bootstrap2($main_cf_file); # main::Bootstrap2()
=head1 DESCRIPTION
C<libexec/loader> (C<libexec/fml/loader>), the wrapper, loads this
program and calls C<Bootstrap2()>.
C<Bootstrap2()> loads main.cf,
analyzes the command arguments
and call C<ProcessSwitch()> finally.
C<ProcessSwitch()> emulates "use $package" to load
module suitable with the arguments.
The fml flow bifurcates here through C<ProcessSwitch()>.
The flow details of program exists in FML::Process:: class.
For example, libexec/distribute (fml.pl) runs in this way.
functions class
----------------------------------------
main::Bootstrap() libexec/loader
|
V
main::Bootstrap2() Process::Switch
|
V
ProcessSwitch() Process::Switch
|
V
FML::Process:Distribute FML::Process::Distribute
=head1 FUNCTIONS
=head2 C<main::Bootstrap2()>
kick off the second phase of bootstrap.
It reads *.cf files, parses them and set the result to C<@cf>
array variable.
We pass it to C<ProcessSwitch()> later.
@cf = (
/etc/fml/defaults/$VERSION/default_config.cf
/etc/fml/site_default_config.cf (required ?)
/etc/fml/domains/$DOMAIN/default_config.cf
/var/spool/ml/elena/config.cf
);
=cut
# Descriptions: the second phase of bootstrap
# Arguments: STR($main_cf_file) HASH_REF($main_cf)
# Side Effects: none
# Return Value: same as FML::Process::Flow::ProcessStart()
sub main::Bootstrap2
{
my ($main_cf_file, $main_cf) = @_;
my @argv = @ARGV; # save the original argument vector
my @cf = ();
my %options = ();
my $ml_home_dir = ''; # e.g. /var/spool/ml/elena
use File::Basename;
my $myname = basename($0); # inspect my name from $0
# 0.1
print STDERR "\nsetuid is not set $< != $>\n\n" if $< != $>;
print STDERR "\nsetgid is not set $( != $)\n\n" if $( ne $);
# 0.2
if ($0 =~ /loader/) {
print "ARGV: @ARGV\n";
}
# 1.1 parse command line options (preliminary)
{
my (@options) = _module_specific_options($myname);
if (@options) {
eval q{
use Getopt::Long;
GetOptions(\%options, _module_specific_options($myname));
};
croak($@) if $@;
}
}
# 2.1 analyze main.cf and get the result in $main_cf
# removed.
# 2.2 parse @ARGV and get a list of configuration files
# XXX $main_cf{ ml_home_dir } (e.g. /var/spool/ml/elena) is defined
# if possible.
# a) resolve $ml_name (e.g. expand "elena" to "/var/spool/ml/elena")
# b) but we need special treatment in some cases e.g. makefml
my $cf = _parse_argv($myname, $main_cf);
# 2.3 prepare @$cf
# XXX hmm, .. '/etc/fml/site_default_config.cf' is good ???
my $sitedef =
File::Spec->catfile($main_cf->{ config_dir }, 'site_default_config.cf');
unshift(@$cf, $sitedef);
unshift(@$cf, $main_cf->{ default_config_cf });
# 3.1 set up @INC
unshift(@INC, split(/\s+/, $main_cf->{ lib_dir }));
unshift(@INC, split(/\s+/, $main_cf->{ local_lib_dir }));
# 3.2 useful for e.g. "fmldoc"
$ENV{'PERL5LIB'} = $main_cf->{ lib_dir };
# 4. debug
if ($0 =~ /loader/) {
eval q{
require Data::Dumper; Data::Dumper->import();
$Data::Dumper::Varname = 'main_cf';
print Dumper( $main_cf );
sleep 3;
};
if ($@) { print STDERR $@;}
}
# 5. o.k. here we go!
use FML::Process::Utils;
my $ml_home_prefix = FML::Process::Utils::__ml_home_prefix_from_main_cf($main_cf);
my $args = {
fml_version => $main_cf->{ fml_version },
myname => $myname,
program_name => $myname,
ml_home_prefix => $ml_home_prefix,
ml_home_dir => $main_cf->{ ml_home_dir },
cf_list => $cf,
options => \%options,
argv => \@argv, # pass the original @ARGV
ARGV => \@ARGV, # @ARGV after getopts()
main_cf => $main_cf,
# options
need_ml_name => _ml_name_is_required($myname),
};
# get the object. The suitable module is speculcated by $0.
my $obj = ProcessSwitch($args);
# start the process.
eval q{
FML::Process::Flow::ProcessStart($obj, $args);
};
if ($@) {
my $reason = $@;
if ($obj->can('help')) { eval $obj->help();};
if (defined( $main_cf->{ debug } ) ||
defined $options{debug}) {
croak($reason);
}
else {
$reason =~ s/[\n\s]*\s+at\s+.*$//m;
croak($reason);
}
}
}
# Descriptions: analyze argument vector
# Arguments: STR($myname) HASH_REF($main_cf)
# Side Effects: none
# Return Value: HASH_ARRAY (list of config.cf's)
sub _parse_argv
{
my ($myname, $main_cf) = @_;
if ($myname eq 'makefml') {
_makefml_parse_argv($myname, $main_cf);
}
else {
_usual_parse_argv($myname, $main_cf);
}
}
# Descriptions: analyze argument vector
# Arguments: STR($myname) HASH_REF($main_cf)
# Side Effects: none
# Return Value: HASH_ARRAY (list of config.cf's)
sub _usual_parse_argv
{
my ($myname, $main_cf) = @_;
use FML::Process::Utils;
my $ml_home_prefix = FML::Process::Utils::__ml_home_prefix_from_main_cf($main_cf);
my $ml_home_dir = '';
my $found_cf = 0;
my @cf = ();
# "elena" is translated to "/var/spool/ml/elena"
for (@ARGV) {
# 1. for the first time
# a) speculate "/var/spool/ml/$_" looks a $ml_home_dir ?
unless ($found_cf) {
my $x = File::Spec->catfile($ml_home_prefix, $_);
my $cf = File::Spec->catfile($x, "config.cf");
if (-d $x && -f $cf) {
$found_cf = 1;
$ml_home_dir = $x;
push(@cf, $cf);
}
}
# 2. /var/spool/ml/elena looks a $ml_home_dir ?
if (-d $_) {
$ml_home_dir = $_;
my $cf = File::Spec->catfile($_, "config.cf");
if (-f $cf) {
push(@cf, $cf);
$found_cf = 1;
}
}
# 3. looks a file, so /var/spool/ml/elena/config.cf ?
elsif (-f $_) {
push(@cf, $_);
}
}
# save $ml_home_dir value in $main_cf directly
$main_cf->{ ml_home_dir } = $ml_home_dir;
\@cf;
}
# Descriptions: analyze argument vector
# Arguments: STR($myname) HASH_REF($main_cf)
# Side Effects: none
# Return Value: HASH_ARRAY (list of config.cf's)
sub _makefml_parse_argv
{
my ($myname, $main_cf) = @_;
use FML::Process::Utils;
my $ml_home_prefix = FML::Process::Utils::__ml_home_prefix_from_main_cf($main_cf);
# makefml specific syntax.
if (@ARGV) {
my ($command, $ml_name, @options) = @ARGV;
if ($command =~ /\-\>/) {
($command, @options) = @ARGV;
($ml_name, $command) = split('->', $command);
}
elsif ($command =~ /::/) {
($command, @options) = @ARGV;
($ml_name, $command) = split('::', $command);
}
# save $ml_home_dir value in $main_cf directly
$main_cf->{ ml_home_dir } = File::Spec->catfile($ml_home_prefix, $ml_name);
# config.cf
my $cf = File::Spec->catfile($ml_home_prefix, $ml_name, "config.cf");
my @cf = ($cf);
return \@cf;
}
else {
return [];
}
}
=head2 C<ProcessSwitch($args)>
load the library and prepare environment to use it.
C<ProcessSwitch($args)> return process object C<$obj>.
To start the process, we pass C<$obj> with C<$args> to
C<FML::Process::Flow::ProcessStart($obj, $args)>.
C<$args> is like this:
my $args = {
fml_version => $main_cf->{ fml_version },
myname => $myname,
ml_home_prefix => $main_cf->{ ml_home_prefix },
ml_home_dir => $main_cf->{ ml_home_dir },
cf_list => $cf,
options => \%options,
argv => \@argv, # pass the original @ARGV
ARGV => \@ARGV, # @ARGV after getopts()
main_cf => $main_cf,
# options
need_ml_name => _ml_name_is_required($myname),
};
# get the object. The suitable module is speculcated by $0.
my $obj = ProcessSwitch($args);
# start the process.
FML::Process::Flow::ProcessStart($obj, $args);
=cut
# Descriptions: top level process switch
# emulates "use $package" but $package is dynamically
# determined by e.g. $0.
# Arguments: HASH_REF($args)
# Side Effects: process switching :-)
# ProcessSwtich() is exported to main:: Name Space.
# Return Value: STR(package name)
sub ProcessSwitch
{
my ($args) = @_;
# Firstly, create process
# $pkg is a package name, for exampl,e "FML::Process::Distribute".
my $pkg = _module_we_use($args);
unless (defined $pkg) {
croak("$args->{ myname } is unknown program\n");
}
# debug, ignore this
if ($0 =~ /loader/) { print "use $pkg\n"; sleep 2;}
eval qq{ require $pkg; $pkg->import();};
croak($@) if $@;
return $pkg;
}
# Descriptions: return the suitable getopt options
# Arguments: STR($myname)
# Side Effects: none
# Return Value: ARRAY (getopt parameters)
sub _module_specific_options
{
my ($myname) = @_;
# XXX Caution!
# XXX xxx.cgi SHOULD NOT ACCPET the same options as command line
# XXX program xxx does.
if ($myname eq 'fml.pl' ||
$myname eq 'distribute' ||
$myname eq 'command' ||
$myname eq 'loader' ) {
return qw(ctladdr! debug! help! params=s -c=s);
}
elsif ($myname eq 'fmlthread') {
return qw(debug! help!
article_id_max=i
spool_dir=s
base_url=s
msg_base_url=s
reverse!
params=s -f=s -c=s);
}
elsif($myname eq 'thread.cgi' ||
$myname eq 'fmlthread.cgi' ||
$myname eq 'threadview.cgi') {
return ();
}
elsif ($myname eq 'fmlconf') {
return qw(debug! help! params=s -c=s n!);
}
elsif ($myname eq 'fmldoc') {
# perldoc [-h] [-v] [-t] [-u] [-m] [-l]
return qw(debug! help! params=s -c=s v! t! u! m! l!);
}
elsif ($myname eq 'makefml') {
return qw(debug! help! params=s -c=s);
}
elsif ($myname eq 'makefml.cgi') {
return ();
}
elsif ($myname eq 'fmlsch') {
return qw(debug! help! -D=s -F=s -m=s a! h!);
}
elsif ($myname eq 'fmlsch.cgi') {
return ();
}
elsif ($myname eq 'fmlhtmlify') {
return qw(debug! help! -I=s);
}
else {
croak "no such program $myname.\n";
}
}
# Descriptions: this program ($0) requires ML name always or not?
# Arguments: STR($myname)
# Side Effects: none
# Return Value: 1 (require ml name always) or 0
sub _ml_name_is_required
{
my ($myname) = @_;
if ($myname eq 'fmldoc') {
return 0;
}
elsif ($myname eq 'fmlsch' || $myname eq 'fmlsch.cgi') {
return 0;
}
elsif ($myname eq 'makefml') {
return 0;
}
else {
return 1;
}
}
# Descriptions: determine package we need and require() it if needed.
# Arguments: HASH_REF($args)
# Side Effects: none
# Return Value: STR(FML::Process::SOMETHING module name)
sub _module_we_use
{
my ($args) = @_;
my $name = $args->{ myname };
my $pkg = '';
if (($name eq 'fml.pl' && $args->{ options }->{ ctladdr }) ||
$name eq 'command' ||
($name eq 'loader' && $args->{ options }->{ ctladdr })) {
$pkg = 'FML::Process::Command';
}
elsif ($name eq 'fml.pl' || $name eq 'distribute' || $name eq 'loader') {
$pkg = 'FML::Process::Distribute';
}
elsif ($name eq 'fmlserv') {
$pkg = 'FML::Process::ListServer';
}
elsif ($name eq 'fmldoc') {
$pkg = 'FML::Process::DocViewer';
}
elsif ($name eq 'fmlconf') {
$pkg = 'FML::Process::ConfViewer';
}
elsif ($name eq 'makefml') {
$pkg = 'FML::Process::Configure';
}
elsif ($name eq 'fmlthread') {
$pkg = 'FML::Process::ThreadTrack';
}
elsif ($name eq 'fmlthread.cgi' ||
$name eq 'thread.cgi' ||
$name eq 'threadview.cgi') {
$pkg = 'FML::CGI::ThreadTrack';
}
elsif ($name eq 'mead') {
$pkg = 'FML::Process::MailErrorAnalyzer';
}
elsif ($name eq 'qmail-ext') {
$pkg = 'FML::Process::QMail';
}
elsif ($name eq 'makefml.cgi') {
$pkg = 'FML::CGI::Configure';
}
elsif ($name eq 'fmlsch') {
$pkg = 'FML::Process::Calender';
}
elsif ($name eq 'fmlsch.cgi') {
$pkg = 'FML::CGI::Calender';
}
elsif ($name eq 'fmlhtmlify') {
$pkg = 'FML::Process::HTMLify';
}
else {
return '';
}
print STDERR "module = $pkg (for $0)\n" if $debug;
return $pkg;
}
=head1 SEE ALSO
L<FML::Process::Distribute>,
L<FML::Process::Command>,
L<FML::Process::ListServer>,
L<FML::Process::Configure>,
L<FML::Process::ThreadTrack>,
L<FML::Process::MailErrorAnalyzer>
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2001,2002 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
FML::Process::Switch appeared in fml5 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|