static int processCharacters (const wchar_t *characters, size_t count, wchar_t end, void *data) { if (opt_reformatText && count) { if (iswspace(characters[0])) if (!flushCharacters('\n', data)) return 0; { unsigned int spaces = !inputLength? 0: 1; size_t newLength = inputLength + spaces + count; if (newLength > inputSize) { size_t newSize = newLength | 0XFF; wchar_t *newBuffer = calloc(newSize, sizeof(*newBuffer)); if (!newBuffer) { noMemory(data); return 0; } wmemcpy(newBuffer, inputBuffer, inputLength); free(inputBuffer); inputBuffer = newBuffer; inputSize = newSize; } while (spaces) { inputBuffer[inputLength++] = WC_C(' '); spaces -= 1; } wmemcpy(&inputBuffer[inputLength], characters, count); inputLength += count; } if (end != '\n') { if (!flushCharacters(0, data)) return 0; if (!putCharacter(end, data)) return 0; } } else { if (!flushCharacters('\n', data)) return 0; if (!writeCharacters(characters, count, data)) return 0; if (!putCharacter(end, data)) return 0; } return 1; }
static char *lXtMalloc(size_t size, UndoBuffer *ub, Boolean *answer) { char *ret_val; ret_val = XtMalloc(size); if (!ret_val && emergencySpace) { XtFree(emergencySpace); emergencySpace = (char *) NULL; ret_val = XtMalloc(size); if (ret_val) { low_memory_warning(ub); } } if (!ret_val) { *answer = noMemory(ub); } return ret_val; }
static int writeCharacters (const wchar_t *inputLine, size_t inputLength, void *data) { const wchar_t *inputBuffer = inputLine; while (inputLength) { int inputCount = inputLength; int outputCount = outputWidth; if (!outputBuffer) { if (!(outputBuffer = malloc(outputWidth))) { noMemory(data); return 0; } } contractText(contractionTable, inputBuffer, &inputCount, outputBuffer, &outputCount, NULL, CTB_NO_CURSOR); if ((inputCount < inputLength) && outputExtend) { free(outputBuffer); outputBuffer = NULL; outputWidth <<= 1; } else { { int index; for (index=0; index<outputCount; index+=1) if (!putCell(outputBuffer[index], data)) return 0; } inputBuffer += inputCount; inputLength -= inputCount; if (inputLength) if (!putCharacter('\n', data)) return 0; } } return 1; }
static void insertNode( char *name, size_t lName, time_t mTime, int write, Froot *root ){ /** | Creates a new Fnode, to be inserted at the _end_ of the linked | list pointed to by root->firstNode (i.e., the list is organized | as a "queue", a.k.a. "FIFO" list): if a new node cannot be created, | an error message is printed and the program aborted. | If "lName" is bigger than zero, the file name is represented by the | first lName characters of "name"; otherwise by the whole string in | "name". **/ Fnode *pFN; /* The new node created by insertNode */ size_t sSize; /* Structure size */ sSize = sizeof(Fnode) + (lName == 0 ? strlen(name) : lName); if ((pFN = malloc(sSize)) == 0) { noMemory(); } pFN->mTime = mTime; pFN->write = write; pFN->next = 0; if (lName == 0) { strcpy(pFN->name, name); } else { strncpy(pFN->name, name, lName); pFN->name[lName] = '\0'; } if (root->lastNode == 0) { root->firstNode = pFN; } else { root->lastNode->next = pFN; } root->lastNode = pFN; }
static void clean( char *dirName ){ /** | Does the job for the directory "dirName". | | Builds a structure holding the TeX-related files, and does the | required cleanup; finally, removes the file structure. | If the list appended to "dirs" has been filled, recurse over the | tree of subdirectories. **/ Froot *teXTree; /* Root node of the TeX-related files */ Froot *dirs; /* Subdirectories in this directory */ Fnode *pFN; /* Running pointer over subdirectories */ if ((dirs = calloc(2, sizeof(Froot))) == 0) { noMemory(); } dirs->extension = "subs"; if ((teXTree = buildTree(dirName, dirs)) != 0) { if (output_level >= DEBUG) { printTree(teXTree); } examineTree(teXTree, dirName); releaseTree(teXTree); } for (pFN = dirs->firstNode; pFN != 0; pFN = pFN->next) { clean(pFN->name); } releaseTree(dirs); }
/* ============================================================================== * M A I N * ============================================================================*/ int main(int argc, const char** argv){ // Get where the input and output does come from std::istream* input = NULL; std::ostream* output = NULL; // Prefixes std::string varprefix(""); std::string funcprefix(""); // Are there any additional arguments specified? if(argc > 1){ int currentarg = 1; // PREFIXES // Variable prefixes if((strncmp(argv[currentarg], "-v", 3) == 0) || (strncmp(argv[currentarg], "--var", 6) == 0)){ if(argc > currentarg+1){ currentarg++; varprefix = argv[currentarg]; currentarg++; }else{ noMemory(); return 2; } } // Function prefixes if((strncmp(argv[currentarg], "-f", 3) == 0) || (strncmp(argv[currentarg], "--func", 7) == 0)){ if(argc > currentarg+1){ currentarg++; funcprefix = argv[currentarg]; currentarg++; }else{ invalidParam(); return 2; } } // INPUT // We print help and immediately exit if((strncmp(argv[currentarg], "-h", 3) == 0) || (strncmp(argv[currentarg], "--help", 7) == 0)){ help(argc, argv); return 0; // We use standard input (piping) }else if(strncmp(argv[currentarg], "-", 2) == 0){ currentarg++; // Use a simple input string }else if((strncmp(argv[currentarg], "--input", 8) == 0) || (strncmp(argv[currentarg], "-i", 3) == 0)){ if(argc > currentarg+1){ currentarg++; input = new std::stringstream(); if(!input){ noMemory(); return 1; } *static_cast<std::stringstream*>(input) << argv[currentarg]; currentarg++; }else{ invalidParam(); return 2; } // Nothing left, we use a file }else{ input = new std::ifstream(argv[currentarg]); if(!input){ noMemory(); return 1; } if(!static_cast<std::ifstream*>(input)->is_open() || static_cast<std::ifstream*>(input)->fail()){ std::cerr << "File \"" << currentarg << "\" does not exist or could not be read." << std::endl; return 3; } currentarg++; } // OUTPUT if(argc >= currentarg){ if(strncmp(argv[currentarg], "-", 2) == 0){ // Nothing currentarg++; // We use a file }else{ output = new std::ofstream(argv[currentarg]); if(!output){ noMemory(); return 1; } currentarg++; } } } // Use standard input/output if not specified if(input == NULL){ input = &std::cin; } if(output == NULL){ output = &std::cout; } // Test output AwS::Translator translator(*input, *output, varprefix, funcprefix); translator.translate(); // Clear up memory if standard input/output wasn't used if(input != &std::cin){ delete input; input = NULL; } if(output != &std::cout){ delete output; output = NULL; } // Done return 0; }
static Froot *buildTree( char *dirName, Froot *subDirs ){ /** | - Opens the required directory; | - allocates a structure to hold the names of the TeX-related files, | initialized from the global structure "protoTree"; | - starts a loop over all the files of the given directory. **/ DIR *pDir; /* Pointer returned from opendir() */ struct dirent *pDe; /* Pointer returned from readdir() */ Froot *teXTree; /* Root node of the TeX-related files */ if (output_level >= DEBUG) { printf("* Scanning directory \"%s\" - confirm = %c, recurse = %c, ", dirName, (confirm ? 'Y' : 'N'), (recurse ? 'Y' : 'N')), printf("keep = %c\n", (keep ? 'Y' : 'N')); printf("* Editor trailer: \"%s\"\n", bExt); puts("------------------------------Phase 1: directory scan"); } if ((pDir = opendir(dirName)) == 0) { fprintf(stderr, "%s: \"%s\" cannot be opened (or is not a directory)\n", programName, dirName); return 0; } if ((teXTree = malloc(sizeof(protoTree))) == 0) { noMemory(); } memcpy(teXTree, protoTree, sizeof(protoTree)); while ((pDe = readdir(pDir)) != 0) { char tName[FILENAME_MAX]; /* Fully qualified file name */ struct stat sStat; /* To be filled by stat(2) */ size_t len; /* Lenght of the current file name */ size_t last; /* Index of its last character */ char *pFe; /* Pointer to file extension */ /** | - Tests for empty inodes (already removed files); | - skips the . and .. (current and previous directory); | - tests the trailing part of the file name against the extension of | the backup files, to be always deleted. **/ if (pDe->d_ino == 0) continue; if (strcmp(pDe->d_name, ".") == 0) continue; if (strcmp(pDe->d_name, "..") == 0) continue; sprintf(tName, "%s/%s", dirName, pDe->d_name); len = strlen(pDe->d_name); last = len - 1; if (n_bExt != 0) { /* If 0, no backup files to delete */ int crit; /* What exceeds backup extensions */ crit = len - n_bExt; if (crit > 0 && strcmp(pDe->d_name + crit, bExt) == 0) { nuke(tName); continue; } } /** | If the file is a directory and the -r option has been given, stores | the directory name in the linked list pointed to by "subDirs", for | recursive calls. | | N.B.: if stat(2) fails, the file is skipped. **/ if (stat(tName, &sStat) != 0) { fprintf(stderr, "File \"%s", tName); perror("\""); continue; } if (S_ISDIR(sStat.st_mode) != 0) { if (output_level >= DEBUG) { printf("File %s - is a directory\n", pDe->d_name); } if (recurse) { insertNode(tName, 0, 0, 0, subDirs); } continue; } /** | If the file has an extension (the rightmost dot followed by at | least one character), and if that extension matches one of the | entries in teXTree[i].extension: stores the file name (with the | extension stripped) in the appropriate linked list, together with | its modification time. **/ if ((pFe = strrchr(pDe->d_name, '.')) != 0) { size_t nameLen; nameLen = pFe - pDe->d_name; if (nameLen < last) { Froot *pTT; if (output_level >= DEBUG) { printf("File %s - extension %s", pDe->d_name, pFe); } /** | Loop on recognized TeX-related file extensions **/ for (pTT = teXTree; pTT->extension != 0; pTT++) { if (strcmp(pFe, pTT->extension) == 0) { insertNode(pDe->d_name, nameLen, sStat.st_mtime, access(tName, W_OK), pTT); if (output_level >= DEBUG) { printf(" - inserted in tree"); } break; } } /* loop on known extensions */ if (output_level >= DEBUG) { puts(""); } } else { if (output_level >= VERBOSE) { printf("File %s - empty extension\n", pDe->d_name); } } } else { if (output_level >= DEBUG) { printf("File %s - without extension\n", pDe->d_name); } } } /* while (readdir) ... */ if (closedir(pDir) != 0) { fprintf(stderr, "Directory \"%s", dirName); perror("\""); } return teXTree; }
int main( int argc, char *argv[] ){ Froot *dirNames; /* To hold the directories to be scanned */ Fnode *pFN; /* Running pointer over directory names */ int to_bExt = FALSE; /* Flag "next parameter to bExt" */ /** | Scans the arguments appropriately; the required directories are stored | in the linked list starting at "dirNames". **/ programName = baseName(argv[0]); if ((dirNames = calloc(2, sizeof(Froot))) == 0) { noMemory(); } dirNames->extension = "argv"; while (--argc) { if ((*++argv)[0] == '-') { switch ( (*argv)[1] ) { case 'i': case 'I': confirm = TRUE; break; case 'r': case 'R': recurse = TRUE; break; case 'k': case 'K': keep = TRUE; break; case 'b': case 'B': to_bExt = TRUE; break; case 'q': case 'Q': output_level = QUIET; break; case 'v': case 'V': output_level = VERBOSE; break; case 'd': case 'D': output_level = DEBUG; break; case 'p': case 'P': pretend = TRUE; break; case 'o': case 'O': older = TRUE; break; default: syntax(); } } else { if (to_bExt) { strcpy(bExt, *argv); to_bExt = FALSE; } else { insertNode(*argv, 0, 0, 0, dirNames); } } } if (to_bExt) { syntax(); } n_bExt = strlen(bExt); /** | If no parameter has been given, clean the current directory **/ if ((pFN = dirNames->firstNode) == 0) { clean("."); } else { while (pFN != 0) { clean(pFN->name); pFN = pFN->next; } } releaseTree(dirNames); return EXIT_SUCCESS; }