Commit 071c0552 071c05523b657a805576f7bfc9562445b43afc20 by Sergey Poznyakoff

Added to the repository

1 parent 09617f83
1 ## Process this file with GNU Automake to create Makefile.in
2
3 ## Copyright (C) 2005 Free Software Foundation, Inc.
4 ##
5 ## GNU Mailutils is free software; you can redistribute it and/or
6 ## modify it under the terms of the GNU General Public License as
7 ## published by the Free Software Foundation; either version 2, or (at
8 ## your option) any later version.
9 ##
10 ## This program is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program; if not, write to the Free Software
17 ## Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/lib\
20 -I${top_builddir}/include/mailutils/gnu @INTLINCS@
21
22 AM_CFLAGS = -DSYSCONFDIR=\"$(sysconfdir)\"
23
24 bin_PROGRAMS = mimeview
25 mimeview_SOURCES = \
26 mimeview.c \
27 mimetypes-gram.c \
28 mimetypes-lex.c \
29 mimetypes-decl.h \
30 mimeview.h
31
32 YLWRAP = $(SHELL) $(top_srcdir)/scripts/ylwrap
33 AM_YFLAGS=-vt
34 AM_LEXFLAGS=-d
35 EXTRA_DIST = mimetypes.y mimetypes.l
36
37 mimetypes-gram.c mimetypes-decl.h: $(srcdir)/mimetypes.y
38 $(YLWRAP) "$(YACC) $(AM_YFLAGS) -d" $< \
39 y.tab.c mimetypes-gram.c y.tab.h mimetypes-decl.h \
40 y.output mimetypes.output \
41 -- -yy mimetypes_yy
42
43 mimetypes-lex.c: $(srcdir)/mimetypes.l mimetypes-decl.h
44 $(YLWRAP) "$(LEX) $(AM_LEXFLAGS) $(LEXFLAGS)" \
45 $(srcdir)/mimetypes.l lex.yy.c mimetypes-lex.c \
46 -- -yy mimetypes_yy
47
48 BUILT_SOURCES = mimetypes-gram.c mimetypes-lex.c mimetypes-decl.h
49
50 mimeview_LDADD = \
51 ../mailbox/libmailbox.la\
52 ../lib/libmailutils.la \
53 @LTLIBINTL@
54
1 %{
2 /* GNU Mailutils -- a suite of utilities for electronic mail
3 Copyright (C) 2005 Free Software Foundation, Inc.
4
5 GNU Mailutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 GNU Mailutils is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNU Mailutils; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <mimeview.h>
24 #include <mimetypes-decl.h>
25 #include <mu_asprintf.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28
29 static int line_num;
30 static const char *file_name;
31 static int file_name_alloc;
32
33 static struct obstack stack;
34 static int prev_state;
35
36 static unsigned
37 digit_to_number (char c)
38 {
39 return (unsigned) (c >= '0' && c <= '9' ? c-'0' :
40 c >= 'A' && c <= 'Z' ? c-'A'+10 :
41 c-'a'+10);
42 }
43 %}
44 %x ARGS HEX
45 X [0-9a-fA-F]
46 IDENT [a-zA-Z_\.][a-zA-Z0-9_\.-]*
47 WS [ \t]*
48 %%
49 /* Comments */
50 <INITIAL>#.*\n { line_num++; }
51 <INITIAL>#.* /* end-of-file comment */;
52 /* Tokens */
53 \\\n { line_num++; }
54 \n { line_num++; return EOL; }
55 {WS} ;
56 {IDENT} {
57 obstack_grow (&stack, yytext, yyleng);
58 yylval.string.len = obstack_object_size (&stack);
59 obstack_1grow (&stack, 0);
60 yylval.string.ptr = obstack_finish (&stack);
61 return IDENT;
62 }
63 <INITIAL>{IDENT}"(" {
64 obstack_grow (&stack, yytext, yyleng-1);
65 yylval.string.len = obstack_object_size (&stack);
66 obstack_1grow (&stack, 0);
67 yylval.string.ptr = obstack_finish (&stack);
68 BEGIN(ARGS);
69 return IDENT_L;
70 }
71 <INITIAL,ARGS>\"[^\\"\n]*\" {
72 obstack_grow (&stack, yytext+1, yyleng-2);
73 yylval.string.len = obstack_object_size (&stack);
74 obstack_1grow (&stack, 0);
75 yylval.string.ptr = obstack_finish (&stack);
76 return STRING;
77 }
78 <INITIAL,ARGS>"<" {
79 prev_state = YYSTATE;
80 BEGIN(HEX);
81 }
82 <ARGS>[^ \t<\\\n),]+/[),] {
83 obstack_grow (&stack, yytext, yyleng);
84 yylval.string.len = obstack_object_size (&stack);
85 obstack_1grow (&stack, 0);
86 yylval.string.ptr = obstack_finish (&stack);
87 return STRING;
88 }
89 <ARGS>[^ \t<\\\n),]+< {
90 obstack_grow (&stack, yytext, yyleng);
91 prev_state = YYSTATE;
92 BEGIN(HEX);
93 }
94 <INITIAL>[^ \t<\\\n)+&]/[ \t\\\n)+&] {
95 obstack_grow (&stack, yytext, yyleng);
96 yylval.string.len = obstack_object_size (&stack);
97 obstack_1grow (&stack, 0);
98 yylval.string.ptr = obstack_finish (&stack);
99 return STRING;
100 }
101 <ARGS>[^ \t<\\\n),]/[ \t\\\n] {
102 obstack_grow (&stack, yytext, yyleng);
103 yylval.string.len = obstack_object_size (&stack);
104 obstack_1grow (&stack, 0);
105 yylval.string.ptr = obstack_finish (&stack);
106 return STRING;
107 }
108 <HEX>{X}{X} {
109 int c = digit_to_number (yytext[0]*16 + yytext[1]);
110 obstack_1grow (&stack, c);
111 }
112 <HEX>">"/[ \t\\\n,)] {
113 BEGIN(prev_state);
114 yylval.string.len = obstack_object_size (&stack);
115 obstack_1grow (&stack, 0);
116 yylval.string.ptr = obstack_finish (&stack);
117 return STRING;
118 }
119 <HEX>">" {
120 BEGIN(prev_state);
121 }
122 /* Special cases: && and ||. Docs don't say anything about them, but
123 I've found them in my mime.types file... --Sergey */
124 "&&" return '+';
125 "||" return ',';
126 /* Operators */
127 "!"|"+"|"("|")"|"/" return yytext[0];
128 <ARGS>"," return yytext[0];
129 <ARGS>")" { BEGIN(INITIAL); return yytext[0]; }
130 <INITIAL,ARGS,HEX>. {
131 fprintf (stderr, "Invalid character '%c', state %d\n", yytext[0], YYSTATE);
132 abort();
133 }
134 %%
135
136 void
137 mimetypes_lex_debug (int level)
138 {
139 yy_flex_debug = level;
140 }
141
142 int
143 mimetypes_open (const char *name)
144 {
145 struct stat st;
146 if (stat (name, &st))
147 {
148 mu_error (_("Cannot stat `%s': %s"), name, mu_strerror (errno));
149 return -1;
150 }
151
152 if (S_ISDIR (st.st_mode))
153 {
154 asprintf (&file_name, "%s/mime.types", name);
155 file_name_alloc = 1;
156 }
157 else
158 {
159 file_name = name;
160 file_name_alloc = 0;
161 }
162
163 yyin = fopen (file_name, "r");
164 if (!yyin)
165 {
166 mu_error (_("Cannot open `%s': %s"), file_name, mu_strerror (errno));
167 if (file_name_alloc)
168 {
169 free (file_name);
170 file_name_alloc = 0;
171 }
172 return -1;
173 }
174 line_num = 1;
175 obstack_init (&stack);
176 return 0;
177 }
178
179 void
180 mimetypes_close ()
181 {
182 fclose (yyin);
183 if (file_name_alloc)
184 {
185 free (file_name);
186 file_name_alloc = 0;
187 }
188 }
189
190 int
191 yyerror (char *s)
192 {
193 mu_error ("%s:%lu: %s", file_name, line_num, s);
194 return 0;
195 }
196
197 int
198 yywrap ()
199 {
200 return 1;
201 }
202
203 struct mimetypes_string
204 mimetypes_append_string2 (struct mimetypes_string *s1,
205 char c,
206 struct mimetypes_string *s2)
207 {
208 struct mimetypes_string r;
209
210 r.len = s1->len + s2->len + 1;
211 obstack_grow (&stack, s1->ptr, s1->len);
212 obstack_1grow (&stack, c);
213 obstack_grow (&stack, s2->ptr, s2->len);
214 obstack_1grow (&stack, 0);
215 r.ptr = obstack_finish (&stack);
216 return r;
217 }
218
219 struct mimetypes_string *
220 mimetypes_string_dup (struct mimetypes_string *s)
221 {
222 obstack_grow (&stack, s, sizeof *s);
223 return obstack_finish (&stack);
224 }
225
226 void *
227 mimetypes_malloc (size_t size)
228 {
229 return obstack_alloc(&stack, size);
230 }
231
232 void
233 reset_lex ()
234 {
235 BEGIN(INITIAL);
236 }
237
238
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 GNU Mailutils 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 GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #ifdef HAVE_STRINGS_H
22 # include <strings.h>
23 #endif
24 #include <mailutils/mailutils.h>
25 #include <xalloc.h>
26 #include <fnmatch.h>
27 #define obstack_chunk_alloc malloc
28 #define obstack_chunk_free free
29 #include <obstack.h>
30
31 struct mimetypes_string
32 {
33 char *ptr;
34 size_t len;
35 };
36
37 int mimetypes_yylex (void);
38 int mimetypes_yyerror (char *s);
39
40 int mimetypes_open (const char *name);
41 void mimetypes_close (void);
42 int mimetypes_parse (const char *name);
43 void mimetypes_gram_debug (int level);
44 void mimetypes_lex_debug (int level);
45 void mimetypes_lex_init (void);
46 void reset_lex (void);
47 void *mimetypes_malloc (size_t size);
48
49 struct mimetypes_string mimetypes_append_string2 (struct mimetypes_string *s1,
50 char c,
51 struct mimetypes_string *s2);
52 struct mimetypes_string *mimetypes_string_dup (struct mimetypes_string *s);
53
54 const char *get_file_type (void);
55
56 extern char *mimeview_file;
57 extern FILE *mimeview_fp;
58 extern int debug_level;
59
60 #define DEBUG(l,f) if (debug_level > (l)) printf f