Version bump (2.5-2)
[ukeyboard.git] / vkb_compiler.c
blob1d4b367cca2c48a03116f737ccaf903522f2f2e7
1 /*
2 * Copyright (c) 2007-2008 Jiri Benc <jbenc@upir.cz>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "vkb_compiler.h"
22 struct priv {
23 FILE *fin;
24 FILE *fout;
25 char *fin_name;
26 char *fout_name;
29 static void error(void *p, int line, char *msg)
31 struct priv *priv = p;
33 if (line < 0)
34 fprintf(stderr, "%s: Error: %s.\n", priv->fin_name, msg);
35 else
36 fprintf(stderr, "%s:%d: Error: %s.\n", priv->fin_name, line, msg);
39 static int warning(void *p, int line, char *msg)
41 struct priv *priv = p;
43 fprintf(stderr, "%s:%d: Warning: %s.\n", priv->fin_name, line, msg);
44 return 0;
47 static int get_line(void *p, void *buf, size_t size)
49 struct priv *priv = p;
51 return fgets(buf, size, priv->fin) ? 0 : -1;
54 static int write_buf(void *p, void *buf, size_t size)
56 struct priv *priv = p;
58 if (fwrite(buf, 1, size, priv->fout) != size) {
59 fprintf(stderr, "Error writing %s.\n", priv->fout_name);
60 return -1;
62 return 0;
65 static off_t tell_buf(void *p)
67 struct priv *priv = p;
69 return ftell(priv->fout);
72 static void seek_buf(void *p, off_t pos)
74 struct priv *priv = p;
76 fseek(priv->fout, pos, SEEK_SET);
79 static void init_input(struct priv *priv, char *fname)
81 priv->fin_name = fname;
82 priv->fin = fopen(fname, "r");
83 if (!priv->fin) {
84 error(priv, -1, "Cannot open input file");
85 exit(1);
89 static void init_output(struct priv *priv, char *fname)
91 priv->fout_name = fname;
92 priv->fout = fopen(fname, "w");
93 if (!priv->fout) {
94 fprintf(stderr, "Error creating %s.\n", fname);
95 exit(1);
99 static void close_input(struct priv *priv)
101 fclose(priv->fin);
104 static void close_output(struct priv *priv)
106 fclose(priv->fout);
109 static struct compiler_ops ops = {
110 .get_line = get_line,
111 .write = write_buf,
112 .tell = tell_buf,
113 .seek = seek_buf,
114 .error = error,
115 .warning = warning,
118 int main(int argc, char **argv)
120 struct priv priv;
121 int res;
123 if (argc < 3) {
124 fprintf(stderr, "Missing parameters (source and destination filename).\n");
125 exit(1);
127 init_input(&priv, argv[1]);
128 init_output(&priv, argv[2]);
129 res = compile(&ops, &priv);
130 close_output(&priv);
131 close_input(&priv);
132 return res ? 1 : 0;