major cleanup and improved block handling
[ctxt.git] / ctxt.c
blob8d09bf3f8754204e88e7a563873243ed8b25676b
1 /*
2 * ctxt - a simple and fast plain text formatter
4 * Copyright (C) 2009-2012 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
7 */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include "ctxt.h"
14 extern struct fmt_ops troff_ops;
15 extern struct fmt_ops latex_ops;
16 extern struct fmt_ops html_ops;
18 static struct fmt_ops *get_ops(char *name)
20 if (!strcmp(name, "troff"))
21 return &troff_ops;
22 if (!strcmp(name, "latex"))
23 return &latex_ops;
24 if (!strcmp(name, "html"))
25 return &html_ops;
26 return NULL;
29 void die(char *msg)
31 write(2, msg, strlen(msg));
32 exit(1);
35 void *xmalloc(int size)
37 void *value = malloc(size);
38 if (!value)
39 die("Out of memory!\n");
40 return value;
43 static char *usage =
44 "usage: ctxt [-m mode] <plaintext >formatted\n\n"
45 " mode: latex, html or troff (default: troff)\n";
47 int main(int argc, char **argv)
49 struct txt *txt;
50 struct doc *doc;
51 char *mode = "troff";
52 struct fmt_ops *ops;
53 int i;
54 for (i = 1; i < argc; i++) {
55 if (!strcmp("-m", argv[i]) && i + 1 < argc) {
56 mode = argv[++i];
57 continue;
59 die(usage);
61 if (!(ops = get_ops(mode)))
62 die("unknown output format\n");
63 txt = txt_alloc(0);
64 doc = doc_alloc(1);
65 format(doc, txt, ops);
66 doc_free(doc);
67 txt_free(txt);
68 return 0;