summaryrefslogblamecommitdiffstats
path: root/c/src/libmisc/shell/shell.c
blob: d7c2502d9040b98f551cbb7311b2e777c58ed748 (plain) (tree)
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





















































































































































































































































































































































































































































































                                                                                          
/*
 *
 *  Instantatiate a new terminal shell.
 *
 *  Author: 
 *
 *   WORK: fernando.ruiz@ctv.es 
 *   HOME: correo@fernando-ruiz.com
 *
 *   Thanks at:
 *    Chris John
 *
 *  $Id$
 */

#undef  __STRICT_ANSI__  /* fileno() */
#include <stdio.h>

#include <rtems.h>
#include <rtems/error.h>
#include <rtems/libio.h>
#include <rtems/libio_.h> 

#include <termios.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>

#include <rtems/shell.h>
/* ----------------------------------------------- *
 * This is a stupidity but is cute.
 * ----------------------------------------------- */
rtems_unsigned32 new_rtems_name(char * rtems_name) {
	static char b[5];
	sprintf(b,"%-4.4s",rtems_name);
	return rtems_build_name(b[0],b[1],b[2],b[3]);
}
/* **************************************************************
 * common linked list of shell commands.
 * Because the help report is very long
 * I have a topic for each command.
 * Help list the topics
 * help [topic] list the commands for the topic
 * help [command] help for the command
 * Can you see help rtems monitor report?
 * ************************************************************** */

struct shell_topic_tt;
typedef struct shell_topic_tt shell_topic_t;

struct shell_topic_tt {
	char * topic;
	shell_topic_t * next;
};


static shell_cmd_t   * shell_first_cmd;
static shell_topic_t * shell_first_topic;
/* ----------------------------------------------- *
 * Using Chain I can reuse the rtems code. 
 * I am more comfortable with this, sorry.
 * ----------------------------------------------- */
shell_topic_t * shell_lookup_topic(char * topic) {
  shell_topic_t * shell_topic;
  shell_topic=shell_first_topic;
  while (shell_topic) {
   if (!strcmp(shell_topic->topic,topic)) return shell_topic;
   shell_topic=shell_topic->next;
  };
  return (shell_topic_t *) NULL;
}
/* ----------------------------------------------- */
shell_topic_t * shell_add_topic(char * topic) {
 shell_topic_t * current,*aux;
 if (!shell_first_topic) {
  aux=malloc(sizeof(shell_topic_t));	
  aux->topic=topic;
  aux->next=(shell_topic_t*)NULL;
  return shell_first_topic=aux;
 } else {
  current=shell_first_topic;
  if (!strcmp(topic,current->topic)) return current;
  while (current->next) {
   if (!strcmp(topic,current->next->topic)) return current->next;
   current=current->next;
  }; 
  aux=malloc(sizeof(shell_topic_t));	
  aux->topic=topic;
  aux->next=(shell_topic_t*)NULL;
  current->next=aux;
  return aux;
 };
}
/* ----------------------------------------------- */
shell_cmd_t * shell_lookup_cmd(char * cmd) {
  shell_cmd_t * shell_cmd;
  shell_cmd=shell_first_cmd;
  while (shell_cmd) {
   if (!strcmp(shell_cmd->name,cmd)) return shell_cmd;
   shell_cmd=shell_cmd->next;
  };
  return (shell_cmd_t *) NULL;
}
/* ----------------------------------------------- */
shell_cmd_t * shell_add_cmd(char * cmd,
		      char * topic,
		      char * usage,
                      shell_command_t command) {
  int shell_help(int argc,char * argv[]);
  shell_cmd_t * shell_cmd,*shell_pvt;
  if (!shell_first_cmd) {
   shell_first_cmd=(shell_cmd_t *) malloc(sizeof(shell_cmd_t));
   shell_first_cmd->name   ="help";
   shell_first_cmd->topic  ="help";
   shell_first_cmd->usage  ="help [topic] # list of usage of commands";
   shell_first_cmd->command=shell_help;
   shell_first_cmd->alias  =(shell_cmd_t *) NULL;
   shell_first_cmd->next   =(shell_cmd_t *) NULL;
   shell_add_topic(shell_first_cmd->topic);
   register_cmds();
  };
  if (!cmd)     return (shell_cmd_t *) NULL;
  if (!command) return (shell_cmd_t *) NULL;
  shell_cmd=(shell_cmd_t *) malloc(sizeof(shell_cmd_t));
  shell_cmd->name   =cmd;
  shell_cmd->topic  =topic;
  shell_cmd->usage  =usage;
  shell_cmd->command=command;
  shell_cmd->alias  =(shell_cmd_t *) NULL;
  shell_cmd->next   =(shell_cmd_t *) NULL;
  shell_add_topic(shell_cmd->topic);
  shell_pvt=shell_first_cmd;
  while (shell_pvt->next) shell_pvt=shell_pvt->next;
  return shell_pvt->next=shell_cmd;
}
/* ----------------------------------------------- *
 * you can make an alias for every command.
 * ----------------------------------------------- */
shell_cmd_t * shell_alias_cmd(char * cmd, char * alias) {
  shell_cmd_t * shell_cmd,* shell_aux;
  shell_aux=(shell_cmd_t *) NULL;
  if (alias) {
   if ((shell_aux=shell_lookup_cmd(alias))!=NULL) {
    return NULL;
   };
   if ((shell_cmd=shell_lookup_cmd(cmd))!=NULL) {
    shell_aux=shell_add_cmd(alias,shell_cmd->topic,
		            shell_cmd->usage,shell_cmd->command);
    if (shell_aux) shell_aux->alias=shell_cmd;
   };
  };
  return shell_aux;
}
/* ----------------------------------------------- *
 * Poor but enough..
 * TODO: Redirection capture. "" evaluate, ... C&S welcome.
 * ----------------------------------------------- */
int shell_make_args(char * cmd,
		 int  * pargc,
		 char * argv[]) {
  int argc=0;
  while ((cmd=strtok(cmd," \t\r\n"))!=NULL) {
    argv[argc++]=cmd;
    cmd=(char*)NULL;
   };
  argv[argc]=(char*)NULL;
  return *pargc=argc;
}
/* ----------------------------------------------- *
 * show the help for one command.
 * ----------------------------------------------- */
int shell_help_cmd(shell_cmd_t * shell_cmd) {
  char * pc;
  int    col,line;
  printf("%-10.10s -",shell_cmd->name);
  col=12;
  line=1;
  if (shell_cmd->alias) {
   printf("is an <alias> for command '%s'",shell_cmd->alias->name);
  } else 
  if (shell_cmd->usage) {
   pc=shell_cmd->usage;
   while (*pc) {
    switch(*pc) {
     case '\r':break;
     case '\n':putchar('\n');
               col=0;
               break;
     default  :putchar(*pc);
               col++;
               break;
    };
    pc++;	    
    if(col>78) { /* What daring... 78?*/
     if (*pc) {
      putchar('\n');
      col=0;
     };
    };
    if (!col && *pc) {
      printf("            ");
      col=12;line++;
    };  
   };
  };
  puts("");
  return line;
}
/* ----------------------------------------------- *
 * show the help. The first command implemented.
 * Can you see the header of routine? Known?
 * The same with all the commands....
 * ----------------------------------------------- */
int shell_help(int argc,char * argv[]) {
  int col,line,arg;	
  shell_topic_t *topic;
  shell_cmd_t * shell_cmd=shell_first_cmd;
  if (argc<2) {
   printf("help: TOPIC? The topics are\n");	  
   topic=shell_first_topic;
   col=0;
   while (topic) {
    if (!col){
     col=printf("   %s",topic->topic);
    } else {
     if ((col+strlen(topic->topic)+2)>78){
      printf("\n");
      col=printf("   %s",topic->topic);
     } else {
      col+=printf(", %s",topic->topic);
     }; 
    };
    topic=topic->next;
   };
   printf("\n");
   return 1;
  };
  line=0;
  for (arg=1;arg<argc;arg++) {
   if (line>16) {
    printf("Press any key to continue...");getchar();
    printf("\n");
    line=0;
   };
   topic=shell_lookup_topic(argv[arg]);
   if (!topic){
    if ((shell_cmd=shell_lookup_cmd(argv[arg]))==NULL) {
     printf("help: topic or cmd '%s' not found. Try <help> alone for a list\n",argv[arg]);
     line++;
    } else {
     line+=shell_help_cmd(shell_cmd);
    }
    continue;
   };
   printf("help: list for the topic '%s'\n",argv[arg]);
   line++;
   while (shell_cmd) {
    if (!strcmp(topic->topic,shell_cmd->topic)) 
     line+=shell_help_cmd(shell_cmd);
    if (line>16) {
     printf("Press any key to continue...");getchar();
     printf("\n");
     line=0;
    };
    shell_cmd=shell_cmd->next;
   };
  }; 
  puts("");
  return 0;
}
/* ----------------------------------------------- *
 * TODO:Change to bash readline() better.
 * ----------------------------------------------- */
int shell_scanline(char * line,int size,FILE * in,FILE * out) {
  int c,col;
  col=0;
  for (;;) {
   line[col]=0;	  
   c=fgetc(in);
   switch (c) {
    case EOF :return 0;
    case '\r':if (out) fputc('\n',out);
	      return 1;
    case '\b':if (col) {
	       if (out) {
	        fputc('\b',out);
	        fputc(' ',out);
	        fputc('\b',out);
	       };
	       col--;
	      } else {
	       if (out) fputc('\a',out);
	      };
	      break;
    default  :if (!iscntrl(c)) {
		if (col<size-1) {
		 line[col++]=c;	
	         if (out) fputc(c,out);
		} else {
	          if (out) fputc('\a',out);
		};
	      } else {
	        if (out) fputc('\a',out);
	      };
	      break;
   };
  };
}
/* ----------------------------------------------- *
 * - The shell TASK                               
 * Poor but enough..
 * TODO: Redirection. Tty Signals. ENVVARs. Shell language.
 * ----------------------------------------------- */
shell_env_t global_shell_env ,
          * current_shell_env=&global_shell_env; 

extern char **environ;	  

rtems_task shell_shell(rtems_task_argument task_argument) {

  shell_env_t * shell_env =(shell_env_t*) task_argument;
  shell_cmd_t * shell_cmd;

  rtems_status_code sc;

  struct termios term;	
  char * devname;
  char * curdir;

  char cmd[256];
  char last_cmd[256]; /* to repeat 'r' */
  int  argc;
  char * argv[128];

  sc=rtems_libio_set_private_env();
  if (sc!=RTEMS_SUCCESSFUL) {
   rtems_error(sc,"rtems_libio_set_private_env():");	 
   printk("rtems_libio_set_private_env():%d",sc);	 
   rtems_task_delete(RTEMS_SELF);
  }; 

  sc=rtems_task_variable_add(RTEMS_SELF,(void*)&current_shell_env,free);
  if (sc!=RTEMS_SUCCESSFUL) {
   rtems_error(sc,"rtems_task_variable_add():");	 
   printk("rtems_task_variable_add():%d",sc);	 
   rtems_task_delete(RTEMS_SELF);
  }; 

  current_shell_env=shell_env; /* Set the task var */

  devname=shell_env->devname;

  stdin =fopen(devname,"r+");
    
  if (!stdin) {
   fprintf(stderr,"shell:unable to open stdin.%s:%s\n",devname,strerror(errno));
   printk("shell:unable to open stdin.(%s)",strerror(errno));	 
   rtems_task_delete(RTEMS_SELF);
  };
  setvbuf(stdin,NULL,_IONBF,0); /* Not buffered*/
  /* make a raw terminal,Linux MANuals */
  if (tcgetattr (fileno(stdin), &term)>=0) {
   term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
   term.c_oflag &= ~OPOST;
   term.c_oflag |= (OPOST|ONLCR);
   term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
   term.c_cflag  = CLOCAL | CREAD |(shell_env->tcflag);
   term.c_cc[VMIN]  = 1;
   term.c_cc[VTIME] = 0;
   if (tcsetattr (fileno(stdin), TCSADRAIN, &term) < 0) {
     fprintf(stderr,"shell:cannot set terminal attributes(%s)\n",devname);
   };
   stdout=fopen(devname,"r+");
   if (!stdout) {
    fprintf(stderr,"shell:unable to open stdout.%s:%s\n",devname,strerror(errno));
   };
   stderr=fopen(devname,"r+");
   if (!stderr) {
    printf("shell:unable to open stderr.%s:%s\n",devname,strerror(errno));
   };
   /* when the future user environment runs ok
    * a freopen() reopens the terminals. Now this don't work
    * (sorry but you can't use because FILENO_STDIN!=0. Better fileno(stdin))
    */
  };
  shell_add_cmd(NULL,NULL,NULL,NULL); /* init the chain list*/
  strcpy(cmd,"");
  printf("\n"
         "RTEMS-SHELL:%s. "__DATE__". 'help' to list commands.\n",devname);
  curdir=malloc(1024); 
  chdir("/");
  for (;;) {
   /* Prompt section */	  
   /* XXX: show_prompt user adjustable */
   getcwd(curdir,1024);
   printf("%s [%s] # ",devname,curdir);
   /* getcmd section */	  
   if (!shell_scanline(cmd,sizeof(cmd),stdin,stdout)) {
    printf("shell:unable scanline(%s)\n",devname);	
    break;
   };
   /* evaluate cmd section */	  
   if (!strcmp(cmd,"r")) {  /* repeat last command, forced, not automatic */
    strcpy(cmd,last_cmd);
   } else 
   if (strcmp(cmd,"")) { /* only for get a new prompt */
    strcpy(last_cmd,cmd);
   }; 
   /* exec cmd section */	  
   /* TODO: 
    *  To avoid user crash catch the signals. 
    *  Open a new stdio files with posibility of redirection *
    *  Run in a new shell task background. (unix &)
    *  Resuming. A little bash.
    */	  
   if (shell_make_args(cmd,&argc,argv)) {
    if ((shell_cmd=shell_lookup_cmd(argv[0]))!=NULL) {
     current_shell_env->errorlevel=shell_cmd->command(argc,argv);
    } else {
     printf("shell:%s command not found\n",argv[0]);
     current_shell_env->errorlevel=-1;
    };
   };
   /* end exec cmd section */	  
  };
  free(curdir);
  rtems_task_delete(RTEMS_SELF);
}
/* ----------------------------------------------- */
rtems_status_code   shell_init (char * task_name,
				rtems_unsigned32    task_stacksize,
				rtems_task_priority task_priority,
				char * devname,
				tcflag_t tcflag) {
 rtems_id task_id;
 rtems_status_code sc;
 shell_env_t * shell_env;
 sc=rtems_task_create(new_rtems_name(task_name),
 		     task_priority,
		     task_stacksize?task_stacksize:RTEMS_MINIMUM_STACK_SIZE,
		     (RTEMS_DEFAULT_MODES&~RTEMS_ASR_MASK)|RTEMS_ASR,
		     RTEMS_DEFAULT_ATTRIBUTES,
		     &task_id);
 if (sc!=RTEMS_SUCCESSFUL) {
  rtems_error(sc,"creating task %s in shell_init()",task_name);	 
  return sc;
 }; 
 shell_env=malloc(sizeof(shell_env_t));
 if (!shell_env) {
  rtems_task_delete(task_id);
  sc=RTEMS_NO_MEMORY;	 
  rtems_error(sc,"allocating shell_env %s in shell_init()",task_name);	 
  return sc;
 };
 if (global_shell_env.magic!=new_rtems_name("SENV")) {
  global_shell_env.magic   =new_rtems_name("SENV");
  global_shell_env.devname ="/dev/console";
  global_shell_env.taskname="GLOBAL";
  global_shell_env.tcflag   =0;
 }; 
 shell_env->magic   =global_shell_env.magic; 
 shell_env->devname =devname;
 shell_env->taskname=task_name;
 shell_env->tcflag  =tcflag;
 return rtems_task_start(task_id,shell_shell,(rtems_task_argument) shell_env);
}
/* ----------------------------------------------- */