executes config.lua config file and converts stdin into lua program that io.writes...
[ikh.git] / miconf / miconf.c
blobd9ee0cde110d2f13b00f2d7915d78a6b4d5f5d75
1 /*
2 * Copyright (c) 2012 Igor Khomyakov. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * 2) Redistributions of source code with modification must include a notice
10 * that the code was modified.
11 * 3) Neither the names of the authors nor the names of their contributors may
12 * be used to endorse or promote products derived from this software without
13 * specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include "lua.h"
31 #include "lauxlib.h"
32 #include "lualib.h"
34 #define WRITE_BATCH 50
35 #define WRITE_BEG "[=====["
36 #define WRITE_END "]=====]"
38 void convert(FILE* fi, FILE* fo) {
39 int lineno = 1;
40 int c;
41 fprintf(fo,"io.write(%s",WRITE_BEG);
42 do {
43 c = getc(fi);
44 if (c == EOF) {
45 if (ferror(fi)) {
46 fprintf(stderr,"stdin error: %d\n", errno);
47 exit(1);
49 } else if (c == '\n') {
50 fputc(c,fo);
51 if (lineno % WRITE_BATCH == 0) {
52 fprintf(fo,"%s)\nio.write(%s",WRITE_END,WRITE_BEG);
54 lineno++;
55 } else {
56 fputc(c,fo);
58 } while (c != EOF);
59 fprintf(fo,"%s)\n",WRITE_END);
63 int main(int argc, char* argv[]) {
64 lua_State *L = luaL_newstate();
65 luaL_openlibs(L);
66 if (luaL_dofile(L,"config.lua")!=0) {
67 fprintf(stderr,"%s\n",lua_tostring(L,-1));
68 lua_pop(L,1);
69 exit(1);
71 convert(stdin,stdout);
72 lua_close(L);
73 return(0);