/****************************************************************/
/*								*/
/*		( gcc mchipdoc.c -o mchipdoc )			*/
/*						by S.Takahata	*/
/****************************************************************/
#include	<stdio.h>
#include	<string.h>

#define	MAXMUM	10000
#define	KUGIRI	" \t\n"
#define	WAKU0	"劕劒劒劒劒劒劒劒劧劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劖\n"
#define WAKU8	"劔  僼傽僀儖柤  劆  %-20s                                  劔\n"
#define WAKU9	"劙劒劒劒劒劒劧劒劯劒劒劒劒劒劒劒劒劧劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劜\n"
#define	WAKU1	"劔  峴斣崋  劆     儖乕僠儞柤     劆           撪            梕           劔\n"
#define	WAKU2	"劙劒劒劒劒劒劰劒劒劒劒劒劒劒劒劒劒劰劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劜\n"
#define	WAKU3	"劔  %6d  劆  %-16s  劆                                      劔\n"
#define	WAKU4	"劦劅劅劅劅劅劑劅劅劅劅劅劅劅劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劮\n"
#define	WAKU5	"劘劒劒劒劒劒劯劒劒劒劒劒劒劒劒劒劒劯劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劒劗\n"
#define	WAKU6	"                                 劅 %3d 劅\n\f"
#define	WAKU7	"                                 劅 %3d 劅\n"
#define	PAGE1	28

typedef struct{
    unsigned char	address[7];
    unsigned char	name[20];
}LABEL;

static LABEL	label[MAXMUM];
static FILE	*fpr, *fpw;

void main(int argc, char **argv)
{
    int		i, j, k, m, n;
    char	s[256], *s1, number[256], name[256];
    unsigned char	in_file[40], out_file[44];
    
    if(argc < 2){
	printf("Input Error !!\n");
	printf("mchipdoc [擖椡僼傽僀儖柤] \n");
	exit(1);
    }
    strcpy(in_file, argv[1]);
    if((fpr = fopen(in_file, "r")) == NULL){
	printf("File Open Error !! [%s]\n", in_file);
	exit(1);
    }
    if((fpw = fopen("OUTFILE", "w")) == NULL){
	printf("File Open Error !! [%s]\n", in_file);
	exit(1);
    }


    if(fgets(s, 256, fpr) != NULL){
	i = j = k = 1;
	do{
	    s1 = strtok(s, KUGIRI);
	    if(s1 != NULL && *s1 != ';'){
		strcpy(name, s1);
		s1 = strtok(NULL, KUGIRI);
		if(s1 != NULL && strcmp(s1,"EQU") == 0){
		    s1 = strtok(NULL, KUGIRI);
		    if(s1 != NULL && strcmp(s1, "$") == 0)	goto NEXTPRC;
		}
	    }
	    j ++;
	}while(fgets(s, 256, fpr) != NULL);
	fclose(fpr);
	fclose(fpw);
	printf("Not Find [EQU]\n");
	exit(1);
NEXTPRC:
	fprintf(fpw, WAKU0);
	fprintf(fpw, WAKU8, in_file);
	fprintf(fpw, WAKU9);
	fprintf(fpw, WAKU1);
	fprintf(fpw, WAKU2);
	fprintf(fpw, WAKU3, j, name);
	while(fgets(s, 256, fpr) != NULL){
	    j ++;
	    if((s1 = strtok(s, KUGIRI)) == NULL || *s1 == ';')	continue;
	    strcpy(name, s1);
	    s1 = strtok(NULL, KUGIRI);
	    if(s1 == NULL || strcmp(s1,"EQU") != 0)	continue;
	    s1 = strtok(NULL, KUGIRI);
	    if(s1 == NULL || strcmp(s1, "$") != 0)	continue;
	    if(i == PAGE1){
		fprintf(fpw, WAKU5);
		fprintf(fpw, WAKU6, k);
		fprintf(fpw, WAKU0);
		fprintf(fpw, WAKU8, in_file);
		fprintf(fpw, WAKU9);
		fprintf(fpw, WAKU1);
		fprintf(fpw, WAKU2);
		fprintf(fpw, WAKU3, j, name);
		i = 1;
		k ++;
	    }
	    else{
		fprintf(fpw, WAKU4);
		fprintf(fpw, WAKU3, j, name);
		i ++;
	    }
	}
	fprintf(fpw, WAKU5);
	for(m = 0; m < (PAGE1-i-1)*2; m ++){
	    fprintf(fpw, "\n");
	}
	fprintf(fpw, WAKU7, k);
    }
    
    fclose(fpr);
    fclose(fpw);

    /*system("lpr -Psjlbp OUTFILE");*/
    system("more OUTFILE");
    system("rm OUTFILE");
}

