fixed temp and output files, changed cmd line, added usage
[ikh.git] / miconf / miconf.c
blob33f420270d4066ab49201a6c2cd1b955ee0798cd
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 #define EQ '='
39 #define LA '<'
40 #define RA '>'
41 #define NL '\n'
43 #define begText() fprintf(fo,"io.write(%s",WRITE_BEG)
44 #define doText(c) fputc(c,fo)
45 #define endText() fprintf(fo,"%s) -- %d\n",WRITE_END,lineno)
47 #define begStat() fputc(NL,fo)
48 #define doStat(c) fputc(c,fo)
49 #define endStat() fprintf(fo," -- %d\n",lineno)
51 #define begExpr() fprintf(fo,"io.write(tostring(")
52 #define doExpr(c) fputc(c,fo)
53 #define endExpr() fprintf(fo,")) -- %d\n", lineno)
55 // if (lineno % WRITE_BATCH == 0) {
57 void convert(FILE* fi, FILE* fo) {
58 int lineno = 1;
59 int c;
60 int s = 1; //state
61 do {
62 c=getc(fi);
63 switch (s) {
64 case 1:
65 switch (c) {
66 case EQ: s=2; break;
67 case LA: s=6; break;
68 case EOF: s=1; break;
69 default: s=5; begText(); doText(c); break;
71 break;
72 case 2:
73 switch (c) {
74 case EQ: s=3; break;
75 case EOF: s=5; doText(EQ); endText(); break;
76 default: s=5; doText(EQ); doText(c); break;
78 break;
79 case 3:
80 switch (c) {
81 case EQ: s=4; begStat(); break;
82 case EOF: s=5; doText(EQ); doText(EQ); endText(); break;
83 default: s=5; doText(EQ); doText(EQ); doText(c); break;
85 break;
86 case 4:
87 switch (c) {
88 case NL: s=1; endStat(); break;
89 case EOF: s=1; endStat(); break;
90 default: s=4; doStat(c); break;
92 break;
93 case 5:
94 switch (c) {
95 case LA: s=6; break;
96 case NL: s=1; doText(c); endText(); break;
97 case EOF: s=5; endText(); break;
98 default: s=5; doText(c); break;
100 break;
101 case 6:
102 switch (c) {
103 case LA: s=7; break;
104 case EOF: s=5; doText(LA); endText(); break;
105 default: s=5; doText(LA); doText(c); break;
107 break;
108 case 7:
109 switch (c) {
110 case LA: s=8; endText(); begExpr(); break;
111 case EOF: s=5; doText(LA); doText(LA); endText(); break;
112 default: s=5; doText(LA); doText(LA); doText(c); break;
114 break;
115 case 8:
116 switch (c) {
117 case RA: s=9; break;
118 case EOF: s=8; endExpr(); break;
119 default: s=8; doExpr(c); break;
121 break;
122 case 9:
123 switch (c) {
124 case RA: s=10; break;
125 case EOF: s=8; doExpr(RA); endExpr(); break;
126 default: s=8; doExpr(RA); doExpr(c); break;
128 break;
129 case 10:
130 switch (c) {
131 case RA: s=5; endExpr(); begText(); break;
132 case EOF: s=8; doExpr(RA); doExpr(RA); endExpr(); break;
133 default: s=8; doExpr(RA); doExpr(RA); doExpr(c); break;
135 break;
137 if (c==NL) {
138 lineno++;
141 } while (c!=EOF);
143 if (ferror(fi)) {
144 fprintf(stderr,"file read error (errno=%d)\n", errno);
145 exit(1);
150 int main(int argc, char* argv[]) {
151 const char* cname = argc>1 ? argv[1] : NULL;
152 const char* iname = argc>2 ? argv[2] : "-";
153 const char* oname = argc>3 ? argv[3] : "-";
154 const char* tname;
155 FILE* fi;
156 FILE* ft;
158 if (argc<2) {
159 fprintf(stderr,"miconf: A Configuration Utility\n");
160 fprintf(stderr,"usage: miconf <config file> <input template> <output file>\n\n");
161 exit(1);
164 lua_State *L = luaL_newstate();
165 luaL_openlibs(L);
167 if (luaL_dofile(L,cname)!=0) {
168 fprintf(stderr,"%s (file=%s)\n",lua_tostring(L,-1),cname);
169 lua_pop(L,1);
170 exit(1);
173 if (strcmp(iname,"-")!=0) {
174 fi = fopen(iname,"r");
175 if (fi==NULL) {
176 fprintf(stderr,"can't open input file (file=%s, errno=%d)\n", iname, errno);
177 exit(1);
179 } else {
180 fi = stdin;
183 if (strcmp(oname,"-")!=0) {
184 stdout = freopen(oname, "w", stdout);
185 if (stdout==NULL) {
186 fprintf(stderr,"can't open output file (file=%s, errno=%d)\n", oname, errno);
187 exit(1);
191 tname = tmpnam(NULL);
192 if (tname==NULL) {
193 fprintf(stderr,"can't obtain temporary file name (errno=%d)\n", errno);
194 exit(1);
196 ft = fopen(tname,"w");
197 if (ft==NULL) {
198 fprintf(stderr,"can't open temporary file (file=%s, errno=%d)\n", tname, errno);
199 exit(1);
201 convert(fi,ft);
202 fclose(ft);
204 if (strcmp(iname,"-")!=0) {
205 fclose(fi);
208 if (luaL_dofile(L,tname)!=0) {
209 fprintf(stderr,"%s (file=%s)\n",lua_tostring(L,-1),tname);
210 lua_pop(L,1);
211 exit(1);
214 lua_close(L);
216 if (unlink(tname)!=0) {
217 fprintf(stderr,"can't remove temporary file (file=%s, errno=%d)\n", tname, errno);
218 exit(1);
221 return(0);