source: rtems/cpukit/libmisc/shell/cmds.c @ 8e2b4de

4.104.114.84.95
Last change on this file since 8e2b4de was 5fef523, checked in by Joel Sherrill <joel.sherrill@…>, on 05/25/01 at 17:52:03

2001-05-25 Joel Sherrill <joel@…>

  • shell/cmds.c: Removed code from inappropriate source.
  • Property mode set to 100644
File size: 15.4 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 * TODO: A lot of improvements of course.
14 *      cp, mv, ...
15 *      hexdump,
16 *     
17 *      More? Say me it, please...
18 *     
19 *      The BSP Specific are not welcome here.
20 *     
21 * C&S welcome...
22 *
23 *  $Id$
24 */
25
26#include <stdio.h>
27#include <termios.h>
28#include <string.h>
29#include <stdlib.h>
30#include <ctype.h>
31#include <dirent.h>
32#include <time.h>
33#include <fcntl.h>
34#include <unistd.h>
35#include <pwd.h>
36#include <grp.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   struct passwd     * pwd;
261   struct group      * grp;
262   char * user;
263   char * group;
264   char   sbuf[256];
265   char   nbuf[1024];
266   int  n,size;
267
268   fname=".";
269   if (argc>1) fname=argv[1];
270
271   if ((dirp = opendir(fname)) == NULL)
272   {
273      printf("%s: No such file or directory.\n", fname);
274      return errno;
275   }
276   n=0;
277   size=0;
278   while ((dp = readdir(dirp)) != NULL)
279   {
280      strcpy(nbuf,fname);
281      if (nbuf[strlen(nbuf)-1]!='/') strcat(nbuf,"/");
282      strcat(nbuf,dp->d_name); /* always the fullpathname. Avoid ftpd problem.*/
283      if (stat(nbuf, &stat_buf) == 0)
284      { /* AWFUL buts works...*/
285         strftime(sbuf,sizeof(sbuf)-1,"%b %d %H:%M",gmtime(&stat_buf.st_atime));     
286         pwd=getpwuid(stat_buf.st_uid);
287         user=pwd?pwd->pw_name:"nouser";
288         grp=getgrgid(stat_buf.st_gid);
289         group=grp?grp->gr_name:"nogrp";
290         printf("%c%c%c%c%c%c%c%c%c%c %3d %6.6s %6.6s %11d %s %s%c\n",
291                 (S_ISLNK(stat_buf.st_mode)?('l'):
292                    (S_ISDIR(stat_buf.st_mode)?('d'):('-'))),
293                 (stat_buf.st_mode & S_IRUSR)?('r'):('-'),
294                 (stat_buf.st_mode & S_IWUSR)?('w'):('-'),
295                 (stat_buf.st_mode & S_IXUSR)?('x'):('-'),
296                 (stat_buf.st_mode & S_IRGRP)?('r'):('-'),
297                 (stat_buf.st_mode & S_IWGRP)?('w'):('-'),
298                 (stat_buf.st_mode & S_IXGRP)?('x'):('-'),
299                 (stat_buf.st_mode & S_IROTH)?('r'):('-'),
300                 (stat_buf.st_mode & S_IWOTH)?('w'):('-'),
301                 (stat_buf.st_mode & S_IXOTH)?('x'):('-'),
302                 (int)stat_buf.st_nlink,
303                 user,group,
304                 (int)stat_buf.st_size,
305                 sbuf,
306                 dp->d_name,
307                 S_ISDIR(stat_buf.st_mode)?'/':' ');
308         n++;
309         size+=stat_buf.st_size;
310      }
311   }
312   printf("%d files %d bytes occupied\n",n,size);
313   closedir(dirp);
314   return 0;
315}
316/*-----------------------------------------------------------*/         
317int main_pwd (int argc, char *argv[]) {
318   char dir[1024];
319   getcwd(dir,1024);
320   printf("%s\n",dir);
321   return 0;
322}
323/*-----------------------------------------------------------*/         
324int main_chdir (int argc, char *argv[]) {
325   char *dir;
326   dir="/";
327   if (argc>1) dir=argv[1];
328   if (chdir(dir)) {
329    printf("chdir to '%s' failed:%s\n",dir,strerror(errno));
330    return errno;
331   };
332   return 0;
333}
334/*-----------------------------------------------------------*/         
335int main_mkdir (int argc, char *argv[]) {
336   char *dir;
337   dir=NULL;
338   if (argc>1) dir=argv[1];
339   if (mkdir(dir,S_IRWXU|S_IRWXG|S_IRWXO)) {
340     printf("mkdir '%s' failed:%s\n",dir,strerror(errno));
341   }; 
342   return errno;
343}
344/*-----------------------------------------------------------*/         
345int main_rmdir (int argc, char *argv[])
346{
347   char *dir;
348   dir=NULL;
349   if (argc>1) dir=argv[1];
350   if (rmdir(dir)) printf("rmdir '%s' failed:%s\n",dir,strerror(errno));
351   return errno;
352}
353/*-----------------------------------------------------------*/         
354int main_chroot(int argc,char * argv[]) {
355 char * new_root="/";
356 if (argc==2) new_root=argv[1];
357 if (chroot(new_root)<0) {
358  printf("error %s:chroot(%s);\n",strerror(errno),new_root);
359  return -1;
360 };
361 return 0;
362}
363/*-----------------------------------------------------------*/         
364int main_cat   (int argc, char *argv[])
365{
366   int n;
367   n=1;
368   while (n<argc) cat_file(stdout,argv[n++]);
369   return 0;
370}
371/*-----------------------------------------------------------*/         
372int main_rm    (int argc, char *argv[])
373{
374   int n;
375   n=1;
376   while (n<argc) {
377    if (unlink(argv[n])) {
378     printf("error %s:rm %s\n",strerror(errno),argv[n]);
379     return -1;
380    };
381    n++;
382   };
383   return 0;
384}
385/*-----------------------------------------------------------*/         
386/* date - print or set time and date */
387
388static int set_time(char *t)
389{
390  rtems_time_of_day tod;       
391  FILE * rtc;
392  int len;
393
394  if ( rtems_clock_get(RTEMS_CLOCK_GET_TOD,&tod) != RTEMS_SUCCESSFUL )
395     memset( &tod, 0, sizeof(tod) );
396
397  /* JRS
398   *
399   *  This code that was used to parse the command line was taken
400   *  from an inappropriate source.  It has been removed and needs
401   *  to be replaced.
402   */
403
404  if (!_TOD_Validate(&tod)) {
405    fprintf(stderr, "Invalid date value\n");
406  } else {
407   rtems_clock_set(&tod);         
408   rtems_clock_get(RTEMS_CLOCK_GET_TOD,&tod);     
409   rtc=fopen("/dev/rtc","r+");
410   if (rtc) {
411    fwrite(&tod,sizeof(tod),1,rtc);
412    fclose(rtc);
413   };
414  };
415  return 1;
416}
417
418int main_date(int argc,char *argv[])
419{
420  time_t t;
421  if (argc == 2)
422    set_time(argv[1]);
423  time(&t);
424  printf("%s", ctime(&t));
425  return 0;
426}
427/*-----------------------------------------------------------*/
428int main_logoff(int argc,char *argv[])
429{
430  printf("logoff from the system..."); 
431  current_shell_env->exit_shell=TRUE;   
432  return 0;
433}
434/*-----------------------------------------------------------*/
435int main_tty   (int argc,char *argv[])
436{
437  printf("%s\n",ttyname(fileno(stdin)));
438  return 0;
439}
440/*-----------------------------------------------------------*/
441int main_whoami(int argc,char *argv[])
442{
443   struct passwd     * pwd;
444   pwd=getpwuid(getuid());
445   printf("%s\n",pwd?pwd->pw_name:"nobody");
446   return 0;
447}
448/*-----------------------------------------------------------*/
449int main_id    (int argc,char *argv[])
450{
451   struct passwd     * pwd;
452   struct group      * grp;
453   pwd=getpwuid(getuid());
454   grp=getgrgid(getgid());
455   printf("uid=%d(%s),gid=%d(%s),",
456                   getuid(),pwd?pwd->pw_name:"",
457                   getgid(),grp?grp->gr_name:"");
458   pwd=getpwuid(geteuid());
459   grp=getgrgid(getegid());
460   printf("euid=%d(%s),egid=%d(%s)\n",
461                   geteuid(),pwd?pwd->pw_name:"",
462                   getegid(),grp?grp->gr_name:"");
463   return 0;
464}
465/*-----------------------------------------------------------*/
466int main_umask(int argc,char *argv[])
467{
468   mode_t msk=umask(0);
469   if (argc == 2) msk=str2int(argv[1]);
470   umask(msk);
471   msk=umask(0);
472   printf("0%o\n",msk);
473   umask(msk);
474   return 0;
475}
476/*-----------------------------------------------------------*/
477int main_chmod(int argc,char *argv[])
478{
479   int n;
480   mode_t mode;
481   if (argc > 2){
482    mode=str2int(argv[1])&0777;
483    n=2;
484    while (n<argc) chmod(argv[n++],mode);
485   };
486   return 0;
487}
488/*-----------------------------------------------------------*         
489 * with this you can call at all the rtems monitor commands.
490 * Not all work fine but you can show the rtems status and more.
491 *-----------------------------------------------------------*/         
492int main_monitor(int argc,char * argv[]) {
493 rtems_monitor_command_entry_t *command;
494 extern rtems_monitor_command_entry_t rtems_monitor_commands[];
495 rtems_task_ident(RTEMS_SELF,0,&rtems_monitor_task_id);
496 rtems_monitor_node = rtems_get_node(rtems_monitor_task_id);
497 rtems_monitor_default_node = rtems_monitor_node;
498 if ((command=rtems_monitor_command_lookup(rtems_monitor_commands,argc,argv)))
499  command->command_function(argc, argv, command->command_arg, 0);
500 return 0;
501}
502/*-----------------------------------------------------------*/         
503void register_cmds(void) {
504  rtems_monitor_command_entry_t *command;
505  extern rtems_monitor_command_entry_t rtems_monitor_commands[];
506  /* monitor topic */
507  command=rtems_monitor_commands;
508  while (command) {
509   if (strcmp("exit",command->command)) /*Exclude EXIT (alias quit)*/
510    shell_add_cmd(command->command,"monitor",
511                  command->usage  ,main_monitor);
512   command=command->next;
513  };
514  /* dir[ectories] topic */
515  shell_add_cmd  ("ls"    ,"dir","ls [dir]     # list files in the directory" ,main_ls   );
516  shell_add_cmd  ("chdir" ,"dir","chdir [dir]  # change the current directory",main_chdir);
517  shell_add_cmd  ("rmdir" ,"dir","rmdir  dir   # remove directory"            ,main_rmdir);
518  shell_add_cmd  ("mkdir" ,"dir","mkdir  dir   # make a directory"            ,main_mkdir);
519  shell_add_cmd  ("pwd"   ,"dir","pwd          # print work directory"        ,main_pwd  );
520  shell_add_cmd  ("chroot","dir","chroot [dir] # change the root directory"   ,main_chroot);
521  shell_add_cmd  ("cat"   ,"dir","cat n1 [n2 [n3...]]# show the ascii contents",main_cat );
522  shell_add_cmd  ("rm"    ,"dir","rm n1 [n2 [n3...]]# remove files"           ,main_rm   );
523  shell_add_cmd  ("chmod" ,"dir","chmod 0777 n1 n2... #change filemode"       ,main_chmod);
524
525  shell_alias_cmd("ls"    ,"dir");
526  shell_alias_cmd("chdir" ,"cd");
527
528  /* misc. topic */
529  shell_add_cmd  ("logoff","misc","logoff from the system"                    ,main_logoff);
530  shell_alias_cmd("logoff","exit");
531  shell_add_cmd  ("date" ,"misc","date [[MMDDYY]hhmm[ss]]"                    ,main_date);
532  shell_add_cmd  ("reset","misc","reset the BSP"                              ,main_reset);
533  shell_add_cmd  ("alias","misc","alias old new"                              ,main_alias);
534  shell_add_cmd  ("tty"  ,"misc","show ttyname"                               ,main_tty  );
535  shell_add_cmd  ("whoami","misc","show current user"                         ,main_whoami);
536  shell_add_cmd  ("id"    ,"misc","show uid,gid,euid,egid"                    ,main_id    );
537  shell_add_cmd  ("umask" ,"misc","umask [new_umask]"                         ,main_umask );
538
539
540  /* memory topic */
541  shell_add_cmd  ("mdump","mem"  ,"mdump [adr [size]]"           ,main_mdump);
542  shell_add_cmd  ("wdump","mem"  ,"wdump [adr [size]]"           ,main_mwdump);
543  shell_add_cmd  ("medit","mem"  ,"medit adr value [value ...]"  ,main_medit);
544  shell_add_cmd  ("mfill","mem"  ,"mfill adr size value"         ,main_mfill);
545  shell_add_cmd  ("mmove","mem"  ,"mmove dst src size"           ,main_mmove);
546#ifdef MALLOC_STATS  /* /rtems/s/src/lib/libc/malloc.c */
547  shell_add_cmd  ("malloc","mem","mem  show memory malloc'ed"                 ,main_mem);
548#endif 
549}
550/*-----------------------------------------------------------*/         
Note: See TracBrowser for help on using the repository browser.