version 0.1. requires improvements:
[ikh.git] / miconf / miconf.c
blob6ad1ee3b882442efd614ca63a74cbf5b1c7c9f2a
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 TMPFNAME "tmp.lua"
36 #define WRITE_BATCH 50
37 #define WRITE_BEG "[=====["
38 #define WRITE_END "]=====]"
40 #define EQ '='
41 #define LA '<'
42 #define RA '>'
43 #define NL '\n'
45 #define begText() fprintf(fo,"io.write(%s",WRITE_BEG)
46 #define doText(c) fputc(c,fo)
47 #define endText() fprintf(fo,"%s) -- %d\n",WRITE_END,lineno)
49 #define begStat() fputc(NL,fo)
50 #define doStat(c) fputc(c,fo)
51 #define endStat() fprintf(fo," -- %d\n",lineno)
53 #define begExpr() fprintf(fo,"io.write(tostring(")
54 #define doExpr(c) fputc(c,fo)
55 #define endExpr() fprintf(fo,")) -- %d\n", lineno)
57 // if (lineno % WRITE_BATCH == 0) {
59 void convert(FILE* fi, FILE* fo) {
60 int lineno = 1;
61 int c;
62 int s = 1; //state
63 do {
64 c=getc(fi);
65 switch (s) {
66 case 1:
67 switch (c) {
68 case EQ: s=2; break;
69 case LA: s=6; break;
70 case EOF: s=1; break;
71 default: s=5; begText(); doText(c); break;
73 break;
74 case 2:
75 switch (c) {
76 case EQ: s=3; break;
77 case EOF: s=5; doText(EQ); endText(); break;
78 default: s=5; doText(EQ); doText(c); break;
80 break;
81 case 3:
82 switch (c) {
83 case EQ: s=4; begStat(); break;
84 case EOF: s=5; doText(EQ); doText(EQ); endText(); break;
85 default: s=5; doText(EQ); doText(EQ); doText(c); break;
87 break;
88 case 4:
89 switch (c) {
90 case NL: s=1; endStat(); break;
91 case EOF: s=1; endStat(); break;
92 default: s=4; doStat(c); break;
94 break;
95 case 5:
96 switch (c) {
97 case LA: s=6; break;
98 case NL: s=1; doText(c); endText(); break;
99 case EOF: s=5; endText(); break;
100 default: s=5; doText(c); break;
102 break;
103 case 6:
104 switch (c) {
105 case LA: s=7; break;
106 case EOF: s=5; doText(LA); endText(); break;
107 default: s=5; doText(LA); doText(c); break;
109 break;
110 case 7:
111 switch (c) {
112 case LA: s=8; endText(); begExpr(); break;
113 case EOF: s=5; doText(LA); doText(LA); endText(); break;
114 default: s=5; doText(LA); doText(LA); doText(c); break;
116 break;
117 case 8:
118 switch (c) {
119 case RA: s=9; break;
120 case EOF: s=8; endExpr(); break;
121 default: s=8; doExpr(c); break;
123 break;
124 case 9:
125 switch (c) {
126 case RA: s=10; break;
127 case EOF: s=8; doExpr(RA); endExpr(); break;
128 default: s=8; doExpr(RA); doExpr(c); break;
130 break;
131 case 10:
132 switch (c) {
133 case RA: s=5; endExpr(); begText(); break;
134 case EOF: s=8; doExpr(RA); doExpr(RA); endExpr(); break;
135 default: s=8; doExpr(RA); doExpr(RA); doExpr(c); break;
137 break;
139 if (c==NL) {
140 lineno++;
143 } while (c!=EOF);
145 if (ferror(fi)) {
146 fprintf(stderr,"file read error (errno=%d)\n", errno);
147 exit(1);
152 int main(int argc, char* argv[]) {
153 const char* cname = argc>1 ? argv[1] : "config.lua";
154 const char* tname = argc>2 ? argv[2] : "-";
155 FILE* fi;
156 FILE* fo;
157 lua_State *L = luaL_newstate();
158 luaL_openlibs(L);
159 if (luaL_dofile(L,cname)!=0) {
160 fprintf(stderr,"%s (file=%s)\n",lua_tostring(L,-1),cname);
161 lua_pop(L,1);
162 exit(1);
164 if (strcmp(tname,"-")!=0) {
165 fi = fopen(tname,"r");
166 if (fi==NULL) {
167 fprintf(stderr,"can't open input file (file=%s, errno=%d)\n", tname, errno);
168 exit(1);
170 } else {
171 fi = stdin;
174 fo = fopen(TMPFNAME,"w");
175 if (fo==NULL) {
176 fprintf(stderr,"can't open temporary file (file=%s, errno=%d)\n", TMPFNAME, errno);
177 exit(1);
179 convert(fi,fo);
180 fclose(fo);
182 if (strcmp(tname,"-")!=0) {
183 fclose(fi);
186 if (luaL_dofile(L,TMPFNAME)!=0) {
187 fprintf(stderr,"%s (file=%s)\n",lua_tostring(L,-1),TMPFNAME);
188 lua_pop(L,1);
189 exit(1);
192 lua_close(L);
194 return(0);