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