version 0.5.2
[fw/sdcc] / sim / ucsim / cmd.src / cmdutil.cc
1 /*
2  * Simulator of microcontrollers (cmd.src/cmdutil.cc)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  * 
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  *
8  */
9
10 /* This file is part of microcontroller simulator: ucsim.
11
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING.  If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 02111-1307, USA. */
26 /*@1@*/
27
28 #include "ddconfig.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 # include <sys/socket.h>
36 # include <netinet/in.h>
37 # include <arpa/inet.h>
38 #endif
39 #include "i_string.h"
40
41 #include "stypes.h"
42 #include "globals.h"
43 #include "uccl.h"
44
45
46 /*
47  * Making a socket which can be used to listen for a specified port
48  */
49
50 #ifdef SOCKET_AVAIL
51 int
52 make_server_socket(unsigned short int port)
53 {
54   int sock, i;
55   struct sockaddr_in name;
56      
57   /* Create the socket. */
58   sock= socket(PF_INET, SOCK_STREAM, 0);
59   if (sock < 0)
60     {
61       perror("socket");
62       return(0);
63     }
64      
65   /* Give the socket a name. */
66   i= 1;
67   if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof(i)) < 0)
68     {
69       perror("setsockopt");
70     }
71   name.sin_family     = AF_INET;
72   name.sin_port       = htons(port);
73   name.sin_addr.s_addr= htonl(INADDR_ANY);
74   if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0)
75     {
76       perror("bind");
77       return(0);
78     }
79
80   return(sock);
81 }
82 #endif
83
84
85 /*
86  * Printing out an integer in binary format
87  */
88
89 /*void
90 print_bin(long data, int bits, class cl_console *con)
91 {
92   long mask= 1;
93
94   mask= mask << ((bits >= 1)?(bits-1):0);
95   while (bits--)
96     {
97       con->printf("%c", (data&mask)?'1':'0');
98       mask>>= 1;
99     }
100 }*/
101
102
103 /*
104  * Searching for a name in the specified table
105  */
106
107 struct name_entry *
108 get_name_entry(struct name_entry tabl[], char *name, class cl_uc *uc)
109 {
110   int i= 0;
111   char *p;
112
113   if (!tabl ||
114       !name ||
115       !(*name))
116     return(0);
117   for (p= name; *p; *p= toupper(*p), p++);
118   while (tabl[i].name &&
119          (!(tabl[i].cpu_type & uc->type) ||
120          (strcmp(tabl[i].name, name) != 0)))
121     {
122       //printf("tabl[%d].name=%s <-> %s\n",i,tabl[i].name,name);
123       i++;
124     }
125   if (tabl[i].name != NULL)
126     return(&tabl[i]);
127   else
128     return(0);
129 }
130
131
132 /*
133  * Interpreting a bitname
134  */
135
136 /*bool
137 interpret_bitname(char *name, class cl_uc *uc,
138                   uchar **cell, uchar *celladdr,
139                   uchar *bitaddr, uchar *bitmask,
140                   char **symname)
141 {
142   char *dot, *p;
143   char *sym, bitnumstr[2];
144   struct name_entry *ne;
145   int bitnum, i;
146   
147   if ((dot= strchr(name, '.')) != NULL)
148     {
149       *dot++= '\0';
150       if ((ne= get_name_entry(uc->sfr_tbl(), name, uc)) == NULL)
151         {
152           *celladdr= strtol(name, &p, 0);
153           if (p && *p)
154             {
155               dot--;
156               *dot= '.';
157               return(DD_FALSE);
158             }
159         }
160       else
161         *celladdr= ne->addr;
162       if ((*celladdr < 0x20) ||
163           ((*celladdr > 0x2f) && (*celladdr < 0x80)) ||
164           ((*celladdr > 0x7f) && (*celladdr & 0x07)))
165         return(DD_FALSE);
166       bitnum= strtol(dot, &p, 0);
167       if ((p && *p) ||
168           (bitnum < 0) ||
169           (bitnum > 7))
170         return(DD_FALSE);
171       if (*celladdr > 0x7f)
172         *bitaddr= *celladdr + bitnum;
173       else
174         *bitaddr= (*celladdr - 0x20)*8 + bitnum;
175       dot--;
176       *dot= '.';
177     }
178   else
179     {
180       if ((ne= get_name_entry(uc->bit_tbl(), name, uc)) == NULL)
181         {
182           *bitaddr= strtol(name, &p, 0);
183           if ((p && *p) ||
184               (*bitaddr > 0xff))
185             return(DD_FALSE);
186         }
187       else
188         *bitaddr= ne->addr;
189       if (*bitaddr > 0x7f)
190         *celladdr= *bitaddr & 0xf8;
191       else
192         *celladdr= (*bitaddr >> 3) + 0x20;
193     }
194   // *bitaddr, *celladdr now OK
195   *cell= uc->get_bit//FIXME
196     (*bitaddr);
197   *bitmask= BIT_MASK(*bitaddr);
198   // making symbolic name
199   if (!symname)
200     return(DD_TRUE);
201   i= 0;
202   while (uc->bit_tbl()[i].name &&
203          (uc->bit_tbl()[i].addr != *bitaddr))
204     i++;
205   if (uc->bit_tbl()[i].name)
206     {
207       sym= strdup(uc->bit_tbl()[i].name);
208       *symname= sym;
209       return(DD_TRUE);
210     }
211   i= 0;
212   while (uc->sfr_tbl()[i].name &&
213          (uc->sfr_tbl()[i].addr != *celladdr))
214     i++;
215   if (uc->sfr_tbl()[i].name)
216     sym= strdup(uc->sfr_tbl()[i].name);
217   else
218     {
219       sym= (char *)malloc(3);
220       sprintf(sym, "%02x", *celladdr);
221     }
222   sym= (char *)realloc(sym, strlen(sym)+2);
223   strcat(sym, ".");
224   sprintf(bitnumstr, "%1d", *bitaddr & 0x07);
225   strcat(sym, bitnumstr);
226   *symname= sym;
227   return(DD_TRUE);
228 }*/
229
230
231 /*
232  * Processing escape sequencies in a string
233  */
234
235 char *
236 proc_escape(char *string, int *len)
237 {
238   char  spec_chars[]= "fnrtvab\"";
239   char  spec[]= "\f\n\r\t\v\a\b\"";
240   char  *s, *str, *p;
241
242   s  = string;
243   str= (char *)malloc(strlen(string)+1);
244   p  = str;
245   while (*s)
246     {
247       char *spec_c;
248
249       if (*s == '\\' &&
250           *(s+1))
251         {
252           s++;
253           if (*s == '0')
254             {
255               if (!isdigit(*(s+1)))
256                 {
257                   *p++= '\0';
258                   s++;
259                 }
260               else
261                 {
262                   char *octal, *chk, data;
263                   int i, j;
264                   i= strspn(s, "01234567");
265                   octal= (char *)malloc(i+1);
266                   j= 0;
267                   while (*s &&
268                          (j < i))
269                     octal[j++]= *s++;
270                   octal[j]= '\0';
271                   data= strtol(octal, &chk, 8);
272                   if (!chk || !(*chk))
273                     *p++= data;
274                 }
275             }
276           else
277             if ((spec_c= strchr(spec_chars, *s)) != NULL)
278               {
279                 *p++= spec[spec_c-spec_chars];
280                 s++;
281               }
282             else
283               *p++= *s++;
284         }
285       else
286         *p++= *s++;
287     }
288   *p= '\0';
289   *len= p-str;
290   return(str);
291 }
292
293
294 /* End of cmd.src/cmdutil.cc */