| 1 | #include <process.h>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 | #include <string.h>
|
|---|
| 5 |
|
|---|
| 6 | #define INCL_DOS
|
|---|
| 7 | #define INCL_DOSERRORS
|
|---|
| 8 | #include <os2.h>
|
|---|
| 9 |
|
|---|
| 10 | #define DEBUG 0
|
|---|
| 11 |
|
|---|
| 12 | int execute( char* script, int argc, char* argv[])
|
|---|
| 13 | {
|
|---|
| 14 | char* python = "/@unixroot/usr/bin/" PYTHON_EXE;
|
|---|
| 15 | char* python2 = PYTHON_EXE;
|
|---|
| 16 | char** argv2;
|
|---|
| 17 | int i;
|
|---|
| 18 | int rc;
|
|---|
| 19 |
|
|---|
| 20 | if (DEBUG)
|
|---|
| 21 | printf( "execute: %s\n", script);
|
|---|
| 22 |
|
|---|
| 23 | argv2 = calloc( argc+1+1, sizeof(char*));
|
|---|
| 24 | if (argv2 == NULL) {
|
|---|
| 25 | perror( "argv2");
|
|---|
| 26 | return(-1);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | argv2[0] = python;
|
|---|
| 30 | argv2[1] = script;
|
|---|
| 31 | for( i = 1; i<=argc; i++) {
|
|---|
| 32 | argv2[i+1] = argv[i];
|
|---|
| 33 | if (DEBUG)
|
|---|
| 34 | printf("%d:%x\n", i, argv[i]);
|
|---|
| 35 | }
|
|---|
| 36 | if (DEBUG)
|
|---|
| 37 | for( i = 0; i<argc+1+1; i++)
|
|---|
| 38 | printf("%d:%x %s\n", i, argv2[i], argv2[i]);
|
|---|
| 39 |
|
|---|
| 40 | /* try default python */
|
|---|
| 41 | rc = execv( python, argv2);
|
|---|
| 42 |
|
|---|
| 43 | /* failed, try searching python.exe in PATH */
|
|---|
| 44 | argv2[0] = python2;
|
|---|
| 45 | rc = execvp( python2, argv2);
|
|---|
| 46 |
|
|---|
| 47 | /* failed again */
|
|---|
| 48 | printf("execv failed, rc=%d\n", rc);
|
|---|
| 49 | perror("execv");
|
|---|
| 50 | // failed
|
|---|
| 51 | return -1;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | int main( int argc, char* argv[])
|
|---|
| 55 | {
|
|---|
| 56 | CHAR Buff[2*_MAX_PATH];
|
|---|
| 57 | PPIB pib;
|
|---|
| 58 | char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME];
|
|---|
| 59 | char* bin;
|
|---|
| 60 | APIRET rc;
|
|---|
| 61 |
|
|---|
| 62 | if (DEBUG)
|
|---|
| 63 | puts(getenv("HOME"));
|
|---|
| 64 |
|
|---|
| 65 | // get executable fullpath
|
|---|
| 66 | rc = DosGetInfoBlocks( NULL, &pib);
|
|---|
| 67 | rc = DosQueryModuleName( pib->pib_hmte, sizeof(Buff), Buff);
|
|---|
| 68 | // extract path info
|
|---|
| 69 | _splitpath( Buff, drive, dir, fname, NULL);
|
|---|
| 70 |
|
|---|
| 71 | // try without extension
|
|---|
| 72 | strcpy( Buff, drive);
|
|---|
| 73 | strcat( Buff, dir);
|
|---|
| 74 | strcat( Buff, fname);
|
|---|
| 75 | if (DEBUG)
|
|---|
| 76 | printf( "try %s\n", Buff);
|
|---|
| 77 | if (access( Buff, 0) == 0)
|
|---|
| 78 | return execute( Buff, argc, argv);
|
|---|
| 79 |
|
|---|
| 80 | // try without extension
|
|---|
| 81 | strcat( Buff, ".py");
|
|---|
| 82 | if (DEBUG)
|
|---|
| 83 | printf( "try %s\n", Buff);
|
|---|
| 84 | if (access( Buff, 0) == 0)
|
|---|
| 85 | return execute( Buff, argc, argv);
|
|---|
| 86 |
|
|---|
| 87 | if (DEBUG)
|
|---|
| 88 | printf("script not found\n");
|
|---|
| 89 | exit(-1);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|