Created
January 12, 2019 21:53
-
-
Save maz-1/169cfa1a21da4f710427d0cc4e59fa3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <dlfcn.h> | |
#include <signal.h> | |
#include <string.h> | |
//#define LIB_PYTHON_PATH "/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/Python" | |
typedef void (*PY_SETPROGNAME)(char *); | |
typedef void (*PY_INIT)(); | |
typedef int (*PYRUN_SIMPLESTR)(const char *); | |
typedef void (*PY_FINAL)(); | |
void sigHandler(int signo) | |
{ | |
switch (signo) | |
{ | |
case SIGINT: // CTRL + c or Break key | |
case SIGTERM: // Shutdown/Restart | |
case SIGHUP: // "Hang up" (legacy) | |
case SIGKILL: // Kill | |
case SIGTSTP: // Close terminal from x button | |
exit(signo); | |
break; | |
default: | |
break; | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
void *handle; | |
char *error; | |
char buffer[256]; | |
FILE *fp; | |
PY_SETPROGNAME Py_SetProgramName = NULL; | |
PY_INIT Py_Initialize = NULL; | |
PYRUN_SIMPLESTR PyRun_SimpleString = NULL; | |
PY_FINAL Py_Finalize = NULL; | |
// Set up error handler | |
signal(SIGHUP, sigHandler); | |
signal(SIGTERM, sigHandler); | |
signal(SIGINT, sigHandler); | |
signal(SIGKILL, sigHandler); | |
signal(SIGTSTP, sigHandler); | |
fp = popen("/bin/bash -c 'otool -L /usr/local/bin/python3|grep -oE \"\\s\\S.*/Python.framework/\\S+/Python\"'", "r"); | |
if (fp == NULL) { | |
printf("Failed to run command\n" ); | |
exit(1); | |
} | |
/* Read the output a line at a time - output it. */ | |
fgetc(fp); | |
if (fgets(buffer, sizeof(buffer)-1, fp) != NULL) { | |
printf("Python framework path: %s", buffer); | |
} | |
else | |
exit(1); | |
//printf("length:%d\n", strlen(buffer)); | |
char libpath[strlen(buffer)-1]; | |
memcpy(libpath, &buffer, sizeof(libpath)); | |
//handle = dlopen(LIB_PYTHON_PATH, RTLD_LAZY); | |
handle = dlopen(libpath, RTLD_LAZY); | |
if (!handle) { | |
fprintf(stderr, "%s\n", dlerror()); | |
exit(EXIT_FAILURE); | |
} | |
dlerror(); | |
*(void **) (&Py_SetProgramName) = (PY_SETPROGNAME)dlsym(handle, "Py_SetProgramName"); | |
if ((error = dlerror()) != NULL) { | |
fprintf(stderr, "%s\n", error); | |
exit(EXIT_FAILURE); | |
} | |
Py_Initialize = (PY_INIT)dlsym(handle, "Py_Initialize"); | |
PyRun_SimpleString = (PYRUN_SIMPLESTR)dlsym(handle, "PyRun_SimpleString"); | |
Py_Finalize = (PY_FINAL)dlsym(handle, "Py_Finalize"); | |
Py_SetProgramName(argv[0]); // optional but recommended | |
Py_Initialize(); | |
PyRun_SimpleString("import re\n" | |
"import sys\n" | |
"from feeluown.__main__ import main\n"); | |
for (int i=0; i<argc; i++) { | |
sprintf(buffer, "sys.argv.append('%s')\n", argv[i]); | |
PyRun_SimpleString(buffer); | |
} | |
//PyRun_SimpleString("sys.argv.append('FeelUOwn')\n"); | |
PyRun_SimpleString("if __name__ == '__main__':\n" | |
" sys.exit(main())\n"); | |
Py_Finalize(); | |
//free(buffer); | |
pclose(fp); | |
dlclose(handle); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment