home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / swtchto1.zip / switchto.c < prev    next >
C/C++ Source or Header  |  1999-03-27  |  2KB  |  90 lines

  1. /*
  2.  
  3. Given a list of tasks in the switch list, switches to the first one it finds.
  4.  
  5. Beware: the task names are case-sensitive.
  6.  
  7. I use it for xfree86... When it starts, it's "xinitrc.cmd", but
  8. sometimes it turns into something else like "xpmroot.exe", so I have
  9. an icon associated with this program with "xinitrc.cmd xpmroot.exe" as
  10. parameters.  (I have another one in xfree86, with "Desktop" as the
  11. parameter, to switch back to PM.)
  12.  
  13. If none of the tasks exist, it just exits.
  14.  
  15. You may use this code however you wish.  It has absolutely no warranty 
  16. whatsoever.  If you find it useful, and are feeling mildly good-hearted,
  17. you could drop me a note at collinsr@cs.rpi.edu, but don't feel bad if you
  18. don't get around to it... :)
  19.  
  20. -Roddy
  21. collinsr@cs.rpi.edu
  22.  
  23. */
  24.  
  25. #define INCL_WIN
  26. #define INCL_PM
  27. #include <os2.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31.  
  32.  
  33. void usage(char *argv0);
  34.  
  35. int main(int argc, char *argv[]) {
  36. HAB hab=0;
  37. SWBLOCK *pList;
  38. char *pName;
  39. int nEntries, rc, okay, i, j, bufsize;
  40.  
  41.   if (argc < 2) {
  42.     usage(argv[0]);
  43.     exit(1);
  44.   }
  45.   
  46.   nEntries = WinQuerySwitchList(hab, (SWBLOCK *)0, 0);
  47.  
  48.   if (nEntries == 0) {
  49.     fprintf(stderr, "WinQuerySwitchList error!\n");
  50.     exit(2);
  51.   }
  52.  
  53.   fprintf(stderr, "%d entries in list\n", nEntries);
  54.  
  55.   bufsize = (nEntries * sizeof(SWENTRY)) + sizeof(HSWITCH);
  56.   pList = (SWBLOCK *) malloc(bufsize);
  57.   rc = WinQuerySwitchList(hab, pList, bufsize);
  58.   if (rc == 0) {
  59.     fprintf(stderr, "WinQuerySwitchList error!\n");
  60.     exit(2);
  61.   }
  62.  
  63.   okay = 0;
  64.   for (j=1; (okay == 0) && (j<argc); j++) {
  65.     for (i=0; (okay == 0) && (i<nEntries); i++) {
  66.       if (j==1) fprintf(stderr, "%d: %s\n", i, pList->aswentry[i].swctl.szSwtitle);
  67.       if (strcmp(pList->aswentry[i].swctl.szSwtitle, argv[j])==0) {
  68.     rc = WinSwitchToProgram(pList->aswentry[i].hswitch);
  69.     if (rc == 0) {
  70.       okay++;
  71.       fprintf(stderr, "switched to %s\n", pList->aswentry[i].swctl.szSwtitle);
  72.     } else {
  73.       fprintf(stderr, "WinSwitchToProgram error %d\n", rc);
  74.     } 
  75.       }
  76.     }
  77.     if (!okay) fprintf(stderr, "no match for %s\n", argv[j]);
  78.   }
  79.   return(0);
  80. }
  81.  
  82.  
  83.  
  84. void usage(char *argv0) 
  85. {
  86.   fprintf(stderr, "usage: %s task1 [task2 task3 ... taskN]\n", argv0);
  87.   fprintf(stderr, "switches to first instance of task1 in the task list.\n");
  88.   fprintf(stderr, "If task1 isn't found, moves to task2, task3, etc.\n");
  89. }
  90.