FILE * win_fopen(const char *filename, const char *mode) { FILE * file; LPWSTR wfilename = UnicodeText(filename, encoding); LPWSTR wmode = UnicodeText(mode, encoding); file = _wfopen(wfilename, wmode); free(wfilename); free(wmode); return file; }
void close_printer(FILE *outfile) { LPTSTR fname; HWND hwnd; TCHAR title[100]; #ifdef UNICODE fname = UnicodeText(win_prntmp, S_ENC_DEFAULT); #else fname = win_prntmp; #endif fclose(outfile); #ifndef WGP_CONSOLE hwnd = textwin.hWndParent; #else hwnd = GetDesktopWindow(); #endif if (term->name != NULL) wsprintf(title, TEXT("gnuplot graph (%hs)"), term->name); else _tcscpy(title, TEXT("gnuplot graph")); DumpPrinter(hwnd, title, fname); #ifdef UNICODE free(fname); #endif }
FILE * fake_popen(const char * command, const char * type) { FILE * f = NULL; char tmppath[MAX_PATH]; char tmpfile[MAX_PATH]; DWORD ret; if (type == NULL) return NULL; pipe_type = NUL; if (pipe_filename != NULL) free(pipe_filename); /* Random temp file name in %TEMP% */ ret = GetTempPathA(sizeof(tmppath), tmppath); if ((ret == 0) || (ret > sizeof(tmppath))) return NULL; ret = GetTempFileNameA(tmppath, "gpp", 0, tmpfile); if (ret == 0) return NULL; pipe_filename = gp_strdup(tmpfile); if (*type == 'r') { char * cmd; int rc; LPWSTR wcmd; pipe_type = *type; /* Execute command with redirection of stdout to temporary file. */ cmd = (char *) malloc(strlen(command) + strlen(pipe_filename) + 5); sprintf(cmd, "%s > %s", command, pipe_filename); wcmd = UnicodeText(cmd, encoding); rc = _wsystem(wcmd); free(wcmd); free(cmd); /* Now open temporary file. */ /* system() returns 1 if the command could not be executed. */ if (rc != 1) { f = fopen(pipe_filename, "r"); } else { remove(pipe_filename); free(pipe_filename); pipe_filename = NULL; errno = EINVAL; } } else if (*type == 'w') { pipe_type = *type; /* Write output to temporary file and handle the rest in fake_pclose. */ if (type[1] == 'b') int_error(NO_CARET, "Could not execute pipe '%s'. Writing to binary pipes is not supported.", command); else f = fopen(pipe_filename, "w"); pipe_command = gp_strdup(command); } return f; }
int Pause(LPSTR str) { int rc; pausewin.Message = UnicodeText(str, encoding); rc = PauseBox(&pausewin) == IDOK; free(pausewin.Message); return rc; }
static int ConsolePutS(const char *str) { LPWSTR wstr = UnicodeText(str, encoding); HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); WriteConsoleW(h, wstr, wcslen(wstr), NULL, NULL); //fputws(wstr, stdout); free(wstr); return 0; }
int fake_pclose(FILE *stream) { int rc = 0; if (!stream) return ECHILD; /* Close temporary file */ fclose(stream); /* Finally, execute command with redirected stdin. */ if (pipe_type == 'w') { char * cmd; LPWSTR wcmd; cmd = (char *) gp_alloc(strlen(pipe_command) + strlen(pipe_filename) + 10, "fake_pclose"); /* FIXME: this won't work for binary data. We need a proper `cat` replacement. */ sprintf(cmd, "type %s | %s", pipe_filename, pipe_command); wcmd = UnicodeText(cmd, encoding); rc = _wsystem(wcmd); free(cmd); free(wcmd); } /* Delete temp file again. */ if (pipe_filename) { remove(pipe_filename); errno = 0; free(pipe_filename); pipe_filename = NULL; } if (pipe_command) { /* system() returns 255 if the command could not be executed. The real popen would have returned an error already. */ if (rc == 255) int_error(NO_CARET, "Could not execute pipe '%s'.", pipe_command); free(pipe_command); } return rc; }
int main(int argc, char **argv) #endif { LPTSTR tail; #ifdef WGP_CONSOLE HINSTANCE hInstance = GetModuleHandle(NULL), hPrevInstance = NULL; #else int i; #endif #ifndef WGP_CONSOLE # if defined( __MINGW32__) && !defined(_W64) # define argc _argc # define argv _argv # else /* MSVC, WATCOM, MINGW-W64 */ # define argc __argc # define argv __argv # endif #endif /* WGP_CONSOLE */ szModuleName = (LPTSTR) malloc((MAXSTR + 1) * sizeof(TCHAR)); CheckMemory(szModuleName); /* get path to gnuplot executable */ GetModuleFileName(hInstance, szModuleName, MAXSTR); if ((tail = _tcsrchr(szModuleName, '\\')) != NULL) { tail++; *tail = 0; } szModuleName = (LPTSTR) realloc(szModuleName, (_tcslen(szModuleName) + 1) * sizeof(TCHAR)); CheckMemory(szModuleName); if (_tcslen(szModuleName) >= 5 && _tcsnicmp(&szModuleName[_tcslen(szModuleName)-5], TEXT("\\bin\\"), 5) == 0) { size_t len = _tcslen(szModuleName) - 4; szPackageDir = (LPTSTR) malloc((len + 1) * sizeof(TCHAR)); CheckMemory(szPackageDir); _tcsncpy(szPackageDir, szModuleName, len); szPackageDir[len] = NUL; } else { szPackageDir = szModuleName; } #ifndef WGP_CONSOLE textwin.hInstance = hInstance; textwin.hPrevInstance = hPrevInstance; textwin.nCmdShow = nCmdShow; textwin.Title = L"gnuplot"; #endif /* create structure of first graph window */ graphwin = (LPGW) calloc(1, sizeof(GW)); listgraphs = graphwin; /* locate ini file */ { char * inifile; #ifdef UNICODE LPWSTR winifile; #endif get_user_env(); /* this hasn't been called yet */ inifile = gp_strdup("~\\wgnuplot.ini"); gp_expand_tilde(&inifile); /* if tilde expansion fails use current directory as default - that was the previous default behaviour */ if (inifile[0] == '~') { free(inifile); inifile = "wgnuplot.ini"; } #ifdef UNICODE graphwin->IniFile = winifile = UnicodeText(inifile, S_ENC_DEFAULT); #else graphwin->IniFile = inifile; #endif #ifndef WGP_CONSOLE textwin.IniFile = graphwin->IniFile; #endif ReadMainIni(graphwin->IniFile, TEXT("WGNUPLOT")); } #ifndef WGP_CONSOLE textwin.IniSection = TEXT("WGNUPLOT"); textwin.DragPre = L"load '"; textwin.DragPost = L"'\n"; textwin.lpmw = &menuwin; textwin.ScreenSize.x = 80; textwin.ScreenSize.y = 80; textwin.KeyBufSize = 2048; textwin.CursorFlag = 1; /* scroll to cursor after \n & \r */ textwin.shutdown = MakeProcInstance((FARPROC)ShutDown, hInstance); textwin.AboutText = (LPTSTR) malloc(1024 * sizeof(TCHAR)); CheckMemory(textwin.AboutText); wsprintf(textwin.AboutText, TEXT("Version %hs patchlevel %hs\n") \ TEXT("last modified %hs\n") \ TEXT("%hs\n%hs, %hs and many others\n") \ TEXT("gnuplot home: http://www.gnuplot.info\n"), gnuplot_version, gnuplot_patchlevel, gnuplot_date, gnuplot_copyright, authors[1], authors[0]); textwin.AboutText = (LPTSTR) realloc(textwin.AboutText, (_tcslen(textwin.AboutText) + 1) * sizeof(TCHAR)); CheckMemory(textwin.AboutText); menuwin.szMenuName = szMenuName; #endif pausewin.hInstance = hInstance; pausewin.hPrevInstance = hPrevInstance; pausewin.Title = L"gnuplot pause"; graphwin->hInstance = hInstance; graphwin->hPrevInstance = hPrevInstance; #ifdef WGP_CONSOLE graphwin->lptw = NULL; #else graphwin->lptw = &textwin; #endif /* init common controls */ { INITCOMMONCONTROLSEX initCtrls; initCtrls.dwSize = sizeof(INITCOMMONCONTROLSEX); initCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&initCtrls); } #ifndef WGP_CONSOLE if (TextInit(&textwin)) gp_exit(EXIT_FAILURE); textwin.hIcon = LoadIcon(hInstance, TEXT("TEXTICON")); SetClassLongPtr(textwin.hWndParent, GCLP_HICON, (LONG_PTR)textwin.hIcon); /* Note: we want to know whether this is an interactive session so that we can * decide whether or not to write status information to stderr. The old test * for this was to see if (argc > 1) but the addition of optional command line * switches broke this. What we really wanted to know was whether any of the * command line arguments are file names or an explicit in-line "-e command". * (This is a copy of a code snippet from plot.c) */ for (i = 1; i < argc; i++) { if (!_stricmp(argv[i], "/noend")) continue; if ((argv[i][0] != '-') || (argv[i][1] == 'e')) { interactive = FALSE; break; } } if (interactive) ShowWindow(textwin.hWndParent, textwin.nCmdShow); if (IsIconic(textwin.hWndParent)) { /* update icon */ RECT rect; GetClientRect(textwin.hWndParent, (LPRECT) &rect); InvalidateRect(textwin.hWndParent, (LPRECT) &rect, 1); UpdateWindow(textwin.hWndParent); } # ifndef __WATCOMC__ /* Finally, also redirect C++ standard output streams. */ RedirectOutputStreams(TRUE); # endif #else /* !WGP_CONSOLE */ # ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING # define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004 # endif { /* Enable Windows 10 Console Virtual Terminal Sequences */ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD mode; GetConsoleMode(handle, &mode); SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); } #endif gp_atexit(WinExit); if (!_isatty(_fileno(stdin))) _setmode(_fileno(stdin), O_BINARY); gnu_main(argc, argv); /* First chance to close help system for console gnuplot, second for wgnuplot */ WinCloseHelp(); gp_exit_cleanup(); return 0; }