LOCAL void append_source_name(char const * pz) { static char const fmt[] = " \\\n\t%s\n"; size_t ln = strlen(pz) + sizeof(fmt) - 2 /* one for '%' and one for 's' */; char * insert_pt; if (source_used + ln >= source_size) { pzSourceList = AGREALOC(pzSourceList, source_size + ln + 2048, "srclist"); source_size += ln + 2048; } insert_pt = (char *)pzSourceList + source_used; ln = sprintf(insert_pt, fmt, pz); /* * See if we've done this file before. */ { char * p = strstr(pzSourceList, insert_pt); if (p == insert_pt) { source_used += ln - 1; p += ln - 1; } else p = insert_pt; *p = NUL; } }
void manageAllocatedData(void* pd) { static int allocPtrCt = 0; static int usedPtrCt = 0; static void** papAllocData = NULL; if (pd == NULL) { void** pp = papAllocData; if (pp == NULL) return; while (--usedPtrCt >= 0) AGFREE(*(pp++)); AGFREE(papAllocData); papAllocData = NULL; } else { if (++usedPtrCt > allocPtrCt) { allocPtrCt += 16; papAllocData = (usedPtrCt > 1) ? AGREALOC(papAllocData, allocPtrCt * sizeof(void*), "atbl") : AGALOC(allocPtrCt * sizeof(void*), "atbl"); } papAllocData[usedPtrCt-1] = pd; } }
/* * find entry support routines: * * add_to_def_list: place a new definition entry on the end of the * list of found definitions (reallocating list size as needed). */ static void add_to_def_list(tDefEntry* pDE, tDefEntryList* pDEL) { if (++(pDEL->usedCt) > pDEL->allocCt) { pDEL->allocCt += pDEL->allocCt + 8; /* 8, 24, 56, ... */ pDEL->papDefEntry = (tDefEntry**) AGREALOC((void*)pDEL->papDefEntry, pDEL->allocCt * sizeof(void*), "add find"); } pDEL->papDefEntry[ pDEL->usedCt-1 ] = pDE; }
/** * Process the stuff in the pseudo macro. */ static tTemplate * digest_pseudo_macro(tmap_info_t * minfo, char * real_file) { tTemplate * pRes; /* * Count the number of macros in the template. Compute * the output data size as a function of the number of macros * and the size of the template data. These may get reduced * by comments. */ char const * pzData = loadPseudoMacro((char const *)minfo->txt_data, real_file); size_t macroCt = cnt_macros(pzData); size_t alocSize = (sizeof(*pRes) + (macroCt * sizeof(tMacro)) + minfo->txt_size - (pzData - (char const *)minfo->txt_data) + strlen(real_file) + 0x10) & ~0x0F; pRes = (tTemplate*)AGALOC(alocSize, "main template"); memset((void*)pRes, 0, alocSize); /* * Initialize the values: */ pRes->magic = magicMark; pRes->descSize = alocSize; pRes->macroCt = macroCt; strcpy(pRes->zStartMac, zStartMac); /* must fit */ strcpy(pRes->zEndMac, zEndMac); /* must fit */ load_macs(pRes, real_file, "*template file*", pzData); pRes->pzTplName -= (long)pRes; pRes->pzTemplText -= (long)pRes; pRes = (tTemplate*)AGREALOC((void*)pRes, pRes->descSize, "resize template"); pRes->pzTplName += (long)pRes; pRes->pzTemplText += (long)pRes; return pRes; }
/* * Put an entry into an argument list. The first argument points to * a pointer to the argument list structure. It gets passed around * as an opaque address. */ LOCAL void addArgListEntry(void ** ppAL, void * entry) { tArgList* pAL = *(void**)ppAL; /* * IF we have never allocated one of these, * THEN allocate one now */ if (pAL == NULL) { pAL = (tArgList*)AGALOC(sizeof(*pAL), "new option arg stack"); if (pAL == NULL) return; pAL->useCt = 0; pAL->allocCt = MIN_ARG_ALLOC_CT; *ppAL = (void*)pAL; } /* * ELSE if we are out of room * THEN make it bigger */ else if (pAL->useCt >= pAL->allocCt) { size_t sz = sizeof(*pAL); pAL->allocCt += INCR_ARG_ALLOC_CT; /* * The base structure contains space for MIN_ARG_ALLOC_CT * pointers. We subtract it off to find our augment size. */ sz += sizeof(char*) * ((size_t)pAL->allocCt - MIN_ARG_ALLOC_CT); pAL = (tArgList*)AGREALOC((void*)pAL, sz, "expanded opt arg stack"); if (pAL == NULL) return; *ppAL = (void*)pAL; } /* * Insert the new argument into the list */ pAL->apzArgs[ (pAL->useCt)++ ] = entry; }
/** * Suck in the entire definitions file and parse it. */ LOCAL void readDefines(void) { char const * pzDefFile; char * pzData; size_t dataSize; size_t sizeLeft; FILE * fp; def_input_mode_t in_mode = ready_input(&pzDefFile, &dataSize); if (in_mode == INPUT_DONE) return; /* * Allocate the space we need for our definitions. */ sizeLeft = dataSize+4+sizeof(*pBaseCtx); pBaseCtx = (tScanCtx*)AGALOC(sizeLeft, "file buffer"); memset((void*)pBaseCtx, 0, sizeLeft); pBaseCtx->lineNo = 1; sizeLeft = dataSize; /* * Our base context will have its currency pointer set to this * input. It is also a scanning pointer, but since this buffer * is never deallocated, we do not have to remember the initial * value. (It may get reallocated here in this routine, tho...) */ pzData = pBaseCtx->pzScan = pBaseCtx->pzData = (char*)(pBaseCtx+1); pBaseCtx->pCtx = NULL; /* * Set the input file pointer, as needed */ if (in_mode == INPUT_STDIN) fp = stdin; else { fp = fopen(pzDefFile, "r" FOPEN_TEXT_FLAG); if (fp == NULL) AG_CANT("open", pzDefFile); if (pfDepends != NULL) append_source_name(pzDefFile); } /* * Read until done... */ for (;;) { size_t rdct = fread((void*)pzData, (size_t)1, sizeLeft, fp); /* * IF we are done, */ if (rdct == 0) { /* * IF it is because we are at EOF, then break out * ELSE abend. */ if (feof(fp) || (in_mode == INPUT_STDIN)) break; AG_CANT("read", pzDefFile); } /* * Advance input pointer, decrease remaining count */ pzData += rdct; sizeLeft -= rdct; /* * See if there is any space left */ if (sizeLeft == 0) { tScanCtx* p; off_t dataOff; /* * IF it is a regular file, then we are done */ if (in_mode != INPUT_STDIN) break; /* * We have more data and we are out of space. * Try to reallocate our input buffer. */ dataSize += (sizeLeft = 0x1000); dataOff = pzData - pBaseCtx->pzData; p = AGREALOC((void*)pBaseCtx, dataSize+4+sizeof(*pBaseCtx), "expanded file buffer"); /* * The buffer may have moved. Set the data pointer at an * offset within the new buffer and make sure our base pointer * has been corrected as well. */ if (p != pBaseCtx) { p->pzScan = \ p->pzData = (char*)(p+1); pzData = p->pzData + dataOff; pBaseCtx = p; } } } if (pzData == pBaseCtx->pzData) AG_ABEND("No definition data were read"); *pzData = NUL; AGDUPSTR(pBaseCtx->pzCtxFname, pzDefFile, "def file name"); manageAllocatedData(pBaseCtx); manageAllocatedData((void*)pBaseCtx->pzCtxFname); /* * Close the input file, parse the data * and alphabetically sort the definition tree contents. */ if (in_mode != INPUT_STDIN) fclose(fp); pCurCtx = pBaseCtx; dp_run_fsm(); }