Handle mailcap's copiousoutput without an external pager.
[elinks.git] / src / mime / backend / common.c
bloba103c19942e1d384ddbc45bae94b519589701661
1 /* MIME handling backends multiplexing */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "elinks.h"
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"
21 /* Backends dynamic area: */
23 #include "mime/backend/default.h"
24 #include "mime/backend/mailcap.h"
25 #include "mime/backend/mimetypes.h"
27 static const struct mime_backend *const 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,
39 unsigned char *
40 get_content_type_backends(unsigned char *extension)
42 const struct mime_backend *backend;
43 int i;
45 foreach_module (backend, mime_backends, i) {
46 if (backend->get_content_type) {
47 unsigned char *content_type;
49 content_type = backend->get_content_type(extension);
50 if (content_type) return content_type;
54 return NULL;
57 struct mime_handler *
58 get_mime_handler_backends(unsigned char *ctype, int have_x)
60 const struct mime_backend *backend;
61 int i;
63 foreach_module (backend, mime_backends, i) {
64 if (backend->get_mime_handler) {
65 struct mime_handler *handler;
67 handler = backend->get_mime_handler(ctype, have_x);
68 if (handler) return handler;
72 return NULL;
75 unsigned char *
76 get_next_path_filename(unsigned char **path_ptr, unsigned char separator)
78 unsigned char *path = *path_ptr;
79 unsigned char *filename = path;
80 int filenamelen;
82 /* Extract file from path */
83 while (*path && *path != separator)
84 path++;
86 filenamelen = path - filename;
88 /* If not at end of string skip separator */
89 if (*path)
90 path++;
92 *path_ptr = path;
94 if (filenamelen > 0) {
95 unsigned char *tmp = memacpy(filename, filenamelen);
97 if (!tmp) return NULL;
98 filename = expand_tilde(tmp);
99 mem_free(tmp);
100 } else {
101 filename = NULL;
104 return filename;
107 struct mime_handler *
108 init_mime_handler(unsigned char *program, unsigned char *description,
109 unsigned char *backend_name, int ask, int block)
111 int programlen = strlen(program);
112 struct mime_handler *handler;
114 handler = mem_calloc(1, sizeof(*handler) + programlen);
115 if (!handler) return NULL;
117 memcpy(handler->program, program, programlen);
119 handler->description = empty_string_or_(description);
120 handler->backend_name = backend_name;
121 handler->block = block;
122 handler->ask = ask;
124 return handler;