blob: e737504d90214dad8d9f135f1db75b14a7b2c966 [file] [log] [blame]
Stefan Reinauer6540ae52007-07-12 16:35:42 +00001/*****************************************************************************\
2 * reg_expr.c
Stefan Reinauer6540ae52007-07-12 16:35:42 +00003 *****************************************************************************
4 * Copyright (C) 2002-2005 The Regents of the University of California.
5 * Produced at the Lawrence Livermore National Laboratory.
6 * Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
7 * UCRL-CODE-2003-012
8 * All rights reserved.
9 *
Uwe Hermann6e565942008-03-01 19:06:32 +000010 * This file is part of nvramtool, a utility for reading/writing coreboot
Stefan Reinauerf527e702008-01-18 15:33:49 +000011 * parameters and displaying information from the coreboot table.
Uwe Hermann6e565942008-03-01 19:06:32 +000012 * For details, see http://coreboot.org/nvramtool.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000013 *
14 * Please also read the file DISCLAIMER which is included in this software
15 * distribution.
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License (as published by the
19 * Free Software Foundation) version 2, dated June 1991.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
24 * conditions of the GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
Stefan Reinauerac7a2d22009-09-23 21:53:25 +000028 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000029\*****************************************************************************/
30
31#include <stdarg.h>
32#include "common.h"
33#include "reg_expr.h"
34
35/****************************************************************************
36 * compile_reg_exprs
37 *
38 * Compile a bunch of regular expressions.
39 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000040void compile_reg_exprs(int cflags, int num_exprs,
41 /* const char *expr1, regex_t *reg1, */ ...)
42{
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;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000049
Stefan Reinauer90b96b62010-01-13 21:00:23 +000050 va_start(ap, num_exprs);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000051
Stefan Reinauer90b96b62010-01-13 21:00:23 +000052 for (i = 0; i < num_exprs; i++) {
53 expr = va_arg(ap, const char *);
54 reg = va_arg(ap, regex_t *);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000055
Stefan Reinauer90b96b62010-01-13 21:00:23 +000056 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 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +000062
Stefan Reinauer90b96b62010-01-13 21:00:23 +000063 va_end(ap);
64}
Stefan Reinauer6540ae52007-07-12 16:35:42 +000065
66/****************************************************************************
67 * free_reg_exprs
68 *
69 * Destroy a bunch of previously compiled regular expressions.
70 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000071void free_reg_exprs(int num_exprs, /* regex_t *reg1, */ ...)
72{
73 va_list ap;
74 int i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000075
Stefan Reinauer90b96b62010-01-13 21:00:23 +000076 va_start(ap, num_exprs);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000077
Stefan Reinauer90b96b62010-01-13 21:00:23 +000078 for (i = 0; i < num_exprs; i++)
79 regfree(va_arg(ap, regex_t *));
Stefan Reinauer6540ae52007-07-12 16:35:42 +000080
Stefan Reinauer90b96b62010-01-13 21:00:23 +000081 va_end(ap);
82}