home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.muug.mb.ca
/
2014.06.ftp.muug.mb.ca.tar
/
ftp.muug.mb.ca
/
pub
/
src
/
autoalias.shar
/
nameptx.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-20
|
6KB
|
251 lines
/*
* nameptx - generate "permuted index" of names
* Based on "nptx.c" from the smail2.5 distribution.
* Compile with -DDOT_REQD if you only want output of
* aliases with dot separators.
* Compile with -DSYSV if you want strchr() and strrchr()
* instead of index() and rindex().
*
* Modifications by: Gilles R. Detillieux, <grdetil@scrc.umanitoba.ca>
* Spinal Cord Research Centre
* Dept. of Physiology
* Univ. of Manitoba, Winnipeg, Canada
*
* The original source did not include any copyright notice.
* I've been told it's in the public domain. I thank the author
* of smail 2.5 for making the source available.
* I've modified nptx.c to make it more self-contained (it used
* to require two object files from the smail program) and to
* convert the name field to lower case, so lcasep.c isn't needed.
*
*
* nameptx reads a list of address name pairs on the standard input
* and prints name permutations and the address pairs on the standard
* output. This can used in the generation of full name aliases for
* sendmail. The format of an input line is:
*
* address name
*
* The address field can contain any address, it is terminated by a
* TAB char (ascii 0x9). No translation is done on the field. The
* name field consists of whitespace separated names or initials with
* an optional nickname given in parentheses, it is terminated by
* either a newline ascii (0xA) or a ',' (ascii 0x2C). The name field
* is converted to lower case. All permutations of the names and
* initials are printed. The only restriction is that the last name
* will appear in each permutation. The permutations are not necessarily
* unique.
*
* E.g.:
*
* $ echo "gpb@ECH.gatech.edu\tWrecker Burdell(George P.)" | nameptx
* burdell gpb@ECH.gatech.edu
* w.burdell gpb@ECH.gatech.edu
* wrecker.burdell gpb@ECH.gatech.edu
* burdell gpb@ECH.gatech.edu
* g.burdell gpb@ECH.gatech.edu
* george.burdell gpb@ECH.gatech.edu
* p.burdell gpb@ECH.gatech.edu
* p.burdell gpb@ECH.gatech.edu
* g.p.burdell gpb@ECH.gatech.edu
* george.p.burdell gpb@ECH.gatech.edu
* g.p.burdell gpb@ECH.gatech.edu
* george.p.burdell gpb@ECH.gatech.edu
* $
*
*/
#include <stdio.h>
#include <ctype.h>
#ifdef SYSV
#define index strchr
#define rindex strrchr
#endif
char *index(), *rindex(), *strcpy(), *strcat();
void nptx(), dotspace();
main()
{
char buf[256], buf2[256];
register char *p, *name, *last, *nick, *ctmp;
while(gets(buf) != NULL) {
/* line should be in form
**
** login First Last
** or
** login First Last(Nickname)
**
*/
if((p = index(buf, '\t')) == NULL) {
(void) fprintf(stderr, "format error: %s\n", buf);
continue;
}
*p++ = '\0';
for(name = p; *name != '\0'; name++)
if(isupper(*name))
*name = tolower(*name);
else if(*name == ',' || *name == '\n') {
*name = '\0';
break;
}
if ((nick = index(p, '(')) != NULL) {
*nick++ = '\0';
if ((ctmp = index(nick, ')')) == NULL) {
nick = NULL;
} else {
*ctmp = '\0';
}
}
name = p;
dotspace(name);
if ((last = rindex(name, '.')) != NULL) {
last++;
if((strcmp(last, "jr") == 0)
|| (strcmp(last, "sr") == 0)) {
while(--last > name && last[-1] != '.')
;
if(last <= name)
last = NULL;
}
}
nptx(buf, name);
if((last != NULL) && (nick != NULL)) {
(void) sprintf(buf2, "%s.%s", nick, last);
dotspace(buf2);
nptx(buf, buf2);
}
}
return(0);
}
void
dotspace(s)
char *s;
{
register char *p, *t;
/* turn whitespace to '.' */
for(p = s; *p != '\0'; p++) {
if((*p == ' ') || (*p == '\t')) {
*p = '.';
}
}
/* elide leading '.'s */
for(p = s; *p == '.' ; p++)
;
/* elide mulitple '.'s and all "'"s */
for(t = s; *p != '\0'; p++, t++) {
*t = *p;
if(*t == '\'') {
t--;
continue;
}
if(*p == '.') {
while(*(++p) == '.')
;
p--;
}
}
*t = '\0';
/* elide trailing '.' */
if((t > s) && (*(--t) == '.')) *t = '\0';
}
void
nptx(login, name)
char *login, *name;
{
int i,j,k,N,lim,mask;
int ii,ji,ki,Ni,limi,maski;
char nl[11][100], il[11][100];
char *pi, *p, *rindex();
char buf[100];
char bufi[100];
if((name == NULL) || (*name == '\0')) {
return;
}
for(i=0; i < 10; i++) {
if((p = rindex(name, '.')) == NULL) break;
(void) strcpy(nl[i], p+1);
*p = '\0';
}
(void) strcpy(nl[i], name);
while((strcmp(nl[i], "mr" ) == 0)
|| (strcmp(nl[i], "dr" ) == 0)
|| (strcmp(nl[i], "mrs" ) == 0)
|| (strcmp(nl[i], "miss") == 0)
|| (strcmp(nl[i], "ms" ) == 0)) {
i--;
}
while((strcmp(nl[0], "jr") == 0)
|| (strcmp(nl[0], "sr") == 0)) {
for(j=0; j < i; j++) {
(void) strcpy(nl[j], nl[j+1]);
}
i--;
}
N = i;
lim = 1 << (N+1);
for(mask = 1 << N ; mask < lim ; mask++) {
buf[0] = '\0';
for(j = 1, k = N; j < lim; j <<=1, k--) {
if(j & mask) {
(void) strcat(buf, nl[k]);
(void) strcat(buf, ".");
}
}
if((p = rindex(buf, '.')) != NULL) {
*p = '\0';
}
for(ii=0; ii < 10; ii++) {
if((pi = rindex(buf, '.')) == NULL) break;
(void) strcpy(il[ii], pi+1);
*pi = '\0';
}
(void) strcpy(il[ii], buf);
Ni = ii;
limi = 1 << (Ni+1);
for(maski = 1 << Ni /* 0 */ ; maski < limi ; maski++) {
bufi[0] = '\0';
for(ji = 1, ki = Ni; ji < limi; ji <<=1, ki--) {
if(ji & maski) {
(void) strcat(bufi, il[ki]);
} else {
char init[3];
init[0] = il[ki][0];
init[1] = '\0';
(void) strcat(bufi, init);
}
(void) strcat(bufi, ".");
}
if((pi = rindex(bufi, '.')) != NULL) {
*pi = '\0';
}
#ifdef DOT_REQD
if(index(bufi, '.') == NULL) {
continue;
}
#endif /* DOT_REQD */
(void) printf("%s\t%s\n",bufi, login); /* */
}
}
}