1 : //==============================================================================
2 : // File <$/dev/cpp/rawasm/rawasm.cpp>
3 : // This file is part of YaOrb : Yet Another Object Request Broker,
4 : // Copyright (c) 2000-2003, Marc Alff.
5 : //
6 : // This program is free software; you can redistribute it and/or
7 : // modify it under the terms of the GNU General Public License
8 : // as published by the Free Software Foundation; either version 2
9 : // of the License, or (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 : //
20 : //==============================================================================
21 :
22 : // "Raw" assembler, used to generate GIOP binary messages.
23 :
24 : // Portability
25 : #include "yaorb/config.h"
26 : #include "src/cpp/prod/port/port_stdc.h"
27 : #include "src/cpp/prod/port/port_stdlib.h"
28 :
29 : #include <stdio.h>
30 : #include <assert.h>
31 : #include <ctype.h>
32 :
33 : static FILE *input = NULL ;
34 : static FILE *output = NULL ;
35 :
36 695 : void processHexa()
37 : {
38 : char c ;
39 : char hexa ;
40 :
41 695 : c = fgetc(input) ;
42 695 : assert (c == 'x') ;
43 :
44 695 : c = fgetc(input) ;
45 695 : if (isdigit(c))
46 : {
47 504 : hexa = c - '0' ;
48 : }
49 : else
50 : {
51 191 : hexa = toupper(c) - 'A' + 10 ;
52 : }
53 :
54 695 : hexa *= 16 ;
55 :
56 695 : c = fgetc(input) ;
57 695 : if (isdigit(c))
58 : {
59 611 : hexa += c - '0' ;
60 : }
61 : else
62 : {
63 84 : hexa += toupper(c) - 'A' + 10 ;
64 : }
65 :
66 695 : fputc (hexa, output) ;
67 : }
68 :
69 437 : void processChar()
70 : {
71 : char c ;
72 :
73 437 : c = fgetc(input) ;
74 :
75 437 : if (c != '\\')
76 : {
77 437 : fputc(c, output) ;
78 : }
79 : else
80 : {
81 0 : c = fgetc(input) ;
82 0 : switch(c)
83 : {
84 : case 'n':
85 0 : fputc('\n', output) ;
86 0 : break ;
87 : case 't':
88 0 : fputc('\t', output) ;
89 0 : break ;
90 : default :
91 0 : fprintf(stderr, "Escape not implemented : %c\n", c) ;
92 : }
93 : }
94 :
95 437 : c = fgetc(input) ;
96 437 : assert(c == '\'') ;
97 : }
98 :
99 7683 : void processComment()
100 : {
101 : char c ;
102 :
103 7683 : while ((c=fgetc(input)) != '\n') ;
104 374 : ungetc(c, input) ;
105 : }
106 :
107 :
108 43 : void loop(void)
109 : {
110 : char c ;
111 :
112 3589 : while ((c=fgetc(input)) != EOF)
113 : {
114 3503 : switch (c)
115 : {
116 : case '\n':
117 : case '\t':
118 : case ' ':
119 : {
120 : break ;
121 : }
122 : case '0':
123 : {
124 695 : processHexa() ;
125 695 : break ;
126 : }
127 : case '\'' :
128 : {
129 437 : processChar() ;
130 437 : break ;
131 : }
132 : case '#' :
133 : {
134 374 : processComment() ;
135 : break ;
136 : }
137 : }
138 : }
139 : }
140 :
141 0 : void usage(void)
142 : {
143 0 : fprintf(stderr, "usage : rawasm [-I input_file_name] [-O output_file_name]\n") ;
144 0 : exit (-1) ;
145 : }
146 :
147 43 : int main(int argc, char* argv[])
148 : {
149 : bool optionI = false ;
150 : bool optionO = false ;
151 43 : input = stdin ;
152 43 : output = stdout ;
153 :
154 43 : if ( (argc != 1)
155 : && (argc != 3)
156 : && (argc != 5) )
157 : {
158 0 : usage() ;
159 : }
160 :
161 43 : argc -- ;
162 43 : argv ++ ;
163 :
164 129 : while (argc>0)
165 : {
166 86 : if (strcmp("-I", argv[0]) == 0)
167 : {
168 : optionI = true ;
169 :
170 43 : input = fopen(argv[1], "r") ;
171 :
172 43 : if (input == NULL)
173 : {
174 0 : perror(argv[1]) ;
175 0 : exit(-1) ;
176 : }
177 :
178 43 : argc -= 2 ;
179 43 : argv += 2 ;
180 : }
181 43 : else if (strcmp("-O", argv[0]) == 0)
182 : {
183 : optionO = true ;
184 :
185 43 : output = fopen(argv[1], "w") ;
186 :
187 43 : if (output == NULL)
188 : {
189 0 : perror(argv[1]) ;
190 0 : exit(-1) ;
191 : }
192 :
193 43 : argc -= 2 ;
194 43 : argv += 2 ;
195 : }
196 : else
197 : {
198 0 : fprintf(stderr, "Bad option <%s>\n", argv[0]) ;
199 0 : usage() ;
200 : }
201 : }
202 :
203 43 : loop() ;
204 :
205 43 : if (optionI) fclose(input) ;
206 43 : if (optionO) fclose(output) ;
207 :
208 : return 0 ;
209 : }
210 :
|