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 TMPFNAME "tmp.lua"
36 #define WRITE_BATCH 50
37 #define WRITE_BEG "[=====["
38 #define WRITE_END "]=====]"
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
) {
71 default: s
=5; begText(); doText(c
); break;
77 case EOF
: s
=5; doText(EQ
); endText(); break;
78 default: s
=5; doText(EQ
); doText(c
); break;
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;
90 case NL
: s
=1; endStat(); break;
91 case EOF
: s
=1; endStat(); break;
92 default: s
=4; doStat(c
); break;
98 case NL
: s
=1; doText(c
); endText(); break;
99 case EOF
: s
=5; endText(); break;
100 default: s
=5; doText(c
); break;
106 case EOF
: s
=5; doText(LA
); endText(); break;
107 default: s
=5; doText(LA
); doText(c
); break;
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;
120 case EOF
: s
=8; endExpr(); break;
121 default: s
=8; doExpr(c
); break;
126 case RA
: s
=10; break;
127 case EOF
: s
=8; doExpr(RA
); endExpr(); break;
128 default: s
=8; doExpr(RA
); doExpr(c
); break;
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;
146 fprintf(stderr
,"file read error (errno=%d)\n", errno
);
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] : "-";
157 lua_State
*L
= luaL_newstate();
159 if (luaL_dofile(L
,cname
)!=0) {
160 fprintf(stderr
,"%s (file=%s)\n",lua_tostring(L
,-1),cname
);
164 if (strcmp(tname
,"-")!=0) {
165 fi
= fopen(tname
,"r");
167 fprintf(stderr
,"can't open input file (file=%s, errno=%d)\n", tname
, errno
);
174 fo
= fopen(TMPFNAME
,"w");
176 fprintf(stderr
,"can't open temporary file (file=%s, errno=%d)\n", TMPFNAME
, errno
);
182 if (strcmp(tname
,"-")!=0) {
186 if (luaL_dofile(L
,TMPFNAME
)!=0) {
187 fprintf(stderr
,"%s (file=%s)\n",lua_tostring(L
,-1),TMPFNAME
);