char far *InitAlias( char far * inname ) /**************************************/ { USHORT hdl,read; static char b[80]; char *bp; unsigned long ppos,pos; USHORT action; char far *endname; static char noalias = 0; endname = inname; while( *endname == ' ' ) ++endname; for( ;; ) { if( *endname == '\0' ) { break; } if( *endname == ' ' ) { *endname = '\0'; ++endname; break; } ++endname; } bp = b; while( *bp = *inname ) { ++bp; ++inname; } action=action; if( DosOpen( b, &hdl, &action, 0L, 0, 1, 0x10 ,0L ) == 0 ) { DosChgFilePtr( hdl, 0L, 2, &ppos ); pos = ppos; DosChgFilePtr( hdl, 0L, 0, &ppos ); #ifdef DOS { static int alias_seg; DosAllocSeg( pos + 1 + ALIAS_SLACK, &alias_seg, 0 ); *(((int far *)&AliasList)+0) = 0; *(((int far *)&AliasList)+1) = alias_seg; AliasSize = pos + ALIAS_SLACK; } #else AliasList = AliasArea; AliasSize = 2047; #endif DosRead( hdl, AliasList, pos, &read ); if( pos > 0 && AliasList[ pos-1 ] != '\n' ) { AliasList[ pos+0 ] = '\r'; AliasList[ pos+1 ] = '\n'; AliasList[ pos+2 ] = '\0'; } else { AliasList[ pos ] = '\0'; } DosClose( hdl ); } else { AliasList = &noalias; } return( endname ); }
static void alarm_init(void) { PFNSIGHANDLER pfnPrev; USHORT pfAction; PIDINFO pid; bAlarmInit = TRUE; if (!DosAllocSeg( ALARM_STACK, (PSEL) &selAlarmStack, SEG_NONSHARED )) { OFFSETOF(pbAlarmStack) = ALARM_STACK - 2; SELECTOROF(pbAlarmStack) = selAlarmStack; /* Create the thread */ if (DosCreateThread( alarm_thread, &tidAlarm, pbAlarmStack )) { fprintf(stderr,"Alarm thread failed to start.\n"); exit(1); } /* Setup the signal handler for Process Flag A */ if (DosSetSigHandler(AlarmSignal,&pfnPrev,&pfAction,SIGA_ACCEPT,SIG_PFLG_A)) { fprintf(stderr,"SigHandler Failed to install.\n"); exit(1); } /* Save main process ID, we'll need it for triggering the signal */ DosGetPID(&pid); pidMain = pid.pid; } else exit(1); }
USHORT DebugAlloc(USHORT usSize, PSEL psel, USHORT flags) { usNumDynamicAllocs++; if (usSize > 4096) { usNumBIGAllocs++; } return(DosAllocSeg(usSize, psel, flags)); }
char __far *InitAlias( char __far * inname ) /******************************************/ { USHORT hdl,read; static char b[80]; char *bp; unsigned long ppos,pos; USHORT action; char __far *endname; static char noalias = 0; #ifndef DOS static char AliasArea[2048]; /* The DLL seems to need static memory */ #endif endname = inname; while( *endname == ' ' ) ++endname; for( ; *endname != '\0'; endname++ ) { if( *endname == ' ' ) { *endname++ = '\0'; break; } } for( bp = b; (*bp = *inname) != '\0'; bp++ ) { ++inname; } action=action; if( DosOpen( b, &hdl, &action, 0L, 0, 1, 0x10 ,0L ) == 0 ) { DosChgFilePtr( hdl, 0L, 2, &ppos ); pos = ppos; DosChgFilePtr( hdl, 0L, 0, &ppos ); #ifdef DOS { static int alias_seg; DosAllocSeg( pos + 1 + ALIAS_SLACK, &alias_seg, 0 ); AliasList = (char __far *)MK_FP( alias_seg, 0 ); AliasSize = pos + ALIAS_SLACK; } #else AliasList = AliasArea; AliasSize = 2047; #endif DosRead( hdl, AliasList, pos, &read ); if( pos > 0 && AliasList[pos - 1] != '\n' ) { AliasList[pos + 0] = '\r'; AliasList[pos + 1] = '\n'; AliasList[pos + 2] = '\0'; } else { AliasList[pos] = '\0'; } DosClose( hdl ); } else { AliasList = &noalias; } return( endname ); }
PVOID gdtAlloc(size_t tSize, BOOL fSwappable) { USHORT rc; SEL sel; rc = DosAllocSeg(tSize, &sel, 0); if (rc) return NULL; printf("Allocating %p\n", MAKEP(sel,0)); fflush(stdout); return MAKEP(sel, 0); }
/* * RecordModHandle - save module handle for later reference */ void RecordModHandle( HMODULE value ) { SEL sel; if( ModHandles == NULL ) { DosAllocSeg( sizeof( ULONG ), (PSEL)&sel, 0 ); ModHandles = MK_FP( sel, 0 ); } else { DosReallocSeg( ( NumModHandles + 1 ) * sizeof( HMODULE ), FP_SEG( ModHandles ) ); } ModHandles[NumModHandles++] = value; }
USHORT MAllocMem(USHORT usSize, CHAR FAR ** ppBuffer) { SEL sel; unsigned int err; err = DosAllocSeg(usSize, &sel, 0); if (err) { return(err); } *ppBuffer = MAKEP(sel, 0); return(0); }
/**********************************************************************/ #include <mt\stdio.h> #include <mt\conio.h> #include <mt\process.h> #define INCL_BASE #include <os2.h> USHORT rc; PCHAR Test; struct _EXCEPTIONREGISTRATIONRECORD { struct _EXCEPTIONREGISTRATIONRECORD * prev_structure; PFN Handler; }; typedef struct _EXCEPTIONREGISTRATIONRECORD EXCEPTIONREGISTRATIONRECORD; typedef struct _EXCEPTIONREGISTRATIONRECORD * PEXCEPTIONREGISTRATIONRECORD; USHORT EXPENTRY SETEXCEPT(PEXCEPTIONREGISTRATIONRECORD); USHORT EXPENTRY UNSETEXCEPT(PEXCEPTIONREGISTRATIONRECORD); TID Tid; #define STACKSIZE 0x2000 SEL SelStack; PCHAR StackPtr; void Thread(unsigned far *args); /* Playing thread function */ void cdecl main() { EXCEPTIONREGISTRATIONRECORD ExceptReg; printf("Setting exception handler\n"); rc=SETEXCEPT(&ExceptReg); DosAllocSeg(STACKSIZE,&SelStack,1); StackPtr=MAKEP(SelStack,0); Tid=_beginthread( Thread, (VOID FAR *)StackPtr, STACKSIZE, NULL); getch(); rc=UNSETEXCEPT(&ExceptReg); }
unsigned __AllocSeg( unsigned int amount ) { unsigned n; /* number of paragraphs desired */ short seg; struct heapstart _WCFAR *p; tag _WCFAR *last_tag; #if !(defined(__QNX__) || defined(__WINDOWS_286__) ) tiny_ret_t rc; #endif if( !__heap_enabled ) return( 0 ); /* heapinfo + frl + frl, end tags */ if( amount > - (sizeof(struct heapstart) + TAG_SIZE*2 ) ) { return( 0 ); } /* heapinfo + frl, allocated blk, end tags */ amount += sizeof(struct heapblk) + TAG_SIZE + TAG_SIZE * 2; if( amount < _amblksiz ) amount = _amblksiz; n = ( amount + 0x0f ) >> 4; if( n == 0 ) n = 0x1000; /* 23-may-89 */ #if defined(__OS2__) rc = DosAllocSeg( n << 4, (PSEL)&seg, 0 ); /* 21-aug-91 */ if( rc != 0 ) return( 0 ); #elif defined(__QNX__) seg = qnx_segment_alloc_flags( ((long)n) << 4, __qnx_alloc_flags ); if( seg == -1 ) return( 0 ); #elif defined(__WINDOWS_286__) { HANDLE hmem; LPSTR p; hmem = GlobalAlloc( __win_alloc_flags, ((long)n) << 4 ); if( hmem == NULL ) return( 0 ); p = GlobalLock( hmem ); if( p == NULL ) { GlobalFree( hmem ); return( 0 ); } #if 0 /* code generator can't handle this */ if( FP_OFF( p ) != 0 ) { /* in case, Microsoft changes Windows */ GlobalUnlock( hmem ); /* in post 3.1 versions */ GlobalFree( hmem ); return( 0 ); } #endif seg = FP_SEG( p ); } #else rc = TinyAllocBlock( n ); if( TINY_ERROR( rc ) ) { return( 0 ); } seg = TINY_INFO( rc ); #endif p = (struct heapstart _WCFAR *)MK_FP( seg, 0 ); p->h.heaplen = n << 4; p->h.prevseg = 0; p->h.nextseg = 0; p->h.rover = offsetof( struct heapstart, first ); p->h.b4rover = 0; p->h.numalloc = 0; p->h.numfree = 1; p->h.freehead.len = 0; p->h.freehead.prev = offsetof( struct heapstart, first ); p->h.freehead.next = offsetof( struct heapstart, first ); p->first.len = p->h.heaplen - sizeof(struct heapblk) - TAG_SIZE*2; p->h.largest_blk = p->first.len; p->first.prev = offsetof( struct heapblk, freehead ); p->first.next = offsetof( struct heapblk, freehead ); last_tag = MK_FP( seg, p->h.heaplen - TAG_SIZE*2 ); *last_tag = END_TAG; last_tag[1] = 0; /* link to next piece of near heap */ return( seg ); /* return allocated segment */ }
AFILE *makefp(DEBFILE *pdf, ULONG mid, ULONG instaddr, UCHAR *fname) { uchar *cp = NULL; uint segbase; uchar *srcbuf; uint Nlines; uint Tlines; SEL srcsel; SEL offsel; ushort srcseglen; ushort *offtab; AFILE *fp; int ftype; int OffsetTableBufSize; char FileNameBuffer[CCHMAXPATH]; char FileSpecBuffer[CCHMAXPATH]; UCHAR FileNameLength; LNOTAB *pLnoTabEntry; int sfi; MODULE *pModule; CSECT *pCsect; UCHAR *pFileName; UCHAR *fn; BOOL found; ULONG junk; int lno; if( fname ) { /***************************************************************************/ /* - if fname is specified, then the view is to be build using the source */ /* file name supplied by the user as in the case of getfile. */ /* - we need to find an sfi to associate with the file. */ /* - build a length-prefixed z-string from the supplied name. */ /***************************************************************************/ fn = strrchr(fname, '\\'); fn = (fn) ? (fn + 1) : fname ; pFileName = Talloc( strlen(fn) + 2 ); *pFileName = (UCHAR)strlen(fn); strcpy( pFileName+1, fn ); found = FALSE; for( sfi=mid=0, pdf = pnode->ExeStruct; pdf != NULL; pdf=pdf->next ) { mid = MapSourceFileToMidSfi( pFileName, &sfi, pdf ); if( mid && sfi ) { found = TRUE; memset(FileNameBuffer, 0, sizeof(FileNameBuffer) ); strcpy(FileNameBuffer+1, fname ); FileNameBuffer[0] = (UCHAR)strlen(fname); break; } } Tfree( pFileName ); if( found == FALSE ) return(NULL); instaddr = DBMapLno(mid, 0, sfi, &junk , pdf ); DBMapInstAddr(instaddr, &pLnoTabEntry, pdf); lno = 0; if( pLnoTabEntry ) lno = pLnoTabEntry->lno; } else { /***************************************************************************/ /* - we're going to build the view from an instruction address. */ /***************************************************************************/ if ( pdf->SrcOrAsm == SOURCE_LEVEL ) { mid = DBMapInstAddr(instaddr, &pLnoTabEntry, pdf); lno = sfi = 0; if( pLnoTabEntry ) { lno = pLnoTabEntry->lno; sfi = pLnoTabEntry->sfi; } if( (pLnoTabEntry != NULL) && (sfi != 0) ) { memset(FileNameBuffer, 0, sizeof(FileNameBuffer) ); cp = GetFileName( mid, sfi ); if( cp ) strcpy(FileNameBuffer, cp); } } } if(ftype) { findsrc(FileNameBuffer+1,FileSpecBuffer+1,sizeof(FileSpecBuffer) ); FileSpecBuffer[0] = (UCHAR)strlen(FileSpecBuffer+1); memcpy(FileNameBuffer,FileSpecBuffer,sizeof(FileSpecBuffer)); } FileNameLength = FileNameBuffer[0]; fp = (AFILE*) Talloc(SizeOfAFILE(FileNameLength)); memcpy( fp->filename, FileNameBuffer, FileNameLength+1); /****************************************************************************/ /* - allocate 64k for the source buffer. */ /* - allocate 20k for the offset buffer. */ /* - load source file into a buffer and define: */ /* - srcsel = source buffer selector. */ /* - offsel = offset buffer selector. */ /* - 0 = number of lines to skip at the beginning of the file. */ /* - srcseglen = source buffer bytes actually used by the load. */ /* - Nlines = number of source lines in the buffer. */ /* - Tlines = number of source lines in the entire file. */ /* - reallocate the source buffer to size really needed. */ /****************************************************************************/ Nlines = srcsel = offsel = 0; if( !DosAllocSeg(0,(PSEL)&srcsel,0) && !DosAllocSeg(20*1024,(PSEL)&offsel, 0) ) { LoadSource( fp->filename+1, (UCHAR *)Sel2Flat(srcsel), (USHORT *)Sel2Flat(offsel), 0, &srcseglen, &Nlines, &Tlines); if( Nlines ) DosReallocSeg(srcseglen, srcsel); } /****************************************************************************/ /* - now, define the view structure. */ /****************************************************************************/ fp->shower = showA; fp->pdf = pdf; fp->mid = mid; fp->sfi = sfi; fp->mseg = segbase; fp->Tlines = Tlines; /* number of lines in file. */ fp->Nlines = Nlines; /* number of lines in source buffer. */ fp->Nbias = 0; /* number of lines skipped in source.*/ fp->topline = 1; /* init to 1st line in the file. */ fp->csrline = lno; /* " " */ fp->hotline = lno; /* " " */ fp->hotaddr = instaddr; fp->skipcols = 0; /* columns skipped on left. */ fp->Nshown = 0; fp->topoff = instaddr; fp->csr.row = 0; fp->csr.col = 0; fp->csr.mode = CSR_NORMAL; fp->sview = NOSRC; /* assume no source disassembler view*/ fp->flags = ASM_VIEW_NEW; if( Nlines ) { srcbuf = (uchar*)Sel2Flat(srcsel); fp->source = srcbuf; /**************************************************************************/ /* Allocate the offtab[] buffer to hold Nlines + 1 offsets. We add the 1 */ /* so that we can make the offset table indices line up with the */ /* source line numbers. */ /**************************************************************************/ OffsetTableBufSize = (Nlines+1)*sizeof(USHORT); offtab = (USHORT*) Talloc(OffsetTableBufSize); memcpy(offtab + 1, (uchar*)Sel2Flat(offsel), Nlines*sizeof(USHORT) ); fp->offtab = offtab; fp->flags = 0; /* clear asm flag. */ if( Tlines > Nlines ) /* does compressed source exceed 64k?*/ fp->flags |= AF_HUGE; /* mark afile with huge file flag */ fp->shower = showC; /* display source */ fp->sview = MIXEDN; /* assume no source disassembler view*/ /* Flag all text lines for which a (line #, offset) pair exists. */ pModule = GetModuleWithAddr( instaddr, pdf ); if( pModule ) { for(pCsect = pModule->pCsects; pCsect != NULL; pCsect=pCsect->next ) { int NumEntries; NumEntries = pCsect->NumEntries; pLnoTabEntry = pCsect->pLnoTab; if( (pLnoTabEntry != NULL) && ( NumEntries > 0 ) ) { for( ; NumEntries; pLnoTabEntry++, NumEntries-- ) { if( pLnoTabEntry->sfi == sfi ) { int lno; lno = pLnoTabEntry->lno; if( (lno != 0) && (lno <= Nlines) ) { *(srcbuf + offtab[lno] - 1) |= LINE_OK; } } } } } } MarkLineBRKs( fp ); /* mark the active breakpoints */ } if( offsel ) /* if there was an offset segment */ DosFreeSeg(offsel); /* allocated then free it up */ if( !Nlines && srcsel ) /* if there was no source then */ DosFreeSeg(srcsel); /* free up the temp source buffer */ return( fp ); }
int set_ea(char FAR *i_eas, char *name) { #ifdef HAVE_EAS int rc=0; char FAR *eas; unsigned int i, total; #if TARGET==OS2 #ifdef __32BIT__ FILESTATUS4 fs; EAOP2 eaop; char FAR *real_pfeal; PFEA2LIST pfeal; PFEA2 pf, opf; #else EAOP eaop; PFEALIST pfeal; PFEA pf; FILESTATUS2 fs; SEL selector; #endif #elif TARGET==WIN32 PFEALIST pfeal0, pfeal; PFEA pf; struct nt_sid *sid; unsigned char *pstreambuf, *streambuf; WIN32_STREAM_ID w32sid; unsigned long stream_len; #endif eas=i_eas; if(discard_ea(name)) return(-1); if((total=mget_word(eas))==0) return(0); #if TARGET==OS2 #ifdef __32BIT__ /* This takes the 4-byte prefixes into account (are the V1.2 EAs still valid if they flow beyond 64K when the oNextEntryOffset is applied?). Also, we ensure that it is aligned properly. In theory, there may be a way to crash this (72K doesn't consider the multitude of EAs) but we don't know/care about it -- ASR 17/10/2000 */ real_pfeal=(char FAR *)farmalloc_msg(73728); pfeal=(PFEA2LIST)align_dword(real_pfeal); eaop.fpFEA2List=pfeal; #else if(DosAllocSeg(65535U, &selector, SEG_NONSHARED)) return(-1); pfeal=(PFEALIST)MAKEP(selector, 0); eaop.fpFEAList=pfeal; #endif #elif TARGET==WIN32 pstreambuf=(char *)farmalloc_msg(65536+260*total); pfeal=pfeal0=(PFEALIST)(streambuf=align_dword(pstreambuf)); #endif eas+=2; pf=&pfeal->list[0]; for(i=0; i<total; i++) { #if TARGET==OS2&&defined(__32BIT__) opf=pf; #endif #if TARGET==WIN32 pf=&pfeal->list[0]; #endif pf->fEA=mget_byte(eas++); pf->cbName=mget_byte(eas++); pf->cbValue=mget_word(eas); eas+=2; #if TARGET==OS2&&defined(__32BIT__) far_memmove((char FAR *)pf+sizeof(FEA2)-1, eas, pf->cbName); *((char FAR *)pf+sizeof(FEA2)-1+pf->cbName)='\0'; #else /* Win32 or OS/2-16 */ far_memmove((char FAR *)pf+sizeof(FEA), eas, pf->cbName); *((char FAR *)pf+sizeof(FEA)+pf->cbName)='\0'; #endif eas+=pf->cbName; #if TARGET==OS2&&defined(__32BIT__) far_memmove((char FAR *)pf+sizeof(FEA2)+pf->cbName, eas, pf->cbValue); #else /* Win32 or OS/2-16 */ far_memmove((char FAR *)pf+sizeof(FEA)+pf->cbName+1, eas, pf->cbValue); #endif eas+=pf->cbValue; #if SFX_LEVEL>=ARJ #if TARGET==OS2&&defined(__32BIT__) if(ea_filter((char FAR *)pf+sizeof(FEA2), 0)&&((pf->fEA&FEA_NEEDEA)||!crit_eas)) #else /* Win32 or OS/2-16 */ if(ea_filter((char FAR *)pf+sizeof(FEA), 0)&&((pf->fEA&FEA_NEEDEA)||!crit_eas)) #endif #endif /* Update the offsets */ #if TARGET==OS2 #ifdef __32BIT__ pf=(PFEA2)((char FAR *)pf+sizeof(FEA2)+pf->cbName+pf->cbValue); #else pf=(PFEA)((char FAR *)pf+sizeof(FEA)+pf->cbName+1+pf->cbValue); #endif /* Align at DWORD boundary and issue the list fixups */ #ifdef __32BIT__ pf=(PFEA2)align_dword((char FAR *)pf); opf->oNextEntryOffset=(i+1==total)?0:(char FAR *)pf-(char FAR *)opf; #endif #elif TARGET==WIN32 pfeal=(PFEALIST)((char FAR *)pfeal+sizeof(FEALIST)+pf->cbName+1+pf->cbValue); if(i<total-1) pfeal=(PFEALIST)align_dword((char FAR*)pfeal); pfeal0->cbList=(i==total-1)? 0: (((char FAR *)pfeal)-((char FAR *)pfeal0)); pfeal0=pfeal; #endif } #if TARGET==OS2 pfeal->cbList=(char FAR *)pf-(char FAR *)pfeal; #ifdef __32BIT__ rc=DosSetPathInfo((PSZ)name, FIL_QUERYEASIZE, (PBYTE)&eaop, sizeof(eaop), 0); farfree(real_pfeal); #else rc=DosSetPathInfo((PSZ)name, FIL_QUERYEASIZE, (PBYTE)&eaop, sizeof(eaop), 0, 0L); DosFreeSeg(selector); #endif if(!rc) { #ifdef __32BIT__ if(DosQueryPathInfo(name, FIL_QUERYEASIZE, (PVOID)&fs, sizeof(fs))) #else if(DosQPathInfo(name, FIL_QUERYEASIZE, (PVOID)&fs, sizeof(fs), 0L)) #endif rc=-1; else if(fs.cbList<=4) rc=-1; } #elif TARGET==WIN32 if((sid=open_streams(name, 1))==NULL) rc=-1; else { memset(&w32sid, 0, sizeof(w32sid)); w32sid.dwStreamId=BACKUP_EA_DATA; w32sid.Size.LowPart=stream_len=(((char FAR *)pfeal)-streambuf); if(create_stream(&w32sid, sid)||write_stream(streambuf, stream_len, sid)<stream_len) rc=-1; close_streams(sid); } free(pstreambuf); #endif return(rc); #else return(-1); #endif }