2015-02-12 16:34:06 -05:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
|
|
|
* Copyright (c) 2012 Sean Pringle <sean.pringle@gmail.com>
|
2015-12-31 18:27:00 -05:00
|
|
|
* Modified 2013-2016 Qball Cow <qball@gmpclient.org>
|
2015-02-12 16:34:06 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
2015-02-03 02:00:33 -05:00
|
|
|
#include <config.h>
|
2014-09-06 08:57:30 -04:00
|
|
|
#include <stdio.h>
|
2014-11-15 10:26:55 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-02-03 02:00:33 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-07-30 02:57:09 -04:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
2015-02-03 02:00:33 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/file.h>
|
2015-07-04 09:36:11 -04:00
|
|
|
#include <sys/stat.h>
|
2015-11-27 07:01:25 -05:00
|
|
|
#include <pwd.h>
|
2015-10-01 08:45:23 -04:00
|
|
|
#include <ctype.h>
|
2015-02-03 02:00:33 -05:00
|
|
|
#include "helper.h"
|
2016-01-07 10:01:56 -05:00
|
|
|
#include "settings.h"
|
2015-10-18 07:40:39 -04:00
|
|
|
#include "x11-helper.h"
|
2015-02-03 02:00:33 -05:00
|
|
|
#include "rofi.h"
|
2016-02-08 03:03:11 -05:00
|
|
|
#include "view.h"
|
2014-09-03 07:07:26 -04:00
|
|
|
|
2015-03-12 03:39:50 -04:00
|
|
|
static int stored_argc = 0;
|
|
|
|
static char **stored_argv = NULL;
|
2015-03-11 13:32:37 -04:00
|
|
|
|
|
|
|
void cmd_set_arguments ( int argc, char **argv )
|
|
|
|
{
|
|
|
|
stored_argc = argc;
|
|
|
|
stored_argv = argv;
|
|
|
|
}
|
|
|
|
|
2014-09-03 07:07:26 -04:00
|
|
|
/**
|
2015-03-05 14:26:52 -05:00
|
|
|
* @param info To Match information on.
|
2014-11-25 06:57:34 -05:00
|
|
|
* @param res The string being generated.
|
|
|
|
* @param data User data
|
|
|
|
*
|
|
|
|
* Replace the entries. This function gets called by g_regex_replace_eval.
|
|
|
|
*
|
|
|
|
* @returns TRUE to stop replacement, FALSE to continue
|
2014-09-03 07:07:26 -04:00
|
|
|
*/
|
2015-09-19 06:57:48 -04:00
|
|
|
static gboolean helper_eval_cb ( const GMatchInfo *info, GString *res, gpointer data )
|
2014-09-03 07:07:26 -04:00
|
|
|
{
|
2015-01-15 11:59:59 -05:00
|
|
|
gchar *match;
|
2014-11-25 06:57:34 -05:00
|
|
|
// Get the match
|
2014-09-03 07:07:26 -04:00
|
|
|
match = g_match_info_fetch ( info, 0 );
|
2014-11-25 06:57:34 -05:00
|
|
|
if ( match != NULL ) {
|
|
|
|
// Lookup the match, so we can replace it.
|
2015-01-15 11:59:59 -05:00
|
|
|
gchar *r = g_hash_table_lookup ( (GHashTable *) data, match );
|
2014-11-25 06:57:34 -05:00
|
|
|
if ( r != NULL ) {
|
|
|
|
// Append the replacement to the string.
|
|
|
|
g_string_append ( res, r );
|
|
|
|
}
|
|
|
|
// Free match.
|
2014-09-03 07:07:26 -04:00
|
|
|
g_free ( match );
|
|
|
|
}
|
2014-11-25 06:57:34 -05:00
|
|
|
// Continue replacement.
|
2014-09-03 07:07:26 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int helper_parse_setup ( char * string, char ***output, int *length, ... )
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GHashTable *h;
|
|
|
|
h = g_hash_table_new ( g_str_hash, g_str_equal );
|
2014-11-25 06:57:34 -05:00
|
|
|
// By default, we insert terminal and ssh-client
|
2014-09-03 07:07:26 -04:00
|
|
|
g_hash_table_insert ( h, "{terminal}", config.terminal_emulator );
|
|
|
|
g_hash_table_insert ( h, "{ssh-client}", config.ssh_client );
|
2014-11-25 06:57:34 -05:00
|
|
|
// Add list from variable arguments.
|
2014-09-03 07:07:26 -04:00
|
|
|
va_list ap;
|
|
|
|
va_start ( ap, length );
|
|
|
|
while ( 1 ) {
|
|
|
|
char * key = va_arg ( ap, char * );
|
|
|
|
if ( key == NULL ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char *value = va_arg ( ap, char * );
|
|
|
|
if ( value == NULL ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_hash_table_insert ( h, key, value );
|
|
|
|
}
|
|
|
|
va_end ( ap );
|
|
|
|
|
|
|
|
// Replace hits within {-\w+}.
|
|
|
|
GRegex *reg = g_regex_new ( "{[-\\w]+}", 0, 0, NULL );
|
2015-09-19 06:57:48 -04:00
|
|
|
char *res = g_regex_replace_eval ( reg, string, -1, 0, 0, helper_eval_cb, h, NULL );
|
2014-11-25 06:57:34 -05:00
|
|
|
// Free regex.
|
2014-09-03 07:07:26 -04:00
|
|
|
g_regex_unref ( reg );
|
2014-11-25 06:57:34 -05:00
|
|
|
// Destroy key-value storage.
|
2014-09-03 07:07:26 -04:00
|
|
|
g_hash_table_destroy ( h );
|
2014-11-25 06:57:34 -05:00
|
|
|
// Parse the string into shell arguments.
|
2014-09-03 07:07:26 -04:00
|
|
|
if ( g_shell_parse_argv ( res, length, output, &error ) ) {
|
|
|
|
g_free ( res );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
g_free ( res );
|
2014-11-25 06:57:34 -05:00
|
|
|
// Throw error if shell parsing fails.
|
2014-09-03 07:07:26 -04:00
|
|
|
if ( error ) {
|
2015-09-19 06:57:48 -04:00
|
|
|
char *msg = g_strdup_printf ( "Failed to parse: '%s'\nError: '%s'", string, error->message );
|
2016-02-08 03:03:11 -05:00
|
|
|
rofi_view_error_dialog ( msg, FALSE );
|
2014-09-03 07:07:26 -04:00
|
|
|
g_free ( msg );
|
|
|
|
// print error.
|
|
|
|
g_error_free ( error );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-11-15 10:26:55 -05:00
|
|
|
|
2015-01-12 08:13:46 -05:00
|
|
|
char *token_collate_key ( const char *token, int case_sensitive )
|
|
|
|
{
|
|
|
|
char *tmp, *compk;
|
|
|
|
|
|
|
|
if ( case_sensitive ) {
|
|
|
|
tmp = g_strdup ( token );
|
2015-01-13 05:48:38 -05:00
|
|
|
}
|
|
|
|
else {
|
2015-01-12 08:13:46 -05:00
|
|
|
tmp = g_utf8_casefold ( token, -1 );
|
|
|
|
}
|
|
|
|
|
2015-11-22 17:49:41 -05:00
|
|
|
compk = g_utf8_normalize ( tmp, -1, G_NORMALIZE_ALL );
|
2015-01-12 08:13:46 -05:00
|
|
|
g_free ( tmp );
|
|
|
|
|
|
|
|
return compk;
|
|
|
|
}
|
2015-11-17 10:10:14 -05:00
|
|
|
void tokenize_free ( char ** tokens )
|
|
|
|
{
|
|
|
|
if ( config.glob ) {
|
|
|
|
for ( size_t i = 0; tokens && tokens[i]; i++ ) {
|
|
|
|
g_pattern_spec_free ( (GPatternSpec *) tokens[i] );
|
|
|
|
}
|
|
|
|
g_free ( tokens );
|
|
|
|
}
|
|
|
|
else if ( config.regex ) {
|
|
|
|
for ( size_t i = 0; tokens && tokens[i]; i++ ) {
|
|
|
|
g_regex_unref ( (GRegex *) tokens[i] );
|
|
|
|
}
|
|
|
|
g_free ( tokens );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g_strfreev ( tokens );
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 08:13:46 -05:00
|
|
|
char **tokenize ( const char *input, int case_sensitive )
|
2014-11-15 10:26:55 -05:00
|
|
|
{
|
|
|
|
if ( input == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *saveptr = NULL, *token;
|
|
|
|
char **retv = NULL;
|
2015-10-04 10:37:07 -04:00
|
|
|
if ( !config.tokenize ) {
|
|
|
|
retv = g_malloc0 ( sizeof ( char* ) * 2 );
|
|
|
|
if ( config.glob ) {
|
2015-11-17 10:10:14 -05:00
|
|
|
token = g_strdup_printf ( "*%s*", input );
|
|
|
|
char *str = token_collate_key ( token, case_sensitive );
|
|
|
|
retv[0] = (char *) g_pattern_spec_new ( str );
|
2015-10-04 10:37:07 -04:00
|
|
|
g_free ( token ); token = NULL;
|
2015-11-17 10:10:14 -05:00
|
|
|
g_free ( str );
|
|
|
|
}
|
|
|
|
else if ( config.regex ) {
|
|
|
|
GRegex *reg = g_regex_new ( input, ( case_sensitive ) ? 0 : G_REGEX_CASELESS, G_REGEX_MATCH_PARTIAL, NULL );
|
|
|
|
if ( reg == NULL ) {
|
2015-11-17 10:25:42 -05:00
|
|
|
gchar *r = g_regex_escape_string ( input, -1 );
|
|
|
|
reg = g_regex_new ( r, ( case_sensitive ) ? 0 : G_REGEX_CASELESS, G_REGEX_MATCH_PARTIAL, NULL );
|
|
|
|
g_free ( r );
|
2015-11-17 10:10:14 -05:00
|
|
|
g_free ( retv );
|
|
|
|
}
|
|
|
|
retv[0] = (char *) reg;
|
2015-10-04 10:37:07 -04:00
|
|
|
}
|
|
|
|
else{
|
2015-10-04 11:02:54 -04:00
|
|
|
retv[0] = token_collate_key ( input, case_sensitive );
|
2015-10-04 10:37:07 -04:00
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2014-11-15 10:26:55 -05:00
|
|
|
// First entry is always full (modified) stringtext.
|
2015-10-04 10:37:07 -04:00
|
|
|
int num_tokens = 0;
|
2014-11-15 10:26:55 -05:00
|
|
|
|
|
|
|
// Copy the string, 'strtok_r' modifies it.
|
|
|
|
char *str = g_strdup ( input );
|
|
|
|
|
|
|
|
// Iterate over tokens.
|
|
|
|
// strtok should still be valid for utf8.
|
2015-09-19 06:57:48 -04:00
|
|
|
for ( token = strtok_r ( str, " ", &saveptr ); token != NULL; token = strtok_r ( NULL, " ", &saveptr ) ) {
|
2015-10-04 10:37:07 -04:00
|
|
|
retv = g_realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) );
|
|
|
|
if ( config.glob ) {
|
2015-11-17 10:25:42 -05:00
|
|
|
char *str = g_strdup_printf ( "*%s*", token );
|
2015-11-17 10:21:30 -05:00
|
|
|
char *t = token_collate_key ( str, case_sensitive );
|
|
|
|
retv[num_tokens] = (char *) g_pattern_spec_new ( t );
|
2015-10-04 10:37:07 -04:00
|
|
|
g_free ( t );
|
2015-11-17 10:10:14 -05:00
|
|
|
g_free ( str );
|
|
|
|
}
|
|
|
|
else if ( config.regex ) {
|
2015-11-17 10:25:42 -05:00
|
|
|
retv[num_tokens] = (char *) g_regex_new ( token, case_sensitive ? 0 : G_REGEX_CASELESS, 0, NULL );
|
2015-11-17 10:10:14 -05:00
|
|
|
if ( retv[num_tokens] == NULL ) {
|
2015-11-17 10:25:42 -05:00
|
|
|
gchar *r = g_regex_escape_string ( input, -1 );
|
|
|
|
retv[num_tokens] = (char *) g_regex_new ( r, ( case_sensitive ) ? 0 : G_REGEX_CASELESS, G_REGEX_MATCH_PARTIAL, NULL );
|
|
|
|
g_free ( r );
|
2015-11-17 10:10:14 -05:00
|
|
|
}
|
2015-10-04 10:37:07 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
retv[num_tokens] = token_collate_key ( token, case_sensitive );
|
|
|
|
}
|
2014-11-15 10:26:55 -05:00
|
|
|
retv[num_tokens + 1] = NULL;
|
|
|
|
num_tokens++;
|
|
|
|
}
|
|
|
|
// Free str.
|
|
|
|
g_free ( str );
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cli arg handling
|
2015-03-11 13:32:37 -04:00
|
|
|
int find_arg ( const char * const key )
|
2014-11-15 10:26:55 -05:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-03-11 13:32:37 -04:00
|
|
|
for ( i = 0; i < stored_argc && strcasecmp ( stored_argv[i], key ); i++ ) {
|
2014-11-15 10:26:55 -05:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:32:37 -04:00
|
|
|
return i < stored_argc ? i : -1;
|
2014-11-15 10:26:55 -05:00
|
|
|
}
|
2015-03-11 13:32:37 -04:00
|
|
|
int find_arg_str ( const char * const key, char** val )
|
2014-11-15 10:26:55 -05:00
|
|
|
{
|
2015-03-11 13:32:37 -04:00
|
|
|
int i = find_arg ( key );
|
2014-11-15 10:26:55 -05:00
|
|
|
|
2015-03-11 13:32:37 -04:00
|
|
|
if ( val != NULL && i > 0 && i < stored_argc - 1 ) {
|
|
|
|
*val = stored_argv[i + 1];
|
2014-11-15 10:26:55 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:32:37 -04:00
|
|
|
int find_arg_int ( const char * const key, int *val )
|
2014-11-15 10:26:55 -05:00
|
|
|
{
|
2015-03-11 13:32:37 -04:00
|
|
|
int i = find_arg ( key );
|
2014-11-15 10:26:55 -05:00
|
|
|
|
2015-03-11 13:32:37 -04:00
|
|
|
if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) {
|
|
|
|
*val = strtol ( stored_argv[i + 1], NULL, 10 );
|
2014-11-15 10:26:55 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-03-11 13:32:37 -04:00
|
|
|
int find_arg_uint ( const char * const key, unsigned int *val )
|
2014-11-15 10:26:55 -05:00
|
|
|
{
|
2015-03-11 13:32:37 -04:00
|
|
|
int i = find_arg ( key );
|
2014-11-15 10:26:55 -05:00
|
|
|
|
2015-03-11 13:32:37 -04:00
|
|
|
if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) {
|
|
|
|
*val = strtoul ( stored_argv[i + 1], NULL, 10 );
|
2014-11-15 10:26:55 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-02-17 04:31:59 -05:00
|
|
|
char helper_parse_char ( const char *arg )
|
|
|
|
{
|
2015-12-03 16:48:30 -05:00
|
|
|
int len = strlen ( arg );
|
2015-02-17 04:31:59 -05:00
|
|
|
// If the length is 1, it is not escaped.
|
|
|
|
if ( len == 1 ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return arg[0];
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
// If the length is 2 and the first character is '\', we unescape it.
|
2015-12-02 11:46:17 -05:00
|
|
|
if ( len == 2 && arg[0] == '\\' ) {
|
2015-02-17 04:31:59 -05:00
|
|
|
// New line
|
|
|
|
if ( arg[1] == 'n' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\n';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
// Bell
|
|
|
|
else if ( arg[1] == 'a' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\a';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
// Backspace
|
|
|
|
else if ( arg[1] == 'b' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\b';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
// Tab
|
|
|
|
else if ( arg[1] == 't' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\t';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
// Vertical tab
|
|
|
|
else if ( arg[1] == 'v' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\v';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
// Form feed
|
|
|
|
else if ( arg[1] == 'f' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\f';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
// Carriage return
|
|
|
|
else if ( arg[1] == 'r' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\r';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
// Forward slash
|
|
|
|
else if ( arg[1] == '\\' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\\';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
2015-09-20 14:05:04 -04:00
|
|
|
else if ( arg[1] == '0' ) {
|
2015-12-02 11:46:17 -05:00
|
|
|
return '\0';
|
2015-09-20 14:05:04 -04:00
|
|
|
}
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
2015-12-02 11:46:17 -05:00
|
|
|
if ( len > 2 && arg[0] == '\\' && arg[1] == 'x' ) {
|
|
|
|
return (char) strtol ( &arg[2], NULL, 16 );
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
2015-12-02 11:46:17 -05:00
|
|
|
fprintf ( stderr, "Failed to parse character string: \"%s\"\n", arg );
|
|
|
|
// for now default to newline.
|
|
|
|
return '\n';
|
2015-02-17 04:31:59 -05:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:32:37 -04:00
|
|
|
int find_arg_char ( const char * const key, char *val )
|
2014-11-15 10:26:55 -05:00
|
|
|
{
|
2015-03-11 13:32:37 -04:00
|
|
|
int i = find_arg ( key );
|
2014-11-15 10:26:55 -05:00
|
|
|
|
2015-03-11 13:32:37 -04:00
|
|
|
if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) {
|
|
|
|
*val = helper_parse_char ( stored_argv[i + 1] );
|
2014-11-15 10:26:55 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-12-02 03:09:20 -05:00
|
|
|
/**
|
|
|
|
* Shared 'token_match' function.
|
|
|
|
* Matches tokenized.
|
|
|
|
*/
|
2015-10-12 02:12:25 -04:00
|
|
|
static int fuzzy_token_match ( char **tokens, const char *input, __attribute__( ( unused ) ) int not_ascii, int case_sensitive )
|
2014-12-02 03:09:20 -05:00
|
|
|
{
|
2015-10-12 02:12:25 -04:00
|
|
|
int match = 1;
|
2015-10-01 08:45:23 -04:00
|
|
|
|
2014-12-02 03:09:20 -05:00
|
|
|
// Do a tokenized match.
|
2015-10-01 08:45:23 -04:00
|
|
|
|
2014-12-02 03:09:20 -05:00
|
|
|
if ( tokens ) {
|
2015-12-03 16:48:30 -05:00
|
|
|
char *compk = not_ascii ? token_collate_key ( input, case_sensitive ) : (char *) g_ascii_strdown ( input, -1 );
|
2014-12-02 03:09:20 -05:00
|
|
|
for ( int j = 0; match && tokens[j]; j++ ) {
|
2015-10-12 02:12:25 -04:00
|
|
|
char *t = compk;
|
|
|
|
char *token = tokens[j];
|
|
|
|
|
|
|
|
while ( *t && *token ) {
|
2015-12-03 16:48:30 -05:00
|
|
|
if ( ( g_utf8_get_char ( t ) == g_utf8_get_char ( token ) ) ) {
|
|
|
|
token = g_utf8_next_char ( token );
|
2015-07-01 03:12:22 -04:00
|
|
|
}
|
2015-12-03 16:48:30 -05:00
|
|
|
t = g_utf8_next_char ( t );
|
2015-07-01 03:12:22 -04:00
|
|
|
}
|
2015-10-12 02:12:25 -04:00
|
|
|
match = !( *token );
|
|
|
|
}
|
2015-12-01 16:09:34 -05:00
|
|
|
g_free ( compk );
|
2014-12-02 03:09:20 -05:00
|
|
|
}
|
2015-10-01 08:45:23 -04:00
|
|
|
|
2014-12-02 03:09:20 -05:00
|
|
|
return match;
|
|
|
|
}
|
2015-10-01 07:16:41 -04:00
|
|
|
static int normal_token_match ( char **tokens, const char *input, int not_ascii, int case_sensitive )
|
2015-06-28 07:53:37 -04:00
|
|
|
{
|
2015-10-12 02:12:25 -04:00
|
|
|
int match = 1;
|
2015-07-01 03:12:22 -04:00
|
|
|
|
2015-06-28 07:53:37 -04:00
|
|
|
// Do a tokenized match.
|
2015-10-01 07:16:41 -04:00
|
|
|
|
2015-06-28 07:53:37 -04:00
|
|
|
if ( tokens ) {
|
2015-10-12 02:12:25 -04:00
|
|
|
char *compk = not_ascii ? token_collate_key ( input, case_sensitive ) : (char *) input;
|
|
|
|
char *( *comparison )( const char *, const char * );
|
|
|
|
comparison = ( case_sensitive || not_ascii ) ? strstr : strcasestr;
|
|
|
|
for ( int j = 0; match && tokens[j]; j++ ) {
|
|
|
|
match = ( comparison ( compk, tokens[j] ) != NULL );
|
|
|
|
}
|
|
|
|
if ( not_ascii ) {
|
|
|
|
g_free ( compk );
|
|
|
|
}
|
2015-06-28 07:53:37 -04:00
|
|
|
}
|
2015-10-01 07:16:41 -04:00
|
|
|
|
2015-06-28 07:53:37 -04:00
|
|
|
return match;
|
|
|
|
}
|
2015-10-05 05:38:17 -04:00
|
|
|
|
2015-11-17 10:10:14 -05:00
|
|
|
static int regex_token_match ( char **tokens, const char *input, G_GNUC_UNUSED int not_ascii, G_GNUC_UNUSED int case_sensitive )
|
|
|
|
{
|
|
|
|
int match = 1;
|
|
|
|
|
|
|
|
// Do a tokenized match.
|
|
|
|
if ( tokens ) {
|
|
|
|
for ( int j = 0; match && tokens[j]; j++ ) {
|
|
|
|
match = g_regex_match ( (GRegex *) tokens[j], input, G_REGEX_MATCH_PARTIAL, NULL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2015-10-05 05:38:17 -04:00
|
|
|
static int glob_token_match ( char **tokens, const char *input, int not_ascii, int case_sensitive )
|
2015-10-02 14:39:57 -04:00
|
|
|
{
|
|
|
|
int match = 1;
|
2015-11-17 10:25:42 -05:00
|
|
|
char *compk = not_ascii ? token_collate_key ( input, case_sensitive ) : g_ascii_strdown ( input, -1 );
|
2015-10-02 14:39:57 -04:00
|
|
|
|
|
|
|
// Do a tokenized match.
|
|
|
|
if ( tokens ) {
|
|
|
|
for ( int j = 0; match && tokens[j]; j++ ) {
|
2015-11-17 10:10:14 -05:00
|
|
|
match = g_pattern_match_string ( (GPatternSpec *) tokens[j], compk );
|
2015-10-02 14:39:57 -04:00
|
|
|
}
|
|
|
|
}
|
2015-11-17 10:21:30 -05:00
|
|
|
g_free ( compk );
|
2015-10-02 14:39:57 -04:00
|
|
|
return match;
|
|
|
|
}
|
2015-10-05 05:38:17 -04:00
|
|
|
|
2015-11-21 17:59:59 -05:00
|
|
|
int token_match ( char **tokens, const char *input, int not_ascii, int case_sensitive )
|
2015-07-01 03:12:22 -04:00
|
|
|
{
|
2015-10-02 14:39:57 -04:00
|
|
|
if ( config.glob ) {
|
2015-10-05 05:38:17 -04:00
|
|
|
return glob_token_match ( tokens, input, not_ascii, case_sensitive );
|
2015-10-02 14:39:57 -04:00
|
|
|
}
|
2015-11-17 10:10:14 -05:00
|
|
|
else if ( config.regex ) {
|
|
|
|
return regex_token_match ( tokens, input, not_ascii, case_sensitive );
|
|
|
|
}
|
2015-10-02 14:39:57 -04:00
|
|
|
else if ( config.fuzzy ) {
|
2015-10-01 07:16:41 -04:00
|
|
|
return fuzzy_token_match ( tokens, input, not_ascii, case_sensitive );
|
2015-07-01 03:12:22 -04:00
|
|
|
}
|
2015-10-01 07:16:41 -04:00
|
|
|
return normal_token_match ( tokens, input, not_ascii, case_sensitive );
|
2015-07-01 03:12:22 -04:00
|
|
|
}
|
2015-01-05 15:53:50 -05:00
|
|
|
|
2015-03-27 15:28:53 -04:00
|
|
|
int execute_generator ( const char * cmd )
|
2015-01-05 15:53:50 -05:00
|
|
|
{
|
|
|
|
char **args = NULL;
|
|
|
|
int argv = 0;
|
|
|
|
helper_parse_setup ( config.run_command, &args, &argv, "{cmd}", cmd, NULL );
|
|
|
|
|
|
|
|
int fd = -1;
|
|
|
|
GError *error = NULL;
|
2015-09-19 06:57:48 -04:00
|
|
|
g_spawn_async_with_pipes ( NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
|
2015-01-05 15:53:50 -05:00
|
|
|
|
|
|
|
if ( error != NULL ) {
|
2015-09-19 06:57:48 -04:00
|
|
|
char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", cmd, error->message );
|
2016-02-08 03:03:11 -05:00
|
|
|
rofi_view_error_dialog ( msg, FALSE );
|
2015-01-05 15:53:50 -05:00
|
|
|
g_free ( msg );
|
|
|
|
// print error.
|
|
|
|
g_error_free ( error );
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
g_strfreev ( args );
|
|
|
|
return fd;
|
|
|
|
}
|
2015-02-03 02:00:33 -05:00
|
|
|
|
2015-07-30 02:57:09 -04:00
|
|
|
int create_pid_file ( const char *pidfile )
|
2015-02-03 02:00:33 -05:00
|
|
|
{
|
|
|
|
if ( pidfile == NULL ) {
|
2015-07-30 02:57:09 -04:00
|
|
|
return -1;
|
2015-02-03 02:00:33 -05:00
|
|
|
}
|
|
|
|
|
2015-07-30 02:57:09 -04:00
|
|
|
int fd = g_open ( pidfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
|
2015-02-03 02:00:33 -05:00
|
|
|
if ( fd < 0 ) {
|
|
|
|
fprintf ( stderr, "Failed to create pid file." );
|
2015-07-30 02:57:09 -04:00
|
|
|
return -1;
|
2015-02-03 02:00:33 -05:00
|
|
|
}
|
|
|
|
// Set it to close the File Descriptor on exit.
|
|
|
|
int flags = fcntl ( fd, F_GETFD, NULL );
|
|
|
|
flags = flags | FD_CLOEXEC;
|
|
|
|
if ( fcntl ( fd, F_SETFD, flags, NULL ) < 0 ) {
|
|
|
|
fprintf ( stderr, "Failed to set CLOEXEC on pidfile." );
|
2015-07-30 02:57:09 -04:00
|
|
|
remove_pid_file ( fd );
|
|
|
|
return -1;
|
2015-02-03 02:00:33 -05:00
|
|
|
}
|
|
|
|
// Try to get exclusive write lock on FD
|
|
|
|
int retv = flock ( fd, LOCK_EX | LOCK_NB );
|
|
|
|
if ( retv != 0 ) {
|
|
|
|
fprintf ( stderr, "Failed to set lock on pidfile: Rofi already running?\n" );
|
2015-07-30 02:57:09 -04:00
|
|
|
fprintf ( stderr, "Got error: %d %s\n", retv, strerror ( errno ) );
|
|
|
|
remove_pid_file ( fd );
|
|
|
|
return -1;
|
2015-02-03 02:00:33 -05:00
|
|
|
}
|
|
|
|
if ( ftruncate ( fd, (off_t) 0 ) == 0 ) {
|
|
|
|
// Write pid, not needed, but for completeness sake.
|
|
|
|
char buffer[64];
|
|
|
|
int length = snprintf ( buffer, 64, "%i", getpid () );
|
|
|
|
ssize_t l = 0;
|
|
|
|
while ( l < length ) {
|
|
|
|
l += write ( fd, &buffer[l], length - l );
|
|
|
|
}
|
|
|
|
}
|
2015-07-30 02:57:09 -04:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_pid_file ( int fd )
|
|
|
|
{
|
|
|
|
if ( fd >= 0 ) {
|
2015-08-02 09:56:52 -04:00
|
|
|
if ( close ( fd ) ) {
|
2015-08-04 03:42:14 -04:00
|
|
|
fprintf ( stderr, "Failed to close pidfile: '%s'\n", strerror ( errno ) );
|
2015-08-02 09:56:52 -04:00
|
|
|
}
|
2015-07-30 02:57:09 -04:00
|
|
|
}
|
2015-02-03 02:00:33 -05:00
|
|
|
}
|
2015-02-03 02:21:59 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do some input validation, especially the first few could break things.
|
|
|
|
* It is good to catch them beforehand.
|
|
|
|
*
|
|
|
|
* This functions exits the program with 1 when it finds an invalid configuration.
|
|
|
|
*/
|
2015-10-18 07:40:39 -04:00
|
|
|
void config_sanity_check ( Display *display )
|
2015-02-03 02:21:59 -05:00
|
|
|
{
|
2015-11-12 11:20:51 -05:00
|
|
|
if ( config.threads == 0 ) {
|
|
|
|
config.threads = 1;
|
|
|
|
long procs = sysconf ( _SC_NPROCESSORS_CONF );
|
|
|
|
if ( procs > 0 ) {
|
2015-12-01 16:09:34 -05:00
|
|
|
config.threads = MIN ( procs, 128l );
|
2015-11-12 11:20:51 -05:00
|
|
|
}
|
|
|
|
}
|
2015-11-04 06:03:59 -05:00
|
|
|
// If alternative row is not set, copy the normal background color.
|
|
|
|
// Do this at the beginning as we might use it in the error dialog.
|
|
|
|
if ( config.menu_bg_alt == NULL ) {
|
|
|
|
config.menu_bg_alt = config.menu_bg;
|
|
|
|
}
|
2015-07-28 16:22:18 -04:00
|
|
|
int found_error = FALSE;
|
2015-09-19 06:21:30 -04:00
|
|
|
GString *msg = g_string_new (
|
|
|
|
"<big><b>The configuration failed to validate:</b></big>\n" );
|
2015-02-03 02:21:59 -05:00
|
|
|
if ( config.element_height < 1 ) {
|
2015-09-19 06:57:48 -04:00
|
|
|
g_string_append_printf ( msg, "\t<b>config.element_height</b>=%d is invalid. An element needs to be atleast 1 line high.\n",
|
|
|
|
config.element_height );
|
2015-07-28 16:22:18 -04:00
|
|
|
config.element_height = 1;
|
|
|
|
found_error = TRUE;
|
2015-02-03 02:21:59 -05:00
|
|
|
}
|
|
|
|
if ( config.menu_columns == 0 ) {
|
2015-09-19 06:57:48 -04:00
|
|
|
g_string_append_printf ( msg, "\t<b>config.menu_columns</b>=%d is invalid. You need at least one visible column.\n",
|
|
|
|
config.menu_columns );
|
2015-07-28 16:22:18 -04:00
|
|
|
config.menu_columns = 1;
|
|
|
|
found_error = TRUE;
|
2015-02-03 02:21:59 -05:00
|
|
|
}
|
|
|
|
if ( config.menu_width == 0 ) {
|
2015-10-18 07:40:39 -04:00
|
|
|
g_string_append_printf ( msg, "<b>config.menu_width</b>=0 is invalid. You cannot have a window with no width." );
|
2015-07-28 16:22:18 -04:00
|
|
|
config.menu_columns = 50;
|
|
|
|
found_error = TRUE;
|
2015-02-03 02:21:59 -05:00
|
|
|
}
|
|
|
|
if ( !( config.location >= WL_CENTER && config.location <= WL_WEST ) ) {
|
2015-09-19 06:57:48 -04:00
|
|
|
g_string_append_printf ( msg, "\t<b>config.location</b>=%d is invalid. Value should be between %d and %d.\n",
|
|
|
|
config.location, WL_CENTER, WL_WEST );
|
2015-07-28 16:22:18 -04:00
|
|
|
config.location = WL_CENTER;
|
|
|
|
found_error = 1;
|
|
|
|
}
|
2015-08-26 15:49:53 -04:00
|
|
|
if ( 0 ) {
|
2015-08-27 01:35:34 -04:00
|
|
|
if ( !( config.line_margin <= 50 ) ) {
|
2015-09-19 06:57:48 -04:00
|
|
|
g_string_append_printf ( msg, "\t<b>config.line_margin</b>=%d is invalid. Value should be between %d and %d.\n",
|
|
|
|
config.line_margin, 0, 50 );
|
2015-08-27 01:35:34 -04:00
|
|
|
config.line_margin = 2;
|
2015-08-26 15:49:53 -04:00
|
|
|
found_error = 1;
|
|
|
|
}
|
2015-08-04 03:42:14 -04:00
|
|
|
}
|
2015-10-18 07:45:15 -04:00
|
|
|
if ( config.fullscreen && config.monitor != -1 ) {
|
2015-10-18 13:02:19 -04:00
|
|
|
g_string_append_printf ( msg,
|
|
|
|
"\t<b>config.monitor</b>=%d is invalid. Value should be unset (-1) when fullscreen mode is enabled.\n",
|
|
|
|
config.monitor );
|
2015-10-18 07:45:15 -04:00
|
|
|
config.monitor = -1;
|
2015-10-18 13:02:19 -04:00
|
|
|
found_error = 1;
|
2015-10-18 07:45:15 -04:00
|
|
|
}
|
2015-10-18 07:40:39 -04:00
|
|
|
|
|
|
|
// Check size
|
|
|
|
{
|
|
|
|
int ssize = monitor_get_smallest_size ( display );
|
|
|
|
if ( config.monitor >= 0 ) {
|
|
|
|
workarea mon;
|
|
|
|
if ( monitor_get_dimension ( display, DefaultScreenOfDisplay ( display ), config.monitor, &mon ) ) {
|
|
|
|
ssize = MIN ( mon.w, mon.h );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
g_string_append_printf ( msg, "\t<b>config.monitor</b>=%d Could not find monitor.\n", config.monitor );
|
|
|
|
ssize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Have todo an estimate here.
|
|
|
|
if ( ( 2 * ( config.padding + config.menu_bw ) ) > ( 0.9 * ssize ) ) {
|
2016-01-30 17:04:14 -05:00
|
|
|
g_string_append_printf ( msg, "\t<b>config.padding+config.menu_bw</b>=%d is too big for the minimum size of the monitor: %d.\n",
|
2015-10-18 07:40:39 -04:00
|
|
|
( config.padding + config.menu_bw ), ssize );
|
|
|
|
|
|
|
|
config.padding = 0;
|
|
|
|
config.menu_bw = 0;
|
|
|
|
found_error = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:22:18 -04:00
|
|
|
if ( found_error ) {
|
|
|
|
g_string_append ( msg, "Please update your configuration." );
|
|
|
|
show_error_message ( msg->str, TRUE );
|
2015-10-18 07:40:39 -04:00
|
|
|
exit ( EXIT_FAILURE );
|
2015-02-03 02:21:59 -05:00
|
|
|
}
|
2015-07-28 16:22:18 -04:00
|
|
|
|
|
|
|
g_string_free ( msg, TRUE );
|
2015-02-03 02:21:59 -05:00
|
|
|
}
|
2015-10-01 07:16:41 -04:00
|
|
|
|
2015-11-27 07:01:25 -05:00
|
|
|
char *rofi_expand_path ( const char *input )
|
|
|
|
{
|
|
|
|
char **str = g_strsplit ( input, G_DIR_SEPARATOR_S, -1 );
|
|
|
|
for ( unsigned int i = 0; str && str[i]; i++ ) {
|
|
|
|
// Replace ~ with current user homedir.
|
|
|
|
if ( str[i][0] == '~' && str[i][1] == '\0' ) {
|
|
|
|
g_free ( str[i] );
|
|
|
|
str[i] = g_strdup ( g_get_home_dir () );
|
|
|
|
}
|
|
|
|
// If other user, ask getpwnam.
|
|
|
|
else if ( str[i][0] == '~' ) {
|
|
|
|
struct passwd *p = getpwnam ( &( str[i][1] ) );
|
|
|
|
if ( p != NULL ) {
|
|
|
|
g_free ( str[i] );
|
|
|
|
str[i] = g_strdup ( p->pw_dir );
|
|
|
|
}
|
|
|
|
}
|
2015-11-28 06:59:10 -05:00
|
|
|
else if ( i == 0 ) {
|
|
|
|
char * s = str[i];
|
|
|
|
if ( input[0] == G_DIR_SEPARATOR ) {
|
|
|
|
str[i] = g_strdup_printf ( "%s%s", G_DIR_SEPARATOR_S, s );
|
|
|
|
g_free ( s );
|
|
|
|
}
|
|
|
|
}
|
2015-11-27 07:01:25 -05:00
|
|
|
}
|
|
|
|
char *retv = g_build_filenamev ( str );
|
|
|
|
g_strfreev ( str );
|
|
|
|
return retv;
|
|
|
|
}
|
2016-01-04 11:14:15 -05:00
|
|
|
|
|
|
|
#define MIN3( a, b, c ) ( ( a ) < ( b ) ? ( ( a ) < ( c ) ? ( a ) : ( c ) ) : ( ( b ) < ( c ) ? ( b ) : ( c ) ) )
|
|
|
|
|
|
|
|
unsigned int levenshtein ( const char *needle, const char *haystack )
|
|
|
|
{
|
|
|
|
unsigned int x, y, lastdiag, olddiag;
|
|
|
|
size_t needlelen = g_utf8_strlen ( needle, -1 );
|
|
|
|
size_t haystacklen = g_utf8_strlen ( haystack, -1 );
|
|
|
|
unsigned int column[needlelen + 1];
|
|
|
|
for ( y = 0; y <= needlelen; y++ ) {
|
|
|
|
column[y] = y;
|
|
|
|
}
|
|
|
|
for ( x = 1; x <= haystacklen; x++ ) {
|
|
|
|
const char *needles = needle;
|
|
|
|
column[0] = x;
|
|
|
|
gunichar haystackc = g_utf8_get_char ( haystack );
|
|
|
|
if ( !config.case_sensitive ) {
|
|
|
|
haystackc = g_unichar_tolower ( haystackc );
|
|
|
|
}
|
|
|
|
for ( y = 1, lastdiag = x - 1; y <= needlelen; y++ ) {
|
|
|
|
gunichar needlec = g_utf8_get_char ( needles );
|
|
|
|
if ( !config.case_sensitive ) {
|
|
|
|
needlec = g_unichar_tolower ( needlec );
|
|
|
|
}
|
|
|
|
olddiag = column[y];
|
|
|
|
column[y] = MIN3 ( column[y] + 1, column[y - 1] + 1, lastdiag + ( needlec == haystackc ? 0 : 1 ) );
|
|
|
|
lastdiag = olddiag;
|
|
|
|
needles = g_utf8_next_char ( needles );
|
|
|
|
}
|
|
|
|
haystack = g_utf8_next_char ( haystack );
|
|
|
|
}
|
|
|
|
return column[needlelen];
|
|
|
|
}
|