/* Check for a trace file rollover event. */ void trace_rollover_check(void) { if (tracef == NULL || tracef_max == 0) return; /* See if we've reached a rollover point. */ if (tracef_size >= tracef_max) { char *alt_filename; char *new_header; #if defined(_WIN32) /*[*/ char *period; #endif /*]*/ /* Close up this file. */ wtrace("Trace rolled over\n"); fclose(tracef); tracef = NULL; /* Unlink and rename the alternate file. */ #if defined(_WIN32) /*[*/ period = strrchr(tracefile_name, '.'); if (period != CN) alt_filename = xs_buffer("%.*s-%s", period - tracefile_name, tracefile_name, period); else #endif /*]*/ alt_filename = xs_buffer("%s-", tracefile_name); (void) unlink(alt_filename); (void) rename(tracefile_name, alt_filename); Free(alt_filename); alt_filename = CN; tracef = fopen(tracefile_name, "w"); if (tracef == (FILE *)NULL) { popup_an_errno(errno, "%s", tracefile_name); return; } /* Initialize it. */ tracef_size = 0L; (void) SETLINEBUF(tracef); new_header = create_tracefile_header("rolled over"); do_ts = True; wtrace(new_header); Free(new_header); } }
/* Callback for "OK" button on trace popup */ static void tracefile_callback(Widget w, XtPointer client_data, XtPointer call_data _is_unused) { char *tfn = CN; int devfd = -1; #if defined(X3270_DISPLAY) /*[*/ int pipefd[2]; Boolean just_piped = False; #endif /*]*/ char *buf; #if defined(X3270_DISPLAY) /*[*/ if (w) tfn = XawDialogGetValueString((Widget)client_data); else #endif /*]*/ tfn = (char *)client_data; tfn = do_subst(tfn, DS_VARS | DS_TILDE | DS_UNIQUE); if (strchr(tfn, '\'') || ((int)strlen(tfn) > 0 && tfn[strlen(tfn)-1] == '\\')) { popup_an_error("Illegal file name: %s", tfn); Free(tfn); goto done; } tracef_max = 0; if (!strcmp(tfn, "stdout")) { tracef = stdout; } else { #if defined(X3270_DISPLAY) /*[*/ FILE *pipefile = NULL; if (!strcmp(tfn, "none") || !tfn[0]) { just_piped = True; if (!appres.trace_monitor) { popup_an_error("Must specify a trace file " "name"); free(tfn); goto done; } } if (appres.trace_monitor) { if (pipe(pipefd) < 0) { popup_an_errno(errno, "pipe() failed"); Free(tfn); goto done; } pipefile = fdopen(pipefd[1], "w"); if (pipefile == NULL) { popup_an_errno(errno, "fdopen() failed"); (void) close(pipefd[0]); (void) close(pipefd[1]); Free(tfn); goto done; } (void) SETLINEBUF(pipefile); (void) fcntl(pipefd[1], F_SETFD, 1); } if (just_piped) { tracef = pipefile; } else #endif /*]*/ { Boolean append = False; #if defined(X3270_DISPLAY) /*[*/ tracef_pipe = pipefile; #endif /*]*/ /* Get the trace file maximum. */ get_tracef_max(); /* Open and configure the file. */ if ((devfd = get_devfd(tfn)) >= 0) tracef = fdopen(dup(devfd), "a"); else if (!strncmp(tfn, ">>", 2)) { append = True; tracef = fopen(tfn + 2, "a"); } else tracef = fopen(tfn, "w"); if (tracef == (FILE *)NULL) { popup_an_errno(errno, "%s", tfn); #if defined(X3270_DISPLAY) /*[*/ fclose(tracef_pipe); (void) close(pipefd[0]); (void) close(pipefd[1]); #endif /*]*/ Free(tfn); goto done; } tracef_size = ftello(tracef); Replace(tracefile_name, NewString(append? tfn + 2: tfn)); (void) SETLINEBUF(tracef); #if !defined(_WIN32) /*[*/ (void) fcntl(fileno(tracef), F_SETFD, 1); #endif /*]*/ } } #if defined(X3270_DISPLAY) /*[*/ /* Start the monitor window */ if (tracef != stdout && appres.trace_monitor) { switch (tracewindow_pid = fork_child()) { case 0: /* child process */ { char cmd[64]; (void) snprintf(cmd, sizeof(cmd), "cat <&%d", pipefd[0]); (void) execlp("xterm", "xterm", "-title", just_piped? "trace": tfn, "-sb", "-e", "/bin/sh", "-c", cmd, CN); } (void) perror("exec(xterm) failed"); _exit(1); default: /* parent */ (void) close(pipefd[0]); ++children; break; case -1: /* error */ popup_an_errno(errno, "fork() failed"); break; } } #endif /*]*/ #if defined(_WIN32) && defined(C3270) /*[*/ /* Start the monitor window. */ if (tracef != stdout && appres.trace_monitor && is_installed) { STARTUPINFO startupinfo; PROCESS_INFORMATION process_information; char *path; char *args; (void) memset(&startupinfo, '\0', sizeof(STARTUPINFO)); startupinfo.cb = sizeof(STARTUPINFO); startupinfo.lpTitle = tfn; (void) memset(&process_information, '\0', sizeof(PROCESS_INFORMATION)); path = xs_buffer("%scatf.exe", instdir); args = xs_buffer("\"%scatf.exe\" \"%s\"", instdir, tfn); if (CreateProcess( path, args, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startupinfo, &process_information) == 0) { popup_an_error("CreateProcess(%s) failed: %s", path, win32_strerror(GetLastError())); Free(path); Free(args); } else { Free(path); Free(args); tracewindow_handle = process_information.hProcess; CloseHandle(process_information.hThread); } } #endif /*]*/ Free(tfn); /* We're really tracing, turn the flag on. */ appres.toggle[trace_reason].value = True; appres.toggle[trace_reason].changed = True; menubar_retoggle(&appres.toggle[trace_reason], trace_reason); /* Display current status. */ buf = create_tracefile_header("started"); do_ts = False; wtrace("%s", buf); Free(buf); done: #if defined(X3270_DISPLAY) /*[*/ if (w) XtPopdown(trace_shell); #endif /*]*/ return; }
/* Start tracing, using the specified file. */ void tracefile_ok(const char *tfn) { int devfd = -1; #if !defined(_WIN32) /*[*/ int pipefd[2]; bool just_piped = false; #endif /*]*/ char *buf; char *stfn; stfn = do_subst(tfn, DS_VARS | DS_TILDE | DS_UNIQUE); if (strchr(stfn, '\'') || ((int)strlen(stfn) > 0 && stfn[strlen(stfn)-1] == '\\')) { popup_an_error("Illegal file name: %s", tfn); Free(stfn); goto done; } tracef_max = 0; if (!strcmp(stfn, "stdout")) { tracef = stdout; } else { #if !defined(_WIN32) /*[*/ FILE *pipefile = NULL; if (!strcmp(stfn, "none") || !stfn[0]) { just_piped = true; if (!appres.trace_monitor) { popup_an_error("Must specify a trace file name"); free(stfn); goto done; } } if (appres.trace_monitor) { if (pipe(pipefd) < 0) { popup_an_errno(errno, "pipe() failed"); Free(stfn); goto done; } pipefile = fdopen(pipefd[1], "w"); if (pipefile == NULL) { popup_an_errno(errno, "fdopen() failed"); (void) close(pipefd[0]); (void) close(pipefd[1]); Free(stfn); goto done; } (void) SETLINEBUF(pipefile); (void) fcntl(pipefd[1], F_SETFD, 1); } if (just_piped) { tracef = pipefile; } else #endif /*]*/ { bool append = false; #if !defined(_WIN32) /*[*/ tracef_pipe = pipefile; #endif /*]*/ /* Get the trace file maximum. */ get_tracef_max(); /* Open and configure the file. */ if ((devfd = get_devfd(stfn)) >= 0) tracef = fdopen(dup(devfd), "a"); else if (!strncmp(stfn, ">>", 2)) { append = true; tracef = fopen(stfn + 2, "a"); } else { tracef = fopen(stfn, "w"); } if (tracef == NULL) { popup_an_errno(errno, "%s", stfn); #if !defined(_WIN32) /*[*/ fclose(tracef_pipe); (void) close(pipefd[0]); (void) close(pipefd[1]); #endif /*]*/ Free(stfn); goto done; } tracef_size = ftello(tracef); Replace(tracefile_name, NewString(append? stfn + 2: stfn)); (void) SETLINEBUF(tracef); #if !defined(_WIN32) /*[*/ (void) fcntl(fileno(tracef), F_SETFD, 1); #endif /*]*/ } } /* Start the monitor window. */ if (tracef != stdout && appres.trace_monitor && product_has_display()) { #if !defined(_WIN32) /*[*/ start_trace_window(just_piped? NULL: stfn, pipefd); #else /*][*/ if (windirs_flags && GD_CATF) { start_trace_window(stfn); } #endif /*]*/ } Free(stfn); /* We're really tracing, turn the flag on. */ set_toggle(trace_reason, true); menubar_retoggle(trace_reason); /* Display current status. */ buf = create_tracefile_header("started"); wtrace(false, "%s", buf); Free(buf); done: return; }