Skip to content
Snippets Groups Projects
Commit b4f9d84b authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add -esc option for C-literal escaping/encoding of output (e.g. for JS/.ini)

<phigz> what would you convert ansi to so it displays right when written out from a .js file?

Now, you can use 'ans2asc -esc' for this purpose.
parent 426e1501
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #4511 passed
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> /* strcmp */ #include <string.h> /* strcmp */
#include "sauce.h" #include "sauce.h"
#include "genwrap.h"
#include "git_branch.h" #include "git_branch.h"
#include "git_hash.h" #include "git_hash.h"
...@@ -45,6 +46,7 @@ static void print_usage(const char* prog) ...@@ -45,6 +46,7 @@ static void print_usage(const char* prog)
fprintf(stderr," -clear insert a clear screen code at beginning of output file\n"); fprintf(stderr," -clear insert a clear screen code at beginning of output file\n");
fprintf(stderr," -pause append a pause (hit a key) code to end of output file\n"); fprintf(stderr," -pause append a pause (hit a key) code to end of output file\n");
fprintf(stderr," -space use space characters for cursor-right movement/alignment\n"); fprintf(stderr," -space use space characters for cursor-right movement/alignment\n");
fprintf(stderr," -esc use C-style string literal escaping of control and CP437 chars\n");
fprintf(stderr," -delay <int> insert a 1/10th second delay code at output byte interval\n"); fprintf(stderr," -delay <int> insert a 1/10th second delay code at output byte interval\n");
fprintf(stderr," (lower interval values result in more delays, slower display)\n"); fprintf(stderr," (lower interval values result in more delays, slower display)\n");
} }
...@@ -57,6 +59,7 @@ int main(int argc, char **argv) ...@@ -57,6 +59,7 @@ int main(int argc, char **argv)
FILE *out=stdout; FILE *out=stdout;
bool ice=false; bool ice=false;
bool normal=false; bool normal=false;
bool encode=false;
int cols=0; int cols=0;
int column=0; int column=0;
int delay=0; int delay=0;
...@@ -64,10 +67,11 @@ int main(int argc, char **argv) ...@@ -64,10 +67,11 @@ int main(int argc, char **argv)
int pause=0; int pause=0;
int space=0; int space=0;
int newline=0; int newline=0;
char* ctrl_a = "\1";
if(argc<2) { if(argc<2) {
print_usage(argv[0]); print_usage(argv[0]);
return(0); return(0);
} }
for(i=1; i<argc; i++) { for(i=1; i<argc; i++) {
...@@ -94,6 +98,10 @@ int main(int argc, char **argv) ...@@ -94,6 +98,10 @@ int main(int argc, char **argv)
normal = true; normal = true;
else if(strcmp(argv[i], "-newline") == 0) else if(strcmp(argv[i], "-newline") == 0)
newline++; newline++;
else if(strcmp(argv[i], "-esc") == 0) {
encode = true;
ctrl_a = "\\x01";
}
else if(IS_DIGIT(argv[i][1])) else if(IS_DIGIT(argv[i][1]))
cols = atoi(argv[i] + 1); cols = atoi(argv[i] + 1);
else { else {
...@@ -103,7 +111,7 @@ int main(int argc, char **argv) ...@@ -103,7 +111,7 @@ int main(int argc, char **argv)
} else if(in==stdin) { } else if(in==stdin) {
if((in=fopen(argv[i],"rb"))==NULL) { if((in=fopen(argv[i],"rb"))==NULL) {
perror(argv[i]); perror(argv[i]);
return(1); return(1);
} }
} else if(out==stdout) { } else if(out==stdout) {
if((out=fopen(argv[i],"wb"))==NULL) { if((out=fopen(argv[i],"wb"))==NULL) {
...@@ -124,7 +132,7 @@ int main(int argc, char **argv) ...@@ -124,7 +132,7 @@ int main(int argc, char **argv)
const char* cond_newline = normal ? "\1+\1N\1/\1-" : "\1/"; const char* cond_newline = normal ? "\1+\1N\1/\1-" : "\1/";
if(clear) if(clear)
fprintf(out,"\1N\1L"); fprintf(out,"%sN%sL", ctrl_a, ctrl_a);
esc=0; esc=0;
while((ch=fgetc(in))!=EOF && ch != CTRL_Z) { while((ch=fgetc(in))!=EOF && ch != CTRL_Z) {
if(ch=='[' && esc) { /* ANSI escape sequence */ if(ch=='[' && esc) { /* ANSI escape sequence */
...@@ -149,10 +157,10 @@ int main(int argc, char **argv) ...@@ -149,10 +157,10 @@ int main(int argc, char **argv)
if(IS_DIGIT(ch)) { /* 3 digits */ if(IS_DIGIT(ch)) { /* 3 digits */
n[ni]*=10; n[ni]*=10;
n[ni]+=ch&0xf; n[ni]+=ch&0xf;
ch=fgetc(in); ch=fgetc(in);
} }
} }
ni++; ni++;
} }
if(ch==';') if(ch==';')
continue; continue;
...@@ -167,23 +175,23 @@ int main(int argc, char **argv) ...@@ -167,23 +175,23 @@ int main(int argc, char **argv)
case 'f': case 'f':
case 'H': case 'H':
if(n[0]<=1 && n[1]<=1) /* home cursor */ if(n[0]<=1 && n[1]<=1) /* home cursor */
fputs("\1'", out); fprintf(out, "%s'", ctrl_a);
column = 0; column = 0;
break; break;
case 'J': case 'J':
if(n[0]==2) /* clear screen */ if(n[0]==2) /* clear screen */
fputs("\1L",out); /* ctrl-aL */ fprintf(out, "%sL", ctrl_a);
else if(n[0]==0) /* clear to EOS */ else if(n[0]==0) /* clear to EOS */
fputs("\1J",out); /* ctrl-aJ */ fprintf(out, "%sJ", ctrl_a);
column = 0; column = 0;
break; break;
case 'K': case 'K':
fputs("\1>",out); /* clear to eol */ fprintf(out, "%s>", ctrl_a); /* clear to eol */
column = cols ? (cols - 1) : 79; column = cols ? (cols - 1) : 79;
break; break;
case 'm': case 'm':
for(i=0;i<ni;i++) { for(i=0;i<ni;i++) {
fputc(1,out); /* ctrl-ax */ fprintf(out, ctrl_a); /* ctrl-ax */
switch(n[i]) { switch(n[i]) {
case 0: case 0:
case 2: /* no attribute */ case 2: /* no attribute */
...@@ -246,74 +254,90 @@ int main(int argc, char **argv) ...@@ -246,74 +254,90 @@ int main(int argc, char **argv)
break; break;
case 47: case 47:
fputc('7',out); fputc('7',out);
break; break;
default: default:
fprintf(stderr,"Unsupported ANSI color code: %u\n", (unsigned int)n[i]); fprintf(stderr,"Unsupported ANSI color code: %u\n", (unsigned int)n[i]);
break; break;
} }
} }
break; break;
case 'B': /* cursor down */ case 'B': /* cursor down */
while(n[0]) { while(n[0]) {
fprintf(out,"\1]"); /* linefeed */ fprintf(out,"%s]", ctrl_a); /* linefeed */
n[0]--; n[0]--;
} }
break; break;
case 'C': /* cursor right */ case 'C': /* cursor right */
if(space) if(space)
fprintf(out, "%*s", n[0], " "); fprintf(out, "%*s", n[0], " ");
else else {
fprintf(out,"\1%c",0x7f+n[0]); if(encode)
fprintf(out, "%s\\x%02X", ctrl_a, 0x7f + n[0]);
else
fprintf(out,"%s%c", ctrl_a, 0x7f+n[0]);
}
column += n[0]; column += n[0];
break; break;
case 'D': /* cursor left */ case 'D': /* cursor left */
column -= n[0]; column -= n[0];
if(n[0] >= column) if(n[0] >= column)
fprintf(out,"\1["); fprintf(out,"%s[", ctrl_a);
else else
while(n[0]) { while(n[0]) {
fprintf(out,"\1<"); fprintf(out,"%s<", ctrl_a);
n[0]--; n[0]--;
} }
break; break;
default: default:
fprintf(stderr,"Unsupported ANSI code '%c' (0x%02X)\r\n",ch,ch); fprintf(stderr,"Unsupported ANSI code '%c' (0x%02X)\r\n",ch,ch);
break; break;
} }
break; break;
} /* end of while */ } /* end of while */
esc=0; esc=0;
continue; continue;
} /* end of ANSI expansion */ } /* end of ANSI expansion */
if(ch=='\x1b') if(ch=='\x1b')
esc=1; esc=1;
else { else {
esc=0; esc=0;
switch(ch) { if(encode) {
case '\r': if(ch < ' ' || ch >= 0x7f) {
case '\n': char* p = c_escape_char(ch);
fputc(ch,out); if(p == NULL)
column = 0; fprintf(out, "\\x%02X", ch);
break; else
default: fprintf(out, "%s", p);
if(cols && column >= cols) { }
fprintf(out, "%s", cond_newline); // Conditional-newline else
fputc(ch, out);
} else {
switch(ch) {
case '\r':
case '\n':
fputc(ch,out);
column = 0; column = 0;
} break;
fputc(ch,out); default:
column++; if(cols && column >= cols) {
break; fprintf(out, "%s", cond_newline); // Conditional-newline
column = 0;
}
fputc(ch,out);
column++;
break;
}
} }
if(delay && (ftell(out)%delay)==0) if(delay && (ftell(out)%delay)==0)
fprintf(out,"\1,"); fprintf(out,"%s,", ctrl_a);
} }
if(column < 0) if(column < 0)
column = 0; column = 0;
} }
while(newline--) while(newline--)
fprintf(out,"\r\n"); fprintf(out,"\r\n");
if(pause) if(pause)
fprintf(out,"\1p"); fprintf(out,"%sp", ctrl_a);
return(0); return(0);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment