aboutsummaryrefslogtreecommitdiff
path: root/src/meta.c
blob: d93f43037d4b1cd11b49bf35606c377b64e703fe (plain)
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
147
148
149
150
151
152
153
/* wydawca - automatic release submission daemon
   Copyright (C) 2007-2011 Sergey Poznyakoff

   Wydawca 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.

   Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"
#include "sql.h"

static const char *
meta_expand (struct metadef *def, void *data)
{
  if (!def->value)
    {
      if (def->expand)
	return def->expand (def, data);
      def->value = "INTERNAL ERROR: NONEXPANDABLE DATA";
    }
  return def->value;
}

static const char *
find_expansion_char (int c, struct metadef *def, void *data)
{
  for (; def->kw; def++)
    if (def->kw[1] == 0 && def->kw[0] == c)
      return meta_expand (def, data);
  return NULL;
}

static const char *
find_expansion_word (const char *kw, size_t len,
		     struct metadef *def, void *data)
{
  for (; def->kw; def++)
    if (strlen (def->kw) == len && memcmp (def->kw, kw, len) == 0)
      return meta_expand (def, data);
  return NULL;
}

char *
meta_expand_string (const char *string, struct metadef *def, void *data,
		    struct dictionary *dict, void *handle)
{
  const char *p, *s;
  char *res;
  struct txtacc *acc;

  if (!string)
    return NULL;

  acc = txtacc_create ();

  for (p = string; *p;)
    {
      char *e;
      size_t len = strcspn (p, "$");

      txtacc_grow (acc, p, len);
      p += len;
      if (*p == '$')
	{
	  switch (*++p)
	    {
	    case '$':
	      txtacc_grow (acc, p, 1);
	      p++;
	      break;

	    case '-':
	      if (*++p)
		p++;
	      break;
	      
	    case '{':
	      e = strchr (p + 1, '}');
	      if (e && (s = find_expansion_word (p + 1, e - p - 1, def, data)))
		{
		  if (dict)
		    {
		      char *newval;
		      size_t len;
		      /* FIXME: Return value? */
		      dictionary_quote_string (dict, handle, s, &newval, &len);
		      txtacc_grow (acc, newval, len);
		      free (newval);
		    }
		  else
		    txtacc_grow (acc, s, strlen (s));
		  p = e + 1;
		}
	      else
		{
		  txtacc_grow (acc, p - 1, 2);
		  p++;
		}
	      break;

	    default:
	      if ((s = find_expansion_char (*p, def, data)) != NULL)
		len = strlen (s);
	      else
		{
		  s = p - 1;
		  len = 1;
		}
	      
	      if (dict)
		{
		  char *newval;
		  size_t len;
		  /* FIXME: Return value? */
		  dictionary_quote_string (dict, handle, s, &newval, &len);
		  txtacc_grow (acc, newval, len);
		  free (newval);
		}
	      else
		txtacc_grow (acc, s, len);
	      p++;
	    }
	}
      else
	txtacc_grow (acc, p, 1);
    }
  txtacc_1grow (acc, 0);
  res = txtacc_finish (acc, 1);
  txtacc_free (acc);
  return res;
}

void
meta_free (struct metadef *def)
{
  for (; def->kw; def++)
    {
      if (def->storage)
	{
	  free (def->storage);
	  def->value = def->storage = NULL;
	}
    }
}

Return to:

Send suggestions and report system problems to the System administrator.