Skip to content

Commit

Permalink
Merge branch 'gethopt-uber-alles'
Browse files Browse the repository at this point in the history
Conflicts:

	Makefile.in
	msvc/Makefile
	msvc/README.md
  • Loading branch information
Orc committed Feb 3, 2017
2 parents d41cf2e + 79367e6 commit 82c24c6
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 84 deletions.
22 changes: 14 additions & 8 deletions Makefile.in
Expand Up @@ -21,6 +21,9 @@ OBJS=mkdio.o markdown.o dumptree.o generate.o \
@AMALLOC@ @H1TITLE@
TESTFRAMEWORK=echo cols branch

# modules that markdown, makepage, mkd2html, &tc use
COMMON=pgm_options.o gethopt.o

MAN3PAGES=mkd-callbacks.3 mkd-functions.3 markdown.3 mkd-line.3

all: $(PGMS) $(SAMPLE_PGMS) $(TESTFRAMEWORK)
Expand Down Expand Up @@ -85,22 +88,25 @@ blocktags: mktags
./mktags > blocktags

# example programs
@THEME@theme: theme.o $(MKDLIB) mkdio.h
@THEME@ $(CC) $(CFLAGS) $(LFLAGS) -o theme theme.o pgm_options.o -lmarkdown @LIBS@
@THEME@theme: theme.o $(COMMON) $(MKDLIB) mkdio.h
@THEME@ $(CC) $(CFLAGS) $(LFLAGS) -o theme theme.o $(COMMON) -lmarkdown @LIBS@


mkd2html: mkd2html.o $(MKDLIB) mkdio.h gethopt.h gethopt.o
$(CC) $(CFLAGS) $(LFLAGS) -o mkd2html mkd2html.o gethopt.o -lmarkdown @LIBS@
mkd2html: mkd2html.o $(MKDLIB) mkdio.h gethopt.h $(COMMON)
$(CC) $(CFLAGS) $(LFLAGS) -o mkd2html mkd2html.o $(COMMON) -lmarkdown @LIBS@

markdown: main.o pgm_options.o $(MKDLIB)
$(CC) $(CFLAGS) $(LFLAGS) -o markdown main.o pgm_options.o -lmarkdown @LIBS@
markdown: main.o $(COMMON) $(MKDLIB)
$(CC) $(CFLAGS) $(LFLAGS) -o markdown main.o $(COMMON) -lmarkdown @LIBS@

makepage: makepage.c pgm_options.o $(MKDLIB) mkdio.h
$(CC) $(CFLAGS) $(LFLAGS) -o makepage makepage.c pgm_options.o -lmarkdown @LIBS@
makepage: makepage.c $(COMMON) $(MKDLIB) mkdio.h
$(CC) $(CFLAGS) $(LFLAGS) -o makepage makepage.c $(COMMON) -lmarkdown @LIBS@

pgm_options.o: pgm_options.c mkdio.h config.h
$(CC) $(CFLAGS) -I. -c pgm_options.c

gethopt.o: gethopt.c
$(CC) $(CFLAGS) -I. -c gethopt.c

main.o: main.c mkdio.h config.h
$(CC) $(CFLAGS) -I. -c main.c

Expand Down
45 changes: 45 additions & 0 deletions gethopt.c
Expand Up @@ -177,6 +177,51 @@ int nropts;
}


void
hoptusage(char *pgm, struct h_opt opts[], int nropts, char *arguments)
{
int i;
int optcount;

fprintf(stderr, "usage: %s", pgm);

/* print out the options that don't have flags first */

for ( optcount=i=0; i < nropts; i++ ) {
if ( opts[i].optchar && !opts[i].opthasarg) {
if (optcount == 0 )
fputs(" [-", stderr);
fputc(opts[i].optchar, stderr);
optcount++;
}
}
if ( optcount )
fputc(']', stderr);

/* print out the options WITH flags */
for ( i = 0; i < nropts; i++ )
if ( opts[i].optchar && opts[i].opthasarg)
fprintf(stderr, " [-%c %s]", opts[i].optchar, opts[i].opthasarg);

/* print out the long options */
for ( i = 0; i < nropts; i++ )
if ( opts[i].optword ) {
fprintf(stderr, " [-%s", opts[i].optword);
if ( opts[i].opthasarg )
fprintf(stderr, " %s", opts[i].opthasarg);
fputc(']', stderr);
}

/* print out the arguments string, if any */

if ( arguments )
fprintf(stderr, " %s", arguments);

/* and we're done */
fputc('\n', stderr);
}


#if DEBUG
struct h_opt opts[] = {
{ 0, "css", 0, 1, "css file" },
Expand Down
4 changes: 3 additions & 1 deletion gethopt.h
Expand Up @@ -14,7 +14,7 @@ struct h_opt {
int option;
char *optword;
char optchar;
int opthasarg;
char *opthasarg;
char *optdesc;
} ;

Expand All @@ -38,4 +38,6 @@ extern void hoptset(struct h_context *, int, char **);
extern int hopterr(struct h_context *, int);
extern struct h_opt *gethopt(struct h_context *, struct h_opt*, int);

extern void hoptusage(char *, struct h_opt*, int, char *);

#endif/*__GETHOPT_D*/
67 changes: 44 additions & 23 deletions main.c
Expand Up @@ -13,12 +13,12 @@
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>

#include "config.h"
#include "amalloc.h"
#include "pgm_options.h"
#include "tags.h"
#include "gethopt.h"

#if HAVE_LIBGEN_H
#include <libgen.h>
Expand Down Expand Up @@ -60,10 +60,28 @@ complain(char *fmt, ...)
}


struct h_opt opts[] = {
{ 0, "html5", '5', 0, "recognise html5 block elements" },
{ 0, "base", 'b', "url-base", "URL prefix" },
{ 0, "debug", 'd', 0, "debugging" },
{ 0, "version",'V', 0, "show version info" },
{ 0, 0, 'E', "flags", "url flags" },
{ 0, 0, 'F', "bitmap", "set/show hex flags" },
{ 0, 0, 'f', "{+-}flags", "set/show named flags" },
{ 0, 0, 'G', 0, "github flavoured markdown" },
{ 0, 0, 'n', 0, "don't write generated html" },
{ 0, 0, 's', "text", "format `text`" },
{ 0, "style", 'S', 0, "output <style> blocks" },
{ 0, 0, 't', "text", "format `text` with mkd_line()" },
{ 0, "toc", 'T', 0, "output a TOC" },
{ 0, 0, 'C', "prefix", "prefix for markdown extra footnotes" },
{ 0, 0, 'o', "file", "write output to file" },
};
#define NROPTS (sizeof opts/sizeof opts[0])

int
main(int argc, char **argv)
{
int opt;
int rc;
mkd_flag_t flags = 0;
int debug = 0;
Expand All @@ -81,68 +99,71 @@ main(int argc, char **argv)
char *urlbase = 0;
char *q;
MMIOT *doc;
struct h_context blob;
struct h_opt *opt;

hoptset(&blob, argc, argv);
hopterr(&blob, 1);

if ( q = getenv("MARKDOWN_FLAGS") )
flags = strtol(q, 0, 0);

pgm = basename(argv[0]);
opterr = 1;

while ( (opt=getopt(argc, argv, "5b:C:df:E:F:Gno:s:St:TV")) != EOF ) {
switch (opt) {
while ( opt=gethopt(&blob, opts, NROPTS) ) {
if ( opt == HOPTERR ) {
hoptusage(pgm, opts, NROPTS, "[file]");
exit(1);
}
switch (opt->optchar) {
case '5': with_html5 = 1;
break;
case 'b': urlbase = optarg;
case 'b': urlbase = hoptarg(&blob);
break;
case 'd': debug = 1;
break;
case 'V': version++;
break;
case 'E': urlflags = optarg;
case 'E': urlflags = hoptarg(&blob);
break;
case 'F': if ( strcmp(optarg, "?") == 0 ) {
case 'F': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
show_flags(0, 0);
exit(0);
}
else
flags = strtol(optarg, 0, 0);
flags = strtol(hoptarg(&blob), 0, 0);
break;
case 'f': if ( strcmp(optarg, "?") == 0 ) {
case 'f': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
show_flags(1, version);
exit(0);
}
else if ( !set_flag(&flags, optarg) )
complain("unknown option <%s>", optarg);
else if ( !set_flag(&flags, hoptarg(&blob)) )
complain("unknown option <%s>", hoptarg(&blob));
break;
case 'G': github_flavoured = 1;
break;
case 'n': content = 0;
break;
case 's': text = optarg;
case 's': text = hoptarg(&blob);
break;
case 'S': styles = 1;
break;
case 't': text = optarg;
case 't': text = hoptarg(&blob);
use_mkd_line = 1;
break;
case 'T': toc = 1;
break;
case 'C': extra_footnote_prefix = optarg;
case 'C': extra_footnote_prefix = hoptarg(&blob);
break;
case 'o': if ( ofile ) {
complain("Too many -o options");
exit(1);
}
if ( !freopen(ofile = optarg, "w", stdout) ) {
if ( !freopen(ofile = hoptarg(&blob), "w", stdout) ) {
perror(ofile);
exit(1);
}
break;
default: fprintf(stderr, "usage: %s [-dTV] [-b url-base]"
" [-F bitmap] [-f {+-}flags]"
" [-o ofile] [-s text]"
" [-t text] [file]\n", pgm);
exit(1);
}
}

Expand All @@ -155,8 +176,8 @@ main(int argc, char **argv)
exit(0);
}

argc -= optind;
argv += optind;
argc -= hoptind(&blob);
argv += hoptind(&blob);

if ( with_html5 )
mkd_with_html5_tags();
Expand Down
40 changes: 25 additions & 15 deletions makepage.c
Expand Up @@ -6,9 +6,9 @@
#include <stdlib.h>
#include <string.h>
#include <mkdio.h>
#include <unistd.h>
#include "config.h"
#include "pgm_options.h"
#include "gethopt.h"

#ifndef HAVE_BASENAME
char*
Expand All @@ -22,48 +22,58 @@ basename(char *p)

char *pgm = "makepage";

struct h_opt opts[] = {
{ 0, "version", 'V', 0, "show version info" },
{ 0, 0, 'F', "bitmap", "set/show hex flags" },
{ 0, "flags", 'f', "{+-}flags", "set/show named flags" },
} ;
#define NROPTS (sizeof opts / sizeof opts[0])

main(argc, argv)
int argc;
char **argv;
{
MMIOT *doc;
char *q;
int version = 0;
int opt;
int ret;
mkd_flag_t flags = 0;
struct h_opt *opt;
struct h_context blob;

if ( (q = getenv("MARKDOWN_FLAGS")) )
flags = strtol(q, 0, 0);

opterr = 1;
hoptset(&blob, argc, argv);
hopterr(&blob, 1);

while ( (opt=getopt(argc, argv, "F:f:V")) != EOF ) {
switch (opt) {
while ( opt = gethopt(&blob, opts, NROPTS) ) {
if ( opt == HOPTERR ) {
hoptusage(pgm, opts, NROPTS, "[file]");
exit(1);
}
switch ( opt->optchar ) {
case 'V': version++;
break;
case 'F': if ( strcmp(optarg, "?") == 0 ) {
case 'F': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
show_flags(0,0);
exit(0);
}
else
flags = strtol(optarg, 0, 0);
flags = strtol(hoptarg(&blob), 0, 0);
break;
case 'f': if ( strcmp(optarg, "?") == 0 ) {
case 'f': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
show_flags(1,version);
exit(0);
}
else if ( !set_flag(&flags, optarg) )
fprintf(stderr, "unknown option <%s>\n", optarg);
else if ( !set_flag(&flags, hoptarg(&blob)) )
fprintf(stderr, "unknown option <%s>\n", hoptarg(&blob));
break;
default: fprintf(stderr, "usage: %s [-V] [-F bitmap] [-f {+-}flags]"
" [file]\n", pgm);
exit(1);
}
}

argc -= optind;
argv += optind;
argc -= hoptind(&blob);
argv += hoptind(&blob);

if ( version ) {
printf("%s: discount %s", pgm, markdown_version);
Expand Down

0 comments on commit 82c24c6

Please sign in to comment.