Code

Initial commit.
[lm2latex.git] / src / lm2latex.c
1 /*
2  * lm2latex - src/lm2latex.c
3  * Copyright (C) 2010 Sebastian Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * A Linux-Magazin markup to LaTeX converter.
30  */
32 #if HAVE_CONFIG_H
33 #       include "config.h"
34 #endif /* HAVE_CONFIG_H */
36 #include "l2l.h"
37 #include "l2l_features.h"
39 #include <errno.h>
41 #if HAVE_LIBGEN_H
42 #       include <libgen.h>
43 #else /* HAVE_LIBGEN_H */
44 #       define basename(path) (path)
45 #endif /* ! HAVE_LIBGEN_H */
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include <unistd.h>
53 static void
54 exit_usage(char *name, int status)
55 {
56         printf(
57 "Usage: %s <options> <infile> [<outfile>]\n"
59 "\nOptions:\n"
60 "  -h    display this help and exit\n"
61 "  -V    display the version number and copyright\n"
63 "\nlm2latex "L2L_VERSION_STRING L2L_VERSION_EXTRA", "PACKAGE_URL"\n",
64 basename(name));
65         exit(status);
66 } /* exit_usage */
68 static void
69 exit_version(void)
70 {
71         printf("lm2latex version "L2L_VERSION_STRING L2L_VERSION_EXTRA", "
72                         "built "BUILD_DATE"\n"
73                         "Copyright (C) 2010 "PACKAGE_BUGREPORT"\n"
75                         "\nThis is free software under the terms of the BSD license, see "
76                         "the source for\ncopying conditions. There is NO WARRANTY; not "
77                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
78                         "PURPOSE.\n");
79         exit(0);
80 } /* exit_version */
82 int
83 main(int argc, char **argv)
84 {
85         char *infile = NULL, *outfile = NULL;
86         FILE *in_fh, *out_fh;
87         int   args;
89         while (42) {
90                 int opt = getopt(argc, argv, "hV");
92                 if (-1 == opt)
93                         break;
95                 switch (opt) {
96                         case 'h':
97                                 exit_usage(argv[0], 0);
98                                 break;
99                         case 'V':
100                                 exit_version();
101                                 break;
102                         default:
103                                 exit_usage(argv[0], 1);
104                 }
105         }
107         args = argc - optind;
108         if ((0 < args) && (args <= 2)) {
109                 infile = argv[optind];
111                 if (1 < args)
112                         outfile = argv[optind + 1];
113                 else
114                         outfile = "-";
115         }
116         else
117                 exit_usage(argv[0], 1);
119         if (! strcmp(infile, "-"))
120                 in_fh = stdin;
121         else
122                 in_fh = fopen(infile, "r");
124         if (! in_fh) {
125                 fprintf(stderr, "lm2latex: FATAL: Failed to open ``%s'' "
126                                 "for reading: %s\n", infile, strerror(errno));
127                 return 1;
128         }
130         if (! strcmp(outfile, "-"))
131                 out_fh = stdout;
132         else
133                 out_fh = fopen(outfile, "w");
135         if (! out_fh) {
136                 fprintf(stderr, "lm2latex: FATAL: Failed to open ``%s'' "
137                                 "for writing: %s\n", outfile, strerror(errno));
138                 return 1;
139         }
141         l2l_convert_file_fh(in_fh, out_fh);
143         if (in_fh != stdin)
144                 fclose(in_fh);
145         if (out_fh != stdout)
146                 fclose(out_fh);
147         return 0;
148 } /* main */
150 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */