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