home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / PMSTART.ZIP / PMSTART.C < prev    next >
C/C++ Source or Header  |  1989-09-14  |  8KB  |  316 lines

  1.   /****************************************************************\
  2.   |* OS2 1.1 Presentation Manager Start Command                   *|
  3.   |* by Massimo Pezzi                                             *|
  4.   |* 611 Camino Dr.                                               *|
  5.   |* Santa Clara, Ca 95050                                        *|
  6.   |* CompuServe id 71531,756                                      *|
  7.   |* user of Micro Foundry BBS (408) 258-3484                     *|
  8.   \****************************************************************/
  9.  
  10. #define INCL_DOSSESMGR
  11. #define INCL_DOSFILEMGR
  12. #include <os2.h>
  13. #include <string.h>
  14. #include <stddef.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18.  
  19. #define _PARM(a) &argv[a][1]
  20. #define NO_ERROR 0
  21. #define SEMANTIC_ERROR 1
  22. #define PROGRAM_NOT_FOUND 2
  23.  
  24.  
  25.  
  26. main (int argc, char *argv[]) {
  27.  
  28. static STARTDATA scntl = {
  29.        50,                        /* Length               */
  30.        FALSE,                     /* Related              */
  31.        FALSE,                     /* FgBg                 */
  32.        FALSE,                     /* TraceOpt             */
  33.        "",                        /* PgmTitle             */
  34.        "",                        /* PgmName              */
  35.        "",                        /* PgmInputs            */
  36.        "",                        /* TermQ                */
  37.        0,                         /* Environment          */
  38.        FALSE,                     /* InheritOpt           */
  39.        0,                         /* SessionType          */
  40.        "",                        /* IconFile             */
  41.        0,                         /* PgmHandle            */
  42.        0x0000,                    /* PgmControl           */
  43.        0,0,                       /* InitXPos, InitYPos   */
  44.        0,0                        /* InitXSize, InitYSize */
  45.        };
  46.  
  47. static CHAR PgmArgs[255]  = "",
  48.             PgmDrive[6]   = "",
  49.             PgmDir[255]   = "",
  50.             PgmName[10]   = "",
  51.             PgmExt[8]     = "",
  52.             IconName[255] = "",
  53.             PgmFName[255] = "",
  54.             PgmTitle[32]  = "";
  55.  
  56.  
  57. USHORT i,
  58.        name_found,
  59.        stype_found,
  60.        title_found,
  61.        exec_cmd,
  62.        retain_cmd,
  63.        idSession, id,
  64.        SearchCode,
  65.        SearchCnt,
  66.        exitrc;
  67.  
  68. CHAR SearchName[14],
  69.      SearchPath[255];
  70.  
  71. HDIR        hdir = HDIR_SYSTEM;
  72. FILEFINDBUF FFbufr;
  73.  
  74. if (argc <= 1) {
  75.    exitrc = SEMANTIC_ERROR;
  76.    goto enditall;
  77. }
  78.  
  79. name_found = stype_found = title_found = exec_cmd = retain_cmd = FALSE;
  80. exitrc = NO_ERROR;
  81.  
  82. for (i = 1; i < argc; i++) {
  83.  
  84.    if (argv[i][0] == '/' || argv[i][0] == '-') {
  85.  
  86.      if (0 == strnicmp(_PARM(i), "TIT:", 4))
  87.        strncpy(PgmTitle, &argv[i][5], sizeof(PgmTitle));
  88.  
  89.      else if (0 == stricmp(_PARM(i), "BG"))
  90.        scntl.FgBg = TRUE;
  91.  
  92.      else if (0 == stricmp(_PARM(i), "FS")) {
  93.  
  94.        if (stype_found) continue;
  95.        scntl.SessionType = 1;
  96.        stype_found = TRUE;
  97.  
  98.      }
  99.  
  100.      else if (0 == stricmp(_PARM(i), "WIN")) {
  101.  
  102.        if (stype_found) continue;
  103.        scntl.SessionType = 2;
  104.        stype_found = TRUE;
  105.  
  106.      }
  107.  
  108.      else if (0 == stricmp(_PARM(i), "PM")) {
  109.  
  110.        if (stype_found) continue;
  111.        scntl.SessionType = 3;
  112.        stype_found = TRUE;
  113.  
  114.      }
  115.  
  116.      else if (0 == stricmp(_PARM(i), "MIN"))
  117.        scntl.PgmControl = (scntl.PgmControl & 0x8008) | 0x0004;
  118.  
  119.      else if (0 == stricmp(_PARM(i), "MAX"))
  120.        scntl.PgmControl = (scntl.PgmControl & 0x8008) | 0x0002;
  121.  
  122.      else if (0 == stricmp(_PARM(i), "NOC"))
  123.        scntl.PgmControl = scntl.PgmControl | 0x0008;
  124.  
  125.      else if (0 == stricmp(_PARM(i), "INV"))
  126.        scntl.PgmControl = 0x0001;
  127.  
  128.      else if (0 == stricmp(_PARM(i), "I"))
  129.        scntl.InheritOpt = TRUE;
  130.  
  131.      else if (0 == stricmp(_PARM(i), "CMD"))
  132.       exec_cmd = TRUE;
  133.  
  134.      else if (0 == stricmp(_PARM(i), "K"))
  135.       retain_cmd = TRUE;
  136.  
  137.      else if (0 == stricmp(_PARM(i), "TRACE"))
  138.        scntl.TraceOpt = TRUE;
  139.  
  140.      else {
  141.        exitrc = SEMANTIC_ERROR;
  142.        goto enditall;
  143.  
  144.      }
  145.  
  146.    }
  147.  
  148.    else {
  149.  
  150.       _splitpath(argv[i], PgmDrive, PgmDir, PgmName, PgmExt);
  151.       name_found    = TRUE;
  152.       break;
  153.  
  154.    }
  155.  
  156. }
  157.  
  158. if (!name_found) {
  159.  
  160.   exitrc = SEMANTIC_ERROR;
  161.   goto enditall;
  162.  
  163. }
  164.  
  165. if (PgmDir[0]) {
  166.  
  167.   strcpy(SearchPath, PgmDrive);
  168.   strcat(SearchPath, PgmDir);
  169.   SearchCode = SEARCH_PATH;
  170.  
  171. }
  172.  
  173. else {
  174.  
  175.   strcpy(SearchPath, "PATH");
  176.   SearchCode = SEARCH_ENVIRONMENT + SEARCH_CUR_DIRECTORY;
  177.  
  178. }
  179.  
  180. strcpy(SearchName, PgmName);
  181.  
  182. if (PgmExt[0])
  183.    strcat(SearchName, PgmExt);
  184. else
  185.    strcat(SearchName, ".*" );
  186.  
  187. memset(PgmFName, '\0', sizeof(PgmFName));
  188.  
  189. DosSearchPath(SearchCode,
  190.               SearchPath,
  191.               SearchName,
  192.               PgmFName,
  193.               sizeof(PgmFName));
  194.  
  195. if (PgmFName[0]) {
  196.   if (!PgmExt[0]) {
  197.  
  198.     SearchCnt = 1;
  199.     _splitpath(PgmFName, PgmDrive, PgmDir, PgmName, PgmExt);
  200.     strcpy(PgmExt, ".COM");
  201.     _makepath(PgmFName, PgmDrive, PgmDir, PgmName, PgmExt);
  202.     hdir = HDIR_SYSTEM;
  203.  
  204.     if (DosFindFirst(PgmFName,
  205.                  &hdir,
  206.                  FILE_NORMAL + FILE_READONLY + FILE_ARCHIVED,
  207.                  &FFbufr,
  208.                  sizeof(FFbufr),
  209.                  &SearchCnt,
  210.                  0L)) {
  211.  
  212.       strcpy(PgmExt, ".CMD");
  213.       _makepath(PgmFName, PgmDrive, PgmDir, PgmName, PgmExt);
  214.       SearchCnt = 1;
  215.       hdir = HDIR_SYSTEM;
  216.  
  217.       if (DosFindFirst(PgmFName,
  218.                    &hdir,
  219.                    FILE_NORMAL + FILE_READONLY + FILE_ARCHIVED,
  220.                    &FFbufr,
  221.                    sizeof(FFbufr),
  222.                    &SearchCnt,
  223.                    0L)) {
  224.  
  225.         strcpy(PgmExt, ".EXE");
  226.         _makepath(PgmFName, PgmDrive, PgmDir, PgmName, PgmExt);
  227.         SearchCnt = 1;
  228.         hdir = HDIR_SYSTEM;
  229.  
  230.         if (DosFindFirst(PgmFName,
  231.                      &hdir,
  232.                      FILE_NORMAL + FILE_READONLY + FILE_ARCHIVED,
  233.                      &FFbufr,
  234.                      sizeof(FFbufr),
  235.                      &SearchCnt,
  236.                      0L)) {
  237.           exitrc = PROGRAM_NOT_FOUND;
  238.           goto enditall;
  239.  
  240.         }
  241.       }
  242.     }
  243.   }
  244. }
  245. else if (exec_cmd) {
  246.   strcpy(PgmName, argv[i]);
  247.   strcpy(PgmFName, argv[i]);
  248. }
  249. else {
  250.   exitrc = PROGRAM_NOT_FOUND;
  251.   goto enditall;
  252. }
  253.  
  254. if (strcmp(PgmExt, ".CMD") == 0 || exec_cmd) {
  255.  
  256.   if (scntl.SessionType == 3)
  257.     scntl.SessionType = 2;
  258.   scntl.PgmName = "CMD.EXE";
  259.   strcpy(PgmArgs, (retain_cmd ? "/K " : "/c "));
  260.   strcat(PgmArgs, PgmFName);
  261.   strcat(PgmArgs, " ");
  262.  
  263. }
  264. else
  265.   scntl.PgmName = PgmFName;
  266.  
  267. for (i++; i < argc; i++) {
  268.  
  269.    strcat(PgmArgs, argv[i]);
  270.    strcat(PgmArgs, " ");
  271.  
  272. }
  273.  
  274. scntl.PgmInputs = PgmArgs;
  275.  
  276. if (PgmTitle[0])
  277.    scntl.PgmTitle = PgmTitle;
  278. else
  279.    scntl.PgmTitle = scntl.PgmName;
  280.  
  281.  
  282. strcpy(SearchName, PgmName);
  283. strcat(SearchName, ".ICO");
  284.  
  285. memset(IconName, '\0', sizeof(IconName));
  286.  
  287. DosSearchPath(SearchCode,
  288.               SearchPath,
  289.               SearchName,
  290.               IconName,
  291.               sizeof(IconName));
  292.  
  293. scntl.IconFile = IconName;
  294.  
  295. exitrc = DosStartSession( &scntl, &idSession, &id);
  296.  
  297. enditall:;
  298. if (exitrc == PROGRAM_NOT_FOUND)
  299.   printf("unable to locate %s\n", argv[i]);
  300.  
  301. if (exitrc == SEMANTIC_ERROR) {
  302.   printf ("Start Session Options invalid or program name not specified\n");
  303.   printf ("Valid values are BG, FS, WIN, PM, MAX, CMD, K,\n");
  304.   printf ("                 MIN, NOC, INV, I, TRACE\n");
  305.  
  306. }
  307.  
  308. else if (exitrc != NO_ERROR)
  309.   printf("Start Session ended with rc = %i\n", exitrc);
  310.  
  311. else;
  312.  
  313. return exitrc;
  314.  
  315. }
  316.