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 */
28 #include "configuration.h"
33 #include <time_support.h>
35 #include <sys/types.h>
50 #include <netinet/tcp.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
59 #include <netinet/in.h>
61 #include <arpa/inet.h>
69 int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
70 char **args, int argc)
74 command_print(cmd_ctx, "rm <filename>");
75 return ERROR_INVALID_ARGUMENTS;
78 if (unlink(args[0]) != 0)
80 command_print(cmd_ctx, "failed: %d", errno);
87 /* loads a file and returns a pointer to it in memory. The file contains
88 * a 0 byte(sentinel) after len bytes - the length of the file. */
89 int loadFile(const char *fileName, void **data, int *len)
92 pFile = fopen(fileName,"rb");
95 LOG_ERROR("Can't open %s\n", fileName);
98 if (fseek(pFile, 0, SEEK_END)!=0)
100 LOG_ERROR("Can't open %s\n", fileName);
107 LOG_ERROR("Can't open %s\n", fileName);
112 if (fseek(pFile, 0, SEEK_SET)!=0)
114 LOG_ERROR("Can't open %s\n", fileName);
118 *data=malloc(*len+1);
121 LOG_ERROR("Can't open %s\n", fileName);
126 if (fread(*data, 1, *len, pFile)!=*len)
130 LOG_ERROR("Can't open %s\n", fileName);
134 *(((char *)(*data))+*len)=0; /* sentinel */
144 int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
145 char **args, int argc)
149 command_print(cmd_ctx, "cat <filename>");
150 return ERROR_INVALID_ARGUMENTS;
153 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
157 int retval = loadFile(args[0], &data, &len);
158 if (retval == ERROR_OK)
160 command_print(cmd_ctx, "%s", data);
165 command_print(cmd_ctx, "%s not found %d", args[0], retval);
170 int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
171 char **args, int argc)
175 command_print(cmd_ctx, "trunc <filename>");
176 return ERROR_INVALID_ARGUMENTS;
179 FILE *config_file = NULL;
180 config_file = fopen(args[0], "w");
181 if (config_file != NULL)
188 int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
191 struct mallinfo info;
195 command_print(cmd_ctx, "meminfo");
196 return ERROR_INVALID_ARGUMENTS;
203 command_print(cmd_ctx, "Diff: %d", prev - info.fordblks);
205 prev = info.fordblks;
207 command_print(cmd_ctx, "Available ram: %d", info.fordblks );
213 int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
214 char **args, int argc)
218 command_print(cmd_ctx,
219 "append <filename> [<string1>, [<string2>, ...]]");
220 return ERROR_INVALID_ARGUMENTS;
223 FILE *config_file = NULL;
224 config_file = fopen(args[0], "a");
225 if (config_file != NULL)
228 fseek(config_file, 0, SEEK_END);
230 for (i = 1; i < argc; i++)
232 fwrite(args[i], strlen(args[i]), 1, config_file);
235 fwrite(" ", 1, 1, config_file);
238 fwrite("\n", 1, 1, config_file);
247 int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
251 return ERROR_INVALID_ARGUMENTS;
254 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
258 int retval = loadFile(args[0], &data, &len);
259 if (retval != ERROR_OK)
262 FILE *f = fopen(args[1], "wb");
264 retval = ERROR_INVALID_ARGUMENTS;
269 int chunk = len - pos;
270 static const int maxChunk = 512 * 1024; // ~1/sec
271 if (chunk > maxChunk)
276 if ((retval==ERROR_OK)&&(fwrite(((char *)data)+pos, 1, chunk, f)!=chunk))
277 retval = ERROR_INVALID_ARGUMENTS;
279 if (retval != ERROR_OK)
284 command_print(cmd_ctx, "%d", len - pos);
292 if (retval == ERROR_OK)
294 command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
297 command_print(cmd_ctx, "Failed: %d", retval);
305 if (retval != ERROR_OK)
314 #define SHOW_RESULT(a, b) LOG_ERROR(#a " failed %d\n", (int)b)
317 void copyfile(char *name2, char *name1)
325 fd1 = open(name1, O_WRONLY | O_CREAT);
327 SHOW_RESULT( open, fd1 );
329 fd2 = open(name2, O_RDONLY);
331 SHOW_RESULT( open, fd2 );
335 done = read(fd2, buf, IOSIZE );
338 SHOW_RESULT( read, done );
342 if( done == 0 ) break;
344 wrote = write(fd1, buf, done);
345 if( wrote != done ) SHOW_RESULT( write, wrote );
347 if( wrote != done ) break;
351 if( err < 0 ) SHOW_RESULT( close, err );
354 if( err < 0 ) SHOW_RESULT( close, err );
358 /* utility fn to copy a directory */
359 void copydir(char *name, char *destdir)
364 dirp = opendir(destdir);
367 mkdir(destdir, 0777);
370 err = closedir(dirp);
373 dirp = opendir(name);
374 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
378 struct dirent *entry = readdir(dirp);
383 if (strcmp(entry->d_name, ".") == 0)
385 if (strcmp(entry->d_name, "..") == 0)
390 char fullPath[PATH_MAX];
391 strncpy(fullPath, name, PATH_MAX);
392 strcat(fullPath, "/");
393 strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
395 if (stat(fullPath, &buf) == -1)
397 LOG_ERROR("unable to read status from %s", fullPath);
400 isDir = S_ISDIR(buf.st_mode) != 0;
405 // diag_printf("<INFO>: entry %14s",entry->d_name);
406 char fullname[PATH_MAX];
407 char fullname2[PATH_MAX];
409 strcpy(fullname, name);
410 strcat(fullname, "/");
411 strcat(fullname, entry->d_name);
413 strcpy(fullname2, destdir);
414 strcat(fullname2, "/");
415 strcat(fullname2, entry->d_name);
416 // diag_printf("from %s to %s\n", fullname, fullname2);
417 copyfile(fullname, fullname2);
419 // diag_printf("\n");
422 err = closedir(dirp);
423 if( err < 0 ) SHOW_RESULT( stat, err );
430 zylinjtag_Jim_Command_rm(Jim_Interp *interp,
432 Jim_Obj * const *argv)
437 Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
442 if (unlink(Jim_GetString(argv[1], NULL)) == 0)
444 if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
447 return del ? JIM_OK : JIM_ERR;
452 zylinjtag_Jim_Command_ls(Jim_Interp *interp,
454 Jim_Obj * const *argv)
458 Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
462 char *name = (char*) Jim_GetString(argv[1], NULL);
465 dirp = opendir(name);
470 Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
474 struct dirent *entry = NULL;
475 entry = readdir(dirp);
479 if ((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name)==0))
482 Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
486 Jim_SetResult(interp, objPtr);
491 int handle_peek_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
495 return ERROR_COMMAND_SYNTAX_ERROR;
497 volatile int *address=(volatile int *)strtoul(args[0], NULL, 0);
499 command_print(cmd_ctx, "0x%x : 0x%x", address, value);
503 int handle_poke_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
507 return ERROR_INVALID_ARGUMENTS;
509 volatile int *address=(volatile int *)strtoul(args[0], NULL, 0);
510 int value=strtoul(args[1], NULL, 0);
516 zylinjtag_Jim_Command_peek(Jim_Interp *interp,
518 Jim_Obj * const *argv)
522 Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
527 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
530 int value = *((volatile int *) address);
532 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
538 zylinjtag_Jim_Command_poke(Jim_Interp *interp,
540 Jim_Obj * const *argv)
544 Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
549 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
552 if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
555 *((volatile int *) address) = value;
560 int ioutil_init(struct command_context_s *cmd_ctx)
562 register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY,
565 register_command(cmd_ctx, NULL, "cat", handle_cat_command, COMMAND_ANY,
566 "display file content");
568 register_command(cmd_ctx, NULL, "trunc", handle_trunc_command, COMMAND_ANY,
569 "truncate a file to 0 size");
571 register_command(cmd_ctx, NULL, "cp", handle_cp_command,
572 COMMAND_ANY, "copy a file <from> <to>");
574 register_command(cmd_ctx, NULL, "append_file", handle_append_command,
575 COMMAND_ANY, "append a variable number of strings to a file");
577 register_command(cmd_ctx, NULL, "meminfo", handle_meminfo_command,
578 COMMAND_ANY, "display available ram memory");
580 Jim_CreateCommand(interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL);
582 Jim_CreateCommand(interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL);
583 Jim_CreateCommand(interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL);
584 Jim_CreateCommand(interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL);