-
Notifications
You must be signed in to change notification settings - Fork 0
/
address
executable file
·334 lines (303 loc) · 9.88 KB
/
address
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
#!/usr/local/bin/perl5
$HOME=$ENV{HOME};
$CONFIGFILE="/home/red/addressbook/addressbook.config";
$MYCONFIGFILE="~/.addressbook.config";
$LIBDIR="/home/red/addressbook";
#
#########################################################################
# #
# address - fast textonly search in addressbook database #
# #
# This is part of my adressbuch / addressbook program #
# Version 0.7, 02.11.1997 #
# Copyright (C) 1995, 1996, 1997 Clemens Durka #
# #
# Clemens Durka ([email protected]) #
# http://home.pages.de/~clemens/ #
# #
#########################################################################
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License Version 2 as #
# published by the Free Software Foundation. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #
# #
#########################################################################
$COUNTRYFILE="$LIBDIR/countries";
if (!@ARGV) {
print "$0: Fast search in addressbookdatabase\n";
print "usage: $0 [-hsl] [-f field] [-n fieldnr]\n";
print " [-a addressfile] [-c configfile] [-ld libdir]\n";
print " [-debug level] substring_of_a_name\n";
print "Try `$0 -h' for more information.\n";
exit 1;
}
$long=-1;
$adrfile="";
$a=$ENV{ADDRBOOK_ADDRFILE};
if ($a) {
$adrfile=$a;
}
$MYCONFIGFILE =~ s/~/$HOME/;
$_="$0";
SWITCH: {
/phone/ && do { $field="special",$action="tel", last SWITCH; };
/tel/ && do { $field="special",$action="tel", last SWITCH; };
/fax/ && do { $field="special",$action="fax", last SWITCH; };
/birthday/ && do { $field="birthday", last SWITCH; };
/email/ && do { $field="email", last SWITCH; };
/letter/ && do { $field="special",$action="letter", last SWITCH; };
}
while (@ARGV) {
$i=shift @ARGV;
if (($i eq "-h") || ($i eq "--help")) {
print "$0: Fast search in addressbookdatabase\n";
print "This programm looks the addressfile defined in your personal or global\n";
print "configfile for matching and outputs the corresponding fields depending\n";
print "on how it was called:\n";
print "\n";
print "Possible names for calling it:\n";
print "phone prints all known phone numbers of a person\n";
print "fax same with all fax numbers \n";
print "email prints the email address\n";
print "birthday prints the birthday\n";
print "letter prints the address as you would write it on a letter\n";
print "address gives all information without formatting\n";
print "\n";
print "You can use the following options:\n";
print "-l long form: name, city and the result (default)\n";
print "-s short form: only the result of the query\n";
print "-f field fieldname for the output (p.ex.: company, www)\n";
print "-n fieldnr number of the field for output (faster)\n";
print "-a addressfile addressfile to search in";
print "-c configfile configfile to read\n";
print "-ld libdir libdir\n";
print "-db level print debugging output (level: 1 - 4)\n";
exit;
} elsif ($i eq "-s") {
$long = 0;
} elsif ($i eq "-l") {
$long = 1;
} elsif ($i eq "-f") {
$field = shift @ARGV;
} elsif ($i eq "-n") {
$fieldnr = shift @ARGV;
} elsif ($i eq "-a") {
$adrfile = shift @ARGV;
} elsif ($i eq "-c") {
$MYCONFIGFILE=shift @ARGV;
} elsif ($i eq "-ld") {
$t = shift @ARGV;
$COUNTRYFILE="$t/countries";
} elsif ($i eq "-db") {
$debug=shift @ARGV;
} elsif ($i eq "-debug") {
$debug=shift @ARGV;
} else {
$searchterm = $i;
}
}
if ($debug > 2) {
print "Long: $long, Field: $field, Fieldnr: $fieldnr, Action: $action\n";
print "Searchterm: $searchterm\n";
print "Adrfile: $adrfile\n";
print "Configfile: $MYCONFIGFILE\n";
print "Countryfile: $COUNTRYFILE\n";
}
if ($adrfile eq "") {
# Load configfile
if (! open (CFG, "<$CONFIGFILE")) {
; # warn ("File $CONFIGFILE could not be opened: $!.\n");
} else {
if ($debug > 0) { print "Reading configfile: $CONFIGFILE\n";}
while (<CFG>) {
if (/^([^# \n][\S]*)\s*(.*)/) {
$options{$1}="$2";
}
}
}
close CFG;
# Load private configfile
if (! open (CFG, "<$MYCONFIGFILE")) {
; # warn ("File $MYCONFIGFILE could not be opened: $!.\n");
} else {
if ($debug > 0) { print "Reading configfile: $MYCONFIGFILE\n";}
while (<CFG>) {
if ( /^([^# \n][\S]*)\s*(.*)/ ) {
$options{$1}="$2";
}
}
}
close CFG;
if ($options{adrfile1} ne "") {
$adrfile=$options{adrfile1};
} else {
$adrfile=$options{adrfile};
}
}
$adrfile =~ s/~/$HOME/;
$fields{special} = -1;
#if ($fieldnr eq "") {
# Load dataformatfile
$adrfmt="$adrfile" . ".fmt";
open (FMT, "<$adrfmt") ||
die ("File $adrfmt could not be opened: $!.\n");
if ($debug > 0) { print "Reading formatfile: $adrfmt\n";}
while (<FMT>) {
if (/^([^# \n][\S]*)\s*(.*)/) {
$fields{$2}="$1";
$fields{$1}="$2";
}
}
close FMT;
if ($field ne "special") {
if (($field ne "") && ($fields{$field}>=0)) {
$fieldnr=$fields{$field};
} elsif ($fieldnr eq "") {
# Default address: give out all info
$fieldnr=-1;
$field="special";
$action="all";
$long=0;
}
}
$birthfld=$fields{birthday};
#}
if ($debug > 2) {
print "Long: $long, Field: $field, Fieldnr: $fieldnr, Action: $action\n";
}
if (($field eq "birthday") || ($action eq "letter")) {
# Load countryfile
if (! open (COU, "<$COUNTRYFILE")) {
; # warn ("File $COUNTRYFILE could not be opened: $!.\n");
} else {
if ($debug > 0) { print "Reading countryfile: $COUNTRYFILE\n";}
while (<COU>) {
chop; # deletes LF at the end
if (/^[^#]/) {
@line = split(/;/);
$intprefix{$line[0]} = $line[2];
$fullname{$line[0]} = $line[7];
$format{$line[0]} = $line[5];
}
}
}
close COU;
}
# Load addressfile and perform action
open (ADR, "<$adrfile") ||
die ("File $adrfile could not be opened: $!.\n");
if ($debug > 0) { print "Reading addressfile: $adrfile\n";}
while (<ADR>) {
if (/$searchterm/i) {
@line = split(/$fields{separatorchar}/);
if ($debug > 3) { print "Read line: @line\n"; }
if (($line[$fields{firstname}] !~ /$searchterm/i) && ($line[$fields{lastname}] !~ /$searchterm/i)) {next;}
if (($field eq "birthday") || ($action eq "letter")) {
$coun = $line[$fields{country}];
if ($coun eq "") { $coun = $options{mycountry} };
}
if (($action ne "letter") && ($long != 0)) {
print "$line[$fields{firstname}] $line[$fields{lastname}], $line[$fields{city}]:\t";
}
if ($field ne "special") {
# Normal Action
print "$line[$fieldnr]\n";
} else {
# Special Action
if ($action eq "tel") {
if (($fields{phone}>0) && ($line[$fields{phone}] ne "")) {
print "$line[$fields{phone}] ";
}
if (($fields{phonepriv}>0) && ($line[$fields{phonepriv}] ne "")) {
print "$line[$fields{phonepriv}] (private) ";
}
if (($fields{phonework}>0) && ($line[$fields{phonework}] ne "")) {
print "$line[$fields{phonework}] (work) ";
}
if (($fields{phonesecretary}>0) && ($line[$fields{phonesecretary}] ne "")) {
print "$line[$fields{phonesecretary}] (secretary) ";
}
if (($fields{phonemobile}>0) && ($line[$fields{phonemobile}] ne "")) {
print "$line[$fields{phonemobile}] (mobile) ";
}
if (($fields{phonepager}>0) && ($line[$fields{phonepager}] ne "")) {
print "$line[$fields{phonepager}] (pager) ";
}
print "\n";
} elsif ($action eq "fax") {
print "Fax: ";
if (($fields{fax}>0) && ($line[$fields{fax}] ne "")) {
print "$line[$fields{fax}] ";
}
if (($fields{faxpriv}>0) && ($line[$fields{faxpriv}] ne "")) {
print "$line[$fields{faxpriv}] ";
}
if (($fields{faxwork}>0) && ($line[$fields{faxwork}] ne "")) {
print "$line[$fields{faxwork}] ";
}
if (($fields{faxsecretary}>0) && ($line[$fields{faxsecretary}] ne "")) {
print "$line[$fields{faxsecretary}] ";
}
if (($fields{faxmobile}>0) && ($line[$fields{faxmobile}] ne "")) {
print "$line[$fields{faxmobile}] ";
}
print "\n";
} elsif ($action eq "letter") {
&myprint (*line,mrmrs);
&myprint (*line,title);
&myprint (*line,firstname,lastname);
&myprint (*line,institute);
&myprint (*line,department);
&myprint (*line,addon);
&myprint (*line,pobox);
&myprint (*line,street);
if ($format{$coun} eq "us") {
&myprint (*line,",",city,state,zip);
} elsif ($format{$coun} eq "uk") {
&myprint (*line,city,state,zip);
} elsif ($format{$coun} eq "eu") {
if ($options{mycountry} ne $coun) {
&myprint (*line,"-",country,zip,city);
} else {
&myprint (*line,zip,city);
}
}
if ($options{mycountry} ne $coun) {
print "$fullname{$coun}\n"
}
print "\n";
} elsif ($action eq "all") {
$record = join("\n",@line);
print "=========================\n";
print "$record";
}
}
}
}
close(ADR);
sub myprint {
local(*lne) = shift @_;
local($nl) = 0;
local($fld);
local($space)=" ";
while (@_) {
$fld=shift @_;
if ($fld eq ",") { $space=", "; }
elsif ($fld eq "-") { $space="-"; }
elsif (($fields{$fld} ne "") && ($lne[$fields{$fld}] ne "")) {
print "$lne[$fields{$fld}]$space";
$space=" ";
$nl++;
}
}
if ($nl > 0) { print "\n"; }
}