home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / css10.zip / CSS.CPP next >
C/C++ Source or Header  |  1995-12-17  |  17KB  |  476 lines

  1. /*-----------------------------------------------------------------------------
  2. ;                                 ***keywords*** "%l"
  3. ; LOCK STATUS       "PAULG/GO"
  4. ;
  5. ; CSS - Command Shell Starter
  6. ; Copyright (c) Paul Gallagher 1995
  7. ; Source code is released but remains copywrite. Derivative works or
  8. ; appropriation of code should be acknowledged by including the statement
  9. ; "portions copywrite (c) Paul Gallagher 1995" in your documentation.
  10. ;
  11. ;                                 ***keywords*** "%n"
  12. ; Filename          "CSS.CPP"
  13. ; Platform          OS/2 (Borland C++ 1.01)
  14. ;
  15. ; Authors           Paul Gallagher (paulpg@ibm.net)
  16. ;
  17. ; Description
  18. ;                   (v1.00) css - Command Shell Starter
  19. ;                   By dropping a file or folder, opens an
  20. ;                   OS/2 Command Window with working directory
  21. ;                   preset to that of the dropped file/folder.
  22. ;                   With C source. "PostcardWare" - If you get
  23. ;                   any value from this app, please send a
  24. ;                   lively or colorful postcard to my 2yo
  25. ;                   daughter Anita & brighten her day!
  26. ;                   (PO Box 5281 Wollongong 2500 Australia)
  27. ;                   Author: Paul Gallagher, paulpg@ibm.net
  28. ;
  29. ;                                 ***keywords*** "Version: %v  Date: %d %t"
  30. ; "Version: 1  Date: 17-Dec-95 23:46:48"
  31. ;
  32. ; Revision History
  33. ;                                 ***revision-history***
  34. ; 1 CSS.CPP 17-Dec-95,23:46:48,`PAULG/GO' First release
  35. ;                                 ***revision-history***
  36. ;----------------------------------------------------------------------------*/
  37.                                   //
  38. /*-----------------------------------------------------------------------------
  39. ;                   OS & C defines and includes
  40. ;----------------------------------------------------------------------------*/
  41. #define INCL_WIN
  42. #define INCL_GPI
  43. #define INCL_DOSSESMGR
  44. #define INCL_DOSFILEMGR
  45. #include <os2.h>
  46. #include <dir.h>
  47. #include <ctype.h>
  48. #include <process.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <string.h>
  52. #include <stdarg.h>
  53.  
  54. /*-----------------------------------------------------------------------------
  55. ;                   My lib defines and includes
  56. ;----------------------------------------------------------------------------*/
  57.  
  58. /*-----------------------------------------------------------------------------
  59. ;                   Application defines and includes
  60. ;----------------------------------------------------------------------------*/
  61. #include "css.h"
  62. #define  UM_DROP WM_USER
  63. #define WX     10
  64. #define WY     10
  65. #define WXSIZE 200
  66. #define WYSIZE 33
  67.  
  68. /*-----------------------------------------------------------------------------
  69. ;                   Declarations
  70. ;----------------------------------------------------------------------------*/
  71. MRESULT EXPENTRY ClientWndProc (HWND,ULONG,MPARAM,MPARAM);
  72. void             ReSize(HWND hwnd);
  73. VOID             WorkerThread (void);
  74. MRESULT EXPENTRY WorkWndProc (HWND, ULONG, MPARAM, MPARAM);
  75. void             Msg( PSZ szFormat,... );
  76. void             SetCDtoFilePath(PSZ path);
  77. void             RunCmd(PSZ path);
  78.  
  79. /*-----------------------------------------------------------------------------
  80. ;                   Global variables
  81. ;----------------------------------------------------------------------------*/
  82. HAB   hab;
  83. HWND  hWndFrame,
  84.       hWndClient;
  85. HWND  hwndWorker;
  86.  
  87. CHAR  szTitle[64];
  88. CHAR  szMsg1[60],szMsg2[60],szMsg3[60];
  89.  
  90. /*-----------------------------------------------------------------------------
  91. ;                   Function definitions
  92. ;----------------------------------------------------------------------------*/
  93.  
  94. int
  95. main( int argc, char *argv[] )
  96. {
  97.                                   // if params passed, runcmd then exit using
  98.                                   // 1st param as path
  99.     if ( argc>1 ) {
  100.         PSZ path;
  101.         path=(PSZ)malloc(strlen((PSZ)argv[1]));
  102.         strcpy(path, (PSZ)argv[1]);
  103.         SetCDtoFilePath(path);
  104.         RunCmd(path);
  105.         free(path);
  106.         return (0);
  107.     }
  108.  
  109.     HMQ   hmq;
  110.     QMSG  qmsg;
  111.     ULONG flFrameFlags    = FCF_TITLEBAR | FCF_SYSMENU  | FCF_SIZEBORDER |
  112.                             FCF_MINMAX   | FCF_TASKLIST | FCF_ICON;
  113.     CHAR  szClientClass[] = "CLIENT";
  114.  
  115.     hab = WinInitialize (0);
  116.     hmq = WinCreateMsgQueue (hab, 0);
  117.  
  118.     WinRegisterClass (hab, szClientClass, (PFNWP)ClientWndProc, 0, 0);
  119.     WinLoadString (hab, 0, ID_APPNAME, sizeof(szTitle), szTitle);
  120.     WinLoadString (hab, 0, IDS_MSG1, sizeof(szMsg1), szMsg1);
  121.     WinLoadString (hab, 0, IDS_MSG2, sizeof(szMsg2), szMsg2);
  122.     WinLoadString (hab, 0, IDS_MSG3, sizeof(szMsg3), szMsg3);
  123.  
  124.     hWndFrame = WinCreateStdWindow (HWND_DESKTOP, 0,
  125.         &flFrameFlags, szClientClass, szTitle, 0, 0, ID_APPNAME, &hWndClient);
  126.     ReSize(hWndFrame);
  127.     WinSetWindowPos(hWndFrame, HWND_TOP,0,0,0,0, SWP_SHOW | SWP_ACTIVATE | SWP_ZORDER);
  128.  
  129.     while (WinGetMsg (hab, &qmsg, 0, 0, 0))
  130.         WinDispatchMsg (hab, &qmsg);
  131.  
  132.     WinDestroyWindow (hWndFrame);
  133.     WinDestroyMsgQueue (hmq);
  134.     WinTerminate (hab);
  135.     return (0);
  136. }
  137.  
  138. /*-----------------------------------------------------------------------------
  139. ;                   ReSize
  140. ;----------------------------------------------------------------------------*/
  141. void
  142. ReSize(HWND hwnd)
  143. {
  144.    LONG menuHeight,captionHeight,borderHeight,borderWidth;
  145.    LONG cx,cy;
  146.  
  147.    captionHeight = WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR);
  148.    borderHeight = WinQuerySysValue(HWND_DESKTOP, SV_CYDLGFRAME) * 2;
  149.    borderWidth = WinQuerySysValue(HWND_DESKTOP, SV_CXDLGFRAME) * 2;
  150.    menuHeight = WinQuerySysValue(HWND_DESKTOP, SV_CYMENU);
  151.  
  152.    cx = borderWidth + WXSIZE;
  153.    cy = menuHeight + captionHeight + borderHeight + WYSIZE;
  154.  
  155.    WinSetWindowPos(hwnd, 0, WX, WY, cx, cy, SWP_MOVE | SWP_SIZE);
  156.    return;
  157. }
  158.  
  159. /*-----------------------------------------------------------------------------
  160. ;                   ClientWndProc
  161. ;----------------------------------------------------------------------------*/
  162. MRESULT EXPENTRY
  163. ClientWndProc (HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  164. {
  165.  
  166.     HPS     hps;
  167.     BOOL    bHandled = TRUE;
  168.     MRESULT mReturn  = 0;
  169.  
  170.     switch (msg)
  171.     {
  172.  
  173.         case DM_DRAGOVER:
  174.             mReturn = MRFROM2SHORT(DOR_DROP, DO_UNKNOWN);
  175.             break;
  176.  
  177.         case DM_DROP:
  178.             {
  179. #           define TEMPSTRLEN 255
  180.                                   // DRAGINFO struct ptr
  181.             PDRAGINFO pDInfo;
  182.                                   // DRAGITEM struct ptr
  183.             PDRAGITEM pDItem;
  184.                                   // buffers
  185.             PSZ     pContainer;
  186.             PSZ     pSource;
  187.  
  188.             pContainer=(PSZ)malloc(TEMPSTRLEN);
  189.             pSource=(PSZ)malloc(TEMPSTRLEN);
  190.  
  191.             if ((pContainer) && (pSource))  {
  192.  
  193.                                   // Get DRAGINFO pointer
  194.                pDInfo = (PDRAGINFO)mp1;
  195.                                   // Access DRAGINFO
  196.                DrgAccessDraginfo(pDInfo);
  197.                                   // Access DRAGITEM, index 0 (first)
  198.                pDItem = DrgQueryDragitemPtr(pDInfo, 0);
  199.                DrgQueryStrName(pDItem->hstrContainerName,TEMPSTRLEN,pContainer);
  200.                DrgQueryStrName(pDItem->hstrSourceName,TEMPSTRLEN,pSource);
  201.                WinPostMsg(hwndWorker, UM_DROP, (PVOID)pContainer, (PVOID)pSource);
  202.                DrgDeleteDraginfoStrHandles(pDInfo);
  203.                DrgFreeDraginfo(pDInfo);
  204.             }
  205.             }
  206.             break;
  207.  
  208.         case WM_PAINT:
  209.                                   // update region
  210.             RECTL  rcl;
  211.  
  212.             hps = WinBeginPaint (hWnd,0,0);
  213.                                   // get window dimensions
  214.             WinQueryWindowRect(hWnd, &rcl);
  215.  
  216.                                   // clear entire window
  217.             WinFillRect(hps, &rcl, CLR_PINK);
  218.  
  219.             WinDrawText(hps, strlen(szMsg1), szMsg1, &rcl, 0L, 0L,
  220.               DT_TOP | DT_LEFT | DT_TEXTATTRS);
  221.             rcl.yTop -= 15;
  222.             WinDrawText(hps, strlen(szMsg2), szMsg2, &rcl, 0L, 0L,
  223.               DT_TOP | DT_LEFT | DT_TEXTATTRS);
  224.             rcl.yTop -= 15;
  225.             WinDrawText(hps, strlen(szMsg3), szMsg3, &rcl, 0L, 0L,
  226.               DT_TOP | DT_LEFT | DT_TEXTATTRS);
  227.  
  228.             WinEndPaint (hps);
  229.             break;
  230.  
  231.         case WM_ERASEBACKGROUND:
  232.             mReturn = MRFROMLONG(1L);
  233.             break;
  234.  
  235.         case WM_CREATE:
  236.             if (_beginthread ( (void (*)(void *)) WorkerThread, 8192, NULL) == -1)
  237.             {
  238.            WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
  239.                "Creation of second thread failed!", "Step 2",
  240.                    0, MB_OK | MB_CUACRITICAL);
  241.             }
  242.             break;
  243.  
  244.         default:
  245.             bHandled = FALSE;
  246.             break;
  247.     }
  248.  
  249.     if (!bHandled)
  250.         mReturn = WinDefWindowProc (hWnd,msg,mp1,mp2);
  251.  
  252.     return (mReturn);
  253. }
  254.  
  255. /*-----------------------------------------------------------------------------
  256. ; background thread
  257. ;----------------------------------------------------------------------------*/
  258. void WorkerThread (void)
  259. {
  260.    HAB  habWork;
  261.    HMQ  hmqWork;
  262.    QMSG qmsg;
  263.  
  264.    habWork = WinInitialize (0);
  265.  
  266.    hmqWork = WinCreateMsgQueue(habWork, 0);
  267.  
  268.    WinRegisterClass(habWork, "WATCH", WorkWndProc, 0, 0);
  269.  
  270.    hwndWorker = WinCreateWindow( HWND_OBJECT, "WATCH", "",
  271.      0, 0, 0, 0, 0, HWND_OBJECT, HWND_BOTTOM, 0, NULL, NULL );
  272.  
  273.    while( WinGetMsg ( habWork, &qmsg, 0, 0, 0 ))
  274.      WinDispatchMsg ( habWork, &qmsg );
  275.  
  276.    WinPostMsg( hWndClient, WM_QUIT, 0, 0 );
  277.  
  278.    WinDestroyWindow( hwndWorker );
  279.    WinDestroyMsgQueue( hmqWork );
  280.    WinTerminate (habWork);
  281.    _endthread ();
  282. }
  283.  
  284. /*-----------------------------------------------------------------------------
  285. ; background thread window procedure
  286. ;----------------------------------------------------------------------------*/
  287. MRESULT EXPENTRY
  288. WorkWndProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  289. {
  290.    BOOL    bHandled = TRUE;
  291.    MRESULT mReturn  = 0;
  292.    PSZ     path;
  293.  
  294.    switch (msg)
  295.    {
  296.       case UM_DROP:
  297.          path=(PSZ)malloc(strlen((PSZ)mp1)+strlen((PSZ)mp2));
  298.          if (path) {
  299.            strcpy(path,(PSZ)mp1);
  300.            strcat(path,(PSZ)mp2);
  301.            SetCDtoFilePath(path);
  302.            RunCmd(path);
  303.            free(path);
  304.          }
  305.          if (mp1) free(mp1);
  306.          if (mp2) free(mp2);
  307.          break;
  308.       case WM_DESTROY:
  309.          break;
  310.    }
  311.    if (!bHandled)
  312.       mReturn = WinDefWindowProc (hWnd,msg,mp1,mp2);
  313.  
  314.    return (mReturn);
  315. }
  316.  
  317. /*-----------------------------------------------------------------------------
  318. ; change current directory to that of the dir/file passed in param
  319. ;----------------------------------------------------------------------------*/
  320. void
  321. SetCDtoFilePath(PSZ path)
  322. {
  323.    PSZ s;
  324.    ULONG        PathInfoLevel;   /* Data required */
  325.    FILESTATUS3  PathInfoBuf;     /* File info buffer */
  326.    ULONG        PathInfoBufSize; /* Data buffer size */
  327.    APIRET       rc;              /* Return code */
  328.  
  329.    if (path[strlen(path)-1]=='\\') path[strlen(path)-1]='\0';
  330.  
  331.    PathInfoLevel = 1;    /* Indicate that Level 1 information */
  332.                           /*   is desired                      */
  333.  
  334.    PathInfoBufSize = sizeof(FILESTATUS3);
  335.                           /* Size of the buffer that will      */
  336.                           /*   receive the Level 1 information */
  337.  
  338.    rc = DosQueryPathInfo(path, PathInfoLevel, &PathInfoBuf, PathInfoBufSize);
  339.                           /* On successful return, the Level 1 */
  340.                           /*   directory information is in the */
  341.                           /*   PathInfoBuf buffer              */
  342.  
  343.    if (rc != 0) {
  344.       Msg("DosQueryPathInfo error: return code = %ld", rc);
  345.       return;
  346.    }
  347.  
  348.    char drive[MAXDRIVE];
  349.    char dir[MAXDIR];
  350.    char file[MAXFILE];
  351.    char ext[MAXEXT];
  352.    int flags;
  353.  
  354.    if (PathInfoBuf.attrFile & FILE_DIRECTORY) {
  355.       setdisk(toupper(path[0])-(int)'A');
  356.    } else {
  357.       flags = fnsplit(path,drive,dir,file,ext);
  358.       path[0]='\0';
  359.       if(flags & DRIVE)
  360.         strcat(path,drive);
  361.       if(flags & DIRECTORY)
  362.         strcat(path,dir);
  363.       if (path[strlen(path)-1]=='\\') path[strlen(path)-1]='\0';
  364.       setdisk(toupper(drive[0])-(int)'A');
  365.    }
  366.    chdir(path);
  367.    return;
  368. }
  369.  
  370. /*-----------------------------------------------------------------------------
  371. ; start command shell
  372. ;----------------------------------------------------------------------------*/
  373. void
  374. RunCmd(PSZ path)
  375. {
  376.    STARTDATA   StartData;         // Start session data structure
  377.    ULONG       SessID;            // Session ID (returned)
  378.    PID         PID;               // Process ID (returned)
  379.    UCHAR       ObjBuf[100];       // Object buffer
  380.    PSZ         PgmTitle;
  381.  
  382.                                   //  Specify the various session start parameters
  383.  
  384.                                   // Length of STARTDATA structure
  385.    StartData.Length = sizeof(STARTDATA);
  386.                                   // unrelated session
  387.    StartData.Related = SSF_RELATED_INDEPENDENT;
  388.                                   // Start child session in foreground
  389.    StartData.FgBg = SSF_FGBG_FORE;
  390.                                   // Don't trace session
  391.    StartData.TraceOpt = SSF_TRACEOPT_NONE;
  392.  
  393.                                   // Session Title string
  394.    PgmTitle=(PSZ)malloc(strlen(path)+18);
  395.    strcpy(PgmTitle,"Command Prompt - ");
  396.    strcat(PgmTitle,path);
  397.    StartData.PgmTitle = PgmTitle;
  398.  
  399.                                   // Program path-name string
  400.    StartData.PgmName = NULL;
  401.                                   // Assume no input arguments need be passed
  402.                                   // to the program
  403.    StartData.PgmInputs = 0;
  404.                                   // Assume no environment string
  405.    StartData.Environment = 0;
  406.                                   // Inherit environment and open file handles
  407.                                   // from parent
  408.    StartData.InheritOpt = SSF_INHERTOPT_PARENT;
  409.                                   // Allow the Shell to establish the session type
  410.    StartData.SessionType = SSF_TYPE_WINDOWABLEVIO;
  411.                                   // Assume no specific icon file is provided
  412.    StartData.IconFile = 0;
  413.                                   // Do not use the installation file
  414.    StartData.PgmHandle = 0;
  415.                                   // Start the program as visible and maximized
  416.    StartData.PgmControl = SSF_CONTROL_VISIBLE;
  417.                                   // Initial window coordinates and size
  418.    StartData.InitXPos = 30;
  419.    StartData.InitYPos = 40;
  420.    StartData.InitXSize = 200;
  421.    StartData.InitYSize = 140;
  422.                                   // Reserved, must be zero
  423.    StartData.Reserved = 0;
  424.                                   // Object buffer to hold DosExecPgm
  425.                                   // failure causes
  426.    StartData.ObjectBuffer = ObjBuf;
  427.                                   // Size of object buffer
  428.    StartData.ObjectBuffLen = 100;
  429.                                   // Start session
  430.    DosStartSession(&StartData, &SessID, &PID);
  431. // freeing PgmTitle causes problems - we seem to have lost 'ownership', but I
  432. // haven't found this documented anywhere. Can anyone clarify?
  433. //   free(PgmTitle);
  434.    return;
  435. }
  436.  
  437. /*-----------------------------------------------------------------------------
  438.   DISPLAY A MESSAGE TO THE USER.
  439.  
  440.   PARMS: a message in printf format with its parms
  441.  
  442.   NOTES:
  443.  
  444.   RETURNS: nothing
  445. ;----------------------------------------------------------------------------*/
  446. #define MESSAGE_SIZE 200
  447. void
  448. Msg( PSZ szFormat,... )
  449. {
  450.     PSZ     szMsg;
  451.     va_list argptr;
  452.  
  453.     szMsg = (PSZ) malloc( MESSAGE_SIZE );
  454.     if( szMsg )
  455.     {
  456.         va_start( argptr, szFormat );
  457.         vsprintf( szMsg, szFormat, argptr );
  458.         va_end( argptr );
  459.  
  460.         szMsg[ MESSAGE_SIZE - 1 ] = 0;
  461.  
  462.         WinAlarm( HWND_DESKTOP, WA_WARNING );
  463.         WinMessageBox(  HWND_DESKTOP, HWND_DESKTOP, szMsg,
  464.                         szTitle, 1,
  465.                         MB_OK | MB_MOVEABLE );
  466.         free( szMsg );
  467.     }
  468.     else
  469.     {
  470.         DosBeep( 1000, 1000 );
  471.         return;
  472.     }
  473. }
  474.  
  475.  
  476.