forked from dajobe/flickcurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
556 lines (461 loc) · 12.5 KB
/
codegen.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* codegen utility - Make C code from Flickr API by reflection
*
* Copyright (C) 2007-2012, David Beckett http://www.dajobe.org/
*
* This file is licensed under the following three licenses as alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of
* the above three licenses.
*
* See LICENSE.html or LICENSE.txt at the top of this package for the
* complete terms and further detail along with the license texts for
* the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
*
*
* USAGE: codegen [OPTIONS] flickr.section
*
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
/* many places for getopt */
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#include <flickcurl_getopt.h>
#endif
#include <flickcurl.h>
#ifdef NEED_OPTIND_DECLARATION
extern int optind;
extern char *optarg;
#endif
static const char* program;
static const char*
my_basename(const char *name)
{
char *p;
if((p = strrchr(name, '/')))
name = p+1;
else if((p = strrchr(name, '\\')))
name = p+1;
return name;
}
static void
my_message_handler(void *user_data, const char *message)
{
fprintf(stderr, "%s: ERROR: %s\n", program, message);
}
#ifdef HAVE_GETOPT_LONG
#define HELP_TEXT(short, long, description) " -" short ", --" long " " description
#define HELP_TEXT_LONG(long, description) " --" long " " description
#define HELP_ARG(short, long) "--" #long
#define HELP_PAD "\n "
#else
#define HELP_TEXT(short, long, description) " -" short " " description
#define HELP_TEXT_LONG(long, description)
#define HELP_ARG(short, long) "-" #short
#define HELP_PAD "\n "
#endif
#define GETOPT_STRING "d:hv"
#ifdef HAVE_GETOPT_LONG
static struct option long_options[] =
{
/* name, has_arg, flag, val */
{"delay", 1, 0, 'd'},
{"help", 0, 0, 'h'},
{"version", 0, 0, 'v'},
{NULL, 0, 0, 0}
};
#endif
#define IS_INT_ARG(arg) (!strcmp(arg->name, "per_page") || !strcmp(arg->name, "page"))
#define IS_OPTIONAL_ARG(arg) (!strcmp(arg->name, "per_page") || !strcmp(arg->name, "page"))
static const char *title_format_string = "Skeleton code generator utility %s\n";
static const char* config_filename = ".flickcurl.conf";
static const char* config_section = "flickr";
int
main(int argc, char *argv[])
{
flickcurl *fc = NULL;
int rc = 0;
int usage = 0;
int help = 0;
int read_auth = 1;
int i;
const char* home;
char config_path[1024];
int request_delay= -1;
char section[50];
size_t section_len;
char** methods = NULL;
flickcurl_init();
program = my_basename(argv[0]);
home = getenv("HOME");
if(home)
sprintf(config_path, "%s/%s", home, config_filename);
else
strcpy(config_path, config_filename);
while (!usage && !help)
{
int c;
#ifdef HAVE_GETOPT_LONG
int option_index = 0;
c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
c = getopt (argc, argv, GETOPT_STRING);
#endif
if (c == -1)
break;
switch (c) {
case 0:
case '?': /* getopt() - unknown option */
usage = 1;
break;
case 'd':
if(optarg)
request_delay = atoi(optarg);
break;
case 'h':
help = 1;
break;
case 'v':
fputs(flickcurl_version_string, stdout);
fputc('\n', stdout);
exit(0);
}
}
if(help)
goto help;
if(argc < 2) {
fprintf(stderr, "%s: No API section given\n", program);
usage = 1;
}
if(usage) {
if(usage>1) {
fprintf(stderr, title_format_string, flickcurl_version_string);
fputs("Flickcurl home page: ", stderr);
fputs(flickcurl_home_url_string, stderr);
fputc('\n', stderr);
fputs(flickcurl_copyright_string, stderr);
fputs("\nLicense: ", stderr);
fputs(flickcurl_license_string, stderr);
fputs("\n\n", stderr);
}
fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n",
program);
rc = 1;
goto tidy;
}
help:
if(help) {
printf(title_format_string, flickcurl_version_string);
puts("Generate C code from Flickr API by reflection.");
printf("Usage: %s [OPTIONS] command args...\n\n", program);
fputs(flickcurl_copyright_string, stdout);
fputs("\nLicense: ", stdout);
puts(flickcurl_license_string);
fputs("Flickcurl home page: ", stdout);
puts(flickcurl_home_url_string);
fputs("\n", stdout);
puts(HELP_TEXT("d", "delay DELAY ", "Set delay between requests in milliseconds"));
puts(HELP_TEXT("h", "help ", "Print this help, then exit"));
puts(HELP_TEXT("v", "version ", "Print the flickcurl version"));
rc = 0;
goto tidy;
}
/* Initialise the Flickcurl library */
fc = flickcurl_new();
if(!fc) {
rc = 1;
goto tidy;
}
flickcurl_set_error_handler(fc, my_message_handler, NULL);
if(read_auth && !access((const char*)config_path, R_OK)) {
if(flickcurl_config_read_ini(fc, config_path, config_section, fc,
flickcurl_config_var_handler)) {
rc = 1;
goto tidy;
}
}
if(request_delay >= 0)
flickcurl_set_request_delay(fc, request_delay);
strcpy(section, "flickr.");
strcpy(section+7, argv[1]);
/* allow old format commands to work */
for(i = 0; section[i]; i++) {
if(section[i] == '-')
section[i] = '.';
}
if(!strncmp(section, "flickr.flickr", 13))
strcpy(section, section+7);
section_len = strlen(section);
fprintf(stderr, "%s: section '%s'\n", program, section);
methods = flickcurl_reflection_getMethods(fc);
if(!methods) {
fprintf(stderr, "%s: getMethods failed\n", program);
rc = 1;
goto tidy;
}
fprintf(stdout,
"/* -*- Mode: c; c-basic-offset: 2 -*-\n"
" *\n"
" * %s-api.c - Flickr %s.* API calls\n"
" *\n",
argv[1], section);
fprintf(stdout,
" * Copyright (C) 2010-2012, David Beckett http://www.dajobe.org/\n"
" * \n"
" * This file is licensed under the following three licenses as alternatives:\n"
" * 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version\n"
" * 2. GNU General Public License (GPL) V2 or any newer version\n"
" * 3. Apache License, V2.0 or any newer version\n"
" * \n"
" * You may not use this file except in compliance with at least one of\n"
" * the above three licenses.\n"
" * \n"
" * See LICENSE.html or LICENSE.txt at the top of this package for the\n"
" * complete terms and further detail along with the license texts for\n"
" * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.\n"
" * \n"
" */\n"
"\n"
"#include <stdio.h>\n"
"#include <string.h>\n"
"#include <stdarg.h>\n"
"\n"
"#ifdef HAVE_CONFIG_H\n"
"#include <config.h>\n"
"#endif\n"
"\n"
"#ifdef HAVE_STDLIB_H\n"
"#include <stdlib.h>\n"
"#undef HAVE_STDLIB_H\n"
"#endif\n"
"#ifdef HAVE_UNISTD_H\n"
"#include <unistd.h>\n"
"#endif\n"
"\n"
"#include <flickcurl.h>\n"
"#include <flickcurl_internal.h>\n"
"\n"
"\n"
);
for(i = 0; methods[i]; i++) {
flickcurl_method* method;
char* method_name;
char function_name[100];
int c, j;
int is_write = 0;
if(strncmp(methods[i], section, section_len))
continue;
method = flickcurl_reflection_getMethodInfo(fc, methods[i]);
if(!method) {
fprintf(stderr, "%s: getMethodInfo(%s) failed\n", program, methods[i]);
rc = 1;
break;
}
method_name = method->name;
if(
strstr(method_name, ".add") ||
strstr(method_name, ".create") ||
strstr(method_name, ".delete") ||
strstr(method_name, ".edit") ||
strstr(method_name, ".remove") ||
strstr(method_name, ".set")
)
is_write = 1;
strcpy(function_name, "flickcurl_");
for(j = 0; (c = methods[i][j+7]); j++) {
if(c == '.')
c = '_';
function_name[j+10] = c;
}
function_name[j+10] = '\0';
fprintf(stdout, "/**\n * %s:\n", function_name);
/* fixed arguments */
fprintf(stdout, " * @fc: flickcurl context\n");
if(method->args_count) {
int argi;
for(argi = 0; method->args[argi]; argi++) {
flickcurl_arg* arg = method->args[argi];
if(!strcmp(arg->name, "api_key"))
continue;
fprintf(stdout, " * @%s: %s", arg->name, arg->description);
if(arg->optional) {
fputs((IS_INT_ARG(arg)? " (or < 0)" : " (or NULL)"), stdout);
}
fputc('\n', stdout);
}
}
fprintf(stdout,
" * \n"
" * %s\n"
" *\n"
" * Implements %s (%s)\n",
method->description, method->name, flickcurl_version_string
);
fprintf(stdout,
" * \n"
" * Return value: non-0 on failure\n"
" **/\n"
);
fprintf(stdout, "int\n%s(flickcurl* fc", function_name);
if(method->args_count) {
int argi;
for(argi = 0; method->args[argi]; argi++) {
flickcurl_arg* arg = method->args[argi];
if(!strcmp(arg->name, "api_key"))
continue;
if(IS_INT_ARG(arg)) {
fprintf(stdout, ", int %s", arg->name);
} else {
fprintf(stdout, ", const char* %s", arg->name);
}
}
}
fprintf(stdout, ")\n{\n");
fprintf(stdout,
" const char* parameters[%d][2];\n"
" int count = 0;\n"
" xmlDocPtr doc = NULL;\n"
" xmlXPathContextPtr xpathCtx = NULL; \n"
" void* result = NULL;\n",
6+method->args_count);
if(method->args_count) {
int argi;
for(argi = 0; method->args[argi]; argi++) {
flickcurl_arg* arg = method->args[argi];
if(IS_INT_ARG(arg)) {
fprintf(stdout,
" char %s_str[10];\n",
arg->name);
}
}
}
fputs(
" \n",
stdout);
if(method->args_count) {
int argi;
int print_or = 0;
fprintf(stdout, " if(");
for(argi = 0; method->args[argi]; argi++) {
flickcurl_arg* arg = method->args[argi];
if(!strcmp(arg->name, "api_key"))
continue;
if(!arg->optional) {
if(print_or)
fprintf(stdout, " || ");
fprintf(stdout, "!%s", arg->name);
print_or = 1;
}
}
fprintf(stdout,
")\n"
" return 1;\n"
"\n");
}
if(method->args_count) {
int argi;
for(argi = 0; method->args[argi]; argi++) {
flickcurl_arg* arg = method->args[argi];
if(!strcmp(arg->name, "api_key"))
continue;
if(arg->optional) {
if(IS_INT_ARG(arg)) {
fprintf(stdout,
" if(%s >= 0) {\n"
" sprintf(%s_str, \"%%d\", %s);\n"
" parameters[count][0] = \"%s\";\n"
" parameters[count++][1]= %s_str;\n"
" }\n",
arg->name, arg->name, arg->name, arg->name, arg->name);
continue;
}
fprintf(stdout,
" if(%s) {\n",
arg->name);
}
fprintf(stdout,
" parameters[count][0] = \"%s\";\n"
" parameters[count++][1]= %s;\n",
arg->name, arg->name);
if(arg->optional) {
fprintf(stdout,
" }\n"
);
}
}
}
fprintf(stdout,
"\n"
" parameters[count][0] = NULL;\n"
"\n"
);
fprintf(stdout,
" if(flickcurl_prepare(fc, \"%s\", parameters, count))\n"
" goto tidy;\n"
"\n",
method->name);
if(is_write)
fprintf(stdout,
" flickcurl_set_write(fc, 1);\n"
" flickcurl_set_data(fc, (void*)\"\", 0);\n"
"\n"
);
fprintf(stdout,
" doc = flickcurl_invoke(fc);\n"
" if(!doc)\n"
" goto tidy;\n"
"\n"
"\n"
" xpathCtx = xmlXPathNewContext(doc);\n"
" if(!xpathCtx) {\n"
" flickcurl_error(fc, \"Failed to create XPath context for document\");\n"
" fc->failed = 1;\n"
" goto tidy;\n"
" }\n"
"\n"
" result = NULL; /* your code here */\n"
"\n"
" tidy:\n"
" if(xpathCtx)\n"
" xmlXPathFreeContext(xpathCtx);\n"
"\n"
" if(fc->failed)\n"
" result = NULL;\n"
"\n"
" return (result == NULL);\n"
"}\n"
"\n"
"\n");
flickcurl_free_method(method);
}
tidy:
if(methods) {
for(i = 0; methods[i]; i++)
free(methods[i]);
free(methods);
methods = NULL;
}
if(fc)
flickcurl_free(fc);
flickcurl_finish();
return(rc);
}