include <grammar/scanner.h> include <assert.h> include <stdio.h> include <ctype.h> include <string.h> include <fstream.h> include <sys/param.h> include <unistd.h> extern "C" char *getwd(char*); //extern "C" char *chdir(char*); //extern "C" void exit(int); static char _prefix[1024] = ""; static char hush_local_search_dirs[24][256]; static int ndir=0; static char root[BUFSIZ]; static int nroot=0; static int the_paths( int argc, char argv[24][256]) //static int the_paths( int argc, char** argv) { char c; char wd[512]; strcpy(hush_local_search_dirs[ndir++],"."); //cerr << "Sofar ... " << endl; getwd(wd); strcpy(root,wd); //cerr << "Sofar ... " << endl; while ( (argc > 0 ) && ( argv[0][0] == '-' ) ) { //cerr << "Checking for paths " << argv[0] << endl; c = argv[0][1]; switch(c) { case 'I': strcpy(hush_local_search_dirs[ndir++],argv[0]+2); //cerr << argv[0] << endl; break; case 'R': strcpy(root,argv[0]+2); break; default: // cerr << "No option: " << c << endl; break; } argc--; argv++; } chdir(root); getwd(wd); cerr << "ROOT: " << root << endl; return 0; } static ifstream* find(char* s, char* ext="") { ifstream *is; int i=0; char fs[BUFSIZ]; sprintf(fs,"%s%s",s,ext); is = new ifstream(); is->open(fs,ios::in); int fp = !is->fail(); if (!fp) while ( i < ndir ) { fs[0]='\0'; if (s[0]=='/' || s[0]=='.' || s[0]=='~' ) { sprintf(fs,"%s%s",s,ext); i=ndir; } else sprintf(fs,"%s/%s%s",hush_local_search_dirs[i],s,ext); is->open(fs,ios::in); fp = !is->fail(); if (fp) i=ndir; else i++; } //if (fp) cerr << "FILE: " << fs << endl; if (is->fail()) cerr << "WARNING: " << fs << " DOES NOT EXIST" << endl; //assert(!is->fail()); return is; }
class lex_file { ifstream* is; char name[BUFSIZ]; public: inline lex_file(char *s, char* ext ) : is ( find( s, ext ) ) { strcpy(name,s); } inline ~lex_file() { is->close(); //cerr << "Closing:" << name << endl; if (is) delete is; } istream* stream() { return is; } inline void process(iter<term>* l) { (*l)(); } };
class lex_string { istrstream* is; public: inline lex_string(char *s ) { is = new istrstream(s); } inline ~lex_string() { //is->close(); //cerr << "Closing:" << name << endl; if (is) delete is; } istream* stream() { return is; } inline void process(iter<term>* l) { (*l)(); } };
static inline int separator(char c) { return c == '/' || c == '~'; } static inline char* strip(char* s) { char *b = s; char *p = b; while (*p != '\0') p++; p--; while ( p != b && !separator(*p) ) p--; if ( p != b ) p++; return p; } static inline void prefix(char* s) { //strcpy(_prefix,s); sprintf(_prefix,"%s%s/",_prefix,s); /* char *b = _prefix; char *p = b; while (*p != '\0') p++; p--; while ( p != b && !separator(*p) ) p--; if ( p != b ) p++; *p = '\0'; */ } inline char* prefix() { return _prefix[0]?_prefix:""; }
class lex_directory { char name[BUFSIZ]; char ext[8]; char wd[BUFSIZ]; char xd[BUFSIZ]; public: inline lex_directory(char* s, char* x = "") { strcpy(name,s); strcpy(ext,x); getwd(wd); chdir(s); getwd(xd); cerr << "CWD: " << xd << endl; } inline ~lex_directory() { chdir(wd); } inline istream* stream() { lex_file f( strip(name), ext ); return f.stream(); } inline void process(iter<term>* l) { (*l)(); } };
static int unflatten(char* in, char argv[12][80]) { char* b = in; while (*b == ' ') b++; char* p = b; int i = 0; while ( *p != '\0' ) { i++; while ( *p != '\0' && *p != ' ' ) p++; if ( *p == '\0' ) { strcpy(argv[i],b); return i; } else { *p = '\0'; strcpy(argv[i],b); p++; b = p; } } return i; } ifdef NAME ifndef input_lex define input_lex NAME endif endif
class input_lex : public lex_scanner { protected: iter<term>* _yacc; int so; int cd; int comment; inline void unso() { so = 0; cd = 0; comment = 0; } //int argc; //char argv[12][80]; public: inline input_lex(istream* in = &cin, ostream* out = &cout ) : lex_scanner(in,out) { unso(); _yacc = 0; } //~lex() { in << endl; } term* operator()(); virtual inline void process(istream* xin) { input_lex l(xin); l(); } inline void paths(int argc, char argv[24][256]) { //void paths(int argc, char** argv) { //cerr << "Calling paths" << endl; ::the_paths(argc,argv); //cerr << "Called paths" << endl; } inline void files(char* s, char* ext) { int argc=0; char argv[12][80]; cerr << "FILES: " << s << endl; argc = unflatten(s,argv); for( int i = 1; i <= argc; i++ ) { //cerr << "Reading:" << argv[i] << "***" << endl; //(*yyout) << endl; soinc(argv[i],ext); //(*yyout) << endl; //cerr << "end reading:" << argv[i] << "***" << endl; } } inline void directories(char* s, char* ext, char* f = 0) { int argc=0; char argv[12][80]; //cerr << "DIRS: " << s << endl; argc = unflatten(s,argv); for( int i = 1; i <= argc; i++ ) { //cerr << "reading " << argv[i]; char buf[1024]; strcpy(buf,_prefix); cerr << "ORIG PREFIX " << _prefix << endl; cdinc(argv[i],f,ext); strcpy(_prefix,buf); cerr << "RESTORING PREFIX " << _prefix << endl; } } inline void strinc(char* s) { lex_string f(s); istream* xin = f.stream(); this->process(xin); } inline void soinc(char* s, char* ext = "") { lex_file f(s,ext); istream* xin = f.stream(); this->process(xin); } inline void cdinc(char* s, char* f = 0, char* ext = "") { if (f && !strcmp(f,"")) f = 0; (*yyout) << endl; prefix(s); cerr << "PREFIX " << _prefix << endl; lex_directory x(s,ext); char buf[1024]; sprintf(buf,"%s",f?f:strip(s)); //cerr << "Getting " << buf << endl; soinc( buf, ext); //soinc( strip(s), ext); //istream* xin = f.stream(); //this->process(xin); } };
Hush Online Technology
hush@cs.vu.nl
11/03/98 |
![]() |
![]() |