/* * model.h -- dtlaunch without X: matching, the calculator, snippets, programs * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * C99 and POSIX only (ext/src/mcde/AGENTS.md); tested by test_model.c. */ #ifndef DTLAUNCH_MODEL_H #define DTLAUNCH_MODEL_H #include /* * How well QUERY matches TEXT, case-insensitively: 0 for no match, otherwise * a score that is higher for a match at the start, for a match at the start * of a word, and for letters that follow each other. Every letter of QUERY * must appear in TEXT in this order. */ int mx_match(const char *query, const char *text); /* * Evaluate an arithmetic expression: numbers, + - * / % ^, parentheses, * unary minus. A leading '=' is ignored. Returns 0 and sets *out, or -1 * if the text is not an expression (or divides by zero). */ int mx_calc(const char *expr, double *out); /* format a result the way a person writes it: no trailing zeros */ void mx_calc_format(double v, char *buf, size_t len); /* * Snippets: a text file, one snippet a line, "keywordtext"; in the text * \n is a newline, \t a tab and \\ a backslash. Lines starting with '#' and * lines without a tab are skipped. */ typedef struct { char *key; char *text; } MxSnippet; int mx_snippets_load(const char *path, MxSnippet **out, size_t *n); void mx_snippets_free(MxSnippet *s, size_t n); /* the escapes above, decoded; the result is malloc'd */ char *mx_unescape(const char *s); /* * The programs in $PATH (executable regular files), sorted, without * duplicates. Returns 0; *out is a malloc'd array of malloc'd names. */ int mx_path_programs(const char *path, char ***out, size_t *n); void mx_strv_free(char **v, size_t n); /* * The panes of System Settings, as `dtsystemsettings -list-panes' prints them: * one a line, "IDnamecategorykeywords" (keywords may be * missing). ID is what `dtsystemsettings -pane ID' takes and ends up in an * exec, so the text is not trusted: an ID of anything but letters, digits, '-' * and '_' (at most 40), an empty name or category, or fewer than three fields * skips the line. *out is malloc'd; free it with mx_panes_free. */ typedef struct { char *id; char *name; char *category; char *keywords; } MxPane; int mx_panes_parse(const char *text, MxPane **out, size_t *n); void mx_panes_free(MxPane *p, size_t n); /* * How well QUERY matches a pane: its name as mx_match scores it, or a word of * its keywords at half that. 0 for no match. */ int mx_pane_match(const char *query, const MxPane *p); #endif /* DTLAUNCH_MODEL_H */