summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 3834c1fb5f52da211b09b0ac2c9453dcd9e28b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "config.h"
#include "cmark.h"
#include "debug.h"
#include "bench.h"

#if defined(_WIN32) && !defined(__CYGWIN__)
#include <io.h>
#include <fcntl.h>
#endif

typedef enum {
	FORMAT_NONE,
	FORMAT_HTML,
	FORMAT_XML,
	FORMAT_MAN,
} writer_format;

void print_usage()
{
	printf("Usage:   cmark [FILE*]\n");
	printf("Options:\n");
	printf("  --to, -t FORMAT  Specify output format (html, xml, man)\n");
	printf("  --sourcepos      Include source position attribute\n");
	printf("  --hardbreaks     Treat newlines as hard line breaks\n");
	printf("  --smart          Use smart punctuation\n");
	printf("  --normalize      Consolidate adjacent text nodes\n");
	printf("  --help, -h       Print usage information\n");
	printf("  --version        Print version\n");
}

static void print_document(cmark_node *document, writer_format writer,
                           long options)
{
	char *result;
	switch (writer) {
	case FORMAT_HTML:
		result = cmark_render_html(document, options);
		break;
	case FORMAT_XML:
		result = cmark_render_xml(document, options);
		break;
	case FORMAT_MAN:
		result = cmark_render_man(document, options);
		break;
	default:
		fprintf(stderr, "Unknown format %d\n", writer);
		exit(1);
	}
	printf("%s", result);
	free(result);
}

int main(int argc, char *argv[])
{
	int i, numfps = 0;
	int *files;
	char buffer[4096];
	cmark_parser *parser;
	size_t bytes;
	cmark_node *document;
	writer_format writer = FORMAT_HTML;
	long options = CMARK_OPT_DEFAULT;

#if defined(_WIN32) && !defined(__CYGWIN__)
	_setmode(_fileno(stdout), _O_BINARY);
#endif

	parser = cmark_parser_new();
	files = (int *)malloc(argc * sizeof(*files));

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "--version") == 0) {
			printf("cmark %s", CMARK_VERSION_STRING);
			printf(" - CommonMark converter (c) 2014 John MacFarlane\n");
			exit(0);
		} else if (strcmp(argv[i], "--sourcepos") == 0) {
			options |= CMARK_OPT_SOURCEPOS;
		} else if (strcmp(argv[i], "--hardbreaks") == 0) {
			options |= CMARK_OPT_HARDBREAKS;
		} else if (strcmp(argv[i], "--smart") == 0) {
			options |= CMARK_OPT_SMARTPUNCT;
		} else if (strcmp(argv[i], "--normalize") == 0) {
			options |= CMARK_OPT_NORMALIZE;
		} else if ((strcmp(argv[i], "--help") == 0) ||
		           (strcmp(argv[i], "-h") == 0)) {
			print_usage();
			exit(0);
		} else if ((strcmp(argv[i], "-t") == 0) ||
		           (strcmp(argv[i], "--to") == 0)) {
			i += 1;
			if (i < argc) {
				if (strcmp(argv[i], "man") == 0) {
					writer = FORMAT_MAN;
				} else if (strcmp(argv[i], "html") == 0) {
					writer = FORMAT_HTML;
				} else if (strcmp(argv[i], "xml") == 0) {
					writer = FORMAT_XML;
				} else {
					fprintf(stderr,
					        "Unknown format %s\n", argv[i]);
					exit(1);
				}
			} else {
				fprintf(stderr, "No argument provided for %s\n",
				        argv[i - 1]);
				exit(1);
			}
		} else if (*argv[i] == '-') {
			print_usage();
			exit(1);
		} else { // treat as file argument
			files[numfps++] = i;
		}
	}

	for (i = 0; i < numfps; i++) {
		FILE *fp = fopen(argv[files[i]], "r");
		if (fp == NULL) {
			fprintf(stderr, "Error opening file %s: %s\n",
			        argv[files[i]], strerror(errno));
			exit(1);
		}

		start_timer();
		while ((bytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
			cmark_parser_feed(parser, buffer, bytes);
			if (bytes < sizeof(buffer)) {
				break;
			}
		}
		end_timer("processing lines");

		fclose(fp);
	}

	if (numfps == 0) {

		while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0) {
			cmark_parser_feed(parser, buffer, bytes);
			if (bytes < sizeof(buffer)) {
				break;
			}
		}
	}

	start_timer();
	document = cmark_parser_finish(parser);
	end_timer("finishing document");
	cmark_parser_free(parser);

	start_timer();
	print_document(document, writer, options);
	end_timer("print_document");

	start_timer();
	cmark_node_free(document);
	end_timer("free_blocks");

	free(files);

	return 0;
}