# Copyright 1989, 1990 Michael DeCorte


${awk} '
function eol(i)
{
        i = 0;

        if (length($0) < current_char)
        {
                i =  1;
        }

        return i;
}
# gets the next char for input
function getchar()
{
        if (length($0) < current_char)
                ret = "";
        else
                ret= substr($0, current_char, 1);
        current_char++;
        return ret;
}

# unreads what ever was the last read
function ungetchar()
{
        current_char--;
}

# skips over quoted strings; c,s  are local vars, not param
function string(c, s)
{
        c = s = "";

        c = getchar()

        if (c != "\"") 
        {
                ungetchar();
                return "";
        }

        while (!eol())
        {
                c = getchar();
                if (c == "\"") break;

                s = s c;

                if (c == "\\")            # quoted char, skip next
                {
                        getchar();
                        continue;
                }
        }
        return s;
}

# skips over quotes; c, s are local vars, not param
function quote(c, s)
{
        c = s = "";

        c = getchar();
        if (c != "(") 
        {
                ungetchar();
                return "";
        }

        while (!eol())
        {
                c = getchar();
                s = s c;

                if (c == "\\")            # quoted char, skip next
                {
                        getchar();
                        continue;
                }
                if (c == ")") 
                {
                        break;     # end commend
                }
                if (c == "(")
                {
                        ungetchar();
                        s = s quote();
                        continue;
                }
                if (c == "\"")
                {
                        ungetchar();
                        s = s "\"" string() "\"";
                        continue;
                }
        }
        return s;
}

# returns the field-name, c,s locals
function field_name(c,s)
{
        c = s = "";

        while (!match(c, "[ \t:]"))
        {
                c = getchar();
                if (c == "") break;
                s = s c;
        }

        return s;
}

# grabs things like this:
# "<" [ "@" domain ":" ] local-part "@" domain ">"
# c,s locals
function route_addr(c,s,a)
{
        c = s = "";

        c = getchar();
        
        if (c != "<") 
        {
                ungetchar();
                return "";
        }

        while (!eol())
        {
                if (quote() != "")
                        continue;                

                if ((s = string()) != "")
                {
                        a = a "\"" s "\"";
                        continue;                
                }
                c = getchar();
                if (c == ">") break;
                a = a c;
        }
        return a;
}

# grabs things like this:
#":" local-part "@" domain ";"
# or
# ":" word "<" [ "@" domain ":" ] local-part "@" domain ">" ";"
# returns the route_addr in this second one
# c,s locals
function group(c,s,a)
{
        c = s = a = "";

        c = getchar();
        if (c != ":") 
        {
                ungetchar();
                return "";
        }

        while (1)
        {
                if (quote() != "")
                        continue;                

                if ((s = string()) != "")
                {
                        a = a "\"" s "\"";
                        continue;                
                }
                if ((s = route_addr()) != "")
                {
                        a = s;
                        break;
                }
                c = getchar();
                if (c == "") break;
                if (c == ";") break;
                a = a c;
        }
        return a;
}

# returns the address without extra stuff
# this is not rfs822 spec.  It doesnt grab multiple address from
# the line.  Only the first.  But it does grab the first correctly
function address(c,s,a)
{
        c = s = a = "";

        while (!eol())
        {
                if (quote() != "")
                        continue;                

                if ((s = string()) != "")
                {
                        a = a "\"" s "\"";
                        continue;                
                }

                if ((s = route_addr()) != "")
                {
                        a = s;
                        break;
                }

                if ((s = group()) != "")
                {
                        a = s;
                        break;
                }
                c = getchar();
                a = a c;
        }
        return a;
}

{
        current_char = 1;
        f = field_name();
        printf("%s %s\n", f, address());
}'
