Debian Codegraph @

examples/apt-config

raw
1 #!/usr/bin/perl
2
3 #
4 # Example: apt-config in perl. See apt-config(8).
5 #
6
7 use AptPkg::Config '$_config';
8
9 (my $self = $0) =~ s#.*/##;
10
11 # initialise the global config object with the default values
12 $_config->init;
13
14 # parse the command line
15 @ARGV = $_config->parse_cmdline([
16 [ 'h', 'help', 'help' ],
17 [ 'v', 'version', 'version' ],
18 [ 'c', 'config-file', '', 'ConfigFile' ],
19 [ 'o', 'option', '', 'ArbItem' ],
20 ], @ARGV);
21
22 if (my $v_opt = $_config->get_bool('version') or # --version
23 $_config->get_bool('help') or # --help
24 !@ARGV) # no args
25 {
26 # we don't have the PACKAGE/VERSION &c apt-config has... punt
27 print "$self (using AptPkg $AptPkg::VERSION, built for ",
28 "$_config->{APT::Architecture})\n";
29
30 print <<EOT unless $v_opt;
31 Usage: $self [options] command
32
33 $self is a simple tool to read the APT config file
34
35 Commands:
36 shell - Shell mode
37 dump - Show the configuration
38
39 Options:
40 -h This help text.
41 -c=? Read this configuration file
42 -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp
43 EOT
44
45 exit;
46 }
47
48 # dispatch args
49 my $op = shift;
50
51 if ($op eq 'dump')
52 {
53 $_config->dump;
54 exit;
55 }
56
57 die "$self: invalid operation `$op'\n" unless $op eq 'shell';
58
59 while (@ARGV)
60 {
61 my $var = shift;
62 my $cname = shift or die "$self: arguments not in pairs\n";
63 local $_;
64
65 # check if the caller has requested a directory path in the old format
66 $cname .= 'd' if $cname =~ m#/$#;
67
68 $_ = $_config->get($cname) if $_config->exists($cname);
69
70 next unless defined;
71 if (m#[^\w@+,./:=-]#)
72 {
73 # quote
74 s/'/'\\''/g;
75 $_ = "'$_" unless s/^'//;
76 $_ = "$_'" unless s/'$//;
77 }
78
79 print "$var=$_\n";
80 }