private void cmdGrepifyScan_Click(object sender, EventArgs e)
{
Grepifyv2 grepifyV2 = null;
List<String> lstV2Filenames = new List<String>();
List<Grepifyv2Check> grepifyV2Checks = null;
// Error checking
if (Directory.Exists(txtCodePath.Text) == false) // does the directory exist?
{
MessageBox.Show("You need to specify a valid path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (txtCodePath.Text.ToLower().Contains("test") && optIgnoreTest.Checked == true)
{
MessageBox.Show("Your patch contains test and you have the ignore directories and files with test option selected." + Environment.NewLine + "No results will be found, cancelling search!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string[] strExts = txtExt.Text.Split(';'); // is the defined extensions list the correct format?
foreach (string strExt in strExts)
{
if (strExt.StartsWith("*.") == false)
{
MessageBox.Show(txtExt.Text + " is not in the correct format\nPlease use *.ext1;*.ext2\n", "Incorrect format", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
profileLines = null; // reset
int intProfileCount=0;
foreach (ToolStripMenuItem tsiTarget in optGrepify.DropDownItems)
{
string[] strNewLines = null;
bool bError = false;
List<string> strRegexs = new List<string>();
if (tsiTarget.CheckState == CheckState.Checked)
{
intProfileCount++;
string strFilename = AssemblyDirectory + "\\Grepify.Profiles\\" + tsiTarget.Text + ".txt";
string strv2Filename = AssemblyDirectory + "\\Grepify.Profiles\\" + tsiTarget.Text + ".grepifyv2";
// Load the file
if (!File.Exists(strFilename) && !File.Exists(strv2Filename))
{
MessageBox.Show("File " + strFilename + " or " + strv2Filename + " does not exist. Aborting scan!", "File does not exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else if(File.Exists(strFilename)) // v1 fileformat
{
try
{
strNewLines = File.ReadAllLines(strFilename);
}
catch (Exception)
{
MessageBox.Show("File " + strFilename + " could not be read. Aborting scan!", "Could not read file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
// Sanity check the regex of v1 files
int intCount = 0;
foreach (string strRegex in strNewLines)
{
intCount++;
if (strRegex.StartsWith("#")) continue;
try
{
Match regexMatch = Regex.Match("Mooo", strRegex);
strRegexs.Add(strRegex);
}
catch (ArgumentException rExcp)
{
MessageBox.Show("Regex looks broken on line " + intCount + ". Regex is '" + strRegex + "'. Error is '" + rExcp.Message + "' in file " + strFilename + ".", "Regex error", MessageBoxButtons.OK, MessageBoxIcon.Error);
profileLines = null;
bError = true;
break;
}
}
if (profileLines == null && bError == false)
{
profileLines = strRegexs.ToArray();
}
else if (bError == false)
{
profileLines = profileLines.Concat(strRegexs).ToArray();
}
}
else if (File.Exists(strv2Filename))
{
lstV2Filenames.Add(strv2Filename);
}
if (lstV2Filenames.Count > 0)
{
grepifyV2 = new Grepifyv2(lstV2Filenames);
}
}
}
if ((intProfileCount == 0 || profileLines == null) && (grepifyV2 == null || grepifyV2.CheckCount() == 0))
{
if (intProfileCount == 0)
{
MessageBox.Show("No Grepify profiles selected for scan. Aborting scan!", "No Grepify profiles", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
Console.WriteLine(grepifyV2.CheckCount());
MessageBox.Show("No Grepify regexes in selected files. Aborting scan!", "No Grepify regexes in selected files", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
return;
}
// Get the grepify V2 checks if needed
try
{
grepifyV2Checks = grepifyV2.GetChecks();
}
catch (Exception)
{
}
// Now try concatinate the extensions that the user supplied and that are required to satisfy any v2 profiles
string strv2Exts = null;
try
{
strv2Exts = grepifyV2.GetExts();
}
catch (Exception)
{
}
string strv1Exts = txtExt.Text;
if (strv2Exts != null)
{
strv1Exts = new StringBuilder().Append(strv1Exts + ";" + strv2Exts).ToString();
}
// Now initalize a search (aka results) form
frmSearch frmSearch = new frmSearch("Grepify scan of " + txtCodePath.Text + " (Regex:True,Case:" + opCaseSearch.Checked + ",Ignore Test:" + optIgnoreTest.Checked + ",Ignore Comments:" + optIgnoreComments.Checked + ") - " + strv1Exts, this, true);
frmSearch.AddRegexColumns(); // Adds the extra columns
frmSearch.MdiParent = this;
frmSearch.Visible = true;
// Now initialize the object and start a scan
Scanner scanYoink = new Scanner(frmSearch, txtCodePath.Text, profileLines, grepifyV2Checks, optIgnoreComments.Checked, true, opCaseSearch.Checked, optIgnoreTest.Checked, strv1Exts, richExclusions.Lines);
frmSearch.SetScanEngine(scanYoink);
scanYoink.Start(this, frmSearch);
}