/*
** aliassort v1.0
**
** Sorts and expands an alias list to unique entries
**
** Copyright (c) 16.3.95 by Andreas Ley <ley@rz.uni-karlsruhe.de>
**
** Permission to use, copy, modify, and distribute this software for any
** purpose and without fee is hereby granted, provided that the above
** copyright notice appears in all copies. This software is provided "as is"
** and without any express or implied warranties.
**
** This program has been tested on a HP9000/715 with HP-UX A.09.05
** In this environment, neither lint -u nor gcc -c -Wall produce any messages.
** If you encounter any errors or need to make any changes to port it
** to another platform, please contact me.
**
** Version history
**
** Version 1.0 - 16.3.95
**	Initial version
*/

char copyright[] = "@(#)Copyright (c) 1995 by Andreas Ley (ley@rz.uni-karlsruhe.de)";
char sccsid[] = "@(#)aliassort v1.0 - Sorts and expands an alias list to unique entries";


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>

#ifndef FALSE
#define	FALSE	(0)		/* This is the naked Truth */
#define	TRUE	(1)		/* and this is the Light */
#endif


typedef struct alias {
	struct alias	*next;
	char		*id;
	char		*url;
	} alias;

alias	*aliases=NULL;


void addalias(id,url)
char	*id,*url;
{
	alias	*aptr=(alias *)&aliases;

	while (aptr->next) {
		aptr=aptr->next;
		if (!strcmp(aptr->id,id))
			return;
	}
	aptr->next=(alias *)malloc(sizeof(alias));
	aptr=aptr->next;
	aptr->next=NULL;
	aptr->id=id;
	aptr->url=url;
}



void readalias(src)
FILE	*src;
{
	char	line[BUFSIZ],*ptr,*url,*id,*name;

	while (fgets(line,sizeof(line),src)) {
		ptr=strchr(line,'\0');
		if (*(ptr-1)=='\n')
			ptr--;
		if (*(ptr-1)=='\r')
			ptr--;
		while (ptr>line && *(ptr-1)==' ')
			ptr--;
		*ptr='\0';
		if (*line&&*line!='#') {
			for(ptr=line;*ptr&&!strchr(" \t",*ptr);ptr++);
			for(*ptr++='\0';*ptr&&strchr(" \t",*ptr);ptr++);
			if (*ptr)
				addalias(strdup(line),strdup(ptr));
		}
	}
}



char *expandalias(url)
char	*url;
{
	static char	buffer[BUFSIZ];
	char		*ptr;
	alias		*aptr;

	url+=2;
	if (ptr=strchr(url,'/'))
		*ptr++='\0';
	for (aptr=aliases;aptr;aptr=aptr->next)
		if (!strcmp(aptr->id,url))
			break;
	if (aptr) {
		strcpy(buffer,strncmp(aptr->url,"/~",2)?aptr->url:expandalias(aptr->url));
		if (ptr)
			strcat(buffer,ptr);
		return(buffer);
	}
	fprintf(stderr,"Can't expand %s\n",url);
	return(NULL);
}



void sortalias()
{
	alias	*aptr;

	for (aptr=aliases;aptr;aptr=aptr->next)
		printf("%s\t%s\n",aptr->id,strncmp(aptr->url,"/~",2)?aptr->url:expandalias(aptr->url));
}



void usage(image)
char *image;
{
	(void)fprintf(stderr,"Usage: %s [-h] [-v] [filename...]\n",image);
	exit(1);
}


main(argc,argv)
int	argc;
char	*argv[];
{
	int		c;
	extern char	*optarg;
	extern int	optind;
	char		error[2*MAXPATHLEN+14];
	FILE		*src;

	while ((c=getopt(argc,argv,"vh?")) != EOF)
		switch ((char)c) {
		case 'v':
			(void)fprintf(stderr,"%s\n",sccsid+4);
			(void)fprintf(stderr,"%s\n",copyright+4);
			exit(0);
		case 'h':
			(void)fprintf(stderr,"%s\n",sccsid+4);
			(void)fprintf(stderr,"%s\n",copyright+4);
		case '?':
			usage(argv[0]);
		}

	if (optind<argc)
		for (;optind<argc;optind++)
			if (strcmp(argv[optind],"-"))
				if ((src=fopen(argv[optind],"r"))) {
					readalias(src);
					(void)fclose(src);
				}
				else {
					(void)sprintf(error,"%s: cannot open %s",argv[0],argv[optind]);
					perror(error);
				}
			else
				readalias(stdin);
	else
		readalias(stdin);

	sortalias();
	return(0);
}

