3 our $us = $0; $us =~ s#.*/##;
6 $us [<opts>] update <number>|<localpart>@[domain] [<reason>] [<comment>]
7 $us [<opts>] assign <number>|<localpart>@[domain] <comment>
8 $us [<opts>] show <count> } will generate more aliases
9 $us [<opts>] range [<from>-[<to>]] } if necessary
11 -G<generator> generator/lister program
12 -N do not generate more aliases
13 -l.., -d.., -F.., -m.. passed to generator
15 Always use $us to edit the comment of a pregen alias;
16 otherwise we may lose track of the next number alias to pregenerate
18 <comment> must always start with #, and you will need to quote the
19 whole comment to protect it from your shell
29 our $comment_pattern = '### PREGEN <number>';
32 sub comment_make ($) {
34 my $r = $comment_pattern;
35 ($r =~ s/\<number\>/$num/) == 1 or die "$r ?";
40 open P, "-!", $generator, qw(list) or die $!;
45 } elsif (m/^\# reject (\S+) (\#.*)$/) {
46 ($alias,$comment) = ($1,$2);
48 next; # future extension
49 } elsif (m/^([^#: ])\: [^#]* (\#.*)$/) {
50 ($alias,$comment) = ($1,$2);
52 die "generator output $_ ?";
54 my $localpart = $alias;
55 $localpart =~ s/\@.*//;
56 my $row = { Alias => $alias, LocalPart => $localpart };
57 if ($comment =~ m/^$comment_re$/o) {
60 $by_localpart{$row->{LocalPart}} = $row;
61 $by_number[$row->{Number}] = $row if defined $row->{Number};
63 $?=0; $!=0; close P or die "$generator $! $?";
68 return undef if $no_generate;
69 my $alias = $by_number[$num];
70 return $alias if $alias;
71 run_generator qw(create)
72 $!=0; $?=0; (system $generator, qw(create), comment_make $num)
73 ==0 or die "$generator failed ($! $?)";
78 @ARGV or badusage "missing specification for alias to change";
79 my $spec = shift @ARGV;
81 if ($spec =~ m/^(\d{1,9})$/) {
82 $target = $by_number[$spec];
83 die "no alias number $target\n" unless $target;
84 } elsif ($spec =~ m/^(.*)\@[^\@]*/) {
85 $target = $by_localpart{$1};
86 die "no alias with local part \`$1'\n" unless $target;
87 die "incorrect or unchecked domain: \`$target->{Alias}' != \`$spec'\n"
88 unless $spec eq $target->{Alias} ||
89 $spec eq $target->{LocalPart}.'@';
91 badusage "bad specification for alias to change";
96 my $target = parse_target;
97 @ARGV or badusage "missing update info\n";
98 if (defined $target->{Number} && $target->{Number} == $#by_number) {
99 my $wanted = $target->{Number}+1;
101 or print STDERR <<END
102 $us: Losing track of next number ($wanted), due to use of -N;
103 $us: will start next alias at #0, unless you say (e.g.) "range $wanted".
106 exec $generator, qw(update), $target->{Alias}, @ARGV;
107 die "$generator: $!";
112 $ARGV[1] =~ m/^#/) or
113 badusage "invalid arguments to assign - forgot to quote it properly?";
119 last unless $ARGV[0] =~ m/^-/;
120 my $arg = shift @ARGV;
121 last if $arg =~ m/^--?$/;
122 while ($arg !~ m/^-$/) {
123 if ($arg =~ s/^-[ldFm]/-/) {
125 } elsif ($arg =~ s/^-G//) {
127 } elsif ($arg =~ s/^-N//) {
130 badusage "unknown option \`$arg'";
136 $child =~ s/-pregen/; fixme fixme this should be defaulting generator
138 $comment_pattern =~ m/^#/s
139 or badusage "comment pattern must start with \`#'";
140 $comment_re = $comment_pattern;
141 $comment_re =~ s/\W/\\$&/g;
142 ($comment_re =~ s'\<number\>'([1-9]\d{0,8})'g) == 1 #'
143 or badusage "comment pattern must contain \`<number>' exactly once";
145 @ARGV or badusage "missing action\n";
146 my $action = shift @ARGV;
148 { no strict qw(refs); &{"action_$action"}; }