home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: WPS_PM / WPS_PM.zip / open201.zip / open.c < prev    next >
C/C++ Source or Header  |  1998-03-08  |  7KB  |  218 lines

  1. /* Open 2.01  (C) 1997 Samuel Audet <guardia@cam.org> */
  2.  
  3. #define INCL_DOS
  4. #define INCL_WINSWITCHLIST
  5. #define INCL_WINWORKPLACE
  6. #include <os2.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <ctype.h>
  11.  
  12. /* just to get OPEN constants */
  13. #include <wpdisk.h>
  14. #include <wpfolder.h>
  15. #include <wpobject.h>
  16.  
  17. int getAndOpen(char *path, char *filename, char *filter, ULONG view);
  18. void DisplayHelp(void);
  19. BOOL _System SwitchToObject(HOBJECT hobj, ULONG view, ULONG timeout, BOOL wait);
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.  
  24.    int i,  /* loop index */
  25.        rc; /* return code variable counter in the for loop */
  26.    char *filter = NULL;       /* pointer to a user specified filter */
  27.    ULONG view = OPEN_DEFAULT; /* default openview */
  28.  
  29.    /* used for wildcards support */
  30.    HDIR         searchHandle = HDIR_SYSTEM;
  31.    ULONG        onefile = 1;
  32.    FILEFINDBUF3 searchBuf;
  33.    ULONG        allfiles = FILE_ARCHIVED | FILE_DIRECTORY | FILE_SYSTEM |
  34.                            FILE_HIDDEN   | FILE_READONLY;
  35.    char *path;
  36.  
  37. /* check for no parameter -> open current dir */
  38.     if (argc < 2)
  39.       return getAndOpen(NULL,".",NULL,view);
  40. /* check for help parameter -> display help */
  41.    else if (argv[1][1] == '?')
  42.    {
  43.       DisplayHelp();
  44.       return 0;
  45.    }
  46.  
  47.    printf("\n");
  48.  
  49. /* check every parameters */
  50.  
  51.    for(i = 1, rc = 0; i < argc; i++)
  52.    {
  53.       /* strip the non-standard trailing backslash */
  54.       if(*(strchr(argv[i],'\0') - 1) == '\\' && strlen(argv[i]) > 1) *(strchr(argv[i],'\0') - 1) = '\0';
  55.  
  56.       /* process options starting with - or / */
  57.       if ((argv[i][0] == '/') || (argv[i][0] == '-'))
  58.          switch(toupper(argv[i][1]))
  59.          {
  60.              case 'F':  filter = argv[++i]; break;
  61.              case 'O':  switch(toupper(argv[i][2]))
  62.                         {
  63.                            case 'P' : view = OPEN_SETTINGS; break;
  64.                            case 'I' : view = OPEN_CONTENTS; break;
  65.                            case 'T' : view = OPEN_TREE; break;
  66.                            case 'D' : view = OPEN_DETAILS; break;
  67.                            default  : view = OPEN_DEFAULT; break;
  68.                         }
  69.                         break;
  70.          }
  71.  
  72.    /* special processing for special filename . and ..                   */
  73.    /* The wild card routines mucks them up, so I send them directly into */
  74.    /* processing.                                                        */
  75.  
  76.       else if (!strcmp(argv[i],".") || !strcmp(argv[i],".."))
  77.          rc += getAndOpen(NULL,argv[i],filter,view);
  78.  
  79.    /* another special treatement for drive roots, gotta love Microsoft
  80.       for that one checks for either "x:", "x:\" and "\" type parameter */
  81.  
  82.       else if (
  83.         ((argv[i][1] == ':') && (argv[i][2] == '\0')) ||
  84.         (((argv[i][1] == ':') && (argv[i][2] == '\\')) && (argv[i][3] == '\0')) ||
  85.         ((argv[i][0] == '\\') && (argv[i][1] == '\0'))
  86.                )
  87.          rc += getAndOpen(NULL,argv[i],filter,view);
  88.  
  89.    /* special treatement for Object IDs ex.: <WP_DESKTOP> */
  90.  
  91.       else if ((argv[i][0] == '<') && (*(strchr(argv[i],'\0')-1) == '>'))
  92.          rc += getAndOpen(NULL,argv[i],filter,view);
  93.  
  94.    /* else start wildcard search */
  95.  
  96.       else
  97.       {
  98.          onefile = 1;
  99.          if (!DosFindFirst(argv[i], &searchHandle, allfiles, &searchBuf,
  100.                            sizeof(searchBuf), &onefile, FIL_STANDARD))
  101.          {
  102.  
  103.          /* find the original path to give to the opener */
  104.  
  105.             if ((path = strrchr(argv[i],'\\')) || (path = strrchr(argv[i],':')))
  106.             {
  107.                *++path = '\0';
  108.                path = argv[i];
  109.             }
  110.             do
  111.                rc += getAndOpen(path,searchBuf.achName,filter,view);
  112.             while (!DosFindNext(searchHandle, &searchBuf, sizeof(searchBuf), &onefile));
  113.          }
  114.          else
  115.          {
  116.             printf("%s: No file or directory found.\n",argv[i]);
  117.             rc++;
  118.          }
  119.       }
  120.    }
  121.  
  122.    return rc;
  123.  
  124. }
  125.  
  126.  
  127. /* get object handle for the filename and open it in the WPS */
  128.  
  129. int getAndOpen(char *path, char *filename, char *filterraw, ULONG view)
  130. {
  131.    
  132.    char fullfilename[CCHMAXPATH],
  133.         filter[CCHMAXPATH],
  134.         *temp;
  135.    HOBJECT hobj;
  136.  
  137. /* patch together the path and filename, if it is present */
  138.    if (path)
  139.    {
  140.       strcpy(fullfilename, path);
  141.       strcat(fullfilename, filename);
  142.    }
  143.    else strcpy(fullfilename, filename);
  144.  
  145. /* make "FILTER=filter" */
  146.    if (filterraw)
  147.    {
  148.       strcpy(filter,"FILTER=");
  149.       strcat(filter,filterraw);
  150.    }
  151.    else *filter = '\0';
  152.  
  153. /* Get the fully qualified path name and handle of the file found */
  154.  
  155.    DosQueryPathInfo(fullfilename, FIL_QUERYFULLNAME, fullfilename, 2048);
  156.    printf("Opening: %s\n",fullfilename);
  157.    if ((hobj = WinQueryObject(fullfilename)) == NULLHANDLE)
  158.    {
  159.       printf("%s: Object not found.\n",fullfilename);
  160.       return 1;
  161.    }
  162.  
  163. /* Set filter and Open the object */
  164.  
  165.    if (*filter)
  166.       WinSetObjectData(hobj,filter);
  167.  
  168.             /* if (!WinOpenObject(hobj,view,TRUE)) */
  169.  
  170.    /* buggy WinOpenObject makes the PMSHELL jam trying to drag an object to
  171.       NS/2 window from a folder opened with it :( */
  172.  
  173.    switch(view)
  174.    {
  175.       case OPEN_DEFAULT:  temp = "OPEN=DEFAULT";  break;
  176.       case OPEN_SETTINGS: temp = "OPEN=SETTINGS"; break;
  177.       case OPEN_TREE:     temp = "OPEN=TREE";     break;
  178.       case OPEN_CONTENTS: temp = "OPEN=ICON";     break;
  179.       case OPEN_DETAILS:  temp = "OPEN=DETAILS";  break;
  180.    }
  181.  
  182.    if (!WinSetObjectData(hobj,temp))
  183.    {
  184.       printf("%s%s: Object not successfully opened.\n",path,filename);
  185.       return 1;
  186.    }
  187.    else SwitchToObject(hobj, view, 10, TRUE);
  188.  
  189.    return 0;
  190. }
  191.  
  192.  
  193. void DisplayHelp(void)
  194. {
  195.    printf(
  196.  
  197. "Open 2.01    (C) 1997 Samuel Audet <guardia@cam.org>\n"
  198. "Freeware, please send an e-mail if you are using this program.               \n\n"
  199. "Usage:                                                                         \n"
  200. "  open [/?] [/F filter] [/Oview] [objects] [/F filter] [/Oview] [objects] ...\n\n"
  201. "where                                                                        \n\n"
  202. "  [/?]        shows this help screen.                                          \n"
  203. "  [/F filter] specifies a title wildcard filter for folder objects.            \n"
  204. "  [/Oview]    specifies the view to open.  Possible views are:                 \n"
  205. "                   blank : default view (same as double-click),                \n"
  206. "                       P : properties view,                                    \n"
  207. "                       I : icon view,                                          \n"
  208. "                       T : tree view, and                                      \n"
  209. "                       D : details view.                                       \n"
  210. "  [objects] are the names of the objects to open (file system name or          \n"
  211. "            object IDs).  Wildcards and multiple entries are accepted!       \n\n"
  212. "The specified objects will be opened as if they were opened from the WPS.      \n"
  213. "If no parameters are specified, the current directory is opened as a folder,   \n"
  214. "with default view and no filter.                                               \n"
  215.  
  216.    );
  217. }
  218.