File: yclientmessage.c

package info (click to toggle)
yiff 2.14.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,344 kB
  • ctags: 3,360
  • sloc: ansic: 40,505; cpp: 7,420; makefile: 425; sh: 242
file content (337 lines) | stat: -rw-r--r-- 10,032 bytes parent folder | download | duplicates (3)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../include/Y2/Y.h"
#include "../include/Y2/Ylib.h"

#include "../include/string.h"


static void print_help(void);


#define MIN(a,b)        ((a) < (b) ? (a) : (b))
#define MAX(a,b)        ((a) > (b) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
#define ABSOLUTE(x)     (((x) < 0) ? ((x) * -1) : (x))


/*
 *	Prints help message.
 */
static void print_help(void)
{
	printf("\
Usage: yclientmessage [options] [message]\n\
\n\
    [options] can be any of the following:\n\
\n\
        --format <string|binary>     Specifies the message data format.\n\
        --type <#>                   Specifies the message type (value must\n\
                                     be an integer).\n\
        --recorder <address:port>    Specify which Y server to connect to.\n\
\n\
    Return values:\n\
\n\
        0       Success.\n\
        1       General error.\n\
        2       Unable to connect to the Y server.\n\
        3       Systems error.\n\
\n"
	);
}


int main(int argc, char *argv[])
{
	int i;
	const char *arg_ptr;

	const char *con_arg = NULL;

	YConnection *con = NULL;

	int format = YClientMessageFormatString;
	int type = YClientMessageTypeComment;
	char message[YClientMessageMessageMax];


        *message = '\0';

	/* If no arguments are given then print the help message. */
	if(argc < 2)
	{
	    print_help();
	    return(1);
	}

	/* Parse arguments. */
	for(i = 1; i < argc; i++)
	{
	    arg_ptr = argv[i];
	    if(arg_ptr == NULL)
		continue;

	    /* Help. */
	    if(strcasepfx(arg_ptr, "--h") ||
               strcasepfx(arg_ptr, "-h") ||
               !strcmp(arg_ptr, "?")
	    )
	    {
		print_help();
		return(0);
	    }
            /* Connect address. */
            else if(strcasepfx(arg_ptr, "--rec") ||
                    strcasepfx(arg_ptr, "-rec")
            )
            {
                i++;
                arg_ptr = (i < argc) ? argv[i] : NULL;
		if(arg_ptr != NULL)
                {
                    con_arg = arg_ptr;
                }
                else
                {
                    fprintf(
			stderr,
			"%s: Requires argument.\n",
			argv[i - 1]
                    );
		    continue;
                }
            }
            /* Message format. */
            else if(strcasepfx(arg_ptr, "--f") ||
                    strcasepfx(arg_ptr, "-f")
            )
            {
                i++;
                arg_ptr = (i < argc) ? argv[i] : NULL;
                if(arg_ptr != NULL)
                {
		    if(!strcasecmp(arg_ptr, "string"))
			format = YClientMessageFormatString;
		    else if(!strcasecmp(arg_ptr, "binary"))
                        format = YClientMessageFormatBinary;
		    else if(!strcasecmp(arg_ptr, "integer8"))
                        format = YClientMessageFormatInteger8;
                    else if(!strcasecmp(arg_ptr, "integeru8"))
                        format = YClientMessageFormatIntegerU8;
                    else if(!strcasecmp(arg_ptr, "integer16"))
                        format = YClientMessageFormatInteger16;
                    else if(!strcasecmp(arg_ptr, "integeru16"))
                        format = YClientMessageFormatIntegerU16;
                    else if(!strcasecmp(arg_ptr, "integer24"))
                        format = YClientMessageFormatInteger24;
                    else if(!strcasecmp(arg_ptr, "integeru24"))
                        format = YClientMessageFormatIntegerU24;
                    else if(!strcasecmp(arg_ptr, "integer32"))
                        format = YClientMessageFormatInteger32;
                    else if(!strcasecmp(arg_ptr, "integeru32"))
                        format = YClientMessageFormatIntegerU32;
                    else if(!strcasecmp(arg_ptr, "integer64"))
                        format = YClientMessageFormatInteger64;
                    else if(!strcasecmp(arg_ptr, "integeru64"))
                        format = YClientMessageFormatIntegerU64;
                    else
                        format = YClientMessageFormatString;
                }
                else
                {
                    fprintf(
                        stderr,
                        "%s: Requires argument.\n",
                        argv[i - 1]
                    );
                    continue;
                }
            }
            /* Message type. */
            else if(strcasepfx(arg_ptr, "--t") ||
                    strcasepfx(arg_ptr, "-t")
            )
            {
                i++;
                arg_ptr = (i < argc) ? argv[i] : NULL;
                if(arg_ptr != NULL)
                {
                    type = atoi(arg_ptr);
                }
                else
                {
                    fprintf(
                        stderr,
                        "%s: Requires argument.\n",
                        argv[i - 1]
                    );
                    continue;
                }
            }
            /* All else assume message. */
            else if((*arg_ptr != '\0') &&
                    (*arg_ptr != '-') &&
                    (*arg_ptr != '+')
	    )
	    {
		strncpy(message, arg_ptr, YClientMessageMessageMax);
		message[YClientMessageMessageMax - 1] = '\0';
	    }
	}


        /* Connect to Y server. */
        con = YOpenConnection(
            NULL,               /* No start argument. */
            con_arg
        );
        if(con == NULL)
        {
            fprintf(stderr, "Unable to connect to the Y server");
            if(con_arg == NULL)
                con_arg = getenv("RECORDER");
            if(con_arg == NULL)
                fprintf(stderr, ".\n");
            else
                fprintf(stderr, ": %s\n", con_arg);
            return(2);
        }

	/* Send client message. */
	if(YSendClientMessage(
	    con, True, format, type, message, strlen(message)
	))
	{
	    /* Error sending client message. */
	    fprintf(
		stderr,
		"Error sending client message.\n"
	    );
	}
	else
	{
	    Boolean need_break = False;
	    YEvent event;
	    YEventClientMessage *clientmessage;

	    /* Wait for client message response, we should get a
	     * YClientMessage event since we specified that we want
	     * one in the call to YSendClientMessage().
	     */
	    while(YGetNextEvent(con, &event, True) > 0)
	    {
		switch(event.type)
		{
		  case YDisconnect:
		  case YShutdown:
		    YCloseConnection(con, False);
		    con = NULL;
		    need_break = True;
		    break;

		  case YClientMessage:
		    clientmessage = &event.clientmessage;
		    if(clientmessage->length > 0)
		    {
			int len = MIN(
			    clientmessage->length,
			    YClientMessageMessageMax - 1
			);
			memcpy(message, clientmessage->message, len);
			message[len] = '\0';
		    }
		    else
		    {
			*message = '\0';
		    }
		    switch(format)
		    {
		      case YClientMessageFormatString:
			printf(
			    "Sent client message \"%s\"\n",
			    message
			);
			break;

                      case YClientMessageFormatBinary:
                        printf(
                            "Sent client message (binary)\n"
                        );
                        break;
                      case YClientMessageFormatInteger8:
                        printf(
                            "Sent client message value %i\n",
                            *(int8_t *)message
                        );
                        break;
                      case YClientMessageFormatIntegerU8:
                        printf(
                            "Sent client message value %i\n",
                            *(u_int8_t *)message
                        );
                        break;
                      case YClientMessageFormatInteger16:
                        printf(
                            "Sent client message value %i\n",
                            *(int16_t *)message
                        );
                        break;
                      case YClientMessageFormatIntegerU16:
                        printf(
                            "Sent client message value %i\n",
                            *(u_int16_t *)message
                        );
                        break;
                      case YClientMessageFormatInteger24:
                        printf(
                            "Sent client message value %i\n",
                            *(int32_t *)message
                        );
                        break;
                      case YClientMessageFormatIntegerU24:
                        printf(
                            "Sent client message value %i\n",
                            *(u_int32_t *)message
                        );
                        break;
                      case YClientMessageFormatInteger32:
                        printf(
                            "Sent client message value %i\n",
                            *(int32_t *)message
                        );
                        break;
                      case YClientMessageFormatIntegerU32:
                        printf(
                            "Sent client message value %i\n",
                            *(u_int32_t *)message
                        );
                        break;
                      case YClientMessageFormatInteger64:
                        printf(
                            "Sent client message value %ld\n",
                            (long)(*(int64_t *)message)
                        );
                        break;
                      case YClientMessageFormatIntegerU64:
                        printf(
                            "Sent client message value %ld\n",
                            (unsigned long)(*(u_int64_t *)message)
                        );
                        break;
		    }
		    need_break = True;
		    break;
		}
		if(need_break)
		    break;
	    }
	}

        /* Disconnect from Y server. */
        YCloseConnection(con, False);
        con = NULL;

	return(0);
}