rofi  1.6.1
helper.c File Reference
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <pwd.h>
#include <ctype.h>
#include <pango/pango.h>
#include <pango/pango-fontmap.h>
#include <pango/pangocairo.h>
#include "display.h"
#include "xcb.h"
#include "helper.h"
#include "helper-theme.h"
#include "settings.h"
#include "rofi.h"
#include "view.h"
Include dependency graph for helper.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "Helper"
 
#define MIN3(a, b, c)   ( ( a ) < ( b ) ? ( ( a ) < ( c ) ? ( a ) : ( c ) ) : ( ( b ) < ( c ) ? ( b ) : ( c ) ) )
 
#define FUZZY_SCORER_MAX_LENGTH   256
 
#define MIN_SCORE   ( INT_MIN / 2 )
 
#define LEADING_GAP_SCORE   -4
 
#define GAP_SCORE   -5
 
#define WORD_START_SCORE   50
 
#define NON_WORD_SCORE   40
 
#define CAMEL_SCORE   ( WORD_START_SCORE + GAP_SCORE - 1 )
 
#define CONSECUTIVE_SCORE   ( WORD_START_SCORE + GAP_SCORE )
 
#define PATTERN_NON_START_MULTIPLIER   1
 
#define PATTERN_START_MULTIPLIER   2
 

Enumerations

enum  CharClass { LOWER, UPPER, DIGIT, NON_WORD }
 

Functions

char * helper_string_replace_if_exists_v (char *string, GHashTable *h)
 
void cmd_set_arguments (int argc, char **argv)
 
int helper_parse_setup (char *string, char ***output, int *length,...)
 
void helper_tokenize_free (rofi_int_matcher **tokens)
 
static gchar * glob_to_regex (const char *input)
 
static gchar * fuzzy_to_regex (const char *input)
 
static char * utf8_helper_simplify_string (const char *s)
 
static GRegex * R (const char *s, int case_sensitive)
 
static rofi_int_matchercreate_regex (const char *input, int case_sensitive)
 
rofi_int_matcher ** helper_tokenize (const char *input, int case_sensitive)
 
int find_arg (const char *const key)
 
int find_arg_str (const char *const key, char **val)
 
const char ** find_arg_strv (const char *const key)
 
int find_arg_int (const char *const key, int *val)
 
int find_arg_uint (const char *const key, unsigned int *val)
 
char helper_parse_char (const char *arg)
 
int find_arg_char (const char *const key, char *val)
 
PangoAttrList * helper_token_match_get_pango_attr (RofiHighlightColorStyle th, rofi_int_matcher **tokens, const char *input, PangoAttrList *retv)
 
int helper_token_match (rofi_int_matcher *const *tokens, const char *input)
 
int execute_generator (const char *cmd)
 
int create_pid_file (const char *pidfile)
 
void remove_pid_file (int fd)
 
gboolean helper_validate_font (PangoFontDescription *pfd, const char *font)
 
int config_sanity_check (void)
 
char * rofi_expand_path (const char *input)
 
unsigned int levenshtein (const char *needle, const glong needlelen, const char *haystack, const glong haystacklen)
 
char * rofi_latin_to_utf8_strdup (const char *input, gssize length)
 
gchar * rofi_escape_markup (gchar *text)
 
char * rofi_force_utf8 (const gchar *data, ssize_t length)
 
static enum CharClass rofi_scorer_get_character_class (gunichar c)
 
static int rofi_scorer_get_score_for (enum CharClass prev, enum CharClass curr)
 
int rofi_scorer_fuzzy_evaluate (const char *pattern, glong plen, const char *str, glong slen)
 
int utf8_strncmp (const char *a, const char *b, size_t n)
 
gboolean helper_execute (const char *wd, char **args, const char *error_precmd, const char *error_cmd, RofiHelperExecuteContext *context)
 
gboolean helper_execute_command (const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
 
char * helper_get_theme_path (const char *file)
 
static void parse_pair (char *input, rofi_range_pair *item)
 
void parse_ranges (char *input, rofi_range_pair **list, unsigned int *length)
 
void rofi_output_formatted_line (const char *format, const char *string, int selected_line, const char *filter)
 
static gboolean helper_eval_cb2 (const GMatchInfo *info, GString *res, gpointer data)
 
char * helper_string_replace_if_exists (char *string,...)
 

Variables

const char *const monitor_position_entries []
 
static int stored_argc = 0
 
static char ** stored_argv = NULL
 

Macro Definition Documentation

◆ CAMEL_SCORE

#define CAMEL_SCORE   ( WORD_START_SCORE + GAP_SCORE - 1 )

CamelCase score

Definition at line 841 of file helper.c.

Referenced by rofi_scorer_get_score_for().

◆ CONSECUTIVE_SCORE

#define CONSECUTIVE_SCORE   ( WORD_START_SCORE + GAP_SCORE )

Consecutive score

Definition at line 843 of file helper.c.

Referenced by rofi_scorer_fuzzy_evaluate().

◆ FUZZY_SCORER_MAX_LENGTH

#define FUZZY_SCORER_MAX_LENGTH   256

Max length of input to score.

Definition at line 829 of file helper.c.

Referenced by rofi_scorer_fuzzy_evaluate().

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "Helper"

The log domain for this helper.

Definition at line 30 of file helper.c.

◆ GAP_SCORE

#define GAP_SCORE   -5

gap score

Definition at line 835 of file helper.c.

Referenced by rofi_scorer_fuzzy_evaluate().

◆ LEADING_GAP_SCORE

#define LEADING_GAP_SCORE   -4

Leading gap score

Definition at line 833 of file helper.c.

Referenced by rofi_scorer_fuzzy_evaluate().

◆ MIN3

#define MIN3 (   a,
  b,
 
)    ( ( a ) < ( b ) ? ( ( a ) < ( c ) ? ( a ) : ( c ) ) : ( ( b ) < ( c ) ? ( b ) : ( c ) ) )

Return the minimum value of a,b,c

Definition at line 742 of file helper.c.

Referenced by levenshtein().

◆ MIN_SCORE

#define MIN_SCORE   ( INT_MIN / 2 )

minimum score

Definition at line 831 of file helper.c.

Referenced by rofi_scorer_fuzzy_evaluate().

◆ NON_WORD_SCORE

#define NON_WORD_SCORE   40

non-word score

Definition at line 839 of file helper.c.

Referenced by rofi_scorer_get_score_for().

◆ PATTERN_NON_START_MULTIPLIER

#define PATTERN_NON_START_MULTIPLIER   1

non-start multiplier

Definition at line 845 of file helper.c.

Referenced by rofi_scorer_fuzzy_evaluate().

◆ PATTERN_START_MULTIPLIER

#define PATTERN_START_MULTIPLIER   2

start multiplier

Definition at line 847 of file helper.c.

Referenced by rofi_scorer_fuzzy_evaluate().

◆ WORD_START_SCORE

#define WORD_START_SCORE   50

start of word score

Definition at line 837 of file helper.c.

Referenced by rofi_scorer_get_score_for().

Enumeration Type Documentation

◆ CharClass

enum CharClass

Character classification.

Enumerator
LOWER 
UPPER 
DIGIT 
NON_WORD 

Definition at line 852 of file helper.c.

Function Documentation

◆ create_regex()

static rofi_int_matcher* create_regex ( const char *  input,
int  case_sensitive 
)
static

Definition at line 222 of file helper.c.

References config, fuzzy_to_regex(), glob_to_regex(), rofi_int_matcher_t::invert, Settings::matching_method, Settings::matching_negate_char, MM_FUZZY, MM_GLOB, MM_REGEX, R(), and rofi_int_matcher_t::regex.

Referenced by helper_tokenize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fuzzy_to_regex()

static gchar* fuzzy_to_regex ( const char *  input)
static

Definition at line 151 of file helper.c.

Referenced by create_regex().

Here is the caller graph for this function:

◆ glob_to_regex()

static gchar* glob_to_regex ( const char *  input)
static

Definition at line 134 of file helper.c.

Referenced by create_regex().

Here is the caller graph for this function:

◆ helper_eval_cb2()

static gboolean helper_eval_cb2 ( const GMatchInfo *  info,
GString *  res,
gpointer  data 
)
static

Definition at line 1252 of file helper.c.

Referenced by helper_string_replace_if_exists_v().

Here is the caller graph for this function:

◆ helper_execute()

gboolean helper_execute ( const char *  wd,
char **  args,
const char *  error_precmd,
const char *  error_cmd,
RofiHelperExecuteContext context 
)
Parameters
wdThe working directory.
argsThe arguments of the command to exec.
error_precmdPrefix to error message command.
error_cmdError message command
contextThe startup notification context, if any

Executes the command

Returns
TRUE when successful, FALSE when failed.

Definition at line 1017 of file helper.c.

References display_startup_notification(), and rofi_view_error_dialog().

Referenced by execshssh(), and helper_execute_command().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ helper_execute_command()

gboolean helper_execute_command ( const char *  wd,
const char *  cmd,
gboolean  run_in_term,
RofiHelperExecuteContext context 
)
Parameters
wdThe work directory (optional)
cmdThe cmd to execute
run_in_termIndicate if command should be run in a terminal
contextThe startup notification context, if any

Execute command. If needed members of context are NULL, they will be filled.

Returns
FALSE On failure, TRUE on success

Definition at line 1042 of file helper.c.

References RofiHelperExecuteContext::binary, RofiHelperExecuteContext::command, config, RofiHelperExecuteContext::description, helper_execute(), helper_parse_setup(), RofiHelperExecuteContext::name, Settings::run_command, and Settings::run_shell_command.

Referenced by exec_cmd(), and file_browser_mode_result().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ helper_get_theme_path()

char* helper_get_theme_path ( const char *  file)
Parameters
fileFile name passed to option.
Returns
path to theme or copy of filename if not found.

Definition at line 1080 of file helper.c.

References rofi_expand_path().

Here is the call graph for this function:

◆ helper_string_replace_if_exists()

char* helper_string_replace_if_exists ( char *  string,
  ... 
)
Parameters
stringThe string with elements to be replaced
...Set of {key}, value that will be replaced, terminated by a NULL

Items {key} are replaced by the value if '{key}' is passed as key/value pair, otherwise removed from string. If the {key} is in between [] all the text between [] are removed if {key} is not found. Otherwise key is replaced and [ & ] removed.

This allows for optional replacement, f.e. '{ssh-client} [-t {title}] -e "{cmd}"' the '-t {title}' is only there if {title} is set.

Returns
a new string with the keys replaced.

Definition at line 1298 of file helper.c.

References helper_string_replace_if_exists_v().

Here is the call graph for this function:

◆ helper_string_replace_if_exists_v()

char * helper_string_replace_if_exists_v ( char *  string,
GHashTable *  h 
)
Parameters
stringThe string with elements to be replaced
hHash table with set of {key}, value that will be replaced, terminated by a NULL

Items {key} are replaced by the value if '{key}' is passed as key/value pair, otherwise removed from string. If the {key} is in between [] all the text between [] are removed if {key} is not found. Otherwise key is replaced and [ & ] removed.

This allows for optional replacement, f.e. '{ssh-client} [-t {title}] -e "{cmd}"' the '-t {title}' is only there if {title} is set.

Returns
a new string with the keys replaced.

Definition at line 1332 of file helper.c.

References helper_eval_cb2(), and rofi_view_error_dialog().

Referenced by helper_parse_setup(), and helper_string_replace_if_exists().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_pair()

static void parse_pair ( char *  input,
rofi_range_pair item 
)
static

Definition at line 1141 of file helper.c.

References rofi_range_pair::start, and rofi_range_pair::stop.

Referenced by parse_ranges().

Here is the caller graph for this function:

◆ parse_ranges()

void parse_ranges ( char *  input,
rofi_range_pair **  list,
unsigned int *  length 
)

Ranges.

Parameters
inputString to parse
listList of ranges
lengthLength of list.

ranges

Definition at line 1170 of file helper.c.

References parse_pair().

Referenced by dmenu_mode_init(), and parse_header_entry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ R()

static GRegex* R ( const char *  s,
int  case_sensitive 
)
inlinestatic

Definition at line 207 of file helper.c.

References config, Settings::normalize_match, and utf8_helper_simplify_string().

Referenced by create_regex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rofi_output_formatted_line()

void rofi_output_formatted_line ( const char *  format,
const char *  string,
int  selected_line,
const char *  filter 
)
Parameters
formatThe format string used. See below for possible syntax.
stringThe selected entry.
selected_lineThe selected line index.
filterThe entered filter.

Function that outputs the selected line in the user-specified format. Currently the following formats are supported:

  • i: Print the index (0-(N-1))
  • d: Print the index (1-N)
  • s: Print input string.
  • q: Print quoted input string.
  • f: Print the entered filter.
  • F: Print the entered filter, quoted

This functions outputs the formatted string to stdout, appends a newline (
) character and calls flush on the file descriptor.

Definition at line 1204 of file helper.c.

Referenced by dmenu_print_results(), and dmenu_switcher_dialog().

Here is the caller graph for this function:

◆ rofi_scorer_get_character_class()

static enum CharClass rofi_scorer_get_character_class ( gunichar  c)
static
Parameters
cThe character to determine class of
Returns
the class of the character c.

Definition at line 869 of file helper.c.

References DIGIT, LOWER, NON_WORD, and UPPER.

Referenced by rofi_scorer_fuzzy_evaluate().

Here is the caller graph for this function:

◆ rofi_scorer_get_score_for()

static int rofi_scorer_get_score_for ( enum CharClass  prev,
enum CharClass  curr 
)
static
Parameters
prevThe previous character.
currThe current character

Scrore the transition.

Returns
score of the transition.

Definition at line 891 of file helper.c.

References CAMEL_SCORE, DIGIT, LOWER, NON_WORD, NON_WORD_SCORE, UPPER, and WORD_START_SCORE.

Referenced by rofi_scorer_fuzzy_evaluate().

Here is the caller graph for this function:

◆ utf8_helper_simplify_string()

static char* utf8_helper_simplify_string ( const char *  s)
static

Definition at line 182 of file helper.c.

Referenced by helper_token_match(), and R().

Here is the caller graph for this function:

◆ utf8_strncmp()

int utf8_strncmp ( const char *  a,
const char *  b,
size_t  n 
)
Parameters
aUTF-8 string to compare
bUTF-8 string to compare
nMaximum number of characters to compare

Compares the G_NORMALIZE_ALL_COMPOSE forms of the two strings.

Returns
less than, equal to, or greater than zero if the first n characters (not bytes) of a are found, respectively, to be less than, to match, or be greater than the first n characters (not bytes) of b.

Definition at line 1005 of file helper.c.

Referenced by combi_mode_result(), and combi_preprocess_input().

Here is the caller graph for this function:

Variable Documentation

◆ monitor_position_entries

const char* const monitor_position_entries[]
Initial value:
= {
"on focused monitor",
"on focused window",
"at mouse pointer",
"on monitor with focused window",
"on monitor that has mouse pointer"
}

Textual description of positioning rofi.

Definition at line 61 of file helper.c.

Referenced by config_sanity_check().

◆ stored_argc

int stored_argc = 0
static

copy of the argc for use in commandline argument parser.

Definition at line 69 of file helper.c.

Referenced by cmd_set_arguments(), find_arg(), find_arg_char(), find_arg_int(), find_arg_str(), find_arg_strv(), and find_arg_uint().

◆ stored_argv

char** stored_argv = NULL
static

copy of the argv pointer for use in the commandline argument parser

Definition at line 71 of file helper.c.

Referenced by cmd_set_arguments(), find_arg(), find_arg_char(), find_arg_int(), find_arg_str(), find_arg_strv(), and find_arg_uint().