home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / CALLDOS.ZIP / CALLDOS.C next >
Text File  |  1990-09-11  |  5KB  |  162 lines

  1. /* Rev 0.1 - document "...\" case                                     */
  2. /* Compiles with IBM C/2 1.1 and its MAKE/2 2.0 using:                  */
  3. /*
  4. ###############################################################
  5. #  Make file for CALLDOS.C                                      #
  6. #                                                              #
  7. #  use MAKE CALLDOS.C                                          #
  8. #                                                              #
  9. ###############################################################
  10. model=S            # use large model when debugging -- bugs JUMP out at you
  11. mode=p
  12. linklibs=$(model)libce$(mode).lib os2.lib
  13. CFLAGS=-A$(model) -Lp -Oxrn -Zpel -W3 -G2 -Gw -J -nologo
  14. LINK=D:\C\Bin\Link
  15.  
  16. calldos.exe : calldos.obj
  17.  $(LINK) calldos.obj,calldos.exe/A:16/FAR/NOD/PACKD/ST:3000,NUL.MAP,$(linklibs);
  18.  
  19. calldos.obj : calldos.c
  20.  !CL $(CFLAGS) -c $?
  21.  
  22. */
  23.  
  24. /* Merge of Peter Fitzsimmons' CALLBOX.C and 2DOS.C programs */
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #define INCL_DOS
  30. #define INCL_DOSERRORS
  31. #define INCL_WINSWITCHLIST
  32. #include <os2.h>
  33.  
  34. void jump2dos(void)
  35. {
  36.     USHORT num, i;
  37.     void *buf = malloc(32000);
  38.     SWENTRY *swe;
  39.     HSWITCH fgHandle;
  40.     SEL selGlobalSeg, selLocalSeg;
  41.     GINFOSEG FAR * volatile pgis;
  42.     UCHAR sgDos;
  43.  
  44.     /* find out who is in the foregound,  so we know who to switch
  45.      * back to after the dos box is done.  This method only works
  46.      * if the foreground session is NOT a pm program.  If it is a
  47.      * pm program,    the "Task List" will be the active app after
  48.      * the task switch
  49.      */
  50.     DosGetInfoSeg(&selGlobalSeg, &selLocalSeg);
  51.     pgis = MAKEPGINFOSEG(selGlobalSeg);
  52.     fgHandle = WinQuerySwitchHandle(0L, pgis->pidForeground);
  53.  
  54.     /* Find the switch list handle of the DOS box: */
  55.     num = WinQuerySwitchList(0L, buf, 32000);
  56.     swe = (SWENTRY *)((USHORT *)buf + 1);
  57.     for(i=0; i<num; i++){
  58.         if(!strcmp("DOS", swe[i].swctl.szSwtitle)){
  59.             WinSwitchToProgram(swe[i].hswitch);
  60.             break;
  61.         }
  62.     }
  63.     free(buf);
  64.     if(i==num){
  65.         printf("This system is configured without a dos box!\n");
  66.         return;
  67.     }
  68.     if(fgHandle){
  69.         DosSleep(500L);     /* give a moment for the task switch */
  70.         sgDos = pgis->sgCurrent;
  71.  
  72.         /*
  73.          * Wait (poll) current screen group -- waiting for the DOS
  74.          * program to end.    I don't like polling,  but this is the only
  75.          * solution I can come up with.  If you don't mind the "Task
  76.          * List" always being brought the the fore after the dos progam
  77.          * ends,  you can forget about all of this and exit() after the
  78.          * WinSwitchToProgram(), above.
  79.          */
  80.  
  81.         while( sgDos == pgis->sgCurrent )
  82.             DosSleep(500L);
  83.  
  84.         WinSwitchToProgram(fgHandle);    /* back to where we started */
  85.     }
  86. }
  87.  
  88. #if 0
  89.  
  90. Comments
  91. --------
  92.  
  93. The only reason this program works is becuase WinSwitchToProgram()
  94. allows you to:
  95.  
  96.     1) Switch a session to the foreground which is not one of your own
  97.        child sessions.
  98.  
  99.     2) Switch a session to the foreground when you are not running
  100.        in the foreground yourself.
  101.  
  102. Acording to the documentation (IBM, v1.20),  you are not supposed to
  103. be able to do either of these things.  However,  the "File Manager",
  104. from what I can figure out,  uses this method to switch to the DOS
  105. box too,  so I am confident that this program will continue to work
  106. under OS/2 1.2x.
  107.  
  108. Peter Fitzsimmons, A:WARE Inc. Thu    09-06-1990    22:15:11
  109. #endif
  110.  
  111. void main(int argc, char **argv)
  112. {
  113.     HFILE hf;
  114.     USHORT rc, dummy;
  115.     static char cmd[256];
  116.     int i;
  117.  
  118.     if( argc < 2 ){
  119.  
  120.  printf("\nUsage:  CALLDOS <command>\n\n");
  121.  printf("   Each <command> with blanks should be enclosed by quotes.\n");
  122.  printf("   Any quoted <command> ending with a '\\' must use '\\ '.\n\n");
  123.  printf("For example:\n\n");
  124.  printf("CALLDOS C: \"CD \\OS2\" \"DIR > \\DIR.LST\" \"CD \\ \"\n\n");
  125.  printf("will make C:\\OS2 current, list the files to C:\\DIR.LST,\n");
  126.  printf("and switch back to C:\\.\n\n");
  127.  printf("If invoked while a PM session is current, the focus returns\n");
  128.  printf("to the Task List, else returns to the current session.\n\n");
  129.         exit (255);
  130.     }
  131.     cmd[0] = '\0';
  132.     for(i=1; i<argc; i++){
  133.         strcat(cmd, "\x80 ");
  134.         strcat(cmd, argv[i]);
  135.         strcat(cmd, "\r");
  136.         if( i != (argc-1) )
  137.             strcat(cmd, " ");
  138.     }
  139.     rc = DosOpen("DOS$", &hf, &dummy, 0L, FILE_NORMAL, FILE_OPEN,
  140.         OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE |
  141.         OPEN_FLAGS_FAIL_ON_ERROR, 0L);
  142.     if(!rc){
  143.         rc = DosWrite(hf, cmd, strlen(cmd)+1, &dummy);
  144.         DosClose(hf);
  145.         if(!rc)
  146.             jump2dos();
  147.         else if(rc == ERROR_NOT_READY) {
  148.             printf("A program is already running in the DOS box\n");
  149.             exit (1);
  150.         }
  151.         else {
  152.             printf("Unexpected OS/2 error SYS%04u\n", rc);
  153.             exit (2);
  154.         }
  155.     }
  156.     else {
  157.         printf("DOS.SYS not installed (sys%04u)\n", rc);
  158.         exit (3);
  159.     }
  160.     DosBeep(1000, 100);
  161. }
  162.