initial commit
[goodguy/cinelerra.git] / cinelerra-5.1 / po / nlx.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <libintl.h>
4
5 #define _(s) gettext(s)
6 #define gettext_noop(s) s
7 #define N_(s) gettext_noop(s)
8
9 int lastch = 0;
10 int linenum = 0;
11 int start_line = 1;
12 int in_quote = 0;
13
14 #define BFRSZ 4096
15 char bfr[BFRSZ], *bp = 0;
16 char sym[BFRSZ];
17 char sep[BFRSZ];
18 char str[BFRSZ];
19
20 FILE *ifp = 0, *ofp = 0;
21
22 int ngetc()
23 {
24   if( lastch < 0 ) return -1;
25   if ((lastch=getc(ifp)) == '\n') linenum++;
26   return lastch;
27 }
28
29 void nungetc(int c)
30 {
31   if( c < 0 ) return;
32   if( c == '\n' ) --linenum;
33   ungetc(c, ifp);
34 }
35
36 // return uncommented char
37 int cch()
38 {
39   for(;;) {
40     int c = ngetc();
41     if( in_quote ||  c != '/' ) return c;
42     c = ngetc();
43     switch(c) {
44     case '*':
45       *bp++ = '/';  *bp++ = '*';
46       c = ' ';
47       for(;;) {
48         int ch = c;  c = ngetc();
49         if( c < 0 ) return c;
50         *bp++ = c;
51         if( ch == '*' && c == '/') break;
52       }
53       continue;
54     case '/':
55       *bp++ = '/';  *bp++ = '/';
56       for(;;) {
57         c = ngetc();
58         if( c < 0 ) return c;
59         *bp++ = c;
60         if( c == '\n') break;
61       }
62       continue;
63     default:
64       nungetc(c);
65       return '/';
66     }
67   }
68 }
69
70 // skip preprocessor lines
71 int nch()
72 {
73   int c = 0;
74   for(;;) {
75     int ch = c;  c = cch();
76     if( c < 0 ) return c;
77     if( !start_line || c != '#' ) break;
78     do {
79       *bp++ = c;
80       ch = c;  c = ngetc();
81       if( c < 0 ) return c;
82     } while( c != '\n' || ch == '\\' );
83     *bp++ = c;
84   }
85   start_line = c == '\n' ? 1 : 0;
86   return c;
87 }
88
89 int is_idch(int c)
90 {
91   if( c < '0' ) return 0;
92   if( c <= '9' ) return 1;
93   if( c < 'A' ) return 0;
94   if( c <= 'Z' ) return 1;
95   if( c == '_' ) return 1;
96   if( c < 'a' ) return 0;
97   if( c <= 'z' ) return 1;
98   return 0;
99 }
100
101 int is_ws(int c)
102 {
103   if( c <= ' ' ) return 1;
104   return 0;
105 }
106
107 // scan next seperator + symbol
108 int nsym()
109 {
110   int c = nch();
111   if( c < 0 ) return c;
112   if( c == '\"' || c == '\'' ) return c;
113   nungetc(c);
114   char *sp = sym;
115   for(;;) {
116     c = nch();
117     if( c < 0 ) return c;
118     if( c == '"' || c == '\'' ) break;
119     if( !is_idch(c) ) break;
120     *bp++ = c;
121     *sp++ = c;
122   }
123   *sp = 0;
124   nungetc(c);
125   sp = sep;
126   for(;;) {
127     c = nch();
128     if( c < 0 ) return c;
129     if( c == '\"' || c == '\'' ) break;
130     if( is_idch(c) ) break;
131     *bp++ = c;
132     if( !is_ws(c) )
133       *sp++ = c;
134   }
135   *sp = 0;
136   nungetc(c);
137   return 0;
138 }
139
140 // sym has preceeding symbol string
141 // sep has non-ws seperator string
142 // bfr has "string" in it
143 int need_nl()
144 {
145   if( strlen(bfr) < 2 ) return 0;
146   if( !strcmp(sym,"_") && !strcmp(sep,"(") ) return 0;
147 // add protected names here
148   if( !strcmp(sym,"get") && !strcmp(sep,"(") ) return 0;
149   if( !strcmp(sym,"get_property") && !strcmp(sep,"(") ) return 0;
150   if( !strcmp(sym,"set_property") && !strcmp(sep,"(") ) return 0;
151   if( !strcmp(sym,"set_title") && !strcmp(sep,"(") ) return 0;
152   if( !strcmp(sym,"title_is") && !strcmp(sep,"(") ) return 0;
153   if( !strcmp(sym,"GET_DEFAULT") && !strcmp(sep,"(") ) return 0;
154   if( !strcmp(sym,"UPDATE_DEFAULT") && !strcmp(sep,"(") ) return 0;
155   if( !strcmp(sym,"TRACE") && !strcmp(sep,"(") ) return 0;
156   if( !strcmp(sym,"lock") && !strcmp(sep,"(") ) return 0;
157   if( !strcmp(sym,"unlock") && !strcmp(sep,"(") ) return 0;
158   if( !strcmp(sym,"lock_window") && !strcmp(sep,"(") ) return 0;
159   if( !strcmp(sym,"unlock_window") && !strcmp(sep,"(") ) return 0;
160   if( !strcmp(sym,"update") && !strcmp(sep,"(") ) return 0;
161   if( !strcmp(sym,"new_image") && !strcmp(sep,"(") ) return 0;
162   if( !strcmp(sym,"get_image") && !strcmp(sep,"(") ) return 0;
163   if( !strcmp(sym,"get_image_set") && !strcmp(sep,"(") ) return 0;
164   if( !strcmp(sym,"Mutex") && !strcmp(sep,"(") ) return 0;
165   if( !strcmp(sym,"SceneNode") && !strcmp(sep,"(") ) return 0;
166   if( !strcmp(sym,"Garbage") && !strcmp(sep,"(") ) return 0;
167   if( !strcmp(sym,"dbmsg") && !strcmp(sep,"(") ) return 0;
168   if( strstr(bfr, "::") || !strncmp(bfr, "/dev/", 5) ) return 0;
169   if( !strcmp(bfr,"toc") || !strcmp(bfr,".toc") ) return 0;
170   if( !strcmp(bfr,"idx") || !strcmp(bfr,".idx") ) return 0;
171   if( !strcmp(bfr,"ads") || !strcmp(bfr,".ads") ) return 0;
172   char *bp;
173   for( bp=bfr; *bp; ++bp ) {
174     if( is_ws(*bp) ) continue;
175     if( *bp == '\\' ) { ++bp;  continue; }
176     if( *bp != '%' ) break;
177     while( bp[1] && !strchr("diojxXeEfFgFaAcsCSpnm%",*++bp) );
178   }
179   if( !*bp ) return 0;
180   return 1;
181 }
182
183 int main(int ac, char **av)
184 {
185   int c;
186   ifp = stdin;
187   ofp = stdout;
188   for(;;) {
189     bp = &bfr[0];  *bp = 0;
190     c = nsym();
191     *bp = 0;
192     if( c < 0 ) break;
193     fputs(bfr, ofp);
194     bp = &bfr[0];  *bp = 0;
195     if( c == '\"' ) {
196       in_quote = c;
197       while( (c=nch()) >= 0 ) {
198         if( c == in_quote ) break;
199         if( c == '\\' ) {
200           *bp++ = c;  c = nch();
201           if( c < 0 ) break;
202         }
203         *bp++ = c;
204       }
205       in_quote = 0;
206       *bp = 0;
207       if( c < 0 ) break;
208       int do_nl = need_nl();
209       if( do_nl ) {
210         fputc('_', ofp);
211         fputc('(', ofp);
212       }
213       fputc('"', ofp);
214       fputs(bfr, ofp);
215       fputc('"', ofp);
216       if( do_nl ) {
217         fputc(')', ofp);
218       }
219     }
220     else if( c == '\'' ) {
221       in_quote = c;
222       while( (c=nch()) >= 0 ) {
223         if( c == in_quote ) break;
224         if( c == '\\' ) {
225           *bp++ = c;  c = nch();
226           if( c < 0 ) break;
227         }
228         *bp++ = c;
229       }
230       in_quote = 0;
231       *bp = 0;
232       fputc('\'', ofp);
233       fputs(bfr, ofp);
234       fputc('\'', ofp);
235       if( c < 0 ) break;
236     }
237     else
238       continue;
239   }
240   fputs(bfr, ofp);
241   return 0;
242 }
243