home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / SESSION.CMD < prev    next >
OS/2 REXX Batch file  |  1995-03-28  |  32KB  |  864 lines

  1. EXTPROC CEnvi2
  2. //************************************************************************
  3. //*** Session.cmd - Allow to start any type of session at any location ***
  4. //*** ver.7         with any title and position.  Etc...  This could   ***
  5. //***               be used in place of OS/2's START command.  And it  ***
  6. //***               runs much quicker if you /BIND it.                 ***
  7. //************************************************************************
  8.  
  9. // This is a list of Default DOS settings, change to what you like.  If
  10. // left commented out then will retain system default.  Any values set
  11. // here will be overwritten by value input at command line.  For those
  12. // options where many text strings are avaialable, remove comments from
  13. // ONE AND ONLY ONE of the choices
  14.  
  15. MyDefaultDOSSettings = {
  16.    //"COM_DIRECT_ACCESS=0",  // 0 or 1
  17.    //"COM_HOLD=0",           // 0 or 1
  18.    //"COM_RECEIVE_BUFFER_FLUSH="   // select one of the following
  19.       //"ALL",
  20.       //"RECEIVE DATA INTERRUPT ENABLE",
  21.       //"SWITCH TO FOREGROUND",
  22.       //"NONE",
  23.    //"COM_SELECT="
  24.       //" ALL",
  25.       //"COM1",
  26.       //"COM2",
  27.       //"COM3",
  28.       //"COM4",
  29.       //"NONE",
  30.    //"DOS_AUTOEXEC=C:\\AUTOEXEC.BAT",
  31.    "DOS_BACKGROUND_EXECUTION=1", // 0 or 1
  32.    //"DOS_BREAK=1", // 0 or 1
  33.    //"DOS_DEVICE=SIZE=0  C:\\OS2\\MDOS\\ANSI.SYS",
  34.    //"DOS_FCBS=16",
  35.    //"DOS_FCBS_KEEP=8",
  36.    //"DOS_FILES=75",
  37.    //"DOS_HIGH=0",  // 0 or 1
  38.    //"DOS_LASTDRIVE=Z",
  39.    //"DOS_RMSIZE=640",
  40.    //"DOS_SHELL=C:\\OS2\\MDOS\\COMMAND.COM C:\\OS2\\MDOS",
  41.    //"DOS_UMB=0",   // 0 or 1
  42.    "DOS_VERSION=DCJSS02.EXE,3,40,255"
  43.       "\nDFIA0MOD.SYS,3,40,255"
  44.       "\nDXMA0MOD.SYS,3,40,255"
  45.       "\nEXCEL.EXE,10,10,4"
  46.       "\nIBMCACHE.COM,3,40,255"
  47.       "\nIBMCACHE.SYS,3,40,255"
  48.       "\nISAM.EXE,3,40,255"
  49.       "\nISAM2.EXE,3,40,255"
  50.       "\nISQL.EXE,3,40,255"
  51.       "\nMSD.EXE,5,00,255"
  52.       "\nNET3.COM,3,40,255"
  53.       "\nNETX.COM,4,00,255"
  54.       "\nNETX.EXE,5,00,255"
  55.       "\nPSCPG.COM,3,40,255"
  56.       "\nSAF.EXE,3,40,255"
  57.       "\nWIN200.BIN,10,10,4",
  58.    //"DPMI_DOS_API="   // select one of the following
  59.       //"AUTO",
  60.       //"ENABLED",
  61.       //"DISABLED",
  62.    //"DPMI_MEMORY_LIMIT=4",
  63.    //"DPMI_NETWORK_BUFF_SIZE=8",
  64.    //"EMS_FRAME_LOCATION="
  65.       //"AUTO",
  66.       //"NONE",
  67.       //"C000",
  68.       //"C400",
  69.       //"C800",
  70.       //"CC00",
  71.       //"D000",
  72.       //"D400",
  73.       //"D800",
  74.       //"DC00",
  75.       //"8000",
  76.       //"8400",
  77.       //"8800",
  78.       //"8C00",
  79.       //"9000",
  80.    //"EMS_HIGH_OS_MAP_REGION=0",
  81.    //"EMS_LOW_OS_MAP_REGION=384",
  82.    //"EMS_MEMORY_LIMIT=2048",
  83.    //"HW_NOSOUND=0",      // 0 or 1
  84.    //"HW_ROM_TO_RAM=0",   // 0 or 1
  85.    //"HW_TIMER=0",        // 0 or 1
  86.    //"IDLE_SECONDS=0",
  87.    //"IDLE_SENSITIVITY=75",
  88.    //"INT_DURING_IO=0",   // 0 or 1
  89.    //"KBD_ALTHOME_BYPASS=0", // 0 or 1
  90.    //"KBD_BUFFER_EXTEND=1",  // 0 or 1
  91.    "KBD_CTRL_BYPASS="   // select one of the following
  92.       "NONE",
  93.       //"ALT_ESC",
  94.       //"CTRL_ESC",
  95.    //"KBD_RATE_LOCK=0",   // 0 or 1
  96.    //"MEM_EXCLUDE_REGIONS=",
  97.    //"MEM_INCLUDE_REGIONS=",
  98.    //"MOUSE_EXCLUSIVE_ACCESS=0",   // 0 or 1
  99.    //"NETWARE_RESOURCES=" // select one of the following
  100.       //"PRIVATE",
  101.       //"GLOBAL ",
  102.       //"NONE   ",
  103.    //"PRINT_SEPARATE_OUTPUT=1",    // 0 or 1
  104.    //"PRINT_TIMEOUT=15",
  105.    //"VIDEO_8514A_XGA_IOTRAP=1",   // 0 or 1
  106.    //"VIDEO_FASTPASTE=0",          // 0 or 1
  107.    //"VIDEO_MODE_RESTRICTION="  // select one of the following
  108.       //"NONE         ",
  109.       //"CGA            ",
  110.       //"MONO           ",
  111.    //"VIDEO_ONDEMAND_MEMORY=1",    // 0 or 1
  112.    //"VIDEO_RETRACE_EMULATION=1",  // 0 or 1
  113.    //"VIDEO_ROM_EMULATION=1",      // 0 or 1
  114.    //"VIDEO_SWITCH_NOTIFICATION=0",// 0 or 1
  115.    //"VIDEO_WINDOW_REFRESH=1",
  116.    //"XMS_HANDLES=32",
  117.    //"XMS_MEMORY_LIMIT=2048",
  118.    //"XMS_MINIMUM_HMA=0",
  119. };
  120.  
  121. #define WINDOWS_EXECUTABLE    "WINOS2.COM"
  122. #define DEFAULT_WINDOWS_PROG  "PROGMAN.EXE"
  123. #define INTERVAL_BETWEEN_LOOKS   1000  // how long to wait between checking if
  124.                                        // window exists yet
  125. #define MAXIMUM_LOOKS_FOR_WINDOW 25    // after looking 25 times for window,
  126.                                        // then give up
  127. #define WAIT_INTERVAL            1500  // how many milliseconds (approx.) to
  128.                                        // wait between checking if session has
  129.                                        // ended; if /WAIT specified
  130.  
  131. // To start Full-screen window, must define where to locate CEnvi2
  132. // for windows and also define a file name to pass the information in.
  133. //#define CENVI_FOR_WINDOWS     `E:\Nombas\CEnvi\Win\CEnviW.exe`
  134. //#define CENVIW_COMMAND_FILE   `E:\TEMP\_FS_WIN.CMM`
  135.  
  136.  
  137. #include <OptParms.lib>    // for parsing command-line parameters
  138. #include <Profile.lib>     // for getting and setting font size or objects
  139. #include <PMdll.lib>       // used by WinTools.lib
  140. #include <WinTools.lib>    // adjust window size and position
  141.  
  142. main(argc,argv)
  143. {
  144.    if ( argc == 1
  145.      || OptionalParameter(argc,argv,"?")
  146.      || OptionalParameter(argc,argv,"help")
  147.      || OptionalParameter(argc,argv,"h") ) {
  148.       Instructions();
  149.       success = False;
  150.    } else {
  151.       ParseInputParameters(argc,argv);
  152.  
  153.       if ( !ObjectSupplied ) {
  154.          if ( Windows && FullScreen && BackGround )
  155.             BuildCEnviForWindowsCode();
  156.          StartData = BuildStartData();
  157.       }
  158.  
  159.       if ( Background )
  160.          RememberActiveWindow = GetActiveWindow();
  161.  
  162.       if ( SetFont ) {
  163.          GetCharacterFont(SaveWidth,SaveHeight);
  164.          SetCharacterFont(FontX,FontY);
  165.       }
  166.  
  167.       if ( LocateTitle )
  168.          OldWindowList = BuildWindowList();
  169.       else
  170.          OldSwitchList = WinQuerySwitchList(Info().hab);
  171.  
  172.       success = ObjectSupplied ? StartObject() : StartSession(StartData);
  173.  
  174.       if ( SetFont ) {
  175.          if ( ObjectSupplied )
  176.             suspend(750); // objects sometimes need a little extra time to get going
  177.          SetCharacterFont(SaveWidth,SaveHeight);
  178.       }
  179.  
  180.       if ( success ) {
  181.  
  182.          if ( Windows && FullScreen && BackGround )
  183.             WaitForCEnviForWindowsToEnd();
  184.  
  185.          if ( MustWait || Background || WantBig || Maximized
  186.            || Minimized || Restored || ForeGround || WantSetSize
  187.            || WantSetPosition || Hidden || TitleSupplied ) {
  188.  
  189.             if ( PreDelay )  suspend(PreDelay);
  190.  
  191.             if ( !(WinHandle = LocateTitle
  192.                              ? FindNewWindowTitle(OldWindowList)
  193.                              : FindNewSwitchWindowHandle(OldSwitchList,SwitchHandle)) ) {
  194.                printf("\n\aUnable to locate \"%s\"\n",Title);
  195.                success = False;
  196.             } else {
  197.                if ( PostDelay )  suspend(PostDelay);
  198.                AdjustWindow(WinHandle,SwitchHandle);
  199.             }
  200.          }
  201.       }
  202.  
  203.       if ( Background )
  204.          SetActiveWindow(RememberActiveWindow);
  205.  
  206.       if ( success && MustWait ) {
  207.          // hang around until WinHandle is no longer valid
  208.          do {
  209.             suspend(WAIT_INTERVAL);
  210.          } while ( IsWindow(WinHandle) );
  211.       }
  212.  
  213.    }
  214.    return success ? EXIT_SUCCESS : EXIT_FAILURE ;
  215. }
  216.  
  217. ForeGround, BackGround;
  218. PreDelay, PostDelay;
  219. Title; TitleSupplied;
  220. LocateTitle;
  221. ObjectID; ObjectSupplied;
  222. InheritConfigSysEnvironment;
  223. FullScreen, Windowed, PresentationManager, DOSApplication;
  224. Windows, Enhanced, Separate;
  225. IconFile;
  226. Hidden, Maximized, Minimized, Restored, WantBig, Keep, MustWait;
  227. WantSetPosition, PosX, XFromRightEdge, PosY, YFromTopEdge;
  228. WantSetSize, SizeX, SizeY;
  229. SetFont, FontX, FontY;
  230. UseDosSettings;
  231. ProgramName, ProgramInputs;
  232. #define ERROR_BUFFER_SIZE  300
  233. ErrorBuffer[0] = ErrorBuffer[ERROR_BUFFER_SIZE+1] = '\0';
  234.  
  235. ParseInputParameters(argc,argv)
  236. {
  237.    ForeGround = OptionalParameter(argc,argv,"F");
  238.    BackGround = OptionalParameter(argc,argv,"B");
  239.    Title = NULL; TitleSupplied = OptionalParameter(argc,argv,"TITLE",Title);
  240.    if ( !OptionalParameter(argc,argv,"LOCATE",LocateTitle) ) LocateTitle = NULL;
  241.    if ( (ObjectSupplied = OptionalParameter(argc,argv,"OBJ",ObjectID)) && !Title )
  242.       strcpy(Title,ObjectID);
  243.    PreDelay = OptionalParameter(argc,argv,"PREDELAY",lTemp)
  244.             ? atoi(lTemp) : 0 ;
  245.    PostDelay = OptionalParameter(argc,argv,"POSTDELAY",lTemp)
  246.              ? atoi(lTemp) : 0 ;
  247.    InheritConfigSysEnvironment = OptionalParameter(argc,argv,"I");
  248.    FullScreen = OptionalParameter(argc,argv,"FS");
  249.    Windowed = OptionalParameter(argc,argv,"WIN");
  250.    PresentationManager = OptionalParameter(argc,argv,"PM");
  251.    DosApplication = OptionalParameter(argc,argv,"DOS");
  252.    Enhanced = OptionalParameter(argc,argv,"ENH");
  253.    Separate = OptionalParameter(argc,argv,"SEP");
  254.    Windows = OptionalParameter(argc,argv,"W")
  255.           || Separate || Enhanced ;
  256.    IconFile = NULL; OptionalParameter(argc,argv,"ICON",IconFile);
  257.    Hidden = OptionalParameter(argc,argv,"HID");
  258.    Maximized = OptionalParameter(argc,argv,"MAX");
  259.    Minimized = OptionalParameter(argc,argv,"MIN");
  260.    Restored = OptionalParameter(argc,argv,"RESTORE");
  261.    WantBig = OptionalParameter(argc,argv,"BIG");
  262.    Keep = OptionalParameter(argc,argv,"K");
  263.    MustWait = OptionalParameter(argc,argv,"WAIT");
  264.    XFromRightEdge = YFromTopEdge = False;
  265.    if ( WantSetPosition = OptionalParameter(argc,argv,"POS",string) ) {
  266.       // Check for XFromRightEdge
  267.       if ( 'r' == tolower(string[0]) ) {
  268.          XFromRightEdge = True;
  269.          string++;
  270.       }
  271.       // Check for YFromTopEdge
  272.       lTopCharOff = strcspn(string,"Xx,") + 1;
  273.       if ( 't' == tolower(string[lTopCharOff]) ) {
  274.          YFromTopEdge = True;
  275.          strcpy(string+lTopCharOff,string+lTopCharOff+1);
  276.       }
  277.       WantSetPosition = ScanfRowCol(string,PosX,PosY);
  278.    }
  279.    WantSetSize = ( OptionalParameter(argc,argv,"SIZE",string)
  280.                 && ScanfRowCol(string,SizeX,SizeY) );
  281.    SetFont = ( OptionalParameter(argc,argv,"FONT",string)
  282.             && ScanfRowCol(string,FontY,FontX) );
  283.    UseDosSettings = DosApplication || Windows;
  284.    while ( OptionalParameter(argc,argv,"SET",string) ) {
  285.       UseDosSettings = True;
  286.       switch ( string[0] ) {
  287.          case '@':
  288.             GetSettingsFromFile(string+1);
  289.             break;
  290.          case '#':
  291.             GetSettingsFromEnvironmentVariable(string+1);
  292.             break;
  293.          default:
  294.             AddNewSetting(string);
  295.             break;
  296.       }
  297.    }
  298.  
  299.    // any further arguments are the ProgramName and ProgramInputs
  300.    ProgramName = (argc == 1) ? NULL : argv[1] ;
  301.    if ( argc < 3 ) {
  302.       ProgramInputs = NULL;
  303.    } else {
  304.       // build program input string of all further arguments together
  305.       strcpy(ProgramInputs,argv[2]);
  306.       for ( i = 3; i < argc; i++ ) {
  307.          strcat(ProgramInputs," ");
  308.          strcat(ProgramInputs,argv[i]);
  309.       }
  310.    }
  311.  
  312.    if ( Windows ) {
  313.       // Windows needs a default program, so use Program Manager
  314.       if ( NULL == ProgramName )
  315.          ProgramName = DEFAULT_WINDOWS_PROG;
  316.    }
  317.  
  318.    if ( !Title ) {
  319.       // YUCK!!! No one likes a session with no title. So give it one.
  320.       // Title will be all but path of ProgramName.  But if no program
  321.       // name then will match the sesion type.
  322.       if ( ProgramName != NULL ) {
  323.          strcpy(Title,ProgramName + strlen(SplitFileName(ProgramName).dir));
  324.       }
  325.       if ( !Title || !Title[0] ) {
  326.          // Get name from session type
  327.          if ( UseDosSettings ) Title = "DOS Session";
  328.          else if ( Windows ) Title = "Windows Session";
  329.          else if ( PresentationManager ) Title = "PM Session";
  330.          else Title = "Session";
  331.       }
  332.    }
  333.  
  334. }
  335.  
  336. ScanfRowCol(string,Row,Col)  // scan ROWxCOL or ROWXCOL or ROW,COL, or return FALSE
  337. {
  338.    return( 2 == sscanf(string,"%dx%d",Row,Col)
  339.         || 2 == sscanf(string,"%dX%d",Row,Col)
  340.         || 2 == sscanf(string,"%d,%d",Row,Col) );
  341. }
  342.  
  343. MyDefaultDOSSettings;
  344.  
  345. AddNewSetting(setting)
  346.    // add setting string to MyDefaultDOSSettings, or replace one that is
  347.    // already there if it has the same "NAME=VALUE" name
  348. {
  349.    // Find the length of variable name: all characters before '='
  350.    VarLen = strcspn(setting,"=");
  351.    if ( 0 == VarLen  ||  strlen(setting) == VarLen ) {
  352.       printf("\n\aInvalid setting \"%s\" ignored.",setting);
  353.       printf("\nPress any key to continue...");
  354.       getch();
  355.       printf("\n");
  356.    } else {
  357.       SettingIndex = 0;
  358.       if ( defined(MyDefaultDOSSettings) ) {
  359.          // check existing settings if this is already in the list
  360.          MaxSettingIndex = GetArraySpan(MyDefaultDOSSettings);
  361.          for( ; SettingIndex <= MaxSettingIndex; SettingIndex++ ) {
  362.             if ( !memicmp(MyDefaultDOSSettings[SettingIndex],setting,
  363.                           VarLen+1) )
  364.                break;
  365.          }
  366.       }
  367.       strcpy(MyDefaultDOSSettings[SettingIndex],setting);
  368.    }
  369. }
  370.  
  371. GetSettingsFromFile(FileName)
  372.    // read in each setting line from file, and add to DOS settings
  373. {
  374.    fp = fopen(FileName,"r");
  375.    if ( NULL == fp ) {
  376.       printf("\n\aUnable to open settings file \"%s\"",FileName);
  377.       printf("\nPress any key to continue...");
  378.       getch();
  379.       printf("\n");
  380.    } else {
  381.       while ( NULL != (setting = fgets(fp)) ) {
  382.          if ( '\n' == setting[len = strlen(setting) - 1] )
  383.             setting[len] = 0;
  384.          if ( ';' != setting[0]  &  '\0' != setting[0] )
  385.             AddNewSetting(setting);
  386.       }
  387.       fclose(fp);
  388.    }
  389. }
  390.  
  391. GetSettingsFromEnvironmentVariable(evar)
  392.    // get settings from environment variable, if there is one.  Individual
  393.    // setting statements are separated by semi-colons
  394. {
  395.    if ( NULL != (string = getenv(evar)) ) {
  396.       for ( NextString = strtok(string,";");
  397.             NULL != NextString; NextString = strtok(NULL,";") ) {
  398.          if ( NULL != (EqualPosition = strchr(NextString,',')) ) {
  399.             EqualPosition[0] = '=';
  400.             AddNewSetting(NextString);
  401.          }
  402.       }
  403.    }
  404. }
  405.  
  406. DetermineSessionType()
  407.    // return integer representing the TYPE of session to start
  408. {
  409.    #define SESSION_DEFAULT             0
  410.    #define SESSION_OS2_FULLSCREEN      1
  411.    #define SESSION_WINDOWABLEVIO       2
  412.    #define SESSION_PM                  3
  413.    #define SESSION_DOS_FULLSCREEN      4
  414.    #define SESSION_DOS_WINDOWED        7
  415.    #define SESSION_WIN_STD_SEPARATE    15
  416.    #define SESSION_WIN_STD             16
  417.    #define SESSION_WIN_ENH_SEPARATE    17
  418.    #define SESSION_WIN_ENH             18
  419.    #define SESSION_WIN_ENH_FULLSCREEN  19
  420.    #define SESSION_WIN_STD_FULLSCREEN  20
  421.  
  422.    if ( PresentationManager ) return SESSION_PM;
  423.    if ( Windows ) {
  424.       if ( Separate )
  425.          return Enhanced ? SESSION_WIN_ENH_SEPARATE : SESSION_WIN_STD_SEPARATE;
  426.       if ( FullScreen )
  427.          // return Enhanced ? SESSION_WIN_ENH_FULLSCREEN : SESSION_WIN_STD_FULLSCREEN;
  428.          return SESSION_DOS_FULLSCREEN;
  429.       return Enhanced ? SESSION_WIN_ENH : SESSION_WIN_STD;
  430.    }
  431.    if ( UseDosSettings ) {
  432.       return ( Windowed && !FullScreen )
  433.            ? SESSION_DOS_WINDOWED : SESSION_DOS_FULLSCREEN ;
  434.    }
  435.    if ( FullScreen ) return SESSION_OS2_FULLSCREEN;
  436.    if ( Windowed ) return SESSION_WINDOWABLEVIO;
  437.    return SESSION_DEFAULT;
  438. }
  439.  
  440. GetDOSSettingsString()
  441.    // return MyDefaultDOSSettings() array as a large string where each
  442.    // element is separaterd by a NULL byte
  443. {
  444.    string = SettingString = "";
  445.    count = GetArraySpan(setting = MyDefaultDOSSettings);
  446.    while( 0 <= count-- ) {
  447.       strcpy(string,setting[0]);
  448.       string += strlen(string) + 1;
  449.       setting++;
  450.    }
  451.    string[0] = '\0';
  452.    return SettingString;
  453. }
  454.  
  455.  
  456. PgmName, PgmInputs, env; // globals used in following function
  457. BuildStartData()
  458.    // Build the StartData structure (BLOb) based on the global data already
  459.    // retrieved.  Return the blob
  460. {
  461.    BLObPut(data,0,UWORD16); // size of block; we'll get back to this
  462.       #define SSF_RELATED_INDEPENDENT 0
  463.       #define SSF_RELATED_CHILD       1
  464.    BLObPut(data,SSF_RELATED_INDEPENDENT,UWORD16);
  465.       #define SSF_FGBG_FORE   0
  466.       #define SSF_FGBG_BACK   1
  467.    BLObPut(data,ForeGround || (Windows && FullScreen && BackGround) ? SSF_FGBG_FORE : SSF_FGBG_BACK,UWORD16);
  468.       #define SSF_TRACEOPT_NONE       0
  469.       #define SSF_TRACEOPT_TRACE      1
  470.       #define SSF_TRACEOPT_TRACEALL   2
  471.    BLObPut(data,SSF_TRACEOPT_NONE,UWORD16);
  472.    if ( Windows  &&  NULL != Title && !Fullscreen ) {
  473.       sprintf(Title-8,"Session:%s",Title);
  474.       BLObPut(data,pointer(Title)-8,UWORD32);
  475.    }
  476.    else
  477.       BLObPut(data,Title ? pointer(Title) : NULL,UWORD32);
  478.  
  479.    // Preform a cheat here with the ProgramName and inputs so that
  480.    // windows programs will be started by WINOS2.COM and be set
  481.    // to enhanced mode if that is wanted
  482.    if ( Windows ) {
  483.       PgmName = WINDOWS_EXECUTABLE;
  484.       if ( NULL == ProgramInputs )
  485.          PgmInputs = ProgramName;
  486.       else
  487.          sprintf(PgmInputs,"%s %s",ProgramName,ProgramInputs);
  488.       if ( Enhanced ) {
  489.          // Add /3 to parameters to start in enhanced mode
  490.          if ( NULL == PgmInputs )
  491.             PgmInputs = "/3";
  492.          else
  493.             sprintf(PgmInputs,"/3 %s",PgmInputs);
  494.       }
  495.    } else {
  496.       PgmName = ProgramName;
  497.       PgmInputs = ProgramInputs;
  498.    }
  499.  
  500.    BLObPut(data,PgmName ? pointer(PgmName) : NULL,UWORD32);
  501.    BLObPut(data,PgmInputs ? pointer(PgmInputs) : NULL,UWORD32);
  502.  
  503.    BLObPut(data,NULL,UWORD32);  // no termination queue
  504.    if ( UseDosSettings  &&  defined(MyDefaultDOSSettings) ) {
  505.       env = GetDOSSettingsString();
  506.       BLObPut(data,pointer(env),UWORD32);
  507.    } else {
  508.       BLObPut(data,NULL,UWORD32);  // no environment setting
  509.    }
  510.       #define SSF_INHERTOPT_SHELL     0
  511.       #define SSF_INHERTOPT_PARENT    1   // for DOS session, inherit drive and directory
  512.    BLObPut( data,
  513.             InheritConfigSysEnvironment ? SSF_INHERTOPT_SHELL : SSF_INHERTOPT_PARENT,
  514.             UWORD16 );
  515.    BLObPut(data,DetermineSessionType(),UWORD16);
  516.    BLObPut(data,IconFile ? pointer(IconFile) : NULL,UWORD32);
  517.    BLObPut(data,NULL,UWORD32);  // no program handle
  518.  
  519.    #define SSF_CONTROL_VISIBLE     0x0000
  520.    #define SSF_CONTROL_INVISIBLE   0x0001
  521.    #define SSF_CONTROL_MAXIMIZE    0x0002
  522.    #define SSF_CONTROL_MINIMIZE    0x0004
  523.    #define SSF_CONTROL_NOAUTOCLOSE 0x0008
  524.    #define SSF_CONTROL_SETPOS      0x8000
  525.    PgmControl = 0;
  526.    if ( Hidden ) PgmControl |= SSF_CONTROL_INVISIBLE;
  527.    if ( Maximized ) PgmControl |= SSF_CONTROL_MAXIMIZE;
  528.    if ( Minimized ) PgmControl |= SSF_CONTROL_MINIMIZE;
  529.    if ( Keep ) PgmControl |= SSF_CONTROL_NOAUTOCLOSE;
  530.    if ( (WantSetPosition || WantSetSize) && !Windowed )
  531.       PgmControl |= SSF_CONTROL_SETPOS;
  532.    BLObPut(data,PgmControl,UWORD16);
  533.  
  534.    if ( !WantSetPosition ) PosX = PosY = 0;
  535.    if ( !WantSetSize ) SizeX = SizeY = 0;
  536.    if ( Windowed ) {
  537.       for ( lNoSet = 0; lNoSet < 4; lNoSet++ )
  538.          BLObPut(data,0,SWORD16);
  539.    } else {
  540.       BLObPut(data,PosX,SWORD16);
  541.       BLObPut(data,PosY,SWORD16);
  542.       BLObPut(data,SizeX,SWORD16);
  543.       BLObPut(data,SizeY,SWORD16);
  544.    }
  545.    BLObPut(data,0,SWORD16);      // reserved byte; required
  546.  
  547.    BLObPut(data,pointer(ErrorBuffer),UWORD32);
  548.    BLObPut(data,ERROR_BUFFER_SIZE,UWORD32);
  549.  
  550.    BLObPut(data,0,BLObSize(data),UWORD16); // size of block
  551.    return data;
  552. }
  553.  
  554. StartSession(StartData)
  555.    // call OS/2 start session functions
  556. {
  557.    #define ORD_DOS32STARTSESSION 37
  558.    rc = DynamicLink("SESMGR",ORD_DOS32STARTSESSION,BIT32,CDECL,
  559.                     StartData,DummySessionID,DummyProcessID);
  560.  
  561.    #define ERROR_SMG_START_IN_BACKGROUND   457
  562.    if ( rc  &&  ERROR_SMG_START_IN_BACKGROUND != rc ) {
  563.       printf("Error %d starting session\a\a\n",rc);
  564.       if ( 0 < strlen(ErrorBuffer) )
  565.          printf("Error in object: %s\n",ErrorBuffer);
  566.       return(False);
  567.    }
  568.    return(True);
  569. }
  570.  
  571. StartObject()
  572. {
  573.    lSetupString = "OPEN=DEFAULT;";
  574.    if ( ( !(lObject = WinQueryObject(ObjectID))
  575.        && !(lObject = FindWPSProgramObject(ObjectID)) )
  576.      || !WinSetObjectData(lObject,lSetupString) ) {
  577.       printf("Error: could not locate or start object \"%s\"\a\a\n",ObjectID);
  578.       return(False);
  579.    }
  580.    return(True);
  581. }
  582.  
  583. FindWPSProgramObject(pObjectSpec)
  584.    // assume this is an object in a folder, but not a real object, and
  585.    // so search the OS2.INI file for information to get this object
  586. {
  587.    // separate folder from object, if no folder then assume desktop
  588.    lFileParts = SplitFileName(pObjectSpec);
  589.    lFolderName = lFileParts.dir;
  590.    if ( !lFolderName[0] ) {
  591.       // no folder spec, so use desktop
  592.       lFolderID = 0xFFFF & WinQueryObject("<WP_DESKTOP>");
  593.    } else {
  594.       if ( '\\' == lFolderName[(lLen = strlen(lFolderName))-1] )
  595.          lFolderName[--lLen] = 0;
  596.       lFolderID = 0xFFFF & WinQueryObject(lFolderName);
  597.    }
  598.    if ( lFolderID ) {
  599.  
  600.       // create buffer that is the string to search objects for
  601.       sprintf(lObjectName,"%s%s",lFileParts.name,lFileParts.ext);
  602.       lMatchBufLen = 2 + strlen(lObjectName) + 1;
  603.       BLObPut(lMatchBuf,0,lMatchBufLen-2,UWORD16);
  604.       BLObPut(lMatchBuf,2,lObjectName,lMatchBufLen-2);
  605.  
  606.       // Find this folder in OS2.INI under PM_Abstract:FldrContent
  607.       sprintf(lKey,"%X",lFolderID);
  608.       BLObSize(lData,lDataSize=4000);
  609.       if ( PrfQueryProfileData(HINI_USERPROFILE,"PM_Abstract:FldrContent",lKey,
  610.                                lData,lDataSize) ) {
  611.          // try on each object within this folder
  612.          for ( lOffset = 0; lOffset < lDataSize; lOffset += 4 ) {
  613.             lObjectID = BLObGet(lData,lOffset,UWORD32);
  614.             // try to find this object ID within PM_Abstract:Objects
  615.             sprintf(lKey,"%X",lObjectID);
  616.             BLObSize(lObjData,lObjDataSize=4000);
  617.             if ( PrfQueryProfileData(HINI_USERPROFILE,"PM_Abstract:Objects",lKey,
  618.                                      lObjData,lObjDataSize) ) {
  619.                while ( 0 < lObjDataSize
  620.                     && NULL != (lFindData=memchr(lObjData,lMatchBuf[0],lObjDataSize)) ) {
  621.                   if ( !memicmp(lFindData,lMatchBuf,lMatchBufLen) )
  622.                      // YEA! FINALLY THE OBJECT WAS FOUND
  623.                      return( 0x20000 | lObjectID );
  624.                   lDataSkip = lFindData - lObjData + 1;
  625.                   lObjDataSize -= lDataSkip;
  626.                   lObjData += lDataSkip;
  627.                }
  628.             }
  629.          }
  630.       }
  631.    }
  632.    return(NULL);
  633. }
  634.  
  635. GetCharacterFont(FontWidth,FontHeight)
  636. {
  637.    // Get current size
  638.    BLObSize(current,2);
  639.    PrfQueryProfileData(HINI_USERPROFILE,"Shield","~Font Size...",current,2);
  640.    FontHeight = BLObGet(current,1,UWORD8);
  641.    FontWidth = BLObGet(current,0,UWORD8);
  642. }
  643.  
  644. SetCharacterFont(FontWidth,FontHeight)
  645. {
  646.    BLObPut(new,0,FontWidth,UWORD8);
  647.    BLObPut(new,1,FontHeight,UWORD8);
  648.    PrfWriteProfileData(HINI_USERPROFILE,"Shield","~Font Size...",new,2);
  649. }
  650.  
  651. FindNewSwitchWindowHandle(pOldSwitchList,pSwitchHandle)
  652. {
  653.    lOldSwitchCount = 1 + GetArraySpan(pOldSwitchList);
  654.  
  655.    // keep trying until window has been found
  656.    for ( retry = MAXIMUM_LOOKS_FOR_WINDOW; retry; retry--, suspend(INTERVAL_BETWEEN_LOOKS) ) {
  657.  
  658.       lNewSwitchCount = 1 + GetArraySpan(lNewSwitchList = WinQuerySwitchList(Info().hab));
  659.  
  660.       for ( lSwitchIdx = 0; lSwitchIdx < lNewSwitchCount; lSwitchIdx++ ) {
  661.          lSwitchHandle = lNewSwitchList[lSwitchIdx];
  662.          for ( lIdx = 0; lIdx < lOldSwitchCount; lIdx++ ) {
  663.             if ( pOldSwitchList[lIdx] == lSwitchHandle )
  664.                // this entry was already in our list, so ignore it
  665.                break;
  666.          }
  667.          if ( lIdx == lOldSwitchCount
  668.            && !WinQuerySwitchEntry(lSwitchHandle,lSwitchData) ) {
  669.  
  670.             // This is new entry.  If it is not the false seamless windows
  671.             // entry then use it.
  672.             if ( Windows && !FullScreen && !strcmp(Title-8,lSwitchData.title) )
  673.                continue;
  674.  
  675.             // if null window handle then try to find window with this process
  676.             if ( !lSwitchData.hwnd ) {
  677.                lSwitchData.hwnd = FindWindowForProcess(lSwitchData.process);
  678.             }
  679.  
  680.             if ( lSwitchData.hwnd ) {
  681.                pSwitchHandle = lSwitchHandle;
  682.                return( lSwitchData.hwnd );
  683.             }
  684.          }
  685.       }
  686.    }
  687.    return(NULL);
  688. }
  689.  
  690. FindWindowForProcess(pID)  // return window with this process ID
  691. {
  692.    lEnum = WinBeginEnumWindows(HWND_DESKTOP);
  693.    while ( lHwnd = WinGetNextWindow(lEnum) ) {
  694.       #define ORD_WIN32QUERYWINDOWPROCESS  838
  695.       undefine(lProcessID); undefine(lThreadID);
  696.       if ( DynamicLink( "PMWIN", ORD_WIN32QUERYWINDOWPROCESS, BIT32, CDECL,
  697.                         lHwnd, lProcessID, lThreadID )
  698.         && pID == lProcessID )
  699.          break;
  700.    }
  701.    WinEndEnumWindows(lEnum);
  702.    return ( lHwnd );
  703. }
  704.  
  705. FindNewWindowTitle(pOldWindowList)
  706. {
  707.    lOldWindowCount = 1 + GetArraySpan(pOldWindowList);
  708.    lTitleLen = strlen(LocateTitle);
  709.  
  710.    // keep trying until window has been found
  711.    for ( retry = MAXIMUM_LOOKS_FOR_WINDOW; retry; retry--, suspend(INTERVAL_BETWEEN_LOOKS) ) {
  712.  
  713.       lNewWindowCount = 1 + GetArraySpan(lNewWindowList = BuildWindowList());
  714.  
  715.       for ( lWinIdx = 0; lWinIdx < lNewWindowCount; lWinIdx++ ) {
  716.          lWinHandle = lNewWindowList[lWinIdx];
  717.          for ( lIdx = 0; lIdx < lOldWindowCount; lIdx++ ) {
  718.             if ( pOldWindowList[lIdx] == lWinHandle )
  719.                // this entry was already in our list, so ignore it
  720.                break;
  721.          }
  722.          if ( lIdx == lOldWindowCount
  723.            && (lTitle = GetWindowTitle(lWinHandle))
  724.            && !strnicmp(lTitle,LocateTitle,lTitleLen) ) {
  725.             return( lWinHandle );
  726.          }
  727.       }
  728.    }
  729.    return(NULL);
  730. }
  731.  
  732. BuildWindowList() // return array of top-level windows
  733. {
  734.    lEnum = WinBeginEnumWindows(HWND_DESKTOP);
  735.    for ( lIdx = 0; lHwnd = WinGetNextWindow(lEnum); lIdx++ )
  736.       lList[lIdx] = lHwnd;
  737.    WinEndEnumWindows(lEnum);
  738.    return lList;
  739. }
  740.  
  741. AdjustWindow(pHwnd,pSwitchHandle)
  742. {
  743.    if ( WantBig || Maximized ) {
  744.       ShowWindow(pHwnd,ForeGround ? SW_SHOWMAXIMIZED : SW_SHOWMAXNOACTIVE);
  745.       if ( WantBig ) {
  746.          // get maximized size, then restore but at maximum size
  747.          GetWindowRect(pHwnd,rect);
  748.          ShowWindow(pHwnd,ForeGround ? SW_RESTORE : SW_RESTORENOACTIVE);
  749.          SetWindowRect(pHwnd,rect);
  750.       }
  751.    }
  752.    if ( Restored )
  753.       ShowWindow(pHwnd,ForeGround ? SW_RESTORE : SW_RESTORENOACTIVE);
  754.    if ( ForeGround ) {
  755.       SetActiveWindow(pHwnd);
  756.       WinSwitchToProgram(pSwitchHandle);
  757.    }
  758.    if ( WantSetPosition && (XFromRightEdge || YFromTopEdge) ) {
  759.       // determine position relative to different edges of screen
  760.       GetScreenSize(lScreenWidth,lScreenHeight);
  761.       if ( !WantSetSize )
  762.          GetSize(pHwnd,SizeX,SizeY);
  763.       if ( XFromRightEdge )
  764.          PosX = lScreenWidth - PosX - SizeX;
  765.       if ( YFromTopEdge )
  766.          PosY = lScreenHeight - PosY - SizeY;
  767.    }
  768.    if ( WantSetSize ) {
  769.       if ( WantSetPosition )
  770.          SetWindowPos(pHwnd,0,PosX,PosY,SizeX,SizeY,SWP_SIZE | SWP_MOVE);
  771.       else
  772.          SetSize(pHwnd,SizeX,SizeY);
  773.    } else if ( WantSetPosition )
  774.       SetPosition(pHwnd,PosX,PosY);
  775.    if ( Minimized )
  776.       ShowWindow(pHwnd,ForeGround ? SW_SHOWMINIMIZED : SW_SHOWMINNOACTIVE);
  777.    if ( Hidden )
  778.       ShowWindow(pHwnd,SW_HIDE);
  779.    if ( TitleSupplied )
  780.       SetWindowTitle(pHwnd,Title);
  781. }
  782.  
  783. BuildCEnviForWindowsCode()
  784. {
  785.    // Build file to run this command via CEnvi2 for Windows so that
  786.    // full-screen background windows can start that program.
  787.    if ( !defined(CENVI_FOR_WINDOWS) || !defined(CENVIW_COMMAND_FILE) ) {
  788.       printf("\aERROR! For Background Full-Screen windows sessions, must edit SESSION.CMD\n"
  789.              "and #define CENVI_FOR_WINDOWS and CENVIW_COMMAND_FILE.\n");
  790.       exit(EXIT_FAILURE);
  791.    }
  792.    if ( fp = fopen(CENVIW_COMMAND_FILE,"wt") ) {
  793.       fprintf(fp,"#include <WinTools.lib>\n");
  794.       fprintf(fp,"spawn(P_NOWAIT,`%s %s`);\n",ProgramName,ProgramInputs?ProgramInputs:"");
  795.       fprintf(fp,"ShowWindow(`Desktop`,SW_RESTORE);\n");
  796.       fprintf(fp,"remove(`%s`);",CENVIW_COMMAND_FILE);
  797.       fclose(fp);
  798.       ProgramName = CENVI_FOR_WINDOWS;
  799.       ProgramInputs = CENVIW_COMMAND_FILE;
  800.    }
  801. }
  802.  
  803. WaitForCEnviForWindowsToEnd()
  804. {
  805.    for ( retry = MAXIMUM_LOOKS_FOR_WINDOW; retry; retry--, suspend(INTERVAL_BETWEEN_LOOKS) ) {
  806.       if ( !Directory(CENVIW_COMMAND_FILE) )
  807.          break;
  808.    }
  809. }
  810.  
  811. Instructions()
  812. {
  813.    puts(``)
  814.    puts(`Session - Start a session`)
  815.    puts(``)
  816.    puts(`SYNTAX: Session [Options] [Command] [Command arguments...]`)
  817.    puts(``)
  818.    puts(`Options: /F - Start session in the foreground`)
  819.    puts(`         /B - Start session in the background`)
  820.    puts(`         /TITLE "Program Title" - Set Session Window Title`)
  821.    puts(`            For Windows sessions this does not set window title`)
  822.    puts(`         /I - Inherit environment of CONFIG.SYS, not current settings`)
  823.    puts(`         /FS - Full-Screen DOS, OS/2, or Windows session`)
  824.    puts(`         /WIN - Windowed DOS, OS/2, or Windows session`)
  825.    puts(`         /PM - Presentation Manager application`)
  826.    puts(`         /DOS - DOS application`)
  827.    puts(`         /W - Windows application`)
  828.    puts(`         /SEP - Separate Windows session (for /W)`)
  829.    puts(`                Always Separate sessions in this version (unless /OBJ)`)
  830.    puts(`         /ENH - Enhanced-mode Windows session`)
  831.    puts(`         /ICON FileSpec - Full name of an Icon file`)
  832.    puts(`         /HID - Window initially hidden`)
  833.    puts(`         /MAX - Initially maximized`)
  834.    puts(`         /MIN - Initially minimized`)
  835.    puts(`         /RESTORE - Initially in restored state`)
  836.    puts(`         /BIG - Not maximized, but at maximum size`)
  837.    puts(`         /LOCATE "Title" - Find new title (partial, case-insensitive) to adjust`)
  838.    puts(`         /PREDELAY time - Time (milliseconds) to delay before finding window`)
  839.    puts(`         /POSTDELAY time - Delay time (milliseconds) after finding window`)
  840.    puts(`         /K - Keep Windowed session open after program completes`)
  841.    puts(`         /WAIT - Wait until session ends`)
  842.    puts(`         /POS [r]X,[t]Y - Set the X and Y position (0,0 is lower-left corner);`)
  843.    puts(`                          r-relative to right edges; t-relative to top`)
  844.    puts(`         /SIZE X,Y - Set the width (X) and height (Y) of session`)
  845.    puts(`         /FONT YxX - Set Height (Y) and Width (X) of DOS or OS/2 windowed font`)
  846.    puts(`         /SET SET=VALUE - Dos Settings, any number of these allowed`)
  847.    puts(`         /SET @FileSpec - Get DOS settings from FileSpec; ignore blank and ';'`)
  848.    puts(`         /SET #ENVVAR - Get DOS settings from environment variable <ENVVAR>`)
  849.    puts(`              where values in NAME,VAL;NAME,VAL format`)
  850.    puts(`         /OBJ ObjectID - Object ID of an existing program object or fully-`)
  851.    puts(`                         qualified filename or directory; these settings`)
  852.    puts(`                         override SESSION settings. ( e.g., /OBJ <WP_DESKTOP>,`)
  853.    puts(`                         /OBJ E:\Utl\MyProg.exe, /OBJ "C:\Desktop\DOS Window" )`)
  854.    puts(``)
  855.    puts(`NOTE: SESSION.CMD contains a list of default DOS settings.  You may want to`)
  856.    puts(`      alter these by editing SESSION.CMD and change MyDefaultDOSSettings`)
  857.    puts(`      DOS settings are only specified for session if the /DOS, /W, or /SET`)
  858.    puts(``)
  859.    puts(`      If command arguments take any of the same flags as SESSION.CMD then you`)
  860.    puts(`      May want to include those in quotes and with spaces.  For example:`)
  861.    puts(`          SESSION /WIN CMD.EXE "/K mode 80,50"`)
  862. }
  863.  
  864.