| [7d4f6ed] | 1 | // 
 | 
|---|
| [ae47a23] | 2 | // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
 | 
|---|
| [7d4f6ed] | 3 | //
 | 
|---|
 | 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
 | 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
 | 6 | // 
 | 
|---|
 | 7 | // main.cc -- 
 | 
|---|
 | 8 | // 
 | 
|---|
 | 9 | // Author           : Peter A. Buhr
 | 
|---|
 | 10 | // Created On       : Wed Jun 28 22:57:26 2017
 | 
|---|
 | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [ae47a23] | 12 | // Last Modified On : Thu Jun 29 13:09:32 2017
 | 
|---|
 | 13 | // Update Count     : 58
 | 
|---|
| [7d4f6ed] | 14 | // 
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include <iostream>
 | 
|---|
| [c9383ee] | 17 | #include <fstream>
 | 
|---|
| [7d4f6ed] | 18 | #include <string>
 | 
|---|
 | 19 | using namespace std;
 | 
|---|
| [c9383ee] | 20 | #include <unistd.h>                                                                             // close
 | 
|---|
| [ae47a23] | 21 | #include <getopt.h>                                                                             // getopt
 | 
|---|
| [fda8168] | 22 | #include "filter.h"
 | 
|---|
 | 23 | 
 | 
|---|
| [c9383ee] | 24 | extern FILE * yyin;
 | 
|---|
| [fda8168] | 25 | extern int yydebug;
 | 
|---|
 | 26 | extern int yyparse( void );
 | 
|---|
 | 27 | 
 | 
|---|
| [ae47a23] | 28 | bool parse_cmdline( int argc, char * argv[] ) {
 | 
|---|
 | 29 |         enum { Html, Identity, Latex, Nocode, ParseTree, };
 | 
|---|
| [c9383ee] | 30 | 
 | 
|---|
| [ae47a23] | 31 |         static struct option long_opts[] = {
 | 
|---|
 | 32 |                 { "html", no_argument, nullptr, Html },
 | 
|---|
 | 33 |                 { "identity", no_argument, nullptr, Identity },
 | 
|---|
 | 34 |                 { "latex", no_argument, nullptr, Latex },
 | 
|---|
 | 35 |                 { "nocode", no_argument, nullptr, Nocode },
 | 
|---|
 | 36 |                 { "parse-tree", no_argument, nullptr, ParseTree },
 | 
|---|
 | 37 |                 { nullptr, 0, nullptr, 0 }
 | 
|---|
 | 38 |         }; // long_opts
 | 
|---|
 | 39 |         int long_index;
 | 
|---|
| [c9383ee] | 40 | 
 | 
|---|
| [ae47a23] | 41 |         opterr = 0;                                                                                     // (global) prevent getopt from printing error messages
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 |         int c;
 | 
|---|
 | 44 |         while ( (c = getopt_long( argc, argv, "hilnp", long_opts, &long_index )) != -1 ) {
 | 
|---|
 | 45 |                 switch ( c ) {
 | 
|---|
 | 46 |                   case Html:
 | 
|---|
 | 47 |                   case 'h':
 | 
|---|
 | 48 |                         filter = HTML;
 | 
|---|
 | 49 |                         break;
 | 
|---|
 | 50 |                   case Identity:
 | 
|---|
 | 51 |                   case 'i':
 | 
|---|
 | 52 |                         filter = ::Identity;
 | 
|---|
 | 53 |                         break;
 | 
|---|
 | 54 |                   case Latex:
 | 
|---|
 | 55 |                   case 'l':
 | 
|---|
 | 56 |                         filter = LaTeX;
 | 
|---|
 | 57 |                         break;
 | 
|---|
 | 58 |                   case Nocode:
 | 
|---|
 | 59 |                   case 'n':
 | 
|---|
 | 60 |                         filter = ::Nocode;
 | 
|---|
 | 61 |                         break;
 | 
|---|
 | 62 |                   case ParseTree:
 | 
|---|
 | 63 |                   case 'p':
 | 
|---|
 | 64 |                         filter = Parse_Tree;
 | 
|---|
 | 65 |                   case '?':
 | 
|---|
 | 66 |                         if ( optopt ) {                                                         // short option ?
 | 
|---|
 | 67 |                                 cerr << "Unknown option: -" << (char)optopt << endl;
 | 
|---|
 | 68 |                         } else {                                                                        // long option
 | 
|---|
 | 69 |                                 cerr << "Unknown option: " << argv[optind - 1] << endl;
 | 
|---|
 | 70 |                         } // if
 | 
|---|
 | 71 |                         return false;
 | 
|---|
| [c9383ee] | 72 |                   default:
 | 
|---|
| [ae47a23] | 73 |                         abort();
 | 
|---|
| [c9383ee] | 74 |                 } // switch
 | 
|---|
| [ae47a23] | 75 |         } // while
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |         if ( optind != argc ) {                                                         // input files ?
 | 
|---|
 | 78 |                 if ( optind == argc - 1 ) {                                             // any commands after the flags ? => input file name
 | 
|---|
 | 79 |                         yyin = fopen( argv[ optind ], "r" );
 | 
|---|
 | 80 |                         if ( yyin == nullptr ) {
 | 
|---|
 | 81 |                                 cerr << "Open failure for input file \"" << argv[ optind ] << "\"" << endl;
 | 
|---|
 | 82 |                                 return false;
 | 
|---|
 | 83 |                         } // if
 | 
|---|
 | 84 |                 } else {
 | 
|---|
 | 85 |                         cerr << "Too many input files " << argv[ optind + 1 ] << endl;
 | 
|---|
 | 86 |                         return false;
 | 
|---|
 | 87 |                 } // if
 | 
|---|
 | 88 |         } // if
 | 
|---|
 | 89 |         return true;
 | 
|---|
 | 90 | } // parse_cmdline
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 | int main( int argc, char *argv[] ) {
 | 
|---|
 | 93 |         yyin = stdin;                                                                           // defaults
 | 
|---|
 | 94 |         filter = Nocode;
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 |         if ( ! parse_cmdline( argc, argv ) ) {
 | 
|---|
 | 97 |                 cerr << "Usage: " << argv[0]
 | 
|---|
 | 98 |                          << " ["
 | 
|---|
 | 99 |                          << "-h/--html | "
 | 
|---|
 | 100 |                          << "-i/--identity | "
 | 
|---|
 | 101 |                          << "-l/--latex | "
 | 
|---|
 | 102 |                          << "-n/--nocode | "
 | 
|---|
 | 103 |                          << "-p/--parse-tree"
 | 
|---|
| [c9383ee] | 104 |                          << "] [input-file]"
 | 
|---|
 | 105 |                          << endl;
 | 
|---|
 | 106 |                 exit( EXIT_FAILURE );                                                   // TERMINATE
 | 
|---|
| [ae47a23] | 107 |         } // if
 | 
|---|
| [fda8168] | 108 | 
 | 
|---|
| [7d4f6ed] | 109 |         //yydebug = 1;
 | 
|---|
 | 110 |         yyparse();
 | 
|---|
| [c9383ee] | 111 | 
 | 
|---|
 | 112 |         if ( yyin != stdin ) fclose( yyin );                            // close file, do not delete cin!
 | 
|---|
 | 113 | } // main
 | 
|---|
| [fda8168] | 114 | 
 | 
|---|
 | 115 | // Local Variables: //
 | 
|---|
| [7d4f6ed] | 116 | // mode: c++ //
 | 
|---|
 | 117 | // tab-width: 4 //
 | 
|---|
 | 118 | // compile-command: "make install" //
 | 
|---|
| [fda8168] | 119 | // End: //
 | 
|---|