home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / STARTD3.ZIP / STARTD.C < prev    next >
Text File  |  1993-11-06  |  10KB  |  298 lines

  1. /*
  2.  *  STARTD:  A simple program to start a DOS session under OS/2 2.x with settings.
  3.  *
  4.  *  Last Modfied: 11/06/93
  5.  *
  6.  *  Author: Norm Ross / Modified by Stuart G. Mentzer
  7.  *
  8.  *  Using the example code for BOOTA.C found in IBM DOCUMENT GBOF-2254
  9.  *
  10.  *  MODIFICATION HISTORY
  11.  *
  12.  *  07-Jul-1992  Norm Ross  1.00 : Initial version
  13.  *  06-Nov-1993  Stuart G. Mentzer  1.00-SGM : Fixed /WAIT + misc. mod's
  14.  */
  15.  
  16. #define INCL_DOSSESMGR
  17. #define INCL_DOSMISC
  18. #define INCL_DOSPROCESS
  19. #define INCL_DOSQUEUES   /* Queue values */
  20. #include <os2.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. PBYTE readEnv(PSZ pFileName);
  25.  
  26. PSZ pBootFailure = "Session could not be started.\r\n";
  27. char szPgmInputs[256];
  28. char szDosPgmInputs[256];
  29.  
  30. STARTDATA startd;                  /* Session start information */
  31. ULONG SessionID;                   /* Session and Process ID for new session*/
  32. PID ProcessID;
  33. HQUEUE hq;                         /* Termination queue */
  34.  
  35. void main(int argc, char *argv[])
  36. {
  37.   USHORT       rc;
  38.   USHORT       flagWin = 0, flagFs = 0, flagKeep = 0, flagNoCmd = 0;
  39.  
  40.   /* ---- init startd struct defaults */
  41.   startd.Length                   = sizeof(STARTDATA);
  42.   startd.Related                  = SSF_RELATED_INDEPENDENT;
  43.   startd.FgBg                     = SSF_FGBG_FORE;
  44.   startd.TraceOpt                 = SSF_TRACEOPT_NONE;
  45.   startd.PgmTitle                 = NULL;
  46.   startd.PgmName                  = NULL;
  47.   startd.PgmInputs                = NULL;
  48.   startd.TermQ                    = NULL;
  49.   startd.Environment              = NULL;
  50.   startd.InheritOpt               = SSF_INHERTOPT_PARENT;
  51.   startd.SessionType              = SSF_TYPE_DEFAULT;
  52.   startd.PgmControl               = SSF_CONTROL_VISIBLE;
  53.  
  54.  
  55.   /* ------ Process args */
  56.   while(--argc > 0) {
  57.     char *arg;
  58.  
  59.     switch(**++argv) {
  60.     case '\"':
  61.     startd.PgmTitle = strtok(*argv, "\"");
  62.     break;
  63.     case '-':
  64.     case '/':
  65.       arg = (*argv)+1;
  66.       if (strlen(arg) == 1) {
  67.     switch(*arg) {
  68.     case 'n':
  69.     case 'N': flagNoCmd = 1; break;
  70.     case 'k':
  71.     case 'K': flagKeep = 1; break;
  72.     case 'c':
  73.     case 'C': break;
  74.     case 'f':
  75.     case 'F': startd.FgBg = SSF_FGBG_FORE; break;
  76.     case 'B':
  77.     case 'b': startd.FgBg = SSF_FGBG_BACK; break;
  78.     case 'i':
  79.     case 'I': startd.InheritOpt = SSF_INHERTOPT_SHELL; break;
  80.     case '?': usage(); break;
  81.     default:
  82.       fprintf(stderr, "Unrecognized option: %s\n", arg);
  83.       usage();
  84.       break;
  85.     }
  86.       } else if (stricmp(arg, "DOS") == 0) {
  87.     startd.SessionType = SSF_TYPE_VDM;
  88.       } else if (stricmp(arg, "WIN") == 0) {
  89.     flagWin = 1;
  90.       } else if (stricmp(arg, "WAIT") == 0) {
  91.     startd.Related = SSF_RELATED_CHILD;
  92.       } else if (stricmp(arg, "FS") == 0) {
  93.     flagFs = 1;
  94.       } else if (stricmp(arg, "FG") == 0) {
  95.     startd.FgBg = SSF_FGBG_FORE;
  96.       } else if (stricmp(arg, "BG") == 0) {
  97.     startd.FgBg = SSF_FGBG_BACK;
  98.       } else if (stricmp(arg, "MAX") == 0) {
  99.     startd.PgmControl |= SSF_CONTROL_MAXIMIZE;
  100.       } else if (stricmp(arg, "MIN") == 0) {
  101.     startd.PgmControl |= SSF_CONTROL_MINIMIZE;
  102.       } else if (stricmp(arg, "INV") == 0) {
  103.     startd.PgmControl |= SSF_CONTROL_INVISIBLE;
  104.       } else if (stricmp(arg, "PM") == 0) {
  105.     startd.SessionType = SSF_TYPE_PM;
  106.       } else if (strnicmp(arg, "pos", 3) == 0) {
  107.     char *s = strtok(arg, "=");
  108.     /* ---- I really should check strtok's return... */
  109.     startd.PgmControl |= SSF_CONTROL_SETPOS;
  110.     startd.InitXPos = atoi(strtok(NULL, ","));
  111.     startd.InitYPos = atoi(strtok(NULL, ","));
  112.     startd.InitXSize = atoi(strtok(NULL, ","));
  113.     startd.InitYSize = atoi(strtok(NULL, ""));
  114.       } else if (stricmp(arg, "PGM") == 0) {
  115.     char *p = szPgmInputs;
  116.     /* ---- strip quotes from name if there are any */
  117.     startd.PgmName = strtok(*argv, "\"");
  118.  
  119.     /* ---- cat the rest of the args together to pass to pgm */
  120.     while (argc > 1) {
  121.       argc--;
  122.       argv++;
  123.       strcpy(p, *argv);
  124.       p += strlen(*argv);
  125.       *p++ = ' '; /* put spaces between the args */
  126.     }
  127.     *p = '\0';
  128.     startd.PgmInputs = szPgmInputs;
  129.     break;
  130.       } else if (stricmp(arg, "ICON") == 0) {
  131.     startd.IconFile = *++argv;
  132.     argc--;
  133.       } else if (stricmp(arg, "SF") == 0) {
  134.     char *fname = *++argv;
  135.     argc--;
  136.     if (access(fname, 4)) {
  137.       fprintf(stderr, "Session File %s not found\n", fname);
  138.     }
  139.     startd.Environment = readEnv(fname);
  140.       } else {
  141.     printf("Unrecognized option: %s\n", arg);
  142.     usage();
  143.       }
  144.       break;
  145.  
  146.     default:
  147.     {
  148.       char *p = szPgmInputs;
  149.       startd.PgmName = *argv;
  150.  
  151.       /* ---- cat the rest of the args together to pass to pgm */
  152.       while (argc > 1) {
  153.         argc--;
  154.         argv++;
  155.         strcpy(p, *argv);
  156.         p += strlen(*argv);
  157.         *p++ = ' '; /* put spaces between the args */
  158.       }
  159.       *p = '\0';
  160.       startd.PgmInputs = szPgmInputs;
  161.       break;
  162.     }
  163.  
  164.     } /* switch */
  165.   } /* while */
  166.  
  167.   /* ------ Start thru Command processor */
  168.   if ((startd.PgmName!=NULL)&&(startd.SessionType!=SSF_TYPE_PM)&&(!flagNoCmd)){
  169.     if (flagKeep)
  170.       strcpy(szDosPgmInputs, "/k ");
  171.     else
  172.       strcpy(szDosPgmInputs, "/c ");
  173.     strcat(szDosPgmInputs, startd.PgmName);
  174.     strcat(szDosPgmInputs, " ");
  175.     strcat(szDosPgmInputs, startd.PgmInputs);
  176.     startd.PgmInputs = szDosPgmInputs;
  177.     startd.PgmName = NULL;
  178.   }
  179.  
  180.   /* ------ Set the correct session type */
  181.   if (flagWin) {
  182.     switch(startd.SessionType) {
  183.     case SSF_TYPE_DEFAULT: startd.SessionType = SSF_TYPE_WINDOWABLEVIO; break;
  184.     case SSF_TYPE_VDM: startd.SessionType = SSF_TYPE_WINDOWEDVDM; break;
  185.     case SSF_TYPE_PM: break;
  186.     }
  187.   } else if (flagFs) {
  188.     switch(startd.SessionType) {
  189.     case SSF_TYPE_DEFAULT: startd.SessionType = SSF_TYPE_FULLSCREEN; break;
  190.     case SSF_TYPE_VDM: break;
  191.     case SSF_TYPE_PM: break;
  192.     }
  193.   }
  194.  
  195.   /* ------ Set up the termination queue */
  196.   {
  197.    APIRET rdcq;
  198.    rdcq = DosCreateQueue( &hq, QUE_FIFO | QUE_CONVERT_ADDRESS, "\\QUEUES\\term.que" );
  199.    if (rdcq) {
  200.      /* ------ Print out failure message */
  201.      printf("DosCreateQueue Error %ld : ", rdcq);
  202.      return;
  203.    }
  204.    startd.TermQ = "\\QUEUES\\term.que";
  205.   }
  206.  
  207.  
  208.   /* ------ Start the Session */
  209.   rc = DosStartSession( &startd, &SessionID, &ProcessID );
  210.  
  211.   if (rc) {
  212.  
  213.     /* ------ Print out failure message */
  214.     printf("DosStartSession Error %ld : ", rc);
  215.     DosPutMessage( 1, strlen(pBootFailure), pBootFailure );
  216.     DosExit( EXIT_PROCESS, rc ); /* SGM */
  217.  
  218.   } else if ( startd.Related == SSF_RELATED_CHILD ) {
  219.  
  220.     /* ------ Wait for child to finish - SGM */
  221.     REQUESTDATA   Request;       /* Request-identification data  */
  222.     ULONG         DataLength;    /* Length of element received   */
  223.     PULONG        DataAddress;   /* Address of element received  */
  224.     ULONG         ElementCode;   /* Request a particular element */
  225.     BOOL32        NoWait;        /* No wait if queue is empty    */
  226.     BYTE          ElemPriority;  /* Priority of element received */
  227.     HEV           SemHandle;     /* Semaphore handle             */
  228.     APIRET        rcq;           /* Return code                  */
  229.  
  230.     Request.pid = 0;           /* Set request data for current process   */
  231.     ElementCode = 0;           /* Read from front of the queue           */
  232.     NoWait = 0;                /* Read should wait if queue empty        */
  233.     SemHandle = 0;             /* Unused since this is a call that waits */
  234.  
  235.     rcq = DosReadQueue( hq, &Request, &DataLength,
  236.                        (PVOID *) &DataAddress, ElementCode, NoWait,
  237.                        &ElemPriority, SemHandle );
  238.     if (rcq) {
  239.       printf( "DosReadQueue error: return code = %ld", rcq );
  240.       return;
  241.     }
  242.   }
  243.  
  244. return;
  245. }
  246.  
  247. #define MAXENV 4096
  248. PBYTE readEnv(PSZ fname) {
  249.   FILE *fptr;
  250.   PBYTE env = (PBYTE)malloc(MAXENV);
  251.   PBYTE p = env;
  252.  
  253.   fptr = fopen(fname, "r");
  254.   if (fptr == (FILE *)NULL) {
  255.     fprintf(stderr, "\nFile %s cannot be found\n");
  256.     exit(-1);
  257.   }
  258.  
  259.   while (fgets(p, 80, fptr)) {
  260.     p+=strlen(p);
  261.     *(p-1)='\0';
  262.     if (p>env + 4096) {
  263.       fprintf(stderr, "ERROR: too many settings\n");
  264.       fflush(stderr);
  265.       exit(-1);
  266.     }
  267.   }
  268.  
  269.   fclose( fptr ); /* SGM */
  270.   realloc(env, p-env);
  271.  
  272. return(env);
  273. }
  274.  
  275. usage( void ) {
  276.   fprintf(stderr, "STARTD version 1.00-SGM\n\n");
  277.   fprintf(stderr, "startd [\"program title\"] [/BG /C /DOS /F /FS /I /ICON iconfile /INV /K /MAX\n\t /MIN /PGM POS=x,y,x1,y1 /SF settingsfile /WIN] [command ...]\n\n");
  278.   fprintf(stderr, "\t/B[G]\t start session in background\n");
  279.   fprintf(stderr, "\t/C\t close session upon completion\n");
  280.   fprintf(stderr, "\t/DOS\t start a dos session\n");
  281.   fprintf(stderr, "\t/F[G]\t start session in foreground\n");
  282.   fprintf(stderr, "\t/FS\t start a full screen session\n");
  283.   fprintf(stderr, "\t/I\t sets SSF_INHERTOPT_SHELL\n");
  284.   fprintf(stderr, "\t/ICON\t uses the specified icon file\n");
  285.   fprintf(stderr, "\t/INV\t start the application invisibly\n");
  286.   fprintf(stderr, "\t/K\t keep the session around after it is finished\n");
  287.   fprintf(stderr, "\t/MAX\t start maximized\n");
  288.   fprintf(stderr, "\t/MIN\t start minimized\n");
  289.   fprintf(stderr, "\t/N\t don't start indirectly through command processor\n");
  290.   fprintf(stderr, "\t/PGM\t the next argument is the program name\n");
  291.   fprintf(stderr, "\t/PM\t start a PM program\n");
  292.   fprintf(stderr, "\t/POS=x,y,x1,y1\t specify window position and size\n");
  293.   fprintf(stderr, "\t/SF\t read the specified dos settings file\n");
  294.   fprintf(stderr, "\t/WIN\t start a windowed session\n");
  295.   fprintf(stderr, "\t/WAIT\t wait for session to finish\n");
  296.   exit(1);
  297. }
  298.