%option noyywrap
%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "getopt.h"
#include <string.h> 
#define MAXLEN 1024
int i, j, idx, len, mode = 0;
char tmps[MAXLEN], reverse[MAXLEN];
%}
SEGA  [2][5][0-5]
SEGB  [2][0-4][0-9]
SEGC  [1][0-9]{2}
SEGD  [1-9][0-9]{0,1}
SEG   {SEGA}|{SEGB}|{SEGC}|{SEGD}
IP    {SEG}["."]{SEG}["."]{SEG}["."]{SEG}
SEGV6   [0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]
SEGA6   {SEGV6}[":"]
SEGI6   {SEGV6}{0,1}[":"]
IPV6    {SEGA6}{SEGI6}{SEGI6}{SEGI6}{SEGI6}{SEGV6}
HOSTDOMAINSEGMENT [0-9a-zA-Z_"\-"]+["."]
TOPLEVELDOMAIN [a-zA-Z]{2,7}
HOSTNAME {HOSTDOMAINSEGMENT}+{TOPLEVELDOMAIN}
USER [0-9A-Za-z_"\-""."]+
EMAIL1 {USER}"@"{HOSTNAME}
EMAIL2 {USER}"@"{IP}
URIPROTOCOL [a-zA-Z]{2,10}"://"
URISUFFIX [^ \t\n\r"@"","">""<""("")""{""}"]
URL1   {URIPROTOCOL}{HOSTNAME}{URISUFFIX}*
URL2   {URIPROTOCOL}{IP}{URISUFFIX}*
%%
<<EOF>> {
    exit(1);
}
{EMAIL1} |
{EMAIL2} { 
    if (strchr(yytext, '@') != (char *)NULL) 
    {
        switch((mode % 16)) 
        {
            case 0: strcpy(tmps, yytext); break;
            case 1: strcpy(tmps, strchr(yytext, (int)'@')); break;
            case 2: strcpy(tmps, &strchr(yytext, (int)'@')[1]); break;
            case 4: 
                strcpy(tmps, &strchr(yytext, (int)'@')[1]);
                len = strlen(tmps); 
                for (j = 0, idx = 0; ((j < len) && (j < MAXLEN-1)); j++) 
                {
                    if (tmps[j] == '.') 
                    {
                        for (i = idx; i <= j; 
                            reverse[(len-j) + (i-idx)] = tmps[i++]);
                        idx = j + 1;
                }
                reverse[len + 1] = '\0';
                strcpy(tmps, reverse);
                break;
            case 8: strcpy(tmps, &strrchr(yytext, (int)'.')[1]); break;
            default: strcpy(tmps, yytext); break;
        }
        (void) printf("%s\n", tmps);
    }
}
{URL1} |
{URL2} {
    if (mode < 16) 
    {
        switch((mode % 16)) 
        {
        case 0: strcpy(tmps, yytext); break;
        case 1: strcpy(tmps, strchr(yytext, (int)'/')); break;
        case 2: strcpy(tmps, &strrchr(yytext, (int)'/')[1]); break; 
        case 4: 
            strcpy(tmps, &strrchr(yytext, (int)'/')[1]);
            len = strlen(tmps);
            for (j = 0, idx = 0; ((j < len) && (j < MAXLEN-1)) ; j++)
            {
                if (tmps[j] == '.') 
                {
                    for (i = idx; i <= j; 
                        reverse[(len-j) + (i-idx)] = tmps[i++]);
                    idx = j + 1;
                }
            }
            for (i = idx; i <= j; 
                reverse[(len-j) + (i-idx)] = (i < len) ? tmps[i] : '.', i++);
            reverse[len + 1] = '\0';
            strcpy(tmps, reverse);
        break;
        case 8: strcpy(tmps, &strrchr(yytext, (int)'.')[1]); break;
        default: strcpy(tmps, yytext); break;
        }
        (void) printf("%s\n", tmps);
    }     
} 
{IP} {
    if (mode >= 32)  
        (void) printf("%s\n", yytext);
}
{IPV6} {
    if (mode >= 64) 
        (void) printf("%s\n", yytext);
}
^[\n;] { ; }
[\r\n]+ { ; }
. { ; }
%%
void yyerror() { exit(1); }
void usage(const char *cmd) 
{
    (void) printf("Usage: %s [-f file] [-a ] [ -r ] [ -u ]\n", cmd);
    (void) printf("\t simple email address and uri lexer reads from stdin \n");
    (void) printf("\t -a, --noat    \t print only hostname of email address (all chars left of \'@\') \n");
    (void) printf("\t -u, --nouser  \t print email without username \n");
    (void) printf("\t -t, --top     \t prints domain toplevel only, when using option -a | -u \n");
    (void) printf("\t -n, --nouris \t print only email address and not uris\n");
    (void) printf("\t -r, --reverse \t reverse the hostdomain / ip address segments\n");
        (void) printf("\t -4, --ipv4 \t prints lonley ipv4 address too.\n");                 (void) printf("\t -6, --ipv6 \t prints lonley ipv6 address too.\n"); 
    exit(0);
}
int _tmain(int argc, TCHAR** argv)
{
    static int verbose_flag;
    int c;
    while(1)
    {
        static struct option long_options[] =
        { 
            {_T("help"), ARG_NONE, 0, _T('h')},
            {_T("noat"), ARG_NONE, 0, _T('a')},
            {_T("nouser"), ARG_NONE, 0, _T('u')},
            {_T("top"), ARG_NONE, 0, _T('t')},
            {_T("nouris"), ARG_NONE, 0, _T('n')},
            {_T("reverse"),     ARG_NONE, 0, _T('r')},
            { ARG_NULL,         ARG_NULL, ARG_NULL, ARG_NULL}
        }; 
        int option_index = 0;
        c = getopt_long(argc, argv, _T("hautnr:"), long_options, &option_index);
 
        if (c == -1) 
            break;
 
        switch (c) // Handle options
        {
            case 0: // If this option set a flag, do nothing else now. 
                    if (long_options[option_index].flag != 0)
                        break;
                    _tprintf (_T("option %s"), long_options[option_index].name);
                    if (optarg)
                        _tprintf (_T(" with arg %s"), optarg);
                    _tprintf (_T("\n"));
                    break;
            case _T('u'): mode = 1; break; 
            case _T('a'): mode = 2; break; 
            case _T('r'): mode = 4; break;
            case _T('h'): usage(argv[0]); break;
            case _T('t'): mode = 8; break;
            case _T('n'): mode += 16; break;   
            case '?': break; // getopt_long already printed an error message. 
            default: abort();
        }
    }
    (void) fflush(stdout);
    yyin = stdin;
    yylex();
    exit(0);
}