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

Last change on this file since b9e230a2 was b9e230a2, checked in by Ralf Corsepius <ralf.corsepius@…>, on 07/08/03 at 08:38:15

2003-07-08 Ralf Corsepius <corsepiu@…>

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