home *** CD-ROM | disk | FTP | other *** search
/ vsiftp.vmssoftware.com / VSIPUBLIC@vsiftp.vmssoftware.com.tar / FREEWARE / FREEWARE40.ZIP / flistfrontend / src / flfind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-06  |  3.9 KB  |  159 lines

  1. #ifndef NO_IDENT
  2. static char *Id = "$Id: flfind.c,v 1.8 1995/06/06 10:18:44 tom Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Title:    flfind.c
  7.  * Author:    Thomas E. Dickey
  8.  * Created:    21 May 1984
  9.  * Last update:
  10.  *        18 Mar 1995, prototyped
  11.  *        14 Aug 1985, added code to make search-state nested.
  12.  *        13 Aug 1985, corrected pointer-references to '->dcl_text' (had
  13.  *                 '.dcl_text', which gave strange result).  Added
  14.  *                 'flfind_show' entrypoint.
  15.  *        16 Jun 1985, typed 'calloc' to shut up CC2.0; corrected arg
  16.  *                 in memory-copy which broke NEXT-status.
  17.  *        02 May 1985, changed call on 'dircmd_dirflg'.
  18.  *        05 Jan 1985, permit "NEXT" keyword.  Altered interface with
  19.  *                 'dirfind'.
  20.  *        24 Dec 1984, added "NFIND" command.
  21.  *        28 Jul 1984, broke off 'dirfind.c' code for common searches.
  22.  *        24 Jul 1984, re-coded test for wildcard pathname
  23.  *        15 Jul 1984, use PATHOF()
  24.  *        10 Jul 1984, changed calling convention
  25.  *        05 Jul 1984
  26.  *
  27.  * Function:    This module performs the FIND, NFIND, and NEXT functions
  28.  *        for FLIST:
  29.  *
  30.  *        FIND -    initiates a search in the current direction, which is
  31.  *            halted when a file entry is found matching the pattern.
  32.  *        NFIND -    is the complement of FIND; halts when a mismatch is
  33.  *            found.
  34.  *        NEXT -    continues the last search.
  35.  *
  36.  * Patch:    Should provide a re-entry point to 'dirarg' so that we can
  37.  *        re-substitute /P/N/T/V stuff if it is needed in NEXT.
  38.  */
  39.  
  40. #include    <stdlib.h>
  41. #include    <string.h>
  42.  
  43. #include    "flist.h"
  44. #include    "dircmd.h"
  45. #include    "dirfind.h"
  46. #include    "dds.h"
  47.  
  48. typedef    struct    {
  49.     int    findFLG;
  50.     DCLARG    *findDCL;
  51.     char    *findTXT;
  52.     }    STK;
  53.  
  54. static    STK    *stk_    = 0;
  55. static    int    deepest    = -1;
  56.  
  57. #define    FindFLG    stk_[lvl].findFLG
  58. #define    FindDCL    stk_[lvl].findDCL
  59. #define    FindTXT    stk_[lvl].findTXT
  60.  
  61. tDIRCMD(flfind)
  62. {
  63.     register int next,
  64.         lvl    = flist_nest() - 1;    /* 0,1,2,...    */
  65.     DCLARG    *pattern;
  66.  
  67.     /*
  68.      * If we have a file-specification, this must be either FIND or
  69.      * NFIND.
  70.      */
  71.     if (dclinx (xdcl_, 1, 0))
  72.     {
  73.         pattern    = xdcl_->dcl_next;    /* => file specification*/
  74.         FindFLG    = (xdcl_->dcl_text[0] == 'N');
  75.     }
  76.     else
  77.         pattern = FindDCL;        /* "NEXT" usage        */
  78.  
  79.     /*
  80.      * Check if the user entered this code with CTRL/N first without
  81.      * initializing the search.
  82.      */
  83.     if (!pattern->dcl_text[0])
  84.     {
  85.         warn ("FIND requires a filename template");
  86.         return;
  87.     }
  88.  
  89.     /*
  90.      * Save a copy of the specification for "NEXT":
  91.      */
  92.     memcpy (FindDCL, pattern, sizeof(DCLARG));
  93.     strcpy (FindTXT, pattern->dcl_text);
  94.     FindDCL->dcl_text = FindTXT;
  95.  
  96.     next = dirfind (*curfile_, dircmd_dirflg(-1), pattern,
  97.             (void(*)(int,int *))0,
  98.             TRUE, /* Need exactly one file */
  99.             FindFLG);
  100.  
  101.     if (next == *curfile_)
  102.         warn ("Current entry is the only match");
  103.     else if (next >= 0)
  104.         dds_index (*curfile_ = next);
  105. }
  106.  
  107. /* <flfind_init>:
  108.  * The argument 'lvl' is numbered 0,1,2,..., for the actual number of the "prior"
  109.  * nesting level of the directory editor.
  110.  */
  111. void    flfind_init (int lvl)
  112. {
  113.     if (lvl == 0)
  114.     {
  115.         stk_    = calloc (deepest = 1, sizeof(STK));
  116.         FindTXT = calloc (1, MAX_PATH);
  117.         FindDCL = calloc (1, sizeof(DCLARG));
  118.     }
  119.     else
  120.     {
  121.         if (lvl > deepest-1)
  122.         {
  123.             stk_    = realloc (stk_, (++deepest) * sizeof(STK));
  124.             FindTXT = calloc (1, MAX_PATH);
  125.             FindDCL = calloc (1, sizeof(DCLARG));
  126.         }
  127.         memcpy (FindDCL, stk_[lvl-1].findDCL, sizeof(DCLARG));
  128.         strcpy (FindTXT, stk_[lvl-1].findTXT);
  129.         FindFLG = stk_[lvl-1].findFLG;
  130.     }
  131.     FindDCL->dcl_text = FindTXT;
  132. }
  133.  
  134. /* <flfind_show>:
  135.  * Display current search-target: "?FIND".  Use 'dirfind_chop' to parse it
  136.  * to show the actual interpretation of wildcards and implicit pathnames.
  137.  */
  138. void    flfind_show (void)
  139. {
  140.     register lvl    = flist_nest() -1;
  141.  
  142.     if (FindDCL)
  143.     {
  144.         if (*FindDCL->dcl_text)
  145.         {
  146.             FILENT    z;
  147.             PATHNT    pz;
  148.             char    p[MAX_PATH],    n[MAX_NAME],    t[MAX_TYPE],
  149.                 fullname[MAX_PATH];
  150.             dirfind_chop (FindDCL, &z, &pz, p, n, t);
  151.             dirent_glue (fullname, &z);
  152.             flist_tell ("%sFind: %.132s", FindFLG ? "N" : "",
  153.                         fullname);
  154.             return;
  155.         }
  156.     }
  157.     warn ("No search target in effect");
  158. }
  159.