2012-11-18 18:23:22 +08:00
|
|
|
/** \file mimedb.c
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
mimedb is a program for checking the mimetype, description and
|
|
|
|
default action associated with a file or mimetype. It uses the
|
2012-11-18 18:23:22 +08:00
|
|
|
xdgmime library written by the fine folks at freedesktop.org. There does
|
2005-09-20 21:26:39 +08:00
|
|
|
not seem to be any standard way for the user to change the preferred
|
|
|
|
application yet.
|
|
|
|
|
|
|
|
The first implementation of mimedb used xml_grep to parse the xml
|
|
|
|
file for the mime entry to determine the description. This was abandoned
|
|
|
|
because of the performance implications of parsing xml. The current
|
|
|
|
version only does a simple string search, which is much, much
|
|
|
|
faster but it might fall on it's head.
|
|
|
|
|
2008-01-13 03:18:48 +08:00
|
|
|
This code is Copyright 2005-2008 Axel Liljencrantz.
|
2005-09-20 21:26:39 +08:00
|
|
|
It is released under the GPL.
|
|
|
|
|
|
|
|
The xdgmime library is dual licensed under LGPL/artistic
|
|
|
|
license. Read the source code of the library for more information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <errno.h>
|
2006-01-23 23:45:00 +08:00
|
|
|
#include <regex.h>
|
|
|
|
#include <locale.h>
|
2012-02-08 17:22:08 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2012-02-08 17:55:35 +08:00
|
|
|
#include <map>
|
|
|
|
|
2006-01-23 23:45:00 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
2006-01-23 23:45:00 +08:00
|
|
|
#if HAVE_LIBINTL_H
|
|
|
|
#include <libintl.h>
|
|
|
|
#endif
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "xdgmime.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
#include "fallback.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "util.h"
|
2006-11-18 00:24:38 +08:00
|
|
|
#include "print_help.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-08 17:55:35 +08:00
|
|
|
typedef std::vector<std::string> string_list_t;
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Location of the applications .desktop file, relative to a base mime directory
|
|
|
|
*/
|
|
|
|
#define APPLICATIONS_DIR "applications/"
|
|
|
|
|
|
|
|
/**
|
|
|
|
Location of the mime xml database, relative to a base mime directory
|
|
|
|
*/
|
|
|
|
#define MIME_DIR "mime/"
|
|
|
|
/**
|
|
|
|
Filename suffix for XML files
|
|
|
|
*/
|
|
|
|
#define MIME_SUFFIX ".xml"
|
|
|
|
|
|
|
|
/**
|
2006-01-23 23:45:00 +08:00
|
|
|
Start tag for langauge-specific comment
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2007-05-24 14:37:28 +08:00
|
|
|
#define START_TAG "<comment( +xml:lang *= *(\"%s\"|'%s'))? *>"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
End tab for comment
|
|
|
|
*/
|
2006-01-23 23:45:00 +08:00
|
|
|
#define STOP_TAG "</comment *>"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
File contains cached list of mime actions
|
|
|
|
*/
|
|
|
|
#define DESKTOP_DEFAULT "applications/defaults.list"
|
|
|
|
|
2006-01-23 23:45:00 +08:00
|
|
|
/**
|
|
|
|
Size for temporary string buffer used to make a regex for language
|
|
|
|
specific descriptions
|
|
|
|
*/
|
|
|
|
#define BUFF_SIZE 1024
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
2006-01-23 23:45:00 +08:00
|
|
|
Program name
|
|
|
|
*/
|
|
|
|
#define MIMEDB "mimedb"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-05-26 22:55:51 +08:00
|
|
|
/**
|
|
|
|
Getopt short switches for mimedb
|
|
|
|
*/
|
|
|
|
#define GETOPT_STRING "tfimdalhv"
|
|
|
|
|
2009-02-23 04:28:52 +08:00
|
|
|
/**
|
|
|
|
Error message if system call goes wrong.
|
|
|
|
*/
|
2012-11-18 18:23:22 +08:00
|
|
|
#define ERROR_SYSTEM "%s: Could not execute command \"%s\"\n"
|
2009-02-23 04:28:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Exit code if system call goes wrong.
|
|
|
|
*/
|
|
|
|
#define STATUS_ERROR_SYSTEM 1
|
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
All types of input and output possible
|
|
|
|
*/
|
|
|
|
enum
|
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
FILEDATA,
|
|
|
|
FILENAME,
|
|
|
|
MIMETYPE,
|
|
|
|
DESCRIPTION,
|
|
|
|
ACTION,
|
|
|
|
LAUNCH
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Regular expression variable used to find start tag of description
|
|
|
|
*/
|
|
|
|
static regex_t *start_re=0;
|
|
|
|
/**
|
|
|
|
Regular expression variable used to find end tag of description
|
|
|
|
*/
|
|
|
|
static regex_t *stop_re=0;
|
2006-01-23 23:45:00 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Error flag. Non-zero if something bad happened.
|
|
|
|
*/
|
|
|
|
static int error = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
String of characters to send to system() to launch a file
|
|
|
|
*/
|
|
|
|
static char *launch_buff=0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Length of the launch_buff buffer
|
|
|
|
*/
|
|
|
|
static int launch_len=0;
|
|
|
|
/**
|
|
|
|
Current position in the launch_buff buffer
|
|
|
|
*/
|
|
|
|
static int launch_pos=0;
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
gettext alias
|
|
|
|
*/
|
2006-11-29 22:18:22 +08:00
|
|
|
#ifdef USE_GETTEXT
|
2006-01-23 23:45:00 +08:00
|
|
|
#define _(string) gettext(string)
|
2006-11-29 22:18:22 +08:00
|
|
|
#else
|
|
|
|
#define _(string) (string)
|
|
|
|
#endif
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Call malloc, set error flag and print message on failure
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
void *my_malloc(size_t s)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
void *res = malloc(s);
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
error=1;
|
|
|
|
fprintf(stderr, _("%s: Out of memory\n"), MIMEDB);
|
|
|
|
}
|
|
|
|
return res;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Duplicate string, set error flag and print message on failure
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
char *my_strdup(const char *s)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
char *res = strdup(s);
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
error=1;
|
|
|
|
fprintf(stderr, _("%s: Out of memory\n"), MIMEDB);
|
|
|
|
}
|
|
|
|
return res;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Search the file \c filename for the first line starting with \c
|
|
|
|
match, which is returned in a newly allocated string.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static const char * search_ini(const char *filename, const char *match)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-03-02 16:27:40 +08:00
|
|
|
/* OK to not use CLO_EXEC here because mimedb is single threaded */
|
2012-11-19 08:30:30 +08:00
|
|
|
FILE *f = fopen(filename, "r");
|
|
|
|
char buf[4096];
|
|
|
|
int len=strlen(match);
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
if (!f)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
perror("fopen");
|
2012-11-18 18:23:22 +08:00
|
|
|
error=1;
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
while (!done)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!fgets(buf, 4096, f))
|
|
|
|
{
|
|
|
|
if (!feof(f))
|
|
|
|
{
|
|
|
|
perror("fgets");
|
|
|
|
error=1;
|
|
|
|
}
|
|
|
|
buf[0]=0;
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
else if (strncmp(buf, match, len) == 0 && buf[len] == '=')
|
|
|
|
{
|
|
|
|
done=1;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
fclose(f);
|
|
|
|
if (buf[0])
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
char *res=strdup(buf);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
if (res[strlen(res)-1]=='\n')
|
|
|
|
res[strlen(res)-1]='\0';
|
|
|
|
}
|
|
|
|
return res;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
else
|
|
|
|
return (char *)0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Test if the specified file exists. If it does not, also try
|
|
|
|
replacing dashes with slashes in \c in.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static char *file_exists(const char *dir, const char *in)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
int dir_len = strlen(dir);
|
|
|
|
int need_sep = dir[dir_len - 1] != '/';
|
|
|
|
char *filename = (char *)my_malloc(dir_len + need_sep + strlen(in) + 1);
|
2011-12-27 11:18:46 +08:00
|
|
|
char *replaceme;
|
2012-11-19 08:30:30 +08:00
|
|
|
struct stat buf;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
// fprintf( stderr, "Check %s%s\n", dir, in );
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!filename)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strcpy(filename, dir);
|
|
|
|
if (need_sep)
|
|
|
|
filename[dir_len++] = '/';
|
|
|
|
strcpy(filename + dir_len, in);
|
|
|
|
|
|
|
|
if (!stat(filename, &buf))
|
|
|
|
return filename;
|
|
|
|
|
|
|
|
free(filename);
|
|
|
|
|
|
|
|
/*
|
|
|
|
DOH! File does not exist. But all is not lost. KDE sometimes uses
|
|
|
|
a slash in the name as a directory separator. We try to replace
|
|
|
|
a dash with a slash and try again.
|
|
|
|
*/
|
|
|
|
replaceme = const_cast<char*>(strchr(in, '-'));
|
|
|
|
if (replaceme)
|
|
|
|
{
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
*replaceme = '/';
|
|
|
|
res = file_exists(dir, in);
|
|
|
|
*replaceme = '-';
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
OK, no more slashes left. We really are screwed. Nothing to to
|
|
|
|
but admit defeat and go home.
|
|
|
|
*/
|
2012-11-18 18:23:22 +08:00
|
|
|
return 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Try to find the specified file in any of the possible directories
|
|
|
|
where mime files can be located. This code is shamelessly stolen
|
|
|
|
from xdg_run_command_on_dirs.
|
2010-11-27 02:46:54 +08:00
|
|
|
|
|
|
|
\param list Full file paths will be appended to this list.
|
|
|
|
\param f The relative filename search for the the data directories.
|
|
|
|
\param all If zero, then stop after the first filename.
|
|
|
|
\return The number of filenames added to the list.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static int append_filenames(string_list_t &list, const char *f, int all)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t prev_count = list.size();
|
|
|
|
char *result;
|
|
|
|
const char *xdg_data_home;
|
|
|
|
const char *xdg_data_dirs;
|
|
|
|
const char *ptr;
|
|
|
|
|
|
|
|
xdg_data_home = getenv("XDG_DATA_HOME");
|
|
|
|
if (xdg_data_home)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
result = file_exists(xdg_data_home, f);
|
|
|
|
if (result)
|
|
|
|
{
|
2012-02-08 17:22:08 +08:00
|
|
|
list.push_back(result);
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!all)
|
|
|
|
return 1;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
else
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *home;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
home = getenv("HOME");
|
|
|
|
if (home != NULL)
|
|
|
|
{
|
|
|
|
char *guessed_xdg_home;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
guessed_xdg_home = (char *)my_malloc(strlen(home) + strlen("/.local/share") + 1);
|
|
|
|
if (!guessed_xdg_home)
|
|
|
|
return 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
strcpy(guessed_xdg_home, home);
|
|
|
|
strcat(guessed_xdg_home, "/.local/share");
|
|
|
|
result = file_exists(guessed_xdg_home, f);
|
|
|
|
free(guessed_xdg_home);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (result)
|
|
|
|
{
|
2012-02-08 17:22:08 +08:00
|
|
|
list.push_back(result);
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!all)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
xdg_data_dirs = getenv("XDG_DATA_DIRS");
|
|
|
|
if (xdg_data_dirs == NULL)
|
|
|
|
xdg_data_dirs = "/usr/local/share:/usr/share";
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
ptr = xdg_data_dirs;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
while (*ptr != '\000')
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *end_ptr;
|
|
|
|
char *dir;
|
|
|
|
int len;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
end_ptr = ptr;
|
|
|
|
while (*end_ptr != ':' && *end_ptr != '\000')
|
|
|
|
end_ptr ++;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (end_ptr == ptr)
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
continue;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
len = end_ptr - ptr;
|
2012-11-19 08:30:30 +08:00
|
|
|
dir = (char *)my_malloc(len + 1);
|
|
|
|
if (!dir)
|
|
|
|
return 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
strncpy(dir, ptr, len);
|
|
|
|
dir[len] = '\0';
|
|
|
|
result = file_exists(dir, f);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
free(dir);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (result)
|
|
|
|
{
|
2012-02-08 17:22:08 +08:00
|
|
|
list.push_back(result);
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!all)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
ptr = end_ptr;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return list.size() - prev_count;
|
2010-11-27 02:46:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-08 17:22:08 +08:00
|
|
|
Find at most one file relative to the XDG data directories; returns the empty string on failure
|
2010-11-27 02:46:54 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static std::string get_filename(char *f)
|
2010-11-27 02:46:54 +08:00
|
|
|
{
|
2012-02-08 17:55:35 +08:00
|
|
|
string_list_t list;
|
2012-02-08 17:22:08 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
append_filenames(list, f, 0);
|
|
|
|
if (list.empty())
|
|
|
|
{
|
2012-02-08 17:22:08 +08:00
|
|
|
return "";
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-08 17:22:08 +08:00
|
|
|
return list.back();
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Remove excessive whitespace from string. Replaces arbitrary sequence
|
|
|
|
of whitespace with a single space. Also removes any leading and
|
|
|
|
trailing whitespace
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static char *munge(char *in)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
char *out = (char *)my_malloc(strlen(in)+1);
|
|
|
|
char *p=out;
|
|
|
|
int had_whitespace = 0;
|
|
|
|
int printed = 0;
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
while (1)
|
|
|
|
{
|
2012-11-18 18:23:22 +08:00
|
|
|
// fprintf( stderr, "%c\n", *in );
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
switch (*in)
|
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
case ' ':
|
|
|
|
case '\n':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
2012-11-19 08:30:30 +08:00
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
had_whitespace = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '\0':
|
|
|
|
*p = '\0';
|
|
|
|
return out;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
if (printed && had_whitespace)
|
|
|
|
{
|
|
|
|
*(p++)=' ';
|
|
|
|
}
|
|
|
|
printed=1;
|
|
|
|
had_whitespace=0;
|
|
|
|
*(p++)=*in;
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
in++;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
fprintf(stderr, _("%s: Unknown error in munge()\n"), MIMEDB);
|
|
|
|
error=1;
|
|
|
|
return 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2006-01-23 23:45:00 +08:00
|
|
|
/**
|
|
|
|
Return a regular expression that matches all strings specifying the current locale
|
|
|
|
*/
|
|
|
|
static char *get_lang_re()
|
|
|
|
{
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
static char buff[BUFF_SIZE];
|
|
|
|
const char *lang = setlocale(LC_MESSAGES, 0);
|
|
|
|
int close=0;
|
|
|
|
char *out=buff;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if ((1+strlen(lang)*4) >= BUFF_SIZE)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: Locale string too long\n"), MIMEDB);
|
|
|
|
error = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
for (; *lang; lang++)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
switch (*lang)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
case '@':
|
|
|
|
case '.':
|
|
|
|
case '_':
|
|
|
|
if (close)
|
|
|
|
{
|
|
|
|
*out++ = ')';
|
|
|
|
*out++ = '?';
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
close=1;
|
|
|
|
*out++ = '(';
|
|
|
|
*out++ = *lang;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
default:
|
|
|
|
*out++ = *lang;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (close)
|
|
|
|
{
|
|
|
|
*out++ = ')';
|
|
|
|
*out++ = '?';
|
|
|
|
}
|
|
|
|
*out++=0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return buff;
|
2006-01-23 23:45:00 +08:00
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
2012-11-18 18:23:22 +08:00
|
|
|
Get description for a specified mimetype.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static char *get_description(const char *mimetype)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
char *fn_part;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
std::string fn;
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
char *contents;
|
|
|
|
char *start=0, *stop=0, *best_start=0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!start_re)
|
|
|
|
{
|
|
|
|
char *lang;
|
|
|
|
char buff[BUFF_SIZE];
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
lang = get_lang_re();
|
|
|
|
if (!lang)
|
|
|
|
return 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
snprintf(buff, BUFF_SIZE, START_TAG, lang, lang);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
start_re = (regex_t *)my_malloc(sizeof(regex_t));
|
|
|
|
stop_re = (regex_t *)my_malloc(sizeof(regex_t));
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2007-05-16 03:37:51 +08:00
|
|
|
int reg_status;
|
2012-11-19 08:30:30 +08:00
|
|
|
if ((reg_status = regcomp(start_re, buff, REG_EXTENDED)))
|
2007-05-16 03:37:51 +08:00
|
|
|
{
|
|
|
|
char regerrbuf[BUFF_SIZE];
|
|
|
|
regerror(reg_status, start_re, regerrbuf, BUFF_SIZE);
|
2012-11-19 08:30:30 +08:00
|
|
|
fprintf(stderr, _("%s: Could not compile regular expressions %s with error %s\n"), MIMEDB, buff, regerrbuf);
|
|
|
|
error=1;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2007-05-16 03:37:51 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
else if ((reg_status = regcomp(stop_re, STOP_TAG, REG_EXTENDED)))
|
|
|
|
{
|
2007-05-16 03:37:51 +08:00
|
|
|
char regerrbuf[BUFF_SIZE];
|
|
|
|
regerror(reg_status, stop_re, regerrbuf, BUFF_SIZE);
|
2012-11-19 08:30:30 +08:00
|
|
|
fprintf(stderr, _("%s: Could not compile regular expressions %s with error %s\n"), MIMEDB, buff, regerrbuf);
|
|
|
|
error=1;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2007-05-16 03:37:51 +08:00
|
|
|
}
|
2006-01-23 23:45:00 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (error)
|
2007-05-16 03:37:51 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
free(start_re);
|
|
|
|
free(stop_re);
|
|
|
|
start_re = stop_re = 0;
|
2006-01-23 23:45:00 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
2007-05-16 03:37:51 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
fn_part = (char *)my_malloc(strlen(MIME_DIR) + strlen(mimetype) + strlen(MIME_SUFFIX) + 1);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!fn_part)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
strcpy(fn_part, MIME_DIR);
|
|
|
|
strcat(fn_part, mimetype);
|
|
|
|
strcat(fn_part, MIME_SUFFIX);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
fn = get_filename(fn_part); //malloc( strlen(MIME_DIR) +strlen( MIME_SUFFIX)+ strlen( mimetype ) + 1 );
|
|
|
|
free(fn_part);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (fn.empty())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-03-02 16:27:40 +08:00
|
|
|
/* OK to not use CLO_EXEC here because mimedb is single threaded */
|
2012-11-19 08:30:30 +08:00
|
|
|
fd = open(fn.c_str(), O_RDONLY);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
// fprintf( stderr, "%s\n", fn );
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
perror("open");
|
|
|
|
error=1;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (stat(fn.c_str(), &st))
|
|
|
|
{
|
|
|
|
perror("stat");
|
|
|
|
error=1;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
contents = (char *)my_malloc(st.st_size + 1);
|
|
|
|
if (!contents)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (read(fd, contents, st.st_size) != st.st_size)
|
|
|
|
{
|
|
|
|
perror("read");
|
|
|
|
error=1;
|
2012-03-26 16:21:10 +08:00
|
|
|
free((void *)contents);
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
/*
|
|
|
|
Don't need to check exit status of close on read-only file descriptors
|
|
|
|
*/
|
|
|
|
close(fd);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
contents[st.st_size]=0;
|
|
|
|
regmatch_t match[1];
|
|
|
|
int w = -1;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
start=contents;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
/*
|
|
|
|
On multiple matches, use the longest match, should be a pretty
|
|
|
|
good heuristic for best match...
|
|
|
|
*/
|
|
|
|
while (!regexec(start_re, start, 1, match, 0))
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
int new_w = match[0].rm_eo - match[0].rm_so;
|
|
|
|
start += match[0].rm_eo;
|
|
|
|
|
|
|
|
if (new_w > w)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
New match is for a longer match then the previous
|
|
|
|
match, so we use the new match
|
|
|
|
*/
|
|
|
|
w=new_w;
|
|
|
|
best_start = start;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (w != -1)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
start = best_start;
|
|
|
|
if (!regexec(stop_re, start, 1, match, 0))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
We've found the beginning and the end of a suitable description
|
|
|
|
*/
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
stop = start + match[0].rm_so;
|
|
|
|
*stop = '\0';
|
|
|
|
res = munge(start);
|
|
|
|
free(contents);
|
|
|
|
return res;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
free(contents);
|
|
|
|
fprintf(stderr, _("%s: No description for type %s\n"), MIMEDB, mimetype);
|
|
|
|
error=1;
|
|
|
|
return 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-11-18 18:23:22 +08:00
|
|
|
Get default action for a specified mimetype.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static char *get_action(const char *mimetype)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
char *res=0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *launcher, *end;
|
|
|
|
string_list_t mime_filenames;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *launcher_str = NULL;
|
|
|
|
const char *launcher_command_str, *launcher_command;
|
|
|
|
char *launcher_full;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!append_filenames(mime_filenames, DESKTOP_DEFAULT, 1))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
for (size_t i = 0; i < mime_filenames.size(); i++)
|
|
|
|
{
|
|
|
|
launcher_str = search_ini(mime_filenames.at(i).c_str(), mimetype);
|
|
|
|
if (launcher_str)
|
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!launcher_str)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This type does not have a launcher. Try the supertype!
|
|
|
|
*/
|
2012-11-18 18:23:22 +08:00
|
|
|
// fprintf( stderr, "mimedb: %s does not have launcher, try supertype\n", mimetype );
|
2012-11-19 08:30:30 +08:00
|
|
|
const char ** parents = xdg_mime_get_mime_parents(mimetype);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const char **p;
|
|
|
|
if (parents)
|
|
|
|
{
|
|
|
|
for (p=parents; *p; p++)
|
|
|
|
{
|
|
|
|
char *a = get_action(*p);
|
|
|
|
if (a != 0)
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Just in case subclassing doesn't work, (It doesn't on Fedora
|
|
|
|
Core 3) we also test some common subclassings.
|
|
|
|
*/
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (strncmp(mimetype, "text/plain", 10) != 0 && strncmp(mimetype, "text/", 5) == 0)
|
|
|
|
return get_action("text/plain");
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
// fprintf( stderr, "WOOT %s\n", launcher_str );
|
2012-11-19 08:30:30 +08:00
|
|
|
launcher = const_cast<char*>(strchr(launcher_str, '='));
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!launcher)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: Could not parse launcher string '%s'\n"), MIMEDB, launcher_str);
|
|
|
|
error=1;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
/* Skip the = */
|
|
|
|
launcher++;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
/* Make one we can change */
|
|
|
|
std::string mut_launcher = launcher;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
/* Only use first launcher */
|
|
|
|
end = strchr(launcher, ';');
|
|
|
|
if (end)
|
|
|
|
mut_launcher.resize(end - launcher);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
launcher_full = (char *)my_malloc(mut_launcher.size() + strlen(APPLICATIONS_DIR)+1);
|
|
|
|
if (!launcher_full)
|
|
|
|
{
|
|
|
|
free((void *)launcher_str);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
strcpy(launcher_full, APPLICATIONS_DIR);
|
|
|
|
strcat(launcher_full, mut_launcher.c_str());
|
|
|
|
free((void *)launcher_str);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
std::string launcher_filename = get_filename(launcher_full);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
free(launcher_full);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
launcher_command_str = search_ini(launcher_filename.c_str(), "Exec");
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!launcher_command_str)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: Default launcher '%s' does not specify how to start\n"), MIMEDB,
|
|
|
|
launcher_filename.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
launcher_command = strchr(launcher_command_str, '=');
|
|
|
|
launcher_command++;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
res = my_strdup(launcher_command);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
free((void *)launcher_command_str);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return res;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Helper function for launch. Write the specified byte to the string we will execute
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static void writer(char c)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
if (launch_len == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (launch_len <= launch_pos)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
int new_len = launch_len?2*launch_len:256;
|
|
|
|
char *new_buff = (char *)realloc(launch_buff, new_len);
|
|
|
|
if (!new_buff)
|
|
|
|
{
|
|
|
|
free(launch_buff);
|
|
|
|
launch_len = -1;
|
|
|
|
error=1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
launch_buff = new_buff;
|
|
|
|
launch_len = new_len;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
launch_buff[launch_pos++]=c;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Write out the specified byte in hex
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static void writer_hex(int num)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
int a, b;
|
|
|
|
a = num /16;
|
|
|
|
b = num %16;
|
2007-04-17 14:29:32 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
writer(a>9?('A'+a-10):('0'+a));
|
|
|
|
writer(b>9?('A'+b-10):('0'+b));
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return current directory in newly allocated string
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static char *my_getcwd()
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t size = 100;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
char *buffer = (char *) malloc(size);
|
|
|
|
if (getcwd(buffer, size) == buffer)
|
|
|
|
return buffer;
|
|
|
|
free(buffer);
|
|
|
|
if (errno != ERANGE)
|
|
|
|
return 0;
|
|
|
|
size *= 2;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return absolute filename of specified file
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static const char *get_fullfile(const char *file)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *fullfile;
|
|
|
|
|
|
|
|
if (file[0] == '/')
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
fullfile = file;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *cwd = my_getcwd();
|
|
|
|
if (!cwd)
|
|
|
|
{
|
|
|
|
error = 1;
|
|
|
|
perror("getcwd");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
int l = strlen(cwd);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
char *tmp = (char *)my_malloc(l + strlen(file)+2);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
free(cwd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strcpy(tmp, cwd);
|
|
|
|
if (cwd[l-1] != '/')
|
|
|
|
strcat(tmp, "/");
|
|
|
|
strcat(tmp, file);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
free(cwd);
|
2012-02-08 17:55:35 +08:00
|
|
|
fullfile = tmp;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
return fullfile;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Write specified file as an URL
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static void write_url(const char *file)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *fullfile = get_fullfile(file);
|
|
|
|
const char *str = fullfile;
|
|
|
|
|
|
|
|
if (str == 0)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
launch_len = -1;
|
|
|
|
return;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
writer('f');
|
|
|
|
writer('i');
|
|
|
|
writer('l');
|
|
|
|
writer('e');
|
|
|
|
writer(':');
|
|
|
|
writer('/');
|
|
|
|
writer('/');
|
|
|
|
while (*str)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
if (((*str >= 'a') && (*str <='z')) ||
|
|
|
|
((*str >= 'A') && (*str <='Z')) ||
|
|
|
|
((*str >= '0') && (*str <='9')) ||
|
|
|
|
(strchr("-_.~/",*str) != 0))
|
|
|
|
{
|
|
|
|
writer(*str);
|
|
|
|
}
|
|
|
|
else if (strchr("()?&=",*str) != 0)
|
|
|
|
{
|
|
|
|
writer('\\');
|
|
|
|
writer(*str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writer('%');
|
|
|
|
writer_hex((unsigned char)*str);
|
|
|
|
}
|
|
|
|
str++;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
if (fullfile != file)
|
|
|
|
free((void *)fullfile);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Write specified file
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static void write_file(const char *file, int print_path)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *fullfile;
|
|
|
|
const char *str;
|
|
|
|
if (print_path)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
fullfile = get_fullfile(file);
|
|
|
|
str = fullfile;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *tmp = my_strdup(file);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
str = basename(tmp);
|
2012-02-08 17:55:35 +08:00
|
|
|
fullfile = tmp;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
error = 1;
|
|
|
|
return;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
while (*str)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
switch (*str)
|
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
case ')':
|
|
|
|
case '(':
|
|
|
|
case '-':
|
|
|
|
case '#':
|
|
|
|
case '$':
|
|
|
|
case '}':
|
|
|
|
case '{':
|
|
|
|
case ']':
|
|
|
|
case '[':
|
|
|
|
case '*':
|
|
|
|
case '?':
|
|
|
|
case ' ':
|
|
|
|
case '|':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '^':
|
|
|
|
case '&':
|
|
|
|
case '\\':
|
|
|
|
case '`':
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
|
|
|
writer('\\');
|
|
|
|
writer(*str);
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case '\n':
|
|
|
|
writer('\\');
|
|
|
|
writer('n');
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case '\r':
|
|
|
|
writer('\\');
|
|
|
|
writer('r');
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case '\t':
|
|
|
|
writer('\\');
|
|
|
|
writer('t');
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case '\b':
|
|
|
|
writer('\\');
|
|
|
|
writer('b');
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case '\v':
|
|
|
|
writer('\\');
|
|
|
|
writer('v');
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
default:
|
|
|
|
writer(*str);
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
str++;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (fullfile != file)
|
|
|
|
free((void *)fullfile);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-18 18:23:22 +08:00
|
|
|
Use the specified launch filter to launch all the files in the specified list.
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
\param filter the action to take
|
|
|
|
\param files the list of files for which to perform the action
|
|
|
|
\param fileno an internal value. Should always be set to zero.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
static void launch(char *filter, const string_list_t &files, size_t fileno)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
char *filter_org=filter;
|
|
|
|
int count=0;
|
|
|
|
int launch_again=0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (files.size() <= fileno)
|
|
|
|
return;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
launch_pos=0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
for (; *filter && !error; filter++)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
if (*filter == '%')
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
filter++;
|
|
|
|
switch (*filter)
|
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'u':
|
2012-11-19 08:30:30 +08:00
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
launch_again = 1;
|
|
|
|
write_url(files.at(fileno).c_str());
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'U':
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<files.size(); i++)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
writer(' ');
|
|
|
|
write_url(files.at(i).c_str());
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'f':
|
|
|
|
case 'n':
|
2012-11-19 08:30:30 +08:00
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
launch_again = 1;
|
|
|
|
write_file(files.at(fileno).c_str(), *filter == 'f');
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'F':
|
|
|
|
case 'N':
|
2012-11-19 08:30:30 +08:00
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
for (size_t i=0; i<files.size(); i++)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
writer(' ');
|
|
|
|
write_file(files.at(i).c_str(), *filter == 'F');
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'd':
|
2012-11-19 08:30:30 +08:00
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
const char *cpy = get_fullfile(files.at(fileno).c_str());
|
2012-11-19 08:30:30 +08:00
|
|
|
char *dir;
|
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
launch_again=1;
|
2012-11-19 08:30:30 +08:00
|
|
|
/*
|
|
|
|
We wish to modify this string, make sure it is only a copy
|
|
|
|
*/
|
2012-11-19 16:31:03 +08:00
|
|
|
if (cpy == files.at(fileno).c_str())
|
2012-11-19 08:30:30 +08:00
|
|
|
cpy = my_strdup(cpy);
|
|
|
|
|
|
|
|
if (cpy == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
dir=dirname((char *)cpy);
|
2012-11-19 08:30:30 +08:00
|
|
|
write_file(dir, 1);
|
|
|
|
free((void *)cpy);
|
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'D':
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<files.size(); i++)
|
|
|
|
{
|
|
|
|
const char *cpy = get_fullfile(files.at(i).c_str());
|
|
|
|
char *dir;
|
|
|
|
|
|
|
|
/*
|
|
|
|
We wish to modify this string, make sure it is only a copy
|
|
|
|
*/
|
|
|
|
if (cpy == files.at(i).c_str())
|
|
|
|
cpy = my_strdup(cpy);
|
|
|
|
|
|
|
|
if (cpy == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dir=dirname((char *)cpy);
|
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
writer(' ');
|
|
|
|
|
|
|
|
write_file(dir, 1);
|
|
|
|
free((void *)cpy);
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, _("%s: Unsupported switch '%c' in launch string '%s'\n"), MIMEDB, *filter, filter_org);
|
|
|
|
launch_len=0;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writer(*filter);
|
|
|
|
count++;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (error)
|
|
|
|
return;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
switch (launch_len)
|
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
case -1:
|
2012-11-19 08:30:30 +08:00
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
launch_len = 0;
|
|
|
|
fprintf(stderr, _("%s: Out of memory\n"), MIMEDB);
|
|
|
|
return;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
writer(' ');
|
|
|
|
writer('&');
|
|
|
|
writer('\0');
|
|
|
|
|
|
|
|
if (system(launch_buff) == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _(ERROR_SYSTEM), MIMEDB, launch_buff);
|
|
|
|
exit(STATUS_ERROR_SYSTEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
if (launch_again)
|
|
|
|
{
|
|
|
|
launch(filter_org, files, fileno+1);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Do locale specific init
|
|
|
|
*/
|
2006-01-23 23:45:00 +08:00
|
|
|
static void locale_init()
|
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE_NAME, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE_NAME);
|
2006-01-23 23:45:00 +08:00
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Main function. Parses options and calls helper function for any heavy lifting.
|
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
int main(int argc, char *argv[])
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
int input_type=FILEDATA;
|
|
|
|
int output_type=MIMETYPE;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *mimetype;
|
|
|
|
char *output=0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
int i;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-08 17:55:35 +08:00
|
|
|
typedef std::map<std::string, string_list_t> launch_hash_t;
|
|
|
|
launch_hash_t launch_hash;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
locale_init();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
/*
|
|
|
|
Parse options
|
|
|
|
*/
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
static struct option
|
|
|
|
long_options[] =
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
{
|
|
|
|
"input-file-data", no_argument, 0, 't'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
"input-filename", no_argument, 0, 'f'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
"input-mime", no_argument, 0, 'i'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
"output-mime", no_argument, 0, 'm'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
"output-description", no_argument, 0, 'd'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
"output-action", no_argument, 0, 'a'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
"help", no_argument, 0, 'h'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
"version", no_argument, 0, 'v'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
"launch", no_argument, 0, 'l'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
0, 0, 0, 0
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt_index = 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
int opt = getopt_long(argc,
|
|
|
|
argv,
|
|
|
|
GETOPT_STRING,
|
|
|
|
long_options,
|
|
|
|
&opt_index);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (opt == -1)
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
switch (opt)
|
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
case 0:
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 't':
|
|
|
|
input_type=FILEDATA;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'f':
|
|
|
|
input_type=FILENAME;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'i':
|
|
|
|
input_type=MIMETYPE;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'm':
|
|
|
|
output_type=MIMETYPE;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'd':
|
|
|
|
output_type=DESCRIPTION;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'a':
|
|
|
|
output_type=ACTION;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'l':
|
|
|
|
output_type=LAUNCH;
|
|
|
|
break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'h':
|
|
|
|
print_help(argv[0], 1);
|
|
|
|
exit(0);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case 'v':
|
|
|
|
printf(_("%s, version %s\n"), MIMEDB, PACKAGE_VERSION);
|
|
|
|
exit(0);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case '?':
|
|
|
|
return 1;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if ((output_type == LAUNCH)&&(input_type==MIMETYPE))
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
fprintf(stderr, _("%s: Can not launch a mimetype\n"), MIMEDB);
|
|
|
|
print_help(argv[0], 2);
|
|
|
|
exit(1);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-11-19 08:30:30 +08:00
|
|
|
Loop over all non option arguments and do the specified lookup
|
2012-11-18 18:23:22 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
//fprintf( stderr, "Input %d, output %d\n", input_type, output_type );
|
|
|
|
|
|
|
|
for (i = optind; (i < argc)&&(!error); i++)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
/* Convert from filename to mimetype, if needed */
|
|
|
|
if (input_type == FILENAME)
|
|
|
|
{
|
|
|
|
mimetype = xdg_mime_get_mime_type_from_file_name(argv[i]);
|
|
|
|
}
|
|
|
|
else if (input_type == FILEDATA)
|
|
|
|
{
|
|
|
|
mimetype = xdg_mime_get_mime_type_for_file(argv[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mimetype = xdg_mime_is_valid_mime_type(argv[i])?argv[i]:0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
mimetype = xdg_mime_unalias_mime_type(mimetype);
|
|
|
|
if (!mimetype)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: Could not parse mimetype from argument '%s'\n"), MIMEDB, argv[i]);
|
|
|
|
error=1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Convert from mimetype to whatever, if needed
|
|
|
|
*/
|
|
|
|
switch (output_type)
|
|
|
|
{
|
2012-11-19 16:31:03 +08:00
|
|
|
case MIMETYPE:
|
|
|
|
{
|
|
|
|
output = (char *)mimetype;
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
case DESCRIPTION:
|
|
|
|
{
|
|
|
|
output = get_description(mimetype);
|
|
|
|
if (!output)
|
|
|
|
output = strdup(_("Unknown"));
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ACTION:
|
|
|
|
{
|
|
|
|
output = get_action(mimetype);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LAUNCH:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
There may be more files using the same launcher, we
|
|
|
|
add them all up in little array_list_ts and launched
|
|
|
|
them together after all the arguments have been
|
|
|
|
parsed.
|
|
|
|
*/
|
|
|
|
output = 0;
|
|
|
|
string_list_t &l = launch_hash[mimetype];
|
|
|
|
l.push_back(argv[i]);
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
/*
|
2012-11-19 08:30:30 +08:00
|
|
|
Print the glorious result
|
2012-11-18 18:23:22 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
if (output)
|
|
|
|
{
|
|
|
|
printf("%s\n", output);
|
|
|
|
if (output != mimetype)
|
|
|
|
free(output);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
output = 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2012-11-18 18:23:22 +08:00
|
|
|
/*
|
2012-11-19 08:30:30 +08:00
|
|
|
Perform the actual launching
|
2012-11-18 18:23:22 +08:00
|
|
|
*/
|
2012-11-19 08:30:30 +08:00
|
|
|
if (output_type == LAUNCH && !error)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
for (launch_hash_t::iterator iter = launch_hash.begin(); iter != launch_hash.end(); ++iter)
|
|
|
|
{
|
|
|
|
const char *mimetype = iter->first.c_str();
|
|
|
|
string_list_t &files = iter->second;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
char *launcher = get_action(mimetype);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (launcher)
|
|
|
|
{
|
|
|
|
launch(launcher, files, 0);
|
|
|
|
free(launcher);
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (launch_buff)
|
|
|
|
free(launch_buff);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (start_re)
|
|
|
|
{
|
|
|
|
regfree(start_re);
|
|
|
|
regfree(stop_re);
|
|
|
|
free(start_re);
|
|
|
|
free(stop_re);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
xdg_mime_shutdown();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return error;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|