home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / f / find37.zip / find-3.7 / find / util.c < prev    next >
C/C++ Source or Header  |  1992-02-07  |  4KB  |  168 lines

  1. /* util.c -- functions for initializing new tree elements, and other things.
  2.    Copyright (C) 1987, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #if defined(USG) || defined(STDC_HEADERS)
  21. #include <string.h>
  22. #define index strchr
  23. #define rindex strrchr
  24. #else
  25. #include <strings.h>
  26. #endif
  27. #include "defs.h"
  28.  
  29. char *find_pred_name ();
  30. boolean pred_and ();
  31.  
  32. /* Return the last component of pathname FNAME, with leading slashes
  33.    compressed into one slash. */
  34.  
  35. char *
  36. basename (fname)
  37.      char *fname;
  38. {
  39.   char *p;
  40.  
  41.   /* For "/", "//", etc., return "/". */
  42.   for (p = fname; *p == '/'; ++p)
  43.     /* Do nothing. */ ;
  44.   if (*p == '\0')
  45.     return p - 1;
  46.   p = rindex (fname, '/');
  47.   return (p == NULL ? fname : p + 1);
  48. }
  49.  
  50. /* Return a pointer to a new predicate structure, which has been
  51.    linked in as the last one in the predicates list.
  52.  
  53.    Set `predicates' to point to the start of the predicates list.
  54.    Set `last_pred' to point to the new last predicate in the list.
  55.    
  56.    Set all cells in the new structure to the default values. */
  57.  
  58. struct predicate *
  59. get_new_pred ()
  60. {
  61.   register struct predicate *new_pred;
  62.  
  63.   if (predicates == NULL)
  64.     {
  65.       predicates = (struct predicate *)
  66.     xmalloc (sizeof (struct predicate));
  67.       last_pred = predicates;
  68.     }
  69.   else
  70.     {
  71.       new_pred = (struct predicate *) xmalloc (sizeof (struct predicate));
  72.       last_pred->pred_next = new_pred;
  73.       last_pred = new_pred;
  74.     }
  75.   last_pred->pred_func = NULL;
  76. #ifdef    DEBUG
  77.   last_pred->p_name = NULL;
  78. #endif    /* DEBUG */
  79.   last_pred->p_type = NO_TYPE;
  80.   last_pred->p_prec = NO_PREC;
  81.   last_pred->side_effects = false;
  82.   last_pred->need_stat = true;
  83.   last_pred->args.str = NULL;
  84.   last_pred->pred_next = NULL;
  85.   last_pred->pred_left = NULL;
  86.   last_pred->pred_right = NULL;
  87.   return (last_pred);
  88. }
  89.  
  90. /* Return a pointer to a new predicate, with operator check.
  91.    Like get_new_pred, but it checks to make sure that the previous
  92.    predicate is an operator.  If it isn't, the AND operator is inserted. */
  93.  
  94. struct predicate *
  95. get_new_pred_chk_op ()
  96. {
  97.   struct predicate *new_pred;
  98.  
  99.   if (last_pred)
  100.     switch (last_pred->p_type)
  101.       {
  102.       case NO_TYPE:
  103.     fflush (stdout);
  104.     error (1, 0, "oops -- invalid default insertion of and!");
  105.     break;
  106.  
  107.       case VICTIM_TYPE:
  108.       case CLOSE_PAREN:
  109.     new_pred = get_new_pred ();
  110.     new_pred->pred_func = pred_and;
  111. #ifdef    DEBUG
  112.     new_pred->p_name = find_pred_name (pred_and);
  113. #endif    /* DEBUG */
  114.     new_pred->p_type = BI_OP;
  115.     new_pred->p_prec = AND_PREC;
  116.     new_pred->need_stat = false;
  117.     new_pred->args.str = NULL;
  118.  
  119.       default:
  120.     break;
  121.       }
  122.   return (get_new_pred ());
  123. }
  124.  
  125. /* Add a victim of predicate type PRED_FUNC to the predicate input list.
  126.  
  127.    Return a pointer to the predicate node just inserted.
  128.  
  129.    Fills in the following cells of the new predicate node:
  130.  
  131.    pred_func        PRED_FUNC
  132.    args(.str)        NULL
  133.    p_type        VICTIM_TYPE
  134.    p_prec        NO_PREC
  135.  
  136.    Other cells that need to be filled in are defaulted by
  137.    get_new_pred_chk_op, which is used to insure that the prior node is
  138.    either not there at all (we are the very first node) or is an
  139.    operator. */
  140.  
  141. struct predicate *
  142. insert_victim (pred_func)
  143.      boolean (*pred_func) ();
  144. {
  145.   struct predicate *new_pred;
  146.  
  147.   new_pred = get_new_pred_chk_op ();
  148.   new_pred->pred_func = pred_func;
  149. #ifdef    DEBUG
  150.   new_pred->p_name = find_pred_name (pred_func);
  151. #endif    /* DEBUG */
  152.   new_pred->args.str = NULL;
  153.   new_pred->p_type = VICTIM_TYPE;
  154.   new_pred->p_prec = NO_PREC;
  155.   return (new_pred);
  156. }
  157.  
  158. void
  159. usage (msg)
  160.      char *msg;
  161. {
  162.   if (msg)
  163.     fprintf (stderr, "%s: %s\n", program_name, msg);
  164.   fprintf (stderr, "\
  165. Usage: %s [path...] [expression]\n", program_name);
  166.   exit (1);
  167. }
  168.