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