
/*
 *  MAKEPROTO.C
 *
 *  (c)Copyright 1990, Matthew Dillon, All Rights Reserved
 *
 *  MAKEPROTO [-o outfile] [-f field] file1 file2 ... fileN
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void ScanFile(char *);
void help(int);

char *Field = "Prototype";
int FieldLen = 9;

main(ac, av)
char *av[];
{
    short i;

    for (i = 1; i < ac; ++i) {
	char *ptr = av[i];
	if (*ptr != '-') {
	    ScanFile(ptr);
	    continue;
	}
	ptr += 2;
	switch(ptr[-1]) {
	case 'f':
	    if (*ptr == 0)
		ptr = av[++i];
	    Field = ptr;
	    FieldLen = strlen(ptr);
	    break;
	case 'o':
	    if (*ptr == 0)
		ptr = av[++i];
	    freopen(ptr, "w", stdout);
	    printf("\n/* MACHINE GENERATED */\n\n");
	    break;
	default:
	    printf("illegal option: -%c\n", ptr[-1]);
	    help(1);
	}
    }
    if (ac == 1)
	help(0);
    return(0);
}

void
help(code)
{
    fputs("MAKEPROTO - Scans for 'Prototype' lines in source files\n", stderr);
    fputs("makeproto [-o outfile] file1 file2... fileN\n", stderr);
    exit(code);
}

void
ScanFile(file)
char *file;
{
    FILE *fi = fopen(file, "r");
    char buf[512];

    if (fi == NULL) {
	fprintf(stderr, "makeproto: couldn't open %s\n", file);
	return;
    }
    printf("\n/* %-20s */\n\n", file);
    while (fgets(buf, sizeof(buf), fi)) {
	char *ptr = buf;

	if (*ptr == ';')
	    ++ptr;
	if (strncmp(ptr, Field, FieldLen) == 0)
	    fputs(ptr, stdout);
    }
    fclose(fi);
}

