tapas

[discontinued] A small program used for compiling refer output into the APA reference format.
git clone https://noxz.tech/git/tapas.git
Log | Files | README | LICENSE

commit: 1c20fa156896313da2aca9bc68a5765fe0798e08
parent: cd78b8f851c7f97d3a168f3cf82542da188aea64
author: Chris Noxz <chris@noxz.tech>
date:   Sun, 10 Nov 2019 20:14:43 +0100
Declutter some more...
Mtapas.c296+++++++++++---------
1 file changed, 158 insertions(+), 138 deletions(-)
diff --git a/tapas.c b/tapas.c
@@ -37,20 +37,6 @@
 #define STR_IN      ".ds APA_IN "
 #define STR_MS      ".ds APA_MS "
 
-struct Settings {
-    char heading[BUF_SIZE];
-    char and[BUF_SIZE];
-    char in[BUF_SIZE];
-    char ms[BUF_SIZE];
-};
-struct Settings settings;
-
-enum macrosets {
-    ms_ms,
-//    ms_mom,
-    /* add more macro sets here... */
-};
-
 typedef struct {
     char author[BUF_SIZE];
     char title[BUF_SIZE];
@@ -80,18 +66,39 @@ typedef struct {
     char url[BUF_SIZE];
 } Referece;
 
+struct State {
+    int inbib;
+    Referece *ref;
+};
+struct State state;
+
+struct Settings {
+    int macroset;
+    char heading[BUF_SIZE];
+    char and[BUF_SIZE];
+    char in[BUF_SIZE];
+    char ms[BUF_SIZE];
+};
+struct Settings settings;
+
+enum macrosets {
+    ms_ms,
+//    ms_mom,
+    /* add more macro sets here... */
+};
+
 static void trim(char*);
 static int readstr(const char*, const char*, char*, int);
 static int readattr(const char*, const char*, char*, char*, int);
-static void setattr(Referece*, char, char*);
-static void printref(Referece*, int, int);
-static void wordsym(Referece*);
+static void setattr(char, char*);
+static void printref(int);
 static void findreplace(char*, const char*, const char*);
-static void wordsym(Referece*);
-static void format_article(Referece*, int);
-static void format_book(Referece*, int);
-static void format_article_in_book(Referece*, int);
+static void wordsym();
+static void format_article();
+static void format_book();
+static void format_article_in_book();
 static int loadstr(char*);
+static void beginbib(void);
 
 void
 trim(char *source)
@@ -153,47 +160,50 @@ readattr(const char *src, const char *fmt, char *t, char *val, int length)
 }
 
 void
-setattr(Referece *ref, char type, char *val)
+setattr(char type, char *val)
 {
     switch (type) {
-    case 'A': strcpy(ref->author, val); break;
-    case 'T': strcpy(ref->title, val); break;
-    case 'B': strcpy(ref->book_title, val); break;
-    case 'R': strcpy(ref->report_number, val); break;
-    case 'J': strcpy(ref->journal_name, val); break;
-    case 'E': strcpy(ref->editor, val); break;
-    case 'e': strcpy(ref->edition, val); break;
-    case 'V': strcpy(ref->volume, val); break;
-    case 'N': strcpy(ref->journal_number, val); break;
-    case 'S': strcpy(ref->series, val); break;
-    case 'C': strcpy(ref->city, val); break;
-    case 'I': strcpy(ref->publisher, val); break;
-    case 'D': strcpy(ref->publication_date, val); break;
-    case 'P': strcpy(ref->page_number, val); break;
-    case 'G': strcpy(ref->gov_number, val); break;
-    case 'O': strcpy(ref->other, val); break;
-    case 'K': strcpy(ref->keywords, val); break;
-    case 'd': strcpy(ref->original_pub_date, val); break;
-    case 'a': strcpy(ref->additions, val); break;
-    case 't': strcpy(ref->reprint_title, val); break;
-    case 'l': strcpy(ref->translator, val); break;
-    case 'r': strcpy(ref->translator_editor, val); break;
-    case 's': strcpy(ref->site_name, val); break;
-    case 'c': strcpy(ref->site_content, val); break;
-    case 'o': strcpy(ref->organization, val); break;
-    case 'u': strcpy(ref->url, val); break;
+    case 'A': strcpy(state.ref->author, val); break;
+    case 'T': strcpy(state.ref->title, val); break;
+    case 'B': strcpy(state.ref->book_title, val); break;
+    case 'R': strcpy(state.ref->report_number, val); break;
+    case 'J': strcpy(state.ref->journal_name, val); break;
+    case 'E': strcpy(state.ref->editor, val); break;
+    case 'e': strcpy(state.ref->edition, val); break;
+    case 'V': strcpy(state.ref->volume, val); break;
+    case 'N': strcpy(state.ref->journal_number, val); break;
+    case 'S': strcpy(state.ref->series, val); break;
+    case 'C': strcpy(state.ref->city, val); break;
+    case 'I': strcpy(state.ref->publisher, val); break;
+    case 'D': strcpy(state.ref->publication_date, val); break;
+    case 'P': strcpy(state.ref->page_number, val); break;
+    case 'G': strcpy(state.ref->gov_number, val); break;
+    case 'O': strcpy(state.ref->other, val); break;
+    case 'K': strcpy(state.ref->keywords, val); break;
+    case 'd': strcpy(state.ref->original_pub_date, val); break;
+    case 'a': strcpy(state.ref->additions, val); break;
+    case 't': strcpy(state.ref->reprint_title, val); break;
+    case 'l': strcpy(state.ref->translator, val); break;
+    case 'r': strcpy(state.ref->translator_editor, val); break;
+    case 's': strcpy(state.ref->site_name, val); break;
+    case 'c': strcpy(state.ref->site_content, val); break;
+    case 'o': strcpy(state.ref->organization, val); break;
+    case 'u': strcpy(state.ref->url, val); break;
     }
 }
 
 void
-printref(Referece *ref, int type, int macroset)
+printref(int type)
 {
-    wordsym(ref);
+    wordsym();
     switch (type) {
-    case 1: format_article(ref, macroset); break;
-    case 2: format_book(ref, macroset); break;
-    case 3: format_article_in_book(ref, macroset); break;
+    case 1: format_article(); break;
+    case 2: format_book(); break;
+    case 3: format_article_in_book(); break;
     }
+
+    free(state.ref);
+    state.ref = NULL;
 }
 
 void
@@ -219,91 +229,91 @@ findreplace(char *source, const char *find, const char *replace)
 }
 
 void
-wordsym(Referece *ref)
+wordsym()
 {
     char str_and[BUF_SIZE];
 
     snprintf(str_and, BUF_SIZE, " %s ", settings.and);
 
-    if (ref->author[0])
-        findreplace(ref->author, " and ", str_and);
-    if (ref->editor[0])
-        findreplace(ref->editor, " and ", str_and);
+    if (state.ref->author[0])
+        findreplace(state.ref->author, " and ", str_and);
+    if (state.ref->editor[0])
+        findreplace(state.ref->editor, " and ", str_and);
 }
 
 void
-format_article(Referece *ref, int macroset)
+format_article()
 {
-    if (!ref->author
-        || !ref->publication_date
-        || !ref->title
-        || !ref->journal_name
-        || !ref->journal_number
-        || !ref->city
-        || !ref->publisher)
+    if (!state.ref->author
+        || !state.ref->publication_date
+        || !state.ref->title
+        || !state.ref->journal_name
+        || !state.ref->journal_number
+        || !state.ref->city
+        || !state.ref->publisher)
         return;
-    switch (macroset) {
+    switch (settings.macroset) {
     case ms_ms:
         fprintf(stdout,
             ".XP\n%s (%s). \n.I \"%s\" \" (%s, %s).\"\n%s: %s.\n",
-            ref->author,
-            ref->publication_date,
-            ref->title,
-            ref->journal_name,
-            ref->journal_number,
-            ref->city,
-            ref->publisher
+            state.ref->author,
+            state.ref->publication_date,
+            state.ref->title,
+            state.ref->journal_name,
+            state.ref->journal_number,
+            state.ref->city,
+            state.ref->publisher
         ); break;
     }
 }
 
 void
-format_book(Referece *ref, int macroset)
+format_book()
 {
-    if (!ref->author
-        || !ref->publication_date
-        || !ref->title
-        || !ref->city
-        || !ref->publisher)
+    if (!state.ref->author
+        || !state.ref->publication_date
+        || !state.ref->title
+        || !state.ref->city
+        || !state.ref->publisher)
         return;
-    switch (macroset) {
+    switch (settings.macroset) {
     case ms_ms:
     fprintf(stdout,
         ".XP\n%s (%s).\n.I \"%s\" .\n%s: %s.\n",
-        ref->author,
-        ref->publication_date,
-        ref->title,
-        ref->city,
-        ref->publisher
+        state.ref->author,
+        state.ref->publication_date,
+        state.ref->title,
+        state.ref->city,
+        state.ref->publisher
         ); break;
     }
 }
 
 void
-format_article_in_book(Referece *ref, int macroset)
+format_article_in_book()
 {
-    if (!ref->author
-        || !ref->publication_date
-        || !ref->title
-        || !ref->editor
-        || !ref->book_title
-        || !ref->page_number
-        || !ref->city
-        || !ref->publisher)
+    if (!state.ref->author
+        || !state.ref->publication_date
+        || !state.ref->title
+        || !state.ref->editor
+        || !state.ref->book_title
+        || !state.ref->page_number
+        || !state.ref->city
+        || !state.ref->publisher)
         return;
-    switch (macroset) {
+    switch (settings.macroset) {
     case ms_ms:
     fprintf(stdout,
         ".XP\n%s (%s). %s. %s %s, \n.I \"%s\" \" (s. %s).\"\n%s: %s.\n",
-        ref->author,
-        ref->publication_date,
-        ref->title,
+        state.ref->author,
+        state.ref->publication_date,
+        state.ref->title,
         settings.in,
-        ref->editor,
-        ref->book_title,
-        ref->page_number,
-        ref->city,
-        ref->publisher
+        state.ref->editor,
+        state.ref->book_title,
+        state.ref->page_number,
+        state.ref->city,
+        state.ref->publisher
         ); break;
     }
 }
@@ -324,17 +334,29 @@ loadstr(char *line)
     return 1;
 }
 
+void
+beginbib(void)
+{
+    state.inbib = 1;
+
+    if (strcmp(settings.ms, "ms"))
+        settings.macroset = ms_ms;
+    else
+        settings.macroset = ms_ms;
+
+    switch (settings.macroset) {
+    case ms_ms:
+        fprintf(stdout, ".SH\n%s\n", settings.heading); break;
+    }
+}
+
 int
 main(int arc, char *argv[])
 {
-    int inbib = 0;
-    int macroset = ms_ms;
-    int reftype;
-    char atype;
-    char val[BUF_SIZE];
-    size_t size;
+    char type, val[BUF_SIZE];
     char *line;
-    Referece *ref = NULL;
+    size_t size;
+    int reftype;
 
     /* default settings */
     strcpy(settings.heading, "References");
@@ -342,40 +364,38 @@ main(int arc, char *argv[])
     strcpy(settings.in, "In");
     strcpy(settings.ms, "");
 
+    /* default state */
+    state.inbib = 0;
+    state.ref = NULL;
+
+    /* handle lines */
     while (getline(&line, &size, stdin) != EOF) {
+        /* handle strings */
         if (loadstr(line))
             continue;
-        if (!inbib && strcmp(line, BIB_START) == 0) {
-            inbib = 1;
-            switch (macroset) {
-            case ms_ms:
-                fprintf(stdout, ".SH\n%s\n", settings.heading); break;
-            } continue;
-        }
-        if (inbib && strcmp(line, BIB_END) == 0) {
-            inbib = 0;
-            if (strcmp(settings.ms, "ms"))
-                macroset = ms_ms;
-            //else if (strcmp(settings.ms, "mom"))
-            //    macroset = ms_mom;
-            /* add more macro sets here... */
-            else
-                macroset = ms_ms;
+
+        /* handle states */
+        if (!state.inbib && strcmp(line, BIB_START) == 0) {
+            beginbib();
+            continue;
+        } else if (state.inbib && strcmp(line, BIB_END) == 0) {
+            state.inbib = 0;
             continue;
         }
-        if (inbib) {
-            if (!ref && strcmp(line, REF_START) == 0) {
-                ref = (Referece*)calloc(1, sizeof(Referece));
-            } else if (ref && readattr(line, REF_ATTR, &atype, val, BUF_SIZE)) {
-                setattr(ref, atype, val);
-            } else if (ref && sscanf(line, REF_END, &reftype) == 1) {
-                printref(ref, reftype, macroset);
-                free(ref);
-                ref = NULL;
-            }
+
+        /* print line if not in bibliography */
+        if (!state.inbib) {
+            printf("%s", line);
             continue;
         }
-        printf("%s", line);
+
+        /* handle bibliography */
+        if (!state.ref && strcmp(line, REF_START) == 0)
+            state.ref = (Referece*)calloc(1, sizeof(Referece));
+        else if (state.ref && readattr(line, REF_ATTR, &type, val, BUF_SIZE))
+            setattr(type, val);
+        else if (state.ref && sscanf(line, REF_END, &reftype) == 1)
+            printref(reftype);
     }
     return 0;
 }