Debian Codegraph @

Tk/CmdLine.pm

raw
1 package Tk::CmdLine; # -*-Perl-*-
2
3 #/----------------------------------------------------------------------------//
4 #/ Module: Tk/CmdLine.pm
5 #/
6 #/ Purpose:
7 #/
8 #/ Process standard X11 command line options and set initial resources.
9 #/
10 #/ Author: ???? Date: ????
11 #/
12 #/ History: SEE POD
13 #/----------------------------------------------------------------------------//
14
15 use vars qw($VERSION);
16 $VERSION = '4.007'; # $Id: //depot/Tkutf8/Tk/CmdLine.pm#6 $
17
18 use 5.004;
19
20 use strict;
21
22 use Config;
23
24 my $OBJECT = undef; # define the current object
25
26 #/----------------------------------------------------------------------------//
27 #/ Constructor
28 #/ Returns the object reference.
29 #/----------------------------------------------------------------------------//
30
31 sub new # Tk::CmdLine::new()
32 {
33 my $this = shift(@_);
34 my $class = ref($this) || $this;
35
36 my $name = 'pTk';
37 $name = $1 if (($0 =~ m/(?:^|[\/\\])([\w-]+)(?:\.\w+)?$/) && ($1 ne '-e'));
38
39 my $self = {
40 name => $name,
41 config => { -name => $name },
42 options => {},
43 methods => {},
44 command => [],
45 synchronous => 0,
46 iconic => 0,
47 motif => ($Tk::strictMotif || 0),
48 resources => {} };
49
50 return bless($self, $class);
51 }
52
53 #/----------------------------------------------------------------------------//
54 #/ Process the arguments in a given array or in @ARGV.
55 #/ Returns the object reference.
56 #/----------------------------------------------------------------------------//
57
58 sub Argument_ # Tk::CmdLine::Argument_($flag) # private method
59 {
60 my $self = shift(@_);
61 my $flag = shift(@_);
62 unless ($self->{offset} < @{$self->{argv}})
63 {
64 die 'Usage: ', $self->{name}, ' ... ', $flag, " <argument> ...\n";
65 }
66 return splice(@{$self->{argv}}, $self->{offset}, 1);
67 }
68
69 sub Config_ # Tk::CmdLine::Config_($flag, $name) # private method
70 {
71 my $self = shift(@_);
72 my ($flag, $name) = @_;
73 my $val = $self->Argument_($flag);
74 push(@{$self->{command}}, $flag, $val);
75 $self->{config}->{"-$name"} = $val;
76 }
77
78 sub Flag_ # Tk::CmdLine::Flag_($flag, $name) # private method
79 {
80 my $self = shift(@_);
81 my ($flag, $name) = @_;
82 push(@{$self->{command}}, $flag);
83 $self->{$name} = 1;
84 }
85
86 sub Option_ # Tk::CmdLine::Option_($flag, $name) # private method
87 {
88 my $self = shift(@_);
89 my ($flag, $name) = @_;
90 my $val = $self->Argument_($flag);
91 push(@{$self->{command}}, $flag, $val);
92 $self->{options}->{"*$name"} = $val;
93 }
94
95 sub Method_ # Tk::CmdLine::Method_($flag, $name) # private method
96 {
97 my $self = shift(@_);
98 my ($flag, $name) = @_;
99 my $val = $self->Argument_($flag);
100 push(@{$self->{command}}, $flag, $val);
101 $self->{methods}->{$name} = $val;
102 }
103
104 sub Resource_ # Tk::CmdLine::Resource_($flag, $name) # private method
105 {
106 my $self = shift(@_);
107 my ($flag, $name) = @_;
108 my $val = $self->Argument_($flag);
109 if ($val =~ /^([^!:\s]+)*\s*:\s*(.*)$/)
110 {
111 push(@{$self->{command}}, $flag, $val);
112 $self->{options}->{$1} = $2;
113 }
114 }
115
116 my %Method = (
117 background => 'Option_',
118 bg => 'background', # alias
119 class => 'Config_',
120 display => 'screen', # alias
121 fg => 'foreground', # alias
122 fn => 'font', # alias
123 font => 'Option_',
124 foreground => 'Option_',
125 geometry => 'Method_',
126 iconic => 'Flag_',
127 iconposition => 'Method_',
128 motif => 'Flag_',
129 name => 'Config_',
130 screen => 'Config_',
131 synchronous => 'Flag_',
132 title => 'Config_',
133 xrm => 'Resource_'
134 );
135
136 sub SetArguments # Tk::CmdLine::SetArguments([@argument])
137 {
138 my $self = (@_ # define the object as necessary
139 ? ((ref($_[0]) eq __PACKAGE__)
140 ? shift(@_)
141 : (($_[0] eq __PACKAGE__) ? shift(@_) : 1) && ($OBJECT ||= __PACKAGE__->new()))
142 : ($OBJECT ||= __PACKAGE__->new()));
143 $OBJECT = $self; # update the current object
144 $self->{argv} = (@_ ? [ @_ ] : \@ARGV);
145 $self->{offset} = 0; # its existence will denote that this method has been called
146
147 my @option = ();
148
149 while ($self->{offset} < @{$self->{argv}})
150 {
151 last if ($self->{argv}->[$self->{offset}] eq '--');
152 unless (
153 (($self->{argv}->[$self->{offset}] =~ /^-{1,2}(\w+)$/) && (@option = $1)) ||
154 (($self->{argv}->[$self->{offset}] =~ /^--(\w+)=(.*)$/) && (@option = ($1, $2))))
155 {
156 ++$self->{offset};
157 next;
158 }
159
160 next if (!exists($Method{$option[0]}) && ++$self->{offset});
161
162 $option[0] = $Method{$option[0]} if exists($Method{$Method{$option[0]}});
163
164 my $method = $Method{$option[0]};
165
166 if (@option > 1) # replace --<option>=<value> with <value>
167 {
168 $self->{argv}->[$self->{offset}] = $option[1];
169 }
170 else # remove the argument
171 {
172 splice(@{$self->{argv}}, $self->{offset}, 1);
173 }
174
175 $self->$method(('-' . $option[0]), $option[0]);
176 }
177
178 $self->{config}->{-class} ||= ucfirst($self->{config}->{-name});
179
180 delete($self->{argv}); # no longer needed
181
182 return $self;
183 }
184
185 use vars qw(&process); *process = \&SetArguments; # alias to keep old code happy
186
187 #/----------------------------------------------------------------------------//
188 #/ Get a list of the arguments that have been processed by SetArguments().
189 #/ Returns an array.
190 #/----------------------------------------------------------------------------//
191
192 sub GetArguments # Tk::CmdLine::GetArguments()
193 {
194 my $self = (@_ # define the object as necessary
195 ? ((ref($_[0]) eq __PACKAGE__)
196 ? shift(@_)
197 : (($_[0] eq __PACKAGE__) ? shift(@_) : 1) && ($OBJECT ||= __PACKAGE__->new()))
198 : ($OBJECT ||= __PACKAGE__->new()));
199 $OBJECT = $self; # update the current object
200
201 $self->SetArguments() unless exists($self->{offset}); # set arguments if not yet done
202
203 return @{$self->{command}};
204 }
205
206 #/----------------------------------------------------------------------------//
207 #/ Get the value of a configuration option (default: -class).
208 #/ Returns the option value.
209 #/----------------------------------------------------------------------------//
210
211 sub cget # Tk::CmdLine::cget([$option])
212 {
213 my $self = (@_ # define the object as necessary
214 ? ((ref($_[0]) eq __PACKAGE__)
215 ? shift(@_)
216 : (($_[0] eq __PACKAGE__) ? shift(@_) : 1) && ($OBJECT ||= __PACKAGE__->new()))
217 : ($OBJECT ||= __PACKAGE__->new()));
218 $OBJECT = $self; # update the current object
219 my $option = shift(@_) || '-class';
220
221 $self->SetArguments() unless exists($self->{offset}); # set arguments if not yet done
222
223 return (exists($self->{config}->{$option}) ? $self->{config}->{$option} : undef);
224 }
225
226 #/----------------------------------------------------------------------------//
227
228 sub CreateArgs # Tk::CmdLine::CreateArgs()
229 {
230 my $self = (@_ # define the object as necessary
231 ? ((ref($_[0]) eq __PACKAGE__)
232 ? shift(@_)
233 : (($_[0] eq __PACKAGE__) ? shift(@_) : 1) && ($OBJECT ||= __PACKAGE__->new()))
234 : ($OBJECT ||= __PACKAGE__->new()));
235 $OBJECT = $self; # update the current object
236
237 $self->SetArguments() unless exists($self->{offset}); # set arguments if not yet done
238
239 return $self->{config};
240 }
241
242 #/----------------------------------------------------------------------------//
243
244 sub Tk::MainWindow::apply_command_line
245 {
246 my $mw = shift(@_);
247
248 my $self = ($OBJECT ||= __PACKAGE__->new());
249
250 $self->SetArguments() unless exists($self->{offset}); # set arguments if not yet done
251
252 foreach my $priority (keys(%{$self->{resources}}))
253 {
254 foreach my $resource (@{$self->{resources}->{$priority}})
255 {
256 $mw->optionAdd(@{$resource}, $priority);
257 }
258 }
259
260 foreach my $key (keys(%{$self->{options}}))
261 {
262 $mw->optionAdd($key => $self->{options}->{$key}, 'interactive');
263 }
264
265 foreach my $key (keys(%{$self->{methods}}))
266 {
267 $mw->$key($self->{methods}->{$key});
268 }
269
270 if ($self->{methods}->{geometry})
271 {
272 if ($self->{methods}->{geometry} =~ /[+-]\d+[+-]\d+/)
273 {
274 $mw->positionfrom('user');
275 }
276 if ($self->{methods}->{geometry} =~ /\d+x\d+/)
277 {
278 $mw->sizefrom('user');
279 }
280 delete $self->{methods}->{geometry}; # XXX needed?
281 }
282
283 $mw->Synchronize() if $self->{synchronous};
284
285 if ($self->{iconic})
286 {
287 $mw->iconify();
288 $self->{iconic} = 0;
289 }
290
291 $Tk::strictMotif = ($self->{motif} || 0);
292
293 # Both these are needed to reliably save state
294 # but 'hostname' is tricky to do portably.
295 # $mw->client(hostname());
296 $mw->protocol('WM_SAVE_YOURSELF' => ['WMSaveYourself',$mw]);
297 $mw->command([ $self->{name}, @{$self->{command}} ]);
298 }
299
300 #/----------------------------------------------------------------------------//
301 #/ Set the initial resources.
302 #/ Returns the object reference.
303 #/----------------------------------------------------------------------------//
304
305 sub SetResources # Tk::CmdLine::SetResources((\@resource | $resource) [, $priority])
306 {
307 my $self = (@_ # define the object as necessary
308 ? ((ref($_[0]) eq __PACKAGE__)
309 ? shift(@_)
310 : (($_[0] eq __PACKAGE__) ? shift(@_) : 1) && ($OBJECT ||= __PACKAGE__->new()))
311 : ($OBJECT ||= __PACKAGE__->new()));
312 $OBJECT = $self; # update the current object
313
314 $self->SetArguments() unless exists($self->{offset}); # set arguments if not yet done
315 return $self unless @_;
316
317 my $data = shift(@_);
318 my $priority = shift(@_) || 'userDefault';
319
320 $self->{resources}->{$priority} = [] unless exists($self->{resources}->{$priority});
321
322 foreach my $resource ((ref($data) eq 'ARRAY') ? @{$data} : $data)
323 {
324 if (ref($resource) eq 'ARRAY') # resources in [ <pattern>, <value> ] format
325 {
326 push(@{$self->{resources}->{$priority}}, [ @{$resource} ])
327 if (@{$resource} == 2);
328 }
329 else # resources in resource file format
330 {
331 push(@{$self->{resources}->{$priority}}, [ $1, $2 ])
332 if ($resource =~ /^([^!:\s]+)*\s*:\s*(.*)$/);
333 }
334 }
335
336 return $self;
337 }
338
339 #/----------------------------------------------------------------------------//
340 #/ Load initial resources from one or more files (default: $XFILESEARCHPATH with
341 #/ priority 'startupFile' and $XUSERFILESEARCHPATH with priority 'userDefault').
342 #/ Returns the object reference.
343 #/----------------------------------------------------------------------------//
344
345 sub LoadResources # Tk::CmdLine::LoadResources([%options])
346 {
347 my $self = (@_ # define the object as necessary
348 ? ((ref($_[0]) eq __PACKAGE__)
349 ? shift(@_)
350 : (($_[0] eq __PACKAGE__) ? shift(@_) : 1) && ($OBJECT ||= __PACKAGE__->new()))
351 : ($OBJECT ||= __PACKAGE__->new()));
352 $OBJECT = $self; # update the current object
353
354 $self->SetArguments() unless exists($self->{offset}); # set arguments if not yet done
355
356 my %options = @_;
357
358 my @file = ();
359 my $echo = (exists($options{-echo})
360 ? (defined($options{-echo}) ? $options{-echo} : \*STDOUT) : undef);
361
362 unless (%options && (exists($options{-file}) || exists($options{-symbol})))
363 {
364 @file = (
365 { -symbol => 'XFILESEARCHPATH', -priority => 'startupFile' },
366 { -symbol => 'XUSERFILESEARCHPATH', -priority => 'userDefault' } );
367 }
368 else
369 {
370 @file = { %options };
371 }
372
373 my $delimiter = (($^O eq 'MSWin32') ? ';' : ':');
374
375 foreach my $file (@file)
376 {
377 my $fileSpec = $file->{-spec} = undef;
378 if (exists($file->{-symbol}))
379 {
380 my $xpath = undef;
381 if ($file->{-symbol} eq 'XUSERFILESEARCHPATH')
382 {
383 $file->{-priority} ||= 'userDefault';
384 foreach my $symbol (qw(XUSERFILESEARCHPATH XAPPLRESDIR HOME))
385 {
386 last if (exists($ENV{$symbol}) && ($xpath = $ENV{$symbol}));
387 }
388 next unless defined($xpath);
389 }
390 else
391 {
392 $file->{-priority} ||= (($file->{-symbol} eq 'XFILESEARCHPATH')
393 ? 'startupFile' : 'userDefault');
394 next unless (
395 exists($ENV{$file->{-symbol}}) && ($xpath = $ENV{$file->{-symbol}}));
396 }
397
398 unless (exists($self->{translation}))
399 {
400 $self->{translation} = {
401 '%l' => '', # ignored
402 '%C' => '', # ignored
403 '%S' => '', # ignored
404 '%L' => ($ENV{LANG} || 'C'), # language
405 '%T' => 'app-defaults', # type
406 '%N' => $self->{config}->{-class} # filename
407 };
408 }
409
410 my @postfix = map({ $_ . '/' . $self->{config}->{-class} }
411 ('/' . $self->{translation}->{'%L'}), '');
412
413 ITEM: foreach $fileSpec (split($Config{path_sep}, $xpath))
414 {
415 if ($fileSpec =~ s/(%[A-Za-z])/$self->{translation}->{$1}/g) # File Pattern
416 {
417 if (defined($echo) && ($file->{-symbol} ne 'XFILESEARCHPATH'))
418 {
419 print $echo 'Checking ', $fileSpec, "\n";
420 }
421 next unless ((-f $fileSpec) && (-r _) && (-s _));
422 $file->{-spec} = $fileSpec;
423 last;
424 }
425 else # Directory - Check for <Directory>/$LANG/<Class>, <Directory>/<CLASS>
426 {
427 foreach my $postfix (@postfix)
428 {
429 my $fileSpec2 = $fileSpec . $postfix;
430 if (defined($echo) && ($file->{-symbol} ne 'XFILESEARCHPATH'))
431 {
432 print $echo 'Checking ', $fileSpec2, "\n";
433 }
434 next unless ((-f $fileSpec2) && (-r _) && (-s _));
435 $file->{-spec} = $fileSpec2;
436 last ITEM;
437 }
438 }
439 }
440 }
441 elsif (exists($file->{-file}) && ($fileSpec = $file->{-file}))
442 {
443 print $echo 'Checking ', $fileSpec, "\n" if defined($echo);
444 next unless ((-f $fileSpec) && (-r _) && (-s _));
445 $file->{-spec} = $fileSpec;
446 }
447 }
448
449 foreach my $file (@file)
450 {
451 next unless defined($file->{-spec});
452 local *SPEC;
453 next unless open(SPEC,$file->{-spec});
454 print $echo ' Loading ', $file->{-spec}, "\n" if defined($echo);
455
456 my $resource = undef;
457 my @resource = ();
458 my $continuation = 0;
459
460 while (defined(my $line = <SPEC>))
461 {
462 chomp($line);
463 next if ($line =~ /^\s*$/); # skip blank lines
464 next if ($line =~ /^\s*!/); # skip comments
465 $continuation = ($line =~ s/\s*\\$/ /); # search for trailing backslash
466 unless (defined($resource)) # it is the first line
467 {
468 $resource = $line;
469 }
470 else # it is a continuation line
471 {
472 $line =~ s/^\s*//; # remove leading whitespace
473 $resource .= $line;
474 }
475 next if $continuation;
476 push(@resource, [ $1, $2 ]) if ($resource =~ /^([^:\s]+)*\s*:\s*(.*)$/);
477 $resource = undef;
478 }
479
480 close(SPEC);
481
482 if (defined($resource)) # special case - EOF after line with trailing backslash
483 {
484 push(@resource, [ $1, $2 ]) if ($resource =~ /^([^:\s]+)*\s*:\s*(.*)$/);
485 }
486
487 $self->SetResources(\@resource, $file->{-priority}) if @resource;
488 }
489
490 return $self;
491 }
492
493 #/----------------------------------------------------------------------------//
494
495 1;
496
497 __END__