File indexing completed on 2024-05-12 11:33:01

0001 /*
0002  * Copyright (C) 2018 David L Parsons.
0003  * The redistribution terms are provided in the COPYRIGHT file that must
0004  * be distributed with this source code.
0005  */
0006 #include "config.h"
0007 #include "pgm_options.h"
0008 
0009 #include <stdio.h>
0010 #include <stdlib.h>
0011 #include <string.h>
0012 #include <stdarg.h>
0013 #include <sys/types.h>
0014 #include <sys/stat.h>
0015 #include <time.h>
0016 #if HAVE_PWD_H
0017 #  include <pwd.h>
0018 #endif
0019 #include <fcntl.h>
0020 #include <errno.h>
0021 #include <ctype.h>
0022 #include <unistd.h>
0023 
0024 #include "mkdio.h"
0025 #include "cstring.h"
0026 #include "amalloc.h"
0027 #include "gethopt.h"
0028 
0029 char *pgm = "pandoc headers";
0030 
0031 void
0032 fail(char *why, ...)
0033 {
0034     va_list ptr;
0035 
0036     va_start(ptr,why);
0037     fprintf(stderr, "%s: ", pgm);
0038     vfprintf(stderr, why, ptr);
0039     fputc('\n', stderr);
0040     va_end(ptr);
0041     exit(1);
0042 }
0043 
0044 
0045 struct h_opt opts[] = {
0046     { 0, "author", 'a', 0, "show the author line" },
0047     { 0, "title" , 't', 0, "show the title line" },
0048     { 0, "date"  , 'd', 0, "show the date line" },
0049 } ;
0050 #define NROPTS (sizeof opts / sizeof opts[0])
0051 
0052 int
0053 main(argc, argv)
0054 char **argv;
0055 {
0056     int show_author=0, show_title=0, show_date=0;
0057     MMIOT *p;
0058     char *res;
0059     struct h_opt *opt;
0060     struct h_context blob;
0061     
0062     hoptset(&blob, argc, argv);
0063     
0064     while ( opt = gethopt(&blob, opts, NROPTS) ) {
0065     if ( opt && (opt != HOPTERR) ) {
0066         switch ( opt->optchar ) {
0067         case 'a':   show_author = 1;
0068             break;
0069         case 't':   show_title = 1;
0070             break;
0071         case 'd':   show_date = 1;
0072             break;
0073         }
0074     }
0075     }
0076 
0077     p = mkd_in(stdin, 0);
0078 
0079     if ( p == 0 )
0080     fail("could not read input?");
0081 
0082     if ( !mkd_compile(p, 0) )
0083     fail("could not compile input?");
0084     
0085     if (show_author) {
0086     if ( res = mkd_doc_author(p) )
0087         printf("author: %s\n", res);
0088     }
0089     if (show_title) {
0090     if ( res = mkd_doc_title(p) )
0091         printf("title : %s\n", res);
0092     }
0093     if (show_date) {
0094     if ( res = mkd_doc_date(p) )
0095         printf("date   : %s\n", res);
0096     }
0097     mkd_cleanup(p);
0098     exit(0);
0099 }