libgeda: Don't double free path argument
[geda-gaf.git] / docs / specifications / config-api.txt
blobe8d6eefc3e9c82c8eeed8b607b886b1316200962
1 ===============================
2  gEDA Configuration System API
3 ===============================
5 :Author: Peter Brett
6 :Status: Proposed Specification
8 ..
9   This document can be processed with docutils <http://docutils.sf.net>
10   to generate HTML or LaTeX.  However, the markup is designed to be
11   fully human-readable as-is.
13 .. contents::
15     1  Introduction
16     2  Error Handling
17       2.1  Errors in C functions
18       2.2  Errors in Scheme functions
19     3  Configuration Contexts
20       3.1  Obtaining a Context
21         3.1.1  Default context
22         3.1.2  System context
23         3.1.3  User context
24       3.2  Loading configuration files
25       3.3  Saving configuration files
26       3.4  Context parents
27       3.5  Context trust
28     4  Configuration Parameters
29       4.1  Groups
30       4.2  Keys
31       4.3  Configuration inheritance
32       4.4  Values
33         4.4.1  Getting configuration values
34         4.4.2  Setting configuration values
35     5  Configuration Events
36       5.1  Event propagation
37       5.2  Handlers
38       5.3  Delaying events
39     6  References
41 Introduction
42 ============
44 This document outlines the API provided by libgeda for configuration
45 of both libgeda and applications based on it.  The API is designed for
46 not only saving and loading static configuration data to and from
47 permanent storage, but also to allow on-line event-driven updates to
48 the configuration state.  The API is designed to be equally usable
49 from C code and from Scheme extensions.  It is heavily inspired by the
50 ``GKeyFile`` API from GLib, with a few changes and enhancements
51 [GLIB]_.
53 Error Handling
54 ==============
56 Errors in C functions
57 ---------------------
59 Recoverable errors that occur in C functions are signalled to the
60 caller using the "GError" mechanism [GLIB]_.
62 - File errors (e.g. "Access denied" or "File not found") are indicated
63   using the ``G_IO_ERROR`` error domain.  See the GIO manual for
64   information on error codes.  Functions that may cause a
65   ``G_IO_ERROR`` are clearly noted as such.
67 - All other errors are indicated using the ``EDA_CONFIG_ERROR`` error
68   domain.  The possible error codes are:
70   ``EDA_CONFIG_ERROR_UNKNOWN_ENCODING``
71     The text being parsed was in an unknown encoding.
73   ``EDA_CONFIG_ERROR_PARSE``
74     The configuration data was ill-formed.
76   ``EDA_CONFIG_ERROR_KEY_NOT_FOUND``
77     A requested configuration key was not found.
79   ``EDA_CONFIG_ERROR_GROUP_NOT_FOUND``
80     A requested configuration group was not found.
82   ``EDA_CONFIG_ERROR_INVALID_VALUE``
83     A configuration value could not be parsed into the requested
84     format.
86 Errors in Scheme functions
87 --------------------------
89 All errors in Scheme functions are reported by raising Guile errors
90 [GUILE]_.  There are two possible error keys that may arise:
92 - File errors (e.g. "Access denied" or "File not found") are indicated
93   with the ``system-error`` key.
95 - All other errors are indicated using the ``config-error`` key.  The
96   ``data`` part of the error arguments is a list containing one of the
97   following symbols:
99   ``unknown-encoding``
100     The text being parsed was in an unknown encoding.
102   ``parse``
103     The configuration data was ill-formed.
105   ``key-not-found``
106     A requested configuration key was not found.
108   ``group-not-found``
109     A requested configuration group was not found.
111   ``invalid-value``
112     A configuration value could not be parsed into the requested
113     format.
115 Configuration Contexts
116 ======================
118 A configuration parameter is always evaluated within a *configuration
119 context*.  Each context is associated with a configuration file
120 (although the file does not necessarily need to exist).
122 Each configuration context may have a *parent context*.  If, when
123 looking up a parameter, it has no value set in the selected context,
124 the parent context is checked, and so on.
126 Configuration contexts are reference counted.  You can increment and
127 decrement their reference counters using ``g_object_ref()`` and
128 ``g_object_unref()`` [GOBJECT]_.
130 Three special contexts are always automatically defined: the `default
131 context`_, the `system context`_, and the `user context`_.  The user
132 context is the default parent context for newly-created configuration
133 contexts.
135 Obtaining a Context
136 -------------------
138 C function::
140   EdaConfig *eda_config_get_context_for_path (const gchar *path,
141                                               GError *error)
143 Scheme function::
145   path-config-context path
147 Normally, you shouldn't create a configuration context directly; you
148 should obtain the configuration context associated with a ``path``.
149 If a context matching the path does not yet exist, it is created.
151 ``path-config-context`` looks for a configuration file named
152 ``geda.conf``.  If ``path`` is not a directory, it is truncated, and
153 then a file named ``geda.conf`` is looked for in that directory.  If
154 none is found, the parent directory is checked, and so on until a
155 configuration file is found or the filesystem root is reached.  If no
156 configuration file was found, the returned context will be associated
157 with a ``geda.conf`` in the same directory as ``path``.
159 .. warning:: Do not assume that the configuration file associated with
160              the context returned by ``path-config-context`` is
161              located in the directory specified by ``path``.
163 The configuration context returned by
164 ``eda_config_get_context_for_path()`` is owned by the library, and
165 should not be disposed of with ``g_object_unref()``.
168 Default context
169 ~~~~~~~~~~~~~~~
171 C function::
173   EdaConfig *eda_config_get_default_context ()
175 Scheme function::
177   default-config-context
179 The default context is not associated with any physical path or
180 on-disk configuration file, and has no parent context.  It contains
181 the default configuration used when no configuration file can be
182 loaded.  Applications should normally populate the default context
183 with their built-in default configuration settings on start-up, before
184 loading any further configuration files.
186 The configuration context returned by
187 ``eda_config_get_default_context()`` is owned by the library, and
188 should not be disposed of with ``g_object_unref()``.
190 System context
191 ~~~~~~~~~~~~~~
193 C function::
195   EdaConfig *eda_config_get_system_context ()
197 Scheme function::
199   system-config-context
201 The system context is used for system-wide configuration.  It is located:
203 1. By searching ``${XDG_CONFIG_DIRS}`` for a ``gEDA/geda-system.conf``
204    configuration file.
206 2. By checking ``${sysconfdir}/gEDA`` for a configuration file.
208 Its parent context is the `default context`_.
210 The configuration context returned by
211 ``eda_config_get_system_context()`` is owned by the library, and
212 should not be disposed of with ``g_object_unref()``.
214 User context
215 ~~~~~~~~~~~~
217 C function::
219   EdaConfig *eda_config_get_user_context ()
221 Scheme function::
223   user-config-context
225 The user context is used for user-specific configuration, and is
226 loaded from ``${XDG_CONFIG_HOME}/gEDA/geda-user.conf`` [XDGDIRS]_.
227 Its parent context is the `system context`_.
229 The configuration context returned by
230 ``eda_config_get_user_context()`` is owned by the library, and should
231 not be disposed of with ``g_object_unref()``.
233 Loading configuration files
234 ---------------------------
236 Other than the `default context`_, all configuration contexts are
237 associated with an on-disk configuration file.
239 C function::
241   const gchar *eda_config_get_filename (EdaConfig *cfg)
243 Scheme function::
245   config-filename cfg
247 Return the filename of the configuration file associated with the
248 context ``cfg``.  For some contexts (including the `default
249 context`_), this will return ``NULL`` (in C) or ``#f`` (in Scheme).
251 C function::
253   gboolean eda_config_load (EdaConfig *cfg, GError **err)
255 Scheme function::
257   load-config! cfg
259 Attempt to load configuration parameters for the context ``cfg`` from
260 its associated file.  This function may generate a ``GIOError`` (in
261 C) or a ``system-error`` (in Scheme).
263 C function::
265   gboolean eda_config_is_loaded (EdaConfig *cfg)
267 Scheme function::
269   config-loaded? cfg
271 Determine whether the context ``cfg`` has been successfully loaded
272 from disk.
274 Saving configuration files
275 --------------------------
277 C function::
279   gboolean eda_config_save (EdaConfig *cfg, GError *error)
281 Scheme function::
283   save-config! cfg
285 Attempt to save configuration parameters for the context ``cfg`` to
286 its associated file.  This function may generate a ``GIOError`` (in
287 C) or a ``system-error`` (in Scheme).
289 C function::
291   gboolean eda_config_is_changed (EdaConfig *cfg)
293 Scheme function::
295   config-changed? cfg
297 Determine whether the context ``cfg`` has been altered since it was
298 last synchronised with the on-disk version by loading or saving it.
300 Context parents
301 ---------------
303 C function::
305   EdaConfig *eda_config_get_parent (EdaConfig *cfg)
307 Scheme function::
309   config-parent cfg
311 Return the parent context of the context ``cfg``, if it has one.
313 C function::
315   void eda_config_set_parent (EdaConfig *cfg, EdaConfig *parent)
317 Scheme function::
319   set-config-parent! cfg parent
321 Sets ``parent`` as the parent context of ``cfg``.  If ``parent`` is
322 NULL or ``#f``, sets ``cfg`` as having no parent context.
324 .. note:: Normally, application code should avoid using this function;
325           keeping to the default configuration inheritance structure
326           is recommended in order to ensure consistent behaviour of
327           all libgeda applications.
329 Context trust
330 -------------
332 Some configuration parameters are dangerous; in particular, parameters
333 that may lead to arbitrary code execution need to be handled
334 carefully.  Such settings might include:
336 - Preferred PDF reader
337 - Preferred web browser
338 - Search path for Scheme plugins
340 Configuration contexts can be flagged as being 'trusted'.  This allows
341 code that needs to access such dangerous parameters to determine
342 whether the value has been obtained from a safe source.
344 By default, the `default context`_, `system context`_ and `user
345 context`_ are trusted, and all other contexts untrusted.
347 C function::
349   gboolean eda_config_is_trusted (EdaConfig *cfg)
351 Scheme function::
353   config-trusted? cfg
355 Test whether ``cfg`` is a trusted configuration context.
357 C function::
359   void eda_config_set_trusted (EdaConfig *cfg, gboolean trusted)
361 Scheme function::
363   set-config-trusted! cfg trusted
365 Set whether the configuration context ``cfg`` should be trusted as a
366 source for dangerous configuration parameters.
368 .. warning:: You should not set a configuration context as trusted
369              unless you are certain that it originated from a safe
370              source (e.g. by interacting with the user to verify it).
372 C function::
374   EdaConfig *eda_config_get_trusted_context (EdaConfig *cfg)
376 Scheme function::
378   config-trusted-context cfg
380 If ``cfg`` is trusted, returns ``cfg``; otherwise, returns the first
381 parent context of the configuration context ``cfg`` that is a trusted
382 context.  If no trusted context could be found, returns NULL or
383 ``#f``.
385 Configuration Parameters
386 ========================
388 A gEDA/gaf *configuration parameter* consists of three components:
390 Group
391   A UTF-8 string which identifies the general category in which the
392   parameter lies (e.g. which application).
394 Name
395   A UTF-8 string which specifically identifies the parameter within
396   the group.
398 Value
399   The value of the parameter.  This is stored as a UTF-8 string, but
400   can be converted to a number of possible scalar and list types.
402 Groups, names and values are all case-sensitive.
404 Groups
405 ------
407 C function::
409   gchar **eda_config_get_groups (EdaConfig *cfg, gsize *length)
411 Scheme function::
413   config-groups cfg
415 Returns a list of all groups available in ``cfg`` and its parent
416 contexts.  The value returned by ``eda_config_get_groups()`` is a
417 newly-allocated NULL-terminated array of strings.  Use
418 ``g_strfreev()`` to free it [GLIB_].  The ``length`` argument is an
419 optional return location for the number of groups returned.
421 C function::
423   gboolean eda_config_has_group (EdaConfig *cfg, const gchar *group)
425 Scheme function::
427   config-has-group? cfg group
429 Test whether ``cfg`` or its parent contexts contain the specified
430 ``group``.
432 Keys
433 ----
435 C function::
437   gchar **eda_config_get_keys (EdaConfig *cfg, const gchar *group,
438                                gsize *length, GError **error)
440 Scheme function::
442   config-keys cfg group
444 Returns a list of all keys available in the specified ``group`` in
445 ``cfg`` and its parent contexts.  The value returned by
446 ``eda_config_get_keys()`` is a newly-allocated NULL-terminated array
447 of strings.  Use ``g_strfreev()`` to free it [GLIB_].  The ``length``
448 argument is an optional return location for the number of keys
449 returned.  If neither ``cfg`` nor any of its parent contexts contain
450 ``group``, raises an ``EdaConfigError`` (in C) or a ``config-error``
451 (in Scheme).
453 C function::
455   gboolean eda_config_has_key (EdaConfig *cfg, const gchar *group,
456                                const gchar *key, GError **err)
458 Scheme function::
460   config-has-key? cfg group key
462 Test whether ``cfg`` or its parent contexts contains ``key`` in the
463 specified ``group``.
465 Configuration inheritance
466 -------------------------
468 C function::
470   gboolean eda_config_is_inherited (EdaConfig *cfg, const gchar *group,
471                                     const gchar *key, GError **err)
473 Scheme function::
475   config-inherited? cfg group key
477 Tests whether the value of the configuration parameter with the given
478 ``group`` and ``key`` is specified in the context ``cfg``, or whether
479 it is inherited from a parent context of ``cfg``.
481 C function::
483   EdaConfig *eda_config_get_source (EdaConfig *cfg, const gchar *group,
484                                     const gchar *key, GError **err)
486 Scheme function::
488   config-source group key
490 Returns the configuration context (either ``cfg`` or one of its parent
491 contexts) in which the configuration parameter with the given
492 ``group`` and ``key`` has a value specified.
495 Values
496 ------
498 Each value is stored as a UTF-8 string in the configuration file.
499 However, this string can be parsed as several different types.  All of
500 the following types are supported:
502 - UTF-8 strings
503 - Booleans
504 - Integers
505 - Double-precision floating point numbers
507 In addition, lists of all of the above are supported.
509 Getting configuration values
510 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512 In general, errors in obtaining a value using the C functions should
513 be checked for using a GError.  The list functions take a return
514 location for the ``length`` of list (for
515 ``eda_config_get_string_list()`` this argument may be NULL, since the
516 returned array is NULL-terminated).  Returned strings should be freed
517 using ``g_free()``, and returned lists should be freed using
518 ``g_free()`` or ``g_strfreev()`` as appropriate [GLIB]_.
520 C functions::
522   gchar *eda_config_get_string (EdaConfig *cfg, const gchar *group, const gchar *key,
523                                 GError **err)
525   gboolean eda_config_get_boolean (EdaConfig *cfg, const gchar *group, const gchar *key,
526                                    GError **err)
528   gint eda_config_get_int (EdaConfig *cfg, const gchar *group, const gchar *key,
529                            GError **err)
531   gdouble eda_config_get_double (EdaConfig *cfg, const gchar *group, const gchar *key,
532                                  GError **err)
534   gchar **eda_config_get_string_list (EdaConfig *cfg, const gchar *group, const gchar *key,
535                                       gsize *length, GError **err)
537   gboolean *eda_config_get_boolean_list (EdaConfig *cfg, const gchar *group, const gchar *key,
538                                          gsize *length, GError **err)
540   gint *eda_config_get_int_list (EdaConfig *cfg, const gchar *group, const gchar *key,
541                                  gsize *length, GError **err)
543   gdouble *eda_config_get_double_list (EdaConfig *cfg, const gchar *group, const gchar *key,
544                                        gsize *length, GError **err)
546 Scheme functions::
548   config-string cfg group key [default]
549   config-boolean cfg group key [default]
550   config-int cfg group key [default]
551   config-real cfg group key [default]
552   config-string-list cfg group key [default]
553   config-boolean-list cfg group key [default]
554   config-int-list cfg group key [default]
555   config-real-list cfg group key [default]
557 If a ``default`` argument is specified, it is returned in lieu of
558 raising a ``config-error`` exception if an error occurs.
560 All values returned by ``config-real`` and ``config-real-list`` are
561 real and inexact [GUILE]_, even if the on-disk representation would
562 permit an exact representation.
564 Setting configuration values
565 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
567 C functions::
569   void eda_config_set_string (EdaConfig *cfg, const gchar *group, const gchar *key,
570                               const gchar *value)
572   void eda_config_set_boolean (EdaConfig *cfg, const gchar *group, const gchar *key,
573                                gboolean value)
575   void eda_config_set_int (EdaConfig *cfg, const gchar *group, const gchar *key,
576                            gint value)
578   void eda_config_set_double (EdaConfig *cfg, const gchar *group, const gchar *key,
579                               gdouble value)
581   void eda_config_set_string_list (EdaConfig *cfg, const char *group, const gchar *key,
582                                    const gchar * const list[], gsize length)
584   void eda_config_set_boolean_list (EdaConfig *cfg, const gchar *group, const gchar *key,
585                                     gboolean list[], gsize length)
587   void eda_config_set_int_list (EdaConfig *cfg, const gchar *group, const gchar *key,
588                                 gint list[], gsize length)
590   void eda_config_set_double_list (EdaConfig *cfg, const gchar *group, const gchar *key,
591                                    gdouble list[], gsize length)
593 Scheme functions::
595   set-config! cfg group key value
597 The ``config-set!`` function infers the necessary type from the type
598 of its ``value`` argument.
600 Configuration Events
601 ====================
603 When the value of a configuration parameter is altered, either by
604 loading a file or directly via the API, a *configuration event* is
605 generated.  Handlers can be registered to be notified when a
606 configuration event occurs.  Handlers can match on:
608 - A particular group and key;
610 - A particular group;
612 - or on all parameters.
614 The events are also emitted as a GObject "config-changed" signal.
616 Event propagation
617 -----------------
619 Recall that configuration value look-ups "flow" from configuration
620 contexts to their parent contexts, i.e. up the configuration tree.  In
621 a similar manner, configuration events flow *down* the configuration
622 tree, from the context where the parameter was altered to any child
623 contexts which inherit that parameter's value.
625 For example, consider a context ``parent`` which has child contexts
626 ``child1`` and ``child2``.  In ``parent``, the parameter ``foo`` has
627 value ``bar``; in ``child1``, its value is ``baz``; and in ``child2``,
628 it is not set.
632                    parent  [foo=bar]
633                      |
634                     / \
635    [foo=baz]  child1   child2  [foo not set]
637 If the ``foo`` parameter is altered in ``parent``, then the resulting
638 event will be dispatched in ``parent`` and ``child2``.
640 Handlers
641 --------
643 In C, configuration event handlers must have the
644 ``EdaConfigEventHandlerFunc`` prototype::
646   void handler (EdaConfig *cfg, const gchar *group, const gchar *key, gpointer user_data)
648 In Scheme, a configuration event handler must be a closure that
649 accepts three arguments::
651   handler cfg group key
653 ``cfg`` is always the configuration context that received the event,
654 and the ``group`` and ``key`` identify the configuration parameter
655 that was changed.  The ``user_data`` argument to the C handler is a
656 data structure provided by the API user at the time when the handler
657 was registered.
659 Adding and removing handlers is quite straightforward:
661 C function::
663   void eda_config_event_add (EdaConfig *cfg, const gchar *group, const gchar *key, EdaConfigEventHandlerFunc func, gpointer user_data)
665 Scheme function::
667   add-config-event! cfg func [group [key]]
669 Registers ``func`` to be called when the configuration parameter
670 identified by ``group`` and ``key`` is modified in the context
671 ``cfg``.  If ``key`` is not specified (or NULL), matches on any
672 parameter in ``group``.  If group is not specified (or NULL), matches
673 on any parameter.  If an identical event handler was already
674 registered, does nothing.
676 C function::
678   void eda_config_event_remove (EdaConfig *cfg, const gchar *group, const gchar *key, EdaConfigEventHandlerFunc func, gpointer user_data)
680 Scheme function::
682   remove-config-event! cfg func [group [key]]
684 Unregisters the event handler ``func`` from the context ``cfg``.  The
685 ``group``, ``key``, and (in C) ``user_data`` parameters must match
686 those specified when the event handler was registered.  If no matching
687 event handler was registered, does nothing.
689 C function::
691   void eda_config_event_remove_all (EdaConfig *cfg, EdaConfigEventHandlerFunc func, gpointer user_data)
693 Scheme function::
695   remove-config-event! cfg func #t
697 Unregisters all occurrences of the event handler ``func`` from the
698 context ``cfg``, no matter what events ``func`` was set to trigger on.
699 In C, the ``user_data`` parameter must match that specified when the
700 event handler was registered.
703 Delaying events
704 ---------------
706 It is sometimes desirable to delay configuration event notifications.
707 For example, this might be necessary if you are planning to change a
708 large number of configuration values at the same time.
710 C functions::
712   void eda_config_event_freeze (EdaConfig *cfg)
713   void eda_config_event_thaw (EdaConfig *cfg)
715 When ``eda_config_event_freeze()`` is called on it, the context
716 ``cfg`` stops dispatching event notifications.  Whenever an event
717 notification would normally be dispatched, it instead is added to an
718 internal queue.  When ``eda_config_event_thaw()`` is called, any
719 queued events are dispatched.  Multiple pairs of
720 ``eda_config_event_freeze()`` and ``eda_config_event_thaw()`` calls
721 can be nested.
723 Scheme function::
725   delay-config-events cfg thunk
727 Call ``thunk``, delaying configuration event notifications until
728 ``thunk`` exits, either normally or non-locally.
731 References
732 ==========
734 .. [GLIB]    The GNOME Project, "GLib Reference Manual".
735              <http://library.gnome.org/devel/glib/stable/>
737 .. [GOBJECT] The GNOME Project, "GObject Reference Manual".
738              <http://library.gnome.org/devel/glib/stable/>
740 .. [GUILE]   Free Software Foundation, "GNU Guile Reference Manual", Version
741              1.8.
742              <http://www.gnu.org/software/guile/manual/>
744 .. [XDGDIRS] W. Bastian, R. Lortie and L. Poettering, "XDG Base
745              Directory Specification", Version 0.7.
746              <http://www.freedesktop.org/wiki/Standards/basedir-spec>
749    Local Variables:
750    mode: rst
751    End: