ACQ2XX_API
acq2sh.cpp
Go to the documentation of this file.
00001 /* ------------------------------------------------------------------------- */
00002 /* file acq2sh.cpp                                                           */
00003 /* ------------------------------------------------------------------------- */
00004 /*   Copyright (C) 2008 Peter Milne, D-TACQ Solutions Ltd
00005  *                      <Peter dot Milne at D hyphen TACQ dot com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of Version 2 of the GNU General Public License
00009     as published by the Free Software Foundation;
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
00019 /* ------------------------------------------------------------------------- */
00020 
00021 /** @file acq2sh.cpp implementation of acq2sh "remote shell" using API.
00022  * acq2sh can run any shell command on the target device
00023  * WARNING: output is limited to 4096 bytes, for longer outputs, consider
00024  * saving to a file, then transferring the file
00025  */
00026 
00027 #include "local.h"
00028 #include "acq2xx_api.h"
00029 #include "acq_transport.h"
00030 
00031 #include "popt.h"
00032 
00033 int acq200_debug;
00034 char *device;
00035 poptContext opt_context;
00036 
00037 #define MAXBUF 4096
00038 char command[MAXBUF] = {};
00039 char response[MAXBUF] = {};
00040 
00041 
00042 int main(int argc, const char** argv)
00043 {
00044         struct poptOption opt_table[] = {
00045                 { "url",        'u', POPT_ARG_STRING, &device,     'u' },
00046                 { "device",     'f', POPT_ARG_STRING, &device,     'f' },
00047                 { "board",      'b', POPT_ARG_STRING, &device,      'b' },
00048                 { "shell",      's', POPT_ARG_STRING, &device,      's' },
00049                 { "verbose",    'v', POPT_ARG_INT,    &acq200_debug,    0   },
00050                 { "version",    0,   POPT_ARG_NONE,   0,           'V' },
00051                 POPT_AUTOHELP
00052                 POPT_TABLEEND           
00053         };
00054         int rc;
00055 
00056         opt_context = poptGetContext( argv[0], argc, argv, opt_table, 0 );
00057         while ( (rc = poptGetNextOpt( opt_context )) > 0 ){
00058                 switch( rc ){
00059                         ;
00060                 }
00061         }
00062 
00063         if (device == 0){
00064                 err("usage acq2sh -f DEVICE command ...");
00065                 return -1;
00066         }
00067         Acq2xx card(Transport::getTransport(device));
00068 
00069         if (poptPeekArg(opt_context)){  
00070                 for (int first = 1; poptPeekArg(opt_context); first = 0){
00071                         if (!first){
00072                                 strcat(command, " ");
00073                         }
00074                         strcat(command, poptGetArg(opt_context));
00075                 }
00076 
00077                 int rc = card.getTransport()->acq2sh(command, response, MAXBUF);
00078 
00079                 puts(response);
00080                 if (STATUS_IS_OK(rc)){
00081                         return 0;
00082                 }else{
00083                         exit(rc);
00084                 }       
00085         }else{          
00086                 for (int line = 0; fgets(command, MAXBUF, stdin); ++line){
00087                         printf(command);
00088                         if (command[0] == '#' || strlen(command) < 2){
00089                                 continue;
00090                         }else{
00091                                 rc = card.getTransport()->acq2sh(
00092                                                 command, response, MAXBUF);
00093                                 puts(response);
00094                                 if (rc < 0){
00095                                         return rc;
00096                                 }
00097                         }
00098                 }
00099                 return 0;
00100         }
00101 }