1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*
* cook - file construction tool
* Copyright (C) 1999, 2006, 2007 Peter Miller;
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
* MANIFEST: functions to manipulate pattern matching stacks
*/
#include <cook/match/stack.h>
#include <common/mem.h>
#include <common/trace.h>
/*
* NAME
* match_stack_push - patch match fields
*
* SYNOPSIS
* void match_stack_push(match_ty *field);
*
* DESCRIPTION
* The match_stack_push function is used to push a pattern onto the
* stack of match fields. A NULL pointer may be pushed. This
* mechanism is used by the chef (cook.c) to indicate implicit and
* explicit recipe replacements.
*
* RETURNS
* void
*/
void
match_stack_push(match_stack_ty *msp, const match_ty *field)
{
trace(("match_stack_push(field = %08X)\n{\n", field));
if (msp->stack_depth >= msp->stack_depth_max)
{
size_t nbytes;
msp->stack_depth_max = msp->stack_depth_max * 2 + 4;
nbytes = msp->stack_depth_max * sizeof(msp->stack[0]);
msp->stack = mem_change_size(msp->stack, nbytes);
}
msp->stack[msp->stack_depth++] = field;
trace(("}\n"));
}
/*
* NAME
* match_stack_top - top of match stack
*
* SYNOPSIS
* match_ty *match_stack_top(void);
*
* DESCRIPTION
* The match_stack_top function is used to indicate the top of the
* match stack.
*
* RETURNS
* match_ty * - a pointer to a match strcuture, or NULL if the stack is
* empty, or a NULL was pashed to mak an exlpicit recipe.
*/
const match_ty *
match_stack_top(const match_stack_ty *msp)
{
if (msp->stack_depth <= 0)
return 0;
return msp->stack[msp->stack_depth - 1];
}
/*
* NAME
* match_stack_pop - shorten stack
*
* SYNOPSIS
* match_ty *match_stack_pop(void);
*
* DESCRIPTION
* The match_stack_pop function is used to pop a match structure
* from the match stack.
*
* RETURNS
* match_ty * - a pointer to a match strcuture, or NULL if the stack is
* empty, or a NULL was pashed to mak an exlpicit recipe.
*
* CAVEAT
* It is an error for the stack to be empty.
*/
const match_ty *
match_stack_pop(match_stack_ty *msp)
{
const match_ty *field;
trace(("match_stack_pop()\n{\n"));
assert(msp->stack_depth);
if (msp->stack_depth > 0)
{
--msp->stack_depth;
field = msp->stack[msp->stack_depth];
}
else
field = 0;
trace(("return %08X;\n", field));
trace(("}\n"));
return field;
}
match_stack_ty *
match_stack_new(void)
{
match_stack_ty *msp;
msp = mem_alloc(sizeof(match_stack_ty));
msp->stack = 0;
msp->stack_depth = 0;
msp->stack_depth_max = 0;
return msp;
}
void
match_stack_delete(match_stack_ty *msp)
{
assert(msp);
if (msp->stack)
mem_free(msp->stack);
mem_free(msp);
}
|