
/*
 *  PARSE.C
 *
 *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
 */

#include <proto/all.h>
#include <stdio.h>
#include <stdlib.h>

/*
 *  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
 */

ParseAddress(str, buf, len)
char *str;
char *buf;
short len;
{
    short i;
    short 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 %
 */

char *
ParseAddress2(addr, buf, len)
char *addr;
char *buf;
short len;
{
    short 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);
}


