cUtilSystem.c 10.6 KB
Newer Older
1
#ifndef __MACH__
2
# include <Carbon.h>
3
#else
4 5
# include <Carbon/Carbon.h>
# include <fcntl.h>
6 7 8 9 10 11
#endif

CFBundleRef systemBundle=NULL;

#ifndef __MACH__
typedef int pid_t;
12 13 14 15 16
typedef int mode_t;

#define        O_WRONLY        0x0001
#define        O_CREAT         0x0200
#define        O_TRUNC         0x0400
17 18 19 20

pid_t (*fork_p)(void) = NULL;
int (*execv_p)(const char *path,char *const argv[])=NULL;
pid_t (*waitpid_p)(pid_t wpid,int *status,int options)=NULL;
21 22 23
int (*open_p)(const char *path,int flags,mode_t mode)=NULL;
int (*dup2_p) (int oldd,int newd)=NULL;
int (*close_p) (int d)=NULL;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#endif

#ifndef __MACH__
static int initSystemBundle (void)
{
	FSRefParam fileRefParam;
	FSRef fileRef;
	OSErr error;
	CFURLRef url;
	
	{
		int i;
		char *p;
		
		p=(char*)&fileRefParam;
		for (i=0; i<sizeof (fileRefParam); ++i)
			p[i]=0;

		p=(char*)&fileRef;
		for (i=0; i<sizeof (fileRef); ++i)
			p[i]=0;
	}
	
	fileRefParam.ioNamePtr = "\pSystem.framework";;
	fileRefParam.newRef = &fileRef;

	error = FindFolder (kSystemDomain,kFrameworksFolderType,false,&fileRefParam.ioVRefNum,&fileRefParam.ioDirID);
	if (error!=noErr)
		return 0;
	
	error = PBMakeFSRefSync (&fileRefParam);
	if (error!=noErr)
		return 0;

	url = CFURLCreateFromFSRef (NULL/*kCFAllocatorDefault*/,&fileRef);
	if (url==NULL)
		return 0;

	systemBundle = CFBundleCreate(NULL/*kCFAllocatorDefault*/, url);
	if (systemBundle==NULL)
		return 0;	
    
	CFRelease (url);
	
	return 1;
}
#endif

#ifdef __MWERKS__

int get_toc (void)
{
	asm {
		mr	r3,RTOC
	}
}

int set_toc (int toc)
{
	asm {
		mr	RTOC,r3
	}
}

void __ptr_glue (void)
{
	asm {
		lwz		r0,0(r12)
		stw		RTOC,20(SP)
		mtctr	r0
		lwz		RTOC,4(r12)
		bctr
	}
}
#else

#define get_toc() 0;
#define set_toc(a) ;

#endif

#ifndef __MACH__
pid_t fork (void)
{
	int r,t;
	
	if (fork_p==NULL){
		if (systemBundle==NULL)
			initSystemBundle ();
		fork_p = (int(*)(void)) CFBundleGetFunctionPointerForName (systemBundle,CFSTR ("fork"));
	    if (fork_p==NULL)
			return -1;
	}

	t=get_toc();
	r= (*fork_p)();
	set_toc (t);
	
	return r;
}

int execv (const char *path,char *const argv[])
{
	int r,t;
	
	if (execv_p==NULL){
		if (systemBundle==NULL)
			initSystemBundle ();
		execv_p = (int (*)(const char *path,char *const argv[])) CFBundleGetFunctionPointerForName (systemBundle,CFSTR ("execv"));
	    if (execv_p==NULL)
			return -1;	
	}

	t=get_toc();
	r=(*execv_p) (path,argv);
	set_toc (t);

	return r;
}

pid_t waitpid (pid_t wpid,int *status,int options)
{
	int r,t;

	if (waitpid_p==NULL){
		if (systemBundle==NULL)
			initSystemBundle ();
		waitpid_p = (pid_t (*)(pid_t wpid,int *status,int options)) CFBundleGetFunctionPointerForName (systemBundle,CFSTR ("waitpid"));
	    if (waitpid_p==NULL)
			return -1;
	}

	t=get_toc();
	r=(*waitpid_p) (wpid,status,options);
	set_toc (t);
	
	return r;
}
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

int open (const char *path,int flags,mode_t mode)
{
	int r,t;

	if (open_p==NULL){
		if (systemBundle==NULL)
			initSystemBundle ();
		open_p = (int (*)(const char *path,int flags,mode_t mode)) CFBundleGetFunctionPointerForName (systemBundle,CFSTR ("open"));
	    if (open_p==NULL)
			return -1;
	}

	t=get_toc();
	r=(*open_p) (path,flags,mode);
	set_toc (t);
	
	return r;
}

int dup2 (int oldd,int newd)
{
	int r,t;

	if (dup2_p==NULL){
		if (systemBundle==NULL)
			initSystemBundle ();
		dup2_p = (int (*)(int oldd,int newd)) CFBundleGetFunctionPointerForName (systemBundle,CFSTR ("dup2"));
	    if (dup2_p==NULL)
			return -1;
	}

	t=get_toc();
	r=(*dup2_p) (oldd,newd);
	set_toc (t);
	
	return r;
}

int close (int d)
{
	int r,t;

	if (close_p==NULL){
		if (systemBundle==NULL)
			initSystemBundle ();
		close_p = (int (*)(int d)) CFBundleGetFunctionPointerForName (systemBundle,CFSTR ("close"));
	    if (close_p==NULL)
			return -1;
	}

	t=get_toc();
	r=(*close_p) (d);
	set_toc (t);
	
	return r;
}
219 220
#endif

221
static int parse_arguments (char *command,char **argv)
222
{
223 224
	char *p;
	int argc;
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		
	argc=0;
	p=command;

	while (*p==' ' || *p=='\t')
		++p;

	while (*p!='\0' && argc<2048){
		if (*p=='\''){
			char c,*d_p;
			
			++p;
			argv[argc]=p;

			d_p=p;
			
			c=*p;
			while (!(c=='\'' && p[1]!='\'') && c!='\0'){
				*d_p++=c;
				if (c=='\'')
					++p;
				c=*++p;
			}
			
			if (*p=='\0'){
				*d_p='\0';
				break;
			}
			
			++argc;
			*d_p='\0';
			++p;
		} else {
			argv[argc++]=p;
			while (*p!=' ' && *p!='\t' && *p!='\0')
				++p;
	
			if (*p!='\0')
				*p++='\0';
		}
				
		while (*p==' ' || *p=='\t')
			++p;			
	}
	argv[argc]=NULL;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	
	return argc;
}

int fork_execv_waitpid (char *command,char *stderr_file_name,int *status_p)
{
	char *(argv[2049]);
	int argc,result;

#ifndef __MACH__
	if (!initSystemBundle())
		return -1;
#endif

	result=0;
		
	argc=parse_arguments (command,argv);

288
	if (argc>0){
289
		int pid;
290 291
		
		pid=fork();
292 293 294 295 296 297 298 299 300 301 302
		if (pid==0){
			if (stderr_file_name!=NULL && *stderr_file_name!='\0'){
				int fd;
				
				fd=open (stderr_file_name,O_CREAT | O_TRUNC | O_WRONLY,0777);
				if (fd!=-1){
					dup2 (fd,1);
					close (fd);
				}
			}

303
			result=execv (argv[0],argv);
304 305 306 307
		} else if (pid!=-1)
			result=waitpid (pid,status_p,0);
	} else
		result=-1;
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

	return result;
}

int fork_execv (char *p_command)
{
	char *(argv[2049]);
	int argc,result;
	char *command;

#ifndef __MACH__
	if (!initSystemBundle())
		return -1;
#endif

	{
	char *p,*d_p;

	for (p=p_command; *p!='\0'; ++p)
		;
	
	command=NewPtr (p+1-p_command);
	
	d_p=command;
	for (p=p_command; *p!='\0'; ++p)
		*d_p++=*p;
	*d_p='\0';
	}

	result=0;
338 339

	argc=parse_arguments (command,argv);
340
		
341 342 343 344 345 346 347 348 349 350 351
	if (argc>0){
		int pid;
		
		pid=fork();
		if (pid==0)
			result=execv (argv[0],argv);
		else if (pid!=-1)
			result=0;
		else
			result=1;
	}
352

353
	DisposePtr (command);
354

355 356
	return result;
}
357

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
int fork_execv_pid (char *p_command,int *pid_p)
{
	char *(argv[2049]);
	int argc,result;
	char *command;

#ifndef __MACH__
	if (!initSystemBundle())
		return -1;
#endif

	{
	char *p,*d_p;

	for (p=p_command; *p!='\0'; ++p)
		;
374
	
375 376 377 378 379 380
	command=NewPtr (p+1-p_command);
	
	d_p=command;
	for (p=p_command; *p!='\0'; ++p)
		*d_p++=*p;
	*d_p='\0';
381 382
	}

383 384 385 386
	result=0;

	argc=parse_arguments (command,argv);
		
387 388 389 390
	if (argc>0){
		int pid;
		
		pid=fork();
391 392 393
		
		*pid_p=pid;
		
394 395 396 397 398 399
		if (pid==0)
			result=execv (argv[0],argv);
		else if (pid!=-1)
			result=0;
		else
			result=1;
400 401
	} else
		*pid_p=0;
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

	DisposePtr (command);

	return result;
}

Boolean hfs2posix(char *hfsPath,char *buffer,int size)
{
	CFURLRef	hfs_url;
	CFStringRef	hfs_string, posix_string;
	Boolean		ok;
	
	hfs_string		= CFStringCreateWithCString(NULL/*kCFAllocatorDefault*/, hfsPath, kCFStringEncodingMacRoman);
	hfs_url			= CFURLCreateWithFileSystemPath(NULL/*kCFAllocatorDefault*/,hfs_string,kCFURLHFSPathStyle,/*isDirectory*/false);
	posix_string	= CFURLCopyFileSystemPath(hfs_url, kCFURLPOSIXPathStyle);
	ok				= CFStringGetCString(posix_string, buffer, size, kCFStringEncodingMacRoman);
	
	CFRelease(posix_string);
	CFRelease(hfs_url);
	CFRelease(hfs_string);

	return ok;
}


OSErr LaunchTheDocument(FSSpec *document, OSType appCreator, FSSpec *appSpec) {
    OSErr err, sresult;
    AEAddressDesc appAddr;
    AEDescList fileList;
    AEDesc fileListElt, paraDesc;
    AppleEvent theEvent;
    AppleEvent eventReply;
    AliasHandle docAlias;
    FInfo fndrInfo;
    LaunchParamBlockRec launchPB;
    Size paraSize;
    AppParametersPtr paraData;

        /* initialize our records to NULL
        descriptors */
    AECreateDesc(typeNull, NULL, 0, &appAddr);
    AECreateDesc(typeNull, NULL, 0, &fileList);
    AECreateDesc(typeNull, NULL, 0, &fileListElt);
    AECreateDesc(typeNull, NULL, 0, &theEvent);
    AECreateDesc(typeNull, NULL, 0, &eventReply);
    AECreateDesc(typeNull, NULL, 0, &paraDesc);
    docAlias = NULL;
    paraData = NULL;

        /* create an open documents Apple Event */
    err = AECreateDesc(typeApplSignature,
          (Ptr)&appCreator, sizeof(OSType), &appAddr);
    if (err != noErr) goto bail;
    err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments,
          &appAddr, kAutoGenerateReturnID, kAnyTransactionID,
          &theEvent);
    if (err != noErr) goto bail;

        /* create a one element list of files to send in the
        event */
    err = AECreateList(NULL, 0, false, &fileList);
    if (err != noErr) goto bail;
    err = NewAlias(NULL, document, &docAlias);
    if (err != noErr) goto bail;
    HLock((Handle) docAlias);
    err = AECreateDesc(typeAlias, (Ptr) (*docAlias),
          GetHandleSize((Handle) docAlias), &fileListElt);
    HUnlock((Handle) docAlias);
    if (err != noErr) goto bail;
    err = AEPutDesc(&fileList, 0, &fileListElt);
    if (err != noErr) goto bail;

        /* add the file list to the open documents event */
    err = AEPutParamDesc(&theEvent, keyDirectObject, &fileList);
    if (err != noErr) goto bail;

        /* send the Apple Event */
    err = AESend(&theEvent, &eventReply, kAENoReply,
          kAENormalPriority, kNoTimeOut, NULL, NULL);

        /* if the target could not be found...
        no such app running? */
    if (err == connectionInvalid || err == procNotFound) {

            /* ask the Apple Event Manager to coerce the Apple
            event into a launch application parameter block
            record. */
        err = AECoerceDesc(&theEvent, typeAppParameters,
              &paraDesc);
        if (err != noErr) goto bail;

            /* copy the record's data into our own memory
            location so it can be incorporated into the
            LaunchApplication parameter block.  Here, the new
            location is referenced using the variable paraData. */
#if TARGET_API_MAC_CARBON
        paraSize = AEGetDescDataSize(&paraDesc);
        paraData = (AppParametersPtr) NewPtr(paraSize);
        if (paraData == NULL) { err = memFullErr; goto bail; }
        err = AEGetDescData(&paraDesc, paraData, paraSize );
        if (err != noErr) goto bail;
#else
        paraSize = GetHandleSize((Handle) paraDesc.dataHandle);
        paraData = (AppParametersPtr) NewPtr(paraSize);
        if (paraData == NULL) { err = memFullErr; goto bail; }
        BlockMoveData(*paraDesc.dataHandle, paraData, paraSize);
#endif

            /* launch the application */
        launchPB.launchBlockID = extendedBlock;
        launchPB.launchEPBLength = extendedBlockLen;
        launchPB.launchFileFlags = 0;
        launchPB.launchControlFlags =
            launchContinue + launchNoFileFlags;
        launchPB.launchAppSpec = appSpec;
        launchPB.launchAppParameters = paraData;
        err = LaunchApplication(&launchPB);
    }
bail:
        /* clean up, and go.. */
    if (docAlias != NULL) DisposeHandle((Handle) docAlias);
    if (paraData != NULL) DisposePtr((Ptr) paraData);
    AEDisposeDesc(&paraDesc);
    AEDisposeDesc(&appAddr);
    AEDisposeDesc(&fileListElt);
    AEDisposeDesc(&fileList);
    AEDisposeDesc(&theEvent);
    AEDisposeDesc(&eventReply);
    return err;
}