code: 9ferno

ref: e81c54ba2ecc673a4d5f8aed0e9b52841fe07b0d
dir: /os/port/mkdevc/

View raw version
$AWK -v 'objtype='$OBJTYPE '
BEGIN{
		if(ARGC < 2)
			exit
}

/^$/{
		next;
}
/^#/{
		next;
}
collect && /^[^	\t]/{
		collect = 0;
}
collect && section ~ "dev"{
	dev[ndev++] = $1;
	if($1 == "sd")
		devsd = 1;
	else if($1 ~ "vga")
		devvga = 1;
}
collect && section ~ "ip"{
		ip[nip++] = $1;
}
collect && (section ~ "ether" || section ~ "link") {
		link[nlink++] = $1;
}
collect && section ~ "mod"{
		mod[nmod++] = $1;
}
collect && section ~ "vga"{
		option = 0;
		for(i = 2; i < NF; i++){
			if($i ~ "[+]hwgc"){
				hwgc[nhwgc++] = $1;
				option = 1;
			} else if($i ~ "[+=]hwgc"){
				hwgc[nhwgc++] = $1;
				if(option == 0)
					option = 2;
			}
		}
		if(option < 2)
			vga[nvga++] = $1;
}
collect && section ~ "misc"{
		misc[nmisc++] = $1;
		if($1 ~ "^arch.*")
			arch[narch++] = $1;
		else if($1 ~ "^sd.*")
			sdifc[nsdifc++] = $1;
		else if($1 ~ "^uart.*")
			physuart[nphysuart++] = substr($1, 5, length($1)-4) "physuart";
		else if($1 ~ "^vga.*"){
			if(NF == 1)
				vgadev[nvgadev++] = $1;
			else for(i = 2; i <= NF; i++){
				if($i ~ "[+]cur")
					vgadev[nvgadev++] = $1;
				if($i ~ "[+=]cur")
					vgacur[nvgacur++] = $1;
			}
		}
		else if($1 ~ ".*\.root"){
			x = substr($1, 1, index($1, ".")-1);
			if(x ~ "(dossrv|kfs)")
				x = "fs";
			fs[nfs++] = x;
		}
}
collect && section ~ "port"{
		port[nport++] = $0;
}
collect && section ~ "code"{
		code[ncode++] = $0;
}
$0 ~ /^[^ \t]/{
		if($0 ~ "(code|dev|ether|ip|lib|link|mod|misc|port|root|vga)"){
			section = $0;
			collect = 1;
		}
		next;
}

END{
		if(ARGC < 2)
			exit "usage"

		printf "#include \"u.h\"\n"
		printf "#include \"../port/lib.h\"\n"
		printf "#include \"mem.h\"\n"
		printf "#include \"dat.h\"\n"
		printf "#include \"fns.h\"\n"
		printf "#include \"io.h\"\n"
		printf "#include \"../port/error.h\"\n"
		printf "#include \"interp.h\"\n\n"
		printf "#include \"%s.root.h\"\n\n", ARGV[1];

		printf "ulong ndevs = %d;\n", ndev+8
		for(i = 0; i < ndev; i++)
			printf "extern Dev %sdevtab;\n", dev[i];
		printf "Dev* devtab[%d]={\n", ndev+8
		for(i = 0; i < ndev; i++)
			printf "\t&%sdevtab,\n", dev[i];
		printf "\tnil,\n};\n\n";


		for(i = 0; i < nfs; i++){
			printf "extern uchar %scode[];\n", fs[i];
			printf "extern ulong %slen;\n", fs[i];
		}
		for(i = 0; i < nlink; i++)
			printf "extern void %slink(void);\n", link[i];

		printf "void links(void){\n";
		for(i = 0; i < nfs; i++)
			printf "\taddrootfile(\"%s\", %scode, %slen);\n", fs[i], fs[i], fs[i];
		for(i = 0; i < nlink; i++)
			printf "\t%slink();\n", link[i];
		printf "}\n\n";

		for(i = 0; i < nmod; i++)
			printf "extern void %smodinit(void);\n", mod[i];
		printf "void modinit(void){\n";
		for(i = 0; i < nmod; i++)
			printf("\t%smodinit();\n",mod[i]);
		printf("}\n\n");

		if(narch || objtype ~ "(386|amd64)"){
			for(i = 0; i < narch; i++)
				printf "extern PCArch %s;\n", arch[i];
			printf "PCArch* knownarch[] = {\n";
			for(i = 0; i < narch; i++)
				printf "\t&%s,\n", arch[i];
			printf "\tnil,\n};\n\n";
		}

		if(devsd){
			printf "#include \"../port/sd.h\"\n";
			for(i = 0; i < nsdifc; i++)
				printf "extern SDifc %sifc;\n", sdifc[i];
			printf "SDifc* sdifc[] = {\n";
		  	for(i = 0; i < nsdifc; i++)
				printf "\t&%sifc,\n", sdifc[i];
		  	printf "\tnil,\n};\n\n";
		}

		if(nphysuart){
			for(i = 0; i < nphysuart; i++)
				printf "extern PhysUart %s;\n", physuart[i];
			printf "PhysUart* physuart[] = {\n";
		  	for(i = 0; i < nphysuart; i++)
				printf "\t&%s,\n", physuart[i];
		  	printf "\tnil,\n};\n\n";
		}

		if(devvga || nvga || nvgadev){
			printf "#include <draw.h>\n"
			printf "#include <memdraw.h>\n"

			if(nvga){
				printf "#include \"vga.h\"\n"
				for(i = 0; i < nvga; i++)
					printf "extern Vgac %s;\n", vga[i];
			  	printf "Vgac* knownvga[] = {\n";
			  	for(i = 0; i < nvga; i++)
					printf "\t&%s,\n", vga[i];
			  	printf "\tnil,\n};\n\n";

				if(nhwgc){
					for(i = 0; i < nhwgc; i++)
						printf "extern Hwgc %shwgc;\n", hwgc[i];
				  	printf "Hwgc* knownhwgc[] = {\n";
				  	for(i = 0; i < nhwgc; i++)
						printf "\t&%shwgc,\n", hwgc[i];
				  	printf "\tnil,\n};\n\n";
				}
			}

			if(nvgadev){
				printf "#include \"screen.h\"\n";
				for(i = 0; i < nvgadev; i++)
					printf "extern VGAdev %sdev;\n", vgadev[i];
			  	printf "VGAdev* vgadev[] = {\n";
			  	for(i = 0; i < nvgadev; i++)
					printf "\t&%sdev,\n", vgadev[i];
			  	printf "\tnil,\n};\n\n";

				for(i = 0; i < nvgacur; i++)
					printf "extern VGAcur %scur;\n", vgacur[i];
			  	printf "VGAcur* vgacur[] = {\n";
			  	for(i = 0; i < nvgacur; i++)
					printf "\t&%scur,\n", vgacur[i];
			  	printf "\tnil,\n};\n\n";
			}
		}
	
		if(nip){
			printf "#include \"../ip/ip.h\"\n";
			for(i = 0; i < nip; i++)
				printf "extern void %sinit(Fs*);\n", ip[i];
			printf "void (*ipprotoinit[])(Fs*) = {\n";
			for(i = 0; i < nip; i++)
				printf "\t%sinit,\n", ip[i];
			printf "\tnil,\n};\n\n";
		}

		for(i = 0; i < ncode; i++)
			printf "%s\n", code[i];

		printf "char* conffile = \"%s\";\n", ARGV[1];
		printf "u32 kerndate = KERNDATE;\n";

		exit
}' $*