aboutsummaryrefslogtreecommitdiffhomepage
path: root/function.c
blob: 385c8c898303d820590b340d6e0295dbd23f1fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/** \file function.c
  Functions for storing and retrieving function information.
*/
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>


#include "config.h"
#include "util.h"
#include "function.h"
#include "proc.h"
#include "parser.h"
#include "common.h"
#include "intern.h"

/**
   Table containing all functions
*/
static hash_table_t function;

/**
   Struct describing a function
*/
typedef struct
{
	/** Function definition */
	wchar_t *cmd;
	/** Function description */
	wchar_t *desc;	
}
function_data_t;

/**
   Free all contents of an entry to the function hash table
*/
static void clear_function_entry( const void *key, 
							   const void *data )
{
	function_data_t *d = (function_data_t *)data;
	free( (void *)d->cmd );
	free( (void *)d->desc );
	free( (void *)d );
}

void function_init()
{
	hash_init( &function,
			   &hash_wcs_func,
			   &hash_wcs_cmp );
}

void function_destroy()
{
	hash_foreach( &function, &clear_function_entry );
	hash_destroy( &function );
}

void function_add( const wchar_t *name, 
				   const wchar_t *val,
				   const wchar_t *desc )
{
	if( function_exists( name ) )
		function_remove( name );
	
	function_data_t *d = malloc( sizeof( function_data_t ) );
	d->cmd = wcsdup( val );
	d->desc = desc?wcsdup( desc ):0;
	hash_put( &function, intern(name), d );
}

int function_exists( const wchar_t *cmd )
{
	return (hash_get(&function, cmd) != 0 );
}

void function_remove( const wchar_t *name )
{
	void *key;
	function_data_t *d;

	hash_remove( &function,
				 name,
				 (const void **) &key,
				 (const void **)&d );

	if( !d )
		return;

	clear_function_entry( key, d );
}
	
const wchar_t *function_get_definition( const wchar_t *argv )
{
	function_data_t *data = 
		(function_data_t *)hash_get( &function, argv );
	if( data == 0 )
		return 0;
	return data->cmd;
}
	
const wchar_t *function_get_desc( const wchar_t *argv )
{
	function_data_t *data = 
		(function_data_t *)hash_get( &function, argv );
	if( data == 0 )
		return 0;
	
	return data->desc?data->desc:data->cmd;
}

void function_set_desc( const wchar_t *name, const wchar_t *desc )
{
	function_data_t *data = 
		(function_data_t *)hash_get( &function, name );
	if( data == 0 )
		return;

	data->desc =wcsdup(desc);
}

/**
   Helper function for removing hidden functions 
*/
static void get_names_internal( const void *key,
								const void *val,
								void *aux )
{
	wchar_t *name = (wchar_t *)key;
	if( name[0] != L'_' )
		al_push( (array_list_t *)aux, name );
}

void function_get_names( array_list_t *list, int get_hidden )
{
	if( get_hidden )
		hash_get_keys( &function, list );
	else
		hash_foreach2( &function, &get_names_internal, list );
	
}