home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / pmsw.zip / PMSW / PMSWALL.ZIP / CSOURCE / PMSW2 / PMSW2.C < prev    next >
Text File  |  1994-08-10  |  11KB  |  234 lines

  1. /*
  2.   ┌───────────────────────────────────────────────────────────────┐
  3.   │ PMSW2 - REXX Function to switch to a task by name in the task │
  4.   │ list.  For example:  to switch to TSO mainframe session that  │
  5.   │ is named "A - A - 3270 EMULATOR", provide a mask string as    │
  6.   │ a command line entry:  "*3270 EMULATOR*". PMSW2 will switch   │
  7.   │ to the first task that matches the mask string.  The chars    │
  8.   │ "*" and "?" can be used to indicate "zero or more" and "one"  │
  9.   │ any character(s) respectively.                                │
  10.   │                                                               │
  11.   │ Example:  Task entry:  "A - A - 3270 Emulator"                │
  12.   │ Result=PMSW2("*3270 Emu*");                                   │
  13.   │ OR:  "A*3270*EM*" is a good mask for selection.               │
  14.   │                                                               │
  15.   │ Example:  Task entry:  "CCMAIL.BAT"                           │
  16.   │ Result=PMSW2("*CCMAIL*");                                     │
  17.   │                                                               │
  18.   │ Example:   Result=PMSW2("*");                                 │
  19.   │            request is to switch to self/own session           │
  20.   │                                                               │
  21.   └───────────────────────────────────────────────────────────────┘
  22.   ┌───────────────────────────────────────────────────────────────┐
  23.   │ Copyright (C) 1993,1994 Bruce E. Högman.  All Rights Reserved.│
  24.   │ This program has been dedicated to the Public Domain.         │
  25.   └───────────────────────────────────────────────────────────────┘
  26.   ┌───────────────────────────────────────────────────────────────┐
  27.   │                                                               │
  28.   │ External functions:  mskchk                                   │
  29.   │   (*mask,mlen,*area,alen,*qmark,*ast)                         │
  30.   │ Command line:  pmsw2 "task mask" [/r]                         │
  31.   │   /r:  test only.  Returns:  READY: if task is in list.       │
  32.   │   if no /r, then switch to named task.                        │
  33.   │                                                               │
  34.   │ Returns:  string                                              │
  35.   │   ERROR:  error in processing or task not found.  If error    │
  36.   │           caused by system service failure or bad syntax,     │
  37.   │           a message is displayed on STDERR file handle.       │
  38.   │   READY:  Requested task by name is active and jumpable.      │
  39.   │   FOCUS:  Requested task by name was made focus.              │
  40.   │                                                               │
  41.   └───────────────────────────────────────────────────────────────┘
  42. */
  43. #define INCL_RXFUNC
  44. #define INCL_DOSMEMMGR
  45. #define INCL_DOSPROCESS
  46. #define INCL_WINSWITCHLIST
  47. #define INCL_PM
  48. #include <stdio.h>
  49. #include <OS2.H>
  50. #include <rexxsaa.h>
  51. #include "pmsw2.h"
  52. /*
  53.   ┌───────────────────────────────────────────────────────────────┐
  54.   │ The OS2H include file code is reproduced here to assist in    │
  55.   │ code development.  Be sure to check the latest .H files.      │
  56.   └───────────────────────────────────────────────────────────────┘
  57. */
  58.  #ifdef USERDUMMY
  59. /*
  60.   ┌───────────────────────────────────────────────────────────────┐
  61.   │ SWBLOCK switch-list block structure.                          │
  62.   └───────────────────────────────────────────────────────────────┘
  63. */
  64.  typedef struct _SWBLOCK {
  65.  ULONG      cswentry;     /* Count of switch list entries  */
  66.  SWENTRY    aswentry[1];  /* Switch list entries  */
  67.   } SWBLOCK;
  68. /*
  69.   ┌───────────────────────────────────────────────────────────────┐
  70.   │ SWENTRY switch-list entry structure.                          │
  71.   └───────────────────────────────────────────────────────────────┘
  72. */
  73.  typedef struct _SWENTRY {
  74.  HSWITCH    hswitch;  /* Switch-list entry handle used for focus */
  75.  SWCNTRL    swctl;    /* Switch-list control block structure  */
  76.   } SWENTRY;
  77. /*
  78.   ┌───────────────────────────────────────────────────────────────┐
  79.   │ SWCNTRL switch-list entry structure.                          │
  80.   └───────────────────────────────────────────────────────────────┘
  81. */
  82.  typedef struct _SWCNTRL {
  83.  HWND        hwnd;                   /* Window handle  */
  84.  HWND        hwndIcon;               /* Window-handle icon  */
  85.  HPROGRAM    hprog;                  /* Program handle  */
  86.  PID         idProcess;              /* Process identity  */
  87.  ULONG       idSession;              /* Session identity  */
  88.  UCHAR       uchVisibility;          /* Visibility  */
  89.  UCHAR       fbJump;                 /* Jump indicator  */
  90.  CHAR        szSwtitle[MAXNAMEL+1];  /* Switch-list control block title (null-terminated)  */
  91.  BYTE        bProgType;              /* Program type  */
  92.   } SWCNTRL;
  93.  #endif
  94. /*
  95.   ┌───────────────────────────────────────────────────────────────┐
  96.   │ PMSW2 program entry as RexxFunctionHandler                    │
  97.   └───────────────────────────────────────────────────────────────┘
  98. */
  99.  RexxFunctionHandler PMSW2;
  100.  ULONG PMSW2(
  101.      PUCHAR    Name,                   /* name of the function       */
  102.      ULONG     Argc,                   /* number of arguments        */
  103.      RXSTRING  Argv[],                 /* list of argument strings   */
  104.      PSZ       Queuename,              /* current queue name         */
  105.      PRXSTRING Retstr)                 /* returned result string     */
  106.  {
  107.  BOOL      bFound=FALSE;    /* TRUE if requested task name found */
  108.  BOOL      bQuery=FALSE;    /* if /r flag is used to return info */
  109.  BOOL      bSelf=FALSE;     /* if title/name mask is single asterisk */
  110.  HAB       hAnchorBlock;    /* Anchor-block handle */
  111.  HSWITCH   hswitchSwHandle; /* Window List entry handle of program to be activated */
  112.  int       i;
  113.  int       iMaskLen=0;
  114.  int       iRCmskchk=0;
  115.  UCHAR     pAster[]="*";
  116.  ULONG     pBase;
  117.  UCHAR     pQmark[]="?";
  118.  SWCNTRL  *pSWCItem;
  119.  SWENTRY  *pSWEItem;
  120.  PSWBLOCK  pswblkBlock;     /* Switch entries block */
  121.  PSWBLOCK  pswblkItem;      /* ptr work to an individual SWCNTRL */
  122.  UCHAR     scInputMask[64]="";
  123.  char      scTestString[80]="";
  124.  CHAR      szUCtitle[MAXNAMEL+1]="";
  125.  ULONG     ulNrItems;
  126.  ULONG     ulRetCode=0;     /* Return code *from Win* functions */
  127.  ULONG     ulcbBufLength;
  128.  PTIB      ptib_us;
  129.  PPIB      ppib_us;
  130.  
  131. /*
  132.   ┌───────────────────────────────────────────────────────────────┐
  133.   │ On bad command line syntax (no argument(s)), display msg.     │
  134.   └───────────────────────────────────────────────────────────────┘
  135. */
  136.  if (Argc<1)
  137.  { ErrorReturn:
  138.    fprintf(stderr,"┌────────────────────────────────────────────────┐\n");
  139.    fprintf(stderr,"│ PMSW2 REXX Function call syntax:               │\n");
  140.    fprintf(stderr,"│                                                │\n");
  141.    fprintf(stderr,"│ Retstr=pmsw2(task_name [,\"/r\"]);               │\n");
  142.    fprintf(stderr,"│                                                │\n");
  143.    fprintf(stderr,"│ Returns: FOCUS:/READY:/ERROR:                  │\n");
  144.    fprintf(stderr,"│                                                │\n");
  145.    fprintf(stderr,"│ FOCUS:  task named was made focus.             │\n");
  146.    fprintf(stderr,"│ READY:  /r suppressed FOCUS:                   │\n");
  147.    fprintf(stderr,"│ ERROR:  Error occurred during processing.      │\n");
  148.    fprintf(stderr,"│                                                │\n");
  149.    fprintf(stderr,"│ Control returns immediately to caller in all   │\n");
  150.    fprintf(stderr,"│ cases.  When FOCUS:, then desktop focus has    │\n");
  151.    fprintf(stderr,"│ changed as requested.                          │\n");
  152.    fprintf(stderr,"└────────────────────────────────────────────────┘\n");
  153.    Retstr->strlength=6;
  154.    Retstr->strptr="ERROR:";  return -1; /* cause REXX message too */
  155.  }
  156.  strcpy(scInputMask,Argv[0].strptr); strupr(scInputMask);
  157.  iMaskLen=strlen(scInputMask);
  158.  if (Argc>1)
  159.  { if ((Argv[1].strptr[0]='/')
  160.    && ((Argv[1].strptr[1]='r')| (Argv[1].strptr[1]='R')))
  161.      bQuery=TRUE;
  162.  }
  163.  if (iMaskLen==1 && scInputMask[0]=='*') bSelf=TRUE;
  164. /*
  165.   ┌───────────────────────────────────────────────────────────────┐
  166.   │ If request is to switch to self, then we get info blocks      │
  167.   │ using DosGetInfoBlocks to get PID, then use                   │
  168.   │ WinQuerySwitchHandle with PID data to get hswitch that we     │
  169.   │ can then use in WinSwitchToProgram call.                      │
  170.   └───────────────────────────────────────────────────────────────┘
  171. */
  172.  if (bSelf)
  173.  {
  174.    DosGetInfoBlocks(&ptib_us,&ppib_us);
  175.    hswitchSwHandle=WinQuerySwitchHandle(NULL,ppib_us->pib_ulpid);
  176.    ulRetCode = WinSwitchToProgram( hswitchSwHandle);
  177.    Retstr->strlength=6; /* in all cases */
  178.    if (0<ulRetCode)   /* return ERROR: on failure to switch */
  179.    { Retstr->strptr="ERROR:";
  180.    /*fprintf(stderr,"PMSW2 WinSwitchToProgram RC=%lu\n",ulRetCode);*/
  181.    }
  182.    else Retstr->strptr="FOCUS:"; /* FOCUS: focus has changed */
  183.    return 0;
  184.  }
  185. /*
  186.   ┌───────────────────────────────────────────────────────────────┐
  187.   │ Assemble the contents of the switch list as an array in       │
  188.   │ our area.  We'll walk thru list item by item, checking names  │
  189.   │ using the name mask.                                          │
  190.   └───────────────────────────────────────────────────────────────┘
  191. */
  192.  ulNrItems = WinQuerySwitchList(hAnchorBlock, NULL, 0);
  193.  ulcbBufLength = (ulNrItems * sizeof(SWENTRY)) + sizeof(ULONG);
  194.  pswblkBlock=(SWBLOCK *)malloc(ulcbBufLength);
  195.  /* gets struct. array */
  196.  WinQuerySwitchList( hAnchorBlock, pswblkBlock, ulcbBufLength);
  197.  
  198.  pSWEItem=(SWENTRY *)&(pswblkBlock->aswentry[0]);
  199.  for (i=0;i<ulNrItems;i++)
  200.  { pSWCItem=(SWCNTRL *)&((pSWEItem+i)->swctl);
  201.    strcpy(scTestString,pSWCItem->szSwtitle);
  202.    if (pSWCItem->fbJump&SWL_JUMPABLE)
  203.    { strcpy(szUCtitle,pSWCItem->szSwtitle); strupr(szUCtitle);
  204.      iRCmskchk=mskchk( scInputMask,iMaskLen, szUCtitle,
  205.        strlen(pSWCItem->szSwtitle), pQmark, pAster);
  206.      if (!iRCmskchk)
  207.      { bFound=TRUE; hswitchSwHandle=(pSWEItem+i)->hswitch; }
  208.    }
  209.    if (bFound) break;
  210.  }
  211. /*
  212.   ┌───────────────────────────────────────────────────────────────┐
  213.   │ If the requested task is found, either set the return code    │
  214.   │ and return, or request change of focus to the target window   │
  215.   │ and then return with the FOCUS: return string.                │
  216.   └───────────────────────────────────────────────────────────────┘
  217. */
  218.  Retstr->strlength=6; /* all return string values are 6 chars */
  219.  if (!bQuery)         /* request is to change focus */
  220.  { ulRetCode = WinSwitchToProgram( hswitchSwHandle);
  221.    if (0<ulRetCode)   /* return ERROR: on failure to switch */
  222.    { Retstr->strptr="ERROR:";
  223.    /*fprintf(stderr,"PMSW2 WinSwitchToProgram RC=%lu\n",ulRetCode);*/
  224.    }
  225.    else Retstr->strptr="FOCUS:"; /* FOCUS: focus has changed */
  226.  }
  227.  else                 /* request is to check on readiness for focus */
  228.  { if (bFound) Retstr->strptr="READY:";
  229.    else Retstr->strptr="ERROR:";
  230.  }
  231.  free(pswblkBlock);
  232.  return 0;
  233.  }
  234.