1/* Pattern Matchers - Common Utilities.
2   Copyright (C) 1992, 1998, 2000, 2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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 Foundation,
16   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18#if HAVE_CONFIG_H
19# include <config.h>
20#endif
21
22/* Specification.  */
23#include "m-common.h"
24
25#include <ctype.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include "error.h"
30#include "exitfail.h"
31#include "xalloc.h"
32#include "gettext.h"
33#define _(str) gettext (str)
34
35#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
36# define IN_CTYPE_DOMAIN(c) 1
37#else
38# define IN_CTYPE_DOMAIN(c) isascii(c)
39#endif
40#define ISUPPER(C) (IN_CTYPE_DOMAIN (C) && isupper (C))
41#define TOLOWER(C) (ISUPPER(C) ? tolower(C) : (C))
42
43void
44kwsinit (struct compiled_kwset *ckwset,
45	 bool match_icase, bool match_words, bool match_lines, char eolbyte)
46{
47  if (match_icase)
48    {
49      int i;
50
51      ckwset->trans = (char *) xmalloc (NCHAR * sizeof (char));
52      for (i = 0; i < NCHAR; i++)
53	ckwset->trans[i] = TOLOWER (i);
54      ckwset->kwset = kwsalloc (ckwset->trans);
55    }
56  else
57    {
58      ckwset->trans = NULL;
59      ckwset->kwset = kwsalloc (NULL);
60    }
61  if (ckwset->kwset == NULL)
62    error (exit_failure, 0, _("memory exhausted"));
63  ckwset->match_words = match_words;
64  ckwset->match_lines = match_lines;
65  ckwset->eolbyte = eolbyte;
66}
67
68#ifdef MBS_SUPPORT
69/* This function allocate the array which correspond to "buf".
70   Then this check multibyte string and mark on the positions which
71   are not singlebyte character nor the first byte of a multibyte
72   character.  Caller must free the array.  */
73char*
74check_multibyte_string (const char *buf, size_t buf_size)
75{
76  char *mb_properties = (char *) malloc (buf_size);
77  mbstate_t cur_state;
78  int i;
79
80  memset (&cur_state, 0, sizeof (mbstate_t));
81  memset (mb_properties, 0, sizeof (char) * buf_size);
82  for (i = 0; i < buf_size ;)
83    {
84      size_t mbclen;
85      mbclen = mbrlen (buf + i, buf_size - i, &cur_state);
86
87      if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0)
88	{
89	  /* An invalid sequence, or a truncated multibyte character.
90	     We treat it as a singlebyte character.  */
91	  mbclen = 1;
92	}
93      mb_properties[i] = mbclen;
94      i += mbclen;
95    }
96
97  return mb_properties;
98}
99#endif
100