1 | /* |
---|
2 | client.c - simple client process |
---|
3 | */ |
---|
4 | |
---|
5 | #include <fcntl.h> |
---|
6 | #include "MP.h" |
---|
7 | #include "gmp.h" |
---|
8 | |
---|
9 | void * my_alloc(size_t n) |
---|
10 | { |
---|
11 | printf("in my_alloc allocating %d bytes\n", n); |
---|
12 | return (void *)malloc(n); |
---|
13 | } |
---|
14 | |
---|
15 | void * my_realloc(void * oldptr, size_t olds, size_t news) |
---|
16 | { |
---|
17 | void * newptr = NULL; |
---|
18 | |
---|
19 | printf("in my_realloc: old size = %d, new size = %d\n", olds, news); |
---|
20 | newptr = (void *)realloc(oldptr, news); |
---|
21 | printf("\told address of ptr is 0x%X, new address is 0x%X\n", oldptr, |
---|
22 | newptr); |
---|
23 | return newptr; |
---|
24 | } |
---|
25 | |
---|
26 | void my_free(void * ptr, size_t n) |
---|
27 | { |
---|
28 | printf("in my_free, freeing %d bytes at address 0x%X\n", n, ptr); |
---|
29 | free(ptr); |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | /* |
---|
34 | get_input() - routine to read sample data from a file. The data is |
---|
35 | assumed to be correct. This routine knows nothing |
---|
36 | about tree structure. It simply creates and sends |
---|
37 | packets based on the information in the input file. |
---|
38 | However the server examples knows something of trees |
---|
39 | and may croak if the input isn't in the correct format. |
---|
40 | */ |
---|
41 | |
---|
42 | void |
---|
43 | #ifdef __STDC__ |
---|
44 | get_input(MP_Link_pt link, char *fname) |
---|
45 | #else |
---|
46 | get_input(link, fname) |
---|
47 | MP_Link_pt link; |
---|
48 | char *fname; |
---|
49 | #endif |
---|
50 | { |
---|
51 | FILE *fd; |
---|
52 | char type[32], str[64], raw[512], c; |
---|
53 | MP_ApInt_t apint = NULL; |
---|
54 | MP_ApReal_t apreal = NULL; |
---|
55 | unsigned long dtag, cop; |
---|
56 | MP_NumAnnot_t annots; |
---|
57 | MP_AnnotType_t atype; |
---|
58 | unsigned int my_uint, flags; |
---|
59 | int sint, done = 0; |
---|
60 | float r32; |
---|
61 | double r64; |
---|
62 | |
---|
63 | if (strlen(fname) == 0) { |
---|
64 | fname = (char *)malloc(32); |
---|
65 | printf("Enter name of input file: "); |
---|
66 | scanf("%s", fname); |
---|
67 | } |
---|
68 | |
---|
69 | if ((fd = fopen(fname, "r")) == NULL) { |
---|
70 | fprintf(stderr, "Can't open %s for reading\n", fname); |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | while (1) { |
---|
75 | atype = 0; |
---|
76 | fscanf(fd, "%s", type); |
---|
77 | if (strcmp(type, "MP_Sint32Type") == 0) { |
---|
78 | fscanf(fd, "%d %d", &sint, &annots); |
---|
79 | MP_PutSint32Packet(link, sint, annots); |
---|
80 | } |
---|
81 | else if (strcmp(type, "MP_Uint32Type") == 0) { |
---|
82 | fscanf(fd, "%d %d", &my_uint, &annots); |
---|
83 | MP_PutUint32Packet(link, my_uint, annots); |
---|
84 | } |
---|
85 | else if (strcmp(type, "MP_Sint8Type") == 0) { |
---|
86 | fscanf(fd, "%d %d", &sint, &annots); |
---|
87 | MP_PutSint8Packet(link, sint, annots); |
---|
88 | } |
---|
89 | else if (strcmp(type, "MP_Uint8Type") == 0) { |
---|
90 | fscanf(fd, "%d %d", &my_uint, &annots); |
---|
91 | MP_PutUint8Packet(link, my_uint, annots); |
---|
92 | } |
---|
93 | else if (strcmp(type, "MP_BooleanType") == 0) { |
---|
94 | /* problem here is to skip the intervening blanks*/ |
---|
95 | fscanf(fd, "%1s %d", &c, &annots); |
---|
96 | my_uint = (c == 'T') ? 1 : 0; |
---|
97 | MP_PutBooleanPacket(link, my_uint, annots); |
---|
98 | } |
---|
99 | else if (strcmp(type, "MP_ApIntType") == 0) { |
---|
100 | mpz_init((mpz_ptr)&apint); |
---|
101 | /* now get the apint from the file */ |
---|
102 | mpz_inp_str((mpz_ptr)&apint, fd, 10); |
---|
103 | fscanf(fd, "%d ", &annots); |
---|
104 | MP_PutApIntPacket(link, &apint, annots); |
---|
105 | mpz_clear((mpz_ptr)&apint); |
---|
106 | } |
---|
107 | else if (strcmp(type, "MP_ApRealType") == 0) { |
---|
108 | mpf_init((mpf_ptr)&apreal); |
---|
109 | /* now get the apreal from the file */ |
---|
110 | mpf_inp_str((mpf_ptr)&apreal, fd, 10); |
---|
111 | fscanf(fd, "%d ", &annots); |
---|
112 | MP_PutApRealPacket(link, &apreal, annots); |
---|
113 | mpf_clear((mpf_ptr)&apreal); |
---|
114 | } |
---|
115 | else if (strcmp(type, "MP_Real32Type") == 0) { |
---|
116 | fscanf(fd, "%f %d", &r32, &annots); |
---|
117 | MP_PutReal32Packet(link, r32, annots); |
---|
118 | } |
---|
119 | else if (strcmp(type, "MP_Real64Type") == 0) { |
---|
120 | fscanf(fd, "%lG %d", &r64, &annots); |
---|
121 | MP_PutReal64Packet(link, r64, annots); |
---|
122 | } |
---|
123 | else if (strcmp(type, "MP_StringType") == 0) { |
---|
124 | fscanf(fd, "%s %d", str, &annots); |
---|
125 | MP_PutStringPacket(link, str, annots); |
---|
126 | } |
---|
127 | else if (strcmp(type, "MP_IdentifierType") == 0) { |
---|
128 | fscanf(fd, "%d %s %d", &dtag, str, &annots); |
---|
129 | MP_PutIdentifierPacket(link, dtag, str, annots); |
---|
130 | } |
---|
131 | else if (strcmp(type, "MP_CommonLatinIdentifierType") == 0) { |
---|
132 | fscanf(fd, "%d %s %d", &dtag, str, &annots); |
---|
133 | MP_PutCommonLatinIdentifierPacket(link, dtag, *str, annots); |
---|
134 | } |
---|
135 | else if (strcmp(type, "MP_CommonGreekIdentifierType") == 0) { |
---|
136 | fscanf(fd, "%d %s %d", &annots, str, &dtag); |
---|
137 | MP_PutCommonGreekIdentifierPacket(link, annots, *str, dtag); |
---|
138 | } |
---|
139 | else if (strcmp(type, "MP_ConstantType") == 0) { |
---|
140 | fscanf(fd, "%d %s %d", &dtag, str, &annots); |
---|
141 | MP_PutConstantPacket(link, dtag, str, annots); |
---|
142 | } |
---|
143 | else if (strcmp(type, "MP_CommonConstantType") == 0) { |
---|
144 | fscanf(fd, "%d %d %d", &dtag, &cop, &annots); |
---|
145 | MP_PutCommonConstantPacket(link, dtag, cop, annots); |
---|
146 | } |
---|
147 | else if (strcmp(type, "MP_MetaType") == 0) { |
---|
148 | fscanf(fd, "%d %s %d", &dtag, str, &annots); |
---|
149 | MP_PutMetaTypePacket(link, dtag, str, annots); |
---|
150 | } |
---|
151 | else if (strcmp(type, "MP_RawType") == 0) { |
---|
152 | fscanf(fd, "%s %d", raw, &annots); |
---|
153 | MP_PutRawPacket(link, raw, strlen(raw), annots); |
---|
154 | } |
---|
155 | else if (strcmp(type, "MP_OperatorType") == 0) { |
---|
156 | fscanf(fd, "%d %s %d %u", &dtag, str, &annots, &my_uint); |
---|
157 | MP_PutOperatorPacket(link, dtag, str, annots, my_uint); |
---|
158 | } |
---|
159 | else if (strcmp(type, "MP_CommonOperatorType") == 0) { |
---|
160 | fscanf(fd, "%d %d %d %u", &dtag, &cop, &annots, &my_uint); |
---|
161 | MP_PutCommonOperatorPacket(link, dtag, cop, annots, my_uint); |
---|
162 | if (dtag == MP_MpDict && cop == MP_CopMpEndSession) { |
---|
163 | MP_EndMsgReset(link); /* make sure stuff gets flushed */ |
---|
164 | return; |
---|
165 | } |
---|
166 | } |
---|
167 | else if(strcmp(type, "Label_AP") == 0) atype = MP_AnnotMpLabel; |
---|
168 | else if(strcmp(type, "Reference_AP") == 0) atype = MP_AnnotMpRef; |
---|
169 | else if(strcmp(type, "Prototype_AP") == 0) atype = MP_AnnotProtoPrototype; |
---|
170 | else if(strcmp(type, "Store_AP") == 0) atype = MP_AnnotMpStore; |
---|
171 | else if(strcmp(type, "Retrieve_AP") == 0) atype = MP_AnnotMpRetrieve; |
---|
172 | else if(strcmp(type, "Stored_AP") == 0) atype = MP_AnnotMpStored; |
---|
173 | else if(strcmp(type, "Source_AP") == 0) atype = MP_AnnotMpSource; |
---|
174 | else if(strcmp(type, "Comment_AP") == 0) atype = MP_AnnotMpComment; |
---|
175 | else if(strcmp(type, "Timing_AP") == 0) atype = MP_AnnotMpTiming; |
---|
176 | else if(strcmp(type, "Type_AP") == 0) atype = MP_AnnotMpType; |
---|
177 | else if(strcmp(type, "Units_AP") == 0) atype = MP_AnnotMpUnits; |
---|
178 | else { |
---|
179 | printf("Error in input file - unknown type: %s\n", type); |
---|
180 | exit (1); |
---|
181 | } |
---|
182 | if (atype != 0) { |
---|
183 | fscanf(fd, "%d %X", &dtag, &flags); |
---|
184 | MP_PutAnnotationPacket(link, dtag, atype, flags); |
---|
185 | } |
---|
186 | |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | int get_outfilename(fname) |
---|
191 | char *fname; |
---|
192 | { |
---|
193 | int fd; |
---|
194 | |
---|
195 | if ((fd = open(fname, O_WRONLY | O_CREAT, 0600)) < 0) |
---|
196 | fprintf(stderr, "can't open %s for writing\n", fname); |
---|
197 | |
---|
198 | return fd; |
---|
199 | } |
---|
200 | |
---|
201 | main(argc, argv) |
---|
202 | int argc; |
---|
203 | char *argv[]; |
---|
204 | { |
---|
205 | char *ifname; |
---|
206 | MP_Link_pt link = NULL; |
---|
207 | MP_Env_pt env = NULL; |
---|
208 | float f = -1789.098; |
---|
209 | double d = -1231789.09292998; |
---|
210 | |
---|
211 | |
---|
212 | #ifdef __WIN32__ |
---|
213 | if (WinSockInitialize() != 0) |
---|
214 | exit(1); |
---|
215 | #endif |
---|
216 | |
---|
217 | env = MP_InitializeEnv(NULL); |
---|
218 | if (env == NULL) { |
---|
219 | fprintf(stderr, "%s: MP_InitializeEnv() failed!\n", argv[0]); |
---|
220 | exit(1); |
---|
221 | } |
---|
222 | |
---|
223 | if ((link = MP_OpenLink(env, argc, argv)) == NULL) { |
---|
224 | fprintf(stderr, "%s: MP_OpenLink() failed!\n", argv[0]); |
---|
225 | exit(1); |
---|
226 | } |
---|
227 | |
---|
228 | MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_INIT_EVENTS); |
---|
229 | MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_WRITE_EVENTS); |
---|
230 | MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_READ_EVENTS); |
---|
231 | |
---|
232 | ifname = IMP_GetCmdlineArg(argc, argv, "-infile"); |
---|
233 | |
---|
234 | if (ifname == NULL) { |
---|
235 | MP_LogEvent(link, MP_ERROR_EVENT, "missing -infile argument"); |
---|
236 | exit(-1); |
---|
237 | } |
---|
238 | MP_SetEnvBigIntFormat(env, &imp_gmp_bigint_ops, MP_GMP); |
---|
239 | MP_SetEnvBigRealFormat(env, &imp_gmp_bigreal_ops, MP_GMP); |
---|
240 | |
---|
241 | mpf_set_default_prec(256); /* for the GMP arb prec reals */ |
---|
242 | |
---|
243 | get_input(link, ifname); |
---|
244 | |
---|
245 | MP_CloseLink(link); |
---|
246 | MP_ReleaseEnv(env); |
---|
247 | |
---|
248 | #ifdef __WIN32__ |
---|
249 | WSACleanup(); |
---|
250 | #endif |
---|
251 | |
---|
252 | printf("All done :-)\n"); |
---|
253 | } |
---|