summaryrefslogtreecommitdiff
path: root/mimetypes/mtint.h
blob: 9ca7b50bfca794d6d9eb50c027b8f0bd751a01fd (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* This file is part of fileserv.
   Copyright (C) 2017-2019 Sergey Poznyakoff

   Fileserv 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, or (at your option)
   any later version.

   Fileserv 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 fileserv.  If not, see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <stdio.h>
#include <regex.h>
#include "locus.h"
#include "yyloc.h"

#include "mimetypes.h"

struct mimetypes_string
{
  char *ptr;
  size_t len;
};

#define LL_ENTRY(type)							\
  struct								\
  {									\
    struct type *lle_next;   /* next element */				\
    struct type *lle_prev;   /* previous element */			\
  }
#define LL_HEAD(name, type)                                             \
  struct name								\
  {									\
    struct type *ll_head;						\
    struct type *ll_tail;						\
    size_t ll_count;							\
  }
#define LL_HEAD_INIT(head)			                        \
  do									\
    {									\
      (head)->ll_head = (head)->ll_tail = NULL;				\
      (head)->ll_count = 0;						\
    }									\
  while (0)
#define LL_HEAD_INITIALIZER                                             \
  { NULL, NULL, 0 }
#define LL_FIRST(head)                                                  \
  (head)->ll_head
#define LL_LAST(head)                                                   \
  (head)->ll_tail
#define LL_COUNT(head)							\
  (head)->ll_count
#define LLE_NEXT(elt,field)			                        \
  (elt)->field.lle_next
#define LLE_PREV(elt,field)				                \
  (elt)->field.lle_prev
#define LLE_APPEND(head,elt,field)                                      \
  do									\
    {									\
      if (((elt)->field.lle_prev = LL_LAST (head)) == NULL)		\
	LL_FIRST (head) = (elt);					\
      else								\
	(elt)->field.lle_prev->field.lle_next = (elt);			\
      (elt)->field.lle_next = NULL;					\
      LL_LAST (head) = (elt);						\
      (head)->ll_count++;						\
    }									\
  while (0)
#define LLE_UNLINK(head,elt,field)					\
  do									\
    {									\
      if ((elt)->field.lle_next != NULL)				\
	(elt)->field.lle_next->field.lle_prev =	(elt)->field.lle_prev;	\
      else								\
	LL_LAST (head) = (elt)->field.lle_prev;				\
      if ((elt)->field.lle_prev != NULL)				\
	(elt)->field.lle_prev->field.lle_next = (elt)->field.lle_next;	\
      else								\
	LL_FIRST (head) = (elt)->field.lle_next;			\
      (head)->ll_count--;						\
    }									\
  while (0)
#define LL_FOREACH(head,elt,field)		                        \
  for (elt = LL_FIRST (head); elt; elt = LLE_NEXT (elt,field))

#define L_OR  0
#define L_AND 1

enum node_type
  {
    true_node,
    functional_node,
    binary_node,
    negation_node,
    suffix_node
  };

struct rule
{
  char *type;
  int priority;
  struct locus_range loc;
  struct node *node;
  LL_ENTRY(rule) link;
};

LL_HEAD(rule_list, rule);
 
union argument
{
  struct mimetypes_string string;
  unsigned number;
  int c;
  regex_t rx;
};

struct filebuf
{
  char const *name;
  FILE *fp;
};

typedef int (*builtin_t) (union argument *args, struct filebuf const *fb);

struct node
{
  enum node_type type;
  struct locus_range loc;
  union
  {
    struct
    {
      builtin_t fun;
      union argument *args;
    } function;
    struct node *arg;
    struct
    {
      int op;
      struct node *arg1;
      struct node *arg2;
    } bin; 
    struct mimetypes_string suffix;
  } v;
};

struct builtin_tab
{
  char *name;
  char *args;
  builtin_t handler;
};

struct builtin_tab const *find_builtin (char const *ident);

struct arg_elt
{
  struct mimetypes_string string;
  LL_ENTRY(arg_elt) link;
};

LL_HEAD(arg_list, arg_elt);

ssize_t mimetypes_error_format (char **pbuf, size_t *psize,
		      struct locus_range const *lr, char const *fmt,
		      va_list ap);
void mimetypes_error_at (struct locus_range const *lr, char const *fmt, ...);
void mimetypes_error (char const *fmt, ...);
void print_locus_range (FILE *fp, struct locus_range const *lr);
void mimetypes_nomem (void);

int mimetypes_open (const char *name);
void mimetypes_close (void);
void lex_next_rule (void);

extern struct rule_list rule_list;




Return to:

Send suggestions and report system problems to the System administrator.