#include #include #include int main (int argc, char **argv) { void * handle; void (*SetRootDir) (char **string, int *len); void (*SetTempDir) (char **string, int *len); void (*EmbedCommand) (char ** string, int *len); char ch; char *error; char *root; char *tempDir; char *dll; char *library; char *buffer; char *logFile; char *cmd; char cmdLine[256]; int i; int len; int len1; int len2; int len3; int pos; int res; int tabs[20]; FILE *fp; FILE *log; if(argc != 4) { fprintf(stderr, "%s\n", "error must give root directory of BUGS, temp directory and library name as command line parameter"); exit(1); } root = argv[1], len1 = strlen(root); tempDir = argv[2]; len2 = strlen(tempDir); dll = argv[3]; len3 = strlen(dll); library = (char *) malloc((len1 + len3 + 1) * sizeof(char)); strcpy(library, root); strcat(library, dll); buffer = (char *) malloc((len2 + 12) * sizeof(char)); strcpy(buffer, tempDir); strcat(buffer, "/buffer.txt"); logFile = (char *) malloc((len2 + 13) * sizeof(char)); strcpy(logFile, tempDir); strcat(logFile, "/bugslog.txt"); handle = dlopen(library, RTLD_LAZY); if (!handle) { fputs(dlerror(), stderr); exit(1); } SetRootDir = dlsym(handle, "SetRootDir"); error = dlerror(); if(error != NULL) { fprintf(stderr, "%s\n", error); exit(1); } SetTempDir = dlsym(handle, "SetTempDir"); error = dlerror(); if(error != NULL) { fprintf(stderr, "%s\n", error); exit(1); } EmbedCommand = dlsym(handle, "EmbedCommand"); error = dlerror(); if(error != NULL) { fprintf(stderr, "%s\n", error); exit(1); } len = strlen(root); SetRootDir(&root, &len); len = strlen(tempDir); SetTempDir(&tempDir, &len); /* Initialize by executing Pascal procedures corresponding to INIT() */ len = 6; cmd = (char *) malloc ((len + 1) * sizeof(char)); strcpy(cmd, "INIT()"); EmbedCommand(&cmd, &len); free(cmd); log = fopen(logFile, "w"); tabs[0] = 1; tabs[1] = 20; i = 2; while (i < 20) { tabs[i] = 12; i = i + 1; } while (1){ printf("%s", "bugs: "); fprintf(log, "%s", "bugs: "); gets(cmdLine); fprintf(log, "%s", cmdLine); fprintf(log, "%c", '\n'); if (strcmp(cmdLine, "model.quit()") == 0) exit(0); len = strlen(cmdLine); cmd = (char *) malloc((len + 1) * sizeof(char)); strcpy(cmd, cmdLine); EmbedCommand(&cmd, &len); free(cmd); fp = fopen(buffer, "r"); ch = fgetc(fp); pos = 0; i = 0; while (ch != EOF) { pos = pos + 1; if (ch == '\n') { pos = 0; i = 0; } if (ch == '\t') { pos = pos % tabs[i]; while (pos < tabs[i]) { pos = pos + 1; printf("%c", ' '); fprintf(log, "%c", ' '); } i = i + 1; pos = 0; } if (ch != '\t') { printf("%c", ch); fprintf(log, "%c", ch); } ch = fgetc(fp); } fclose(fp); } fclose(log); dlclose(handle); return 0; }