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
58 //#ifdef HAVE_STRINGS_H
59 //#include <strings.h>
63 int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
64 char **args, int argc)
68 command_print(cmd_ctx, "rm <filename>");
69 return ERROR_INVALID_ARGUMENTS;
72 if (unlink(args[0]) != 0)
74 command_print(cmd_ctx, "failed: %d", errno);
81 /* loads a file and returns a pointer to it in memory. The file contains
82 * a 0 byte(sentinel) after len bytes - the length of the file. */
83 int loadFile(const char *fileName, void **data, size_t *len)
85 // ensure returned length is always sane
89 pFile = fopen(fileName,"rb");
92 LOG_ERROR("Can't open %s\n", fileName);
95 if (fseek(pFile, 0, SEEK_END)!=0)
97 LOG_ERROR("Can't open %s\n", fileName);
101 long fsize = ftell(pFile);
104 LOG_ERROR("Can't open %s\n", fileName);
110 if (fseek(pFile, 0, SEEK_SET)!=0)
112 LOG_ERROR("Can't open %s\n", fileName);
116 *data = malloc(*len + 1);
119 LOG_ERROR("Can't open %s\n", fileName);
124 if (fread(*data, 1, *len, pFile)!=*len)
128 LOG_ERROR("Can't open %s\n", fileName);
133 // 0-byte after buffer (not included in *len) serves as a sentinel
134 char *buf = (char *)*data;
142 int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
143 char **args, int argc)
147 command_print(cmd_ctx, "cat <filename>");
148 return ERROR_INVALID_ARGUMENTS;
151 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
155 int retval = loadFile(args[0], &data, &len);
156 if (retval == ERROR_OK)
158 command_print(cmd_ctx, "%s", (char *)data);
163 command_print(cmd_ctx, "%s not found %d", args[0], retval);
168 int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
169 char **args, int argc)
173 command_print(cmd_ctx, "trunc <filename>");
174 return ERROR_INVALID_ARGUMENTS;
177 FILE *config_file = NULL;
178 config_file = fopen(args[0], "w");
179 if (config_file != NULL)
186 int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
189 struct mallinfo info;
193 command_print(cmd_ctx, "meminfo");
194 return ERROR_INVALID_ARGUMENTS;
201 command_print(cmd_ctx, "Diff: %d", prev - info.fordblks);
203 prev = info.fordblks;
205 command_print(cmd_ctx, "Available ram: %d", info.fordblks );
211 int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
212 char **args, int argc)
216 command_print(cmd_ctx,
217 "append <filename> [<string1>, [<string2>, ...]]");
218 return ERROR_INVALID_ARGUMENTS;
221 int retval=ERROR_FAIL;
222 FILE *config_file = NULL;
223 config_file = fopen(args[0], "a");
224 if (config_file != NULL)
227 fseek(config_file, 0, SEEK_END);
229 for (i = 1; i < argc; i++)
231 if (fwrite(args[i], strlen(args[i]), 1, config_file)!=strlen(args[i]))
235 if (fwrite(" ", 1, 1, config_file)!=1)
239 if ((i==argc)&&(fwrite("\n", 1, 1, config_file)==1))
251 int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
255 return ERROR_INVALID_ARGUMENTS;
258 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
262 int retval = loadFile(args[0], &data, &len);
263 if (retval != ERROR_OK)
266 FILE *f = fopen(args[1], "wb");
268 retval = ERROR_INVALID_ARGUMENTS;
273 size_t chunk = len - pos;
274 static const size_t maxChunk = 512 * 1024; // ~1/sec
275 if (chunk > maxChunk)
280 if ((retval==ERROR_OK)&&(fwrite(((char *)data)+pos, 1, chunk, f)!=chunk))
281 retval = ERROR_INVALID_ARGUMENTS;
283 if (retval != ERROR_OK)
288 command_print(cmd_ctx, "%d", len - pos);
296 if (retval == ERROR_OK)
298 command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
301 command_print(cmd_ctx, "Failed: %d", retval);
309 if (retval != ERROR_OK)
318 #define SHOW_RESULT(a, b) LOG_ERROR(#a " failed %d\n", (int)b)
321 void copyfile(char *name2, char *name1)
329 fd1 = open(name1, O_WRONLY | O_CREAT);
331 SHOW_RESULT( open, fd1 );
333 fd2 = open(name2, O_RDONLY);
335 SHOW_RESULT( open, fd2 );
339 done = read(fd2, buf, IOSIZE );
342 SHOW_RESULT( read, done );
346 if( done == 0 ) break;
348 wrote = write(fd1, buf, done);
349 if( wrote != done ) SHOW_RESULT( write, wrote );
351 if( wrote != done ) break;
355 if( err < 0 ) SHOW_RESULT( close, err );
358 if( err < 0 ) SHOW_RESULT( close, err );
362 /* utility fn to copy a directory */
363 void copydir(char *name, char *destdir)
368 dirp = opendir(destdir);
371 mkdir(destdir, 0777);
374 err = closedir(dirp);
377 dirp = opendir(name);
378 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
382 struct dirent *entry = readdir(dirp);
387 if (strcmp(entry->d_name, ".") == 0)
389 if (strcmp(entry->d_name, "..") == 0)
394 char fullPath[PATH_MAX];
395 strncpy(fullPath, name, PATH_MAX);
396 strcat(fullPath, "/");
397 strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
399 if (stat(fullPath, &buf) == -1)
401 LOG_ERROR("unable to read status from %s", fullPath);
404 isDir = S_ISDIR(buf.st_mode) != 0;
409 // diag_printf("<INFO>: entry %14s",entry->d_name);
410 char fullname[PATH_MAX];
411 char fullname2[PATH_MAX];
413 strcpy(fullname, name);
414 strcat(fullname, "/");
415 strcat(fullname, entry->d_name);
417 strcpy(fullname2, destdir);
418 strcat(fullname2, "/");
419 strcat(fullname2, entry->d_name);
420 // diag_printf("from %s to %s\n", fullname, fullname2);
421 copyfile(fullname, fullname2);
423 // diag_printf("\n");
426 err = closedir(dirp);
427 if( err < 0 ) SHOW_RESULT( stat, err );
434 zylinjtag_Jim_Command_rm(Jim_Interp *interp,
436 Jim_Obj * const *argv)
441 Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
446 if (unlink(Jim_GetString(argv[1], NULL)) == 0)
448 if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
451 return del ? JIM_OK : JIM_ERR;
456 zylinjtag_Jim_Command_ls(Jim_Interp *interp,
458 Jim_Obj * const *argv)
462 Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
466 char *name = (char*) Jim_GetString(argv[1], NULL);
469 dirp = opendir(name);
474 Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
478 struct dirent *entry = NULL;
479 entry = readdir(dirp);
483 if ((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name)==0))
486 Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
490 Jim_SetResult(interp, objPtr);
496 zylinjtag_Jim_Command_peek(Jim_Interp *interp,
498 Jim_Obj * const *argv)
502 Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
507 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
510 int value = *((volatile int *) address);
512 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
518 zylinjtag_Jim_Command_poke(Jim_Interp *interp,
520 Jim_Obj * const *argv)
524 Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
529 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
532 if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
535 *((volatile int *) address) = value;
541 /* not so pretty code to fish out ip number*/
542 static int zylinjtag_Jim_Command_ip(Jim_Interp *interp, int argc,
543 Jim_Obj * const *argv)
545 #if !defined(__CYGWIN__)
546 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
548 struct ifaddrs *ifa = NULL, *ifp = NULL;
550 if (getifaddrs(&ifp) < 0)
555 for (ifa = ifp; ifa; ifa = ifa->ifa_next)
560 if (ifa->ifa_addr->sa_family == AF_INET)
561 salen = sizeof(struct sockaddr_in);
562 else if (ifa->ifa_addr->sa_family == AF_INET6)
563 salen = sizeof(struct sockaddr_in6);
567 if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
573 Jim_AppendString(interp, tclOutput, ip, strlen(ip));
580 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "fixme!!!", 0);
581 LOG_ERROR("NOT IMPLEMENTED!!!");
583 Jim_SetResult(interp, tclOutput);
588 /* not so pretty code to fish out eth0 mac address */
589 static int zylinjtag_Jim_Command_mac(Jim_Interp *interp, int argc,
590 Jim_Obj * const *argv)
594 struct ifreq *ifr, *ifend;
600 SockFD = socket(AF_INET, SOCK_DGRAM, 0);
606 ifc.ifc_len = sizeof(ifs);
608 if (ioctl(SockFD, SIOCGIFCONF, &ifc) < 0)
614 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
615 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
617 //if (ifr->ifr_addr.sa_family == AF_INET)
619 if (strcmp("eth0", ifr->ifr_name)!=0)
621 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
622 if (ioctl(SockFD, SIOCGIFHWADDR, &ifreq) < 0)
631 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
634 sprintf(buffer, "%02x-%02x-%02x-%02x-%02x-%02x",
635 ifreq.ifr_hwaddr.sa_data[0]&0xff,
636 ifreq.ifr_hwaddr.sa_data[1]&0xff,
637 ifreq.ifr_hwaddr.sa_data[2]&0xff,
638 ifreq.ifr_hwaddr.sa_data[3]&0xff,
639 ifreq.ifr_hwaddr.sa_data[4]&0xff,
640 ifreq.ifr_hwaddr.sa_data[5]&0xff);
642 Jim_AppendString(interp, tclOutput, buffer, strlen(buffer));
644 Jim_SetResult(interp, tclOutput);
657 int ioutil_init(struct command_context_s *cmd_ctx)
659 register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY,
662 register_command(cmd_ctx, NULL, "cat", handle_cat_command, COMMAND_ANY,
663 "display file content");
665 register_command(cmd_ctx, NULL, "trunc", handle_trunc_command, COMMAND_ANY,
666 "truncate a file to 0 size");
668 register_command(cmd_ctx, NULL, "cp", handle_cp_command,
669 COMMAND_ANY, "copy a file <from> <to>");
671 register_command(cmd_ctx, NULL, "append_file", handle_append_command,
672 COMMAND_ANY, "append a variable number of strings to a file");
674 register_command(cmd_ctx, NULL, "meminfo", handle_meminfo_command,
675 COMMAND_ANY, "display available ram memory");
677 Jim_CreateCommand(interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL);
679 Jim_CreateCommand(interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL);
680 Jim_CreateCommand(interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL);
681 Jim_CreateCommand(interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL);
683 Jim_CreateCommand(interp, "mac", zylinjtag_Jim_Command_mac,
686 Jim_CreateCommand(interp, "ip", zylinjtag_Jim_Command_ip,