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

4.104.114.84.95
Last change on this file since b2b143f4 was b2b143f4, checked in by Joel Sherrill <joel.sherrill@…>, on 03/05/04 at 17:58:51

2004-03-05 Joel Sherrill <joel@…>

  • libblock/src/bdbuf.c, libblock/src/ramdisk.c, libcsupport/src/newlibc.c, libcsupport/src/sync.c, libmisc/cpuuse/cpuuse.c, libmisc/monitor/mon-symbols.c, libmisc/shell/cmds.c, libmisc/shell/shell.c, libnetworking/kern/kern_sysctl.c, libnetworking/lib/ftpfs.c, libnetworking/lib/tftpDriver.c, libnetworking/libc/gethostbydns.c, libnetworking/libc/gethostbyht.c, libnetworking/libc/gethostnamadr.c, libnetworking/libc/getnetbyht.c, libnetworking/libc/getnetnamadr.c, libnetworking/libc/inet_addr.c, libnetworking/libc/linkaddr.c, libnetworking/libc/map_v4v6.c, libnetworking/libc/ns_print.c, libnetworking/libc/ns_ttl.c, libnetworking/libc/nsap_addr.c, libnetworking/libc/rcmd.c, libnetworking/libc/res_debug.c, libnetworking/libc/res_mkupdate.c, libnetworking/libc/res_query.c, libnetworking/libc/res_send.c, libnetworking/libc/res_update.c, libnetworking/net/radix.c, libnetworking/rtems/mkrootfs.c, librpc/src/rpc/clnt_perror.c, librpc/src/rpc/rtems_rpc.c, librpc/src/rpc/svc.c, sapi/include/confdefs.h, score/macros/rtems/score/chain.inl, score/src/objectidtoname.c:
  • Property mode set to 100644
File size: 14.8 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#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30/* Since we compile with strict ANSI we need to undef it to get
31 * prototypes for extensions
32 */
33#undef __STRICT_ANSI__
34
35#include <stdio.h>
36#include <termios.h>
37#include <string.h>
38#include <stdlib.h>
39#include <ctype.h>
40#include <dirent.h>
41#include <time.h>
42#include <fcntl.h>
43#include <unistd.h>
44#include <pwd.h>
45#include <grp.h>
46#include <errno.h>
47#include <sys/types.h>
48#include <stddef.h>
49
50#include <rtems.h>
51#include <rtems/monitor.h>
52#include <rtems/score/tod.h>
53
54#include <imfs.h>
55#include <rtems/shell.h>
56
57/* ----------------------------------------------- *
58  - str to int "0xaffe" "0b010010" "0123" "192939"
59 * ----------------------------------------------- */
60int str2int(char * s) {
61 int sign=1;   
62 int base=10;
63 int value=0;
64 int digit;
65 if (!s) return 0;
66 if (*s) {
67  if (*s=='-') {
68   sign=-1;
69   s++;
70   if (!*s) return 0;
71  };
72  if (*s=='0') {
73   s++;
74   switch(*s) {
75    case 'x':
76    case 'X':s++;
77             base=16;
78             break;
79    case 'b':
80    case 'B':s++;
81             base=2;
82             break;
83    default :base=8;
84             break;
85   }
86  };
87  while (*s) {
88   switch(*s) {
89    case '0':
90    case '1':
91    case '2':
92    case '3':
93    case '4':
94    case '5':
95    case '6':
96    case '7':
97    case '8':
98    case '9':digit=*s-'0';
99             break;
100    case 'A':     
101    case 'B':     
102    case 'C':     
103    case 'D':     
104    case 'E':     
105    case 'F':digit=*s-'A'+10;
106             break;
107    case 'a':     
108    case 'b':     
109    case 'c':     
110    case 'd':     
111    case 'e':     
112    case 'f':digit=*s-'a'+10;
113             break;
114    default:return value*sign;       
115   };
116   if (digit>base) return value*sign;
117   value=value*base+digit;
118   s++;
119  };
120 };
121 return value*sign;     
122}
123/*----------------------------------------------------------------------------*
124 * RAM MEMORY COMMANDS
125 *----------------------------------------------------------------------------*/
126
127#define mdump_adr (current_shell_env->mdump_adr)  /* static value */
128
129int main_mdump(int argc,char * argv[]) {
130 unsigned char n,m,max=0;
131 int adr=mdump_adr;
132 unsigned char * pb;
133 if (argc>1) adr=str2int(argv[1]);
134 if (argc>2) max=str2int(argv[2]);
135 max/=16;
136 if (!max) max=20;
137 for (m=0;m<max;m++) {
138  printf("0x%08X ",adr);
139  pb=(unsigned char*) adr;
140  for (n=0;n<16;n++)
141   printf("%02X%c",pb[n],n==7?'-':' ');
142  for (n=0;n<16;n++) {
143   printf("%c",isprint(pb[n])?pb[n]:'.');
144  };
145  printf("\n");
146  adr+=16;
147 };
148 mdump_adr=adr;
149 return 0;
150}
151/*----------------------------------------------------------------------------*/
152int main_mwdump(int argc,char * argv[]) {
153 unsigned char n,m,max=0;
154 int adr=mdump_adr;
155 unsigned short * pw;
156 if (argc>1) adr=str2int(argv[1]);
157 if (argc>2) max=str2int(argv[2]);
158 max/=16;
159 if (!max) max=20;
160 for (m=0;m<max;m++) {
161  printf("0x%08X ",adr);
162  pw=(unsigned short*) adr;
163  for (n=0;n<8;n++)
164   printf("%02X %02X%c",pw[n]/0x100,pw[n]%0x100,n==3?'-':' ');
165  for (n=0;n<8;n++) {
166   printf("%c",isprint(pw[n]/0x100)?pw[n]/0x100:'.');
167   printf("%c",isprint(pw[n]%0x100)?pw[n]%0x100:'.');
168  };
169  printf("\n");
170  adr+=16;
171 };
172 mdump_adr=adr;
173 return 0;
174}
175/*----------------------------------------------------------------------------*/
176int main_medit(int argc,char * argv[]) {
177 unsigned char * pb;
178 int n,i;
179 if (argc<3) {
180  printf("too few arguments\n");
181  return 0;
182 };
183 pb=(unsigned char*)str2int(argv[1]);
184 i=2;
185 n=0;
186 while (i<=argc) {
187  pb[n++]=str2int(argv[i++])%0x100;
188 }
189 mdump_adr=(int)pb;
190 return main_mdump(0,NULL);
191}
192/*----------------------------------------------------------------------------*/
193int main_mfill(int argc,char * argv[]) {
194 int  adr;
195 int  size;
196 unsigned char value;
197 if (argc<4) {
198  printf("too few arguments\n");
199  return 0;
200 };
201 adr  =str2int(argv[1]);
202 size =str2int(argv[2]);
203 value=str2int(argv[3])%0x100;
204 memset((unsigned char*)adr,size,value);
205 mdump_adr=adr;
206 return main_mdump(0,NULL);
207}
208/*----------------------------------------------------------------------------*/
209int main_mmove(int argc,char * argv[]) {
210 int  src;
211 int  dst;
212 int  size;
213 if (argc<4) {
214  printf("too few arguments\n");
215  return 0;
216 };
217 dst  =str2int(argv[1]);
218 src  =str2int(argv[2]);
219 size =str2int(argv[3]);
220 memcpy((unsigned char*)dst,(unsigned char*)src,size);
221 mdump_adr=dst;
222 return main_mdump(0,NULL);
223}
224/*----------------------------------------------------------------------------*/
225#ifdef MALLOC_STATS  /* /rtems/s/src/lib/libc/malloc.c */
226int main_malloc_dump(int argc,char * argv[]) {
227 void malloc_dump(void);
228 malloc_dump();
229 return 0;
230}
231#endif
232/*----------------------------------------------------------------------------
233 * Reset. Assumes that the watchdog is present.
234 *----------------------------------------------------------------------------*/
235int main_reset (int argc, char **argv)
236{
237  rtems_interrupt_level level;
238  printf ("Waiting for watchdog ... ");
239  tcdrain(fileno(stdout));
240
241  rtems_interrupt_disable (level);
242  for (;;)
243      ;
244  return 0;
245}
246/*----------------------------------------------------------------------------
247 * Alias. make an alias
248 *----------------------------------------------------------------------------*/
249int main_alias (int argc, char **argv)
250{
251 if (argc<3) {
252  printf("too few arguments\n");
253  return 1;
254 };
255 if (!shell_alias_cmd(argv[1],argv[2])) {
256  printf("unable to make an alias(%s,%s)\n",argv[1],argv[2]);
257 };
258 return 0;
259
260/*-----------------------------------------------------------*         
261 * Directory commands
262 *-----------------------------------------------------------*/
263int main_ls(int argc, char *argv[])
264{
265   char * fname;
266   DIR                 *dirp;
267   struct dirent       *dp;
268   struct stat         stat_buf;
269   struct passwd     * pwd;
270   struct group      * grp;
271   char * user;
272   char * group;
273   char   sbuf[256];
274   char   nbuf[1024];
275   int  n,size;
276
277   fname=".";
278   if (argc>1) fname=argv[1];
279
280   if ((dirp = opendir(fname)) == NULL)
281   {
282      printf("%s: No such file or directory.\n", fname);
283      return errno;
284   }
285   n=0;
286   size=0;
287   while ((dp = readdir(dirp)) != NULL)
288   {
289      strcpy(nbuf,fname);
290      if (nbuf[strlen(nbuf)-1]!='/') strcat(nbuf,"/");
291      strcat(nbuf,dp->d_name); /* always the fullpathname. Avoid ftpd problem.*/
292      if (stat(nbuf, &stat_buf) == 0)
293      { /* AWFUL buts works...*/
294         strftime(sbuf,sizeof(sbuf)-1,"%b %d %H:%M",gmtime(&stat_buf.st_mtime));     
295         pwd=getpwuid(stat_buf.st_uid);
296         user=pwd?pwd->pw_name:"nouser";
297         grp=getgrgid(stat_buf.st_gid);
298         group=grp?grp->gr_name:"nogrp";
299         printf("%c%c%c%c%c%c%c%c%c%c %3d %6.6s %6.6s %11d %s %s%c\n",
300                 (S_ISLNK(stat_buf.st_mode)?('l'):
301                    (S_ISDIR(stat_buf.st_mode)?('d'):('-'))),
302                 (stat_buf.st_mode & S_IRUSR)?('r'):('-'),
303                 (stat_buf.st_mode & S_IWUSR)?('w'):('-'),
304                 (stat_buf.st_mode & S_IXUSR)?('x'):('-'),
305                 (stat_buf.st_mode & S_IRGRP)?('r'):('-'),
306                 (stat_buf.st_mode & S_IWGRP)?('w'):('-'),
307                 (stat_buf.st_mode & S_IXGRP)?('x'):('-'),
308                 (stat_buf.st_mode & S_IROTH)?('r'):('-'),
309                 (stat_buf.st_mode & S_IWOTH)?('w'):('-'),
310                 (stat_buf.st_mode & S_IXOTH)?('x'):('-'),
311                 (int)stat_buf.st_nlink,
312                 user,group,
313                 (int)stat_buf.st_size,
314                 sbuf,
315                 dp->d_name,
316                 S_ISDIR(stat_buf.st_mode)?'/':' ');
317         n++;
318         size+=stat_buf.st_size;
319      }
320   }
321   printf("%d files %d bytes occupied\n",n,size);
322   closedir(dirp);
323   return 0;
324}
325/*-----------------------------------------------------------*/         
326int main_pwd (int argc, char *argv[]) {
327   char dir[1024];
328   getcwd(dir,1024);
329   printf("%s\n",dir);
330   return 0;
331}
332/*-----------------------------------------------------------*/         
333int main_chdir (int argc, char *argv[]) {
334   char *dir;
335   dir="/";
336   if (argc>1) dir=argv[1];
337   if (chdir(dir)) {
338    printf("chdir to '%s' failed:%s\n",dir,strerror(errno));
339    return errno;
340   };
341   return 0;
342}
343/*-----------------------------------------------------------*/         
344int main_mkdir (int argc, char *argv[]) {
345   char *dir;
346   int n;
347   n=1;
348   while (n<argc) {
349    dir=argv[n++];
350    if (mkdir(dir,S_IRWXU|S_IRWXG|S_IRWXO)) {
351      printf("mkdir '%s' failed:%s\n",dir,strerror(errno));
352    }; 
353   }; 
354   return errno;
355}
356/*-----------------------------------------------------------*/         
357int main_rmdir (int argc, char *argv[])
358{
359   char *dir;
360   int n;
361   n=1;
362   while (n<argc) {
363    dir=argv[n++];
364    if (rmdir(dir)) printf("rmdir '%s' failed:%s\n",dir,strerror(errno));
365   };
366   return errno;
367}
368/*-----------------------------------------------------------*/         
369int main_chroot(int argc,char * argv[]) {
370 char * new_root="/";
371 if (argc==2) new_root=argv[1];
372 if (chroot(new_root)<0) {
373  printf("error %s:chroot(%s);\n",strerror(errno),new_root);
374  return -1;
375 };
376 return 0;
377}
378/*-----------------------------------------------------------*/         
379int main_cat   (int argc, char *argv[])
380{
381   int n;
382   n=1;
383   while (n<argc) cat_file(stdout,argv[n++]);
384   return 0;
385}
386/*-----------------------------------------------------------*/         
387int main_rm    (int argc, char *argv[])
388{
389   int n;
390   n=1;
391   while (n<argc) {
392    if (unlink(argv[n])) {
393     printf("error %s:rm %s\n",strerror(errno),argv[n]);
394     return -1;
395    };
396    n++;
397   };
398   return 0;
399}
400/*-----------------------------------------------------------*/         
401/* date - print time and date */
402
403int main_date(int argc,char *argv[])
404{
405  time_t t;
406  time(&t);
407  printf("%s", ctime(&t));
408  return 0;
409}
410/*-----------------------------------------------------------*/
411int main_logoff(int argc,char *argv[])
412{
413  printf("logoff from the system..."); 
414  current_shell_env->exit_shell=TRUE;   
415  return 0;
416}
417/*-----------------------------------------------------------*/
418int main_tty   (int argc,char *argv[])
419{
420  printf("%s\n",ttyname(fileno(stdin)));
421  return 0;
422}
423/*-----------------------------------------------------------*/
424int main_whoami(int argc,char *argv[])
425{
426   struct passwd     * pwd;
427   pwd=getpwuid(getuid());
428   printf("%s\n",pwd?pwd->pw_name:"nobody");
429   return 0;
430}
431/*-----------------------------------------------------------*/
432int main_id    (int argc,char *argv[])
433{
434   struct passwd     * pwd;
435   struct group      * grp;
436   pwd=getpwuid(getuid());
437   grp=getgrgid(getgid());
438   printf("uid=%d(%s),gid=%d(%s),",
439                   getuid(),pwd?pwd->pw_name:"",
440                   getgid(),grp?grp->gr_name:"");
441   pwd=getpwuid(geteuid());
442   grp=getgrgid(getegid());
443   printf("euid=%d(%s),egid=%d(%s)\n",
444                   geteuid(),pwd?pwd->pw_name:"",
445                   getegid(),grp?grp->gr_name:"");
446   return 0;
447}
448/*-----------------------------------------------------------*/
449int main_umask(int argc,char *argv[])
450{
451   mode_t msk=umask(0);
452   if (argc == 2) msk=str2int(argv[1]);
453   umask(msk);
454   msk=umask(0);
455   printf("0%o\n", (unsigned int) msk);
456   umask(msk);
457   return 0;
458}
459/*-----------------------------------------------------------*/
460int main_chmod(int argc,char *argv[])
461{
462   int n;
463   mode_t mode;
464   if (argc > 2){
465    mode=str2int(argv[1])&0777;
466    n=2;
467    while (n<argc) chmod(argv[n++],mode);
468   };
469   return 0;
470}
471/*-----------------------------------------------------------*         
472 * with this you can call at all the rtems monitor commands.
473 * Not all work fine but you can show the rtems status and more.
474 *-----------------------------------------------------------*/         
475int main_monitor(int argc,char * argv[]) {
476 rtems_monitor_command_entry_t *command;
477 rtems_task_ident(RTEMS_SELF,0,&rtems_monitor_task_id);
478 rtems_monitor_node = rtems_get_node(rtems_monitor_task_id);
479 rtems_monitor_default_node = rtems_monitor_node;
480 if ((command=rtems_monitor_command_lookup(rtems_monitor_commands,argc,argv)))
481  command->command_function(argc, argv, &command->command_arg, 0);
482 return 0;
483}
484/*-----------------------------------------------------------*/         
485void register_cmds(void) {
486  rtems_monitor_command_entry_t *command;
487  /* monitor topic */
488  command=rtems_monitor_commands;
489  while (command) {
490   if (strcmp("exit",command->command)) /*Exclude EXIT (alias quit)*/
491    shell_add_cmd(command->command,"monitor",
492                  command->usage  ,main_monitor);
493   command=command->next;
494  };
495  /* dir[ectories] topic */
496  shell_add_cmd  ("ls"    ,"dir","ls [dir]     # list files in the directory" ,main_ls   );
497  shell_add_cmd  ("chdir" ,"dir","chdir [dir]  # change the current directory",main_chdir);
498  shell_add_cmd  ("rmdir" ,"dir","rmdir  dir   # remove directory"            ,main_rmdir);
499  shell_add_cmd  ("mkdir" ,"dir","mkdir  dir   # make a directory"            ,main_mkdir);
500  shell_add_cmd  ("pwd"   ,"dir","pwd          # print work directory"        ,main_pwd  );
501  shell_add_cmd  ("chroot","dir","chroot [dir] # change the root directory"   ,main_chroot);
502  shell_add_cmd  ("cat"   ,"dir","cat n1 [n2 [n3...]]# show the ascii contents",main_cat );
503  shell_add_cmd  ("rm"    ,"dir","rm n1 [n2 [n3...]]# remove files"           ,main_rm   );
504  shell_add_cmd  ("chmod" ,"dir","chmod 0777 n1 n2... #change filemode"       ,main_chmod);
505
506  shell_alias_cmd("ls"    ,"dir");
507  shell_alias_cmd("chdir" ,"cd");
508
509  /* misc. topic */
510  shell_add_cmd  ("logoff","misc","logoff from the system"                    ,main_logoff);
511  shell_alias_cmd("logoff","exit");
512  shell_add_cmd  ("date" ,"misc","date"                                       ,main_date);
513  shell_add_cmd  ("reset","misc","reset the BSP"                              ,main_reset);
514  shell_add_cmd  ("alias","misc","alias old new"                              ,main_alias);
515  shell_add_cmd  ("tty"  ,"misc","show ttyname"                               ,main_tty  );
516  shell_add_cmd  ("whoami","misc","show current user"                         ,main_whoami);
517  shell_add_cmd  ("id"    ,"misc","show uid,gid,euid,egid"                    ,main_id    );
518  shell_add_cmd  ("umask" ,"misc","umask [new_umask]"                         ,main_umask );
519
520
521  /* memory topic */
522  shell_add_cmd  ("mdump","mem"  ,"mdump [adr [size]]"           ,main_mdump);
523  shell_add_cmd  ("wdump","mem"  ,"wdump [adr [size]]"           ,main_mwdump);
524  shell_add_cmd  ("medit","mem"  ,"medit adr value [value ...]"  ,main_medit);
525  shell_add_cmd  ("mfill","mem"  ,"mfill adr size value"         ,main_mfill);
526  shell_add_cmd  ("mmove","mem"  ,"mmove dst src size"           ,main_mmove);
527#ifdef MALLOC_STATS  /* /rtems/s/src/lib/libc/malloc.c */
528  shell_add_cmd  ("malloc","mem","mem  show memory malloc'ed"                 ,main_mem);
529#endif 
530}
531/*-----------------------------------------------------------*/         
Note: See TracBrowser for help on using the repository browser.