| 1 | // LSP Process wrapper - platform independent implementation |
| 2 | // Handles process creation, pipes, and I/O for LSP servers |
| 3 | |
| 4 | #ifdef _WIN32 |
| 5 | // Windows implementation |
| 6 | #include <windows.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | typedef struct { |
| 12 | HANDLE hProcess; |
| 13 | DWORD pid; |
| 14 | HANDLE stdin_write; |
| 15 | HANDLE stdout_read; |
| 16 | HANDLE stderr_read; |
| 17 | } lsp_process_t; |
| 18 | |
| 19 | // Get temp directory path |
| 20 | static const char* get_temp_dir(void) { |
| 21 | static char temp_path[MAX_PATH]; |
| 22 | GetTempPathA(MAX_PATH, temp_path); |
| 23 | return temp_path; |
| 24 | } |
| 25 | |
| 26 | // Start an LSP server process |
| 27 | lsp_process_t* lsp_start_server(const char* command) { |
| 28 | lsp_process_t* proc = (lsp_process_t*)malloc(sizeof(lsp_process_t)); |
| 29 | if (!proc) return NULL; |
| 30 | |
| 31 | memset(proc, 0, sizeof(lsp_process_t)); |
| 32 | |
| 33 | // Create pipes for stdin, stdout, stderr |
| 34 | HANDLE stdin_read, stdin_write; |
| 35 | HANDLE stdout_read, stdout_write; |
| 36 | HANDLE stderr_read, stderr_write; |
| 37 | |
| 38 | SECURITY_ATTRIBUTES sa; |
| 39 | sa.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 40 | sa.bInheritHandle = TRUE; |
| 41 | sa.lpSecurityDescriptor = NULL; |
| 42 | |
| 43 | if (!CreatePipe(&stdin_read, &stdin_write, &sa, 0)) { |
| 44 | free(proc); |
| 45 | return NULL; |
| 46 | } |
| 47 | // Don't inherit the write end of stdin |
| 48 | SetHandleInformation(stdin_write, HANDLE_FLAG_INHERIT, 0); |
| 49 | |
| 50 | if (!CreatePipe(&stdout_read, &stdout_write, &sa, 0)) { |
| 51 | CloseHandle(stdin_read); |
| 52 | CloseHandle(stdin_write); |
| 53 | free(proc); |
| 54 | return NULL; |
| 55 | } |
| 56 | // Don't inherit the read end of stdout |
| 57 | SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0); |
| 58 | |
| 59 | if (!CreatePipe(&stderr_read, &stderr_write, &sa, 0)) { |
| 60 | CloseHandle(stdin_read); |
| 61 | CloseHandle(stdin_write); |
| 62 | CloseHandle(stdout_read); |
| 63 | CloseHandle(stdout_write); |
| 64 | free(proc); |
| 65 | return NULL; |
| 66 | } |
| 67 | // Don't inherit the read end of stderr |
| 68 | SetHandleInformation(stderr_read, HANDLE_FLAG_INHERIT, 0); |
| 69 | |
| 70 | // Set up process startup info |
| 71 | STARTUPINFOA si; |
| 72 | PROCESS_INFORMATION pi; |
| 73 | |
| 74 | ZeroMemory(&si, sizeof(si)); |
| 75 | si.cb = sizeof(si); |
| 76 | si.hStdInput = stdin_read; |
| 77 | si.hStdOutput = stdout_write; |
| 78 | si.hStdError = stderr_write; |
| 79 | si.dwFlags |= STARTF_USESTDHANDLES; |
| 80 | |
| 81 | ZeroMemory(&pi, sizeof(pi)); |
| 82 | |
| 83 | // Build command line for cmd.exe |
| 84 | char cmd_line[2048]; |
| 85 | snprintf(cmd_line, sizeof(cmd_line), "cmd.exe /c %s", command); |
| 86 | |
| 87 | // Create the process |
| 88 | if (!CreateProcessA( |
| 89 | NULL, // Application name (use command line) |
| 90 | cmd_line, // Command line |
| 91 | NULL, // Process security attributes |
| 92 | NULL, // Thread security attributes |
| 93 | TRUE, // Inherit handles |
| 94 | CREATE_NO_WINDOW, // Don't create a console window |
| 95 | NULL, // Environment (inherit) |
| 96 | NULL, // Current directory (inherit) |
| 97 | &si, // Startup info |
| 98 | &pi // Process info |
| 99 | )) { |
| 100 | CloseHandle(stdin_read); |
| 101 | CloseHandle(stdin_write); |
| 102 | CloseHandle(stdout_read); |
| 103 | CloseHandle(stdout_write); |
| 104 | CloseHandle(stderr_read); |
| 105 | CloseHandle(stderr_write); |
| 106 | free(proc); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | // Close handles we don't need |
| 111 | CloseHandle(stdin_read); |
| 112 | CloseHandle(stdout_write); |
| 113 | CloseHandle(stderr_write); |
| 114 | CloseHandle(pi.hThread); |
| 115 | |
| 116 | proc->hProcess = pi.hProcess; |
| 117 | proc->pid = pi.dwProcessId; |
| 118 | proc->stdin_write = stdin_write; |
| 119 | proc->stdout_read = stdout_read; |
| 120 | proc->stderr_read = stderr_read; |
| 121 | |
| 122 | // Make stdout and stderr non-blocking by using overlapped I/O |
| 123 | // For simplicity, we'll use PeekNamedPipe for non-blocking reads |
| 124 | |
| 125 | // Debug log |
| 126 | { |
| 127 | char log_path[MAX_PATH]; |
| 128 | snprintf(log_path, sizeof(log_path), "%sfac_lsp_read.log", get_temp_dir()); |
| 129 | FILE* dbg = fopen(log_path, "a"); |
| 130 | if (dbg) { |
| 131 | fprintf(dbg, "Started LSP server: pid=%lu, cmd=%s\n", |
| 132 | (unsigned long)proc->pid, command); |
| 133 | fclose(dbg); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return proc; |
| 138 | } |
| 139 | |
| 140 | // Send data to LSP server |
| 141 | int lsp_send_message(lsp_process_t* proc, const char* message, int len) { |
| 142 | if (!proc || proc->stdin_write == INVALID_HANDLE_VALUE) return -1; |
| 143 | |
| 144 | DWORD written; |
| 145 | if (!WriteFile(proc->stdin_write, message, (DWORD)len, &written, NULL)) { |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | return (int)written; |
| 150 | } |
| 151 | |
| 152 | // Read data from LSP server (non-blocking) |
| 153 | int lsp_read_message(lsp_process_t* proc, char* buffer, int max_len) { |
| 154 | if (!proc || proc->stdout_read == INVALID_HANDLE_VALUE) return -1; |
| 155 | |
| 156 | // Check if data is available |
| 157 | DWORD available = 0; |
| 158 | if (!PeekNamedPipe(proc->stdout_read, NULL, 0, NULL, &available, NULL)) { |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | if (available == 0) { |
| 163 | return 0; // No data available |
| 164 | } |
| 165 | |
| 166 | // Read available data |
| 167 | DWORD bytes_read; |
| 168 | DWORD to_read = (available < (DWORD)(max_len - 1)) ? available : (DWORD)(max_len - 1); |
| 169 | |
| 170 | if (!ReadFile(proc->stdout_read, buffer, to_read, &bytes_read, NULL)) { |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | if (bytes_read > 0) { |
| 175 | buffer[bytes_read] = '\0'; |
| 176 | } |
| 177 | |
| 178 | return (int)bytes_read; |
| 179 | } |
| 180 | |
| 181 | // Check if process is still running |
| 182 | int lsp_is_running(lsp_process_t* proc) { |
| 183 | if (!proc || proc->hProcess == INVALID_HANDLE_VALUE) return 0; |
| 184 | |
| 185 | DWORD exit_code; |
| 186 | if (!GetExitCodeProcess(proc->hProcess, &exit_code)) { |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | return (exit_code == STILL_ACTIVE) ? 1 : 0; |
| 191 | } |
| 192 | |
| 193 | // Stop LSP server |
| 194 | void lsp_stop_server(lsp_process_t* proc) { |
| 195 | if (!proc) return; |
| 196 | |
| 197 | if (proc->stdin_write != INVALID_HANDLE_VALUE) { |
| 198 | CloseHandle(proc->stdin_write); |
| 199 | proc->stdin_write = INVALID_HANDLE_VALUE; |
| 200 | } |
| 201 | |
| 202 | if (proc->stdout_read != INVALID_HANDLE_VALUE) { |
| 203 | CloseHandle(proc->stdout_read); |
| 204 | proc->stdout_read = INVALID_HANDLE_VALUE; |
| 205 | } |
| 206 | |
| 207 | if (proc->stderr_read != INVALID_HANDLE_VALUE) { |
| 208 | CloseHandle(proc->stderr_read); |
| 209 | proc->stderr_read = INVALID_HANDLE_VALUE; |
| 210 | } |
| 211 | |
| 212 | if (proc->hProcess != INVALID_HANDLE_VALUE) { |
| 213 | // Try graceful termination first |
| 214 | TerminateProcess(proc->hProcess, 0); |
| 215 | WaitForSingleObject(proc->hProcess, 1000); // Wait up to 1 second |
| 216 | CloseHandle(proc->hProcess); |
| 217 | proc->hProcess = INVALID_HANDLE_VALUE; |
| 218 | } |
| 219 | |
| 220 | free(proc); |
| 221 | } |
| 222 | |
| 223 | // Get process ID |
| 224 | DWORD lsp_get_pid(lsp_process_t* proc) { |
| 225 | return proc ? proc->pid : 0; |
| 226 | } |
| 227 | |
| 228 | #else |
| 229 | // Unix implementation |
| 230 | #include <stdio.h> |
| 231 | #include <stdlib.h> |
| 232 | #include <string.h> |
| 233 | #include <unistd.h> |
| 234 | #include <fcntl.h> |
| 235 | #include <sys/types.h> |
| 236 | #include <sys/wait.h> |
| 237 | #include <signal.h> |
| 238 | #include <errno.h> |
| 239 | |
| 240 | typedef struct { |
| 241 | pid_t pid; |
| 242 | int stdin_fd; |
| 243 | int stdout_fd; |
| 244 | int stderr_fd; |
| 245 | } lsp_process_t; |
| 246 | |
| 247 | // Start an LSP server process |
| 248 | lsp_process_t* lsp_start_server(const char* command) { |
| 249 | lsp_process_t* proc = malloc(sizeof(lsp_process_t)); |
| 250 | if (!proc) return NULL; |
| 251 | |
| 252 | // Create pipes for stdin, stdout, stderr |
| 253 | int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2]; |
| 254 | |
| 255 | if (pipe(stdin_pipe) < 0 || pipe(stdout_pipe) < 0 || pipe(stderr_pipe) < 0) { |
| 256 | free(proc); |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | pid_t pid = fork(); |
| 261 | if (pid < 0) { |
| 262 | // Fork failed |
| 263 | close(stdin_pipe[0]); close(stdin_pipe[1]); |
| 264 | close(stdout_pipe[0]); close(stdout_pipe[1]); |
| 265 | close(stderr_pipe[0]); close(stderr_pipe[1]); |
| 266 | free(proc); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | if (pid == 0) { |
| 271 | // Child process |
| 272 | // Redirect stdin, stdout, stderr |
| 273 | dup2(stdin_pipe[0], STDIN_FILENO); |
| 274 | dup2(stdout_pipe[1], STDOUT_FILENO); |
| 275 | dup2(stderr_pipe[1], STDERR_FILENO); |
| 276 | |
| 277 | // Close unused pipe ends |
| 278 | close(stdin_pipe[1]); |
| 279 | close(stdout_pipe[0]); |
| 280 | close(stderr_pipe[0]); |
| 281 | |
| 282 | // Close original pipe fds |
| 283 | close(stdin_pipe[0]); |
| 284 | close(stdout_pipe[1]); |
| 285 | close(stderr_pipe[1]); |
| 286 | |
| 287 | // Execute the command - use sh for simplicity |
| 288 | execl("/bin/sh", "sh", "-c", command, NULL); |
| 289 | |
| 290 | // If we get here, exec failed |
| 291 | fprintf(stderr, "Failed to execute: %s\n", command); |
| 292 | exit(1); |
| 293 | } |
| 294 | |
| 295 | // Parent process |
| 296 | proc->pid = pid; |
| 297 | proc->stdin_fd = stdin_pipe[1]; |
| 298 | proc->stdout_fd = stdout_pipe[0]; |
| 299 | proc->stderr_fd = stderr_pipe[0]; |
| 300 | |
| 301 | // Debug log |
| 302 | { |
| 303 | FILE* dbg = fopen("/tmp/fac_lsp_read.log", "a"); |
| 304 | if (dbg) { |
| 305 | fprintf(dbg, "Started LSP server: pid=%d, stdin_fd=%d, stdout_fd=%d, cmd=%s\n", |
| 306 | pid, proc->stdin_fd, proc->stdout_fd, command); |
| 307 | fclose(dbg); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Close unused pipe ends |
| 312 | close(stdin_pipe[0]); |
| 313 | close(stdout_pipe[1]); |
| 314 | close(stderr_pipe[1]); |
| 315 | |
| 316 | // Make stdout and stderr non-blocking |
| 317 | int flags = fcntl(proc->stdout_fd, F_GETFL, 0); |
| 318 | fcntl(proc->stdout_fd, F_SETFL, flags | O_NONBLOCK); |
| 319 | flags = fcntl(proc->stderr_fd, F_GETFL, 0); |
| 320 | fcntl(proc->stderr_fd, F_SETFL, flags | O_NONBLOCK); |
| 321 | |
| 322 | return proc; |
| 323 | } |
| 324 | |
| 325 | // Send data to LSP server |
| 326 | int lsp_send_message(lsp_process_t* proc, const char* message, int len) { |
| 327 | if (!proc || proc->stdin_fd < 0) return -1; |
| 328 | |
| 329 | // Debug log |
| 330 | { |
| 331 | FILE* dbg = fopen("/tmp/fac_lsp_read.log", "a"); |
| 332 | if (dbg) { |
| 333 | fprintf(dbg, "lsp_send_message: len=%d, stdin_fd=%d, pid=%d\n", len, proc->stdin_fd, proc->pid); |
| 334 | fprintf(dbg, " message (first 200 chars): %.200s\n", message); |
| 335 | fclose(dbg); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | ssize_t written = write(proc->stdin_fd, message, (size_t)len); |
| 340 | |
| 341 | // Debug log result |
| 342 | { |
| 343 | FILE* dbg = fopen("/tmp/fac_lsp_read.log", "a"); |
| 344 | if (dbg) { |
| 345 | fprintf(dbg, " write() returned: %zd, errno=%d\n", written, errno); |
| 346 | // Check stderr for errors |
| 347 | if (proc->stderr_fd >= 0) { |
| 348 | char stderr_buf[512]; |
| 349 | ssize_t err_bytes = read(proc->stderr_fd, stderr_buf, sizeof(stderr_buf) - 1); |
| 350 | if (err_bytes > 0) { |
| 351 | stderr_buf[err_bytes] = '\0'; |
| 352 | fprintf(dbg, " STDERR: %s\n", stderr_buf); |
| 353 | } |
| 354 | } |
| 355 | fclose(dbg); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (written < 0) { |
| 360 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 361 | return -1; |
| 362 | } |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | return (int)written; |
| 367 | } |
| 368 | |
| 369 | // Read data from LSP server (non-blocking) |
| 370 | int lsp_read_message(lsp_process_t* proc, char* buffer, int max_len) { |
| 371 | static int read_call_count = 0; |
| 372 | read_call_count++; |
| 373 | |
| 374 | if (!proc || proc->stdout_fd < 0) return -1; |
| 375 | |
| 376 | ssize_t bytes_read = read(proc->stdout_fd, buffer, (size_t)(max_len - 1)); |
| 377 | |
| 378 | // Debug: log every 100th call or when data is read |
| 379 | if (bytes_read > 0 || read_call_count % 100 == 0) { |
| 380 | FILE* dbg = fopen("/tmp/fac_lsp_read.log", "a"); |
| 381 | if (dbg) { |
| 382 | fprintf(dbg, "read call %d: bytes_read=%zd, pid=%d\n", |
| 383 | read_call_count, bytes_read, proc->pid); |
| 384 | if (bytes_read > 0) { |
| 385 | fprintf(dbg, " data: %.100s...\n", buffer); |
| 386 | } |
| 387 | // Check stderr |
| 388 | if (proc->stderr_fd >= 0) { |
| 389 | char stderr_buf[1024]; |
| 390 | ssize_t err_bytes = read(proc->stderr_fd, stderr_buf, sizeof(stderr_buf) - 1); |
| 391 | if (err_bytes > 0) { |
| 392 | stderr_buf[err_bytes] = '\0'; |
| 393 | fprintf(dbg, " STDERR: %s\n", stderr_buf); |
| 394 | } |
| 395 | } |
| 396 | fclose(dbg); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (bytes_read < 0) { |
| 401 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 402 | return 0; // No data available |
| 403 | } |
| 404 | return -1; // Error |
| 405 | } |
| 406 | |
| 407 | if (bytes_read > 0) { |
| 408 | buffer[bytes_read] = '\0'; |
| 409 | } |
| 410 | |
| 411 | return (int)bytes_read; |
| 412 | } |
| 413 | |
| 414 | // Check if process is still running |
| 415 | int lsp_is_running(lsp_process_t* proc) { |
| 416 | if (!proc || proc->pid <= 0) return 0; |
| 417 | |
| 418 | int status; |
| 419 | pid_t result = waitpid(proc->pid, &status, WNOHANG); |
| 420 | |
| 421 | if (result == 0) { |
| 422 | // Process is still running |
| 423 | return 1; |
| 424 | } else if (result == proc->pid) { |
| 425 | // Process has exited |
| 426 | proc->pid = -1; |
| 427 | return 0; |
| 428 | } else { |
| 429 | // Error |
| 430 | return 0; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // Stop LSP server |
| 435 | void lsp_stop_server(lsp_process_t* proc) { |
| 436 | if (!proc) return; |
| 437 | |
| 438 | if (proc->stdin_fd >= 0) { |
| 439 | close(proc->stdin_fd); |
| 440 | proc->stdin_fd = -1; |
| 441 | } |
| 442 | |
| 443 | if (proc->stdout_fd >= 0) { |
| 444 | close(proc->stdout_fd); |
| 445 | proc->stdout_fd = -1; |
| 446 | } |
| 447 | |
| 448 | if (proc->stderr_fd >= 0) { |
| 449 | close(proc->stderr_fd); |
| 450 | proc->stderr_fd = -1; |
| 451 | } |
| 452 | |
| 453 | if (proc->pid > 0) { |
| 454 | // Send SIGTERM first |
| 455 | kill(proc->pid, SIGTERM); |
| 456 | |
| 457 | // Wait a bit for graceful shutdown |
| 458 | usleep(100000); // 100ms |
| 459 | |
| 460 | // Check if still running |
| 461 | if (lsp_is_running(proc)) { |
| 462 | // Force kill |
| 463 | kill(proc->pid, SIGKILL); |
| 464 | waitpid(proc->pid, NULL, 0); |
| 465 | } |
| 466 | |
| 467 | proc->pid = -1; |
| 468 | } |
| 469 | |
| 470 | free(proc); |
| 471 | } |
| 472 | |
| 473 | // Get process ID |
| 474 | pid_t lsp_get_pid(lsp_process_t* proc) { |
| 475 | return proc ? proc->pid : -1; |
| 476 | } |
| 477 | |
| 478 | #endif |
| 479 | |
| 480 | // Fortran-callable wrappers (platform independent) |
| 481 | void lsp_start_server_f(const char* command, int command_len, void** handle) { |
| 482 | char cmd[1024]; |
| 483 | int len = command_len < 1023 ? command_len : 1023; |
| 484 | strncpy(cmd, command, len); |
| 485 | cmd[len] = '\0'; |
| 486 | |
| 487 | *handle = lsp_start_server(cmd); |
| 488 | } |
| 489 | |
| 490 | void lsp_stop_server_f(void** handle) { |
| 491 | if (*handle) { |
| 492 | lsp_stop_server((lsp_process_t*)*handle); |
| 493 | *handle = NULL; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | int lsp_send_message_f(void** handle, const char* message, int message_len) { |
| 498 | if (!*handle) return -1; |
| 499 | return lsp_send_message((lsp_process_t*)*handle, message, message_len); |
| 500 | } |
| 501 | |
| 502 | int lsp_read_message_f(void** handle, char* buffer, int buffer_len) { |
| 503 | if (!*handle) return -1; |
| 504 | return lsp_read_message((lsp_process_t*)*handle, buffer, buffer_len); |
| 505 | } |
| 506 | |
| 507 | int lsp_is_running_f(void** handle) { |
| 508 | if (!*handle) return 0; |
| 509 | return lsp_is_running((lsp_process_t*)*handle); |
| 510 | } |
| 511 | |
| 512 | int lsp_get_pid_f(void** handle) { |
| 513 | if (!*handle) return -1; |
| 514 | #ifdef _WIN32 |
| 515 | return (int)lsp_get_pid((lsp_process_t*)*handle); |
| 516 | #else |
| 517 | return lsp_get_pid((lsp_process_t*)*handle); |
| 518 | #endif |
| 519 | } |