wlmaker
Loading...
Searching...
No Matches
desktop-parser.c File Reference
#include "desktop-parser.h"
#include <ini.h>
#include <libbase/libbase.h>
#include <regex.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for desktop-parser.c:

Classes

struct  _desktop_parser_handler_arg
struct  desktop_parser
struct  key_descriptor

Functions

static int _desktop_parser_handler (void *userdata_ptr, const char *section_ptr, const char *name_ptr, const char *value_ptr)
static char * _create_locale_key (const char *l, const char *t, const char *m)
static bool _desktop_parser_translate_type (const char *value_ptr, void *dest_ptr)
static bool _desktop_parser_translate_boolean (const char *value_ptr, void *dest_ptr)
static bool _desktop_parser_translate_string (const char *value_ptr, void *dest_ptr)
static bool _desktop_parser_translate_strings (const char *value_ptr, void *dest_ptr)
static bool _desktop_parser_translate_exec (const char *value_ptr, void *dest_ptr)
static void _desktop_parser_destroy_string (void *dest_ptr)
static void _desktop_parser_destroy_strings (void *dest_ptr)
struct desktop_parserdesktop_parser_create (const char *locale_ptr)
void desktop_parser_destroy (struct desktop_parser *parser)
int desktop_parser_file_to_entry (const struct desktop_parser *parser, const char *fname_ptr, struct desktop_entry *entry_ptr)
int desktop_parser_string_to_entry (const struct desktop_parser *parser, const char *string_ptr, struct desktop_entry *entry_ptr)
void desktop_parser_entry_release (struct desktop_entry *entry_ptr)
static void _desktop_parser_test_ini_string (bs_test_t *test_ptr)
static void _desktop_parser_test_ini_file (bs_test_t *test_ptr)
static void _desktop_parser_test_locale_string (bs_test_t *test_ptr)
static void _desktop_parser_test_translate (bs_test_t *test_ptr)

Variables

static const char * desktop_entry_group_name = "Desktop Entry"
static const char * key_regex_str
struct key_descriptor keys []
static const bs_test_case_t _desktop_parser_test_cases []
const bs_test_set_t desktop_parser_test_set

Detailed Description

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Function Documentation

◆ _create_locale_key()

char * _create_locale_key ( const char * l,
const char * t,
const char * m )
static

Creates a lookup key for localization.

Parameters
lLanguage ("lang"). If NULL, no string is created.
tTerritory (Freedesktop speficication: "COUNTRY"), may be NULL.
mModifier ("MODIFIER"), may be NULL.
Returns
Pointer to an allocated string, holding "lang[_COUNTRY][@MODIFIER]", or NULL on error, or NULL if l was NULL.

◆ _desktop_parser_destroy_string()

void _desktop_parser_destroy_string ( void * dest_ptr)
static

Frees the memory associated with the string.

◆ _desktop_parser_destroy_strings()

void _desktop_parser_destroy_strings ( void * dest_ptr)
static

Frees the memory associated with the NULL-terminated string array.

◆ _desktop_parser_handler()

int _desktop_parser_handler ( void * userdata_ptr,
const char * section_ptr,
const char * name_ptr,
const char * value_ptr )
static

Callback handler for the inih parse methods. Parses "Desktop Entry".

Parameters
userdata_ptr
section_ptr
name_ptr
value_ptr
Returns
0 on error, and 1 if the name/value was parsed (or skipped for legitimate reason).

◆ _desktop_parser_test_ini_file()

void _desktop_parser_test_ini_file ( bs_test_t * test_ptr)
static

Tests parsing a sample .desktop file.

◆ _desktop_parser_test_ini_string()

void _desktop_parser_test_ini_string ( bs_test_t * test_ptr)
static

Tests parsing INI content, but from a string.

◆ _desktop_parser_test_locale_string()

void _desktop_parser_test_locale_string ( bs_test_t * test_ptr)
static

Tests parsing localized strings.

◆ _desktop_parser_test_translate()

void _desktop_parser_test_translate ( bs_test_t * test_ptr)
static

Tests translators: Escaped string.

◆ _desktop_parser_translate_boolean()

bool _desktop_parser_translate_boolean ( const char * value_ptr,
void * dest_ptr )
static

Translates a boolean-typed value into a bool at *dest_ptr.

Parameters
value_ptrMust either be "false" or "true".
dest_ptrPointer to a bool.
Returns
true on success.

◆ _desktop_parser_translate_exec()

bool _desktop_parser_translate_exec ( const char * value_ptr,
void * dest_ptr )
static

Translates an exec key value, and un-escapes the specific escape codes.

https://specifications.freedesktop.org/desktop-entry/latest/exec-variables.html

TODO(kaese.nosp@m.r@gu.nosp@m.bbe.c.nosp@m.h): This is... lossy. When un-escaping the arguments, the result should be stored as separate strings, such as usable for execve(2).

Parameters
value_ptr
dest_ptr
Returns
true on success

◆ _desktop_parser_translate_string()

bool _desktop_parser_translate_string ( const char * value_ptr,
void * dest_ptr )
static

Translates a string, while un-escaping supported escape codes (s, n, t, r).

Parameters
value_ptr
dest_ptr
Returns
true on success.

◆ _desktop_parser_translate_strings()

bool _desktop_parser_translate_strings ( const char * value_ptr,
void * dest_ptr )
static

Translates multiple strings, separated by semicolon; as for "Categories".

Parameters
value_ptr
dest_ptr
Returns
true on success.

◆ _desktop_parser_translate_type()

bool _desktop_parser_translate_type ( const char * value_ptr,
void * dest_ptr )
static

Translates the "Type" key into an enum deskop_entry_type.

Parameters
value_ptr
dest_ptr
Returns
true on success.

◆ desktop_parser_create()

struct desktop_parser * desktop_parser_create ( const char * locale_ptr)

Creates a desktop parser, using the provided locale.

Parameters
locale_ptrLocale set for LC_MESSAGES. See setlocale(3).
Returns
Pointer to the desktop parser, or NULL on error. Must be destroyed by calling desktop_parser_destroy.

◆ desktop_parser_destroy()

void desktop_parser_destroy ( struct desktop_parser * parser)

Destroys the desktop parser.

Parameters
parser

◆ desktop_parser_entry_release()

void desktop_parser_entry_release ( struct desktop_entry * entry_ptr)

Releases the resources associated to the entry.

Parameters
entry_ptr

◆ desktop_parser_file_to_entry()

int desktop_parser_file_to_entry ( const struct desktop_parser * parser,
const char * fname_ptr,
struct desktop_entry * entry_ptr )

Parses a file into the provided entry.

Parameters
parser
fname_ptr
entry_ptr
Returns
0 on success, or the line number where the parser failed.

◆ desktop_parser_string_to_entry()

int desktop_parser_string_to_entry ( const struct desktop_parser * parser,
const char * string_ptr,
struct desktop_entry * entry_ptr )

Parses an in-memory string into the provided entry.

Parameters
parser
string_ptr
entry_ptr
Returns
0 on success, or the line number where the parser failed.

Variable Documentation

◆ _desktop_parser_test_cases

const bs_test_case_t _desktop_parser_test_cases[]
static
Initial value:
= {
{ true, "ini_string", _desktop_parser_test_ini_string },
{ true, "ini_file", _desktop_parser_test_ini_file },
{ true, "locale_string", _desktop_parser_test_locale_string },
{ true, "translate", _desktop_parser_test_translate },
}
static void _desktop_parser_test_locale_string(bs_test_t *test_ptr)
Definition desktop-parser.c:685
static void _desktop_parser_test_translate(bs_test_t *test_ptr)
Definition desktop-parser.c:743
static void _desktop_parser_test_ini_string(bs_test_t *test_ptr)
Definition desktop-parser.c:630
static void _desktop_parser_test_ini_file(bs_test_t *test_ptr)
Definition desktop-parser.c:655

Test cases for action items.

◆ desktop_entry_group_name

const char* desktop_entry_group_name = "Desktop Entry"
static

Group name for the application details in a .desktop file.

◆ desktop_parser_test_set

const bs_test_set_t desktop_parser_test_set
Initial value:
= BS_TEST_SET(
true, "desktop-parser", _desktop_parser_test_cases)
static const bs_test_case_t _desktop_parser_test_cases[]
Definition desktop-parser.c:617

Unit test set.

◆ key_regex_str

const char* key_regex_str
static
Initial value:
=
"^"
"([A-Za-z0-9-]+)"
"(\\[[a-z]{2,3}[a-zA-Z0-9_@\\.-]*\\])?"
"$"

Regular expression describing a (possibly localized) key.

◆ keys

struct key_descriptor keys[]

Supported keys.