using System; using System.Collections.Generic; using System.Text; using System.IO; // Parse ASP // Written by Patrick Coston // November 2008 // patcoston@gmail.com // http://www.patcoston.com/ // This code is protected by the GNU General Public License // http://patcoston.com/daymoon/GNU.txt namespace ASPCodeParse { class Program { static string RootDir = ""; // root directory of web site static string IncludeType = "file"; // type of include: "virtual" or "file" static int FilesParsed = 0; // number of files parsed so far static string[] Parsed = new string[1000]; // array of parsed files // Returns true of line is commented out, otherwise false public static bool CommentLine(string Line) { // if line not commented out if (Line.Substring(0, 1) != "'") { if (Line.Length > 3) { if ((Line.Substring(0, 4).ToUpper() != "REM ") && (Line.Substring(0, 4).ToUpper() != "REM\t")) { return (false); } } } return (true); } // Parses the lines in a file // Name - the name of the file to parse // Find - the string you are looking for // Used to output includes, Sub names and Function names public static void ParseLines(string Name, string Find) { int p1; StreamReader re = File.OpenText(Name); string Line = null; while ((Line = re.ReadLine()) != null) { Line = Line.Trim(); if (Line.Length > 0) { if (!CommentLine(Line)) // if line is not commented out { // trim <% %> if (Line.Substring(0, 2) == "<%") Line = Line.Substring(2, Line.Length - 2); if (Line.Substring(Line.Length - 2, 2) == "%>") Line = Line.Substring(0, Line.Length - 2); Line = Line.Trim(); p1 = Line.IndexOf(Find); bool ParseLine = false; if ((Find == "Sub ") || (Find == "Function ")) { if (p1 == 0) // Sub or Function start the line? ParseLine = true; } else if (p1 != -1) // does this line include a file? ParseLine = true; if (ParseLine) Console.WriteLine(Line); } } } re.Close(); } // Search for // #include file="foo.asp" // #include virtual="foo.asp" public static string GetIncludePath(string Line) { int p1 = Line.IndexOf("#include") + 9; int p2 = Line.IndexOf("virtual", p1) + 8; if (p2 == 7) // virtual not found { IncludeType = "file"; p2 = Line.IndexOf("file", p1) + 5; } else IncludeType = "virtual"; int p3 = Line.IndexOf("\"", p2) + 1; int p4 = Line.IndexOf("\"", p3); int Len = p4 - p3; string Name = Line.Substring(p3, Len); return (Name); } // Returns the full path to an include file name // Converts the path used in the include statement to a full system path // Examples // ../../../include/settings.asp might be converted to f:\web\staging\root\include\settings.asp // login/accounts/settings/default.asp might be converted to c:\inetpub\wwwroot\krc\live3\login\accounts\settings\default.asp public static string GetFullPath(string Name) { string FullPath = RootDir; Name = Name.Replace("/", "\\"); if (IncludeType == "virtual") { string str = Name.Substring(0,1); if (str != "\\") Name = "\\" + Name; FullPath += Name; } else { string CurrentDir = Directory.GetCurrentDirectory(); if (Name.Substring(0, 2) == "..") { while (Name.Substring(0, 2) == "..") { Name = Name.Substring(3, Name.Length - 3); // remove "..\\" Directory.SetCurrentDirectory(".."); // move up a directory CurrentDir = Directory.GetCurrentDirectory(); } } FullPath = CurrentDir + "\\" + Name; } return (FullPath); } // Sets the current directory and returns the current directory // Name - full path to include file public static string SetCurrentDir(string Name) { int p = 0; int LastIndex = 0; // look for file name by skipping past back slashes while (p != -1) { LastIndex = p; p = Name.IndexOf("\\", p + 2); } string Path = Name.Substring(0, LastIndex); // strip off file name leaving path to file Directory.SetCurrentDirectory(Path); return (Path); } // recursively parse include files within an asp page // Name - name of asp file to parse public static void ParseIncludes(string Name) { int Pos = 0; string Find = "#include"; string IncludePath = ""; string FullPath = ""; string CurrentDir = SetCurrentDir(Name); StreamReader re = File.OpenText(Name); string Line = null; while ((Line = re.ReadLine()) != null) { Line = Line.Trim(); if (Line.Length > 0) { if (!CommentLine(Line)) // if line is not commented out { Pos = Line.IndexOf(Find); if (Pos != -1) { IncludePath = GetIncludePath(Line); FullPath = GetFullPath(IncludePath); ParseFile(FullPath); Directory.SetCurrentDirectory(CurrentDir); } } } } re.Close(); } // File been parsed before? returns true/false public static bool ParsedBefore(string Name) { bool Before = false; for (int i = 0; i < FilesParsed; i++) if (Parsed[i] == Name) Before = true; if (!Before) { Parsed[FilesParsed] = Name; FilesParsed++; } return (Before); } // Name - filename to parse // Type - file for main file or file include, virtual for virtual include public static void ParseFile(string Name) { if (ParsedBefore(Name)) // don't parse the same include file more than once return; if (!File.Exists(Name)) { Console.WriteLine("\nFile " + Name + " does not exist\n"); return; } Console.WriteLine("\nParse " + Name + "\n"); ParseLines(Name, "#include"); ParseLines(Name, "Sub "); ParseLines(Name, "Function "); ParseIncludes(Name); } // args[0] - full path to root of site // args[1] - name of asp file to parse public static int Main(string[] args) { if (args.Length == 2) { RootDir = args[0]; string FullPath = Directory.GetCurrentDirectory() + "\\" + args[1]; ParseFile(FullPath); } else { Console.WriteLine("Parse ASP: Version 1.2"); Console.WriteLine("Syntax: parseasp [root] [file]"); Console.WriteLine("root - full path to root of drive"); Console.WriteLine("file - name of asp file to parse"); Console.WriteLine("\n"); Console.WriteLine("Examples:"); Console.WriteLine("parseasp c:\\web\\httpdocs default.asp"); Console.WriteLine("parseasp d:\\inetpub\\wwwroot welcome.asp"); Console.WriteLine("\n"); Console.WriteLine("Instructions:"); Console.WriteLine("* This is a command line app"); Console.WriteLine("* Do not end path to root of site with a slash"); Console.WriteLine("* Add executable to your path"); Console.WriteLine("* Run from directory of page to parse"); Console.WriteLine("* Do not add a path to the file to parse"); } return 0; } } }