2 * Simulator of microcontrollers (cmd.src/cmdutil.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
10 /* This file is part of microcontroller simulator: ucsim.
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.
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.
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
33 #include <sys/types.h>
35 # include HEADER_SOCKET
36 # if defined HAVE_SYS_SOCKET_H
37 # include <netinet/in.h>
38 # include <arpa/inet.h>
49 * Making a socket which can be used to listen for a specified port
54 make_server_socket(unsigned short int port)
57 struct sockaddr_in name;
59 /* Create the socket. */
60 sock= socket(PF_INET, SOCK_STREAM, 0);
67 /* Give the socket a name. */
69 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof(i)) < 0)
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)
88 * Printing out an integer in binary format
92 print_bin(long data, int bits, class cl_console *con)
96 mask= mask << ((bits >= 1)?(bits-1):0);
99 con->printf("%c", (data&mask)?'1':'0');
106 * Searching for a name in the specified table
110 get_name_entry(struct name_entry tabl[], char *name, class cl_uc *uc)
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)))
124 //printf("tabl[%d].name=%s <-> %s\n",i,tabl[i].name,name);
127 if (tabl[i].name != NULL)
135 * Interpreting a bitname
139 interpret_bitname(char *name, class cl_uc *uc,
140 uchar **cell, uchar *celladdr,
141 uchar *bitaddr, uchar *bitmask,
145 char *sym, bitnumstr[2];
146 struct name_entry *ne;
149 if ((dot= strchr(name, '.')) != NULL)
152 if ((ne= get_name_entry(uc->sfr_tbl(), name, uc)) == NULL)
154 *celladdr= strtol(name, &p, 0);
164 if ((*celladdr < 0x20) ||
165 ((*celladdr > 0x2f) && (*celladdr < 0x80)) ||
166 ((*celladdr > 0x7f) && (*celladdr & 0x07)))
168 bitnum= strtol(dot, &p, 0);
173 if (*celladdr > 0x7f)
174 *bitaddr= *celladdr + bitnum;
176 *bitaddr= (*celladdr - 0x20)*8 + bitnum;
182 if ((ne= get_name_entry(uc->bit_tbl(), name, uc)) == NULL)
184 *bitaddr= strtol(name, &p, 0);
192 *celladdr= *bitaddr & 0xf8;
194 *celladdr= (*bitaddr >> 3) + 0x20;
196 // *bitaddr, *celladdr now OK
197 *cell= uc->get_bit//FIXME
199 *bitmask= BIT_MASK(*bitaddr);
200 // making symbolic name
204 while (uc->bit_tbl()[i].name &&
205 (uc->bit_tbl()[i].addr != *bitaddr))
207 if (uc->bit_tbl()[i].name)
209 sym= strdup(uc->bit_tbl()[i].name);
214 while (uc->sfr_tbl()[i].name &&
215 (uc->sfr_tbl()[i].addr != *celladdr))
217 if (uc->sfr_tbl()[i].name)
218 sym= strdup(uc->sfr_tbl()[i].name);
221 sym= (char *)malloc(3);
222 sprintf(sym, "%02x", *celladdr);
224 sym= (char *)realloc(sym, strlen(sym)+2);
226 sprintf(bitnumstr, "%1d", *bitaddr & 0x07);
227 strcat(sym, bitnumstr);
234 * Processing escape sequencies in a string
238 proc_escape(char *string, int *len)
240 char spec_chars[]= "fnrtvab\"";
241 char spec[]= "\f\n\r\t\v\a\b\"";
245 str= (char *)malloc(strlen(string)+1);
257 if (!isdigit(*(s+1)))
264 char *octal, *chk, data;
266 i= strspn(s, "01234567");
267 octal= (char *)malloc(i+1);
273 data= strtol(octal, &chk, 8);
279 if ((spec_c= strchr(spec_chars, *s)) != NULL)
281 *p++= spec[spec_c-spec_chars];
297 extern "C" int vasprintf(char **strp, const char *format, va_list ap);
298 extern "C" int vsnprintf(char *str, size_t size,const char *format,va_list ap);
301 cmd_vfprintf(FILE *f, char *format, va_list ap)
306 #ifdef HAVE_VASPRINTF
308 vasprintf(&msg, format, ap);
309 ret= fprintf(f, "%s", msg);
312 # ifdef HAVE_VSNPRINTF
314 vsnprintf(msg, 80*25, format, ap);
315 ret= fprintf(f, "%s", msg);
319 vsprintf(msg, format, ap); /* Dangerous */
320 ret= fprintf(f, "%s", msg);
324 /*strcpy(msg, "Unimplemented printf has called.\n");*/
326 /*strcpy(msg, "printf can not be implemented, upgrade your libc.\n");*/
336 cmd_fprintf(FILE *f, char *format, ...)
339 va_start(ap, format);
340 int ret= cmd_vfprintf(f, format, ap);
346 /* End of cmd.src/cmdutil.cc */