summaryrefslogtreecommitdiffstats
path: root/c/src/libmisc/shell/cmds.c
blob: 52a4079bf2697b3f275409650e089a427aea2d75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * Author: Fernando RUIZ CASAS
 *
 *  Work: fernando.ruiz@ctv.es
 *  Home: correo@fernando-ruiz.com
 *
 * This file is inspired in rtems_monitor & Chris John MyRightBoot
 * 
 * But I want to make it more user friendly
 * A 'monitor' command is added to adapt the call rtems monitor commands
 * at my call procedure
 * 
 * MINIX date.c is adapted to run here. Like a exercise only....
 *
 * TODO: A lot of improvements of course. 
 *      cat, cp, rm, mv, ...
 *      hexdump,
 *      
 *      More? Say me it, please...
 *      
 *      The BSP Specific are not welcome here.
 *      
 * C&S welcome...
 *
 *  $Id$
 */

#undef  __STRICT_ANSI__ /* fileno() */
#include <stdio.h>
#include <termios.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <dirent.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <stddef.h>

#include <rtems.h>
#include <rtems/monitor.h>
#include <rtems/score/tod.h>

#include <imfs.h>
#include <rtems/shell.h>

/* ----------------------------------------------- *
  - str to int "0xaffe" "0b010010" "0123" "192939"
 * ----------------------------------------------- */
int str2int(char * s) {
 int sign=1;	
 int base=10;
 int value=0;
 int digit;
 if (!s) return 0;
 if (*s) {
  if (*s=='-') {
   sign=-1;
   s++;
   if (!*s) return 0;
  };
  if (*s=='0') {
   s++;
   switch(*s) {
    case 'x':
    case 'X':s++;
 	     base=16;
	     break;
    case 'b':
    case 'B':s++;
	     base=2;
	     break;
    default :base=8;
	     break;
   }
  }; 
  while (*s) {
   switch(*s) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':digit=*s-'0';
 	     break;
    case 'A':	   
    case 'B':	   
    case 'C':	   
    case 'D':	   
    case 'E':	   
    case 'F':digit=*s-'A'+10;
	     break;
    case 'a':	   
    case 'b':	   
    case 'c':	   
    case 'd':	   
    case 'e':	   
    case 'f':digit=*s-'a'+10;
	     break;
    default:return value*sign;	     
   };
   if (digit>base) return value*sign;
   value=value*base+digit;
   s++;
  };
 };
 return value*sign;	
}
/*----------------------------------------------------------------------------*
 * RAM MEMORY COMMANDS
 *----------------------------------------------------------------------------*/

#define mdump_adr (current_shell_env->mdump_adr)  /* static value */

int main_mdump(int argc,char * argv[]) {
 unsigned char n,m,max=0;
 int adr=mdump_adr;
 unsigned char * pb;
 if (argc>1) adr=str2int(argv[1]);
 if (argc>2) max=str2int(argv[2]);
 max/=16;
 if (!max) max=20;
 for (m=0;m<max;m++) {
  printf("0x%08X ",adr);
  pb=(unsigned char*) adr;
  for (n=0;n<16;n++)
   printf("%02X%c",pb[n],n==7?'-':' ');
  for (n=0;n<16;n++) {
   printf("%c",isprint(pb[n])?pb[n]:'.');
  };
  printf("\n");
  adr+=16;
 };
 mdump_adr=adr;
 return 0;
}
/*----------------------------------------------------------------------------*/
int main_mwdump(int argc,char * argv[]) {
 unsigned char n,m,max=0;
 int adr=mdump_adr;
 unsigned short * pw;
 if (argc>1) adr=str2int(argv[1]);
 if (argc>2) max=str2int(argv[2]);
 max/=16;
 if (!max) max=20;
 for (m=0;m<max;m++) {
  printf("0x%08X ",adr);
  pw=(unsigned short*) adr;
  for (n=0;n<8;n++)
   printf("%02X %02X%c",pw[n]/0x100,pw[n]%0x100,n==3?'-':' ');
  for (n=0;n<8;n++) {
   printf("%c",isprint(pw[n]/0x100)?pw[n]/0x100:'.');
   printf("%c",isprint(pw[n]%0x100)?pw[n]%0x100:'.');
  };
  printf("\n");
  adr+=16;
 };
 mdump_adr=adr;
 return 0;
}
/*----------------------------------------------------------------------------*/
int main_medit(int argc,char * argv[]) {
 unsigned char * pb;
 int n,i;
 if (argc<3) {
  printf("too few arguments\n");
  return 0;
 };
 pb=(unsigned char*)str2int(argv[1]);
 i=2;
 n=0;
 while (i<=argc) {
  pb[n++]=str2int(argv[i++])%0x100;
 }
 mdump_adr=(int)pb;
 return main_mdump(0,NULL);
}
/*----------------------------------------------------------------------------*/
int main_mfill(int argc,char * argv[]) {
 int  adr;
 int  size;
 unsigned char value;
 if (argc<4) {
  printf("too few arguments\n");
  return 0;
 };
 adr  =str2int(argv[1]);
 size =str2int(argv[2]);
 value=str2int(argv[3])%0x100;
 memset((unsigned char*)adr,size,value);
 mdump_adr=adr;
 return main_mdump(0,NULL);
}
/*----------------------------------------------------------------------------*/
int main_mmove(int argc,char * argv[]) {
 int  src;
 int  dst;
 int  size;
 if (argc<4) {
  printf("too few arguments\n");
  return 0;
 };
 dst  =str2int(argv[1]);
 src  =str2int(argv[2]);
 size =str2int(argv[3]);
 memcpy((unsigned char*)dst,(unsigned char*)src,size);
 mdump_adr=dst;
 return main_mdump(0,NULL);
}
/*----------------------------------------------------------------------------*/
#ifdef MALLOC_STATS  /* /rtems/s/src/lib/libc/malloc.c */
int main_malloc_dump(int argc,char * argv[]) {
 void malloc_dump(void); 
 malloc_dump();	
 return 0;
}
#endif
/*----------------------------------------------------------------------------
 * Reset. Assumes that the watchdog is present.
 *----------------------------------------------------------------------------*/
int main_reset (int argc, char **argv)
{
  rtems_interrupt_level level;
  printf ("Waiting for watchdog ... ");
  tcdrain(fileno(stdout));

  rtems_interrupt_disable (level);
  for (;;) 
      ;
  return 0;
}
/*----------------------------------------------------------------------------
 * Alias. make an alias
 *----------------------------------------------------------------------------*/
int main_alias (int argc, char **argv)
{
 if (argc<3) {
  printf("too few arguments\n");
  return 1;
 };
 if (!shell_alias_cmd(argv[1],argv[2])) {
  printf("unable to make an alias(%s,%s)\n",argv[1],argv[2]);
 };
 return 0;
}  
/*-----------------------------------------------------------*  	
 * Directory commands
 *-----------------------------------------------------------*/
int main_ls(int argc, char *argv[])
{
   char * fname;
   DIR                 *dirp;
   struct dirent       *dp;
   struct stat         stat_buf;
   char   sbuf[256];
   char   nbuf[1024];
   int  n,size;

   fname=".";
   if (argc>1) fname=argv[1];

   if ((dirp = opendir(fname)) == NULL)
   {
      printf("%s: No such file or directory.\n", fname);
      return errno;
   }
   n=0;
   size=0;
   while ((dp = readdir(dirp)) != NULL)
   {
      strcpy(nbuf,fname);
      if (nbuf[strlen(nbuf)-1]!='/') strcat(nbuf,"/");
      strcat(nbuf,dp->d_name); /* always the fullpathname. Avoid ftpd problem.*/
      if (stat(nbuf, &stat_buf) == 0)
      { /* AWFUL buts works...*/
	 strftime(sbuf,sizeof(sbuf)-1,"%b %d %H:%M",gmtime(&stat_buf.st_atime));     
         printf("%c%c%c%c%c%c%c%c%c%c %3d rtems  rtems  %11d %s %s%c\n",
                 (S_ISLNK(stat_buf.st_mode)?('l'):
                    (S_ISDIR(stat_buf.st_mode)?('d'):('-'))),
                 (stat_buf.st_mode & S_IRUSR)?('r'):('-'),
                 (stat_buf.st_mode & S_IWUSR)?('w'):('-'),
                 (stat_buf.st_mode & S_IXUSR)?('x'):('-'),
                 (stat_buf.st_mode & S_IRGRP)?('r'):('-'),
                 (stat_buf.st_mode & S_IWGRP)?('w'):('-'),
                 (stat_buf.st_mode & S_IXGRP)?('x'):('-'),
                 (stat_buf.st_mode & S_IROTH)?('r'):('-'),
                 (stat_buf.st_mode & S_IWOTH)?('w'):('-'),
                 (stat_buf.st_mode & S_IXOTH)?('x'):('-'),
                 (int)stat_buf.st_nlink,
                 (int)stat_buf.st_size,
		 sbuf,
                 dp->d_name,
                 S_ISDIR(stat_buf.st_mode)?'/':' ');
	 n++;
	 size+=stat_buf.st_size;
      }
   }
   printf("%d files %d bytes occupied\n",n,size);
   closedir(dirp);
   return 0;
}
/*-----------------------------------------------------------*/  	
int main_pwd (int argc, char *argv[]) {
   char dir[1024];
   getcwd(dir,1024);
   printf("%s\n",dir);
   return 0;
}
/*-----------------------------------------------------------*/  	
int main_chdir (int argc, char *argv[]) {
   char *dir;
   dir="/";
   if (argc>1) dir=argv[1];
   if (chdir(dir)) {
    printf("chdir to '%s' failed:%s\n",dir,strerror(errno));
    return errno;
   }; 
   return 0;
}
/*-----------------------------------------------------------*/  	
int main_mkdir (int argc, char *argv[]) {
   char *dir;
   dir=NULL;
   if (argc>1) dir=argv[1];
   if (mkdir(dir,S_IRWXU|S_IRWXG|S_IRWXO)) {
     printf("mkdir '%s' failed:%s\n",dir,strerror(errno));
   };  
   return errno;
}
/*-----------------------------------------------------------*/  	
int main_rmdir (int argc, char *argv[])
{
   char *dir;
   dir=NULL;
   if (argc>1) dir=argv[1];
   if (rmdir(dir)) printf("rmdir '%s' failed:%s\n",dir,strerror(errno));
   return errno;
}
/*-----------------------------------------------------------*/  	
int main_chroot(int argc,char * argv[]) {
 char * new_root="/";
 if (argc==2) new_root=argv[1];
 if (chroot(new_root)<0) {
  printf("error %s:chroot(%s);\n",strerror(errno),new_root);
  return -1;
 };
 return 0;
}
/*-----------------------------------------------------------*/  	
/* date - print or set time and date		Author: Jan Looyen */
/* MINIX 1.5 GPL'ed */


#define	MIN	60L		/* # seconds in a minute */
#define	HOUR	(60 * MIN)	/* # seconds in an hour */
#define	DAY	(24 * HOUR)	/* # seconds in a day */
#define	YEAR	(365 * DAY)	/* # seconds in a year */

static int conv(unsigned32 *value,char **ptr,unsigned32 max) 
{
  int buf;
  *ptr -= 2;
  buf = atoi(*ptr);
  **ptr = 0;
  if (buf < 0 || buf > max) {
   fprintf(stderr, "Date: bad conversion\n");
   return 0;
  }; 
  *value=buf;
  return 1;
}

static int set_time(char *t) 
{
  rtems_time_of_day tod;	
  FILE * rtc;
  char *tp;
  int len;
  if (rtems_clock_get(RTEMS_CLOCK_GET_TOD,&tod)!=RTEMS_SUCCESSFUL) 
	  memset(&tod,0,sizeof(tod));
  len = strlen(t);
  if (len != 12 && len != 10 && len != 6 && len != 4) return 0;
  tp = t;
  while (*tp)
   if (!isdigit(*tp++)) {
    fprintf(stderr, "date: bad conversion\n");
    return 0;
   };
  if (len == 6 || len == 12) 
   if (!conv(&tod.second,&tp, 59)) return 0;
  if (!conv(&tod.minute,&tp, 59)) return 0;
  if (!conv(&tod.hour,&tp, 23)) return 0;
  if (len == 12 || len == 10) {
   if (!conv(&tod.year,&tp, 99)) return 0;
   tod.year+=1900;
   if (tod.year<TOD_BASE_YEAR) tod.year+=100;
   if (!conv(&tod.day   ,&tp, 31)) return 0;
   if (!conv(&tod.month ,&tp, 12)) return 0;
  }
  if (!_TOD_Validate(&tod)) {
    fprintf(stderr, "Invalid date value\n");
  } else {
   rtems_clock_set(&tod);	  
   rtems_clock_get(RTEMS_CLOCK_GET_TOD,&tod);	  
   rtc=fopen("/dev/rtc","r+");
   if (rtc) {
    fwrite(&tod,sizeof(tod),1,rtc);
    fclose(rtc);
   };
  };
  return 1;
}

int main_date(int argc,char *argv[]) 
{
  time_t t;
  if (argc == 2) set_time(argv[1]);
  time(&t);
  printf("%s", ctime(&t));
  return 0;
}
/*-----------------------------------------------------------*  	
 * with this you can call at all the rtems monitor commands.
 * Not all work fine but you can show the rtems status and more.
 *-----------------------------------------------------------*/  	
int main_monitor(int argc,char * argv[]) {
 rtems_monitor_command_entry_t *command;
 extern rtems_monitor_command_entry_t rtems_monitor_commands[];
 rtems_task_ident(RTEMS_SELF,0,&rtems_monitor_task_id);
 rtems_monitor_node = rtems_get_node(rtems_monitor_task_id);
 rtems_monitor_default_node = rtems_monitor_node;
 if ((command=rtems_monitor_command_lookup(rtems_monitor_commands,argc,argv)))
  command->command_function(argc, argv, command->command_arg, 0);
 return 0;
}
/*-----------------------------------------------------------*/  	
void register_cmds(void) {
  rtems_monitor_command_entry_t *command;
  extern rtems_monitor_command_entry_t rtems_monitor_commands[];
  /* dir[ectories] topic */
  shell_add_cmd  ("ls"    ,"dir","ls [dir]     # list files in the directory" ,main_ls   );
  shell_add_cmd  ("chdir" ,"dir","chdir [dir]  # change the current directory",main_chdir);
  shell_add_cmd  ("rmdir" ,"dir","rmdir  dir   # remove directory"            ,main_rmdir);
  shell_add_cmd  ("mkdir" ,"dir","mkdir  dir   # make a directory"            ,main_mkdir);
  shell_add_cmd  ("pwd"   ,"dir","pwd          # print work directory"        ,main_pwd  );
  shell_add_cmd  ("chroot","dir","chroot [dir] # change the root directory"   ,main_chroot);

  shell_alias_cmd("ls"    ,"dir");
  shell_alias_cmd("chdir" ,"cd");

  /* misc. topic */
  shell_add_cmd  ("date" ,"misc","date [[MMDDYY]hhmm[ss]]"                    ,main_date);
  shell_add_cmd  ("reset","misc","reset the BSP"                              ,main_reset);
  shell_add_cmd  ("alias","misc","alias old new"                              ,main_alias);

  /* memory topic */
  shell_add_cmd  ("mdump","mem"  ,"mdump [adr [size]]"           ,main_mdump);
  shell_add_cmd  ("wdump","mem"  ,"wdump [adr [size]]"           ,main_mwdump);
  shell_add_cmd  ("medit","mem"  ,"medit adr value [value ...]"  ,main_medit);
  shell_add_cmd  ("mfill","mem"  ,"mfill adr size value"         ,main_mfill);
  shell_add_cmd  ("mmove","mem"  ,"mmove dst src size"           ,main_mmove);
#ifdef MALLOC_STATS  /* /rtems/s/src/lib/libc/malloc.c */
  shell_add_cmd  ("malloc","mem","mem  show memory malloc'ed"                 ,main_mem);
#endif  
  /* monitor topic */
  command=rtems_monitor_commands;
  while (command) {
   shell_add_cmd(command->command,"monitor",
                 command->usage  ,main_monitor);
   command=command->next;
  };
}
/*-----------------------------------------------------------*/