1 /***************************************************************************
\r
2 * Copyright (C) 2007-2008 by Øyvind Harboe *
\r
4 * This program is free software; you can redistribute it and/or modify *
\r
5 * it under the terms of the GNU General Public License as published by *
\r
6 * the Free Software Foundation; either version 2 of the License, or *
\r
7 * (at your option) any later version. *
\r
9 * This program is distributed in the hope that it will be useful, *
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
12 * GNU General Public License for more details. *
\r
14 * You should have received a copy of the GNU General Public License *
\r
15 * along with this program; if not, write to the *
\r
16 * Free Software Foundation, Inc., *
\r
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
\r
18 ***************************************************************************/
\r
20 /* this file contains various functionality useful to standalone systems */
\r
22 #ifdef HAVE_CONFIG_H
\r
28 #include "configuration.h"
\r
31 #include "command.h"
\r
33 #include <time_support.h>
\r
34 #include <sys/time.h>
\r
35 #include <sys/types.h>
\r
36 #include <strings.h>
\r
41 #if !BUILD_ECOSBOARD
\r
48 #include <sys/stat.h>
\r
50 #include <netinet/tcp.h>
\r
51 #include <sys/ioctl.h>
\r
52 #include <sys/socket.h>
\r
53 #include <netinet/in.h>
\r
55 #include <arpa/inet.h>
\r
56 #include <sys/types.h>
\r
57 #include <sys/socket.h>
\r
59 #include <netinet/in.h>
\r
61 #include <arpa/inet.h>
\r
69 int handle_rm_command(struct command_context_s *cmd_ctx, char *cmd,
\r
70 char **args, int argc)
\r
74 command_print(cmd_ctx, "rm <filename>");
\r
75 return ERROR_INVALID_ARGUMENTS;
\r
78 if (unlink(args[0]) != 0)
\r
80 command_print(cmd_ctx, "failed: %d", errno);
\r
87 /* loads a file and returns a pointer to it in memory. The file contains
\r
88 * a 0 byte(sentinel) after len bytes - the length of the file. */
\r
89 int loadFile(const char *fileName, void **data, int *len)
\r
92 pFile = fopen(fileName,"rb");
\r
95 LOG_ERROR("Can't open %s\n", fileName);
\r
98 if (fseek(pFile, 0, SEEK_END)!=0)
\r
100 LOG_ERROR("Can't open %s\n", fileName);
\r
107 LOG_ERROR("Can't open %s\n", fileName);
\r
112 if (fseek(pFile, 0, SEEK_SET)!=0)
\r
114 LOG_ERROR("Can't open %s\n", fileName);
\r
118 *data=malloc(*len+1);
\r
121 LOG_ERROR("Can't open %s\n", fileName);
\r
126 if (fread(*data, 1, *len, pFile)!=*len)
\r
130 LOG_ERROR("Can't open %s\n", fileName);
\r
134 *(((char *)(*data))+*len)=0; /* sentinel */
\r
144 int handle_cat_command(struct command_context_s *cmd_ctx, char *cmd,
\r
145 char **args, int argc)
\r
149 command_print(cmd_ctx, "cat <filename>");
\r
150 return ERROR_INVALID_ARGUMENTS;
\r
153 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
\r
157 int retval = loadFile(args[0], &data, &len);
\r
158 if (retval == ERROR_OK)
\r
160 command_print(cmd_ctx, "%s", data);
\r
165 command_print(cmd_ctx, "%s not found %d", args[0], retval);
\r
170 int handle_trunc_command(struct command_context_s *cmd_ctx, char *cmd,
\r
171 char **args, int argc)
\r
175 command_print(cmd_ctx, "trunc <filename>");
\r
176 return ERROR_INVALID_ARGUMENTS;
\r
179 FILE *config_file = NULL;
\r
180 config_file = fopen(args[0], "w");
\r
181 if (config_file != NULL)
\r
182 fclose(config_file);
\r
188 int handle_meminfo_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
190 static int prev = 0;
\r
191 struct mallinfo info;
\r
195 command_print(cmd_ctx, "meminfo");
\r
196 return ERROR_INVALID_ARGUMENTS;
\r
203 command_print(cmd_ctx, "Diff: %d", prev - info.fordblks);
\r
205 prev = info.fordblks;
\r
207 command_print(cmd_ctx, "Available ram: %d", info.fordblks );
\r
213 int handle_append_command(struct command_context_s *cmd_ctx, char *cmd,
\r
214 char **args, int argc)
\r
218 command_print(cmd_ctx,
\r
219 "append <filename> [<string1>, [<string2>, ...]]");
\r
220 return ERROR_INVALID_ARGUMENTS;
\r
223 FILE *config_file = NULL;
\r
224 config_file = fopen(args[0], "a");
\r
225 if (config_file != NULL)
\r
228 fseek(config_file, 0, SEEK_END);
\r
230 for (i = 1; i < argc; i++)
\r
232 fwrite(args[i], strlen(args[i]), 1, config_file);
\r
235 fwrite(" ", 1, 1, config_file);
\r
238 fwrite("\n", 1, 1, config_file);
\r
239 fclose(config_file);
\r
247 int handle_cp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
251 return ERROR_INVALID_ARGUMENTS;
\r
254 // NOTE!!! we only have line printing capability so we print the entire file as a single line.
\r
258 int retval = loadFile(args[0], &data, &len);
\r
259 if (retval != ERROR_OK)
\r
262 FILE *f = fopen(args[1], "wb");
\r
264 retval = ERROR_INVALID_ARGUMENTS;
\r
269 int chunk = len - pos;
\r
270 static const int maxChunk = 512 * 1024; // ~1/sec
\r
271 if (chunk > maxChunk)
\r
276 if ((retval==ERROR_OK)&&(fwrite(((char *)data)+pos, 1, chunk, f)!=chunk))
\r
277 retval = ERROR_INVALID_ARGUMENTS;
\r
279 if (retval != ERROR_OK)
\r
284 command_print(cmd_ctx, "%d", len - pos);
\r
292 if (retval == ERROR_OK)
\r
294 command_print(cmd_ctx, "Copied %s to %s", args[0], args[1]);
\r
297 command_print(cmd_ctx, "Failed: %d", retval);
\r
305 if (retval != ERROR_OK)
\r
314 #define SHOW_RESULT(a, b) LOG_ERROR(#a " failed %d\n", (int)b)
\r
317 void copyfile(char *name2, char *name1)
\r
323 ssize_t done, wrote;
\r
325 fd1 = open(name1, O_WRONLY | O_CREAT);
\r
327 SHOW_RESULT( open, fd1 );
\r
329 fd2 = open(name2, O_RDONLY);
\r
331 SHOW_RESULT( open, fd2 );
\r
335 done = read(fd2, buf, IOSIZE );
\r
338 SHOW_RESULT( read, done );
\r
342 if( done == 0 ) break;
\r
344 wrote = write(fd1, buf, done);
\r
345 if( wrote != done ) SHOW_RESULT( write, wrote );
\r
347 if( wrote != done ) break;
\r
351 if( err < 0 ) SHOW_RESULT( close, err );
\r
354 if( err < 0 ) SHOW_RESULT( close, err );
\r
358 /* utility fn to copy a directory */
\r
359 void copydir(char *name, char *destdir)
\r
364 dirp = opendir(destdir);
\r
367 mkdir(destdir, 0777);
\r
370 err = closedir(dirp);
\r
373 dirp = opendir(name);
\r
374 if( dirp == NULL ) SHOW_RESULT( opendir, -1 );
\r
378 struct dirent *entry = readdir(dirp);
\r
383 if (strcmp(entry->d_name, ".") == 0)
\r
385 if (strcmp(entry->d_name, "..") == 0)
\r
390 char fullPath[PATH_MAX];
\r
391 strncpy(fullPath, name, PATH_MAX);
\r
392 strcat(fullPath, "/");
\r
393 strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
\r
395 if (stat(fullPath, &buf) == -1)
\r
397 LOG_ERROR("unable to read status from %s", fullPath);
\r
400 isDir = S_ISDIR(buf.st_mode) != 0;
\r
405 // diag_printf("<INFO>: entry %14s",entry->d_name);
\r
406 char fullname[PATH_MAX];
\r
407 char fullname2[PATH_MAX];
\r
409 strcpy(fullname, name);
\r
410 strcat(fullname, "/");
\r
411 strcat(fullname, entry->d_name);
\r
413 strcpy(fullname2, destdir);
\r
414 strcat(fullname2, "/");
\r
415 strcat(fullname2, entry->d_name);
\r
416 // diag_printf("from %s to %s\n", fullname, fullname2);
\r
417 copyfile(fullname, fullname2);
\r
419 // diag_printf("\n");
\r
422 err = closedir(dirp);
\r
423 if( err < 0 ) SHOW_RESULT( stat, err );
\r
430 zylinjtag_Jim_Command_rm(Jim_Interp *interp,
\r
432 Jim_Obj * const *argv)
\r
437 Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?");
\r
442 if (unlink(Jim_GetString(argv[1], NULL)) == 0)
\r
444 if (rmdir(Jim_GetString(argv[1], NULL)) == 0)
\r
447 return del ? JIM_OK : JIM_ERR;
\r
452 zylinjtag_Jim_Command_ls(Jim_Interp *interp,
\r
454 Jim_Obj * const *argv)
\r
458 Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
\r
462 char *name = (char*) Jim_GetString(argv[1], NULL);
\r
465 dirp = opendir(name);
\r
470 Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
\r
474 struct dirent *entry = NULL;
\r
475 entry = readdir(dirp);
\r
479 if ((strcmp(".", entry->d_name)==0)||(strcmp("..", entry->d_name)==0))
\r
482 Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
\r
486 Jim_SetResult(interp, objPtr);
\r
491 int handle_peek_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
495 return ERROR_COMMAND_SYNTAX_ERROR;
\r
497 volatile int *address=(volatile int *)strtoul(args[0], NULL, 0);
\r
498 int value=*address;
\r
499 command_print(cmd_ctx, "0x%x : 0x%x", address, value);
\r
503 int handle_poke_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
507 return ERROR_INVALID_ARGUMENTS;
\r
509 volatile int *address=(volatile int *)strtoul(args[0], NULL, 0);
\r
510 int value=strtoul(args[1], NULL, 0);
\r
516 zylinjtag_Jim_Command_peek(Jim_Interp *interp,
\r
518 Jim_Obj * const *argv)
\r
522 Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
\r
527 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
\r
530 int value = *((volatile int *) address);
\r
532 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
\r
538 zylinjtag_Jim_Command_poke(Jim_Interp *interp,
\r
540 Jim_Obj * const *argv)
\r
544 Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
\r
549 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
\r
552 if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
\r
555 *((volatile int *) address) = value;
\r
560 int ioutil_init(struct command_context_s *cmd_ctx)
\r
562 register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY,
\r
565 register_command(cmd_ctx, NULL, "cat", handle_cat_command, COMMAND_ANY,
\r
566 "display file content");
\r
568 register_command(cmd_ctx, NULL, "trunc", handle_trunc_command, COMMAND_ANY,
\r
569 "truncate a file to 0 size");
\r
571 register_command(cmd_ctx, NULL, "cp", handle_cp_command,
\r
572 COMMAND_ANY, "copy a file <from> <to>");
\r
574 register_command(cmd_ctx, NULL, "append_file", handle_append_command,
\r
575 COMMAND_ANY, "append a variable number of strings to a file");
\r
577 register_command(cmd_ctx, NULL, "meminfo", handle_meminfo_command,
\r
578 COMMAND_ANY, "display available ram memory");
\r
580 Jim_CreateCommand(interp, "rm", zylinjtag_Jim_Command_rm, NULL, NULL);
\r
582 Jim_CreateCommand(interp, "peek", zylinjtag_Jim_Command_peek, NULL, NULL);
\r
583 Jim_CreateCommand(interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL);
\r
584 Jim_CreateCommand(interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL);
\r