1 /* MIME handling backends multiplexing */
2
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "elinks.h"
12
13 #include "main/module.h"
14 #include "mime/backend/common.h"
15 #include "mime/mime.h"
16 #include "util/file.h"
17 #include "util/memory.h"
18 #include "util/string.h"
19
20
21 /* Backends dynamic area: */
22
23 #include "mime/backend/default.h"
24 #include "mime/backend/mailcap.h"
25 #include "mime/backend/mimetypes.h"
26
27 static struct mime_backend *mime_backends[] = {
28 &default_mime_backend,
29 #ifdef CONFIG_MAILCAP
30 &mailcap_mime_backend,
31 #endif
32 #ifdef CONFIG_MIMETYPES
33 &mimetypes_mime_backend,
34 #endif
35 NULL,
36
37 };
38
39 unsigned char *
get_content_type_backends(unsigned char * extension)40 get_content_type_backends(unsigned char *extension)
41 {
42 struct mime_backend *backend;
43 int i;
44
45 foreach_module (backend, mime_backends, i) {
46 if (backend->get_content_type) {
47 unsigned char *content_type;
48
49 content_type = backend->get_content_type(extension);
50 if (content_type) return content_type;
51 }
52 }
53
54 return NULL;
55 }
56
57 struct mime_handler *
get_mime_handler_backends(unsigned char * ctype,int have_x)58 get_mime_handler_backends(unsigned char *ctype, int have_x)
59 {
60 struct mime_backend *backend;
61 int i;
62
63 foreach_module (backend, mime_backends, i) {
64 if (backend->get_mime_handler) {
65 struct mime_handler *handler;
66
67 handler = backend->get_mime_handler(ctype, have_x);
68 if (handler) return handler;
69 }
70 }
71
72 return NULL;
73 }
74
75 unsigned char *
get_next_path_filename(unsigned char ** path_ptr,unsigned char separator)76 get_next_path_filename(unsigned char **path_ptr, unsigned char separator)
77 {
78 unsigned char *path = *path_ptr;
79 unsigned char *filename = path;
80 int filenamelen;
81
82 /* Extract file from path */
83 while (*path && *path != separator)
84 path++;
85
86 filenamelen = path - filename;
87
88 /* If not at end of string skip separator */
89 if (*path)
90 path++;
91
92 *path_ptr = path;
93
94 if (filenamelen > 0) {
95 unsigned char *tmp = memacpy(filename, filenamelen);
96
97 if (!tmp) return NULL;
98 filename = expand_tilde(tmp);
99 mem_free(tmp);
100 } else {
101 filename = NULL;
102 }
103
104 return filename;
105 }
106
107 struct mime_handler *
init_mime_handler(unsigned char * program,unsigned char * description,unsigned char * backend_name,int ask,int block)108 init_mime_handler(unsigned char *program, unsigned char *description,
109 unsigned char *backend_name, int ask, int block)
110 {
111 int programlen = strlen(program);
112 struct mime_handler *handler;
113
114 handler = mem_calloc(1, sizeof(*handler) + programlen);
115 if (!handler) return NULL;
116
117 memcpy(handler->program, program, programlen);
118
119 handler->description = empty_string_or_(description);
120 handler->backend_name = backend_name;
121 handler->block = block;
122 handler->ask = ask;
123
124 return handler;
125 }
126