home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / find12as.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  5KB  |  194 lines

  1. /* Misc Utilities needed by Find
  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 1, 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. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  19.    This port is also distributed under the terms of the
  20.    GNU General Public License as published by the
  21.    Free Software Foundation.
  22.  
  23.    Please note that this file is not identical to the
  24.    original GNU release, you should have received this
  25.    code as patch to the official release.
  26.  
  27.    $Header: e:/gnu/find/RCS/util.c 1.2.0.3 90/09/23 16:10:02 tho Exp $
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #ifndef USG
  33. #include <strings.h>
  34. #else
  35. #include <string.h>
  36. #define index strchr
  37. #define rindex strrchr
  38. #endif
  39. #include "defs.h"
  40.  
  41. #ifdef MSDOS
  42. extern char *basename (char *fname);
  43. #endif /* MSDOS */
  44.  
  45. char *find_pred_name ();
  46. boolean pred_and ();
  47.  
  48. /* Return the last component of pathname FNAME. */
  49. char *
  50. basename (fname)
  51.      char *fname;
  52. {
  53.   char *p;
  54.  
  55.   /* For "/", "//", etc., return "/". */
  56.   for (p = fname; *p == '/'; ++p)
  57.     /* Do nothing. */ ;
  58.   if (*p == '\0')
  59.     return p - 1;
  60.   p = rindex (fname, '/');
  61.   return (p == NULL ? fname : p + 1);
  62. }
  63.  
  64. /* Return a pointer to a new predicate structure, which has been
  65.    linked in as the last one in the predicates list.
  66.  
  67.    Set `predicates' to point to the start of the predicates list.
  68.    Set `last_pred' to point to the new last predicate in the list.
  69.    
  70.    Set all cells in the new structure to the default values. */
  71.  
  72. struct pred_struct *
  73. get_new_pred ()
  74. {
  75.   register struct pred_struct *new_pred;
  76.  
  77.   if (predicates == NULL)
  78.     {
  79.       predicates = (struct pred_struct *)
  80.     xmalloc (sizeof (struct pred_struct));
  81.       last_pred = predicates;
  82.     }
  83.   else
  84.     {
  85.       new_pred = (struct pred_struct *) xmalloc (sizeof (struct pred_struct));
  86.       last_pred->pred_next = new_pred;
  87.       last_pred = new_pred;
  88.     }
  89.   last_pred->pred_func = NULL;
  90. #ifdef    DEBUG
  91.   last_pred->p_name = NULL;
  92. #endif    /* DEBUG */
  93.   last_pred->p_type = NO_TYPE;
  94.   last_pred->p_prec = NO_PREC;
  95.   last_pred->side_effects = false;
  96.   last_pred->args.str = NULL;
  97.   last_pred->pred_next = NULL;
  98.   last_pred->pred_left = NULL;
  99.   last_pred->pred_right = NULL;
  100.   return (last_pred);
  101. }
  102.  
  103. /* Return a pointer to a new predicate, with operator check.
  104.    Like get_new_pred, but it checks to make sure that the previous
  105.    predicate is an operator.  If it isn't, the AND operator is inserted. */
  106.  
  107. struct pred_struct *
  108. get_new_pred_chk_op ()
  109. {
  110.   struct pred_struct *new_pred;
  111.  
  112.   if (last_pred)
  113.     switch (last_pred->p_type)
  114.       {
  115.       case NO_TYPE:
  116.     error (1, 0, "oops -- invalid default insertion of and!");
  117.     break;
  118.  
  119.       case VICTIM_TYPE:
  120.       case CLOSE_PAREN:
  121.     new_pred = get_new_pred ();
  122.     new_pred->pred_func = pred_and;
  123. #ifdef    DEBUG
  124.     new_pred->p_name = find_pred_name (pred_and);
  125. #endif    /* DEBUG */
  126.     new_pred->p_type = BI_OP;
  127.     new_pred->p_prec = AND_PREC;
  128.     new_pred->args.str = NULL;
  129.  
  130.       default:
  131.     break;
  132.       }
  133.   return (get_new_pred ());
  134. }
  135.  
  136. /* Add a victim of predicate type PRED_FUNC to the predicate input list.
  137.  
  138.    Return a pointer to the predicate node just inserted.
  139.  
  140.    Fills in the following cells of the new predicate node:
  141.  
  142.    pred_func        PRED_FUNC
  143.    args(.str)        NULL
  144.    p_type        VICTIM_TYPE
  145.    p_prec        NO_PREC
  146.  
  147.    Other cells that need to be filled in are defaulted by
  148.    get_new_pred_chk_op, which is used to insure that the prior node is
  149.    either not there at all (we are the very first node) or is an
  150.    operator. */
  151.  
  152. struct pred_struct *
  153. insert_victim (pred_func)
  154.      boolean (*pred_func) ();
  155. {
  156.   struct pred_struct *new_pred;
  157.  
  158.   new_pred = get_new_pred_chk_op ();
  159.   new_pred->pred_func = pred_func;
  160. #ifdef    DEBUG
  161.   new_pred->p_name = find_pred_name (pred_func);
  162. #endif    /* DEBUG */
  163.   new_pred->args.str = NULL;
  164.   new_pred->p_type = VICTIM_TYPE;
  165.   new_pred->p_prec = NO_PREC;
  166.   return (new_pred);
  167. }
  168.  
  169. /* Allocate N bytes of memory dynamically, with error checking.  */
  170.  
  171. char *
  172. xmalloc (n)
  173.      unsigned n;
  174. {
  175.   char *p;
  176.  
  177.   p = malloc (n);
  178.   if (p == 0)
  179.     error (1, 0, "virtual memory exhausted");
  180.   return p;
  181. }
  182.  
  183. void
  184. usage (msg)
  185.      char *msg;
  186. {
  187.   if (msg)
  188.     fprintf (stderr, "%s: %s\n", program_name, msg);
  189.   fprintf (stderr, "\
  190. Usage: %s path... [expression]\n",
  191.        program_name);
  192.   exit (1);
  193. }
  194.