4 our $us = $0; $us =~ s#.*/##;
7 $us [<opts>] update <number>|<localpart>@[domain] [<reason>] [<comment>]
8 $us [<opts>] assign <number>|<localpart>@[domain] <comment>
10 $us [<opts>] some <count> } will generate aliases
11 $us [<opts>] range <min>-<max>|<min>+<count> } as necessary
13 -G<generator> generator/lister program
14 -N do not generate more aliases
15 -l.., -d.., -F.., -m.. passed to generator
17 Always use $us to edit the comment of a pregen alias;
18 otherwise we may lose track of the next number alias to pregenerate
20 <comment> must always start with #, and you will need to quote the
21 whole comment to protect it from your shell
24 sub fail { die "$us: @_\n"; }
25 sub badusage { fail "bad usage: @_"; }
26 sub failstdout () { fail "stdout: $!"; }
36 our $comment_pattern = '### PREGEN <number>';
39 sub comment_make ($) {
41 my $r = $comment_pattern;
42 ($r =~ s/\<number\>/$num/) == 1 or confess "$r ?";
48 open P, "-!", $generator, @genargs or fail "fork $generator: $!";
53 } elsif (m/^\# reject (\S+) (\#.*)$/) {
54 ($alias,$comment) = ($1,$2);
56 next; # future extension
57 } elsif (m/^([^#: ])\: [^#]* (\#.*)$/) {
58 ($alias,$comment) = ($1,$2);
60 die "generator output $_ ?";
62 my $localpart = $alias;
63 $localpart =~ s/\@.*//;
64 my $row = { Alias => $alias, LocalPart => $localpart };
65 if ($comment =~ m/^$comment_re$/o) {
68 $by_localpart{$row->{LocalPart}} = $row;
69 if (defined $row->{Number}) {
70 $by_number[$row->{Number}] = $row;
71 $min_number = $row->{Number} if
72 (!defined $min_number) || $min_number > $row->{Number};
74 $?=0; $!=0; close P or fail "$generator $genargs[0] failed ($! $?)\n";
78 run_generator qw(list);
81 sub perhaps_generate ($) {
83 return undef if $no_generate;
84 my $alias = $by_number[$num];
85 return $alias if $alias;
86 return undef unless $num > $#by_number;
87 run_generator qw(create), (comment_make $num);
88 $alias = $by_number[$num];
89 confess "$num ?" unless $alias;
94 @ARGV or badusage "missing specification for alias to change";
95 my $spec = shift @ARGV;
97 if ($spec =~ m/^(\d{1,9})$/) {
98 $target = $by_number[$spec];
99 fail "no alias number $target" unless $target;
100 } elsif ($spec =~ m/^(.*)\@[^\@]*/) {
101 $target = $by_localpart{$1};
102 fail "no alias with local part \`$1'" unless $target;
103 fail "incorrect or unchecked domain: \`$target->{Alias}' != \`$spec'"
104 unless $spec eq $target->{Alias} ||
105 $spec eq $target->{LocalPart}.'@';
107 badusage "bad specification for alias to change";
113 confess unless $alias;
114 print $alias->{Number}, ' ', $alias->{LocalPart}, "\n"
119 my $target = parse_target;
120 @ARGV or badusage "missing update info\n";
121 if (defined $target->{Number} && $target->{Number} == $#by_number) {
122 my $wanted = $#by_number + 1;
123 perhaps_generate $wanted
124 or print STDERR <<END or fail "stderr: $!"
125 $us: Losing track of next number ($wanted), due to use of -N;
126 $us: will start next alias at #0, unless you say (e.g.) "range $wanted".
129 exec $generator, qw(update), $target->{Alias}, @ARGV;
130 fail "exec $generator: $!";
135 $ARGV[1] =~ m/^#/) or
136 badusage "invalid arguments to assign - forgot to quote it properly?";
142 badusage "invalid arguments to list";
143 my $num = $min_number;
144 while ($num <= $#by_number) {
145 my $alias = $by_number[$num];
146 report $alias if $alias;
152 $ARGV[0] =~ m/^(\d+)$/s) or
153 badusage "invalid arguments to some";
155 my $num = $min_number;
157 my $alias = generate $num;
162 last if $num > $#by_number; # -N
170 $ARGV[0] =~ m/^(\d+)(?:([-+])(\d+))?$/s) or
171 badusage "invalid arguments to range";
172 my ($num,$op,$rarg) = ($1,$2,$3);
174 $op eq '+' ? $num+$rarg-1 :
177 for (; $num<=$limit; $num++) {
178 my $alias = generate $num;
179 report $alias if $alias;
185 last unless $ARGV[0] =~ m/^-/;
186 my $arg = shift @ARGV;
187 last if $arg =~ m/^--?$/;
188 while ($arg !~ m/^-$/) {
189 if ($arg =~ s/^-[ldFm]/-/) {
191 } elsif ($arg =~ s/^-G//) {
193 } elsif ($arg =~ s/^-N//) {
196 badusage "unknown option \`$arg'";
201 if (!defined $generator) {
203 $generator =~ s/-pregen$//
204 or fail "unable to calculate default generator".
205 " (want to strip \`-pregen' from our program name, \`$us')";
208 $comment_pattern =~ m/^#/s
209 or badusage "comment pattern must start with \`#'";
210 $comment_re = $comment_pattern;
211 $comment_re =~ s/\W/\\$&/g;
212 ($comment_re =~ s'\<number\>'([1-9]\d{0,8})'g) == 1 #'
213 or badusage "comment pattern must contain \`<number>' exactly once";
215 @ARGV or badusage "missing action\n";
216 my $action = shift @ARGV;
221 my $actionsub = { no strict qw(refs); \&{"action_$action"}; }
222 defined $actionsub or badusage "unknown action $action";
225 close STDOUT or failstdout;