APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
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
/*
Copyright (c) 2009-2019 Roger Light <roger@atchoo.org>
 
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
 
The Eclipse Public License is available at
   http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
  http://www.eclipse.org/org/documents/edl-v10.php.
 
Contributors:
   Roger Light - initial implementation and documentation.
*/
 
#include "config.h"
 
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef WIN32
#include <unistd.h>
#else
#include <process.h>
#include <winsock2.h>
#define snprintf sprintf_s
#endif
 
#ifdef __APPLE__
#  include <sys/time.h>
#endif
 
#include <mosquitto.h>
#include "client_shared.h"
 
extern struct mosq_config cfg;
 
static int get_time(struct tm **ti, long *ns)
{
#ifdef WIN32
    SYSTEMTIME st;
#elif defined(__APPLE__)
    struct timeval tv;
#else
    struct timespec ts;
#endif
    time_t s;
 
#ifdef WIN32
    s = time(NULL);
 
    GetLocalTime(&st);
    *ns = st.wMilliseconds*1000000L;
#elif defined(__APPLE__)
    gettimeofday(&tv, NULL);
    s = tv.tv_sec;
    *ns = tv.tv_usec*1000;
#else
    if(clock_gettime(CLOCK_REALTIME, &ts) != 0){
        err_printf(&cfg, "Error obtaining system time.\n");
        return 1;
    }
    s = ts.tv_sec;
    *ns = ts.tv_nsec;
#endif
 
    *ti = localtime(&s);
    if(!(*ti)){
        err_printf(&cfg, "Error obtaining system time.\n");
        return 1;
    }
 
    return 0;
}
 
 
static void write_payload(const unsigned char *payload, int payloadlen, int hex)
{
    int i;
 
    if(hex == 0){
        (void)fwrite(payload, 1, payloadlen, stdout);
    }else if(hex == 1){
        for(i=0; i<payloadlen; i++){
            fprintf(stdout, "%02x", payload[i]);
        }
    }else if(hex == 2){
        for(i=0; i<payloadlen; i++){
            fprintf(stdout, "%02X", payload[i]);
        }
    }
}
 
 
static void write_json_payload(const char *payload, int payloadlen)
{
    int i;
 
    for(i=0; i<payloadlen; i++){
        if(payload[i] == '"' || payload[i] == '\\' || (payload[i] >=0 && payload[i] < 32)){
            printf("\\u%04x", payload[i]);
        }else{
            fputc(payload[i], stdout);
        }
    }
}
 
 
static void json_print(const struct mosquitto_message *message, const struct tm *ti, bool escaped)
{
    char buf[100];
 
    strftime(buf, 100, "%s", ti);
    printf("{\"tst\":%s,\"topic\":\"%s\",\"qos\":%d,\"retain\":%d,\"payloadlen\":%d,", buf, message->topic, message->qos, message->retain, message->payloadlen);
    if(message->qos > 0){
        printf("\"mid\":%d,", message->mid);
    }
    if(escaped){
        fputs("\"payload\":\"", stdout);
        write_json_payload(message->payload, message->payloadlen);
        fputs("\"}", stdout);
    }else{
        fputs("\"payload\":", stdout);
        write_payload(message->payload, message->payloadlen, 0);
        fputs("}", stdout);
    }
}
 
 
static void formatted_print(const struct mosq_config *lcfg, const struct mosquitto_message *message)
{
    int len;
    int i;
    struct tm *ti = NULL;
    long ns;
    char strf[3];
    char buf[100];
 
    len = strlen(lcfg->format);
 
    for(i=0; i<len; i++){
        if(lcfg->format[i] == '%'){
            if(i < len-1){
                i++;
                switch(lcfg->format[i]){
                    case '%':
                        fputc('%', stdout);
                        break;
 
                    case 'I':
                        if(!ti){
                            if(get_time(&ti, &ns)){
                                err_printf(lcfg, "Error obtaining system time.\n");
                                return;
                            }
                        }
                        if(strftime(buf, 100, "%FT%T%z", ti) != 0){
                            fputs(buf, stdout);
                        }
                        break;
 
                    case 'j':
                        if(!ti){
                            if(get_time(&ti, &ns)){
                                err_printf(lcfg, "Error obtaining system time.\n");
                                return;
                            }
                        }
                        json_print(message, ti, true);
                        break;
 
                    case 'J':
                        if(!ti){
                            if(get_time(&ti, &ns)){
                                err_printf(lcfg, "Error obtaining system time.\n");
                                return;
                            }
                        }
                        json_print(message, ti, false);
                        break;
 
                    case 'l':
                        printf("%d", message->payloadlen);
                        break;
 
                    case 'm':
                        printf("%d", message->mid);
                        break;
 
                    case 'p':
                        write_payload(message->payload, message->payloadlen, 0);
                        break;
 
                    case 'q':
                        fputc(message->qos + 48, stdout);
                        break;
 
                    case 'r':
                        if(message->retain){
                            fputc('1', stdout);
                        }else{
                            fputc('0', stdout);
                        }
                        break;
 
                    case 't':
                        fputs(message->topic, stdout);
                        break;
 
                    case 'U':
                        if(!ti){
                            if(get_time(&ti, &ns)){
                                err_printf(lcfg, "Error obtaining system time.\n");
                                return;
                            }
                        }
                        if(strftime(buf, 100, "%s", ti) != 0){
                            printf("%s.%09ld", buf, ns);
                        }
                        break;
 
                    case 'x':
                        write_payload(message->payload, message->payloadlen, 1);
                        break;
 
                    case 'X':
                        write_payload(message->payload, message->payloadlen, 2);
                        break;
                }
            }
        }else if(lcfg->format[i] == '@'){
            if(i < len-1){
                i++;
                if(lcfg->format[i] == '@'){
                    fputc('@', stdout);
                }else{
                    if(!ti){
                        if(get_time(&ti, &ns)){
                            err_printf(lcfg, "Error obtaining system time.\n");
                            return;
                        }
                    }
 
                    strf[0] = '%';
                    strf[1] = lcfg->format[i];
                    strf[2] = 0;
 
                    if(lcfg->format[i] == 'N'){
                        printf("%09ld", ns);
                    }else{
                        if(strftime(buf, 100, strf, ti) != 0){
                            fputs(buf, stdout);
                        }
                    }
                }
            }
        }else if(lcfg->format[i] == '\\'){
            if(i < len-1){
                i++;
                switch(lcfg->format[i]){
                    case '\\':
                        fputc('\\', stdout);
                        break;
 
                    case '0':
                        fputc('\0', stdout);
                        break;
 
                    case 'a':
                        fputc('\a', stdout);
                        break;
 
                    case 'e':
                        fputc('\033', stdout);
                        break;
 
                    case 'n':
                        fputc('\n', stdout);
                        break;
 
                    case 'r':
                        fputc('\r', stdout);
                        break;
 
                    case 't':
                        fputc('\t', stdout);
                        break;
 
                    case 'v':
                        fputc('\v', stdout);
                        break;
                }
            }
        }else{
            fputc(lcfg->format[i], stdout);
        }
    }
    if(lcfg->eol){
        fputc('\n', stdout);
    }
    fflush(stdout);
}
 
 
void print_message(struct mosq_config *cfg, const struct mosquitto_message *message)
{
    if(cfg->format){
        formatted_print(cfg, message);
    }else if(cfg->verbose){
        if(message->payloadlen){
            printf("%s ", message->topic);
            write_payload(message->payload, message->payloadlen, false);
            if(cfg->eol){
                printf("\n");
            }
        }else{
            if(cfg->eol){
                printf("%s (null)\n", message->topic);
            }
        }
        fflush(stdout);
    }else{
        if(message->payloadlen){
            write_payload(message->payload, message->payloadlen, false);
            if(cfg->eol){
                printf("\n");
            }
            fflush(stdout);
        }
    }
}