From eb751c576754d03d4927825a2dd5539cb77b765f Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Tue, 28 Apr 2026 14:35:09 +0200 Subject: [PATCH] Add a function get_calling_process_nameto retrieve the name of the calling process. Use this function to identify Eeschema as caller for automatically setting the compatibility flag ki. --- src/sharedspice.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/sharedspice.c b/src/sharedspice.c index 60db65a49..2b51832a8 100644 --- a/src/sharedspice.c +++ b/src/sharedspice.c @@ -55,6 +55,8 @@ int myputs(const char* inp, FILE* f); int myputc(int inp, FILE* f); int myfputc(int inp, FILE* f); +static char* get_calling_process_name(void); + int myputs(const char* inp, FILE* f) { @@ -80,6 +82,7 @@ myfputc(int inp, FILE* f) #include "ngspice/ngspice.h" #include "misc/misc_time.h" #include "ngspice/randnumb.h" +#include "ngspice/compatmode.h" /*Use Windows threads if on W32 without pthreads*/ #ifndef HAVE_LIBPTHREAD @@ -1053,6 +1056,14 @@ ngSpice_Init(SendChar* printfcn, SendStat* statusfcn, ControlledExit* ngspiceexi /* initialize display to 'no display at all'*/ DevInit(); + /* Check if we are called by Eeschema */ + char* thisproc = get_calling_process_name(); + if (thisproc) { + if( ciprefix("eeschema", thisproc)) + newcompat.ki = TRUE; + tfree(thisproc); + } + #ifdef FastRand // initialization and seed for FastNorm Gaussian random generator { @@ -2606,3 +2617,33 @@ static int totalreset(void) return 0; }; + +char * get_calling_process_name(void) { +#if defined(HAVE__PROC_MEMINFO) + char proc_name[16]; // Linux process names are limited to 15 chars + null + FILE* f = fopen("/proc/self/comm", "r"); + if (f) { + if (fgets(proc_name, sizeof(proc_name), f)) { + fclose(f); + return copy(proc_name); + } + else + fclose(f); + return NULL; + } +#elif defined(_WIN32) + char processPath[MAX_PATH]; + // Passing NULL retrieves the path of the executable for the current process + DWORD length = GetModuleFileNameA(NULL, processPath, MAX_PATH); + if (length > 0) { + // Find the last backslash to isolate the executable name from the full path + char* processName = strrchr(processPath, '\\'); + if (processName) { + return copy(processName + 1); + } + } + return NULL; +#else + return NULL; +#endif +}