Launch.c

/*
    File:       Launch.c
 
    Contains:   This is a little demo that shows how to call SignatureToApp.
                Usage:
                    Launch ( [-proc] [-file] [-launch] signature )*
                    where signature is a 4-character application signature.
    
                Default behavior is to launch or bring to the front the application with that signature.
                Leading flags will modify this:
                    -proc means only to look for a running process with that signature.
                    -file means to find a running process or a file, but not launch it
                    -launch (the default) means to find a running process or find and launch an app. 
                You can list more than one signature. Each flag modifies the behavior for following
                signatures until another flag is found.
 
    Written by: Jens Alfke  
 
    Copyright:  Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/27/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include <stdio.h>
#include <console.h>
#include <stdlib.h>
#include <string.h>
 
#include <Errors.h>
#include <Memory.h>
 
#include "SignatureToApp.h"
 
int main( int argc, char *argv[] )
{   
    Boolean foundSignature = false;
    short arg;
    Sig2App_Mode mode = Sig2App_LaunchApplication;
    char *argstr;
    OSType sig;
    ProcessSerialNumber psn;
    FSSpec file;
    Boolean launched;
    OSErr err;
    long len;
    
    argc=ccommand(&argv);
    
    if( argc<=1 ) {
        puts("Launch: Finds and/or launches application with given signature");
        return 1;
    }
    
    for( arg=1; arg<argc; arg++ ) {
        argstr = argv[arg];
        
        // Interpret a flag:
        
        if( argstr[0] == '-' ) {
            if( strcmp(argstr,"-proc")==0 )
                mode = Sig2App_FindProcess;
            else if( strcmp(argstr,"-file")==0 )
                mode = Sig2App_FindApplication;
            else if( strcmp(argstr,"-launch")==0 )
                mode = Sig2App_LaunchApplication;
            else {
                fprintf(stderr,"Launch: Unknown flag Ò%sÓ\n",argstr);
                return 1;
            }
            continue;
        } else {
        
            // Interpret anything else as an application signature:
            
            foundSignature = true;
            sig = '    ';
            len = strlen(argstr);
            if( len>4 )
                len = 4;
            BlockMove(argstr,&sig,len);             // Copy signature
            
            // Here we go. Watch carefully:
            
            err = SignatureToApp(sig, &psn, &file, &launched, mode, launchContinue);
            
            if( err==fnfErr ) {
                fprintf(stderr,"Launch: No application with signature '%.4s' found.\n", &sig);
                return 1;
            } else if( err==procNotFound ) {
                fprintf(stderr,"Launch: No application with signature '%.4s' is running.\n", &sig);
                return 1;
            }else if( err ) {
                fprintf(stderr,"Launch: Error %d (signature '%.4s').\n", err,&sig);
                return 1;
            }
            
            if( mode < Sig2App_LaunchApplication ) {
                printf("'%.4s': PSN= {%d/%d}; File= {vol=%hd, dir=%d, name=%P}\n",
                                &sig,
                                psn.highLongOfPSN,psn.lowLongOfPSN,
                                file.vRefNum, file.parID, file.name);
            }
        }
    }
    
    if( !foundSignature ) {
        fprintf(stderr,"Launch: Missing application signature\n");
        return 1;
    }
    return 0;
}