File: plugin.c

package info (click to toggle)
exult 1.12.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,856 kB
  • sloc: cpp: 170,016; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 550; objc: 416
file content (163 lines) | stat: -rw-r--r-- 3,841 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * author: Aurelien Marchand
 * licence: GPL
 * date: 20/06/03
 *
 */

#ifndef _WIN32
#	include <dlfcn.h>
#endif
#include "globals.h"
#include "linked.h"

#include <stdio.h>
#include <stdlib.h>

char* plug_error(void) {
	// Colourless: Unix specific part. Add your #def here
#ifdef _WIN32
	HRESULT hRes = GetLastError();

	if (FAILED(hRes)) {
		static TCHAR lpMsgBuf[256];
		FormatMessage(
				FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
				NULL, GetLastError(),
				MAKELANGID(
						LANG_NEUTRAL, SUBLANG_DEFAULT),    // Default language
				lpMsgBuf, 256, NULL);
		return lpMsgBuf;
	} else {
		return 0;
	}
#else
	return dlerror();
#endif
}

libhandle_t plug_load(char* plug_name) {
	libhandle_t a_hdl;

	// Colourless: Unix specific part. Add your #def here
#ifdef _WIN32
	a_hdl = LoadLibrary(plug_name);
#else
	a_hdl = dlopen(plug_name, RTLD_LAZY);
#endif
	if (!a_hdl) {
		fprintf(stderr, "%s\n", plug_error());
		return NULL;
	}

	return a_hdl;
}

int plug_unload(libhandle_t a_hdl) {
	// in linux, dlclose return 0 when success and non-zero on error, so let's
	// invert that to be consistant with the rest in Windows, FreeLibrary
	// returns 0 on failure, non zero on success Colourless: Unix specific part.
	// Add your #def here
#ifdef _WIN32
	return FreeLibrary(a_hdl);
#else
	return !dlclose(a_hdl);
#endif
}

void* plug_load_func(libhandle_t a_hdl, const char* func_name) {
	// Colourless: Unix specific part. Add your #def here
#ifdef _WIN32
	// Need to add _ to the start of the function names in windows
	//	TCHAR strProcName[256] = "_";
	//	strncat(strProcName, func_name, 256);
	void* ret;
	*(FARPROC*)&ret = GetProcAddress(a_hdl, func_name);
	return ret;
#else
	return dlsym(a_hdl, func_name);
#endif
}

int add_plugin_apply(int col_index, libhandle_t a_hdl) {
	// NOTE: It is necessary to add the handle AT THE END of the list for index
	// col_index
	//       since the last transforming must have the priority (by design)

	// add a node to action_table[col_index] that points to handle's apply
	// function

	// special case: action_table[col_index] == NULL

	// otherwise, cursor=action_table[col_index]
	// while (cursor->next) != NULL
	// cursor = cursor->next;
	// insert new node at cursor->next, with handle as the pointer to
	// plugin_apply

	//  colour_hex(*apply)(colour_hex,glob_variables *);

	// first of all, load a_hdl's apply function
	pfnPluginApply apply;
	*(void**)&apply = plug_load_func(a_hdl, "plugin_apply");

	char* error = plug_error();
	if (error != NULL) {
		fprintf(stderr, "%s\n", error);
		return -1;
	}
	node* new_node = create_node();
	if (new_node == NULL) {
		// problem
		fprintf(stderr, "Couldn't create node\n");
		return -1;
	}
	new_node->plugin_apply = apply;

	// add new_node at end of list found on action_table[idx]
	// dealing with the special case
	if (action_table[col_index] == NULL) {
		action_table[col_index] = new_node;
	} else {
		// go to the end and add it there
		node* cursor = action_table[col_index];
		while (cursor->next != NULL) {
			cursor = cursor->next;
		}
		// cursor->next == NULL
		cursor->next = new_node;
	}
	// all done
	return 0;
}

int add_plugin_parse(char* line, libhandle_t a_hdl) {
	int (*teach)(char*);
	*(void**)&teach = plug_load_func(a_hdl, "plugin_parse");

	char* error = plug_error();
	if (error != NULL) {
		fprintf(stderr, "%s\n", error);
		return -1;
	}
	return (*teach)(line);
}

node* add_handle(libhandle_t a_hdl, node* list) {
	// add a node to list that points to handle

	// create a node with a_hdl as node->a_hdl value and list as node->next
	// value insert node at head;

	node* new_head = create_node();
	if (new_head == NULL) {
		// problem
		fprintf(stderr, "WARNING: couldn't create node\n");
		return NULL;
	} else {
		new_head->next   = list;
		new_head->handle = a_hdl;
	}

	return new_head;
}