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
20 #include "vkb_compiler.h"
29 static void error(void *p
, int line
, char *msg
)
31 struct priv
*priv
= p
;
34 fprintf(stderr
, "%s: Error: %s.\n", priv
->fin_name
, msg
);
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
);
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
);
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");
84 error(priv
, -1, "Cannot open input file");
89 static void init_output(struct priv
*priv
, char *fname
)
91 priv
->fout_name
= fname
;
92 priv
->fout
= fopen(fname
, "w");
94 fprintf(stderr
, "Error creating %s.\n", fname
);
99 static void close_input(struct priv
*priv
)
104 static void close_output(struct priv
*priv
)
109 static struct compiler_ops ops
= {
110 .get_line
= get_line
,
118 int main(int argc
, char **argv
)
124 fprintf(stderr
, "Missing parameters (source and destination filename).\n");
127 init_input(&priv
, argv
[1]);
128 init_output(&priv
, argv
[2]);
129 res
= compile(&ops
, &priv
);