Missed this file in the previous check-in, r3248.
[coreboot.git] / util / nvramtool / reg_expr.c
1 /*****************************************************************************\
2  * reg_expr.c
3  * $Id$
4  *****************************************************************************
5  *  Copyright (C) 2002-2005 The Regents of the University of California.
6  *  Produced at the Lawrence Livermore National Laboratory.
7  *  Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
8  *  UCRL-CODE-2003-012
9  *  All rights reserved.
10  *
11  *  This file is part of nvramtool, a utility for reading/writing coreboot
12  *  parameters and displaying information from the coreboot table.
13  *  For details, see http://coreboot.org/nvramtool.
14  *
15  *  Please also read the file DISCLAIMER which is included in this software
16  *  distribution.
17  *
18  *  This program is free software; you can redistribute it and/or modify it
19  *  under the terms of the GNU General Public License (as published by the
20  *  Free Software Foundation) version 2, dated June 1991.
21  *
22  *  This program is distributed in the hope that it will be useful, but
23  *  WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the terms and
25  *  conditions of the GNU General Public License for more details.
26  *
27  *  You should have received a copy of the GNU General Public License along
28  *  with this program; if not, write to the Free Software Foundation, Inc.,
29  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30 \*****************************************************************************/
31
32 #include <stdarg.h>
33 #include "common.h"
34 #include "reg_expr.h"
35
36 /****************************************************************************
37  * compile_reg_exprs
38  *
39  * Compile a bunch of regular expressions.
40  ****************************************************************************/
41 void compile_reg_exprs (int cflags, int num_exprs,
42                         /* const char *expr1, regex_t *reg1, */ ...)
43  { static const size_t ERROR_BUF_SIZE = 256;
44    char error_msg[ERROR_BUF_SIZE];
45    va_list ap;
46    regex_t *reg;
47    const char *expr;
48    int i, result;
49
50    va_start(ap, num_exprs);
51
52    for (i = 0; i < num_exprs; i++)
53     { expr = va_arg(ap, const char *);
54       reg  = va_arg(ap, regex_t *);
55
56       if ((result = regcomp(reg, expr, cflags)) != 0)
57        { regerror(result, reg, error_msg, ERROR_BUF_SIZE);
58          fprintf(stderr, "%s: %s\n", prog_name, error_msg);
59          exit(1);
60        }
61     }
62
63    va_end(ap);
64  }
65
66 /****************************************************************************
67  * free_reg_exprs
68  *
69  * Destroy a bunch of previously compiled regular expressions.
70  ****************************************************************************/
71 void free_reg_exprs (int num_exprs, /* regex_t *reg1, */ ...)
72  { va_list ap;
73    int i;
74
75    va_start(ap, num_exprs);
76
77    for (i = 0; i < num_exprs; i++)
78       regfree(va_arg(ap, regex_t *));
79
80    va_end(ap);
81  }