996
|
1 /*****************************************************************************
|
|
2
|
|
3 NAME
|
|
4 convickt.c -- translate between various INTERCAL formats
|
|
5
|
|
6 LICENSE TERMS
|
|
7 Copyright (C) 2007 Alex Smith
|
|
8
|
|
9 This program is free software; you can redistribute it and/or modify
|
|
10 it under the terms of the GNU General Public License as published by
|
|
11 the Free Software Foundation; either version 2 of the License, or
|
|
12 (at your option) any later version.
|
|
13
|
|
14 This program is distributed in the hope that it will be useful,
|
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 GNU General Public License for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with this program; if not, write to the Free Software
|
|
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
22
|
|
23 ***************************************************************************/
|
|
24
|
|
25 #include <stdio.h>
|
|
26 #include <stdlib.h>
|
|
27 #include <time.h>
|
|
28 #include <string.h>
|
|
29 #include <unistd.h>
|
|
30
|
|
31 extern int ick_clc_cset_convert(const char* in, /*@partial@*/ char* out, const char* incset,
|
|
32 const char* outcset, int padstyle, size_t outsize,
|
|
33 /*@null@*/ FILE* errsto);
|
|
34
|
|
35 /* For communication with clc_cset.c */
|
|
36 /*@observer@*/ char* ick_globalargv0;
|
|
37 /*@-redef@*/ /* it's never the case that both definitions are used at once */
|
|
38 /*@observer@*/ /*@dependent@*/ const char* ick_datadir;
|
|
39 /*@=redef@*/
|
|
40 /* We want to read the latest versions of the character sets from disk. */
|
|
41 /*@null@*/ const char* ick_clc_cset_atari=0;
|
|
42 /*@null@*/ const char* ick_clc_cset_baudot=0;
|
|
43 /*@null@*/ const char* ick_clc_cset_ebcdic=0;
|
|
44 /*@null@*/ const char* ick_clc_cset_latin1=0;
|
|
45
|
|
46 #ifndef ICKDATADIR
|
|
47 #define ICKDATADIR "/usr/local/share/ick-0.27"
|
|
48 #endif
|
|
49
|
|
50 /*@-redef@*/ /* because only one main is used at a time */
|
|
51 int main(int argc, char** argv)
|
|
52 /*@=redef@*/
|
|
53 {
|
|
54 size_t allocsize=100;
|
|
55 char* in;
|
|
56 char* out;
|
|
57 size_t i=0;
|
|
58 int ti;
|
|
59 int c;
|
|
60 int padding=-1;
|
|
61 ick_globalargv0=argv[0];
|
|
62 srand((unsigned)time(NULL));
|
|
63 if(argc<3||argc>5)
|
|
64 {
|
|
65 fprintf(stderr,"Usage: convickt informat outformat [padding [arrayname]]\n");
|
|
66 fprintf(stderr,"Available formats are:\n");
|
|
67 fprintf(stderr,"\tPrinceton: latin1, baudot, ebcdic\n");
|
|
68 fprintf(stderr,"\tAtari: atari (7-bit ASCII)\n");
|
|
69 fprintf(stderr,"Possible values for padding are:\n");
|
|
70 fprintf(stderr,"\tprintable: try to leave the output in char range "
|
|
71 "32-126\n");
|
|
72 fprintf(stderr,"\tzero: leave the output with 0s in the high bits\n");
|
|
73 fprintf(stderr,"\trandom: pad with random data\n");
|
|
74 fprintf(stderr,"Padding only has an effect on character sets with less\n");
|
|
75 fprintf(stderr,"than 8 bits used per bytes; default is 'printable'.\n");
|
|
76 fprintf(stderr,"If arrayname is given, which must be a legal name for\n");
|
|
77 fprintf(stderr,"a tail array, the output will instead be a portable\n");
|
|
78 fprintf(stderr,"INTERCAL program that stores the required byte pattern\n");
|
|
79 fprintf(stderr,"in that array.\n");
|
|
80 return 0;
|
|
81 }
|
|
82 if(argc>=4&&!strcmp(argv[3],"printable")) padding=1;
|
|
83 if(argc>=4&&!strcmp(argv[3],"zero")) padding=0;
|
|
84 if(argc>=4&&!strcmp(argv[3],"random")) padding=2;
|
|
85 if(argc>=4&&padding==-1)
|
|
86 {
|
|
87 fprintf(stderr,"Error: padding value not recognized.\n");
|
|
88 return 1;
|
|
89 }
|
|
90 if(!(ick_datadir=getenv("ICKDATADIR")))
|
|
91 ick_datadir=ICKDATADIR;
|
|
92 in=malloc(allocsize);
|
|
93 if(!in) {perror("Error: Memory allocation failure"); return 0;}
|
|
94 while((c=getchar())!=EOF)
|
|
95 {
|
|
96 in[i++]=(char)c;
|
|
97 if(i+1>=allocsize)
|
|
98 {
|
|
99 char* temp=in;
|
|
100 allocsize*=2;
|
|
101 in=realloc(in,allocsize);
|
|
102 if(!in)
|
|
103 {
|
|
104 perror("Error: Memory allocation failure");
|
|
105 /* Annotation, because in hasn't been freed here; that's what the
|
|
106 error return from realloc means. */
|
|
107 /*@-usereleased@*/
|
|
108 if(temp) free(temp);
|
|
109 /*@=usereleased@*/
|
|
110 return 0;
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114 in[i]='\0';
|
|
115 out=malloc(allocsize*6+6);
|
|
116 if(!out)
|
|
117 {
|
|
118 perror("Error: Memory allocation failure");
|
|
119 free(in);
|
|
120 return 0;
|
|
121 }
|
|
122 ti=ick_clc_cset_convert(in,out,argv[1],argv[2],padding,allocsize*6+6,stderr);
|
|
123 if(ti>=0&&argc<5) (void) fwrite(out,1,(size_t)ti,stdout);
|
|
124 else if(ti>=0&&argc==5)
|
|
125 {
|
|
126 int pleasedelay=2;
|
|
127 printf("\tDO %s <- #%d\n",argv[4],ti);
|
|
128 while(ti--)
|
|
129 {
|
|
130 printf("\t");
|
|
131 if(!--pleasedelay) {printf("PLEASE "); pleasedelay=4;}
|
|
132 printf("DO %s SUB #%d <- #%d\n",argv[4],ti+1,(int)(out[ti]));
|
|
133 }
|
|
134 }
|
|
135 free(in);
|
|
136 free(out);
|
|
137 return ti<0;
|
|
138 }
|