Reformat code with consistent style

This commit is contained in:
dogeystamp 2021-08-06 14:32:04 -04:00
parent 7637e6f609
commit 9719fbf303
No known key found for this signature in database
GPG Key ID: 4B11A996ADE99001
6 changed files with 142 additions and 143 deletions

View File

@ -22,8 +22,8 @@ itemAction(itemStruct *item, const char *folder)
if (fileName[0]) if (fileName[0])
filePath = ecalloc( filePath = ecalloc(
strlen(folder) + strlen(fileName) + 2, strlen(folder) + strlen(fileName) + 2,
sizeof(char)); sizeof(char));
else { else {
logMsg(1, "Invalid article title.\n"); logMsg(1, "Invalid article title.\n");
@ -83,35 +83,35 @@ main(int argc, char *argv[])
else if (argc != 1) else if (argc != 1)
logMsg(0, "Usage: minrss [-v]\n"); logMsg(0, "Usage: minrss [-v]\n");
unsigned int i = 0; unsigned int i = 0;
initCurl(); initCurl();
outputStruct outputs[LEN(links)]; outputStruct outputs[LEN(links)];
memset(outputs, 0, sizeof(outputs)); memset(outputs, 0, sizeof(outputs));
for (i = 0; i < LEN(links); i++) { for (i = 0; i < LEN(links); i++) {
if (links[0].url[0] == '\0') if (links[0].url[0] == '\0')
logMsg(0, "No feeds, add them in config.def.h\n"); logMsg(0, "No feeds, add them in config.def.h\n");
logMsg(4, "Requesting %s\n", links[i].url); logMsg(4, "Requesting %s\n", links[i].url);
createRequest(links[i].url, &outputs[i]); createRequest(links[i].url, &outputs[i]);
} }
performRequests(finish); performRequests(finish);
logMsg(3, "Finished downloads.\n"); logMsg(3, "Finished downloads.\n");
for (i = 0; i < LEN(links); i++) { for (i = 0; i < LEN(links); i++) {
logMsg(4, "Parsing %s\n", links[i].url); logMsg(4, "Parsing %s\n", links[i].url);
if (outputs[i].buffer && outputs[i].buffer[0]) { if (outputs[i].buffer && outputs[i].buffer[0]) {
readDoc(outputs[i].buffer, links[i].feedName, itemAction); readDoc(outputs[i].buffer, links[i].feedName, itemAction);
free(outputs[i].buffer); free(outputs[i].buffer);
} }
} }
logMsg(3, "Finished parsing feeds.\n"); logMsg(3, "Finished parsing feeds.\n");
return 0; return 0;
} }

112
net.c
View File

@ -12,99 +12,99 @@ static CURLM *multiHandle;
int int
initCurl() initCurl()
{ {
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
multiHandle = curl_multi_init(); multiHandle = curl_multi_init();
return !multiHandle; return !multiHandle;
} }
static size_t static size_t
writeCallback(void *ptr, size_t size, size_t nmemb, void *data) writeCallback(void *ptr, size_t size, size_t nmemb, void *data)
{ {
size_t realsize = size * nmemb; size_t realsize = size * nmemb;
outputStruct *mem = (outputStruct*) data; outputStruct *mem = (outputStruct*) data;
mem->buffer = realloc(mem->buffer, mem->size + realsize + 1); mem->buffer = realloc(mem->buffer, mem->size + realsize + 1);
if (mem->buffer) { if (mem->buffer) {
memcpy(&(mem->buffer[mem->size]), ptr, realsize); memcpy(&(mem->buffer[mem->size]), ptr, realsize);
mem->size += realsize; mem->size += realsize;
mem->buffer[mem->size] = 0; mem->buffer[mem->size] = 0;
} }
return realsize; return realsize;
} }
int int
createRequest(const char* url, outputStruct *output) createRequest(const char* url, outputStruct *output)
{ {
CURL *requestHandle = curl_easy_init(); CURL *requestHandle = curl_easy_init();
if (!requestHandle) if (!requestHandle)
logMsg(0, "Can't initialise curl.\n"); logMsg(0, "Can't initialise curl.\n");
output->buffer = NULL; output->buffer = NULL;
output->size = 0; output->size = 0;
CURLcode stat; CURLcode stat;
if (curl_easy_setopt(requestHandle, CURLOPT_URL, url)) { if (curl_easy_setopt(requestHandle, CURLOPT_URL, url)) {
logMsg(1, "Invalid URL: %s\n", url); logMsg(1, "Invalid URL: %s\n", url);
} }
stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEFUNCTION, writeCallback); stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEFUNCTION, writeCallback);
stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEDATA, (void*)output); stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEDATA, (void*)output);
stat = curl_easy_setopt(requestHandle, CURLOPT_MAXREDIRS, maxRedirs); stat = curl_easy_setopt(requestHandle, CURLOPT_MAXREDIRS, maxRedirs);
stat = curl_easy_setopt(requestHandle, CURLOPT_FOLLOWLOCATION, 1L); stat = curl_easy_setopt(requestHandle, CURLOPT_FOLLOWLOCATION, 1L);
if (stat) { if (stat) {
fprintf(stderr, "Unexpected curl error: %s.\n", curl_easy_strerror(stat)); fprintf(stderr, "Unexpected curl error: %s.\n", curl_easy_strerror(stat));
return 1; return 1;
} }
CURLMcode multiStat = curl_multi_add_handle(multiHandle, requestHandle); CURLMcode multiStat = curl_multi_add_handle(multiHandle, requestHandle);
if (multiStat) { if (multiStat) {
fprintf(stderr, "Unexpected curl error: %s.\n", curl_multi_strerror(multiStat)); fprintf(stderr, "Unexpected curl error: %s.\n", curl_multi_strerror(multiStat));
return 1; return 1;
} }
return 0; return 0;
} }
int int
performRequests(void callback(char *, long)) performRequests(void callback(char *, long))
{ {
int runningRequests; int runningRequests;
do { do {
curl_multi_wait(multiHandle, NULL, 0, 1000, NULL); curl_multi_wait(multiHandle, NULL, 0, 1000, NULL);
curl_multi_perform(multiHandle, &runningRequests); curl_multi_perform(multiHandle, &runningRequests);
CURLMsg* msg; CURLMsg* msg;
int queueMsgs; int queueMsgs;
while ((msg = curl_multi_info_read(multiHandle, &queueMsgs))) { while ((msg = curl_multi_info_read(multiHandle, &queueMsgs))) {
if (msg->msg == CURLMSG_DONE) { if (msg->msg == CURLMSG_DONE) {
CURL *requestHandle = msg->easy_handle; CURL *requestHandle = msg->easy_handle;
char *url = NULL; char *url = NULL;
long responseCode = 0; long responseCode = 0;
curl_easy_getinfo(requestHandle, CURLINFO_EFFECTIVE_URL, &url); curl_easy_getinfo(requestHandle, CURLINFO_EFFECTIVE_URL, &url);
curl_easy_getinfo(requestHandle, CURLINFO_RESPONSE_CODE, &responseCode); curl_easy_getinfo(requestHandle, CURLINFO_RESPONSE_CODE, &responseCode);
callback(url, responseCode); callback(url, responseCode);
curl_multi_remove_handle(multiHandle, requestHandle); curl_multi_remove_handle(multiHandle, requestHandle);
curl_easy_cleanup(requestHandle); curl_easy_cleanup(requestHandle);
} }
} }
// > 0 because curl puts negative numbers when there's broken requests // > 0 because curl puts negative numbers when there's broken requests
} while (runningRequests > 0); } while (runningRequests > 0);
curl_multi_cleanup(multiHandle); curl_multi_cleanup(multiHandle);
return 0; return 0;
} }

4
net.h
View File

@ -1,8 +1,8 @@
#include <curl/curl.h> #include <curl/curl.h>
typedef struct { typedef struct {
char *buffer; char *buffer;
size_t size; size_t size;
} outputStruct; } outputStruct;
int initCurl(); int initCurl();

22
util.c
View File

@ -9,16 +9,16 @@
void void
logMsg(int lvl, char *msg, ...) logMsg(int lvl, char *msg, ...)
{ {
va_list args; va_list args;
va_start(args, msg); va_start(args, msg);
if (lvl <= logLevel) if (lvl <= logLevel)
vfprintf(stderr, msg, args); vfprintf(stderr, msg, args);
va_end(args); va_end(args);
if (!lvl) if (!lvl)
exit(1); exit(1);
} }
void * void *
@ -63,11 +63,11 @@ san(char *str, int rep)
char c = dup[i]; char c = dup[i];
if ((c >= 'a' && c <= 'z') || if ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || (c >= '0' && c <= '9') ||
(c == '.' && i - offset != 0) || (c == '.' && i - offset != 0) ||
c == '-' || c == '_' || c == '-' || c == '_' ||
c == ' ') c == ' ')
dup[i - offset] = dup[i]; dup[i - offset] = dup[i];
else else
offset++; offset++;
@ -81,8 +81,8 @@ san(char *str, int rep)
char fsep() char fsep()
{ {
#ifdef _WIN32 #ifdef _WIN32
return '\\'; return '\\';
#else #else
return '/'; return '/';
#endif #endif
} }

107
xml.c
View File

@ -39,49 +39,49 @@ freeItem(itemStruct *item)
static int static int
parseXml(xmlDocPtr doc, parseXml(xmlDocPtr doc,
const char *feedName, const char *feedName,
void itemAction(itemStruct *, const char *)) void itemAction(itemStruct *, const char *))
{ {
if (!feedName || !feedName[0]) { if (!feedName || !feedName[0]) {
logMsg(1, "Missing feed name, please set one.\n"); logMsg(1, "Missing feed name, please set one.\n");
return 1; return 1;
} }
xmlNodePtr rootNode; xmlNodePtr rootNode;
rootNode = xmlDocGetRootElement(doc); rootNode = xmlDocGetRootElement(doc);
if (!rootNode) { if (!rootNode) {
logMsg(1, "Empty document for feed. Skipping...\n"); logMsg(1, "Empty document for feed. Skipping...\n");
return 1; return 1;
} }
if (!TAGIS(rootNode, "rss")) { if (!TAGIS(rootNode, "rss")) {
logMsg(1, "XML document is not an RSS feed. Skipping...\n"); logMsg(1, "XML document is not an RSS feed. Skipping...\n");
return 1; return 1;
} }
xmlChar *key; xmlChar *key;
// Get channel XML tag // Get channel XML tag
xmlNodePtr channel = rootNode->children; xmlNodePtr channel = rootNode->children;
while(channel && !TAGIS(channel, "channel")) while(channel && !TAGIS(channel, "channel"))
channel = channel->next; channel = channel->next;
if (!channel || !TAGIS(channel, "channel")) { if (!channel || !TAGIS(channel, "channel")) {
logMsg(1, "Invalid RSS syntax. Skipping...\n"); logMsg(1, "Invalid RSS syntax. Skipping...\n");
} }
xmlNodePtr cur = channel->children; xmlNodePtr cur = channel->children;
itemStruct *prev = NULL; itemStruct *prev = NULL;
while (cur) { while (cur) {
key = xmlNodeListGetString(doc, cur->children, 1); key = xmlNodeListGetString(doc, cur->children, 1);
if (TAGIS(cur, "item")) { if (TAGIS(cur, "item")) {
itemStruct *item = ecalloc(1, sizeof(itemStruct)); itemStruct *item = ecalloc(1, sizeof(itemStruct));
item->next = prev; item->next = prev;
@ -125,32 +125,31 @@ parseXml(xmlDocPtr doc,
if (TAGIS(itemNode, "category")) { if (TAGIS(itemNode, "category")) {
if (item->categories) { if (item->categories) {
erealloc(item->categories, erealloc(item->categories,
strlen(item->categories) + strlen(itemKey) + 2); strlen(item->categories) + strlen(itemKey) + 2);
strcat(item->categories, " "); strcat(item->categories, " ");
strcat(item->categories, itemKey); strcat(item->categories, itemKey);
} } else {
else {
item->categories = ecalloc( item->categories = ecalloc(
strlen(itemKey) + 2, strlen(itemKey) + 2,
sizeof(char)); sizeof(char));
strcpy(item->categories, itemKey); strcpy(item->categories, itemKey);
} }
} }
if (TAGIS(itemNode, "enclosure")) { if (TAGIS(itemNode, "enclosure")) {
item->enclosureUrl = item->enclosureUrl =
(char *) xmlGetProp(itemNode, (xmlChar *) "url"); (char *) xmlGetProp(itemNode, (xmlChar *) "url");
item->enclosureType = item->enclosureType =
(char *) xmlGetProp(itemNode, (xmlChar *) "type"); (char *) xmlGetProp(itemNode, (xmlChar *) "type");
char *endPtr; char *endPtr;
errno = 0; errno = 0;
item->enclosureLen = strtoul( item->enclosureLen = strtoul(
(char *) xmlGetProp(itemNode, (xmlChar *) "length"), (char *) xmlGetProp(itemNode, (xmlChar *) "length"),
&endPtr, &endPtr,
10); 10);
if (errno) if (errno)
logMsg(1, "Invalid RSS: enclosure length is invalid.\n"); logMsg(1, "Invalid RSS: enclosure length is invalid.\n");
@ -160,41 +159,41 @@ parseXml(xmlDocPtr doc,
itemNode = itemNode->next; itemNode = itemNode->next;
} }
} }
xmlFree(key); xmlFree(key);
cur = cur->next; cur = cur->next;
} }
errno = 0; errno = 0;
int stat = mkdir((const char* ) feedName, S_IRWXU); int stat = mkdir((const char* ) feedName, S_IRWXU);
if (!stat && errno && errno != EEXIST) { if (!stat && errno && errno != EEXIST) {
logMsg(1, "Error creating directory for feed.\n"); logMsg(1, "Error creating directory for feed.\n");
return 1; return 1;
} }
itemAction(prev, feedName); itemAction(prev, feedName);
return 0; return 0;
} }
int int
readDoc(char *content, readDoc(char *content,
const char *feedName, const char *feedName,
void itemAction(itemStruct *, const char *)) void itemAction(itemStruct *, const char *))
{ {
xmlDocPtr doc; xmlDocPtr doc;
doc = xmlReadMemory(content, strlen(content), "noname.xml", NULL, 0); doc = xmlReadMemory(content, strlen(content), "noname.xml", NULL, 0);
if (!doc) { if (!doc) {
logMsg(1, "XML parser error.\n"); logMsg(1, "XML parser error.\n");
return 1; return 1;
} }
int stat = parseXml(doc, feedName, itemAction); int stat = parseXml(doc, feedName, itemAction);
xmlFreeDoc(doc); xmlFreeDoc(doc);
return stat; return stat;
} }

6
xml.h
View File

@ -19,6 +19,6 @@ struct itemStruct {
void freeItem(itemStruct *item); void freeItem(itemStruct *item);
int readDoc( int readDoc(
char *content, char *content,
const char *feedName, const char *feedName,
void itemAction(itemStruct *, char const *chanTitle)); void itemAction(itemStruct *, char const *chanTitle));