1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102 |
- #define BUILDING_OPENDOORS
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <time.h>
- #include <errno.h>
- #include "OpenDoor.h"
- #ifdef ODPLAT_NIX
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #endif
- #include "ODCore.h"
- #include "ODGen.h"
- #include "ODCom.h"
- #include "ODPlat.h"
- #include "ODScrn.h"
- #include "ODInQue.h"
- #include "ODInEx.h"
- #include "ODUtil.h"
- #include "ODKrnl.h"
- #include "ODSwap.h"
- #ifdef ODPLAT_WIN32
- #include "ODFrame.h"
- #endif
- #if defined(ODPLAT_WIN32) && defined(_MSC_VER)
- #undef P_WAIT
- #undef P_NOWAIT
- #include <process.h>
- #endif
- #ifdef ODPLAT_DOS
- int _swap = 0;
- char *_swappath = NULL;
- int _useems = 0;
- int _required = 0;
- static long swapsize;
- static int ems = 2;
- static int mapsize;
- static unsigned int tempno = 1;
- static char errtab[] =
- {
- 0,
- EINVAL,
- ENOENT,
- ENOENT,
- EMFILE,
- EACCES,
- EBADF,
- ENOMEM,
- ENOMEM,
- ENOMEM,
- E2BIG,
- ENOEXEC,
- EINVAL,
- EINVAL,
- -1,
- EXDEV,
- EACCES,
- EXDEV,
- ENOENT,
- -1
- };
- static VECTOR vectab1[]=
- {
- 0, 1, 0, 0,
- 1, 1, 0, 0,
- 2, 1, 0, 0,
- 3, 1, 0, 0,
- 0x1B, 1, 0, 0,
- 0x23, 1, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 2, 0, 0,
- 0, 3, 0, 0
- };
- static VECTOR vectab2[(sizeof vectab1)/(sizeof vectab1[0])];
- int _spawnvpe(int nModeFlag, char *pszPath, char *papszArgs[],
- char *papszEnviron[]);
- int _spawnve(int nModeFlag, char *pszPath, char *papszArgs[],
- char * papszEnviron[]);
- static void savevect(void);
- #endif
- #ifdef ODPLAT_NIX
- int _spawnvpe(int nModeFlag, char *pszPath, char *papszArgs[],
- char *papszEnviron[]);
- #endif
- ODAPIDEF BOOL ODCALL od_spawn(const char *pszCommandLine)
- {
- #ifdef ODPLAT_DOS
- char *apszArgs[4];
- INT16 nReturnCode;
-
- TRACE(TRACE_API, "od_spawn()");
- *apszArgs=getenv("COMSPEC");
- apszArgs[1] = "/c";
- apszArgs[2] = pszCommandLine;
- apszArgs[3] = NULL;
-
- if(*apszArgs != NULL)
- {
- if((nReturnCode = od_spawnvpe(P_WAIT, *apszArgs, apszArgs, NULL)) != -1
- || errno != ENOENT)
- {
- return(nReturnCode != -1);
- }
- }
- *apszArgs = "command.com";
- return(od_spawnvpe(P_WAIT, *apszArgs, apszArgs, NULL) != -1);
- #endif
- #ifdef ODPLAT_WIN32
- char *pch;
- char *apszArgs[3];
- char szProgName[80];
-
-
- ODStringCopy(szProgName, pszCommandLine, sizeof(szProgName));
- pch = strchr(szProgName, ' ');
- if(pch != NULL) *pch = '\0';
- apszArgs[0] = szProgName;
-
- pch = strchr(pszCommandLine, ' ');
- if(pch == NULL)
- {
- apszArgs[1] = NULL;
- }
- else
- {
- apszArgs[1] = pch + 1;
- apszArgs[2] = NULL;
- }
-
- return(od_spawnvpe(P_WAIT, *apszArgs, apszArgs, NULL) != -1);
- #endif
- #ifdef ODPLAT_NIX
- sigset_t block;
- int retval;
-
- sigemptyset(&block);
- sigaddset(&block,SIGALRM);
- sigprocmask(SIG_BLOCK,&block,NULL);
- retval=system(pszCommandLine);
-
- sigemptyset(&block);
- sigaddset(&block,SIGALRM);
- sigprocmask(SIG_UNBLOCK,&block,NULL);
- return(retval!=-1 && retval != 127);
- #endif
- }
- ODAPIDEF INT16 ODCALL od_spawnvpe(INT16 nModeFlag, char *pszPath,
- char *papszArg[], char *papszEnv[])
- {
- INT16 nToReturn;
- time_t nStartUnixTime;
- DWORD dwQuotient;
- #ifdef ODPLAT_WIN32
- void *pWindow;
- #endif
- #ifdef ODPLAT_DOS
- char *pszDir;
- BYTE *abtScreenBuffer;
- INT nDrive;
- tODScrnTextInfo TextInfo;
- #endif
-
- TRACE(TRACE_API, "od_spawnvpe()");
-
- if(!bODInitialized) od_init();
- #ifdef ODPLAT_DOS
-
-
- if(nModeFlag != P_WAIT)
- {
- od_control.od_error = ERR_PARAMETER;
- return(-1);
- }
-
- if((abtScreenBuffer = malloc(4000)) == NULL)
- {
- od_control.od_error = ERR_MEMORY;
- return(-1);
- }
- if((pszDir = malloc(256)) == NULL)
- {
- od_control.od_error = ERR_MEMORY;
- free(abtScreenBuffer);
- return(-1);
- }
-
- ODScrnGetTextInfo(&TextInfo);
-
- ODScrnSetBoundary(1,1,80,25);
-
- ODScrnGetText(1, 1, 80, 25, (char *)abtScreenBuffer);
-
- ODScrnSetAttribute(0x07);
-
-
- if(od_control.od_clear_on_exit)
- {
- ODScrnClear();
- }
- else
- {
- ODScrnSetCursorPos(1, 1);
- }
-
- strcpy(pszDir, "X:\\");
- pszDir[0] = 'A' + (nDrive = _getdrv());
- _getcd(0, (char *)pszDir + 3);
- #endif
-
- nStartUnixTime = time(NULL);
- if(nModeFlag == P_WAIT)
- {
-
- #ifdef ODPLAT_WIN32
- pWindow = ODScrnShowMessage("Running sub-program...", 0);
- #endif
-
- ODWaitDrain(10000);
- #ifdef OD_MULTITHREADED
-
-
- ODKrnlShutdown();
- #endif
-
- if(od_control.baud != 0)
- {
- #ifdef ODPLAT_WIN32
-
-
- ODInExDisableDTR();
- #endif
- ODComClose(hSerialPort);
- }
- }
-
- nToReturn = _spawnvpe(nModeFlag, pszPath, papszArg, papszEnv);
- if(nModeFlag == P_WAIT)
- {
-
- if(od_control.baud != 0)
- {
- ODComOpen(hSerialPort);
- }
- #ifdef OD_MULTITHREADED
-
-
- ODKrnlInitialize();
- #endif
- if(!(bIsShell || od_control.od_spawn_freeze_time))
- {
- ODDWordDivide(&dwQuotient, NULL, time(NULL) - nStartUnixTime, 60L);
- od_control.user_timelimit -= (int)dwQuotient;
- }
- else
- {
- nNextTimeDeductTime += time(NULL) - nStartUnixTime;
- }
-
-
-
- ODInQueueResetLastActivity(hODInputQueue);
-
- od_clear_keybuffer();
-
- #ifdef ODPLAT_WIN32
- ODScrnRemoveMessage(pWindow);
- #endif
- }
- #ifdef ODPLAT_DOS
-
- ODScrnPutText(1, 1, 80, 25, (char *)abtScreenBuffer);
-
- ODScrnSetBoundary(TextInfo.winleft, TextInfo.wintop,
- TextInfo.winright, TextInfo.winbottom);
- ODScrnSetAttribute(TextInfo.attribute);
- ODScrnSetCursorPos(TextInfo.curx, TextInfo.cury);
- _setdrvcd(nDrive, pszDir);
-
- free(abtScreenBuffer);
- free(pszDir);
- #endif
-
- return(nToReturn);
- }
- #ifdef ODPLAT_DOS
- int _spawnvpe(int nModeFlag, char *pszPath, char *papszArgs[],
- char *papszEnviron[])
- {
- char *e;
- char *p;
- char buf[80];
- int nReturnCode;
- _swappath = (char *)(strlen(od_control.od_swapping_path) == 0 ? NULL
- : (char *)od_control.od_swapping_path);
- _useems = od_control.od_swapping_noems;
- _swap = od_control.od_swapping_disable;
- if((nReturnCode=_spawnve(nModeFlag, pszPath, papszArgs, papszEnviron))!=-1
- || errno!=ENOENT || *pszPath=='\\' || *pszPath=='/'
- || *pszPath && *(pszPath+1)==':' || (e=getenv("PATH"))==NULL)
- {
- return(nReturnCode);
- }
- for (;;e++)
- {
- if((p=strchr(e,';'))!=NULL)
- {
- if(p-e > 66)
- {
- e=p;
- continue;
- }
- }
- else if(strlen(e)>66)
- {
- return( -1 );
- }
- p=buf;
- while(*e && *e!=';') *p++=*e++;
- if(p>buf)
- {
- if(*(p-1)!='\\' && *(p-1)!='/') *p++ = '\\';
- strcpy(p,pszPath);
- if((nReturnCode=_spawnve(nModeFlag,buf,papszArgs,papszEnviron))!=-1 || errno!=ENOENT)
- {
- return(nReturnCode);
- }
- }
- if(*e=='\0') return(-1);
- }
- }
- int addvect(int number, int opcode)
- {
- VECTOR *vect = vectab1;
- if ( number < 0 || number > 0xFF ||
- ( opcode != IRET && opcode != CURRENT ))
- {
- errno = EINVAL;
- return( -1 );
- }
-
- while ( vect->flag != 3 && ( vect->flag == 2 ||
- vect->number != ( char )number ))
- {
- vect++;
- }
- if ( vect->flag == 3 )
- {
-
- vect = vectab1;
- while ( vect->flag == CURRENT || vect->flag == IRET )
- vect++;
- }
- if ( vect->flag != 3 )
- {
- vect->number = ( char )number;
- vect->flag = ( char )opcode;
- if ( opcode == CURRENT )
- _getvect( number, &vect->vseg, &vect->voff );
- return( 0 );
- }
- errno = ENOMEM;
- return( -1 );
- }
- static void savevect(void)
- {
- VECTOR *vect1 = vectab1;
- VECTOR *vect2 = vectab2;
- while ( vect1->flag != 3 )
- {
- if ( vect1->flag != 2 )
- {
- vect2->number = vect1->number;
- vect2->flag = CURRENT;
- _getvect( vect1->number, &vect2->vseg, &vect2->voff );
- }
- else
- vect2->flag = 2;
- vect1++;
- vect2++;
- }
- vect2->flag = 3;
- }
- static int testfile(char *p, char *file, int *handle)
- {
- unsigned int startno = tempno;
- int nDrive = ( *file | 32 ) - 96;
- int root;
- unsigned int bytes;
- unsigned int clusters;
- int need;
- int nReturnCode;
- unsigned long dwQuotient;
- unsigned long remainder;
- if ( file + 2 == p )
- {
- *p++ = '\\';
- if ( _getcd( nDrive, p ))
- return( 1 );
- p = file + strlen( file );
- }
- else
- {
- *p = '\0';
- if ( ODFileAccessMode( file, 0 ))
- return( 1 );
- }
- if ( *( p - 1 ) != '\\' && *( p - 1 ) != '/' )
- *p++ = '\\';
- if ( p - file == 3 )
- root = 1;
- else
- root = 0;
- strcpy( p, "swp" );
- p += 3;
- if ( _dskspace( nDrive, &bytes, &clusters ) != 0 )
- return( 1 );
- ODDWordDivide(&dwQuotient, &remainder, swapsize, bytes);
- need = (int)dwQuotient;
- if ( remainder )
- need++;
- if ( root == 0 )
- need++;
- if ( clusters < ( unsigned int )need )
- return( 1 );
- do
- {
- again: tempno = ( ++tempno ) ? tempno : 1;
- if ( tempno == startno )
- return( 1 );
- ltoa(( long )tempno, p, 10 );
- }
- while ( !ODFileAccessMode( file, 0 ));
- if (( nReturnCode = _create( file, handle )) == 80 )
- goto again;
- return( nReturnCode );
- }
- static int tempfile(char *file, int *handle)
- {
- char *s = _swappath;
- char *p = file;
- if ( s )
- {
- for ( ;; s++ )
- {
- while ( *s && *s != ';' )
- *p++ = *s++;
- if ( p > file )
- {
- if ( p == file + 1 || file[ 1 ] != ':' )
- {
- memmove( file + 2, file, ( int )( p - file ));
- *file = ( char )( _getdrv() + 'a' );
- file[ 1 ] = ':';
- p += 2;
- }
- if ( testfile( p, file, handle ) == 0 )
- return( 0 );
- p = file;
- }
- if ( *s == '\0' )
- break;
- }
- }
- else
- {
- *p++ = ( char )( _getdrv() + 'a' );
- *p++ = ':';
- if ( testfile( p, file, handle ) == 0 )
- return( 0 );
- }
- errno = EACCES;
- return( 1 );
- }
- static int cmdenv(char **papszArgs, char **papszEnviron, char *command,
- char **env, char **memory)
- {
- char **vp;
- unsigned int elen = 0;
- char *p;
- int cnt;
- int len;
-
- if ( papszEnviron == NULL )
- {
- char far *parent_env;
- char far *env_ptr;
- int nul_count;
- ASM mov ah, 0x62
- ASM int 0x21
- ASM push es
- ASM mov es, bx
- ASM mov ax, es:[0x2c]
- ASM pop es
- ASM mov word ptr parent_env, 0
- ASM mov word ptr parent_env + 2, ax
- env_ptr = parent_env;
- nul_count = 0;
- while(nul_count < 2)
- {
- if(*env_ptr)
- {
- nul_count = 0;
- }
- else
- {
- ++nul_count;
- }
- ++env_ptr;
- ++elen;
- }
- if ( elen > 32766 )
- {
- errno = E2BIG;
- return( -1 );
- }
- if (( p = malloc(elen + 15 )) == NULL )
- {
- errno = ENOMEM;
- return( -1 );
- }
- *memory = p;
- *( unsigned int * )&p = *( unsigned int * )&p + 15 & ~15;
- *env = p;
- len = elen;
- while(len--)
- {
- *p++ = *parent_env++;
- }
- }
- else
- {
- for ( vp = papszEnviron; *vp; vp++ )
- {
- elen += strlen( *vp ) + 1;
- if ( elen > 32766 )
- {
- errno = E2BIG;
- return( -1 );
- }
- }
- if (( p = malloc( ++elen + 15 )) == NULL )
- {
- errno = ENOMEM;
- return( -1 );
- }
- *memory = p;
- *( unsigned int * )&p = *( unsigned int * )&p + 15 & ~15;
- *env = p;
- for ( vp = papszEnviron; *vp; vp++ )
- p = strchr( strcpy( p, *vp ), '\0' ) + 1;
- *p = '\0';
- }
-
- vp = papszArgs;
- p = command + 1;
- cnt = 0;
- if (vp!=NULL && *vp )
- {
- while ( *++vp )
- {
- *p++ = ' ';
- cnt++;
- len = strlen( *vp );
- if ( cnt + len > 125 )
- {
- errno = E2BIG;
- free( *memory );
- return( -1 );
- }
- strcpy( p, *vp );
- p += len;
- cnt += len;
- }
- }
- *p = '\r';
- *command = ( char )cnt;
- return(( int )elen );
- }
- static int doxspawn(char *pszPath, char *papszArgs[], char *papszEnviron[])
- {
- int nReturnCode = 0;
- int doswap = 0;
- int elen;
- char *memory;
- char *env;
- char command[ 128 ];
- long totalsize;
- int handle;
- int pages;
- char file[ 79 ];
- char *mapbuf = NULL;
-
- if (( elen = cmdenv( papszArgs, papszEnviron, command, &env, &memory )) == -1 )
- return( -1 );
- if ( _swap == 0 )
- {
- if ( _useems == 0 )
- {
- if ( ems == 2 )
- ems = _chkems( "EMMXXXX0", &mapsize );
- if ( ems == 0 && ( mapbuf = malloc( mapsize )) == NULL )
- {
- errno = ENOMEM;
- free( memory );
- return( -1 );
- }
- }
- if (( nReturnCode = _xsize( _psp, &swapsize, &totalsize )) == 0 )
- {
- if ( _required == 0 || totalsize - swapsize - 272
- < (long)ODDWordShiftLeft(( long )_required , 10 ))
- {
- if ( ems == 0 && _useems == 0 )
- {
- pages = ( int )ODDWordShiftRight( swapsize , 14);
- if ((long)ODDWordShiftLeft(( long )pages , 14 ) < swapsize )
- pages++;
- if ( _savemap( mapbuf ) == 0 &&
- _getems( pages, &handle ) == 0 )
- *file = '\0';
- else if ( tempfile( file, &handle ) != 0 )
- nReturnCode = -1;
- }
- else if ( tempfile( file, &handle ) != 0 )
- nReturnCode = -1;
- }
- else
- doswap = 1;
- }
- else
- {
- errno = errtab[ nReturnCode ];
- nReturnCode = -1;
- }
- }
- else
- doswap = 1;
- if ( nReturnCode == 0 )
- {
- savevect();
- nReturnCode = _xspawn( pszPath, command, env, vectab1, doswap, elen, file,
- handle );
- _setvect( vectab2 );
- if ( nReturnCode == 0 )
- nReturnCode = _getrc();
- else
- {
- errno = errtab[ nReturnCode ];
- nReturnCode = -1;
- }
-
- if ( doswap == 0 && *file == '\0' && _restmap( mapbuf ) != 0 )
- {
- errno = EACCES;
- nReturnCode = -1;
- }
- }
- if ( mapbuf )
- free( mapbuf );
- free( memory );
- return( nReturnCode );
- }
- int _spawnve(int nModeFlag, char *pszPath, char *papszArgs[],
- char * papszEnviron[])
- {
- char *p;
- char *s;
- int nReturnCode = -1;
- char buf[ 80 ];
- if ( nModeFlag != P_WAIT )
- {
- errno = EINVAL;
- return( -1 );
- }
- p = strrchr( pszPath, '\\' );
- s = strrchr( pszPath, '/' );
- if ( p == NULL && s == NULL )
- p = pszPath;
- else if ( p == NULL || s > p )
- p = s;
- if ( strchr( p, '.' ))
- {
- if ( !ODFileAccessMode( pszPath, 0 ))
- nReturnCode = doxspawn( pszPath, papszArgs, papszEnviron );
-
- }
- else
- {
- strcpy( buf, pszPath );
- strcat( buf, ".com" );
- if ( !ODFileAccessMode( buf, 0 ))
- nReturnCode = doxspawn( buf, papszArgs, papszEnviron );
- else
- {
- strcpy( strrchr( buf, '.' ), ".exe" );
- if ( !ODFileAccessMode( buf, 0 ))
- nReturnCode = doxspawn( buf, papszArgs, papszEnviron );
-
- }
- }
- return( nReturnCode );
- }
- #endif
- #ifdef ODPLAT_NIX
- int _spawnvpe(int nModeFlag, char *pszPath, char *papszArgs[],
- char *papszEnviron[])
- {
- pid_t child;
- int status;
- pid_t wret;
- struct sigaction act;
- child=fork();
- if(nModeFlag == P_WAIT) {
-
- act.sa_handler=SIG_IGN;
- sigemptyset(&(act.sa_mask));
- act.sa_flags=SA_NOCLDSTOP;
- sigaction(SIGCHLD,&act,NULL);
- }
- else {
-
- act.sa_handler=SIG_IGN;
- sigemptyset(&(act.sa_mask));
- act.sa_flags=SA_NOCLDSTOP|SA_NOCLDWAIT;
- sigaction(SIGCHLD,&act,NULL);
- }
- if(!child) {
-
- execve(pszPath,papszArgs,papszEnviron);
- exit(-1);
- }
- if(nModeFlag == P_WAIT) {
- wret=waitpid(child,&status,0);
- if(WIFEXITED(status)) {
- return(WEXITSTATUS(status));
- }
- return(-1);
- }
- return(0);
- }
- #endif
|