Annotation of sbbs/src/sbbs3/ans2asc.c, revision 1.1.1.2

1.1       root        1: /* ans2asc.c */
                      2: 
                      3: /* Convert ANSI messages to Synchronet .asc (Ctrl-A code) format */
                      4: 
1.1.1.2 ! root        5: /* $Id: ans2asc.c,v 1.7 2009/09/21 18:41:30 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>
                     39: #include <stdlib.h>
                     40: #include <ctype.h>             /* isdigit */
                     41: #include <string.h>            /* strcmp */
                     42: 
                     43: int main(int argc, char **argv)
                     44: {
1.1.1.2 ! root       45:        unsigned char esc,n[25];
1.1       root       46:        char revision[16];
                     47:        int i,ch,ni;
                     48:        FILE *in,*out;
                     49: 
1.1.1.2 ! root       50:        sscanf("$Revision: 1.7 $", "%*s %s", revision);
1.1       root       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 */
1.1.1.2 ! root       80:                        memset(n,1,sizeof(n));
1.1       root       81:                        while((ch=fgetc(in))!=EOF) {
                     82:                                if(isdigit(ch)) {                       /* 1 digit */
                     83:                                        n[ni]=ch&0xf;
                     84:                                        ch=fgetc(in);
                     85:                                        if(ch==EOF)
                     86:                                                break;
                     87:                                        if(isdigit(ch)) {               /* 2 digits */
                     88:                                                n[ni]*=10;
                     89:                                                n[ni]+=ch&0xf;
                     90:                                                ch=fgetc(in);
                     91:                                                if(ch==EOF)
                     92:                                                        break;
                     93:                                                if(isdigit(ch)) {       /* 3 digits */
                     94:                                                        n[ni]*=10;
                     95:                                                        n[ni]+=ch&0xf;
                     96:                                                        ch=fgetc(in); 
                     97:                                                } 
                     98:                                        }
                     99:                                        ni++; 
                    100:                                }
                    101:                                if(ch==';')
                    102:                                        continue;
                    103:                                switch(ch) {
                    104:                                        case '=':
                    105:                                        case '?':
                    106:                                                ch=fgetc(in);      /* First digit */
                    107:                                                if(isdigit(ch)) ch=fgetc(in);   /* l or h ? */
                    108:                                                if(isdigit(ch)) ch=fgetc(in);
                    109:                                                if(isdigit(ch)) fgetc(in);
                    110:                                                break;
                    111:                                        case 'J':
                    112:                                                if(n[0]==2)                                     /* clear screen */
                    113:                                                        fputs("\1l",out);           /* ctrl-al */
                    114:                                                break;
                    115:                                        case 'K':
                    116:                                                fputs("\1>",out);               /* clear to eol */
                    117:                                                break;
                    118:                                        case 'm':
                    119:                                                for(i=0;i<ni;i++) {
                    120:                                                        fputc(1,out);                           /* ctrl-ax */
                    121:                                                        switch(n[i]) {
                    122:                                                                default:
                    123:                                                                case 0:
                    124:                                                                case 2:                                 /* no attribute */
                    125:                                                                        fputc('n',out);
                    126:                                                                        break;
                    127:                                                                case 1:                                 /* high intensity */
                    128:                                                                        fputc('h',out);
                    129:                                                                        break;
                    130:                                                                case 3:
                    131:                                                                case 4:
                    132:                                                                case 5:                                 /* blink */
                    133:                                                                case 6:
                    134:                                                                case 7:
                    135:                                                                        fputc('i',out);
                    136:                                                                        break;
                    137:                                                                case 8:                                 /* concealed */
                    138:                                                                        fputc('e',out);
                    139:                                                                        break;
                    140:                                                                case 30:
                    141:                                                                        fputc('k',out);
                    142:                                                                        break;
                    143:                                                                case 31:
                    144:                                                                        fputc('r',out);
                    145:                                                                        break;
                    146:                                                                case 32:
                    147:                                                                        fputc('g',out);
                    148:                                                                        break;
                    149:                                                                case 33:
                    150:                                                                        fputc('y',out);
                    151:                                                                        break;
                    152:                                                                case 34:
                    153:                                                                        fputc('b',out);
                    154:                                                                        break;
                    155:                                                                case 35:
                    156:                                                                        fputc('m',out);
                    157:                                                                        break;
                    158:                                                                case 36:
                    159:                                                                        fputc('c',out);
                    160:                                                                        break;
                    161:                                                                case 37:
                    162:                                                                        fputc('w',out);
                    163:                                                                        break;
                    164:                                                                case 40:
                    165:                                                                        fputc('0',out);
                    166:                                                                        break;
                    167:                                                                case 41:
                    168:                                                                        fputc('1',out);
                    169:                                                                        break;
                    170:                                                                case 42:
                    171:                                                                        fputc('2',out);
                    172:                                                                        break;
                    173:                                                                case 43:
                    174:                                                                        fputc('3',out);
                    175:                                                                        break;
                    176:                                                                case 44:
                    177:                                                                        fputc('4',out);
                    178:                                                                        break;
                    179:                                                                case 45:
                    180:                                                                        fputc('5',out);
                    181:                                                                        break;
                    182:                                                                case 46:
                    183:                                                                        fputc('6',out);
                    184:                                                                        break;
                    185:                                                                case 47:
                    186:                                                                        fputc('7',out);
                    187:                                                                        break; } }
                    188:                                                break;
1.1.1.2 ! root      189:                                        case 'B':       /* cursor down */
        !           190:                                                while(n[0]) {
        !           191:                                                        fprintf(out,"\n");
        !           192:                                                        n[0]--;
        !           193:                                                }
        !           194:                                                break;
        !           195:                                        case 'C':       /* cursor right */
1.1       root      196:                                                fprintf(out,"\1%c",0x7f+n[0]);
                    197:                                                break;
1.1.1.2 ! root      198:                                        case 'D':       /* cursor left */
        !           199:                                                if(n[0]>=80)
        !           200:                                                        fprintf(out,"\r");
        !           201:                                                else
        !           202:                                                        while(n[0]) {
        !           203:                                                                fprintf(out,"\b");
        !           204:                                                                n[0]--;
        !           205:                                                        }
        !           206:                                                break;
1.1       root      207:                                        default:
                    208:                                                fprintf(stderr,"Unsupported ANSI code '%c' (0x%02X)\r\n",ch,ch);
                    209:                                                break; 
                    210:                                }
                    211:                                break; 
                    212:                        }       /* end of while */
                    213:                        esc=0;
                    214:                        continue; 
                    215:                }       /* end of ANSI expansion */
                    216:                if(ch=='\x1b')
                    217:                        esc=1;
                    218:                else {
                    219:                        esc=0;
                    220:                        fputc(ch,out); 
                    221:                } 
                    222:        }
                    223:        return(0);
                    224: }
                    225: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.