Inferno  0.2
read_args.cpp
Go to the documentation of this file.
00001 
00002 #include "read_args.hpp"
00003 #include <string.h>
00004 #include <string>
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 
00008 std::string ReadArgs::exename;
00009 std::string ReadArgs::infile;
00010 std::string ReadArgs::outfile;
00011 bool ReadArgs::intermediate_graph = false;
00012 int ReadArgs::pattern_graph = -1; // -1 disables
00013 bool ReadArgs::trace = false;
00014 bool ReadArgs::trace_hits = false;
00015 std::string ReadArgs::hits_format;
00016 bool ReadArgs::selftest = false;
00017 int ReadArgs::runonlystep = 0; 
00018 bool ReadArgs::runonlyenable = false; 
00019 int ReadArgs::quitafter = 0x7fffffff; // basically never
00020 bool ReadArgs::quitenable = false;
00021 int ReadArgs::repetitions = 100; // default behaviour
00022 bool ReadArgs::rep_error = true; // default behaviour
00023 bool ReadArgs::assert_pedigree = false;
00024 bool ReadArgs::documentation_graphs = false;
00025 
00026 void ReadArgs::Usage()
00027 {
00028     fprintf(stderr, "Usage:\n"
00029                 "%s [-i<infile>] [-o<outfile>] [-t] [-s] [-g<opts>] [-q<n>] \n"
00030                 "\n"
00031                 "-i<infile>  Read input program (C/C++) from <infile>.\n"
00032                 "-o<outfile> Write output program to <outfile>. C/C++ by default. Writes to stdout if omitted.\n"
00033                 "-t          Turn on tracing internals (very verbose).\n"               
00034                     "-th<fmt>    Dump hit counts at the end of execution based on <fmt>.\n"
00035                     "            Note: use -th? for help on <fmt>.\n"
00036                 "-s          Run self-tests.\n"
00037                 "-ap         Enable pedigree assertions in search and replace engine.\n"
00038                     "-q<n>       Stop before step <n>. <n> may be 0 to exercise just parser and renderer.\n"    
00039                     "            Note: -q<n> makes -t and -r operate only on step n-1.\n"                
00040                     "-n<n>       Only run step <n>. User must ensure input program meets any restrictions of the step.\n"                    
00041                   "-gi         Generate Graphviz dot file for output or intermediate if used with -q.\n"
00042                   "-gp<n>      Generate dot file for specified transformation step n.\n"
00043                   "-gd         Generate dot files for documentation; -o specifies directory.\n"
00044                     "-rn<n>      Stop search and replace after n repetitions and do not generate an error.\n"
00045                     "-re<n>      Stop search and replace after n repetitions and do generate an error.\n"
00046                     "\n"
00047                 "One of -i, -s, -th, -gp, -gd required; all others are optional.\n",
00048                 exename.c_str() );
00049     exit(1);
00050 }
00051 
00052 std::string ReadArgs::GetArg( int al )
00053 {
00054     if( strlen(argv[curarg]) > al+1 )
00055     {
00056         return std::string( argv[curarg]+al+1 );
00057     }
00058     else
00059     {
00060         curarg++;
00061         if(curarg >= argc)
00062             Usage();
00063         return std::string( argv[curarg] );
00064     }    
00065 }
00066 
00067 ReadArgs::ReadArgs( int ac, char *av[] )
00068 { 
00069     argc = ac;
00070     argv = av;
00071   exename = argv[0];
00072     for( curarg=1; curarg<argc; curarg++ )
00073     {
00074       if( argv[curarg][0] != '-' || ((std::string)(argv[curarg])).size()<2 )
00075         Usage();
00076 
00077         char option = argv[curarg][1];
00078         
00079         if( option=='i' )
00080         {
00081             infile = GetArg();
00082         }
00083         else if( option=='o' )
00084         {
00085             outfile = GetArg();
00086         }
00087         else if( option=='t' )
00088         {
00089             char option2 = argv[curarg][2];
00090             if( option2=='\0' )
00091                 trace = true;
00092             else if( option2=='h' )
00093             {                
00094                 trace_hits = true;
00095                 hits_format = GetArg(2);
00096             }
00097             else
00098                 Usage();
00099         }
00100         else if( option=='g' )
00101         {
00102             char option2 = argv[curarg][2];
00103             if( option2=='i' )
00104                 intermediate_graph = true;
00105             else if( option2=='p' )
00106                 pattern_graph = strtoul( GetArg(2).c_str(), NULL, 10 );
00107             else if( option2=='d' )
00108                 documentation_graphs = true;
00109             else
00110                 Usage();
00111         }
00112         else if( option=='r' )
00113         {
00114             char option2 = argv[curarg][2];
00115             if( option2=='e' )
00116                 rep_error = true;
00117             else if( option2=='n' )
00118                 rep_error = false;
00119             else
00120                 Usage();
00121             repetitions = strtoul( GetArg(2).c_str(), NULL, 10 );
00122         }
00123         else if( option=='s' )
00124         {
00125             selftest = true;
00126         }
00127         else if( option=='a' )
00128         {
00129             char option2 = argv[curarg][2];
00130             if( option2=='p' )
00131             {                
00132                 assert_pedigree = true;
00133             }
00134             else
00135                 Usage();
00136         }
00137         else if( option=='q' )
00138         {
00139           quitafter = strtoul( GetArg().c_str(), NULL, 10 );
00140           quitenable = true;
00141         }
00142         else if( option=='n' )
00143         {
00144           runonlystep = strtoul( GetArg().c_str(), NULL, 10 );
00145           runonlyenable = true;
00146         }
00147         else 
00148         {
00149             Usage();
00150         }
00151     }    
00152     
00153     // infile or selftest is always required
00154     if( infile.empty() && 
00155         !selftest && 
00156         pattern_graph==-1 && 
00157         !(trace_hits && hits_format==std::string("?") ) &&
00158         !documentation_graphs )
00159         Usage();
00160 }