%{

#include <string.h>
char line[256], link[256], path[256], *pathfile=path, *linkfile=link, *name, *pathname;
int file_only = 0, attach_path = 0, prepend_file = 0, line_i = 0;

int main(int argc, char **argv)
{
  int i;
  for (i=1; i<argc; i++) {
    char *arg = argv[i];
    if (arg[0] = '-') {
      switch (arg[1]) {
        case 'f': file_only = 1; attach_path = (arg[2]=='a'); break;
        case 'b': 
          strcpy(link, arg+2); 
	  strcpy(path, arg+2);
          linkfile = link+strlen(link); 
          pathfile = path+strlen(path);
          break;
        case 'p': prepend_file = 1; break;
      }
    }
  }
  yylex();
  return 0;
}

void print_link(char *link, char *name)
{
  printf("<A HREF=%s>%s</A>", link, name); 
}

%}

%%

[^\ ]*\n   { 
             int is_file = 0;
             strcpy(linkfile,yytext); 
             linkfile[strlen(linkfile)-1]=0; // strip new '\n'
             if (file_only) {
               name = strrchr(linkfile, '/');
	       pathfile[0] = 0;
               if (name) { 
                 name++;
                 if (attach_path) {
		   strcpy(pathfile, linkfile);
                   pathfile[name-linkfile-1] = 0;
                 }
               } else
                 name = linkfile;
             } else
               name = linkfile;
	     line[line_i] = 0; line_i = 0;
             if (prepend_file)
               printf("%s\t", name);
             printf("%s", line);
	     is_file = strlen(name);
	     if (is_file)
	       print_link(link, name); 
	     if (attach_path && strlen(pathfile)) {
	       if (is_file)
                 printf(" (");
	       print_link(path, pathfile);
	       if (is_file)
	         printf(")");
	       else
	         printf(" (dir)");
             }
	     printf("\n");
           }

.          line[line_i++] = yytext[0];
%%  




