-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
90 lines (77 loc) · 1.75 KB
/
main.c
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
/*
* All int returning functions returns non-zero in case of error, unless
* explicitly mentioned.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dumper.h"
#include "parser.h"
#include "utils.h"
static void
usage (const char *progname)
{
printf ("\
%s [-h|--help] <wikitext-file> \n\
\n\
Convert the provided file in mediawiki markup to markdown, printed on stdout. \n\
", progname);
}
int
main (int argc, char **argv)
{
int err = 0;
node_t *root = NULL;
char *content = NULL;
if (argc > 1 && (strncmp (argv[1], "-h", 10) == 0 || strncmp (argv[1], "--help", 10) == 0))
{
usage (argv[0]);
goto cleanup;
}
if (argc != 2)
{
err = 1;
usage (argv[0]);
goto cleanup;
}
const char *filename = argv[1];
err = access (filename, F_OK);
if (err)
{
fprintf (stderr, "No such file : %s\n", filename);
usage (argv[0]);
goto cleanup;
}
root = xalloc (sizeof *root);
root->type = NODE_ROOT;
root->is_block_level = true;
root->can_have_block_children = true;
err = parse (filename, root);
if (err)
{
fprintf (stderr, "main.c : main() : error while building representation of file.\n");
goto cleanup;
}
content = xalloc (MAX_FILE_SIZE);
size_t max_len = MAX_FILE_SIZE - 1;
char *writing_ptr = content;
dumping_params_t params = {
.node = root,
.writing_ptr = &writing_ptr,
.start_of_buffer = content,
.max_len = &max_len,
};
err = dump (¶ms);
if (err)
{
fprintf (stderr, "main.c : main() : error while dumping markdown.\n");
goto cleanup;
}
puts (content);
cleanup:
if (root) free_node (root);
if (content) free (content);
return err;
}