All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Vladimir 'φ-coder/phcoder' Serbinenko" <phcoder@gmail.com>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: [PATCH] Regexp matching in grub
Date: Sun, 27 Dec 2009 16:38:37 +0100	[thread overview]
Message-ID: <4B377F7D.5050206@gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 2136 bytes --]

Hello, I imported regexp from gnulib into grub. Available from
people/phcoder/regexp. Following changes required:
common.rmk Add:
pkglib_MODULES += regexp.mod
regexp_mod_SOURCES = gnulib/regex.c commands/regexp.c
regexp_mod_CFLAGS = $(COMMON_CFLAGS) -Wno-sign-compare -Wno-unused
regexp_mod_LDFLAGS = $(COMMON_LDFLAGS)
commands/regexp.c New file (attached)
gnulib/regcomp.c, gnulib/regex_internal.c, gnulib/regexec.c: Copied
verbatim from gnulib.
gnulib/regex.c: New file (attached). Based on file from gnulib, heavily
modified
gnulib/regex_internal.h. Copied from gnulib following modification needed:
--- /home/phcoder/repos/gnulib/lib/regex_internal.h    2009-11-19
14:02:55.406619037 +0100
+++ gnulib/regex_internal.h    2009-12-27 14:28:10.504789886 +0100
@@ -21,33 +21,6 @@
 #ifndef _REGEX_INTERNAL_H
 #define _REGEX_INTERNAL_H 1
 
-#include <assert.h>
-#include <ctype.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#ifdef _LIBC
-# include <langinfo.h>
-#else
-# include "localcharset.h"
-#endif
-#if defined HAVE_LOCALE_H || defined _LIBC
-# include <locale.h>
-#endif
-
-#include <wchar.h>
-#include <wctype.h>
-#include <stdint.h>
-#if defined _LIBC
-# include <bits/libc-lock.h>
-#else
-# define __libc_lock_init(NAME) do { } while (0)
-# define __libc_lock_lock(NAME) do { } while (0)
-# define __libc_lock_unlock(NAME) do { } while (0)
-#endif
-
 /* In case that the system doesn't have isblank().  */
 #if !defined _LIBC && ! (defined isblank || (HAVE_ISBLANK &&
HAVE_DECL_ISBLANK))
 # define isblank(ch) ((ch) == ' ' || (ch) == '\t')

include/grub/gnulib-wrap.h: New file
include/grub/regex.h: Copied from gnulib. Following modification:
--- /home/phcoder/repos/gnulib/lib/regex.h    2009-11-19
14:02:55.402619158 +0100
+++ include/grub/regex.h    2009-12-27 14:20:37.356524246 +0100
@@ -21,7 +21,7 @@
 #ifndef _REGEX_H
 #define _REGEX_H 1
 
-#include <sys/types.h>
+#include <grub/gnulib-wrap.h>
 
 /* Allow the use in C++ code.  */
 #ifdef __cplusplus

-- 
Regards
Vladimir 'φ-coder/phcoder' Serbinenko


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: regexp.c --]
[-- Type: text/x-csrc; name="regexp.c", Size: 2042 bytes --]

/* regexp.c -- The regexp command.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2005,2007  Free Software Foundation, Inc.
 *
 *  GRUB 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/command.h>
#include <grub/regex.h>

static grub_err_t
grub_cmd_regexp (grub_command_t cmd __attribute__ ((unused)),
		 int argc, char **args)
{
  int argn = 0;
  int matches = 0;
  regex_t regex;
  int ret;
  grub_size_t s;
  char *comperr;
  grub_err_t err;

  if (argc != 2)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "2 arguments expected");

  ret = regcomp (&regex, args[0], RE_SYNTAX_GNU_AWK);
  if (ret)
    goto fail;

  ret = regexec (&regex, args[1], 0, 0, 0);
  if (!ret)
    {
      regfree (&regex);
      return GRUB_ERR_NONE;
    }

 fail:
  s = regerror (ret, &regex, 0, 0);
  comperr = grub_malloc (s);
  if (!comperr)
    {
      regfree (&regex);
      return grub_errno;
    }
  regerror (ret, &regex, comperr, s);
  err = grub_error (GRUB_ERR_TEST_FAILURE, "%s", comperr);
  regfree (&regex);
  grub_free (comperr);
  return err;
}

static grub_command_t cmd;
\f
GRUB_MOD_INIT(regexp)
{
  cmd = grub_register_command ("regexp", grub_cmd_regexp,
			       "REGEXP STRING",
			       "Test if REGEXP matches STRING.");
}

GRUB_MOD_FINI(regexp)
{
  grub_unregister_command (cmd);
}

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.3: regex.c --]
[-- Type: text/x-csrc; name="regex.c", Size: 1085 bytes --]

/* Extended regular expression matching and search library.
   Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.

   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, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */

#include <grub/regex.h>

#include "regex_internal.h"

#include "regex_internal.c"
#include "regcomp.c"
#include "regexec.c"

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.4: gnulib-wrap.h --]
[-- Type: text/x-chdr; name="gnulib-wrap.h", Size: 3282 bytes --]

/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2009  Free Software Foundation, Inc.
 *
 *  GRUB 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef GRUB_GNULIB_WRAP_H
#define GRUB_GNULIB_WRAP_H	1

#include <grub/mm.h>
#include <grub/misc.h>

typedef grub_size_t size_t;
typedef int bool;
static const bool true = 1;
static const bool false = 0;

#define assert(x) assert_real(__FILE__, __LINE__, x)

static inline void
assert_real (const char *file, int line, int cond)
{
  if (!cond)
    grub_fatal ("Assertion failed at %s:%d\n", file, line);
}

static inline char *
locale_charset (void)
{
  return "UTF-8";
}

#define MB_CUR_MAX 6

static inline void *
realloc (void *ptr, grub_size_t size)
{
  return grub_realloc (ptr, size);
}

static inline int
toupper (int c)
{
  return grub_toupper (c);
}

static inline int 
isspace (int c)
{
  return grub_isspace (c);
}

static inline int 
isdigit (int c)
{
  return grub_isdigit (c);
}

static inline int
islower (int c)
{
  return (c >= 'a' && c <= 'z');
}

static inline int
isupper (int c)
{
  return (c >= 'A' && c <= 'Z');
}

static inline int
isxdigit (int c)
{
  return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
    || (c >= '0' && c <= '9');
}

static inline int 
isprint (int c)
{
  return grub_isprint (c);
}

static inline int 
iscntrl (int c)
{
  return !grub_isprint (c);
}

static inline int 
isgraph (int c)
{
  return grub_isprint (c) && !grub_isspace (c);
}

static inline int
isalnum (int c)
{
  return grub_isalpha (c) || grub_isdigit (c);
}

static inline int 
ispunct (int c)
{
  return grub_isprint (c) && !grub_isspace (c) && !isalnum (c);
}

static inline int 
isalpha (int c)
{
  return grub_isalpha (c);
}

static inline int
tolower (int c)
{
  return grub_tolower (c);
}

static inline grub_size_t
strlen (const char *s)
{
  return grub_strlen (s);
}

static inline int 
strcmp (const char *s1, const char *s2)
{
  return grub_strcmp (s1, s2);
}

static inline void
abort (void)
{
  grub_abort ();
}

static inline void 
free (void *ptr)
{
  grub_free (ptr);
}

static inline void *
malloc (grub_size_t size)
{
  return grub_malloc (size);
}

static inline void *
calloc (grub_size_t size, grub_size_t nelem)
{
  return grub_zalloc (size * nelem);
}

#define ULONG_MAX GRUB_ULONG_MAX
#define UCHAR_MAX 0xff

/* UCS-4.  */
typedef grub_uint32_t wchar_t;

#undef __libc_lock_init
#define __libc_lock_init(x)
#undef __libc_lock_lock
#define __libc_lock_lock(x)
#undef __libc_lock_unlock
#define __libc_lock_unlock(x)

#endif

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 293 bytes --]

                 reply	other threads:[~2009-12-27 15:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B377F7D.5050206@gmail.com \
    --to=phcoder@gmail.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.