home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / findutils-4.1-src.lha / findutils-4.1 / find / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-20  |  4.1 KB  |  159 lines

  1. /* util.c -- functions for initializing new tree elements, and other things.
  2.    Copyright (C) 1990, 91, 92, 93, 94 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 <config.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <stdio.h>
  22. #include "defs.h"
  23.  
  24. /* Return the last component of pathname FNAME, with leading slashes
  25.    compressed into one slash. */
  26.  
  27. char *
  28. basename (fname)
  29.      char *fname;
  30. {
  31.   char *p;
  32.  
  33.   /* For "/", "//", etc., return "/". */
  34.   for (p = fname; *p == '/'; ++p)
  35.     /* Do nothing. */ ;
  36.   if (*p == '\0')
  37.     return p - 1;
  38.   p = strrchr (fname, '/');
  39.   return (p == NULL ? fname : p + 1);
  40. }
  41.  
  42. /* Return a pointer to a new predicate structure, which has been
  43.    linked in as the last one in the predicates list.
  44.  
  45.    Set `predicates' to point to the start of the predicates list.
  46.    Set `last_pred' to point to the new last predicate in the list.
  47.    
  48.    Set all cells in the new structure to the default values. */
  49.  
  50. struct predicate *
  51. get_new_pred ()
  52. {
  53.   register struct predicate *new_pred;
  54.  
  55.   if (predicates == NULL)
  56.     {
  57.       predicates = (struct predicate *)
  58.     xmalloc (sizeof (struct predicate));
  59.       last_pred = predicates;
  60.     }
  61.   else
  62.     {
  63.       new_pred = (struct predicate *) xmalloc (sizeof (struct predicate));
  64.       last_pred->pred_next = new_pred;
  65.       last_pred = new_pred;
  66.     }
  67.   last_pred->pred_func = NULL;
  68. #ifdef    DEBUG
  69.   last_pred->p_name = NULL;
  70. #endif    /* DEBUG */
  71.   last_pred->p_type = NO_TYPE;
  72.   last_pred->p_prec = NO_PREC;
  73.   last_pred->side_effects = false;
  74.   last_pred->need_stat = true;
  75.   last_pred->args.str = NULL;
  76.   last_pred->pred_next = NULL;
  77.   last_pred->pred_left = NULL;
  78.   last_pred->pred_right = NULL;
  79.   return (last_pred);
  80. }
  81.  
  82. /* Return a pointer to a new predicate, with operator check.
  83.    Like get_new_pred, but it checks to make sure that the previous
  84.    predicate is an operator.  If it isn't, the AND operator is inserted. */
  85.  
  86. struct predicate *
  87. get_new_pred_chk_op ()
  88. {
  89.   struct predicate *new_pred;
  90.  
  91.   if (last_pred)
  92.     switch (last_pred->p_type)
  93.       {
  94.       case NO_TYPE:
  95.     error (1, 0, "oops -- invalid default insertion of and!");
  96.     break;
  97.  
  98.       case PRIMARY_TYPE:
  99.       case CLOSE_PAREN:
  100.     new_pred = get_new_pred ();
  101.     new_pred->pred_func = pred_and;
  102. #ifdef    DEBUG
  103.     new_pred->p_name = find_pred_name (pred_and);
  104. #endif    /* DEBUG */
  105.     new_pred->p_type = BI_OP;
  106.     new_pred->p_prec = AND_PREC;
  107.     new_pred->need_stat = false;
  108.     new_pred->args.str = NULL;
  109.  
  110.       default:
  111.     break;
  112.       }
  113.   return (get_new_pred ());
  114. }
  115.  
  116. /* Add a primary of predicate type PRED_FUNC to the predicate input list.
  117.  
  118.    Return a pointer to the predicate node just inserted.
  119.  
  120.    Fills in the following cells of the new predicate node:
  121.  
  122.    pred_func        PRED_FUNC
  123.    args(.str)        NULL
  124.    p_type        PRIMARY_TYPE
  125.    p_prec        NO_PREC
  126.  
  127.    Other cells that need to be filled in are defaulted by
  128.    get_new_pred_chk_op, which is used to insure that the prior node is
  129.    either not there at all (we are the very first node) or is an
  130.    operator. */
  131.  
  132. struct predicate *
  133. insert_primary (pred_func)
  134.      boolean (*pred_func) ();
  135. {
  136.   struct predicate *new_pred;
  137.  
  138.   new_pred = get_new_pred_chk_op ();
  139.   new_pred->pred_func = pred_func;
  140. #ifdef    DEBUG
  141.   new_pred->p_name = find_pred_name (pred_func);
  142. #endif    /* DEBUG */
  143.   new_pred->args.str = NULL;
  144.   new_pred->p_type = PRIMARY_TYPE;
  145.   new_pred->p_prec = NO_PREC;
  146.   return (new_pred);
  147. }
  148.  
  149. void
  150. usage (msg)
  151.      char *msg;
  152. {
  153.   if (msg)
  154.     fprintf (stderr, "%s: %s\n", program_name, msg);
  155.   fprintf (stderr, "\
  156. Usage: %s [path...] [expression]\n", program_name);
  157.   exit (1);
  158. }
  159.