|
|
1.1 root 1: /* asc2ans.c */
2:
3: /* Converts Synchronet Ctrl-A codes into ANSI escape sequences */
4:
1.1.1.2 ! root 5: /* $Id: asc2ans.c,v 1.5 2009/02/16 02:52:45 rswindell Exp $ */
1.1 root 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: * *
1.1.1.2 ! root 11: * Copyright 2009 Rob Swindell - http://www.synchro.net/copyright.html *
1.1 root 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>
1.1.1.2 ! root 39: #include <ctype.h> /* toupper */
! 40: #include <string.h> /* strcmp */
1.1 root 41:
1.1.1.2 ! root 42: #define CTRL_A '\1'
! 43: #define ANSI fprintf(out,"\x1b[")
1.1 root 44:
45: int main(int argc, char **argv)
46: {
47: char revision[16];
48: int ch;
49: FILE* in;
50: FILE* out;
51:
1.1.1.2 ! root 52: sscanf("$Revision: 1.5 $", "%*s %s", revision);
1.1 root 53:
54: if(argc<2) {
55: fprintf(stderr,"\nasc2ans %s\n",revision);
56: fprintf(stderr,"\nusage: %s infile.asc [outfile.ans]\n",argv[0]);
57: return(0);
58: }
59:
60: if(strcmp(argv[1],"-")) {
61: if((in=fopen(argv[1],"rb"))==NULL) {
62: perror(argv[1]);
63: return(1);
64: }
65: }
66: else
67: in=stdin;
68:
69: if(argc > 2 && (strcmp(argv[2],"-"))) {
70: if((out=fopen(argv[2],"wb"))==NULL) {
71: perror(argv[2]);
72: return(1);
73: }
74: }
75: else
76: out=stdout;
77:
78: while((ch=fgetc(in))!=EOF) {
1.1.1.2 ! root 79: if(ch==CTRL_A) { /* ctrl-a */
1.1 root 80: ch=fgetc(in);
1.1.1.2 ! root 81: if(ch==EOF || toupper(ch)=='Z') /* EOF */
1.1 root 82: break;
1.1.1.2 ! root 83: if(ch>0x7f) { /* move cursor right x columns */
! 84: int cnt=ch-0x7f;
1.1 root 85: ANSI;
1.1.1.2 ! root 86: if(cnt>1)
! 87: fprintf(out,"%u",cnt);
! 88: fputc('C',out);
1.1 root 89: continue;
90: }
91: switch(toupper(ch)) {
92: case 'A':
93: fputc('\1',out);
94: break;
95: case '<':
96: fputc('\b',out);
97: break;
98: case '>':
99: ANSI;
100: fputc('K',out);
101: break;
102: case '[':
103: fputc('\r',out);
104: break;
105: case ']':
106: fputc('\n',out);
107: break;
108: case 'L':
1.1.1.2 ! root 109: ANSI;
! 110: fprintf(out,"2J"); /* clear screen */
! 111: ANSI;
! 112: fprintf(out,"H"); /* home cursor */
1.1 root 113: break;
114: case '-':
115: case '_':
116: case 'N':
117: ANSI;
118: fprintf(out,"0m");
119: break;
120: case 'H':
121: ANSI;
122: fprintf(out,"1m");
123: break;
124: case 'I':
125: ANSI;
126: fprintf(out,"5m");
127: break;
128: case 'K':
129: ANSI;
130: fprintf(out,"30m");
131: break;
132: case 'R':
133: ANSI;
134: fprintf(out,"31m");
135: break;
136: case 'G':
137: ANSI;
138: fprintf(out,"32m");
139: break;
140: case 'Y':
141: ANSI;
142: fprintf(out,"33m");
143: break;
144: case 'B':
145: ANSI;
146: fprintf(out,"34m");
147: break;
148: case 'M':
149: ANSI;
150: fprintf(out,"35m");
151: break;
152: case 'C':
153: ANSI;
154: fprintf(out,"36m");
155: break;
156: case 'W':
157: ANSI;
158: fprintf(out,"37m");
159: break;
160: case '0':
161: ANSI;
162: fprintf(out,"40m");
163: break;
164: case '1':
165: ANSI;
166: fprintf(out,"41m");
167: break;
168: case '2':
169: ANSI;
170: fprintf(out,"42m");
171: break;
172: case '3':
173: ANSI;
174: fprintf(out,"43m");
175: break;
176: case '4':
177: ANSI;
178: fprintf(out,"44m");
179: break;
180: case '5':
181: ANSI;
182: fprintf(out,"45m");
183: break;
184: case '6':
185: ANSI;
186: fprintf(out,"46m");
187: break;
188: case '7':
189: ANSI;
190: fprintf(out,"47m");
191: break;
192: default:
193: fprintf(out,"\1%c",ch);
194: break;
195: }
196: }
197: else
198: fputc(ch,out);
199: }
200:
201: return(0);
202: }
203:
204:
205:
206:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.