浏览代码

Add --debug option; Reimplement fixes for Term on Windows

Arthur Yefimov 3 年之前
父节点
当前提交
be5784a55b
共有 5 个文件被更改,包括 54 次插入17 次删除
  1. 5 0
      src/Builder.Mod
  2. 10 0
      src/Config.Mod
  3. 1 0
      src/Fob.Mod
  4. 1 0
      src/FreeOberon.Mod
  5. 37 17
      src/term/term_win32.c

+ 5 - 0
src/Builder.Mod

@@ -221,6 +221,11 @@ BEGIN ok := TRUE;
       END
     END;
     Utf8.Encode(cmd, q);
+
+    IF Config.debug THEN
+      Out.String('Term.RunProcess "'); Out.String(cmd); Out.Char('"'); Out.Ln
+    END;
+
     success := (Term.RunProcess(q, buf, bufLen, len, err) # 0) & (err = 0);
     IF ~success & (onError # NIL) THEN
       z := ''; line := 0; col := 0;

+ 10 - 0
src/Config.Mod

@@ -6,4 +6,14 @@ CONST
 
   version* = '1.1.0-alpha.6';
   year* = 2022;
+
+VAR
+  debug*: BOOLEAN;
+
+PROCEDURE SetDebug*(deb: BOOLEAN);
+BEGIN debug := deb
+END SetDebug;
+
+BEGIN
+  debug := FALSE
 END Config.

+ 1 - 0
src/Fob.Mod

@@ -60,6 +60,7 @@ BEGIN i := 1; lang := 'en';
   WHILE i <= Args.Count DO
     Args.Get(i, s);
     IF s = '--lang' THEN Args.Get(i + 1, lang); INC(i)
+    ELSIF s = '--debug' THEN Config.SetDebug(TRUE)
     ELSE Strings.Copy(s, mainFname)
     END;
     INC(i)

+ 1 - 0
src/FreeOberon.Mod

@@ -928,6 +928,7 @@ BEGIN fs := TRUE; sw := FALSE; i := 1; nofnames := 0; w := defW; h := defH;
   WHILE i <= Args.Count DO Args.Get(i, s);
     IF s = '--window' THEN fs := FALSE
     ELSIF s = '--software' THEN sw := TRUE
+    ELSIF s = '--debug' THEN Config.SetDebug(TRUE)
     ELSIF s = '--size' THEN
       IF i # Args.Count THEN
         INC(i); Args.Get(i, s); ParseSize(s, w, h)

+ 37 - 17
src/term/term_win32.c

@@ -113,23 +113,31 @@ int StartProcessIn(char *process, char *dir) {
   saAttr.lpSecurityDescriptor = NULL;
 
   // Create a pipe for the child process's STDOUT.
-  if (!MyCreatePipeEx(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0, FILE_FLAG_OVERLAPPED, 0) )
+  if (!MyCreatePipeEx(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0, FILE_FLAG_OVERLAPPED, 0)) {
     ErrorExit(TEXT("StdoutRd CreatePipe"));
+    return 0;
+  }
 
   // Ensure the read handle to the pipe for STDOUT is not inherited.
-  if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) )
+  if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0)) {
     ErrorExit(TEXT("Stdout SetHandleInformation"));
+    return 0;
+  }
 
   // Create a pipe for the child process's STDIN.
-  if (!MyCreatePipeEx(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0, FILE_FLAG_OVERLAPPED, 0))
+  if (!MyCreatePipeEx(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0, FILE_FLAG_OVERLAPPED, 0)) {
     ErrorExit(TEXT("Stdin CreatePipe"));
+    return 0;
+  }
 
   // Ensure the write handle to the pipe for STDIN is not inherited.
-  if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
+  if (!SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0)) {
     ErrorExit(TEXT("Stdin SetHandleInformation"));
+    return 0;
+  }
 
   ZeroMemory(&oOverlap, sizeof(oOverlap));
- 
+
   // ACTUAL START PROCESS
 
   //TCHAR szCmdline[]=TEXT(process);
@@ -150,11 +158,11 @@ int StartProcessIn(char *process, char *dir) {
   siStartInfo.wShowWindow = SW_HIDE;
 
   /* Environment variables */
-  
+
   LPTSTR pszOldVal, childEnvPath;
   BOOL envPathExists = TRUE;
   DWORD pathSize, dwRet, dwErr;
-  
+
   // Save original value of the PATH environment variable
   pszOldVal = (LPTSTR)malloc(BUFSIZE * sizeof(TCHAR));
   pathSize = GetEnvironmentVariable(TEXT("PATH"), pszOldVal, BUFSIZE);
@@ -167,31 +175,42 @@ int StartProcessIn(char *process, char *dir) {
     pszOldVal = (LPTSTR)realloc(pszOldVal, pathSize * sizeof(TCHAR));
     if (pszOldVal == NULL) {
       ErrorExit(TEXT("realloc out memory"));
+      return 0;
     }
     dwRet = GetEnvironmentVariable(TEXT("PATH"), pszOldVal, pathSize);
     if (!dwRet) {
+      free(pszOldVal);
       ErrorExit(TEXT("GetEnvironmentVariable failed"));
+      return 0;
     }
   }
-  
+
   // Determine the value of environment variable PATH for the child process
-  childEnvPath = (LPTSTR)malloc((pathSize + 3) * sizeof(TCHAR));
+  childEnvPath = (LPTSTR)malloc((pathSize + 4) * sizeof(TCHAR));
   if (childEnvPath == NULL) {
+    free(pszOldVal);
     ErrorExit(TEXT("malloc out memory"));
+    return 0;
   }
   childEnvPath[0] = '.';
   childEnvPath[1] = '.';
   childEnvPath[2] = ';';
-  memcpy(childEnvPath + 3, pszOldVal, pathSize + 1);
-  
+  memcpy(childEnvPath + 3, pszOldVal, (pathSize + 1) * sizeof(TCHAR));
+
   // Set value of PATH for child process to inherit
   if (!SetEnvironmentVariable(TEXT("PATH"), childEnvPath)) {
+    free(childEnvPath);
+    free(pszOldVal);
     ErrorExit(TEXT("SetEnvironmentVariable 1 failed"));
+    return 0;
   }
 
+  free(childEnvPath);
+  free(pszOldVal);
+
   // Create the child process.
   bSuccess = CreateProcess(NULL,
-    process, //szCmdline,    // command line
+    process, // command line
     NULL,    // process security attributes
     NULL,    // primary thread security attributes
     TRUE,    // handles are inherited
@@ -200,14 +219,15 @@ int StartProcessIn(char *process, char *dir) {
     dir,          // current directory of the process
     &siStartInfo, // STARTUPINFO pointer
     &piProcInfo); // receives PROCESS_INFORMATION
-  
-  // If an error occurs, exit the application.
+
+  // Show error but do not exit this procedure
   if (!bSuccess) ErrorExit(TEXT("CreateProcess"));
-  
+
   // Restore original value of PATH
   if (envPathExists) {
     if (!SetEnvironmentVariable(TEXT("PATH"), pszOldVal)) {
       ErrorExit(TEXT("SetEnvironmentVariable 2 failed"));
+      return 0;
     }
   } else {
     SetEnvironmentVariable(TEXT("PATH"), NULL);
@@ -332,7 +352,7 @@ void ErrorExit(PTSTR lpszFunction) {
     0, NULL);
 
   lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
-    (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
+    (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
 
   snprintf((LPTSTR)lpDisplayBuf,
     LocalSize(lpDisplayBuf) / sizeof(TCHAR),
@@ -343,7 +363,7 @@ void ErrorExit(PTSTR lpszFunction) {
 
   LocalFree(lpMsgBuf);
   LocalFree(lpDisplayBuf);
-  ExitProcess(1);
+  //ExitProcess(1);
 }
 
 int RunProcessIn(char *cmd, char *dir, char *buf, int limit, int *len, int *err) {