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