-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchat_main.c
149 lines (122 loc) · 3.12 KB
/
chat_main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
/* dlsym, etc */
#include <dlfcn.h>
#include "chat.h"
#include "chat-internal.h"
int feedback(const char *msg, void *cc)
{
if (cc==NULL)
{
return -1;
}
return fprintf(stdout, "\r[CHAT] %s\n", msg);
}
static void * load_chat_lib(char *name, chat_functions *cf)
{
void *handle;
handle = dlopen (name, RTLD_LAZY);
char *error;
if (!handle) {
fputs (dlerror(), stderr);
return NULL;
}
fprintf(stderr, "handle: %p\n", handle);
cf->chat_init = dlsym(handle, "chat_init");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
fprintf(stderr, "could not load chat_init from lib\n");
return NULL;
}
cf->chat_close = dlsym(handle, "chat_close");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
fprintf(stderr, "could not load chat_close from lib\n");
return NULL;
}
cf->chat_loop = dlsym(handle, "chat_loop");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
fprintf(stderr, "could not load chat_loop from lib\n");
return NULL;
}
cf->chat_set_feedback_fun = dlsym(handle, "chat_set_feedback_fun");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
fprintf(stderr, "could not load chat_set_feedback_fun from lib\n");
return NULL;
}
fprintf(stderr, "chat_functions: %p\n", cf);
return handle;
}
static int start_chat(char *host, int port, char *lib)
{
void *handle;
chat_functions c_funs;
void *chat_lib_data;
fprintf(stderr, "lib: '%s'\n", lib);
fprintf(stderr, "host: '%s'\n", host);
fprintf(stderr, "Loading the chat lib '%s'\n", lib);
handle = load_chat_lib(lib, &c_funs);
if (handle == NULL)
{
fprintf(stderr, "Failed loading the chat lib '%s'\n", lib);
return 1;
}
fprintf(stderr, "Init chat (%s,%d)'%s'\n", host, port, lib);
chat_lib_data = c_funs.chat_init(host, port);
if (chat_lib_data == NULL)
{
fprintf(stderr, "Failed initialising the client\n");
return 1;
}
fprintf(stderr, "Init chat => %p \n", chat_lib_data);
fprintf(stderr, " -------------------- \n");
/*
* Use our own feedback function
*/
c_funs.chat_set_feedback_fun(chat_lib_data, feedback);
fprintf(stderr, " -------------------- \n");
/*
* Enter chat - loop until bye or quit
*/
c_funs.chat_loop(chat_lib_data);
fprintf(stderr, " -------------------- \n");
/*
* Close the chat client
*/
c_funs.chat_close(chat_lib_data);
fprintf(stderr, "Closing handle %p\n", handle);
dlclose(handle);
return CHAT_CLIENT_OK;
}
int main(int argc, char **argv)
{
int port;
char *host;
char *lib;
/*
*
* Rudimentary parser
*
*/
if (argc < 4)
{
fprintf(stderr,"Usage:\n");
fprintf(stderr," %s chat-library <hostname> <port>\n", argv[0]);
return 1;
}
/* turn port number into an (unsigned) int */
if (sscanf(argv[3], "%ud", &port)!=1)
{
fprintf(stderr,"invalid port: %s\n", argv[3]);
return 2;
}
host = argv[2];
lib = argv[1];
start_chat(host, port, lib);
return 0;
}