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

4.104.114.84.95
Last change on this file since aea9bb46 was 70d689a, checked in by Joel Sherrill <joel.sherrill@…>, on 08/09/01 at 22:08:46

2001-08-09 Fernando-Ruiz Casas <correo@…>

  • shell/Makefile.am, shell/README, shell/cmds.c, shell/shell.c, shell/shell.h: Updates.
  • Property mode set to 100644
File size: 14.7 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   int n;
338   n=1;
339   while (n<argc) {
340    dir=argv[n++];
341    if (mkdir(dir,S_IRWXU|S_IRWXG|S_IRWXO)) {
342      printf("mkdir '%s' failed:%s\n",dir,strerror(errno));
343    }; 
344   }; 
345   return errno;
346}
347/*-----------------------------------------------------------*/         
348int main_rmdir (int argc, char *argv[])
349{
350   char *dir;
351   int n;
352   n=1;
353   while (n<argc) {
354    dir=argv[n++];
355    if (rmdir(dir)) printf("rmdir '%s' failed:%s\n",dir,strerror(errno));
356   };
357   return errno;
358}
359/*-----------------------------------------------------------*/         
360int main_chroot(int argc,char * argv[]) {
361 char * new_root="/";
362 if (argc==2) new_root=argv[1];
363 if (chroot(new_root)<0) {
364  printf("error %s:chroot(%s);\n",strerror(errno),new_root);
365  return -1;
366 };
367 return 0;
368}
369/*-----------------------------------------------------------*/         
370int main_cat   (int argc, char *argv[])
371{
372   int n;
373   n=1;
374   while (n<argc) cat_file(stdout,argv[n++]);
375   return 0;
376}
377/*-----------------------------------------------------------*/         
378int main_rm    (int argc, char *argv[])
379{
380   int n;
381   n=1;
382   while (n<argc) {
383    if (unlink(argv[n])) {
384     printf("error %s:rm %s\n",strerror(errno),argv[n]);
385     return -1;
386    };
387    n++;
388   };
389   return 0;
390}
391/*-----------------------------------------------------------*/         
392/* date - print time and date */
393
394int main_date(int argc,char *argv[])
395{
396  time_t t;
397  time(&t);
398  printf("%s", ctime(&t));
399  return 0;
400}
401/*-----------------------------------------------------------*/
402int main_logoff(int argc,char *argv[])
403{
404  printf("logoff from the system..."); 
405  current_shell_env->exit_shell=TRUE;   
406  return 0;
407}
408/*-----------------------------------------------------------*/
409int main_tty   (int argc,char *argv[])
410{
411  printf("%s\n",ttyname(fileno(stdin)));
412  return 0;
413}
414/*-----------------------------------------------------------*/
415int main_whoami(int argc,char *argv[])
416{
417   struct passwd     * pwd;
418   pwd=getpwuid(getuid());
419   printf("%s\n",pwd?pwd->pw_name:"nobody");
420   return 0;
421}
422/*-----------------------------------------------------------*/
423int main_id    (int argc,char *argv[])
424{
425   struct passwd     * pwd;
426   struct group      * grp;
427   pwd=getpwuid(getuid());
428   grp=getgrgid(getgid());
429   printf("uid=%d(%s),gid=%d(%s),",
430                   getuid(),pwd?pwd->pw_name:"",
431                   getgid(),grp?grp->gr_name:"");
432   pwd=getpwuid(geteuid());
433   grp=getgrgid(getegid());
434   printf("euid=%d(%s),egid=%d(%s)\n",
435                   geteuid(),pwd?pwd->pw_name:"",
436                   getegid(),grp?grp->gr_name:"");
437   return 0;
438}
439/*-----------------------------------------------------------*/
440int main_umask(int argc,char *argv[])
441{
442   mode_t msk=umask(0);
443   if (argc == 2) msk=str2int(argv[1]);
444   umask(msk);
445   msk=umask(0);
446   printf("0%o\n",msk);
447   umask(msk);
448   return 0;
449}
450/*-----------------------------------------------------------*/
451int main_chmod(int argc,char *argv[])
452{
453   int n;
454   mode_t mode;
455   if (argc > 2){
456    mode=str2int(argv[1])&0777;
457    n=2;
458    while (n<argc) chmod(argv[n++],mode);
459   };
460   return 0;
461}
462/*-----------------------------------------------------------*         
463 * with this you can call at all the rtems monitor commands.
464 * Not all work fine but you can show the rtems status and more.
465 *-----------------------------------------------------------*/         
466int main_monitor(int argc,char * argv[]) {
467 rtems_monitor_command_entry_t *command;
468 extern rtems_monitor_command_entry_t rtems_monitor_commands[];
469 rtems_task_ident(RTEMS_SELF,0,&rtems_monitor_task_id);
470 rtems_monitor_node = rtems_get_node(rtems_monitor_task_id);
471 rtems_monitor_default_node = rtems_monitor_node;
472 if ((command=rtems_monitor_command_lookup(rtems_monitor_commands,argc,argv)))
473  command->command_function(argc, argv, command->command_arg, 0);
474 return 0;
475}
476/*-----------------------------------------------------------*/         
477void register_cmds(void) {
478  rtems_monitor_command_entry_t *command;
479  extern rtems_monitor_command_entry_t rtems_monitor_commands[];
480  /* monitor topic */
481  command=rtems_monitor_commands;
482  while (command) {
483   if (strcmp("exit",command->command)) /*Exclude EXIT (alias quit)*/
484    shell_add_cmd(command->command,"monitor",
485                  command->usage  ,main_monitor);
486   command=command->next;
487  };
488  /* dir[ectories] topic */
489  shell_add_cmd  ("ls"    ,"dir","ls [dir]     # list files in the directory" ,main_ls   );
490  shell_add_cmd  ("chdir" ,"dir","chdir [dir]  # change the current directory",main_chdir);
491  shell_add_cmd  ("rmdir" ,"dir","rmdir  dir   # remove directory"            ,main_rmdir);
492  shell_add_cmd  ("mkdir" ,"dir","mkdir  dir   # make a directory"            ,main_mkdir);
493  shell_add_cmd  ("pwd"   ,"dir","pwd          # print work directory"        ,main_pwd  );
494  shell_add_cmd  ("chroot","dir","chroot [dir] # change the root directory"   ,main_chroot);
495  shell_add_cmd  ("cat"   ,"dir","cat n1 [n2 [n3...]]# show the ascii contents",main_cat );
496  shell_add_cmd  ("rm"    ,"dir","rm n1 [n2 [n3...]]# remove files"           ,main_rm   );
497  shell_add_cmd  ("chmod" ,"dir","chmod 0777 n1 n2... #change filemode"       ,main_chmod);
498
499  shell_alias_cmd("ls"    ,"dir");
500  shell_alias_cmd("chdir" ,"cd");
501
502  /* misc. topic */
503  shell_add_cmd  ("logoff","misc","logoff from the system"                    ,main_logoff);
504  shell_alias_cmd("logoff","exit");
505  shell_add_cmd  ("date" ,"misc","date"                                       ,main_date);
506  shell_add_cmd  ("reset","misc","reset the BSP"                              ,main_reset);
507  shell_add_cmd  ("alias","misc","alias old new"                              ,main_alias);
508  shell_add_cmd  ("tty"  ,"misc","show ttyname"                               ,main_tty  );
509  shell_add_cmd  ("whoami","misc","show current user"                         ,main_whoami);
510  shell_add_cmd  ("id"    ,"misc","show uid,gid,euid,egid"                    ,main_id    );
511  shell_add_cmd  ("umask" ,"misc","umask [new_umask]"                         ,main_umask );
512
513
514  /* memory topic */
515  shell_add_cmd  ("mdump","mem"  ,"mdump [adr [size]]"           ,main_mdump);
516  shell_add_cmd  ("wdump","mem"  ,"wdump [adr [size]]"           ,main_mwdump);
517  shell_add_cmd  ("medit","mem"  ,"medit adr value [value ...]"  ,main_medit);
518  shell_add_cmd  ("mfill","mem"  ,"mfill adr size value"         ,main_mfill);
519  shell_add_cmd  ("mmove","mem"  ,"mmove dst src size"           ,main_mmove);
520#ifdef MALLOC_STATS  /* /rtems/s/src/lib/libc/malloc.c */
521  shell_add_cmd  ("malloc","mem","mem  show memory malloc'ed"                 ,main_mem);
522#endif 
523}
524/*-----------------------------------------------------------*/         
Note: See TracBrowser for help on using the repository browser.