correctly handle newlines in the file names
[findutils.git] / find / util.c
blob02fc25631b177ae6435476b834003a3c20c5d147
1 /* util.c -- functions for initializing new tree elements, and other things.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA.
20 #include "defs.h"
22 #if ENABLE_NLS
23 # include <libintl.h>
24 # define _(Text) gettext (Text)
25 #else
26 # define _(Text) Text
27 #endif
28 #ifdef gettext_noop
29 # define N_(String) gettext_noop (String)
30 #else
31 /* See locate.c for explanation as to why not use (String) */
32 # define N_(String) String
33 #endif
36 /* Return a pointer to a new predicate structure, which has been
37 linked in as the last one in the predicates list.
39 Set `predicates' to point to the start of the predicates list.
40 Set `last_pred' to point to the new last predicate in the list.
42 Set all cells in the new structure to the default values. */
44 struct predicate *
45 get_new_pred (void)
47 register struct predicate *new_pred;
49 if (predicates == NULL)
51 predicates = (struct predicate *)
52 xmalloc (sizeof (struct predicate));
53 last_pred = predicates;
55 else
57 new_pred = (struct predicate *) xmalloc (sizeof (struct predicate));
58 last_pred->pred_next = new_pred;
59 last_pred = new_pred;
61 last_pred->pred_func = NULL;
62 #ifdef DEBUG
63 last_pred->p_name = NULL;
64 #endif /* DEBUG */
65 last_pred->p_type = NO_TYPE;
66 last_pred->p_prec = NO_PREC;
67 last_pred->side_effects = false;
68 last_pred->no_default_print = false;
69 last_pred->need_stat = true;
70 last_pred->args.str = NULL;
71 last_pred->pred_next = NULL;
72 last_pred->pred_left = NULL;
73 last_pred->pred_right = NULL;
74 return (last_pred);
77 /* Return a pointer to a new predicate, with operator check.
78 Like get_new_pred, but it checks to make sure that the previous
79 predicate is an operator. If it isn't, the AND operator is inserted. */
81 struct predicate *
82 get_new_pred_chk_op (void)
84 struct predicate *new_pred;
86 if (last_pred)
87 switch (last_pred->p_type)
89 case NO_TYPE:
90 error (1, 0, _("oops -- invalid default insertion of and!"));
91 break;
93 case PRIMARY_TYPE:
94 case CLOSE_PAREN:
95 new_pred = get_new_pred ();
96 new_pred->pred_func = pred_and;
97 #ifdef DEBUG
98 new_pred->p_name = find_pred_name (pred_and);
99 #endif /* DEBUG */
100 new_pred->p_type = BI_OP;
101 new_pred->p_prec = AND_PREC;
102 new_pred->need_stat = false;
103 new_pred->args.str = NULL;
105 default:
106 break;
108 return (get_new_pred ());
111 /* Add a primary of predicate type PRED_FUNC to the predicate input list.
113 Return a pointer to the predicate node just inserted.
115 Fills in the following cells of the new predicate node:
117 pred_func PRED_FUNC
118 args(.str) NULL
119 p_type PRIMARY_TYPE
120 p_prec NO_PREC
122 Other cells that need to be filled in are defaulted by
123 get_new_pred_chk_op, which is used to insure that the prior node is
124 either not there at all (we are the very first node) or is an
125 operator. */
127 struct predicate *
128 insert_primary (boolean (*pred_func) (/* ??? */))
130 struct predicate *new_pred;
132 new_pred = get_new_pred_chk_op ();
133 new_pred->pred_func = pred_func;
134 #ifdef DEBUG
135 new_pred->p_name = find_pred_name (pred_func);
136 #endif /* DEBUG */
137 new_pred->args.str = NULL;
138 new_pred->p_type = PRIMARY_TYPE;
139 new_pred->p_prec = NO_PREC;
140 return (new_pred);
143 void
144 usage (char *msg)
146 if (msg)
147 fprintf (stderr, "%s: %s\n", program_name, msg);
148 fprintf (stderr, _("\
149 Usage: %s [path...] [expression]\n"), program_name);
150 exit (1);