Newer
Older
/* ans2asc.c */
/* Convert ANSI messages to Synchronet .asc (Ctrl-A code) format */
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* $Id$ */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* See the GNU General Public License for more details: gpl.txt or *
* http://www.fsf.org/copyleft/gpl.html *
* *
* Anonymous FTP access to the most recent released source is available at *
* ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
* *
* Anonymous CVS access to the development source and modification history *
* is available at cvs.synchro.net:/cvsroot/sbbs, example: *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login *
* (just hit return, no password is necessary) *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src *
* *
* For Synchronet coding style and modification guidelines, see *
* http://www.synchro.net/source.html *
* *
* You are encouraged to submit any modifications (preferably in Unix diff *
* format) via e-mail to mods@synchro.net *
* *
* Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> /* isdigit */
int main(int argc, char **argv)
{
FILE *in,*out;
sscanf("$Revision$", "%*s %s", revision);
if(argc<3) {
fprintf(stderr,"\nans2asc %s\n",revision);
fprintf(stderr,"\nusage: %s infile.ans outfile.asc\n",argv[0]);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
return(0);
}
if((in=fopen(argv[1],"rb"))==NULL) {
perror(argv[1]);
return(1);
}
if((out=fopen(argv[2],"wb"))==NULL) {
perror(argv[2]);
return(1);
}
esc=0;
while((ch=fgetc(in))!=EOF) {
if(ch=='[' && esc) { /* ANSI escape sequence */
ni=0; /* zero number index */
while((ch=fgetc(in))!=EOF) {
if(isdigit(ch)) { /* 1 digit */
n[ni]=ch&0xf;
ch=fgetc(in);
if(ch==EOF)
break;
if(isdigit(ch)) { /* 2 digits */
n[ni]*=10;
n[ni]+=ch&0xf;
ch=fgetc(in);
if(ch==EOF)
break;
if(isdigit(ch)) { /* 3 digits */
n[ni]*=10;
n[ni]+=ch&0xf;
ch=fgetc(in);
}
}
ni++;
}
if(ch==';')
continue;
switch(ch) {
case '=':
case '?':
ch=fgetc(in); /* First digit */
if(isdigit(ch)) ch=fgetc(in); /* l or h ? */
if(isdigit(ch)) ch=fgetc(in);
if(isdigit(ch)) fgetc(in);
break;
case 'J':
if(n[0]==2) /* clear screen */
fputs("\1l",out); /* ctrl-al */
break;
case 'K':
fputs("\1>",out); /* clear to eol */
break;
case 'm':
for(i=0;i<ni;i++) {
fputc(1,out); /* ctrl-ax */
switch(n[i]) {
default:
case 0:
case 2: /* no attribute */
fputc('n',out);
break;
case 1: /* high intensity */
fputc('h',out);
break;
case 3:
case 4:
case 5: /* blink */
case 6:
case 7:
fputc('i',out);
break;
case 8: /* concealed */
fputc('e',out);
break;
case 30:
fputc('k',out);
break;
case 31:
fputc('r',out);
break;
case 32:
fputc('g',out);
break;
case 33:
fputc('y',out);
break;
case 34:
fputc('b',out);
break;
case 35:
fputc('m',out);
break;
case 36:
fputc('c',out);
break;
case 37:
fputc('w',out);
break;
case 40:
fputc('0',out);
break;
case 41:
fputc('1',out);
break;
case 42:
fputc('2',out);
break;
case 43:
fputc('3',out);
break;
case 44:
fputc('4',out);
break;
case 45:
fputc('5',out);
break;
case 46:
fputc('6',out);
break;
case 47:
fputc('7',out);
break; } }
break;
case 'C':
fprintf(out,"\1%c",0x7f+n[0]);
break;
default:
fprintf(stderr,"Unsupported ANSI code '%c' (0x%02X)\r\n",ch,ch);
break;
}
break;
} /* end of while */
esc=0;
continue;
} /* end of ANSI expansion */
if(ch=='\x1b')
esc=1;
else {
esc=0;
fputc(ch,out);
}
}
return(0);
}