/*
	Expander for ++filename macro in text input files,
	also removes comments (anything following a #)

	Wessex Scientific and Technical Services Ltd
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>

/* maximum characters per line */
#define MAXCHAR 320

/* recursive expansion routine */

void expand(unit)
FILE *unit;
{
	char line[MAXCHAR];
	FILE *new_unit;
	int i;

	while (fgets(line, MAXCHAR, unit) != NULL) {
		if (line[0] == '+' && line[1] == '+') {
			line[strlen(line)-1] = '\0';
			if ((new_unit = fopen(&line[2], "r")) == NULL) {
				fprintf(stderr,
				   "expander: can't open %s\n", &line[2]);
				exit(1);
			} else {
				expand(new_unit);
				fclose(new_unit);
			}
		} else {
                        for (i=0;i<strlen(line);i++)
                           if (line[i] == '#') { line[i] = '\0'; break; }
                        if (strlen(line) > 0) printf("%s",line);
		}
	}
}

/* main routine calls expander on stdin */

int main()
{
expand(stdin);
exit(0);
}
