/*-------------------------------------------------------------------------* * MAIN * * * *-------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { FILE *f; int i; Parse_Arguments(argc, argv); if (nb_arg == 0) { if (cmd_line) Pl_Fatal_Error("command-line is empty"); One_File(stdin); return 0; } for (i = 0; i < nb_arg; i++) { if (cmd_line) { One_Line(arg[i]); putchar('\n'); continue; } if ((f = fopen(arg[i], "rt")) == NULL) Pl_Fatal_Error("cannot open %s", arg[i]); One_File(f); fclose(f); } return 0; }
/*-------------------------------------------------------------------------* * PL_M_SYSTEM_TIME * * * * returns the system time used since the start of the process (in ms). * *-------------------------------------------------------------------------*/ PlLong Pl_M_System_Time(void) { PlLong system_time; #if defined(__unix__) && !defined(__CYGWIN__) struct rusage rsr_usage; getrusage(RUSAGE_SELF, &rsr_usage); system_time = (rsr_usage.ru_stime.tv_sec * 1000) + (rsr_usage.ru_stime.tv_usec / 1000); #elif defined(_WIN32) || defined(__CYGWIN__) FILETIME creat_t, exit_t, kernel_t, user_t; /* Success on Windows NT */ if (GetProcessTimes(GetCurrentProcess(), &creat_t, &exit_t, &kernel_t, &user_t)) system_time = (PlLong) (((__int64) kernel_t.dwHighDateTime << 32) + (__int64) kernel_t.dwLowDateTime) / 10000; else /* not implemented on Windows 95/98 */ system_time = 0; #else Pl_Fatal_Error("system time not available"); return 0; #endif return system_time - start_system_time; }
/*-------------------------------------------------------------------------* * CTRL_C_MANAGER * * * *-------------------------------------------------------------------------*/ static PlLong Ctrl_C_Manager(int from_callback) { StmInf *pstm = pl_stm_tbl[pl_stm_top_level_output]; PredInf *pred; int c; CodePtr to_execute; // Pl_Reset_Prolog_In_Signal(); Restore_Machine_Regs(buff_save_machine_regs); start: Pl_Stream_Printf(pstm, "\nProlog interruption (h for help) ? "); Pl_Stream_Flush(pstm); c = Pl_Stream_Get_Key(pl_stm_tbl[pl_stm_top_level_input], TRUE, FALSE); Pl_Stream_Putc('\n', pstm); switch (c) { case 'a': /* abort */ to_execute = Prolog_Predicate(ABORT, 0); if (from_callback) return (PlLong) to_execute; Pl_Execute_A_Continuation(to_execute); break; case 'b': /* break */ Pl_Call_Prolog(Prolog_Predicate(BREAK, 0)); goto start; break; case 'c': /* continue */ break; case 'e': /* exit */ Pl_Exit_With_Value(0); case 't': /* trace */ case 'd': /* debug */ if (SYS_VAR_DEBUGGER) { pred = Pl_Lookup_Pred(Pl_Create_Atom((c == 't') ? "trace" : "debug"), 0); if (pred == NULL) Pl_Fatal_Error(ERR_DEBUGGER_NOT_FOUND); /* should not occur */ Pl_Call_Prolog((CodePtr) pred->codep); break; } default: /* help */ Pl_Stream_Printf(pstm, " a abort b break\n"); Pl_Stream_Printf(pstm, " c continue e exit\n"); if (SYS_VAR_DEBUGGER) Pl_Stream_Printf(pstm, " d debug t trace\n"); Pl_Stream_Printf(pstm, " h/? help\n"); goto start; } return 0; }
/*-------------------------------------------------------------------------* * PL_QUERY_NEXT_SOLUTION * * * *-------------------------------------------------------------------------*/ int Pl_Query_Next_Solution(void) { if (query_stack_top == query_stack) Pl_Fatal_Error("Pl_Query_Next_Solution() but no query remaining"); pl_query_exception = pl_atom_void; return Pl_Call_Prolog_Next_Sol(pl_query_top_b); }
/*-------------------------------------------------------------------------* * PL_QUERY_END * * * *-------------------------------------------------------------------------*/ void Pl_Query_End(int op) { WamWord *query_b, *prev_b, *b; Bool recoverable; if (query_stack_top == query_stack) Pl_Fatal_Error("Pl_Query_End() but no query remaining"); query_b = *--query_stack_top; pl_query_top_b = query_stack_top[-1]; recoverable = (ALTB(query_b) == Prolog_Predicate(PL_QUERY_RECOVER_ALT, 0)); prev_b = BB(query_b); switch (op) { case PL_RECOVER: Assign_B(query_b); if (!recoverable) Pl_Fatal_Error("Pl_Query_End(PL_RECOVER) but unrecoverable query"); Pl_Delete_Choice_Point(0); /* remove recover chc-point */ break; case PL_CUT: Assign_B((recoverable) ? prev_b : query_b); break; default: /* case PL_KEEP_FOR_PROLOG */ if (recoverable) { if (B == query_b) Assign_B(prev_b); else for (b = B; b > query_b; b = BB(b)) /* unlink recover chc-point */ if (BB(b) == query_b) BB(b) = prev_b; } Pl_Keep_Rest_For_Prolog(query_b); } }
/*-------------------------------------------------------------------------* * PL_CALLOC_CHECK * * * *-------------------------------------------------------------------------*/ char * Pl_Calloc_Check(unsigned nb, unsigned size, char *src_file, int src_line) { char *m = calloc(nb, size); if (m == NULL) Pl_Fatal_Error(ERR_ALLOC_FAULT, "calloc", src_file, src_line); return m; }
/*-------------------------------------------------------------------------* * PL_STRDUP_CHECK * * * *-------------------------------------------------------------------------*/ char * Pl_Strdup_Check(char *str, char *src_file, int src_line) { char *s = strdup(str); if (s == NULL) Pl_Fatal_Error(ERR_ALLOC_FAULT, "strdup", src_file, src_line); return s; }
/*-------------------------------------------------------------------------* * PL_REALLOC_CHECK * * * *-------------------------------------------------------------------------*/ char * Pl_Realloc_Check(char *ptr, unsigned size, char *src_file, int src_line) { char *m = realloc(ptr, size); if (m == NULL) Pl_Fatal_Error(ERR_ALLOC_FAULT, "realloc", src_file, src_line); return m; }
/*-------------------------------------------------------------------------* * PL_QUERY_BEGIN * * * *-------------------------------------------------------------------------*/ void Pl_Query_Begin(Bool recoverable) { if (query_stack_top - query_stack >= QUERY_STACK_SIZE) Pl_Fatal_Error("too many nested Pl_Query_Start() (max: %d)", QUERY_STACK_SIZE); if (recoverable) Pl_Create_Choice_Point(Prolog_Predicate(PL_QUERY_RECOVER_ALT, 0), 0); }
/*-------------------------------------------------------------------------* * PL_M_REAL_TIME * * * * returns the real time used since the start of the process (in ms). * *-------------------------------------------------------------------------*/ PlLong Pl_M_Real_Time(void) { PlLong real_time; #if defined(__unix__) && !defined(__CYGWIN__) struct timeval tv; gettimeofday(&tv, NULL); real_time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); #elif defined(_WIN32) || defined(__CYGWIN__) real_time = (PlLong) ((double) clock() * 1000 / CLOCKS_PER_SEC); #else Pl_Fatal_Error("real time not available"); return 0; #endif return real_time - start_real_time; }
/*-------------------------------------------------------------------------* * PL_SR_OPEN_FILE_2 * * * *-------------------------------------------------------------------------*/ void Pl_SR_Open_File_2(WamWord file_name_word, WamWord from_stream_word) { SRInf *sr = cur_sr; int atom_file_name; int stm; SRFile *file; Bool from_stream = Pl_Rd_Boolean(from_stream_word); Bool master_file = (sr->file_top == NULL); StmInf *pstm, *pstm_tmp; if (sr->next_to_reread == NULL) { if (from_stream) { stm = Pl_Get_Stream_Or_Alias(file_name_word, STREAM_CHECK_INPUT); Pl_Check_Stream_Type(stm, TRUE, TRUE); atom_file_name = pl_stm_tbl[stm]->atom_file_name; } else { atom_file_name = Pl_Rd_Atom(file_name_word); if (strcmp(pl_atom_tbl[atom_file_name].name, "user") == 0) #if 0 stm = pl_stm_input; #else { stm = Pl_Add_Stream(0, (long) 0, pl_stm_tbl[pl_stm_input]->prop, NULL, NULL, NULL, NULL, NULL, NULL, NULL); *pl_stm_tbl[stm] = *pl_stm_tbl[pl_stm_input]; } #endif else { stm = Pl_Add_Stream_For_Stdio_File(pl_atom_tbl[atom_file_name].name, STREAM_MODE_READ, TRUE); if (stm < 0) { if (errno == ENOENT || errno == ENOTDIR) Pl_Err_Existence(pl_existence_source_sink, file_name_word); else Pl_Err_Permission(pl_permission_operation_open, pl_permission_type_source_sink, file_name_word); } } } pstm = pl_stm_tbl[stm]; file = (SRFile *) Malloc(sizeof(SRFile)); file->atom_file_name = atom_file_name; file->stm = stm; file->reposition = pstm->prop.reposition; if (!file->reposition) { file->tmp_path = Pl_M_Tempnam(NULL, NULL); file->tmp_stm = Pl_Add_Stream_For_Stdio_File(file->tmp_path, STREAM_MODE_WRITE, TRUE); if (file->tmp_stm < 0) Pl_Fatal_Error("cannot create tmp file %s in %s:%d", file->tmp_path, __FILE__, __LINE__); /* try to be similar to original file */ pstm_tmp = pl_stm_tbl[file->tmp_stm]; pstm_tmp->atom_file_name = atom_file_name; pstm_tmp->prop.eof_action = pstm->prop.eof_action; if (pstm_tmp->prop.buffering != pstm->prop.buffering) { pstm_tmp->prop.buffering = pstm->prop.buffering; Pl_Stdio_Set_Buffering((FILE *) pstm_tmp->file, pstm_tmp->prop.buffering); } Pl_Add_Mirror_To_Stream(stm, file->tmp_stm); } else { file->tmp_path = NULL; file->tmp_stm = -1; } file->next = NULL; if (sr->file_first == NULL) sr->file_first = file; else sr->file_last->next = file; sr->file_last = file; }
/*-------------------------------------------------------------------------* * PARSE_ARGUMENTS * * * *-------------------------------------------------------------------------*/ void Parse_Arguments(int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) { if (*argv[i] == '-' && argv[i][1] != '\0') { if (Check_Arg(i, "--encode")) { encode = 1; continue; } if (Check_Arg(i, "--relax")) { strict = 0; continue; } if (Check_Arg(i, "--printf")) { if (++i >= argc) Pl_Fatal_Error("format missing after -printf option"); format = argv[i]; continue; } if (Check_Arg(i, "--aux-father")) { decode_aux = 1; continue; } if (Check_Arg(i, "--aux-father2")) { decode_aux = 2; continue; } if (Check_Arg(i, "--cmd-line")) { cmd_line = 1; continue; } if (Check_Arg(i, "-H")) { encode = 1; cmd_line = 1; strict = 0; continue; } if (Check_Arg(i, "-P")) { encode = 0; cmd_line = 1; strict = 0; decode_aux = 0; continue; } if (Check_Arg(i, "--version")) { fprintf(stderr, "Prolog/Hexa Filter "); fprintf(stderr, " Daniel Diaz - 1998\n"); fprintf(stderr, "%s version %s\n", HEXGPLC, HEXGPLC_VERSION); exit(0); } if (Check_Arg(i, "-h") || Check_Arg(i, "--help")) { Display_Help(); exit(0); } Pl_Fatal_Error("unknown option %s - try %s --help", argv[i], HEXGPLC); } arg[nb_arg++] = argv[i]; } }