1 /***************************************************************************
2 * Copyright (C) 2007-2008 by Øyvind Harboe *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 /* this file contains various functionality useful to standalone systems */
27 #include "time_support.h"
29 #ifdef HAVE_ARPA_INET_H
30 #include <arpa/inet.h>
41 //#ifdef HAVE_NETINET_TCP_H
42 //#include <netinet/tcp.h>
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_STAT_H
60 int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
61 char **args, int argc)
65 command_print(cmd_ctx, "rm <filename>");
66 return ERROR_INVALID_ARGUMENTS;
69 if (unlink(args[0]) != 0)
71 command_print(cmd_ctx, "failed: %d", errno);
78 /* loads a file and returns a pointer to it in memory. The file contains
79 * a 0 byte(sentinel) after len bytes - the length of the file. */
80 int loadFile(const char *fileName, void **data, size_t *len)
82 // ensure returned length is always sane
86 pFile = fopen(fileName,"rb");
89 LOG_ERROR("Can't open %s\n", fileName);
92 if (fseek(pFile, 0, SEEK_END) != 0)
94 LOG_ERROR("Can't open %s\n", fileName);
98 long fsize = ftell(pFile);
101 LOG_ERROR("Can't open %s\n", fileName);
107 if (fseek(pFile, 0, SEEK_SET) != 0)
109 LOG_ERROR("Can't open %s\n", fileName);
113 *data = malloc(*len + 1);
116 LOG_ERROR("Can't open %s\n", fileName);
121 if (fread(*data, 1, *len, pFile)!=*len)
125 LOG_ERROR("Can't open %s\n", fileName);
130 // 0-byte after buffer (not included in *len) serves as a sentinel
131 char *buf = (char *)*data;
139 int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
140 char **args, int argc)
144 command_print(cmd_ctx, "cat <filename>");
145 return ERROR_INVALID_ARGUMENTS;
148 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
152 int retval = loadFile(args[0], &data, &len);
153 if (retval == ERROR_OK)
155 command_print(cmd_ctx, "%s", (char *)data);
160 command_print(cmd_ctx, "%s not found %d", args[0], retval);
165 int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
166 char **args, int argc)
170 command_print(cmd_ctx, "trunc <filename>");
171 return ERROR_INVALID_ARGUMENTS;
174 FILE *config_file = NULL;
175 config_file = fopen(args[0], "w");
176 if (config_file != NULL)
183 int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
186 struct mallinfo info;
190 command_print(cmd_ctx, "meminfo");
191 return ERROR_INVALID_ARGUMENTS;
198 command_print(cmd_ctx, "Diff: %d", prev - info.fordblks);
200 prev = info.fordblks;
202 command_print(cmd_ctx, "Available ram: %d", info.fordblks );
208 int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
209 char **args, int argc)
213 command_print(cmd_ctx,
214 "append <filename> [<string1>, [<string2>, ...]]");
215 return ERROR_INVALID_ARGUMENTS;
218 int retval=ERROR_FAIL;
219 FILE *config_file = NULL;
220 config_file = fopen(args[0], "a");
221 if (config_file != NULL)
224 fseek(config_file, 0, SEEK_END);
226 for (i = 1; i < argc; i++)
228 if (fwrite(args[i], 1, strlen(args[i]), config_file) != strlen(args[i]))
232 if (fwrite(" ", 1, 1, config_file) != 1)
236 if ((i == argc) && (fwrite("\n", 1, 1, config_file) == 1))
248 int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
252 return ERROR_INVALID_ARGUMENTS;
255 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
259 int retval = loadFile(args[0], &data, &len);
260 if (retval != ERROR_OK)
263 FILE *f = fopen(args[1], "wb");
265 retval = ERROR_INVALID_ARGUMENTS;
270 size_t chunk = len - pos;
271 static const size_t maxChunk = 512 * 1024; // ~1/sec
272 if (chunk > maxChunk)
277 if ((retval == ERROR_OK) && (fwrite(((char *)data)+pos, 1, chunk, f) != chunk))
278 retval = ERROR_INVALID_ARGUMENTS;
280 if (retval != ERROR_OK)
285 command_print(cmd_ctx, "%zu", len - pos);
293 if (retval == ERROR_OK)
295 command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
298 command_print(cmd_ctx, "Failed: %d", retval);
306 if (retval != ERROR_OK)
315 #define SHOW_RESULT(a, b) LOG_ERROR(#a " failed %d\n", (int)b)
318 void copyfile(char *name2, char *name1)
326 fd1 = open(name1, O_WRONLY | O_CREAT, 0664);
328 SHOW_RESULT( open, fd1 );
330 fd2 = open(name2, O_RDONLY);
332 SHOW_RESULT( open, fd2 );
336 done = read(fd2, buf, IOSIZE );
339 SHOW_RESULT( read, done );
343 if ( done == 0 ) break;
345 wrote = write(fd1, buf, done);
346 if ( wrote != done ) SHOW_RESULT( write, wrote );
348 if ( wrote != done ) break;
352 if ( err < 0 ) SHOW_RESULT( close, err );
355 if ( err < 0 ) SHOW_RESULT( close, err );
359 /* utility fn to copy a directory */
360 void copydir(char *name, char *destdir)
365 dirp = opendir(destdir);
368 mkdir(destdir, 0777);
371 err = closedir(dirp);
374 dirp = opendir(name);
375 if ( dirp == NULL ) SHOW_RESULT( opendir, -1 );
379 struct dirent *entry = readdir(dirp);
384 if (strcmp(entry->d_name, ".") == 0)
386 if (strcmp(entry->d_name, "..") == 0)
391 char fullPath[PATH_MAX];
392 strncpy(fullPath, name, PATH_MAX);
393 strcat(fullPath, "/");
394 strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
396 if (stat(fullPath, &buf) == -1)
398 LOG_ERROR("unable to read status from %s", fullPath);
401 isDir = S_ISDIR(buf.st_mode) != 0;
406 // diag_printf("<INFO>: entry %14s",entry->d_name);
407 char fullname[PATH_MAX];
408 char fullname2[PATH_MAX];
410 strcpy(fullname, name);
411 strcat(fullname, "/");
412 strcat(fullname, entry->d_name);
414 strcpy(fullname2, destdir);
415 strcat(fullname2, "/");
416 strcat(fullname2, entry->d_name);
417 // diag_printf("from %s to %s\n", fullname, fullname2);
418 copyfile(fullname, fullname2);
420 // diag_printf("\n");
423 err = closedir(dirp);
424 if ( err < 0 ) SHOW_RESULT( stat, err );
431 zylinjtag_Jim_Command_rm(Jim_Interp *interp,
433 Jim_Obj * const *argv)
438 Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
443 if (unlink(Jim_GetString(argv[1], NULL)) == 0)
445 if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
448 return del ? JIM_OK : JIM_ERR;
453 zylinjtag_Jim_Command_ls(Jim_Interp *interp,
455 Jim_Obj * const *argv)
459 Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
463 char *name = (char*) Jim_GetString(argv[1], NULL);
466 dirp = opendir(name);
471 Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
475 struct dirent *entry = NULL;
476 entry = readdir(dirp);
480 if ((strcmp(".", entry->d_name) == 0)||(strcmp("..", entry->d_name) == 0))
483 Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
487 Jim_SetResult(interp, objPtr);
493 zylinjtag_Jim_Command_peek(Jim_Interp *interp,
495 Jim_Obj * const *argv)
499 Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
504 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
507 int value = *((volatile int *) address);
509 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
515 zylinjtag_Jim_Command_poke(Jim_Interp *interp,
517 Jim_Obj * const *argv)
521 Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
526 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
529 if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
532 *((volatile int *) address) = value;
538 /* not so pretty code to fish out ip number*/
539 static int zylinjtag_Jim_Command_ip(Jim_Interp *interp, int argc,
540 Jim_Obj * const *argv)
542 #if !defined(__CYGWIN__)
543 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
545 struct ifaddrs *ifa = NULL, *ifp = NULL;
547 if (getifaddrs(&ifp) < 0)
552 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
557 if (ifa->ifa_addr->sa_family == AF_INET)
558 salen = sizeof(struct sockaddr_in);
559 else if (ifa->ifa_addr->sa_family == AF_INET6)
560 salen = sizeof(struct sockaddr_in6);
564 if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
570 Jim_AppendString(interp, tclOutput, ip, strlen(ip));
577 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "fixme!!!", 0);
578 LOG_ERROR("NOT IMPLEMENTED!!!");
580 Jim_SetResult(interp, tclOutput);
585 /* not so pretty code to fish out eth0 mac address */
586 static int zylinjtag_Jim_Command_mac(Jim_Interp *interp, int argc,
587 Jim_Obj * const *argv)
591 struct ifreq *ifr, *ifend;
597 SockFD = socket(AF_INET, SOCK_DGRAM, 0);
603 ifc.ifc_len = sizeof(ifs);
605 if (ioctl(SockFD, SIOCGIFCONF, &ifc) < 0)
611 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
612 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
614 //if (ifr->ifr_addr.sa_family == AF_INET)
616 if (strcmp("eth0", ifr->ifr_name) != 0)
618 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
619 if (ioctl(SockFD, SIOCGIFHWADDR, &ifreq) < 0)
628 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
631 sprintf(buffer, "%02x-%02x-%02x-%02x-%02x-%02x",
632 ifreq.ifr_hwaddr.sa_data[0]&0xff,
633 ifreq.ifr_hwaddr.sa_data[1]&0xff,
634 ifreq.ifr_hwaddr.sa_data[2]&0xff,
635 ifreq.ifr_hwaddr.sa_data[3]&0xff,
636 ifreq.ifr_hwaddr.sa_data[4]&0xff,
637 ifreq.ifr_hwaddr.sa_data[5]&0xff);
639 Jim_AppendString(interp, tclOutput, buffer, strlen(buffer));
641 Jim_SetResult(interp, tclOutput);
654 int ioutil_init(struct command_context_s *cmd_ctx)
656 register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY,
659 register_command(cmd_ctx, NULL, "cat", handle_cat_command, COMMAND_ANY,
660 "display file content");
662 register_command(cmd_ctx, NULL, "trunc", handle_trunc_command, COMMAND_ANY,
663 "truncate a file to 0 size");
665 register_command(cmd_ctx, NULL, "cp", handle_cp_command,
666 COMMAND_ANY, "copy a file <from> <to>");
668 register_command(cmd_ctx, NULL, "append_file", handle_append_command,
669 COMMAND_ANY, "append a variable number of strings to a file");
671 register_command(cmd_ctx, NULL, "meminfo", handle_meminfo_command,
672 COMMAND_ANY, "display available ram memory");
674 Jim_CreateCommand(interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL);
676 Jim_CreateCommand(interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL);
677 Jim_CreateCommand(interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL);
678 Jim_CreateCommand(interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL);
680 Jim_CreateCommand(interp, "mac", zylinjtag_Jim_Command_mac,
683 Jim_CreateCommand(interp, "ip", zylinjtag_Jim_Command_ip,