make update-po
[findutils.git] / find / util.c
blobf8b130be069aea66021f06e3b0d666f489ccaab6
1 /* util.c -- functions for initializing new tree elements, and other things.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA.
20 #include "defs.h"
21 #include "xalloc.h"
23 #if ENABLE_NLS
24 # include <libintl.h>
25 # define _(Text) gettext (Text)
26 #else
27 # define _(Text) Text
28 #endif
29 #ifdef gettext_noop
30 # define N_(String) gettext_noop (String)
31 #else
32 /* See locate.c for explanation as to why not use (String) */
33 # define N_(String) String
34 #endif
36 #include <assert.h>
39 /* Return a pointer to a new predicate structure, which has been
40 linked in as the last one in the predicates list.
42 Set `predicates' to point to the start of the predicates list.
43 Set `last_pred' to point to the new last predicate in the list.
45 Set all cells in the new structure to the default values. */
47 struct predicate *
48 get_new_pred (const struct parser_table *entry)
50 register struct predicate *new_pred;
51 (void) entry;
53 /* Options should not be turned into predicates. */
54 assert(entry->type != ARG_OPTION);
55 assert(entry->type != ARG_POSITIONAL_OPTION);
57 if (predicates == NULL)
59 predicates = (struct predicate *)
60 xmalloc (sizeof (struct predicate));
61 last_pred = predicates;
63 else
65 new_pred = (struct predicate *) xmalloc (sizeof (struct predicate));
66 last_pred->pred_next = new_pred;
67 last_pred = new_pred;
69 last_pred->parser_entry = entry;
70 last_pred->pred_func = NULL;
71 #ifdef DEBUG
72 last_pred->p_name = NULL;
73 #endif /* DEBUG */
74 last_pred->p_type = NO_TYPE;
75 last_pred->p_prec = NO_PREC;
76 last_pred->side_effects = false;
77 last_pred->no_default_print = false;
78 last_pred->need_stat = true;
79 last_pred->need_type = true;
80 last_pred->args.str = NULL;
81 last_pred->pred_next = NULL;
82 last_pred->pred_left = NULL;
83 last_pred->pred_right = NULL;
84 return (last_pred);
87 /* Return a pointer to a new predicate, with operator check.
88 Like get_new_pred, but it checks to make sure that the previous
89 predicate is an operator. If it isn't, the AND operator is inserted. */
91 struct predicate *
92 get_new_pred_chk_op (const struct parser_table *entry)
94 struct predicate *new_pred;
95 static const struct parser_table *entry_and = NULL;
97 /* Locate the entry in the parser table for the "and" operator */
98 if (NULL == entry_and)
99 entry_and = find_parser("and");
101 /* Check that it's actually there. If not, that is a bug.*/
102 assert(entry_and != NULL);
104 if (last_pred)
105 switch (last_pred->p_type)
107 case NO_TYPE:
108 error (1, 0, _("oops -- invalid default insertion of and!"));
109 break;
111 case PRIMARY_TYPE:
112 case CLOSE_PAREN:
113 /* We need to interpose the and operator. */
114 new_pred = get_new_pred (entry_and);
115 new_pred->pred_func = pred_and;
116 #ifdef DEBUG
117 new_pred->p_name = find_pred_name (pred_and);
118 #endif /* DEBUG */
119 new_pred->p_type = BI_OP;
120 new_pred->p_prec = AND_PREC;
121 new_pred->need_stat = false;
122 new_pred->need_type = false;
123 new_pred->args.str = NULL;
124 new_pred->side_effects = false;
125 new_pred->no_default_print = false;
126 break;
128 default:
129 break;
132 new_pred = get_new_pred (entry);
133 new_pred->parser_entry = entry;
134 return new_pred;
137 /* Add a primary of predicate type PRED_FUNC (described by ENTRY) to the predicate input list.
139 Return a pointer to the predicate node just inserted.
141 Fills in the following cells of the new predicate node:
143 pred_func PRED_FUNC
144 args(.str) NULL
145 p_type PRIMARY_TYPE
146 p_prec NO_PREC
148 Other cells that need to be filled in are defaulted by
149 get_new_pred_chk_op, which is used to insure that the prior node is
150 either not there at all (we are the very first node) or is an
151 operator. */
153 struct predicate *
154 insert_primary_withpred (const struct parser_table *entry, PRED_FUNC pred_func)
156 struct predicate *new_pred;
158 new_pred = get_new_pred_chk_op (entry);
159 new_pred->pred_func = pred_func;
160 #ifdef DEBUG
161 new_pred->p_name = entry->parser_name;
162 #endif /* DEBUG */
163 new_pred->args.str = NULL;
164 new_pred->p_type = PRIMARY_TYPE;
165 new_pred->p_prec = NO_PREC;
166 return new_pred;
169 /* Add a primary described by ENTRY to the predicate input list.
171 Return a pointer to the predicate node just inserted.
173 Fills in the following cells of the new predicate node:
175 pred_func PRED_FUNC
176 args(.str) NULL
177 p_type PRIMARY_TYPE
178 p_prec NO_PREC
180 Other cells that need to be filled in are defaulted by
181 get_new_pred_chk_op, which is used to insure that the prior node is
182 either not there at all (we are the very first node) or is an
183 operator. */
184 struct predicate *
185 insert_primary (const struct parser_table *entry)
187 assert(entry->pred_func != NULL);
188 return insert_primary_withpred(entry, entry->pred_func);
193 void
194 usage (char *msg)
196 if (msg)
197 fprintf (stderr, "%s: %s\n", program_name, msg);
198 fprintf (stderr, _("\
199 Usage: %s [-H] [-L] [-P] [path...] [expression]\n"), program_name);
200 exit (1);