libgeda

s_clib.c File Reference

The component library system. More...

#include <config.h>
#include <missing.h>
#include <stdio.h>
#include <glib.h>
#include <time.h>
#include "libgeda_priv.h"
Include dependency graph for s_clib.c:

Go to the source code of this file.

Data Structures

struct  _CLibSource
struct  _CLibSymbol
struct  _CacheEntry

Defines

#define WIFSIGNALED(x)   0
#define WTERMSIG(x)   0
#define WIFEXITED(x)   1
#define WEXITSTATUS(x)   0
#define SYM_FILENAME_FILTER   ".sym"
#define CLIB_LIST_CMD   "list"
#define CLIB_DATA_CMD   "get"
#define CLIB_MAX_SYMBOL_CACHE   128
#define CLIB_MIN_SYMBOL_CACHE   96

Typedefs

typedef struct _CacheEntry CacheEntry

Enumerations

enum  CLibSourceType { CLIB_NONE = 0, CLIB_DIR, CLIB_CMD, CLIB_SCM }

Functions

static void free_symbol (gpointer data, gpointer user_data)
 Iterator callback for freeing a symbol.
static void free_symbol_cache_entry (gpointer data)
 Iterator callback for freeing a symbol cache entry.
static void free_source (gpointer data, gpointer user_data)
 Iterator callback for freeing a source.
static gint compare_source_name (gconstpointer a, gconstpointer b)
 Compare two component sources by name.
static gint compare_symbol_name (gconstpointer a, gconstpointer b)
 Compare two component symbols by name.
static void cache_find_oldest (gpointer key, gpointer value, gpointer user_data)
 Iterator callback for finding oldest symbol cache entry.
static gchar * run_source_command (const gchar *command)
 Execute a library command.
static CLibSymbolsource_has_symbol (const CLibSource *source, const gchar *name)
 Find any symbols within a source with a given name.
static gchar * uniquify_source_name (const gchar *name)
 Make sure a source name is unique.
static void refresh_directory (CLibSource *source)
 Rescan a directory for symbols.
static void refresh_command (CLibSource *source)
 Re-poll a library command for symbols.
static void refresh_scm (CLibSource *source)
 Re-poll a scheme procedure for symbols.
static gchar * get_data_directory (const CLibSymbol *symbol)
 Get symbol data from a directory source.
static gchar * get_data_command (const CLibSymbol *symbol)
 Get symbol data from a library command.
static gchar * get_data_scm (const CLibSymbol *symbol)
 Get symbol data from a Scheme-based component source.
void s_clib_init ()
 Initialise the component library.
void s_clib_free ()
 Free all memory used by the component library.
GList * s_clib_get_sources (const gboolean sorted)
 Get a list of available component sources.
void s_clib_refresh ()
 Rescan all available component libraries.
const CLibSources_clib_get_source_by_name (const gchar *name)
 Get a named component source.
const CLibSources_clib_add_directory (const gchar *directory, const gchar *name)
 Add a directory of symbol files to the library.
const CLibSources_clib_add_command (const gchar *list_cmd, const gchar *get_cmd, const gchar *name)
 Add symbol-generating commands to the library.
const CLibSources_clib_add_scm (SCM listfunc, SCM getfunc, const gchar *name)
 Add symbol-generating Scheme procedures to the library.
const gchar * s_clib_source_get_name (const CLibSource *source)
 Get the name of a source.
GList * s_clib_source_get_symbols (const CLibSource *source)
 Get a list of symbols available from a given source.
const gchar * s_clib_symbol_get_name (const CLibSymbol *symbol)
 Get the name of a symbol.
gchar * s_clib_symbol_get_filename (const CLibSymbol *symbol)
 Get a filename for editing a symbol.
const CLibSources_clib_symbol_get_source (const CLibSymbol *symbol)
 Get the source to which a symbol belongs.
gchar * s_clib_symbol_get_data (const CLibSymbol *symbol)
 Get symbol data.
GList * s_clib_search (const gchar *pattern, const CLibSearchMode mode)
 Find all symbols matching a pattern.
void s_clib_flush_search_cache ()
 Flush the symbol name lookup cache.
void s_clib_flush_symbol_cache ()
 Flush the symbol data cache.
void s_clib_symbol_invalidate_data (const CLibSymbol *symbol)
 Invalidate all cached data about a symbol.
const CLibSymbols_clib_get_symbol_by_name (const gchar *name)
 Get symbol structure for a given symbol name.
gchar * s_clib_symbol_get_data_by_name (const gchar *name)
 Get symbol data for a given symbol name.
GList * s_toplevel_get_symbols (const TOPLEVEL *toplevel)
 Get a list of symbols used.

Variables

static GList * clib_sources = NULL
static GHashTable * clib_search_cache = NULL
static GHashTable * clib_symbol_cache = NULL

Detailed Description

clib stands for component library.

The component library is made up of a number of component sources, each of which in turn makes available a number of component symbols. Each source may be either a directory on disk containing symbol files, a command in the system PATH which can generate gEDA symbol data (e.g. from a database), or a Scheme function which can do likewise. A component source is represented by a CLibSource instance.

The component library system manages component sources and symbols, and abstracts the interface to the underlying storage.

To initialise the component library, s_clib_init() is called. To clean up when it is no longer needed, s_clib_free() should be called.

A directory which contains one or more symbol files in gEDA format may be used as a component source. Each symbol file should have a filename ending in ".sym" (case insensitive). A component source based on a directory can be added using s_clib_add_directory(). Symbol files with filenames starting with a period "." are ignored.

An executable program in the system search path may be used as a component source, and it must conform with the specification given on page Library Commands. A component source based on a command may be added using s_clib_add_command().

Scheme functions may be used as a component source; for more information, please see page Library Scheme Procedures. A component source based on Scheme functions may be added using s_clib_add_scm().

Each symbol is identified by its name, which is stored in the saved schematic file. The name must be a valid for storage in a gEDA schematic file as the "basename" of a "component" object. For symbols from directory sources, the filename of the symbol is taken as the symbol name. For a command source, the name may be any permissible string. Guidelines to follow:

  1. Do not begin a symbol name with "EMBEDDED"
  2. Do not use whitespace, or any of the characters "<tt>/:!*?</tt>".
  3. Try to use unique names.

The component database may be queried using s_clib_search(). A null-terminated buffer containing symbol data (suitable for loading using o_read_buffer()) may be obtained using s_clib_symbol_get_data(). If an exact symbol name is known, the symbol data may be requested directly using s_clib_symbol_get_data_by_name().

Library Commands

A program or set of programs can be used as a component source. The procedure used to add such a source from a gEDA rc file is:

(component-library-command listcmd getcmd name)

This is implemented by g_rc_component_library_command(), which is a wrapper for s_clib_add_command().

The list command will be executed with no further arguments, and should output a list of available symbol names on stdout. The get command will have a symbol name appended to it as the final argument, and should output gEDA symbol data on stdout.

If the command cannot successfully complete, it should exit with non-zero exit status. Anything it has output on stdout will be ignored, and any stderr output displayed to the user.

Library Scheme Procedures

A set of Scheme procedures can be used as a component source. The procedure used to add such a source from a gEDA rc file is:

(component-library-funcs listfunc getfunc name)

This is implemented by g_rc_component_library_funcs(), which is a wrapper for s_clib_add_scm().

listfunc and getfunc must both be Guile procedures. listfunc takes no arguments, and returns a list of symbol names. getfunc takes a symbol name as an argument, and returns gEDA symbol data in a string, or #f if not known.

Definition in file s_clib.c.


Define Documentation

#define WIFSIGNALED (   x)    0

Definition at line 130 of file s_clib.c.

#define WTERMSIG (   x)    0

Definition at line 131 of file s_clib.c.

#define WIFEXITED (   x)    1

Definition at line 132 of file s_clib.c.

#define WEXITSTATUS (   x)    0

Definition at line 133 of file s_clib.c.

#define SYM_FILENAME_FILTER   ".sym"

All symbols in directory sources end with this string. Must be lowercase.

Definition at line 146 of file s_clib.c.

#define CLIB_LIST_CMD   "list"

Library command mode used to fetch list of symbols

Definition at line 149 of file s_clib.c.

#define CLIB_DATA_CMD   "get"

Library command mode used to fetch symbol data

Definition at line 152 of file s_clib.c.

#define CLIB_MAX_SYMBOL_CACHE   128

Maximum number of symbol cache entries

Definition at line 155 of file s_clib.c.

#define CLIB_MIN_SYMBOL_CACHE   96

When symbol cache gets full, remove down to this level

Definition at line 157 of file s_clib.c.


Typedef Documentation

typedef struct _CacheEntry CacheEntry

Symbol data cache entry

Definition at line 206 of file s_clib.c.


Enumeration Type Documentation

Valid types of component source

Enumerator:
CLIB_NONE 
CLIB_DIR 

Directory source

CLIB_CMD 

Command source

CLIB_SCM 

Guile function source

Definition at line 164 of file s_clib.c.


Function Documentation

static void free_symbol ( gpointer  data,
gpointer  user_data 
) [static]
Function Description
Private function used only in s_clib.c.

Definition at line 290 of file s_clib.c.

static void free_symbol_cache_entry ( gpointer  data) [static]
Function Description
Private function used only in s_clib.c.

Definition at line 309 of file s_clib.c.

static void free_source ( gpointer  data,
gpointer  user_data 
) [static]
Function Description
Private function used only in s_clib.c.

Definition at line 321 of file s_clib.c.

Here is the call graph for this function:

static gint compare_source_name ( gconstpointer  a,
gconstpointer  b 
) [static]
Function Description
Compare two component sources by name, case-insensitively. Typically used when calling g_list_sort(). Private function used only in s_clib.c. Argument order is as strcasecmp().
Parameters:
aFirst source to compare
bSecond source to compare
Returns:
As strcasecmp().

Definition at line 378 of file s_clib.c.

static gint compare_symbol_name ( gconstpointer  a,
gconstpointer  b 
) [static]
Function Description
Compare two component symbols by name, case-insensitively. Typically used when calling g_list_sort(). Private function used only in s_clib.c. Argument order is as strcasecmp().
Parameters:
aFirst symbol to compare
bSecond symbol to compare
Returns:
As strcasecmp().

Definition at line 403 of file s_clib.c.

static void cache_find_oldest ( gpointer  key,
gpointer  value,
gpointer  user_data 
) [static]
Function Description
Private function used only in s_clib.c.

Definition at line 421 of file s_clib.c.

static gchar * run_source_command ( const gchar *  command) [static]
Function Description
Execute a library command, returning the standard output, or NULL if the command fails for some reason. The system PATH is used to find the program to execute. The command can write messages to the standard error output. They are forwarded to the libgeda logging mechanism.

Private function used only in s_clib.c.

Todo:
This is probably generally useful.
Parameters:
commandCommand string to execute.
Returns:
The program's output, or NULL on failure.

Definition at line 448 of file s_clib.c.

static CLibSymbol * source_has_symbol ( const CLibSource source,
const gchar *  name 
) [static]
Function Description
Iterates through the symbol list of the given source, checking if there is already a symbol with the given name. If there is such a symbol, it is returned.
Parameters:
sourceThe source to check.
nameThe symbol name to look for.
Returns:
The matching symbol, or NULL if no match was found.

Definition at line 520 of file s_clib.c.

static gchar * uniquify_source_name ( const gchar *  name) [static]
Function Description
Checks if a source already exists with the given name. If one does, appends a number to the source name. If name is not already in use, returns it as is. The return value is always a newly-allocated string, and should be freed. it.

Definition at line 546 of file s_clib.c.

Here is the call graph for this function:

static void refresh_directory ( CLibSource source) [static]
Function Description
Rescans a directory for symbols.
Todo:
Does this need to do something more sane with subdirectories than just skipping them silently?

Private function used only in s_clib.c.

Definition at line 576 of file s_clib.c.

Here is the call graph for this function:

static void refresh_command ( CLibSource source) [static]
Function Description
Runs a library command, requesting a list of available symbols, and updates the source with the new list.

Private function used only in s_clib.c.

Definition at line 653 of file s_clib.c.

Here is the call graph for this function:

static void refresh_scm ( CLibSource source) [static]
Function Description
Calls a Scheme procedure to obtain a list of available symbols, and updates the source with the new list

Private function used only in s_clib.c.

Definition at line 716 of file s_clib.c.

Here is the call graph for this function:

static gchar * get_data_directory ( const CLibSymbol symbol) [static]
Function Description
Get symbol data from a directory data source. The return value should be free()'d when no longer needed.

Private function used only in s_clib.c.

Parameters:
symbolSymbol to get data for.
Returns:
Allocated buffer containing symbol data.

Definition at line 1068 of file s_clib.c.

static gchar * get_data_command ( const CLibSymbol symbol) [static]
Function Description
Get symbol data from a library command. The return value should be free()'d when no longer needed.

Private function used only in s_clib.c.

Parameters:
symbolSymbol to get data for.
Returns:
Allocated buffer containing symbol data.

Definition at line 1102 of file s_clib.c.

Here is the call graph for this function:

static gchar * get_data_scm ( const CLibSymbol symbol) [static]
Function Description
Get symbol data from a Scheme-based component source. The return value should be free()'d when no longer needed.

Private function used only in s_clib.c.

Parameters:
symbolSymbol to get data for.
Returns:
Allocated buffer containing symbol data.

Definition at line 1130 of file s_clib.c.

void s_clib_init ( void  )
Function Description
Resets and initialises the component library.
Warning:
This function must be called before any other functions from s_clib.c.

Definition at line 260 of file s_clib.c.

Here is the call graph for this function:

void s_clib_free ( void  )
Function Description
Should be called at program exit to clean up any remaining data being used by the component library system.

Definition at line 358 of file s_clib.c.

Here is the call graph for this function:

GList* s_clib_get_sources ( const gboolean  sorted)
Function Description
Gets the current list of sources.
Warning:
The GList returned should be freed when no longer needed. The returned value is not guaranteed to remain valid over calls to s_clib_add_directory() or s_clib_add_command().
Returns:
A GList of CLibSource.

Definition at line 501 of file s_clib.c.

Here is the call graph for this function:

void s_clib_refresh ( )
Function Description
Resets the list of symbols available from each source, and repopulates it from scratch. Useful e.g. for checking for new symbols.
Bug:
Disabled for now because it would break cached CLibSymbols used all over the place (e.g. in st_object).

Definition at line 779 of file s_clib.c.

Here is the call graph for this function:

const CLibSource* s_clib_get_source_by_name ( const gchar *  name)
Function Description
Iterates through the known component sources, checking if there is a source with the given name.
Parameters:
nameThe source name to look for.
Returns:
The matching source, or NULL if no match was found.

Definition at line 817 of file s_clib.c.

const CLibSource* s_clib_add_directory ( const gchar *  directory,
const gchar *  name 
)
Function Description
Adds a directory containing symbol files to the library. Only files ending with SYM_FILENAME_FILTER are considered to be symbol files. A name may be specified for the source; if name is NULL, the basename of the directory as returned by g_path_get_basename() is used.
Parameters:
directoryThe path of the directory to add.
nameA descriptive name for the directory.
Returns:
The CLibSource associated with the directory.

Definition at line 847 of file s_clib.c.

Here is the call graph for this function:

const CLibSource* s_clib_add_command ( const gchar *  list_cmd,
const gchar *  get_cmd,
const gchar *  name 
)
Function Description
Adds a set of commands which can generate symbols to the library. list_cmd and get_cmd should be strings consisting of an executable name followed by any arguments required. Executables are resolved using the current PATH. See page Library Commands for more information on library commands.
Parameters:
list_cmdThe executable & arguments used to list available symbols.
get_cmdThe executable & arguments used to retrieve symbol data.
nameA descriptive name for the component source.
Returns:
The CLibSource associated with the component source.

Definition at line 893 of file s_clib.c.

Here is the call graph for this function:

const CLibSource* s_clib_add_scm ( SCM  listfunc,
SCM  getfunc,
const gchar *  name 
)
Function Description
Adds a source to the library based on Scheme procedures. See page Library Scheme Procedures for more information. Two procedures are required: listfunc must return a Scheme list of symbol names, and getfunc must return a string containing symbol data when passed a symbol name.
Parameters:
listfuncA Scheme function returning a list of symbols.
getfuncA Scheme function returning symbol data.
nameA descriptive name for the component source.
Returns:
The new CLibSource.

Definition at line 942 of file s_clib.c.

Here is the call graph for this function:

const gchar* s_clib_source_get_name ( const CLibSource source)
Function Description
Get the name of a source for use e.g. in displaying a GUI.
Parameters:
sourceSource to be examined.
Returns:
Name of source.

Definition at line 981 of file s_clib.c.

GList* s_clib_source_get_symbols ( const CLibSource source)
Function Description
Get a GList containing all of the symbols available from source.
Warning:
The returned GList will not be consistent over a call to s_clib_refresh(). It should be freed when no longer needed.
Parameters:
sourceSource to be examined.
Returns:
A GList of CLibSymbol.

Definition at line 998 of file s_clib.c.

const gchar* s_clib_symbol_get_name ( const CLibSymbol symbol)
Function Description
Get the name of a symbol. The symbol name uniquely identifies it to libgeda.
Parameters:
symbolSymbol to be examined.
Returns:
Name of symbol.

Definition at line 1013 of file s_clib.c.

gchar* s_clib_symbol_get_filename ( const CLibSymbol symbol)
Function Description
Get the filename of the file a symbol was loaded from, if possible (e.g. to allow loading for user editing).
Warning:
The returned string should be freed when no longer needed.
Todo:
This is hack until there is a way to edit documents in gschem which do not have a file in the filesystem associated with them.
Deprecated:
This function is a temporary workaround.
Parameters:
symbolSymbol to be examined.
Returns:
Filename of symbol.

Definition at line 1036 of file s_clib.c.

const CLibSource* s_clib_symbol_get_source ( const CLibSymbol symbol)
Function Description
Get the source which a symbol is associated.
Parameters:
symbolSymbol to be examined.
Returns:
Source which owns symbol.

Definition at line 1052 of file s_clib.c.

gchar* s_clib_symbol_get_data ( const CLibSymbol symbol)
Function Description
Get the unparsed gEDA-format data corresponding to a symbol from the symbol's data source. The return value should be free()'d when no longer needed.

On failure, returns NULL (the error will be logged).

Parameters:
symbolSymbol to get data for.
Returns:
Allocated buffer containing symbol data.

Definition at line 1168 of file s_clib.c.

Here is the call graph for this function:

GList* s_clib_search ( const gchar *  pattern,
const CLibSearchMode  mode 
)
Function Description
Searches the library, returning all symbols whose names match pattern.

Two search modes are available: CLIB_EXACT, where pattern is compared to the symbol name using strcmp(), and CLIB_GLOB, where pattern is assumed to be a glob pattern (see the GLib documentation for details of the glob syntax applicable).

Warning:
The CLibSymbol instances in the GList returned belong to the component library, and should be considered constants; they should not be manipulated or free()'d. On the other hand, the GList returned must be freed with g_list_free() when no longer needed. Note that the values returned will be invalidated by a call to s_clib_free() or s_clib_refresh().
Parameters:
patternThe pattern to match against.
modeThe search mode to use.
Returns:
A GList of matching CLibSymbol structures.

Definition at line 1251 of file s_clib.c.

void s_clib_flush_search_cache ( )
Function Description
Clears the hashtable which caches the results of s_clib_search(). You shouldn't ever need to call this, as all functions which invalidate the cache are supposed to make sure it's flushed.

Definition at line 1339 of file s_clib.c.

void s_clib_flush_symbol_cache ( )
Function Description
Clears the hashtable which caches the results of s_clib_symbol_get_data(). You shouldn't ever need to call this, as all functions which invalidate the cache are supposed to make sure it's flushed.

Definition at line 1351 of file s_clib.c.

void s_clib_symbol_invalidate_data ( const CLibSymbol symbol)
Function Description
Removes all cached symbol data for symbol.
Parameters:
symbolSymbol to flush cached data for.

Definition at line 1363 of file s_clib.c.

const CLibSymbol* s_clib_get_symbol_by_name ( const gchar *  name)
Function Description
Return the first symbol found with the given name. If more than one matching symbol is found or no matches are found at all, emits a log message warning the user.
Parameters:
nameThe symbol name to match against.
Returns:
The first matching symbol, or NULL if none found.

Definition at line 1377 of file s_clib.c.

Here is the call graph for this function:

gchar* s_clib_symbol_get_data_by_name ( const gchar *  name)
Function Description
Return the data for the first symbol found with the given name. This is a helper function for the schematic load system, as it will always want to load symbols given only their name.

On failure, returns NULL (the error will be logged).

Parameters:
nameThe symbol name to match against.
Returns:
Allocated buffer containing symbol data.

Definition at line 1412 of file s_clib.c.

Here is the call graph for this function:

GList* s_toplevel_get_symbols ( const TOPLEVEL toplevel)
Function Description

Scan a TOPLEVEL structure's object list looking for symbols, and return them in a list.

Warning:
The CLibSymbol instances in the GList returned belong to the component library, and should be considered constants; they should not be manipulated or free()'d. On the other hand, the GList returned must be freed with g_list_free() when no longer needed. Note that the values returned will be invalidated by a call to s_clib_free() or s_clib_refresh().
Bug:
Only includes components which are not embedded, but they should (probably) also appear in the list.
Parameters:
toplevelTOPLEVEL structure to scan.
Returns:
GList of symbols.

Definition at line 1440 of file s_clib.c.

Here is the call graph for this function:


Variable Documentation

GList* clib_sources = NULL [static]

Holds the list of all known component sources

Definition at line 221 of file s_clib.c.

GHashTable* clib_search_cache = NULL [static]

Caches results of s_clib_search(). The key of the hashtable is a string describing the search that was carried out, and the value is a list of symbol pointers.

Definition at line 226 of file s_clib.c.

GHashTable* clib_symbol_cache = NULL [static]

Caches symbol data. The key of the hashtable is a symbol pointer, and the value is a CacheEntry structure containing the data and the time it was last used.

Definition at line 231 of file s_clib.c.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines