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])
filePath = ecalloc(
strlen(folder) + strlen(fileName) + 2,
sizeof(char));
strlen(folder) + strlen(fileName) + 2,
sizeof(char));
else {
logMsg(1, "Invalid article title.\n");
@ -83,35 +83,35 @@ main(int argc, char *argv[])
else if (argc != 1)
logMsg(0, "Usage: minrss [-v]\n");
unsigned int i = 0;
unsigned int i = 0;
initCurl();
initCurl();
outputStruct outputs[LEN(links)];
memset(outputs, 0, sizeof(outputs));
outputStruct outputs[LEN(links)];
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')
logMsg(0, "No feeds, add them in config.def.h\n");
logMsg(4, "Requesting %s\n", links[i].url);
createRequest(links[i].url, &outputs[i]);
}
logMsg(4, "Requesting %s\n", links[i].url);
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++) {
logMsg(4, "Parsing %s\n", links[i].url);
for (i = 0; i < LEN(links); i++) {
logMsg(4, "Parsing %s\n", links[i].url);
if (outputs[i].buffer && outputs[i].buffer[0]) {
readDoc(outputs[i].buffer, links[i].feedName, itemAction);
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
initCurl()
{
curl_global_init(CURL_GLOBAL_ALL);
multiHandle = curl_multi_init();
curl_global_init(CURL_GLOBAL_ALL);
multiHandle = curl_multi_init();
return !multiHandle;
return !multiHandle;
}
static size_t
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) {
memcpy(&(mem->buffer[mem->size]), ptr, realsize);
if (mem->buffer) {
memcpy(&(mem->buffer[mem->size]), ptr, realsize);
mem->size += realsize;
mem->buffer[mem->size] = 0;
}
mem->size += realsize;
mem->buffer[mem->size] = 0;
}
return realsize;
return realsize;
}
int
createRequest(const char* url, outputStruct *output)
{
CURL *requestHandle = curl_easy_init();
CURL *requestHandle = curl_easy_init();
if (!requestHandle)
logMsg(0, "Can't initialise curl.\n");
if (!requestHandle)
logMsg(0, "Can't initialise curl.\n");
output->buffer = NULL;
output->size = 0;
output->buffer = NULL;
output->size = 0;
CURLcode stat;
if (curl_easy_setopt(requestHandle, CURLOPT_URL, url)) {
logMsg(1, "Invalid URL: %s\n", url);
}
CURLcode stat;
if (curl_easy_setopt(requestHandle, CURLOPT_URL, url)) {
logMsg(1, "Invalid URL: %s\n", url);
}
stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEFUNCTION, writeCallback);
stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEDATA, (void*)output);
stat = curl_easy_setopt(requestHandle, CURLOPT_MAXREDIRS, maxRedirs);
stat = curl_easy_setopt(requestHandle, CURLOPT_FOLLOWLOCATION, 1L);
stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEFUNCTION, writeCallback);
stat = curl_easy_setopt(requestHandle, CURLOPT_WRITEDATA, (void*)output);
stat = curl_easy_setopt(requestHandle, CURLOPT_MAXREDIRS, maxRedirs);
stat = curl_easy_setopt(requestHandle, CURLOPT_FOLLOWLOCATION, 1L);
if (stat) {
fprintf(stderr, "Unexpected curl error: %s.\n", curl_easy_strerror(stat));
return 1;
}
if (stat) {
fprintf(stderr, "Unexpected curl error: %s.\n", curl_easy_strerror(stat));
return 1;
}
CURLMcode multiStat = curl_multi_add_handle(multiHandle, requestHandle);
if (multiStat) {
fprintf(stderr, "Unexpected curl error: %s.\n", curl_multi_strerror(multiStat));
return 1;
}
CURLMcode multiStat = curl_multi_add_handle(multiHandle, requestHandle);
if (multiStat) {
fprintf(stderr, "Unexpected curl error: %s.\n", curl_multi_strerror(multiStat));
return 1;
}
return 0;
return 0;
}
int
performRequests(void callback(char *, long))
{
int runningRequests;
int runningRequests;
do {
curl_multi_wait(multiHandle, NULL, 0, 1000, NULL);
curl_multi_perform(multiHandle, &runningRequests);
do {
curl_multi_wait(multiHandle, NULL, 0, 1000, NULL);
curl_multi_perform(multiHandle, &runningRequests);
CURLMsg* msg;
CURLMsg* msg;
int queueMsgs;
int queueMsgs;
while ((msg = curl_multi_info_read(multiHandle, &queueMsgs))) {
if (msg->msg == CURLMSG_DONE) {
CURL *requestHandle = msg->easy_handle;
while ((msg = curl_multi_info_read(multiHandle, &queueMsgs))) {
if (msg->msg == CURLMSG_DONE) {
CURL *requestHandle = msg->easy_handle;
char *url = NULL;
char *url = NULL;
long responseCode = 0;
curl_easy_getinfo(requestHandle, CURLINFO_EFFECTIVE_URL, &url);
curl_easy_getinfo(requestHandle, CURLINFO_RESPONSE_CODE, &responseCode);
curl_easy_getinfo(requestHandle, CURLINFO_EFFECTIVE_URL, &url);
curl_easy_getinfo(requestHandle, CURLINFO_RESPONSE_CODE, &responseCode);
callback(url, responseCode);
callback(url, responseCode);
curl_multi_remove_handle(multiHandle, requestHandle);
curl_easy_cleanup(requestHandle);
}
}
// > 0 because curl puts negative numbers when there's broken requests
} while (runningRequests > 0);
curl_multi_remove_handle(multiHandle, requestHandle);
curl_easy_cleanup(requestHandle);
}
}
// > 0 because curl puts negative numbers when there's broken requests
} 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>
typedef struct {
char *buffer;
size_t size;
char *buffer;
size_t size;
} outputStruct;
int initCurl();

22
util.c
View File

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

107
xml.c
View File

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