/*************************************************************************** $RCSfile: cardcommander.cpp,v $ ------------------- cvs : $Id: cardcommander.cpp,v 1.6 2003/05/08 11:01:22 aquamaniac Exp $ begin : Fri Dec 13 2002 copyright : (C) 2002 by Martin Preuss email : martin@libchipcard.de **************************************************************************** * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ****************************************************************************/ #ifdef HAVE_CONFIG_H # include #endif /* Internationalization */ #ifdef HAVE_GETTEXT_ENVIRONMENT # include # include # define I18N(m) gettext(m) #else # define I18N(m) m #endif #define I18NT(m) m #include #include #include #include #include #define k_PRG "cardcommander" #define k_PRG_VERSION_INFO \ "cardcommander v0.2 (part of libchipcard v"k_CHIPCARD_VERSION_STRING")\n"\ "(c) 2003 Martin Preuss\n" \ "This program is free software licensed under GPL.\n"\ "See COPYING for details.\n" void usage(string name) { fprintf(stderr, "CardCommander - A command line too to manipulate a chip card.\n" "(c) 2003 Martin Preuss\n" "This program is free software; you can redistribute it and/or\n" "modify it under the terms of the GNU General Public\n" "License as published by the Free Software Foundation; either\n" "version 2.1 of the License, or (at your option) any later version.\n" "\n" "Usage:\n" "%s [-C CONFIGFILE]\n" " OPTIONS\n" " -C CONFIGFILE - which configuration file to use (defaults to\n" " "CHIPCARDC_CFGFILE")\n" , name.c_str()); } struct s_args { string configFile; // -C list params; }; int checkArgs(s_args &args, int argc, char **argv) { int i; string tmp; i=1; args.configFile=CHIPCARDC_CFGFILE; while (i=argc) return 1; args.configFile=argv[i]; } else if (tmp=="-h" || tmp=="--help") { usage(argv[0]); return -1; } else if (tmp=="-V" || tmp=="--version") { fprintf(stdout,k_PRG_VERSION_INFO); return -1; } else // otherwise add param args.params.push_back(tmp); i++; } // while // that's it return 0; } string dumpString(const string &s) { unsigned int i; unsigned int j; unsigned int pos; string r; pos=0; fprintf(stderr,"String size is %d:\n",s.length()); while(pos=s.length()) j=s.length(); // show hex dump for (i=pos; i trader, CTPointer &card, const string &cmd) { unsigned int i; string cm; CTError err; static string lastAPDU; CTCard *cp; // skip leading blanks i=0; while(i card; string cmdstring; char *cmd; char buffer[256]; CTPointer trader; CTError err; #ifdef HAVE_GETTEXT_ENVIRONMENT setlocale(LC_ALL,""); if (bindtextdomain("cardcommander", I18N_PATH)==0) { fprintf(stderr," Error bindtextdomain()\n"); } if (textdomain("cardcommander")==0) { fprintf(stderr," Error textdomain()\n"); } #endif rv=checkArgs(args,argc,argv); if (rv==-1) return 0; else if (rv) return rv; rv=ChipCard_Init(args.configFile.c_str(),0); if (rv!=CHIPCARD_SUCCESS) { fprintf(stderr, "Error initializing libchipcard (%d), aborting.\n",rv); return 2; } rv=0; trader=new CTCardTrader(false, 0, 0, CHIPCARD_STATUS_INSERTED, CHIPCARD_STATUS_INSERTED | CHIPCARD_STATUS_LOCKED_BY_OTHER, CHIPCARD_STATUS_LOCKED_BY_OTHER| CHIPCARD_STATUS_INSERTED); err=trader.ref().start(); if (!err.isOk()) { fprintf(stderr, I18N("Could not initialize trader")); return 2; } while(rv!=-1) { fprintf(stdout,"Card: "); cmd=fgets(buffer,sizeof(buffer),stdin); cmdstring=cmd; rv=execCommand(args, trader, card, cmdstring); } ChipCard_Fini(); return rv; }