Check-in [5882c34adc]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Yet more utstring usage Remove cplanet* and syslog support
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5882c34adce8735d3891e2797f62e9d0e28c5899
User & Date: bapt 2013-07-22 13:26:20
Context
2013-07-22
13:26
Remove -l check-in: 183ffb8fda user: bapt tags: trunk
13:26
Yet more utstring usage Remove cplanet* and syslog support check-in: 5882c34adc user: bapt tags: trunk
13:16
Remove dead code check-in: 975da460b7 user: bapt tags: trunk
Changes

Changes to cplanet.c.

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
...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
...
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
...
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
...
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
...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
	ret = 0;
cleanup:
	if (sqlbuf != NULL)
		sqlite3_free(sqlbuf);
	return (ret);
}

static void
cplanet_err(int eval, const char* message, ...)
{
	va_list args;
	va_start(args, message);
	if (syslog_flag) {
		vsyslog(LOG_ERR, message, args);
		exit(eval);
		/* NOTREACHED */
	} else {
		verrx(eval, message, args);
		/* NOTREACHED */
	}
}

static void
cplanet_warn(const char* message, ...)
{
	va_list args;
	va_start(args, message);
	if (syslog_flag)
		vsyslog(LOG_WARNING, message, args);
	else
		vwarnx(message, args);
	va_end(args);
}

static size_t
write_to_buffer(void *ptr, size_t size, size_t memb, void *data) {
	size_t realsize = size * memb;
	struct buffer *mem = (struct buffer *)data;


	if ((mem->data = realloc(mem->data, mem->size + realsize + 1)) == NULL)
		cplanet_err(1, "not enough memory");

	memcpy(&(mem->data[mem->size]), ptr, realsize);
	mem->size += realsize;
	mem->data[mem->size] = '\0';
	mem->cap = mem->size;

	return (realsize);
}

/* convert the iso format as the RFC3339 is a subset of it */
static time_t
iso8601_to_time_t(const char *d)
................................................................................
			pos = s;
		}

	}
	free(s);
	if (pos == NULL) {
		errno = EINVAL;
		cplanet_warn("Convert  ISO8601 '%s' to struct tm failed", s);
		return 0;
	}
	t = mktime(&date);
	if (t == (time_t)-1) {
		errno = EINVAL;
		cplanet_warn("Convert struct tm (from '%s') to time_t failed", s);
		return 0;
	}
	return t;
}

/* convert RFC822 to epoch time */
static time_t
................................................................................
{
	struct tm date;
	time_t t;
	char *pos;
	errno = 0;

	if (s == NULL) {
		cplanet_warn("Invalide empty date");
		return 0;
	}

	if ((pos = strptime(s, "%a, %d %b %Y %T", &date)) == NULL) {
		errno = EINVAL;
		cplanet_warn("Convert RFC822 '%s' to struct tm failed", s);

		return 0;
	}

	if ((t = mktime(&date)) == -1) {
		errno = EINVAL;
		cplanet_warn("Convert struct tm (from '%s') to time_t failed", s);
		return 0;
	}

	return t;
}

static void
................................................................................
		feed->tag = NULL;
		feed->nbtags = 0;
		feed->has_author = false;
	}
	feed->xmlpath->size -= strlen(elt);
	feed->xmlpath->data[feed->xmlpath->size] = '\0';
	if (feed->xmlpath->data[feed->xmlpath->size - 1] != '/')
		cplanet_warn("invalid xml");

	feed->xmlpath->size--;
	feed->xmlpath->data[feed->xmlpath->size] = '\0';
}

static void XMLCALL
xml_data(void *userdata, const char *s, int len)
................................................................................

/* retreive ports and prepare the dataset for the template */
static int
fetch_posts(const unsigned char *name, const unsigned char *url)
{
	CURL *curl;
	CURLcode res;
	struct buffer rawfeed;
	struct XML_ParserStruct *parser;
	struct feed feed;

	rawfeed.data = malloc(1);
	rawfeed.size = 0;

	feed.type = NONE;
	feed.name = name;
	feed.has_author = false;
	feed.tag = NULL;
	feed.nbtags = 0;
	utstring_new(feed.blog_title);
................................................................................
	    "(uid, tag) values (?1, ?2)", -1, &feed.tags, NULL) != SQLITE_OK) {
		warnx("sqlite: %s", sqlite3_errmsg(db));
		return (0);
	}

	curl_global_init(CURL_GLOBAL_ALL);
	if ((curl = curl_easy_init()) == NULL)
		cplanet_err(1, "Unable to initalise curl");

	curl_easy_setopt(curl, CURLOPT_URL, url);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
	curl_easy_setopt(curl, CURLOPT_HEADER, 0);
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_buffer);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &rawfeed);
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "cplanet/"CPLANET_VERSION);
	curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");

	res = curl_easy_perform(curl);

	if (res != CURLE_OK || rawfeed.size == 0) {
		free(rawfeed.data);
		curl_easy_cleanup(curl);
		cplanet_warn("An error occured while fetching %s\n", url);
		free(feed.xmlpath->data);
		free(feed.xmlpath);
		return (0);
	}

	if ((parser = XML_ParserCreate(NULL)) == NULL)
		cplanet_err(1, "Unable to initialise expat");

	XML_SetStartElementHandler(parser, xml_startel);
	XML_SetEndElementHandler(parser, xml_endel);
	XML_SetCharacterDataHandler(parser, xml_data);
	XML_SetUserData(parser, &feed);

	if (XML_Parse(parser, rawfeed.data, rawfeed.size, true) == XML_STATUS_ERROR) {
		cplanet_warn("Parse error at line %lu: %s for %s",
		    XML_GetCurrentLineNumber(parser),
		    XML_ErrorString(XML_GetErrorCode(parser)),
		    url);
	}

	XML_ParserFree(parser);




	sqlite3_finalize(feed.stmt);
	sqlite3_finalize(feed.tags);

	free(feed.xmlpath->data);
	free(feed.xmlpath);
	return (0);
};
................................................................................
	neoerr = cs_render(parse, &cs_output_data, cplanet_output);

	if (neoerr != STATUS_OK)
		goto warn0;

	FILE *output = fopen((const char *)cs_output, "w+");
	if (output == NULL)
		cplanet_err(1, "%s", cs_output);
	fprintf(output, "%s", cs_output_data.buf);
	fflush(output);
	fclose(output);
	cs_destroy(&parse);
	string_clear(&cs_output_data);

	return;

warn0:
	cs_destroy(&parse);
warn1:
	string_clear(&cs_output_data);
	nerr_error_string(neoerr,&neoerr_str);
	cplanet_warn(neoerr_str.buf);
}

static void
usage(void)
{
	errx(1, "usage: cplanet -c conf.hdf [-l]\n");
}







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



<
>

<
<
<
|
<
<
<







 







|





|







 







|





|






|







 







|







 







|



<
|







 







|







|





|
<

|






|






|
|






>
>
>
>







 







|













|







151
152
153
154
155
156
157



























158
159
160

161
162



163



164
165
166
167
168
169
170
...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
...
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
...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
...
417
418
419
420
421
422
423
424
425
426
427

428
429
430
431
432
433
434
435
...
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
...
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
557
558
559
	ret = 0;
cleanup:
	if (sqlbuf != NULL)
		sqlite3_free(sqlbuf);
	return (ret);
}




























static size_t
write_to_buffer(void *ptr, size_t size, size_t memb, void *data) {
	size_t realsize = size * memb;

	UT_string *mem = (UT_string *)data;




	utstring_bincpy(mem, ptr, realsize);




	return (realsize);
}

/* convert the iso format as the RFC3339 is a subset of it */
static time_t
iso8601_to_time_t(const char *d)
................................................................................
			pos = s;
		}

	}
	free(s);
	if (pos == NULL) {
		errno = EINVAL;
		warnx("Convert  ISO8601 '%s' to struct tm failed", s);
		return 0;
	}
	t = mktime(&date);
	if (t == (time_t)-1) {
		errno = EINVAL;
		warnx("Convert struct tm (from '%s') to time_t failed", s);
		return 0;
	}
	return t;
}

/* convert RFC822 to epoch time */
static time_t
................................................................................
{
	struct tm date;
	time_t t;
	char *pos;
	errno = 0;

	if (s == NULL) {
		warnx("Invalide empty date");
		return 0;
	}

	if ((pos = strptime(s, "%a, %d %b %Y %T", &date)) == NULL) {
		errno = EINVAL;
		warnx("Convert RFC822 '%s' to struct tm failed", s);

		return 0;
	}

	if ((t = mktime(&date)) == -1) {
		errno = EINVAL;
		warnx("Convert struct tm (from '%s') to time_t failed", s);
		return 0;
	}

	return t;
}

static void
................................................................................
		feed->tag = NULL;
		feed->nbtags = 0;
		feed->has_author = false;
	}
	feed->xmlpath->size -= strlen(elt);
	feed->xmlpath->data[feed->xmlpath->size] = '\0';
	if (feed->xmlpath->data[feed->xmlpath->size - 1] != '/')
		warnx("invalid xml");

	feed->xmlpath->size--;
	feed->xmlpath->data[feed->xmlpath->size] = '\0';
}

static void XMLCALL
xml_data(void *userdata, const char *s, int len)
................................................................................

/* retreive ports and prepare the dataset for the template */
static int
fetch_posts(const unsigned char *name, const unsigned char *url)
{
	CURL *curl;
	CURLcode res;
	UT_string *rawfeed;
	struct XML_ParserStruct *parser;
	struct feed feed;


	utstring_new(rawfeed);

	feed.type = NONE;
	feed.name = name;
	feed.has_author = false;
	feed.tag = NULL;
	feed.nbtags = 0;
	utstring_new(feed.blog_title);
................................................................................
	    "(uid, tag) values (?1, ?2)", -1, &feed.tags, NULL) != SQLITE_OK) {
		warnx("sqlite: %s", sqlite3_errmsg(db));
		return (0);
	}

	curl_global_init(CURL_GLOBAL_ALL);
	if ((curl = curl_easy_init()) == NULL)
		errx(1, "Unable to initalise curl");

	curl_easy_setopt(curl, CURLOPT_URL, url);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
	curl_easy_setopt(curl, CURLOPT_HEADER, 0);
	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_buffer);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, rawfeed);
	curl_easy_setopt(curl, CURLOPT_USERAGENT, "cplanet/"CPLANET_VERSION);
	curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");

	res = curl_easy_perform(curl);

	if (res != CURLE_OK || utstring_len(rawfeed) == 0) {

		curl_easy_cleanup(curl);
		warnx("An error occured while fetching %s", url);
		free(feed.xmlpath->data);
		free(feed.xmlpath);
		return (0);
	}

	if ((parser = XML_ParserCreate(NULL)) == NULL)
		errx(1, "Unable to initialise expat");

	XML_SetStartElementHandler(parser, xml_startel);
	XML_SetEndElementHandler(parser, xml_endel);
	XML_SetCharacterDataHandler(parser, xml_data);
	XML_SetUserData(parser, &feed);

	if (XML_Parse(parser, utstring_body(rawfeed), utstring_len(rawfeed), true) == XML_STATUS_ERROR) {
		warnx("Parse error at line %lu: %s for %s",
		    XML_GetCurrentLineNumber(parser),
		    XML_ErrorString(XML_GetErrorCode(parser)),
		    url);
	}

	XML_ParserFree(parser);
	utstring_free(rawfeed);
	utstring_free(feed.data);
	utstring_free(feed.blog_title);
	utstring_free(feed.author);
	sqlite3_finalize(feed.stmt);
	sqlite3_finalize(feed.tags);

	free(feed.xmlpath->data);
	free(feed.xmlpath);
	return (0);
};
................................................................................
	neoerr = cs_render(parse, &cs_output_data, cplanet_output);

	if (neoerr != STATUS_OK)
		goto warn0;

	FILE *output = fopen((const char *)cs_output, "w+");
	if (output == NULL)
		errx(1, "%s", cs_output);
	fprintf(output, "%s", cs_output_data.buf);
	fflush(output);
	fclose(output);
	cs_destroy(&parse);
	string_clear(&cs_output_data);

	return;

warn0:
	cs_destroy(&parse);
warn1:
	string_clear(&cs_output_data);
	nerr_error_string(neoerr,&neoerr_str);
	warnx("%s", neoerr_str.buf);
}

static void
usage(void)
{
	errx(1, "usage: cplanet -c conf.hdf [-l]\n");
}