ctxt: introduce an escape character to prevent unwanted markups
[ctxt.git] / ctxt.c
blob7ff017263d175c717baffabc7a9e4f062fdf1e82
1 /*
2 * ctxt - a simple and fast plain text formatter
4 * Copyright (C) 2009-2012 Ali Gholami Rudi
6 * This program is released under the modified BSD license.
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] [-e esc] <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 esc = '\0';
54 int i;
55 for (i = 1; i < argc; i++) {
56 if (argv[i][0] == '-' && argv[i][1] == 'm') {
57 mode = argv[i][2] ? argv[i] + 2 : argv[++i];
58 continue;
60 if (argv[i][0] == '-' && argv[i][1] == 'e') {
61 esc = argv[i][2] ? argv[i][2] : argv[++i][0];
62 continue;
64 die(usage);
66 if (!(ops = get_ops(mode)))
67 die("unknown output format\n");
68 txt = txt_alloc(0);
69 doc = doc_alloc(1);
70 format(doc, txt, ops, esc);
71 doc_free(doc);
72 txt_free(txt);
73 return 0;