#include "stdinc.h" #include "BufPkr.h" using namespace std; BufPkr::BufPkr(unsigned int sz_in) { cerr << "BufPkr called\n"; sz = sz_in; try { buf = new u_char[sz]; line = new char[2*sz]; doit( ); } catch (bad_alloc) { cerr << "*** BufPkr: Memory exhausted ***\n"; exit(1); } } int BufPkr::get_code( char *keyword ) { if( strcmp(keyword, "spawn" ) == 0 ) return 1; else if( strcmp(keyword, "wait" ) == 0 ) return 2; else if( strcmp(keyword, "send" ) == 0 ) return 3; else return -1; } void BufPkr::doit( void ) { const char *WS = " \t\n"; int code; while ( cin.getline(line, sz)) { char *word[sz/sizeof(char *)]; char *p = line; int nwords = 0; if (cin.gcount() == (sz-1)) { cerr << "*** Ooops, line is full.\n"; exit(1); } cout << line << endl; memset(buf, '\0', sz); nwords = 0; while ( (word[nwords] = strtok(p, WS)) != NULL) { //XXX cout << "\t" << word[nwords] << endl; ++nwords; p = NULL; } code = get_code( word[0] ); if( code != -1 ) { pack( (u_long) code, nwords-1, &word[1] ); fflush(stdout); printHex(code, nwords-1); } if (strcmp(word[0], "quit") == 0) break; } } void BufPkr::pack( u_long code, int nwords, char *word[] ) { u_char **str_ptr = (u_char **) buf; u_char *str; *((int *)buf) = code; str_ptr = (u_char **) (buf+sizeof(code)); str = buf + sizeof(code) + (nwords+1)*sizeof(char *); for (int i=0; i