Fossil SCM

If a @-line for a string literal ends with "\" then omit the newline.

drh 2017-12-05 16:22 trunk
Commit 09d882ee819572a0f62a09e6daeef7e713280126f8d7c5a3939bfee9195b3706
1 file changed +19 -3
+19 -3
--- src/translate.c
+++ src/translate.c
@@ -74,10 +74,15 @@
7474
/*
7575
** True if we are currently doing a free string
7676
*/
7777
static int inStr = 0;
7878
79
+/*
80
+** Name of files being processed
81
+*/
82
+static const char *zInFile = "(stdin)";
83
+
7984
/*
8085
** Terminate an active cgi_printf() or free string
8186
*/
8287
static void end_block(FILE *out){
8388
if( inPrint ){
@@ -94,15 +99,17 @@
9499
static void trans(FILE *in, FILE *out){
95100
int i, j, k; /* Loop counters */
96101
char c1, c2; /* Characters used to start a comment */
97102
int lastWasEq = 0; /* True if last non-whitespace character was "=" */
98103
int lastWasComma = 0; /* True if last non-whitespace character was "," */
104
+ int lineNo = 0; /* Line number */
99105
char zLine[2000]; /* A single line of input */
100106
char zOut[4000]; /* The input line translated into appropriate output */
101107
102108
c1 = c2 = '-';
103109
while( fgets(zLine, sizeof(zLine), in) ){
110
+ lineNo++;
104111
for(i=0; zLine[i] && isspace(zLine[i]); i++){}
105112
if( zLine[i]!='@' ){
106113
if( inPrint || inStr ) end_block(out);
107114
fprintf(out,"%s",zLine);
108115
/* 0123456789 12345 */
@@ -120,28 +127,36 @@
120127
** generate a string literal. But skip comments
121128
** consisting of all text between c1 and c2 (default "--")
122129
** and end of line.
123130
*/
124131
int indent, omitline;
132
+ char *zNewline = "\\n";
125133
i++;
126134
if( isspace(zLine[i]) ){ i++; }
127135
indent = i - 2;
128136
if( indent<0 ) indent = 0;
129137
omitline = 0;
130138
for(j=0; zLine[i] && zLine[i]!='\r' && zLine[i]!='\n'; i++){
131139
if( zLine[i]==c1 && (c2==' ' || zLine[i+1]==c2) ){
132140
omitline = 1; break;
133141
}
134
- if( zLine[i]=='"' || zLine[i]=='\\' ){ zOut[j++] = '\\'; }
142
+ if( zLine[i]=='\\' && (zLine[i+1]==0 || zLine[i+1]=='\r'
143
+ || zLine[i+1]=='\n') ){
144
+ zLine[i] = 0;
145
+ zNewline = "";
146
+ /* fprintf(stderr, "%s:%d: omit newline\n", zInFile, lineNo); */
147
+ break;
148
+ }
149
+ if( zLine[i]=='\\' || zLine[i]=='"' ){ zOut[j++] = '\\'; }
135150
zOut[j++] = zLine[i];
136151
}
137
- while( j>0 && isspace(zOut[j-1]) ){ j--; }
152
+ if( zNewline[0] ) while( j>0 && isspace(zOut[j-1]) ){ j--; }
138153
zOut[j] = 0;
139154
if( j<=0 && omitline ){
140155
fprintf(out,"\n");
141156
}else{
142
- fprintf(out,"%*s\"%s\\n\"\n",indent, "", zOut);
157
+ fprintf(out,"%*s\"%s%s\"\n",indent, "", zOut, zNewline);
143158
}
144159
}else{
145160
/* Otherwise (if the last non-whitespace was not '=') then generate
146161
** a cgi_printf() statement whose format is the text following the '@'.
147162
** Substrings of the form "%C(...)" (where C is any sequence of
@@ -197,10 +212,11 @@
197212
FILE *in = fopen(argv[1], "r");
198213
if( in==0 ){
199214
fprintf(stderr,"can not open %s\n", argv[1]);
200215
exit(1);
201216
}
217
+ zInFile = argv[1];
202218
printf("#line 1 \"");
203219
for(arg=argv[1]; *arg; arg++){
204220
if( *arg!='\\' ){
205221
printf("%c", *arg);
206222
}else{
207223
--- src/translate.c
+++ src/translate.c
@@ -74,10 +74,15 @@
74 /*
75 ** True if we are currently doing a free string
76 */
77 static int inStr = 0;
78
 
 
 
 
 
79 /*
80 ** Terminate an active cgi_printf() or free string
81 */
82 static void end_block(FILE *out){
83 if( inPrint ){
@@ -94,15 +99,17 @@
94 static void trans(FILE *in, FILE *out){
95 int i, j, k; /* Loop counters */
96 char c1, c2; /* Characters used to start a comment */
97 int lastWasEq = 0; /* True if last non-whitespace character was "=" */
98 int lastWasComma = 0; /* True if last non-whitespace character was "," */
 
99 char zLine[2000]; /* A single line of input */
100 char zOut[4000]; /* The input line translated into appropriate output */
101
102 c1 = c2 = '-';
103 while( fgets(zLine, sizeof(zLine), in) ){
 
104 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
105 if( zLine[i]!='@' ){
106 if( inPrint || inStr ) end_block(out);
107 fprintf(out,"%s",zLine);
108 /* 0123456789 12345 */
@@ -120,28 +127,36 @@
120 ** generate a string literal. But skip comments
121 ** consisting of all text between c1 and c2 (default "--")
122 ** and end of line.
123 */
124 int indent, omitline;
 
125 i++;
126 if( isspace(zLine[i]) ){ i++; }
127 indent = i - 2;
128 if( indent<0 ) indent = 0;
129 omitline = 0;
130 for(j=0; zLine[i] && zLine[i]!='\r' && zLine[i]!='\n'; i++){
131 if( zLine[i]==c1 && (c2==' ' || zLine[i+1]==c2) ){
132 omitline = 1; break;
133 }
134 if( zLine[i]=='"' || zLine[i]=='\\' ){ zOut[j++] = '\\'; }
 
 
 
 
 
 
 
135 zOut[j++] = zLine[i];
136 }
137 while( j>0 && isspace(zOut[j-1]) ){ j--; }
138 zOut[j] = 0;
139 if( j<=0 && omitline ){
140 fprintf(out,"\n");
141 }else{
142 fprintf(out,"%*s\"%s\\n\"\n",indent, "", zOut);
143 }
144 }else{
145 /* Otherwise (if the last non-whitespace was not '=') then generate
146 ** a cgi_printf() statement whose format is the text following the '@'.
147 ** Substrings of the form "%C(...)" (where C is any sequence of
@@ -197,10 +212,11 @@
197 FILE *in = fopen(argv[1], "r");
198 if( in==0 ){
199 fprintf(stderr,"can not open %s\n", argv[1]);
200 exit(1);
201 }
 
202 printf("#line 1 \"");
203 for(arg=argv[1]; *arg; arg++){
204 if( *arg!='\\' ){
205 printf("%c", *arg);
206 }else{
207
--- src/translate.c
+++ src/translate.c
@@ -74,10 +74,15 @@
74 /*
75 ** True if we are currently doing a free string
76 */
77 static int inStr = 0;
78
79 /*
80 ** Name of files being processed
81 */
82 static const char *zInFile = "(stdin)";
83
84 /*
85 ** Terminate an active cgi_printf() or free string
86 */
87 static void end_block(FILE *out){
88 if( inPrint ){
@@ -94,15 +99,17 @@
99 static void trans(FILE *in, FILE *out){
100 int i, j, k; /* Loop counters */
101 char c1, c2; /* Characters used to start a comment */
102 int lastWasEq = 0; /* True if last non-whitespace character was "=" */
103 int lastWasComma = 0; /* True if last non-whitespace character was "," */
104 int lineNo = 0; /* Line number */
105 char zLine[2000]; /* A single line of input */
106 char zOut[4000]; /* The input line translated into appropriate output */
107
108 c1 = c2 = '-';
109 while( fgets(zLine, sizeof(zLine), in) ){
110 lineNo++;
111 for(i=0; zLine[i] && isspace(zLine[i]); i++){}
112 if( zLine[i]!='@' ){
113 if( inPrint || inStr ) end_block(out);
114 fprintf(out,"%s",zLine);
115 /* 0123456789 12345 */
@@ -120,28 +127,36 @@
127 ** generate a string literal. But skip comments
128 ** consisting of all text between c1 and c2 (default "--")
129 ** and end of line.
130 */
131 int indent, omitline;
132 char *zNewline = "\\n";
133 i++;
134 if( isspace(zLine[i]) ){ i++; }
135 indent = i - 2;
136 if( indent<0 ) indent = 0;
137 omitline = 0;
138 for(j=0; zLine[i] && zLine[i]!='\r' && zLine[i]!='\n'; i++){
139 if( zLine[i]==c1 && (c2==' ' || zLine[i+1]==c2) ){
140 omitline = 1; break;
141 }
142 if( zLine[i]=='\\' && (zLine[i+1]==0 || zLine[i+1]=='\r'
143 || zLine[i+1]=='\n') ){
144 zLine[i] = 0;
145 zNewline = "";
146 /* fprintf(stderr, "%s:%d: omit newline\n", zInFile, lineNo); */
147 break;
148 }
149 if( zLine[i]=='\\' || zLine[i]=='"' ){ zOut[j++] = '\\'; }
150 zOut[j++] = zLine[i];
151 }
152 if( zNewline[0] ) while( j>0 && isspace(zOut[j-1]) ){ j--; }
153 zOut[j] = 0;
154 if( j<=0 && omitline ){
155 fprintf(out,"\n");
156 }else{
157 fprintf(out,"%*s\"%s%s\"\n",indent, "", zOut, zNewline);
158 }
159 }else{
160 /* Otherwise (if the last non-whitespace was not '=') then generate
161 ** a cgi_printf() statement whose format is the text following the '@'.
162 ** Substrings of the form "%C(...)" (where C is any sequence of
@@ -197,10 +212,11 @@
212 FILE *in = fopen(argv[1], "r");
213 if( in==0 ){
214 fprintf(stderr,"can not open %s\n", argv[1]);
215 exit(1);
216 }
217 zInFile = argv[1];
218 printf("#line 1 \"");
219 for(arg=argv[1]; *arg; arg++){
220 if( *arg!='\\' ){
221 printf("%c", *arg);
222 }else{
223

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button