
/*
 *  PARSE.C
 *
 *  $Header: Beta:src/uucp/src/sendmail/RCS/parse.c,v 1.1 90/02/02 12:15:05 dillon Exp Locker: dillon $
 *
 *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
 */

#include "defs.h"

Prototype int ParseAddress(char *, char *, int);

Local char *ParseAddress2(char *, char *, int);

/*
 *  PARSEADDRESS()
 *
 *  Takes an address containing ! @ % : and converts it to a level 3 ! path.
 *
 *  [path]@mach 	->  mach[!path]
 *  [path]%mach 	->  mach[!path]
 *  patha:pathb 	->  patha!pathb
 *  patha:pathb:pathc	->  patha!pathb!pathc
 */

int
ParseAddress(str, buf, len)
char *str;
char *buf;
int len;
{
    int i;
    int j;
    char *base = buf;
    char *ParseAddress2();

    for (i = j = 0; i < len; ++i) {
	if (str[i] == ':') {
	    buf = ParseAddress2(str + j, buf, i - j);
	    *buf++ = '!';
	    j = i + 1;
	}
    }
    buf = ParseAddress2(str + j, buf, i - j);
    *buf = 0;
    for (i = 0; base[i] && base[i] != '!'; ++i);
    return((int)i);
}

/*
 *  deals with !, @, and %
 */

static char *
ParseAddress2(addr, buf, len)
char *addr;
char *buf;
int len;
{
    int i;

    for (i = len - 1; i >= 0; --i) {
	if (addr[i] == '@' || addr[i] == '%') {
	    short j = len - i;
	    strncpy(buf, addr + i + 1, j - 1);
	    buf += j - 1;
	    len -= j;
	    if (len)
		*buf++ = '!';
	}
    }
    strncpy(buf, addr, len);
    buf += len;
    return(buf);
}


