#include #include #include #include #include #include #include // set this to 3 for further information #define DEBUG 0 /* forward reference for the C compiler */ int getchunk(char *chunkname); /* a memory mapped buffer copy of the file */ char *buffer; int bufsize; /* return the length of the next line */ int nextline(int i) { int j; if (i >= bufsize) return(-1); for (j=0; ((i+j < bufsize) && (buffer[i+j] != '\n')); j++); return(j); } /* output the line we need */ int printline(int i, int length) { int j; for (j=0; j 0) { getname = getChunkname(k,getlen); getchunk(getname); free(getname); k=k+getlen+12l; } else { if ((linelen >= 11) && (foundEnd(k,chunkname) == 1)) { if (DEBUG==3) { printf("=== \\end{%s} ===\n",chunkname); } return(k+12); } else { if (DEBUG==2) { printf("======== printchunk else %d %d\n",k,linelen); } printline(k,linelen); k=k+linelen+1; } }} if (DEBUG==2) { printf("=================\\out{%s} %d\n",chunkname,k); } return(k); } /* find the named chunk and call printchunk on it */ int getchunk(char *chunkname) { int i; int linelen; int chunklen = strlen(chunkname); if (DEBUG==3) { printf("getchunk(%s)\n",chunkname); } for (i=0; ((linelen=nextline(i)) != -1); ) { if (DEBUG==2) { printf("----"); printline(i,linelen); printf("----\n"); } if ((linelen >= chunklen+15) && (foundchunk(i,chunkname) == 1)) { if (DEBUG==2) { fprintf(stderr,"=================\\getchunk(%s)\n",chunkname); } i=printchunk(i,linelen,chunkname); } else { i=i+linelen+1; } } if (DEBUG==2) { fprintf(stderr,"=================getchunk returned=%d\n",i); } return(i); } /* memory map the input file into the global buffer and get the chunk */ int main(int argc, char *argv[]) { int fd; struct stat filestat; if ((argc == 1) || (argc > 3)) { perror("Usage: tangle filename chunkname"); exit(-1); } fd = open(argv[1],O_RDONLY); if (fd == -1) { perror("Error opening file for reading"); exit(-2); } if (fstat(fd,&filestat) < 0) { perror("Error getting input file size"); exit(-3); } bufsize = (int)filestat.st_size; buffer = mmap(0,filestat.st_size,PROT_READ,MAP_SHARED,fd,0); if (buffer == MAP_FAILED) { close(fd); perror("Error reading the file"); exit(-4); } if (argc == 2) { getchunk("*"); } else { getchunk(argv[2]); } close(fd); return(0); }