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.
34 #define WRITE_BATCH 50
35 #define WRITE_BEG "[=====["
36 #define WRITE_END "]=====]"
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
) {
69 default: s
=5; begText(); doText(c
); break;
75 case EOF
: s
=5; doText(EQ
); endText(); break;
76 default: s
=5; doText(EQ
); doText(c
); break;
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;
88 case NL
: s
=1; endStat(); break;
89 case EOF
: s
=1; endStat(); break;
90 default: s
=4; doStat(c
); break;
96 case NL
: s
=1; doText(c
); endText(); break;
97 case EOF
: s
=5; endText(); break;
98 default: s
=5; doText(c
); break;
104 case EOF
: s
=5; doText(LA
); endText(); break;
105 default: s
=5; doText(LA
); doText(c
); break;
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;
118 case EOF
: s
=8; endExpr(); break;
119 default: s
=8; doExpr(c
); break;
124 case RA
: s
=10; break;
125 case EOF
: s
=8; doExpr(RA
); endExpr(); break;
126 default: s
=8; doExpr(RA
); doExpr(c
); break;
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;
144 fprintf(stderr
,"file read error (errno=%d)\n", errno
);
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] : "-";
159 fprintf(stderr
,"miconf: A Configuration Utility\n");
160 fprintf(stderr
,"usage: miconf <config file> <input template> <output file>\n\n");
164 lua_State
*L
= luaL_newstate();
167 if (luaL_dofile(L
,cname
)!=0) {
168 fprintf(stderr
,"%s (file=%s)\n",lua_tostring(L
,-1),cname
);
173 if (strcmp(iname
,"-")!=0) {
174 fi
= fopen(iname
,"r");
176 fprintf(stderr
,"can't open input file (file=%s, errno=%d)\n", iname
, errno
);
183 if (strcmp(oname
,"-")!=0) {
184 stdout
= freopen(oname
, "w", stdout
);
186 fprintf(stderr
,"can't open output file (file=%s, errno=%d)\n", oname
, errno
);
191 tname
= tmpnam(NULL
);
193 fprintf(stderr
,"can't obtain temporary file name (errno=%d)\n", errno
);
196 ft
= fopen(tname
,"w");
198 fprintf(stderr
,"can't open temporary file (file=%s, errno=%d)\n", tname
, errno
);
204 if (strcmp(iname
,"-")!=0) {
208 if (luaL_dofile(L
,tname
)!=0) {
209 fprintf(stderr
,"%s (file=%s)\n",lua_tostring(L
,-1),tname
);
216 if (unlink(tname
)!=0) {
217 fprintf(stderr
,"can't remove temporary file (file=%s, errno=%d)\n", tname
, errno
);