rllib  1
rlcommandlineinterface.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           rlcommandlineinterface.cpp  -  description
00003                              -------------------
00004     begin                : Sat Mar 27 2010
00005     copyright            : (C) 2010 by Rainer Lehrig
00006     email                : lehrig@t-online.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as        *
00013  *   published by the Free Software Foundation                             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #include "rlcommandlineinterface.h"
00017 #include "rlstring.h"
00018 #include "rlcutil.h"
00019 #include "rlthread.h"
00020 #include <unistd.h>
00021 #include <stdarg.h>
00022 
00023 rlCommandlineInterface::rlCommandlineInterface()
00024 {
00025   sock = NULL;
00026   spawn = NULL;
00027   tty = NULL;
00028 }
00029 
00030 rlCommandlineInterface::~rlCommandlineInterface()
00031 {
00032   if(sock != NULL) delete sock;
00033   if(spawn != NULL) delete spawn;
00034 }
00035 
00036 int rlCommandlineInterface::start(const char *how, const char *command)
00037 {
00038   if(sock != NULL) delete sock;
00039   sock = NULL;
00040   if(spawn != NULL) delete spawn;
00041   spawn = NULL;
00042   tty = NULL;
00043 
00044   if(strcmp(how,"pipe") == 0)
00045   {
00046     if(command == NULL) return -1;
00047     spawn = new rlSpawn();
00048     int ret = spawn->spawn(command);
00049     if(ret < 0)
00050     {
00051       delete spawn;
00052       spawn = NULL;
00053       return -1;
00054     }
00055     return ret;
00056   }
00057   else if(strcmp(how,"stdio") == 0)
00058   {
00059     return 1;
00060   }
00061   else
00062   {
00063     rlString rlhow(how);
00064     char *cptr, *host;
00065     host = rlhow.text();
00066     cptr = strchr(host,':');
00067     if(cptr == NULL) return -1;
00068     *cptr = '\0'; cptr++;
00069     int port = atoi(cptr);
00070     if(strcmp(host,"server.localhost") == 0)
00071     {
00072       sock = new rlSocket(host,port,0);
00073     }
00074     else
00075     {
00076       if(strcmp(host,"localhost") == 0 && command != NULL)
00077       {
00078         rlString cmd(command);
00079 #ifdef RLUNIX
00080         cmd += " &";
00081 #endif
00082         rlsystem(cmd.text());
00083       }
00084       sock = new rlSocket(host,port,1);
00085     }
00086     for(int itry=0; itry<10; itry++)
00087     {
00088       sock->connect();
00089       if(sock->isConnected()) return sock->s;
00090       rlsleep(10);
00091     }  
00092     return -1;
00093   }
00094 }
00095 
00096 int rlCommandlineInterface::start(rlSerial *ttyinterface)
00097 {
00098   if(sock != NULL) delete sock;
00099   sock = NULL;
00100   if(spawn != NULL) delete spawn;
00101   spawn = NULL;
00102   tty = ttyinterface;
00103   if(tty == NULL) return -1;
00104   return 1;
00105 }
00106 
00107 int rlCommandlineInterface::readBlock(void *buf, int len, int timeout)
00108 {
00109   if(spawn != NULL)
00110   {
00111     unsigned char *cbuf = (unsigned char *) buf;
00112     int i = 0;
00113     while(i<len)
00114     {
00115       if(timeout > 0)
00116       {
00117         if(spawn->select(timeout) == 0) return -1;
00118       }
00119       cbuf[i++] = spawn->getchar();
00120     }  
00121     return len;
00122   }
00123   else if(sock != NULL)
00124   {
00125     if(sock->isConnected() == 0) return -1;
00126     int ret = sock->read(buf,len,timeout);
00127     if(ret <= 0) 
00128     {
00129       sock->disconnect();
00130       return -1;
00131     }
00132     return len; 
00133   }
00134   else if(tty != NULL)
00135   {
00136     int timout = 0;
00137     if(timeout > 0) timout = timeout;
00138     return tty->readBlock ((unsigned char *) buf, len, timout);
00139   }
00140   else
00141   {
00142    int ret = read(0,buf,len);
00143    return ret;
00144   }
00145 }
00146 
00147 const char *rlCommandlineInterface::readLine(int timeout)
00148 {
00149   if(spawn != NULL)
00150   {
00151     if(timeout > 0)
00152     {
00153       if(spawn->select(timeout) == 0) return NULL;
00154     }
00155     return spawn->readLine();
00156   }
00157   else if(sock != NULL)
00158   {
00159     if(sock->isConnected() == 0) return NULL;
00160     int ret = sock->readStr(line,sizeof(line)-1,timeout);
00161     if(ret <= 0) 
00162     {
00163       sock->disconnect();
00164       return NULL;
00165     }
00166     return line; 
00167   }
00168   else if(tty != NULL)
00169   {
00170     int ret = tty->readLine((unsigned char *) line,sizeof(line)-1, timeout);
00171     if(ret <= 0) return NULL;
00172     return line;
00173   }
00174   else
00175   {
00176     return fgets(line,sizeof(line)-1,stdin);
00177   }
00178 }
00179 
00180 int rlCommandlineInterface::printf(const char *format, ...)
00181 {
00182   va_list ap;
00183   va_start(ap,format);
00184   int ret = rlvsnprintf(line, sizeof(line) - 1, format, ap);
00185   va_end(ap);
00186   if(ret < 0) return ret;
00187 
00188   if(spawn != NULL)
00189   {
00190     return spawn->printf("%s",line);
00191   }
00192   else if(sock != NULL)
00193   {
00194     if(sock->isConnected() == 0) return -1;
00195     int ret = sock->printf("%s",line);
00196    return ret;
00197   }
00198   else if(tty != NULL)
00199   {
00200     return tty->writeBlock((unsigned char *) line, strlen(line));
00201   }
00202   else
00203   {
00204    int ret = ::printf("%s",line);
00205    fflush(stdout);
00206    return ret;
00207   }
00208 }
00209 
00210 int rlCommandlineInterface::writeBlock(void *buf, int len)
00211 {
00212   if(spawn != NULL)
00213   {
00214     return spawn->write((const char *) buf, len);
00215   }
00216   else if(sock != NULL)
00217   {
00218     if(sock->isConnected() == 0) return -1;
00219     return sock->write(buf,len);
00220   }
00221   else if(tty != NULL)
00222   {
00223     return tty->writeBlock((unsigned char *) buf, len);
00224   }
00225   else
00226   {
00227     return write(1,buf,len);
00228   }
00229 }
00230 
00231 #ifdef TESTING
00232 int main()
00233 {
00234   const char *line;
00235   rlCommandlineInterface cli;
00236 
00237   // connect to a server over TCP
00238   // or be a TCP server if how = "server.localhost:port"
00239   printf("localhost:\n");
00240   cli.start("localhost:50500");
00241   int i = 0;
00242   while((line = cli.readLine()) != NULL)
00243   {
00244     printf("line=%s",line);
00245     if(i<5) cli.printf("%s%d\n","hallo",i++);
00246     else    cli.printf("%s\n","exit");
00247   }
00248 
00249   // run command and connect it over a pipe to us
00250   printf("pipe:\n");
00251   cli.start("pipe","./myecho");
00252   i = 0;
00253   while((line = cli.readLine()) != NULL)
00254   {
00255     ::printf("line=%s",line);
00256     if(i<5) cli.printf("%s%d\n","hallo",i++);
00257     else    cli.printf("%s\n","exit");
00258   }
00259 
00260   // communicate via stdin / stdout
00261   printf("stdio:\n");
00262   cli.start("stdio");
00263   cli.printf("Bitte tipp was ein\n");
00264   printf("Du hast '%s' eingegeben\n", cli.readLine());
00265 
00266   return 0;
00267 }
00268 #endif
00269 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines