Separated ariables representing current state from variable representing option infor...
[findutils.git] / find / util.c
blob4b08fa26b7eff695eba625ad53bbe42853e5d620
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA.
20 #include "defs.h"
21 #include "../gnulib/lib/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
37 /* Return a pointer to a new predicate structure, which has been
38 linked in as the last one in the predicates list.
40 Set `predicates' to point to the start of the predicates list.
41 Set `last_pred' to point to the new last predicate in the list.
43 Set all cells in the new structure to the default values. */
45 struct predicate *
46 get_new_pred (void)
48 register struct predicate *new_pred;
50 if (predicates == NULL)
52 predicates = (struct predicate *)
53 xmalloc (sizeof (struct predicate));
54 last_pred = predicates;
56 else
58 new_pred = (struct predicate *) xmalloc (sizeof (struct predicate));
59 last_pred->pred_next = new_pred;
60 last_pred = new_pred;
62 last_pred->pred_func = NULL;
63 #ifdef DEBUG
64 last_pred->p_name = NULL;
65 #endif /* DEBUG */
66 last_pred->p_type = NO_TYPE;
67 last_pred->p_prec = NO_PREC;
68 last_pred->side_effects = false;
69 last_pred->no_default_print = false;
70 last_pred->need_stat = true;
71 last_pred->args.str = NULL;
72 last_pred->pred_next = NULL;
73 last_pred->pred_left = NULL;
74 last_pred->pred_right = NULL;
75 return (last_pred);
78 /* Return a pointer to a new predicate, with operator check.
79 Like get_new_pred, but it checks to make sure that the previous
80 predicate is an operator. If it isn't, the AND operator is inserted. */
82 struct predicate *
83 get_new_pred_chk_op (void)
85 struct predicate *new_pred;
87 if (last_pred)
88 switch (last_pred->p_type)
90 case NO_TYPE:
91 error (1, 0, _("oops -- invalid default insertion of and!"));
92 break;
94 case PRIMARY_TYPE:
95 case CLOSE_PAREN:
96 new_pred = get_new_pred ();
97 new_pred->pred_func = pred_and;
98 #ifdef DEBUG
99 new_pred->p_name = find_pred_name (pred_and);
100 #endif /* DEBUG */
101 new_pred->p_type = BI_OP;
102 new_pred->p_prec = AND_PREC;
103 new_pred->need_stat = false;
104 new_pred->args.str = NULL;
106 default:
107 break;
109 return (get_new_pred ());
112 /* Add a primary of predicate type PRED_FUNC to the predicate input list.
114 Return a pointer to the predicate node just inserted.
116 Fills in the following cells of the new predicate node:
118 pred_func PRED_FUNC
119 args(.str) NULL
120 p_type PRIMARY_TYPE
121 p_prec NO_PREC
123 Other cells that need to be filled in are defaulted by
124 get_new_pred_chk_op, which is used to insure that the prior node is
125 either not there at all (we are the very first node) or is an
126 operator. */
128 struct predicate *
129 insert_primary (boolean (*pred_func) (/* ??? */))
131 struct predicate *new_pred;
133 new_pred = get_new_pred_chk_op ();
134 new_pred->pred_func = pred_func;
135 #ifdef DEBUG
136 new_pred->p_name = find_pred_name (pred_func);
137 #endif /* DEBUG */
138 new_pred->args.str = NULL;
139 new_pred->p_type = PRIMARY_TYPE;
140 new_pred->p_prec = NO_PREC;
141 return (new_pred);
144 void
145 usage (char *msg)
147 if (msg)
148 fprintf (stderr, "%s: %s\n", program_name, msg);
149 fprintf (stderr, _("\
150 Usage: %s [-H] [-L] [-P] [path...] [expression]\n"), program_name);
151 exit (1);