Search in sources :

Example 1 with TextFile

use of utilMDE.TextFile in project spoon by INRIA.

the class Daikon method read_options.

// /////////////////////////////////////////////////////////////////////////
// Read in the command line options
// Return {decls, dtrace, spinfo, map} files.
protected static FileOptions read_options(String[] args, String usage) {
    if (args.length == 0) {
        System.out.println("Daikon error: no files supplied on command line.");
        System.out.println(usage);
        throw new Daikon.TerminationMessage();
    }
    // LinkedHashSet because it can be confusing to users if files (of the
    // same type) are gratuitously processed in a different order than they
    // were supplied on the command line.
    HashSet<File> decl_files = new LinkedHashSet<File>();
    HashSet<String> dtrace_files = new LinkedHashSet<String>();
    /* either file names or "-"*/
    HashSet<File> spinfo_files = new LinkedHashSet<File>();
    HashSet<File> map_files = new LinkedHashSet<File>();
    LongOpt[] longopts = new LongOpt[] { // Control output
    new LongOpt(help_SWITCH, LongOpt.NO_ARGUMENT, null, 0), new LongOpt(no_text_output_SWITCH, LongOpt.NO_ARGUMENT, null, 0), new LongOpt(format_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(show_progress_SWITCH, LongOpt.NO_ARGUMENT, null, 0), new LongOpt(no_show_progress_SWITCH, LongOpt.NO_ARGUMENT, null, 0), new LongOpt(noversion_SWITCH, LongOpt.NO_ARGUMENT, null, 0), new LongOpt(output_num_samples_SWITCH, LongOpt.NO_ARGUMENT, null, 0), new LongOpt(files_from_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(omit_from_output_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), // Control invariant detection
    new LongOpt(conf_limit_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(list_type_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(no_dataflow_hierarchy_SWITCH, LongOpt.NO_ARGUMENT, null, 0), new LongOpt(suppress_redundant_SWITCH, LongOpt.NO_ARGUMENT, null, 0), // Process only part of the trace file
    new LongOpt(ppt_regexp_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(ppt_omit_regexp_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(var_regexp_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(var_omit_regexp_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), // Configuration options
    new LongOpt(server_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(config_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(config_option_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), // Debugging
    new LongOpt(debugAll_SWITCH, LongOpt.NO_ARGUMENT, null, 0), new LongOpt(debug_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(track_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(disc_reason_SWITCH, LongOpt.REQUIRED_ARGUMENT, null, 0), new LongOpt(mem_stat_SWITCH, LongOpt.NO_ARGUMENT, null, 0) };
    Getopt g = new Getopt("daikon.Daikon", args, "ho:", longopts);
    int c;
    while ((c = g.getopt()) != -1) {
        switch(c) {
            case 0:
                // got a long option
                String option_name = longopts[g.getLongind()].getName();
                // Control output
                if (help_SWITCH.equals(option_name)) {
                    System.out.println(usage);
                    throw new Daikon.TerminationMessage();
                } else if (no_text_output_SWITCH.equals(option_name)) {
                    no_text_output = true;
                } else if (format_SWITCH.equals(option_name)) {
                    String format_name = g.getOptarg();
                    Daikon.output_format = OutputFormat.get(format_name);
                    if (Daikon.output_format == null) {
                        throw new Daikon.TerminationMessage("Unknown output format:  --format " + format_name);
                    }
                } else if (show_progress_SWITCH.equals(option_name)) {
                    show_progress = true;
                    LogHelper.setLevel("daikon.Progress", LogHelper.FINE);
                } else if (no_show_progress_SWITCH.equals(option_name)) {
                    show_progress = false;
                } else if (noversion_SWITCH.equals(option_name)) {
                    noversion_output = true;
                } else if (output_num_samples_SWITCH.equals(option_name)) {
                    output_num_samples = true;
                } else if (files_from_SWITCH.equals(option_name)) {
                    String files_from_filename = g.getOptarg();
                    try {
                        for (String filename : new TextFile(files_from_filename)) {
                            // Ignore blank lines in file.
                            if (filename.equals("")) {
                                continue;
                            }
                            // This code is duplicated below outside the options loop.
                            // These aren't "endsWith()" because there might be a suffix
                            // on the end (eg, a date, or ".gz").
                            File file = new File(filename);
                            if (!file.exists()) {
                                throw new Daikon.TerminationMessage("File " + filename + " not found.");
                            }
                            if (filename.indexOf(".decls") != -1) {
                                decl_files.add(file);
                            } else if (filename.indexOf(".dtrace") != -1) {
                                dtrace_files.add(filename);
                            } else if (filename.indexOf(".spinfo") != -1) {
                                spinfo_files.add(file);
                            } else if (filename.indexOf(".map") != -1) {
                                map_files.add(file);
                            } else {
                                throw new Daikon.TerminationMessage("Unrecognized file extension: " + filename);
                            }
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(String.format("Error reading --files_from file: %s", files_from_filename));
                    }
                    break;
                } else if (omit_from_output_SWITCH.equals(option_name)) {
                    String f = g.getOptarg();
                    for (int i = 0; i < f.length(); i++) {
                        if ("0rs".indexOf(f.charAt(i)) == -1)
                            throw new Daikon.TerminationMessage("omit_from_output flag letter '" + f.charAt(i) + "' is unknown");
                        omit_types[f.charAt(i)] = true;
                    }
                    omit_from_output = true;
                } else // Control invariant detection
                if (conf_limit_SWITCH.equals(option_name)) {
                    double limit = Double.parseDouble(g.getOptarg());
                    if ((limit < 0.0) || (limit > 1.0)) {
                        throw new Daikon.TerminationMessage(conf_limit_SWITCH + " must be between [0..1]");
                    }
                    Configuration.getInstance().apply("daikon.inv.Invariant.confidence_limit", String.valueOf(limit));
                } else if (list_type_SWITCH.equals(option_name)) {
                    try {
                        String list_type_string = g.getOptarg();
                        ProglangType.list_implementors.add(list_type_string);
                    } catch (Exception e) {
                        throw new Daikon.TerminationMessage("Problem parsing " + list_type_SWITCH + " option: " + e);
                    }
                    break;
                } else if (no_dataflow_hierarchy_SWITCH.equals(option_name)) {
                    use_dataflow_hierarchy = false;
                } else if (suppress_redundant_SWITCH.equals(option_name)) {
                    suppress_redundant_invariants_with_simplify = true;
                } else // Process only part of the trace file
                if (ppt_regexp_SWITCH.equals(option_name)) {
                    if (ppt_regexp != null)
                        throw new Daikon.TerminationMessage("multiple --" + ppt_regexp_SWITCH + " regular expressions supplied on command line");
                    String regexp_string = g.getOptarg();
                    try {
                        // System.out.println("Regexp = " + regexp_string);
                        ppt_regexp = Pattern.compile(regexp_string);
                    } catch (Exception e) {
                        throw new Daikon.TerminationMessage("Bad regexp " + regexp_string + " for " + ppt_regexp_SWITCH + ": " + e.getMessage());
                    }
                    break;
                } else if (ppt_omit_regexp_SWITCH.equals(option_name)) {
                    if (ppt_omit_regexp != null)
                        throw new Daikon.TerminationMessage("multiple --" + ppt_omit_regexp_SWITCH + " regular expressions supplied on command line");
                    String regexp_string = g.getOptarg();
                    try {
                        // System.out.println("Regexp = " + regexp_string);
                        ppt_omit_regexp = Pattern.compile(regexp_string);
                    } catch (Exception e) {
                        throw new Daikon.TerminationMessage("Bad regexp " + regexp_string + " for " + ppt_omit_regexp_SWITCH + ": " + e.getMessage());
                    }
                    break;
                } else if (var_regexp_SWITCH.equals(option_name)) {
                    if (var_regexp != null)
                        throw new Daikon.TerminationMessage("multiple --" + var_regexp_SWITCH + " regular expressions supplied on command line");
                    String regexp_string = g.getOptarg();
                    try {
                        // System.out.println("Regexp = " + regexp_string);
                        var_regexp = Pattern.compile(regexp_string);
                    } catch (Exception e) {
                        throw new Daikon.TerminationMessage("Bad regexp " + regexp_string + " for " + var_regexp_SWITCH + ": " + e.getMessage());
                    }
                    break;
                } else if (var_omit_regexp_SWITCH.equals(option_name)) {
                    if (var_omit_regexp != null)
                        throw new Daikon.TerminationMessage("multiple --" + var_omit_regexp_SWITCH + " regular expressions supplied on command line");
                    String regexp_string = g.getOptarg();
                    try {
                        // System.out.println("Regexp = " + regexp_string);
                        var_omit_regexp = Pattern.compile(regexp_string);
                    } catch (Exception e) {
                        throw new Daikon.TerminationMessage("Bad regexp " + regexp_string + " for " + var_omit_regexp_SWITCH + ": " + e.getMessage());
                    }
                    break;
                } else if (server_SWITCH.equals(option_name)) {
                    String input_dir = g.getOptarg();
                    server_dir = new File(input_dir);
                    if (!server_dir.isDirectory() || !server_dir.canRead() || !server_dir.canWrite())
                        throw new RuntimeException("Could not open config file in server directory " + server_dir);
                    break;
                // Configuration options
                } else if (config_SWITCH.equals(option_name)) {
                    String config_file = g.getOptarg();
                    try {
                        InputStream stream = new FileInputStream(config_file);
                        Configuration.getInstance().apply(stream);
                    } catch (IOException e) {
                        throw new Daikon.TerminationMessage(// Is this the only possible reason for an IOException?
                        "Could not open config file " + config_file);
                    }
                    break;
                } else if (config_option_SWITCH.equals(option_name)) {
                    String item = g.getOptarg();
                    try {
                        Configuration.getInstance().apply(item);
                    } catch (daikon.config.Configuration.ConfigException e) {
                        throw new Daikon.TerminationMessage(e);
                    }
                    break;
                } else if (debugAll_SWITCH.equals(option_name)) {
                    Global.debugAll = true;
                } else if (debug_SWITCH.equals(option_name)) {
                    LogHelper.setLevel(g.getOptarg(), LogHelper.FINE);
                } else if (track_SWITCH.equals(option_name)) {
                    LogHelper.setLevel("daikon.Debug", LogHelper.FINE);
                    String error = Debug.add_track(g.getOptarg());
                    if (error != null) {
                        throw new Daikon.TerminationMessage("Error parsing track argument '" + g.getOptarg() + "' - " + error);
                    }
                } else if (disc_reason_SWITCH.equals(option_name)) {
                    try {
                        PrintInvariants.discReasonSetup(g.getOptarg());
                    } catch (IllegalArgumentException e) {
                        throw new Daikon.TerminationMessage(e);
                    }
                } else if (mem_stat_SWITCH.equals(option_name)) {
                    use_mem_monitor = true;
                } else {
                    throw new Daikon.TerminationMessage("Unknown option " + option_name + " on command line");
                }
                break;
            case 'h':
                System.out.println(usage);
                throw new Daikon.TerminationMessage();
            case 'o':
                String inv_filename = g.getOptarg();
                if (inv_file != null) {
                    throw new Daikon.TerminationMessage("multiple serialization output files supplied on command line: " + inv_file + " " + inv_filename);
                }
                inv_file = new File(inv_filename);
                if (!UtilMDE.canCreateAndWrite(inv_file)) {
                    throw new Daikon.TerminationMessage("Cannot write to serialization output file " + inv_file);
                }
                break;
            // 
            case '?':
                // break; // getopt() already printed an error
                System.out.println(usage);
                throw new Daikon.TerminationMessage();
            // 
            default:
                System.out.println("getopt() returned " + c);
                break;
        }
    }
    // processing only to bail out at the end.
    for (int i = g.getOptind(); i < args.length; i++) {
        String filename = args[i];
        File file = null;
        if (!filename.equals("-") && !filename.equals("+")) {
            file = new File(filename);
            if (!file.exists()) {
                throw new Daikon.TerminationMessage("File " + file + " not found.");
            }
            filename = file.toString();
        }
        // (eg, a date or ".gz").
        if (filename.indexOf(".decls") != -1) {
            decl_files.add(file);
        } else if (filename.indexOf(".dtrace") != -1) {
            dtrace_files.add(filename);
            // specified on the command line.
            if (inv_file == null) {
                String basename;
                // This puts the .inv file in the current directory.
                basename = new File(filename).getName();
                // This puts the .inv file in the same directory as the .dtrace file.
                // basename = filename;
                int base_end = basename.indexOf(".dtrace");
                String inv_filename = basename.substring(0, base_end) + ".inv.gz";
                inv_file = new File(inv_filename);
                if (!UtilMDE.canCreateAndWrite(inv_file)) {
                    throw new Daikon.TerminationMessage("Cannot write to file " + inv_file);
                }
            }
        } else if (filename.indexOf(".spinfo") != -1) {
            spinfo_files.add(file);
        } else if (filename.indexOf(".map") != -1) {
            map_files.add(file);
        } else if (filename.equals("-") || filename.equals("+")) {
            dtrace_files.add(filename);
        } else {
            throw new Daikon.TerminationMessage("Unrecognized file type: " + file);
        }
    }
    // Set the fuzzy float comparison ratio.  This needs to be done after
    // any configuration options (which may set the ratio) are processed.
    Global.fuzzy.set_rel_diff(Invariant.dkconfig_fuzzy_ratio);
    // Setup ppt_max_name based on the specified percentage of ppts to process
    if (dkconfig_ppt_perc != 100) {
        ppt_max_name = setup_ppt_perc(decl_files, dkconfig_ppt_perc);
        System.out.println("Max ppt name = " + ppt_max_name);
    }
    // Validate guardNulls option
    PrintInvariants.validateGuardNulls();
    return new FileOptions(decl_files, dtrace_files, spinfo_files, map_files);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TextFile(utilMDE.TextFile) MemberString(daikon.inv.binary.sequenceString.MemberString) OneOfString(daikon.inv.unary.string.OneOfString) EltOneOfString(daikon.inv.unary.stringsequence.EltOneOfString) PrintableString(daikon.inv.unary.string.PrintableString) FileIOException(utilMDE.FileIOException) IOException(java.io.IOException) FileIOException(utilMDE.FileIOException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Getopt(gnu.getopt.Getopt) LongOpt(gnu.getopt.LongOpt) TextFile(utilMDE.TextFile) File(java.io.File)

Aggregations

MemberString (daikon.inv.binary.sequenceString.MemberString)1 OneOfString (daikon.inv.unary.string.OneOfString)1 PrintableString (daikon.inv.unary.string.PrintableString)1 EltOneOfString (daikon.inv.unary.stringsequence.EltOneOfString)1 Getopt (gnu.getopt.Getopt)1 LongOpt (gnu.getopt.LongOpt)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 LinkedHashSet (java.util.LinkedHashSet)1 FileIOException (utilMDE.FileIOException)1 TextFile (utilMDE.TextFile)1