code-style and pledge(2) support - sub - subscene.com subtitle search
 (HTM) git clone git://git.codemadness.org/sub
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit e6b0ea0a0865bfefd753ce290d9415595960f866
 (DIR) parent 2a1ec07886e85085548880b4f39210671818860a
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Fri,  2 Mar 2018 14:04:54 +0100
       
       code-style and pledge(2) support
       
       Diffstat:
         M sub.c                               |      69 ++++++++++++++++++-------------
       
       1 file changed, 40 insertions(+), 29 deletions(-)
       ---
 (DIR) diff --git a/sub.c b/sub.c
       @@ -1,8 +1,14 @@
        #include <ctype.h>
       +#include <errno.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        #include <strings.h>
       +#ifdef __OpenBSD__
       +#include <unistd.h>
       +#else
       +#define pledge(a,b) 0
       +#endif
        
        #include "util.h"
        #include "xml.h"
       @@ -43,44 +49,45 @@ xml_handler_data(XMLParser *p, const char *data, size_t datalen)
                char buf[1024];
                size_t len;
        
       -        if(!curclass[0])
       +        if (!curclass[0])
                        return;
        
                /* skip leading space */
       -        for(s = (char *)data; *s && isspace(*s); s++);
       +        for (s = (char *)data; *s && isspace(*s); s++)
       +                ;
                strlcpy(buf, s, sizeof(buf));
       -        for(s = buf; *s; s++) {
       -                if(*s == '\r' || *s == '\n')
       +        for (s = buf; *s; s++) {
       +                if (*s == '\r' || *s == '\n')
                                *s = ' ';
                }
                /* trim remaining space */
                len = strlen(buf);
       -        for(; len > 0; len--) {
       -                if(!isspace(buf[len - 1]))
       +        for (; len > 0; len--) {
       +                if (!isspace(buf[len - 1]))
                                break;
                        buf[len - 1] = '\0';
                }
        
                s = buf;
       -        if(!strlen(s))
       +        if (!*s)
                        return;
       -        if(strcmp(curclass, "a1") == 0) {
       +        if (strcmp(curclass, "a1") == 0) {
                        /* link */
       -                if(strcmp(spanclass, "") == 0) {
       +                if (strcmp(spanclass, "") == 0) {
                                strlcpy(sub.title, s, sizeof(sub.title));
                        } else {
                                strlcpy(sub.lang, s, sizeof(sub.lang));
                        }
       -        } else if(strcmp(curclass, "a3") == 0) {
       +        } else if (strcmp(curclass, "a3") == 0) {
                        /* files */
                        sub.files = atoi(s);
       -        } else if(strcmp(curclass, "a41") == 0) {
       +        } else if (strcmp(curclass, "a41") == 0) {
                        /* hearing impaired? */
                        sub.hi = 1;
       -        } else if(strcmp(curclass, "a5") == 0) {
       +        } else if (strcmp(curclass, "a5") == 0) {
                        /* author / user profile */
                        strlcpy(sub.author, s, sizeof(sub.author));
       -        } else if(strcmp(curclass, "a6") == 0) {
       +        } else if (strcmp(curclass, "a6") == 0) {
                        /* description */
                        strlcpy(sub.description, s, sizeof(sub.description));
                }
       @@ -92,9 +99,8 @@ xml_handler_start_element(XMLParser *p, const char *tag, size_t taglen)
                (void)p;
                (void)taglen;
        
       -        if(istag(tag, "tr")) {
       +        if (istag(tag, "tr"))
                        memset(&sub, 0, sizeof(sub));
       -        }
        }
        
        static void
       @@ -105,12 +111,12 @@ xml_handler_end_element(XMLParser *p, const char *tag, size_t taglen,
                (void)taglen;
                (void)isshort;
        
       -        if(istag(tag, "tr") && sub.issub == 1) {
       +        if (istag(tag, "tr") && sub.issub == 1) {
                        printf("LANG:%s\tTITLE:%s\tURL:http://subscene.com%s\tHI:%d\tFILES:%d\tAUTHOR:%s\n",
                               sub.lang, sub.title, sub.url, sub.hi, sub.files, sub.author);
       -        } else if(istag(tag, "td")) {
       +        } else if (istag(tag, "td")) {
                        curclass[0] = '\0';
       -        } else if(istag(tag, "span")) {
       +        } else if (istag(tag, "span")) {
                        spanclass[0] = '\0';
                }
        }
       @@ -124,27 +130,27 @@ xml_handler_attr(XMLParser *p, const char *tag, size_t taglen,
                (void)namelen;
                (void)valuelen;
        
       -        if(istag(tag, "td")) {
       -                if(isattr(name, "class")) {
       +        if (istag(tag, "td")) {
       +                if (isattr(name, "class")) {
                                strlcpy(curclass, value, sizeof(curclass));
                                /* link */
       -                        if(strcmp(value, "a1") == 0) {
       +                        if (strcmp(value, "a1") == 0) {
                                        sub.issub = 1;
                                }
                        }
       -        } else if(istag(tag, "span")) {
       -                if(strcmp(curclass, "a1") == 0) {
       -                        if(isattr(name, "class")) {
       +        } else if (istag(tag, "span")) {
       +                if (strcmp(curclass, "a1") == 0) {
       +                        if (isattr(name, "class")) {
                                        strlcpy(spanclass, value, sizeof(spanclass));
                                }
                        }
       -        } else if(istag(tag, "a")) {
       +        } else if (istag(tag, "a")) {
                        /* subtitle / author profile url */
       -                if(strcmp(name, "href") == 0) {
       -                        if((strcmp(curclass, "a1") == 0)) {
       +                if (strcmp(name, "href") == 0) {
       +                        if ((strcmp(curclass, "a1") == 0)) {
                                        strlcpy(sub.url, value, sizeof(sub.url));
                                }
       -                        if((strcmp(curclass, "a5") == 0)) {
       +                        if ((strcmp(curclass, "a5") == 0)) {
                                        strlcpy(sub.authorurl, value, sizeof(sub.authorurl));
                                }
                        }
       @@ -154,6 +160,11 @@ xml_handler_attr(XMLParser *p, const char *tag, size_t taglen,
        int
        main(void)
        {
       +        if (pledge("stdio", NULL) < 0) {
       +                fprintf(stderr, "pledge: %s\n", strerror(errno));
       +                return 1;
       +        }
       +
                xmlparser_init(&parser, stdin);
        
                parser.xmltagstart = xml_handler_start_element;
       @@ -163,5 +174,5 @@ main(void)
        
                xmlparser_parse(&parser);
        
       -        return EXIT_SUCCESS;
       +        return 0;
        }