NetSurf
Macros | Functions | Variables
nsoption.c File Reference

Option reading and saving (implementation). More...

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "netsurf/inttypes.h"
#include "netsurf/plot_style.h"
#include "utils/errors.h"
#include "utils/log.h"
#include "utils/utils.h"
#include "utils/nsoption.h"
#include "desktop/options.h"
Include dependency graph for nsoption.c:

Go to the source code of this file.

Macros

#define NSOPTION_MAX_LINE_LEN   1024
 Length of buffer used to read lines from input file. More...
 
#define NSOPTION_BOOL(NAME, DEFAULT)    { #NAME, sizeof(#NAME) - 1, OPTION_BOOL, { .b = DEFAULT } },
 
#define NSOPTION_STRING(NAME, DEFAULT)    { #NAME, sizeof(#NAME) - 1, OPTION_STRING, { .cs = DEFAULT } },
 
#define NSOPTION_INTEGER(NAME, DEFAULT)    { #NAME, sizeof(#NAME) - 1, OPTION_INTEGER, { .i = DEFAULT } },
 
#define NSOPTION_UINT(NAME, DEFAULT)    { #NAME, sizeof(#NAME) - 1, OPTION_UINT, { .u = DEFAULT } },
 
#define NSOPTION_COLOUR(NAME, DEFAULT)    { #NAME, sizeof(#NAME) - 1, OPTION_COLOUR, { .c = DEFAULT } },
 

Functions

static bool strtooption (const char *value, struct nsoption_s *option)
 Set an option value based on a string. More...
 
static void nsoption_validate (struct nsoption_s *opts, struct nsoption_s *defs)
 
static bool nsoption_is_set (const struct nsoption_s *opts, const struct nsoption_s *defs, const enum nsoption_e entry)
 Determines if an option is different between two option tables. More...
 
static nserror nsoption_output (FILE *fp, struct nsoption_s *opts, struct nsoption_s *defs, bool all)
 Output choices to file stream. More...
 
static size_t nsoption_output_value_html (struct nsoption_s *option, size_t size, size_t pos, char *string)
 Output an option value into a string, in HTML format. More...
 
static size_t nsoption_output_value_text (struct nsoption_s *option, size_t size, size_t pos, char *string)
 Output an option value into a string, in plain text format. More...
 
static nserror nsoption_dup (struct nsoption_s *src, struct nsoption_s **pdst)
 Duplicates an option table. More...
 
static nserror nsoption_free (struct nsoption_s *opts)
 frees an option table. More...
 
static nserror get_key_value (char *line, int linelen, char **key_out, char **value_out)
 extract key/value from a line of input More...
 
static nserror optionline (struct nsoption_s *opts, char *line, int linelen)
 Process a line from a user option file. More...
 
nserror nsoption_init (nsoption_set_default_t *set_defaults, struct nsoption_s **popts, struct nsoption_s **pdefs)
 Initialise option system. More...
 
nserror nsoption_finalise (struct nsoption_s *opts, struct nsoption_s *defs)
 Finalise option system. More...
 
nserror nsoption_read (const char *path, struct nsoption_s *opts)
 Read choices file and set them in the passed table. More...
 
nserror nsoption_write (const char *path, struct nsoption_s *opts, struct nsoption_s *defs)
 Write options that have changed from the defaults to a file. More...
 
nserror nsoption_dump (FILE *outf, struct nsoption_s *opts)
 Write all options to a stream. More...
 
nserror nsoption_commandline (int *pargc, char **argv, struct nsoption_s *opts)
 Process commandline and set options approriately. More...
 
int nsoption_snoptionf (char *string, size_t size, enum nsoption_e option_idx, const char *fmt)
 Fill a buffer with an option using a format. More...
 
nserror nsoption_set_tbl_charp (struct nsoption_s *opts, enum nsoption_e option_idx, char *s)
 Set string option in specified table. More...
 

Variables

struct nsoption_snsoptions = NULL
 global active option table. More...
 
struct nsoption_snsoptions_default = NULL
 global default option table. More...
 
static struct nsoption_s defaults []
 The table of compiled in default options. More...
 

Detailed Description

Option reading and saving (implementation).

Options are stored in the format key:value, one per line.

For bool options, value is "0" or "1".

Definition in file nsoption.c.

Macro Definition Documentation

◆ NSOPTION_BOOL

#define NSOPTION_BOOL (   NAME,
  DEFAULT 
)     { #NAME, sizeof(#NAME) - 1, OPTION_BOOL, { .b = DEFAULT } },

Definition at line 48 of file nsoption.c.

◆ NSOPTION_COLOUR

#define NSOPTION_COLOUR (   NAME,
  DEFAULT 
)     { #NAME, sizeof(#NAME) - 1, OPTION_COLOUR, { .c = DEFAULT } },

Definition at line 60 of file nsoption.c.

◆ NSOPTION_INTEGER

#define NSOPTION_INTEGER (   NAME,
  DEFAULT 
)     { #NAME, sizeof(#NAME) - 1, OPTION_INTEGER, { .i = DEFAULT } },

Definition at line 54 of file nsoption.c.

◆ NSOPTION_MAX_LINE_LEN

#define NSOPTION_MAX_LINE_LEN   1024

Length of buffer used to read lines from input file.

Definition at line 43 of file nsoption.c.

◆ NSOPTION_STRING

#define NSOPTION_STRING (   NAME,
  DEFAULT 
)     { #NAME, sizeof(#NAME) - 1, OPTION_STRING, { .cs = DEFAULT } },

Definition at line 51 of file nsoption.c.

◆ NSOPTION_UINT

#define NSOPTION_UINT (   NAME,
  DEFAULT 
)     { #NAME, sizeof(#NAME) - 1, OPTION_UINT, { .u = DEFAULT } },

Definition at line 57 of file nsoption.c.

Function Documentation

◆ get_key_value()

static nserror get_key_value ( char *  line,
int  linelen,
char **  key_out,
char **  value_out 
)
static

extract key/value from a line of input

\retun NSERROR_OK and key_out and value_out updated NSERROR_NOT_FOUND if not a key/value input line NSERROR_INVALID if the line is and invalid format (missing colon)

Definition at line 552 of file nsoption.c.

References nsoption_s::key, line(), NSERROR_INVALID, NSERROR_NOT_FOUND, NSERROR_OK, and nsoption_s::value.

Referenced by optionline().

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

◆ nsoption_commandline()

nserror nsoption_commandline ( int *  pargc,
char **  argv,
struct nsoption_s opts 
)

Process commandline and set options approriately.

Parameters
pargcPointer to the size of the argument vector.
argvThe argument vector.
optsThe options table to enerate values from or NULL to use global
Returns
The error status

Definition at line 824 of file nsoption.c.

References nsoption_s::key, NSERROR_BAD_PARAMETER, NSERROR_OK, NSLOG, NSOPTION_LISTEND, nsoption_validate(), nsoptions, nsoptions_default, and strtooption().

Referenced by gui_init_replicant(), main(), monkey_options_handle_command(), nsgtk_option_init(), and nsw32_option_init().

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

◆ nsoption_dump()

nserror nsoption_dump ( FILE *  outf,
struct nsoption_s opts 
)

Write all options to a stream.

Parameters
outfThe stream to write to
optsThe options table to enerate values from or NULL to use global
Returns
The error status

Definition at line 804 of file nsoption.c.

References NSERROR_BAD_PARAMETER, nsoption_output(), and nsoptions.

Referenced by ro_gui_signal().

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

◆ nsoption_dup()

static nserror nsoption_dup ( struct nsoption_s src,
struct nsoption_s **  pdst 
)
static

Duplicates an option table.

Allocates a new option table and copies an existing one into it.

Parameters
[in]srcThe source table to copy
[out]pdstThe output table
Returns
NSERROR_OK on success or appropriate error code.

Definition at line 489 of file nsoption.c.

References defaults, nsoption_s::key, NSERROR_NOMEM, NSERROR_OK, OPTION_STRING, nsoption_s::s, nsoption_s::type, and nsoption_s::value.

Referenced by nsoption_init().

Here is the caller graph for this function:

◆ nsoption_finalise()

nserror nsoption_finalise ( struct nsoption_s opts,
struct nsoption_s defs 
)

Finalise option system.

Releases all resources allocated in the initialisation.

Parameters
optsthe options table or NULL to use global table.
defsthe default options table to use or NULL to use global table return The error status

Definition at line 684 of file nsoption.c.

References NSERROR_OK, nsoption_free(), nsoptions, and nsoptions_default.

Referenced by main(), nsgtk_finalise(), and WinMain().

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

◆ nsoption_free()

static nserror nsoption_free ( struct nsoption_s opts)
static

frees an option table.

Iterates through an option table a freeing resources as required finally freeing the option table itself.

Parameters
optsThe option table to free.

Definition at line 522 of file nsoption.c.

References nsoption_s::key, NSERROR_BAD_PARAMETER, NSERROR_OK, OPTION_STRING, nsoption_s::s, nsoption_s::type, and nsoption_s::value.

Referenced by nsoption_finalise(), and nsoption_init().

Here is the caller graph for this function:

◆ nsoption_init()

nserror nsoption_init ( nsoption_set_default_t set_default,
struct nsoption_s **  popts,
struct nsoption_s **  pdefs 
)

Initialise option system.

Parameters
set_defaultcallback to allow the customisation of the default options.
poptspointer to update to get options table or NULL.
pdefspointer to update to get default options table or NULL.
Returns
The error status
Todo:
it would be better if the frontends actually set values in the passed in table instead of assuming the global one.

Definition at line 629 of file nsoption.c.

References defaults, NSERROR_OK, nsoption_dup(), nsoption_free(), nsoptions, nsoptions_default, and set_defaults().

Referenced by gui_init_replicant(), main(), nsgtk_option_init(), and nsw32_option_init().

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

◆ nsoption_is_set()

static bool nsoption_is_set ( const struct nsoption_s opts,
const struct nsoption_s defs,
const enum nsoption_e  entry 
)
static

Determines if an option is different between two option tables.

Parameters
optsThe first table to compare.
defsThe second table to compare.
entryThe option to compare.
Returns
true if the option differs false if not.

Definition at line 227 of file nsoption.c.

References nsoption_s::b, nsoption_s::c, nsoption_s::i, OPTION_BOOL, OPTION_COLOUR, OPTION_INTEGER, OPTION_STRING, OPTION_UINT, nsoption_s::s, type, nsoption_s::u, and nsoption_s::value.

Referenced by nsoption_output(), and nsoption_snoptionf().

Here is the caller graph for this function:

◆ nsoption_output()

static nserror nsoption_output ( FILE *  fp,
struct nsoption_s opts,
struct nsoption_s defs,
bool  all 
)
static

Output choices to file stream.

Parameters
fpThe file stream to write to.
optsThe options table to write.
defsThe default value table to compare with.
allOutput all entries not just ones changed from defaults

Definition at line 289 of file nsoption.c.

References nsoption_s::c, nsoption_s::key, NSERROR_OK, nsoption_is_set(), NSOPTION_LISTEND, OPTION_BOOL, OPTION_COLOUR, OPTION_INTEGER, OPTION_STRING, OPTION_UINT, nsoption_s::s, type, and nsoption_s::value.

Referenced by nsoption_dump(), and nsoption_write().

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

◆ nsoption_output_value_html()

static size_t nsoption_output_value_html ( struct nsoption_s option,
size_t  size,
size_t  pos,
char *  string 
)
static

Output an option value into a string, in HTML format.

Parameters
optionThe option to output the value of.
sizeThe size of the string buffer.
posThe current position in string
stringThe string in which to output the value.
Returns
The number of bytes written to string or -1 on error

Definition at line 356 of file nsoption.c.

References nsoption_s::b, nsoption_s::c, colour_rb_swap, colour_to_bw_furthest, nsoption_s::i, OPTION_BOOL, OPTION_COLOUR, OPTION_INTEGER, OPTION_STRING, OPTION_UINT, nsoption_s::s, nsoption_s::type, nsoption_s::u, and nsoption_s::value.

Referenced by nsoption_snoptionf().

Here is the caller graph for this function:

◆ nsoption_output_value_text()

static size_t nsoption_output_value_text ( struct nsoption_s option,
size_t  size,
size_t  pos,
char *  string 
)
static

Output an option value into a string, in plain text format.

Parameters
optionThe option to output the value of.
sizeThe size of the string buffer.
posThe current position in string
stringThe string in which to output the value.
Returns
The number of bytes written to string or -1 on error

Definition at line 429 of file nsoption.c.

References nsoption_s::b, nsoption_s::c, nsoption_s::i, OPTION_BOOL, OPTION_COLOUR, OPTION_INTEGER, OPTION_STRING, OPTION_UINT, nsoption_s::s, nsoption_s::type, nsoption_s::u, and nsoption_s::value.

Referenced by nsoption_snoptionf().

Here is the caller graph for this function:

◆ nsoption_read()

nserror nsoption_read ( const char *  path,
struct nsoption_s opts 
)

Read choices file and set them in the passed table.

Parameters
pathThe path to read the file from
optsThe options table to enerate values from or NULL to use global
Returns
The error status
Todo:
is this an API bug not being a parameter

Definition at line 717 of file nsoption.c.

References NSERROR_BAD_PARAMETER, NSERROR_NOT_FOUND, NSERROR_OK, NSLOG, NSOPTION_MAX_LINE_LEN, nsoption_validate(), nsoptions, nsoptions_default, optionline(), path(), and nsoption_s::s.

Referenced by ami_nsoption_read(), gui_init(), gui_init_replicant(), main(), nsgtk_option_init(), nsw32_option_init(), and save_settings().

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

◆ nsoption_set_tbl_charp()

nserror nsoption_set_tbl_charp ( struct nsoption_s opts,
enum nsoption_e  option_idx,
char *  s 
)

Set string option in specified table.

Sets the string option to the value given freeing any resources currently allocated to the option. If the passed string is empty it is converted to the NULL value.

Parameters
optsThe table to set option in
option_idxThe option
sThe string to set. This is used directly and not copied.

Definition at line 1014 of file nsoption.c.

References NSERROR_BAD_PARAMETER, NSERROR_OK, OPTION_STRING, nsoption_s::s, nsoption_s::type, and nsoption_s::value.

◆ nsoption_snoptionf()

int nsoption_snoptionf ( char *  string,
size_t  size,
enum nsoption_e  option,
const char *  fmt 
)

Fill a buffer with an option using a format.

The format string is copied into the output buffer with the following replaced: k - The options key t - The options type V - value (HTML formatting) v - value (plain formatting) p - provenance either "user" or "default"

Parameters
stringThe buffer in which to place the results.
sizeThe size of the string buffer.
optionThe option .
fmtThe format string.
Returns
The number of bytes written to string or -1 on error

Definition at line 898 of file nsoption.c.

References nsoption_s::key, min, nsoption_is_set(), NSOPTION_LISTEND, nsoption_output_value_html(), nsoption_output_value_text(), nsoptions, nsoptions_default, OPTION_BOOL, OPTION_COLOUR, OPTION_INTEGER, OPTION_STRING, OPTION_UINT, and nsoption_s::type.

Here is the call graph for this function:

◆ nsoption_validate()

static void nsoption_validate ( struct nsoption_s opts,
struct nsoption_s defs 
)
static

Definition at line 145 of file nsoption.c.

References nsoption_s::c, nsoption_s::i, nslog_set_filter_by_options(), NSOPTION_SYS_COLOUR_END, NSOPTION_SYS_COLOUR_START, nsoption_s::u, and nsoption_s::value.

Referenced by nsoption_commandline(), and nsoption_read().

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

◆ nsoption_write()

nserror nsoption_write ( const char *  path,
struct nsoption_s opts,
struct nsoption_s defs 
)

Write options that have changed from the defaults to a file.

The nsoption_dump can be used to output all entries not just changed ones.

Parameters
pathThe path to read the file from
optsThe options table to enerate values from or NULL to use global
defsThe default table to use or NULL to use global
Returns
The error status

Definition at line 763 of file nsoption.c.

References NSERROR_BAD_PARAMETER, NSERROR_NOT_FOUND, NSLOG, nsoption_output(), nsoptions, nsoptions_default, and path().

Referenced by ami_nsoption_write(), menu_savewin(), nsgtk_preferences_dialogPreferences_deleteevent(), nsgtk_preferences_dialogPreferences_destroy(), nsgtk_preferences_dialogPreferences_response(), nsgtk_toolbar_customisation_save(), nsws_prefs_save(), ro_gui_save_options(), save_settings(), and savewindowsize_button_clicked_cb().

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

◆ optionline()

static nserror optionline ( struct nsoption_s opts,
char *  line,
int  linelen 
)
static

Process a line from a user option file.

Definition at line 603 of file nsoption.c.

References get_key_value(), nsoption_s::key, line(), NSERROR_OK, strtooption(), and nsoption_s::value.

Referenced by nsoption_read().

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

◆ strtooption()

static bool strtooption ( const char *  value,
struct nsoption_s option 
)
static

Set an option value based on a string.

Definition at line 97 of file nsoption.c.

References nsoption_s::b, nsoption_s::c, nsoption_s::i, OPTION_BOOL, OPTION_COLOUR, OPTION_INTEGER, OPTION_STRING, OPTION_UINT, nsoption_s::s, nsoption_s::type, nsoption_s::u, and nsoption_s::value.

Referenced by nsoption_commandline(), and optionline().

Here is the caller graph for this function:

Variable Documentation

◆ defaults

struct nsoption_s defaults[]
static
Initial value:
= {
{ NULL, 0, OPTION_INTEGER, { 0 } }
}
@ OPTION_INTEGER
Option is an integer.
Definition: nsoption.h:104

The table of compiled in default options.

Definition at line 64 of file nsoption.c.

Referenced by nsoption_dup(), nsoption_init(), and set_defaults().

◆ nsoptions

struct nsoption_s* nsoptions = NULL

◆ nsoptions_default

struct nsoption_s* nsoptions_default = NULL