source: rtems/cpukit/libmisc/shell/cmds.c @ ab94137

4.104.114.84.95
Last change on this file since ab94137 was d72caa6, checked in by Joel Sherrill <joel.sherrill@…>, on 05/07/01 at 13:17:25

2001-04-28 Ralf Corsepius <corsepiu@…>

  • shell/cmds.c, shell/shell.c: Remove fileno-hacks.
  • monitor/mon-symbols.c: Remove #undef STRICT_ANSI.
  • Property mode set to 100644
File size: 13.2 KB
Line 
1/*
2 * Author: Fernando RUIZ CASAS
3 *
4 *  Work: fernando.ruiz@ctv.es
5 *  Home: correo@fernando-ruiz.com
6 *
7 * This file is inspired in rtems_monitor & Chris John MyRightBoot
8 *
9 * But I want to make it more user friendly
10 * A 'monitor' command is added to adapt the call rtems monitor commands
11 * at my call procedure
12 *
13 * MINIX date.c is adapted to run here. Like a exercise only....
14 *
15 * TODO: A lot of improvements of course.
16 *      cat, cp, rm, mv, ...
17 *      hexdump,
18 *     
19 *      More? Say me it, please...
20 *     
21 *      The BSP Specific are not welcome here.
22 *     
23 * C&S welcome...
24 *
25 *  $Id$
26 */
27
28#include <stdio.h>
29#include <termios.h>
30#include <string.h>
31#include <stdlib.h>
32#include <ctype.h>
33#include <dirent.h>
34#include <time.h>
35#include <fcntl.h>
36#include <unistd.h>
37#include <errno.h>
38#include <sys/types.h>
39#include <stddef.h>
40
41#include <rtems.h>
42#include <rtems/monitor.h>
43#include <rtems/score/tod.h>
44
45#include <imfs.h>
46#include <rtems/shell.h>
47
48/* ----------------------------------------------- *
49  - str to int "0xaffe" "0b010010" "0123" "192939"
50 * ----------------------------------------------- */
51int str2int(char * s) {
52 int sign=1;   
53 int base=10;
54 int value=0;
55 int digit;
56 if (!s) return 0;
57 if (*s) {
58  if (*s=='-') {
59   sign=-1;
60   s++;
61   if (!*s) return 0;
62  };
63  if (*s=='0') {
64   s++;
65   switch(*s) {
66    case 'x':
67    case 'X':s++;
68             base=16;
69             break;
70    case 'b':
71    case 'B':s++;
72             base=2;
73             break;
74    default :base=8;
75             break;
76   }
77  };
78  while (*s) {
79   switch(*s) {
80    case '0':
81    case '1':
82    case '2':
83    case '3':
84    case '4':
85    case '5':
86    case '6':
87    case '7':
88    case '8':
89    case '9':digit=*s-'0';
90             break;
91    case 'A':     
92    case 'B':     
93    case 'C':     
94    case 'D':     
95    case 'E':     
96    case 'F':digit=*s-'A'+10;
97             break;
98    case 'a':     
99    case 'b':     
100    case 'c':     
101    case 'd':     
102    case 'e':     
103    case 'f':digit=*s-'a'+10;
104             break;
105    default:return value*sign;       
106   };
107   if (digit>base) return value*sign;
108   value=value*base+digit;
109   s++;
110  };
111 };
112 return value*sign;     
113}
114/*----------------------------------------------------------------------------*
115 * RAM MEMORY COMMANDS
116 *----------------------------------------------------------------------------*/
117
118#define mdump_adr (current_shell_env->mdump_adr)  /* static value */
119
120int main_mdump(int argc,char * argv[]) {
121 unsigned char n,m,max=0;
122 int adr=mdump_adr;
123 unsigned char * pb;
124 if (argc>1) adr=str2int(argv[1]);
125 if (argc>2) max=str2int(argv[2]);
126 max/=16;
127 if (!max) max=20;
128 for (m=0;m<max;m++) {
129  printf("0x%08X ",adr);
130  pb=(unsigned char*) adr;
131  for (n=0;n<16;n++)
132   printf("%02X%c",pb[n],n==7?'-':' ');
133  for (n=0;n<16;n++) {
134   printf("%c",isprint(pb[n])?pb[n]:'.');
135  };
136  printf("\n");
137  adr+=16;
138 };
139 mdump_adr=adr;
140 return 0;
141}
142/*----------------------------------------------------------------------------*/
143int main_mwdump(int argc,char * argv[]) {
144 unsigned char n,m,max=0;
145 int adr=mdump_adr;
146 unsigned short * pw;
147 if (argc>1) adr=str2int(argv[1]);
148 if (argc>2) max=str2int(argv[2]);
149 max/=16;
150 if (!max) max=20;
151 for (m=0;m<max;m++) {
152  printf("0x%08X ",adr);
153  pw=(unsigned short*) adr;
154  for (n=0;n<8;n++)
155   printf("%02X %02X%c",pw[n]/0x100,pw[n]%0x100,n==3?'-':' ');
156  for (n=0;n<8;n++) {
157   printf("%c",isprint(pw[n]/0x100)?pw[n]/0x100:'.');
158   printf("%c",isprint(pw[n]%0x100)?pw[n]%0x100:'.');
159  };
160  printf("\n");
161  adr+=16;
162 };
163 mdump_adr=adr;
164 return 0;
165}
166/*----------------------------------------------------------------------------*/
167int main_medit(int argc,char * argv[]) {
168 unsigned char * pb;
169 int n,i;
170 if (argc<3) {
171  printf("too few arguments\n");
172  return 0;
173 };
174 pb=(unsigned char*)str2int(argv[1]);
175 i=2;
176 n=0;
177 while (i<=argc) {
178  pb[n++]=str2int(argv[i++])%0x100;
179 }
180 mdump_adr=(int)pb;
181 return main_mdump(0,NULL);
182}
183/*----------------------------------------------------------------------------*/
184int main_mfill(int argc,char * argv[]) {
185 int  adr;
186 int  size;
187 unsigned char value;
188 if (argc<4) {
189  printf("too few arguments\n");
190  return 0;
191 };
192 adr  =str2int(argv[1]);
193 size =str2int(argv[2]);
194 value=str2int(argv[3])%0x100;
195 memset((unsigned char*)adr,size,value);
196 mdump_adr=adr;
197 return main_mdump(0,NULL);
198}
199/*----------------------------------------------------------------------------*/
200int main_mmove(int argc,char * argv[]) {
201 int  src;
202 int  dst;
203 int  size;
204 if (argc<4) {
205  printf("too few arguments\n");
206  return 0;
207 };
208 dst  =str2int(argv[1]);
209 src  =str2int(argv[2]);
210 size =str2int(argv[3]);
211 memcpy((unsigned char*)dst,(unsigned char*)src,size);
212 mdump_adr=dst;
213 return main_mdump(0,NULL);
214}
215/*----------------------------------------------------------------------------*/
216#ifdef MALLOC_STATS  /* /rtems/s/src/lib/libc/malloc.c */
217int main_malloc_dump(int argc,char * argv[]) {
218 void malloc_dump(void);
219 malloc_dump();
220 return 0;
221}
222#endif
223/*----------------------------------------------------------------------------
224 * Reset. Assumes that the watchdog is present.
225 *----------------------------------------------------------------------------*/
226int main_reset (int argc, char **argv)
227{
228  rtems_interrupt_level level;
229  printf ("Waiting for watchdog ... ");
230  tcdrain(fileno(stdout));
231
232  rtems_interrupt_disable (level);
233  for (;;)
234      ;
235  return 0;
236}
237/*----------------------------------------------------------------------------
238 * Alias. make an alias
239 *----------------------------------------------------------------------------*/
240int main_alias (int argc, char **argv)
241{
242 if (argc<3) {
243  printf("too few arguments\n");
244  return 1;
245 };
246 if (!shell_alias_cmd(argv[1],argv[2])) {
247  printf("unable to make an alias(%s,%s)\n",argv[1],argv[2]);
248 };
249 return 0;
250
251/*-----------------------------------------------------------*         
252 * Directory commands
253 *-----------------------------------------------------------*/
254int main_ls(int argc, char *argv[])
255{
256   char * fname;
257   DIR                 *dirp;
258   struct dirent       *dp;
259   struct stat         stat_buf;
260   char   sbuf[256];
261   char   nbuf[1024];
262   int  n,size;
263
264   fname=".";
265   if (argc>1) fname=argv[1];
266
267   if ((dirp = opendir(fname)) == NULL)
268   {
269      printf("%s: No such file or directory.\n", fname);
270      return errno;
271   }
272   n=0;
273   size=0;
274   while ((dp = readdir(dirp)) != NULL)
275   {
276      strcpy(nbuf,fname);
277      if (nbuf[strlen(nbuf)-1]!='/') strcat(nbuf,"/");
278      strcat(nbuf,dp->d_name); /* always the fullpathname. Avoid ftpd problem.*/
279      if (stat(nbuf, &stat_buf) == 0)
280      { /* AWFUL buts works...*/
281         strftime(sbuf,sizeof(sbuf)-1,"%b %d %H:%M",gmtime(&stat_buf.st_atime));     
282         printf("%c%c%c%c%c%c%c%c%c%c %3d rtems  rtems  %11d %s %s%c\n",
283                 (S_ISLNK(stat_buf.st_mode)?('l'):
284                    (S_ISDIR(stat_buf.st_mode)?('d'):('-'))),
285                 (stat_buf.st_mode & S_IRUSR)?('r'):('-'),
286                 (stat_buf.st_mode & S_IWUSR)?('w'):('-'),
287                 (stat_buf.st_mode & S_IXUSR)?('x'):('-'),
288                 (stat_buf.st_mode & S_IRGRP)?('r'):('-'),
289                 (stat_buf.st_mode & S_IWGRP)?('w'):('-'),
290                 (stat_buf.st_mode & S_IXGRP)?('x'):('-'),
291                 (stat_buf.st_mode & S_IROTH)?('r'):('-'),
292                 (stat_buf.st_mode & S_IWOTH)?('w'):('-'),
293                 (stat_buf.st_mode & S_IXOTH)?('x'):('-'),
294                 (int)stat_buf.st_nlink,
295                 (int)stat_buf.st_size,
296                 sbuf,
297                 dp->d_name,
298                 S_ISDIR(stat_buf.st_mode)?'/':' ');
299         n++;
300         size+=stat_buf.st_size;
301      }
302   }
303   printf("%d files %d bytes occupied\n",n,size);
304   closedir(dirp);
305   return 0;
306}
307/*-----------------------------------------------------------*/         
308int main_pwd (int argc, char *argv[]) {
309   char dir[1024];
310   getcwd(dir,1024);
311   printf("%s\n",dir);
312   return 0;
313}
314/*-----------------------------------------------------------*/         
315int main_chdir (int argc, char *argv[]) {
316   char *dir;
317   dir="/";
318   if (argc>1) dir=argv[1];
319   if (chdir(dir)) {
320    printf("chdir to '%s' failed:%s\n",dir,strerror(errno));
321    return errno;
322   };
323   return 0;
324}
325/*-----------------------------------------------------------*/         
326int main_mkdir (int argc, char *argv[]) {
327   char *dir;
328   dir=NULL;
329   if (argc>1) dir=argv[1];
330   if (mkdir(dir,S_IRWXU|S_IRWXG|S_IRWXO)) {
331     printf("mkdir '%s' failed:%s\n",dir,strerror(errno));
332   }; 
333   return errno;
334}
335/*-----------------------------------------------------------*/         
336int main_rmdir (int argc, char *argv[])
337{
338   char *dir;
339   dir=NULL;
340   if (argc>1) dir=argv[1];
341   if (rmdir(dir)) printf("rmdir '%s' failed:%s\n",dir,strerror(errno));
342   return errno;
343}
344/*-----------------------------------------------------------*/         
345int main_chroot(int argc,char * argv[]) {
346 char * new_root="/";
347 if (argc==2) new_root=argv[1];
348 if (chroot(new_root)<0) {
349  printf("error %s:chroot(%s);\n",strerror(errno),new_root);
350  return -1;
351 };
352 return 0;
353}
354/*-----------------------------------------------------------*/         
355/* date - print or set time and date            Author: Jan Looyen */
356/* MINIX 1.5 GPL'ed */
357
358
359#define MIN     60L             /* # seconds in a minute */
360#define HOUR    (60 * MIN)      /* # seconds in an hour */
361#define DAY     (24 * HOUR)     /* # seconds in a day */
362#define YEAR    (365 * DAY)     /* # seconds in a year */
363
364static int conv(unsigned32 *value,char **ptr,unsigned32 max)
365{
366  int buf;
367  *ptr -= 2;
368  buf = atoi(*ptr);
369  **ptr = 0;
370  if (buf < 0 || buf > max) {
371   fprintf(stderr, "Date: bad conversion\n");
372   return 0;
373  };
374  *value=buf;
375  return 1;
376}
377
378static int set_time(char *t)
379{
380  rtems_time_of_day tod;       
381  FILE * rtc;
382  char *tp;
383  int len;
384  if (rtems_clock_get(RTEMS_CLOCK_GET_TOD,&tod)!=RTEMS_SUCCESSFUL)
385          memset(&tod,0,sizeof(tod));
386  len = strlen(t);
387  if (len != 12 && len != 10 && len != 6 && len != 4) return 0;
388  tp = t;
389  while (*tp)
390   if (!isdigit(*tp++)) {
391    fprintf(stderr, "date: bad conversion\n");
392    return 0;
393   };
394  if (len == 6 || len == 12)
395   if (!conv(&tod.second,&tp, 59)) return 0;
396  if (!conv(&tod.minute,&tp, 59)) return 0;
397  if (!conv(&tod.hour,&tp, 23)) return 0;
398  if (len == 12 || len == 10) {
399   if (!conv(&tod.year,&tp, 99)) return 0;
400   tod.year+=1900;
401   if (tod.year<TOD_BASE_YEAR) tod.year+=100;
402   if (!conv(&tod.day   ,&tp, 31)) return 0;
403   if (!conv(&tod.month ,&tp, 12)) return 0;
404  }
405  if (!_TOD_Validate(&tod)) {
406    fprintf(stderr, "Invalid date value\n");
407  } else {
408   rtems_clock_set(&tod);         
409   rtems_clock_get(RTEMS_CLOCK_GET_TOD,&tod);     
410   rtc=fopen("/dev/rtc","r+");
411   if (rtc) {
412    fwrite(&tod,sizeof(tod),1,rtc);
413    fclose(rtc);
414   };
415  };
416  return 1;
417}
418
419int main_date(int argc,char *argv[])
420{
421  time_t t;
422  if (argc == 2) set_time(argv[1]);
423  time(&t);
424  printf("%s", ctime(&t));
425  return 0;
426}
427/*-----------------------------------------------------------*         
428 * with this you can call at all the rtems monitor commands.
429 * Not all work fine but you can show the rtems status and more.
430 *-----------------------------------------------------------*/         
431int main_monitor(int argc,char * argv[]) {
432 rtems_monitor_command_entry_t *command;
433 extern rtems_monitor_command_entry_t rtems_monitor_commands[];
434 rtems_task_ident(RTEMS_SELF,0,&rtems_monitor_task_id);
435 rtems_monitor_node = rtems_get_node(rtems_monitor_task_id);
436 rtems_monitor_default_node = rtems_monitor_node;
437 if ((command=rtems_monitor_command_lookup(rtems_monitor_commands,argc,argv)))
438  command->command_function(argc, argv, command->command_arg, 0);
439 return 0;
440}
441/*-----------------------------------------------------------*/         
442void register_cmds(void) {
443  rtems_monitor_command_entry_t *command;
444  extern rtems_monitor_command_entry_t rtems_monitor_commands[];
445  /* dir[ectories] topic */
446  shell_add_cmd  ("ls"    ,"dir","ls [dir]     # list files in the directory" ,main_ls   );
447  shell_add_cmd  ("chdir" ,"dir","chdir [dir]  # change the current directory",main_chdir);
448  shell_add_cmd  ("rmdir" ,"dir","rmdir  dir   # remove directory"            ,main_rmdir);
449  shell_add_cmd  ("mkdir" ,"dir","mkdir  dir   # make a directory"            ,main_mkdir);
450  shell_add_cmd  ("pwd"   ,"dir","pwd          # print work directory"        ,main_pwd  );
451  shell_add_cmd  ("chroot","dir","chroot [dir] # change the root directory"   ,main_chroot);
452
453  shell_alias_cmd("ls"    ,"dir");
454  shell_alias_cmd("chdir" ,"cd");
455
456  /* misc. topic */
457  shell_add_cmd  ("date" ,"misc","date [[MMDDYY]hhmm[ss]]"                    ,main_date);
458  shell_add_cmd  ("reset","misc","reset the BSP"                              ,main_reset);
459  shell_add_cmd  ("alias","misc","alias old new"                              ,main_alias);
460
461  /* memory topic */
462  shell_add_cmd  ("mdump","mem"  ,"mdump [adr [size]]"           ,main_mdump);
463  shell_add_cmd  ("wdump","mem"  ,"wdump [adr [size]]"           ,main_mwdump);
464  shell_add_cmd  ("medit","mem"  ,"medit adr value [value ...]"  ,main_medit);
465  shell_add_cmd  ("mfill","mem"  ,"mfill adr size value"         ,main_mfill);
466  shell_add_cmd  ("mmove","mem"  ,"mmove dst src size"           ,main_mmove);
467#ifdef MALLOC_STATS  /* /rtems/s/src/lib/libc/malloc.c */
468  shell_add_cmd  ("malloc","mem","mem  show memory malloc'ed"                 ,main_mem);
469#endif 
470  /* monitor topic */
471  command=rtems_monitor_commands;
472  while (command) {
473   shell_add_cmd(command->command,"monitor",
474                 command->usage  ,main_monitor);
475   command=command->next;
476  };
477}
478/*-----------------------------------------------------------*/         
Note: See TracBrowser for help on using the repository browser.