1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-28 01:35:32 +00:00
elinks/src/mime/backend/default.c

229 lines
6.4 KiB
C
Raw Normal View History

/* Option system based mime backend */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include "elinks.h"
#include "config/options.h"
#include "intl/libintl.h"
#include "main/module.h"
#include "mime/backend/common.h"
#include "mime/backend/default.h"
#include "mime/mime.h"
#include "osdep/osdep.h" /* For get_system_str() */
#include "terminal/terminal.h"
#include "util/conv.h"
#include "util/memory.h"
#include "util/string.h"
bug 764: Initialize the right member of union option_value INIT_OPTION used to initialize union option_value at compile time by casting the default value to LIST_OF(struct option) *, which is the type of the first member. On sparc64 and other big-endian systems where sizeof(int) < sizeof(struct list_head *), this tended to leave option->value.number as zero, thus messing up OPT_INT and OPT_BOOL at least. OPT_LONG however tended to work right. This would be easy to fix with C99 designated initializers, but doc/hacking.txt says ELinks must be kept C89 compatible. Another solution would be to make register_options() read the value from option->value.tree (the first member), cast it back to the right type, and write it to the appropriate member; but that would still require somewhat dubious conversions between integers, data pointers, and function pointers. So here's a rather more invasive solution. Add struct option_init, which is somewhat similar to struct option but has non-overlapping members for different types of values, to ensure nothing is lost in compile-time conversions. Move unsigned char *path from struct option_info to struct option_init, and replace struct option_info with a union that contains struct option_init and struct option. Now, this union can be initialized with no portability problems, and register_options() then moves the values from struct option_init to their final places in struct option. In my x86 ELinks build with plenty of options configured in, this change bloated the text section by 340 bytes but compressed the data section by 2784 bytes, presumably because union option_info is a pointer smaller than struct option_info was. (cherry picked from elinks-0.12 commit e5f6592ee20780a61f70feeb1f9e17631b9c5835) Conflicts: src/protocol/fsp/fsp.c: All options had been removed in 0.13.GIT. src/protocol/smb/smb2.c: Ditto.
2009-08-15 19:39:07 +00:00
static union option_info default_mime_options[] = {
INIT_OPT_TREE("mime", N_("MIME type associations"),
"type", OPT_AUTOCREATE,
N_("Handler <-> MIME type association. The first sub-tree is "
"the MIME class while the second sub-tree is the MIME type "
"(ie. image/gif handler will reside at mime.type.image.gif). "
"Each MIME type option should contain (case-sensitive) name "
"of the MIME handler (its properties are stored at "
"mime.handler.<name>).")),
INIT_OPT_TREE("mime.type", NULL,
"_template_", OPT_AUTOCREATE,
N_("Handler matching this MIME-type class "
"('*' is used here in place of '.').")),
INIT_OPT_STRING("mime.type._template_", NULL,
"_template_", OPT_ZERO, "",
N_("Handler matching this MIME-type name "
"('*' is used here in place of '.').")),
INIT_OPT_TREE("mime", N_("File type handlers"),
"handler", OPT_AUTOCREATE,
N_("A file type handler is a set of information about how to "
"use an external program to view a file. It is possible to "
"refer to it for several MIME types -- e.g., you can define "
"an 'image' handler to which mime.type.image.png, "
"mime.type.image.jpeg, and so on will refer; or one might "
"define a handler for a more specific type of file -- e.g., "
"PDF files. Note you must define both a MIME handler "
"and a MIME type association for it to work.")),
INIT_OPT_TREE("mime.handler", NULL,
"_template_", OPT_AUTOCREATE,
N_("Description of this handler.")),
INIT_OPT_TREE("mime.handler._template_", NULL,
"_template_", OPT_ZERO,
N_("System-specific handler description "
"(ie. unix, unix-xwin, ...).")),
INIT_OPT_BOOL("mime.handler._template_._template_", N_("Ask before opening"),
"ask", OPT_ZERO, 1,
N_("Ask before opening.")),
INIT_OPT_BOOL("mime.handler._template_._template_", N_("Block terminal"),
"block", OPT_ZERO, 1,
N_("Block the terminal when the handler is running.")),
INIT_OPT_STRING("mime.handler._template_._template_", N_("Program"),
"program", OPT_ZERO, "",
/* xgettext:no-c-format */
N_("External viewer for this file type. "
"'%f' in this string will be substituted by a file name, "
"'%u' by its uri. "
"Do _not_ put single- or double-quotes around the % sign.")),
INIT_OPT_TREE("mime", N_("File extension associations"),
"extension", OPT_AUTOCREATE,
N_("Extension <-> MIME type association.")),
INIT_OPT_STRING("mime.extension", NULL,
"_template_", OPT_ZERO, "",
N_("MIME-type matching this file extension "
"('*' is used here in place of '.').")),
#define INIT_OPT_MIME_EXTENSION(extension, type) \
INIT_OPT_STRING("mime.extension", NULL, extension, OPT_ZERO, type, NULL)
INIT_OPT_MIME_EXTENSION("gif", "image/gif"),
INIT_OPT_MIME_EXTENSION("jpg", "image/jpg"),
INIT_OPT_MIME_EXTENSION("jpeg", "image/jpeg"),
INIT_OPT_MIME_EXTENSION("png", "image/png"),
INIT_OPT_MIME_EXTENSION("txt", "text/plain"),
INIT_OPT_MIME_EXTENSION("htm", "text/html"),
INIT_OPT_MIME_EXTENSION("html", "text/html"),
INIT_OPT_MIME_EXTENSION("gmi", "text/gemini"),
#ifdef CONFIG_BITTORRENT
INIT_OPT_MIME_EXTENSION("torrent", "application/x-bittorrent"),
#endif
#ifdef CONFIG_DOM
INIT_OPT_MIME_EXTENSION("rss", "application/rss+xml"),
INIT_OPT_MIME_EXTENSION("xbel", "application/xbel+xml"),
INIT_OPT_MIME_EXTENSION("sgml", "application/docbook+xml"),
#endif
NULL_OPTION_INFO,
};
static char *
get_content_type_default(char *extension)
{
struct option *opt_tree;
struct option *opt;
char *extend = extension + strlen(extension) - 1;
if (extend < extension) return NULL;
opt_tree = get_opt_rec_real(config_options, "mime.extension");
assert(opt_tree);
foreach (opt, *opt_tree->value.tree) {
2022-01-29 16:49:38 +00:00
const char *namepos = opt->name + strlen(opt->name) - 1;
char *extpos = extend;
/* Match the longest possible part of URL.. */
#define star2dot(achar) ((achar) == '*' ? '.' : (achar))
while (extension <= extpos && opt->name <= namepos
&& *extpos == star2dot(*namepos)) {
extpos--;
namepos--;
}
#undef star2dot
/* If we matched whole extension and it is really an
* extension.. */
if (namepos < opt->name
&& (extpos < extension || *extpos == '.'))
return stracpy(opt->value.string);
}
return NULL;
}
static struct option *
get_mime_type_option(char *type)
{
struct option *opt;
struct string name;
opt = get_opt_rec_real(config_options, "mime.type");
if (!opt) return NULL;
if (!init_string(&name)) return NULL;
if (add_optname_to_string(&name, type, strlen(type))) {
/* Search for end of the base type. */
char *pos = strchr(name.source, '/');
if (pos) {
*pos = '.';
opt = get_opt_rec_real(opt, name.source);
done_string(&name);
return opt;
}
}
done_string(&name);
return NULL;
}
static inline struct option *
get_mime_handler_option(struct option *type_opt, int xwin)
{
struct option *handler_opt;
assert(type_opt);
handler_opt = get_opt_rec_real(config_options, "mime.handler");
if (!handler_opt) return NULL;
handler_opt = get_opt_rec_real(handler_opt, type_opt->value.string);
if (!handler_opt) return NULL;
return get_opt_rec_real(handler_opt, get_system_str(xwin));
}
static struct mime_handler *
get_mime_handler_default(char *type, int have_x)
{
struct option *type_opt = get_mime_type_option(type);
struct option *handler_opt;
if (!type_opt) return NULL;
handler_opt = get_mime_handler_option(type_opt, have_x);
if (!handler_opt) return NULL;
return init_mime_handler(get_opt_str_tree(handler_opt, "program", NULL),
type_opt->value.string,
default_mime_module.name,
get_opt_bool_tree(handler_opt, "ask", NULL),
get_opt_bool_tree(handler_opt, "block", NULL));
}
2007-02-04 11:34:48 +00:00
const struct mime_backend default_mime_backend = {
/* get_content_type: */ get_content_type_default,
/* get_mime_handler: */ get_mime_handler_default,
};
struct module default_mime_module = struct_module(
/* name: */ N_("Option system"),
/* options: */ default_mime_options,
/* hooks: */ NULL,
/* submodules: */ NULL,
/* data: */ NULL,
/* init: */ NULL,
/* done: */ NULL
);