Skip to content

Instantly share code, notes, and snippets.

@steveroush
Created May 30, 2026 02:07
Show Gist options
  • Select an option

  • Save steveroush/345acc7ab92d28a1966e9da5b6037f8a to your computer and use it in GitHub Desktop.

Select an option

Save steveroush/345acc7ab92d28a1966e9da5b6037f8a to your computer and use it in GitHub Desktop.
gvInclude.gvpr - Graphviz #include feature
/**************************************************************
gvInclude.gvpr
- uses gvpr as a simple awk-like language, NOT as a Graphviz-parser
- recognives "#include filename" instruction and inserts the contents of "filename"
- #include files can be nested
- #include files can be included multiple times
usage: gvpr -aFmygvfile.gv
**************************************************************/
BEGIN{
int i, lineNo, First[], Last[], __DEBUG__, needFile[];
string inpt, before, after, InputFile, nxtFile, oneLine[int], workStr[];
/////////////////////////////////////////////////////////////////////////
void readFile(string InputFile) {
int fd, done;
//print("// readFile ", InputFile, " First: ", lineNo+1);
fd=openF(InputFile, "r");
if (fd==-1) {
print("OPEN ERROR: ", InputFile);
} else {
done=0;
lineNo++;
First[InputFile]=lineNo;
while (done==0){
inpt=readL(fd);
//print("// READ >", inpt,"<");
if (inpt==""){ // EOF
//print("// DONE");
done=1;
Last[InputFile]=lineNo;
closeF(fd);
}else{
before=sub(inpt,"\n$"); // strip trailing linefeed
oneLine[lineNo]=before;
after=sub(before,"^*([\t ])#include*([\t ])"); // ksh pattern matching
//print("// >>",before,"<<");
//print("// >>",before,"<<");
//print("// >>",after,"<<");
if (strcmp(before,after)!=0){
after=sub(after,"^\""); // strip leading quote
after=sub(after,"\"$"); // strip trailing quote
//print("// Include >", before,"< >",after,"<");
workStr[lineNo]=after;
if (First[after]<1)
needFile[after]=1;
}
lineNo++;
}
}
}
//print("// leaving reader ", InputFile, " ", First[InputFile]," ", Last[InputFile]);
}
/////////////////////////////////////////////////////////////////////////
int printFile(string aFile, int From, int To, int saveNo) {
int fd, done, l;
if (aFile != InputFile)
print(" #### including file >", aFile);
for (l=From; l<=To; l++){
//print("// ",l,"::",oneLine[l]);
if ((workStr[l]=="" && (l<To || oneLine[l]!="")))
print(oneLine[l]);
if (workStr[l]!=""){
//print(" ## including ", workStr[l]);
after=workStr[l];
l=printFile(after, First[after], Last[after], l );
}
}
if (aFile != InputFile)
print(" #### finished including file ", aFile);
return saveNo;
}
/////////////////////////////////////////////////////////////////////////
i=0;
while (i<ARGC) {
InputFile="";
if (ARGV[i]=="F") { // -a "F graphvizFile.gv ..."
InputFile=ARGV[++i];
} else if (ARGV[i]=="F*") { // -a "FgraphvizFile.gv"
InputFile=substr(ARGV[i],1);
} else {
print(help);
exit (0);
}
i++;
//print("// file >", InputFile,"<");
if (InputFile!="") {
readFile(InputFile);
}
}
// do we also require a haveFile[] array ???
for (needFile[nxtFile]) {
//print("// reading >", nxtFile,"<");
readFile(nxtFile);
unset(needFile, nxtFile);
}
printFile(InputFile, First[InputFile], Last[InputFile], 0);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment