]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control-pxmc.git/blob - src/libs4c/cmdproc/cmdio_std_file.c
Raspberry Pi 3-phase motor control educational system based on PXMC.
[fpga/rpi-motor-control-pxmc.git] / src / libs4c / cmdproc / cmdio_std_file.c
1 /*******************************************************************
2   Components for embedded applications builded for
3   laboratory and medical instruments firmware  
4  
5   cmdio_std_file.c - interconnection of text command processor
6                   with generic file interface
7  
8   Copyright (C) 2001-2009 by Pavel Pisa pisa@cmp.felk.cvut.cz
9             (C) 2002-2009 by PiKRON Ltd. http://www.pikron.com
10             (C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
11
12   This file can be used and copied according to next
13   license alternatives
14    - MPL - Mozilla Public License
15    - GPL - GNU Public License
16    - other license provided by project originators
17
18  *******************************************************************/
19
20 #include <ctype.h>
21 #include <malloc.h>
22 #include <string.h>
23 #include <cmd_proc.h>
24 #include <stdio.h>
25
26 cmd_des_t const **cmd_forshell;
27
28 static int cmd_io_putc_forfile(struct cmd_io *cmd_io,int ch)
29 {
30   int res=fputc(ch,cmd_io->priv.file.out);
31   res=(res==EOF)?-1:0;
32   return res;
33 }
34
35 static int cmd_io_getc_forfile(struct cmd_io *cmd_io)
36 {
37   int ch=fgetc(cmd_io->priv.file.in);
38   if(ch==EOF) ch=-1;
39   return ch;
40 }
41
42 static  int cmd_io_write_forfile(struct cmd_io *cmd_io,const void *buf,int count)
43 {
44   int res=fwrite(buf, 1, count, cmd_io->priv.file.out);
45   return res;
46 }
47
48 static  int cmd_io_read_forfile(struct cmd_io *cmd_io,void *buf,int count)
49 {
50   int res=fread(buf, 1, count, cmd_io->priv.file.in);
51   return res;
52 }
53
54 int cmd_proc_forfile(FILE * in,FILE * out, cmd_des_t const **des_arr, char *line)
55 {
56   cmd_io_t cmd_io;
57   
58   cmd_io.putc=cmd_io_putc_forfile;
59   cmd_io.getc=cmd_io_getc_forfile;
60   cmd_io.write=cmd_io_write_forfile;
61   cmd_io.read=cmd_io_read_forfile;
62   cmd_io.priv.file.in=in;
63   cmd_io.priv.file.out=out;
64
65   return proc_cmd_line(&cmd_io, des_arr, line);
66 }
67
68 int cmd_proc_forshell (int argc, char **argv)
69 {
70   int i;
71   int res;
72   size_t len;
73   char *line, *p;
74   
75   if(argc<2){
76     cmd_proc_forfile(stdin, stdout, cmd_forshell, "help");
77     printf("\n");
78     return 1;
79   }
80   
81   for(len=0, i=1; i<argc; i++)
82     len+=strlen(argv[i])+1;
83
84   line=malloc(len);
85   if(!line)
86     return 3;
87   
88   for(p=line, i=1; i<argc; i++) {
89     len=strlen(argv[i]);
90     memcpy(p,argv[i],len);
91     p+=len;
92     *(p++)=' ';
93   }
94   *(--p)=0;
95
96   res=cmd_proc_forfile(stdin, stdout, cmd_forshell, line);
97   printf("\n");
98
99   if(res<0) {
100     printf("cmdproc \"%s\" failed with error %d\n",line,res);
101     res=2;
102   } else res=0;
103   
104   free(line);
105   
106   return res;
107
108 }