977068fc1b7b8eeb60f8dc3e424b89853aee2215
[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 SOCKET_AVAIL
35 # include HEADER_SOCKET
36 # if defined HAVE_SYS_SOCKET_H
37 #  include <netinet/in.h>
38 #  include <arpa/inet.h>
39 # endif
40 #endif
41 #include "i_string.h"
42
43 #include "stypes.h"
44 #include "globals.h"
45 #include "uccl.h"
46
47
48 /*
49  * Making a socket which can be used to listen for a specified port
50  */
51
52 #ifdef SOCKET_AVAIL
53 int
54 make_server_socket(unsigned short int port)
55 {
56   int sock, i;
57   struct sockaddr_in name;
58      
59   /* Create the socket. */
60   sock= socket(PF_INET, SOCK_STREAM, 0);
61   if (sock < 0)
62     {
63       perror("socket");
64       return(0);
65     }
66      
67   /* Give the socket a name. */
68   i= 1;
69   if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof(i)) < 0)
70     {
71       perror("setsockopt");
72     }
73   name.sin_family     = AF_INET;
74   name.sin_port       = htons(port);
75   name.sin_addr.s_addr= htonl(INADDR_ANY);
76   if (bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0)
77     {
78       perror("bind");
79       return(0);
80     }
81
82   return(sock);
83 }
84 #endif
85
86
87 /*
88  * Printing out an integer in binary format
89  */
90
91 /*void
92 print_bin(long data, int bits, class cl_console *con)
93 {
94   long mask= 1;
95
96   mask= mask << ((bits >= 1)?(bits-1):0);
97   while (bits--)
98     {
99       con->printf("%c", (data&mask)?'1':'0');
100       mask>>= 1;
101     }
102 }*/
103
104
105 /*
106  * Searching for a name in the specified table
107  */
108
109 struct name_entry *
110 get_name_entry(struct name_entry tabl[], char *name, class cl_uc *uc)
111 {
112   int i= 0;
113   char *p;
114
115   if (!tabl ||
116       !name ||
117       !(*name))
118     return(0);
119   for (p= name; *p; *p= toupper(*p), p++);
120   while (tabl[i].name &&
121          (!(tabl[i].cpu_type & uc->type) ||
122          (strcmp(tabl[i].name, name) != 0)))
123     {
124       //printf("tabl[%d].name=%s <-> %s\n",i,tabl[i].name,name);
125       i++;
126     }
127   if (tabl[i].name != NULL)
128     return(&tabl[i]);
129   else
130     return(0);
131 }
132
133
134 /*
135  * Interpreting a bitname
136  */
137
138 /*bool
139 interpret_bitname(char *name, class cl_uc *uc,
140                   uchar **cell, uchar *celladdr,
141                   uchar *bitaddr, uchar *bitmask,
142                   char **symname)
143 {
144   char *dot, *p;
145   char *sym, bitnumstr[2];
146   struct name_entry *ne;
147   int bitnum, i;
148   
149   if ((dot= strchr(name, '.')) != NULL)
150     {
151       *dot++= '\0';
152       if ((ne= get_name_entry(uc->sfr_tbl(), name, uc)) == NULL)
153         {
154           *celladdr= strtol(name, &p, 0);
155           if (p && *p)
156             {
157               dot--;
158               *dot= '.';
159               return(DD_FALSE);
160             }
161         }
162       else
163         *celladdr= ne->addr;
164       if ((*celladdr < 0x20) ||
165           ((*celladdr > 0x2f) && (*celladdr < 0x80)) ||
166           ((*celladdr > 0x7f) && (*celladdr & 0x07)))
167         return(DD_FALSE);
168       bitnum= strtol(dot, &p, 0);
169       if ((p && *p) ||
170           (bitnum < 0) ||
171           (bitnum > 7))
172         return(DD_FALSE);
173       if (*celladdr > 0x7f)
174         *bitaddr= *celladdr + bitnum;
175       else
176         *bitaddr= (*celladdr - 0x20)*8 + bitnum;
177       dot--;
178       *dot= '.';
179     }
180   else
181     {
182       if ((ne= get_name_entry(uc->bit_tbl(), name, uc)) == NULL)
183         {
184           *bitaddr= strtol(name, &p, 0);
185           if ((p && *p) ||
186               (*bitaddr > 0xff))
187             return(DD_FALSE);
188         }
189       else
190         *bitaddr= ne->addr;
191       if (*bitaddr > 0x7f)
192         *celladdr= *bitaddr & 0xf8;
193       else
194         *celladdr= (*bitaddr >> 3) + 0x20;
195     }
196   // *bitaddr, *celladdr now OK
197   *cell= uc->get_bit//FIXME
198     (*bitaddr);
199   *bitmask= BIT_MASK(*bitaddr);
200   // making symbolic name
201   if (!symname)
202     return(DD_TRUE);
203   i= 0;
204   while (uc->bit_tbl()[i].name &&
205          (uc->bit_tbl()[i].addr != *bitaddr))
206     i++;
207   if (uc->bit_tbl()[i].name)
208     {
209       sym= strdup(uc->bit_tbl()[i].name);
210       *symname= sym;
211       return(DD_TRUE);
212     }
213   i= 0;
214   while (uc->sfr_tbl()[i].name &&
215          (uc->sfr_tbl()[i].addr != *celladdr))
216     i++;
217   if (uc->sfr_tbl()[i].name)
218     sym= strdup(uc->sfr_tbl()[i].name);
219   else
220     {
221       sym= (char *)malloc(3);
222       sprintf(sym, "%02x", *celladdr);
223     }
224   sym= (char *)realloc(sym, strlen(sym)+2);
225   strcat(sym, ".");
226   sprintf(bitnumstr, "%1d", *bitaddr & 0x07);
227   strcat(sym, bitnumstr);
228   *symname= sym;
229   return(DD_TRUE);
230 }*/
231
232
233 /*
234  * Processing escape sequencies in a string
235  */
236
237 char *
238 proc_escape(char *string, int *len)
239 {
240   char  spec_chars[]= "fnrtvab\"";
241   char  spec[]= "\f\n\r\t\v\a\b\"";
242   char  *s, *str, *p;
243
244   s  = string;
245   str= (char *)malloc(strlen(string)+1);
246   p  = str;
247   while (*s)
248     {
249       char *spec_c;
250
251       if (*s == '\\' &&
252           *(s+1))
253         {
254           s++;
255           if (*s == '0')
256             {
257               if (!isdigit(*(s+1)))
258                 {
259                   *p++= '\0';
260                   s++;
261                 }
262               else
263                 {
264                   char *octal, *chk, data;
265                   int i, j;
266                   i= strspn(s, "01234567");
267                   octal= (char *)malloc(i+1);
268                   j= 0;
269                   while (*s &&
270                          (j < i))
271                     octal[j++]= *s++;
272                   octal[j]= '\0';
273                   data= strtol(octal, &chk, 8);
274                   if (!chk || !(*chk))
275                     *p++= data;
276                 }
277             }
278           else
279             if ((spec_c= strchr(spec_chars, *s)) != NULL)
280               {
281                 *p++= spec[spec_c-spec_chars];
282                 s++;
283               }
284             else
285               *p++= *s++;
286         }
287       else
288         *p++= *s++;
289     }
290   *p= '\0';
291   *len= p-str;
292   return(str);
293 }
294
295
296 /* End of cmd.src/cmdutil.cc */