summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenny Shields <mail@kennyshields.net>2020-12-25 16:50:43 -0500
committerKenny Shields <mail@kennyshields.net>2020-12-25 16:50:43 -0500
commit25a9cc2fb261d20b56464c3271178c13eb81dc16 (patch)
tree3f519214e1735217d60019f7fdd53e31f82525f2
parent1d07cbeb239a1895908156e079fcf17c7ba75689 (diff)
downloaddsymlist-25a9cc2fb261d20b56464c3271178c13eb81dc16.tar.gz
dsymlist-25a9cc2fb261d20b56464c3271178c13eb81dc16.zip
Convert code to D style
-rw-r--r--source/main.d148
1 files changed, 84 insertions, 64 deletions
diff --git a/source/main.d b/source/main.d
index 0ef765c..1ea41b7 100644
--- a/source/main.d
+++ b/source/main.d
@@ -14,7 +14,8 @@ import std.algorithm.searching : startsWith;
import std.conv : to;
/// Symbol data structure.
-struct Symbol {
+struct Symbol
+{
/// The name of the symbol.
string name;
/// The type of the symbol.
@@ -64,55 +65,53 @@ ulong namespaceCount = 0;
/**
* Program entry point.
*/
-void main(string[] args) {
+void main(string[] args)
+{
// check options
GetoptResult helpInfo;
- try {
+ try
+ {
helpInfo = getopt(args, config.passThrough,
- "quiet|q",
- "Don't print to standard out.",
- &__opt_quiet,
- "outfile|o",
- "Writes output to the specified file.",
- &__opt_outFile,
- "mangle|m",
- "Include mangled symbol in output.",
- &__opt_showMangle,
- "spacing|s",
- "Add extra spacing for less compact output.",
- &__opt_extraSpacing,
- "namespace|n",
- "Comma-separated list of namespaces to restrict the symbols collection to.",
- &__opt_nameSpaces
- );
- } catch(Exception e) {
+ "quiet|q", "Don't print to standard out.", &__opt_quiet,
+ "outfile|o", "Writes output to the specified file.", &__opt_outFile,
+ "mangle|m", "Include mangled symbol in output.", &__opt_showMangle,
+ "spacing|s", "Add extra spacing for less compact output.", &__opt_extraSpacing,
+ "namespace|n", "Comma-separated list of namespaces to restrict the symbols collection to.", &__opt_nameSpaces);
+ }
+ catch (Exception e)
+ {
writeln("Invalid option.");
return;
}
- if(helpInfo.helpWanted) {
+ if (helpInfo.helpWanted)
+ {
writeln("dsymlist - List D language symbols in a binary file.");
writeln("\t");
writeln("Usage: [file] [option(s)]");
writeln("\t");
- foreach(item; helpInfo.options) {
+ foreach (item; helpInfo.options)
+ {
writefln("%7s %s\n\t%s\n", item.optShort, item.optLong, item.help);
}
return;
}
// get file name
- if(args.length < 2) {
+ if (args.length < 2)
+ {
writeln("Error: No input file specified.");
return;
}
inputFile = args[1];
// check file path
- if(!exists(inputFile) || !isFile(inputFile)) {
+ if (!exists(inputFile) || !isFile(inputFile))
+ {
writeln("Error: Invalid input file.");
return;
}
// collect symbols and perform output
collect(inputFile);
- if(symbols.length == 0) {
+ if (symbols.length == 0)
+ {
writeln("No symbols found.");
return;
}
@@ -125,53 +124,60 @@ void main(string[] args) {
* Params:
* file = The path to the file.
*/
-void collect(string file) {
- if(__opt_nameSpaces.length > 0) {
+void collect(string file)
+{
+ if (__opt_nameSpaces.length > 0)
+ {
namespaces = __opt_nameSpaces.split(",");
}
// run nm on the file and iterate over each output line
auto result = execute(["nm", file]);
- foreach(line; result.output.splitLines()) {
+ foreach (line; result.output.splitLines())
+ {
// attempt to match symbol from nm output line
auto matches = matchFirst(line, nmLineMatch);
- if(matches.length != 4) {
+ if (matches.length != 4)
+ {
continue;
}
// attempt to demangle symbol to determine if it is a D symbol
auto symbol = demangle(matches[3]);
- if(symbol == matches[3]) {
+ if (symbol == matches[3])
+ {
continue;
}
// attempt to match sections of the symbol (type, namespace, symbol name, params)
auto symbolMatches = matchFirst(symbol, symbolMatch);
- if(symbolMatches.length != 5) {
+ if (symbolMatches.length != 5)
+ {
continue;
}
// filter symbols if namespaces were provided by the user
- if(namespaces.length > 0) {
+ if (namespaces.length > 0)
+ {
import std.algorithm.iteration : each;
+
bool found = false;
namespaces.each!((n) {
- if(symbolMatches[2].startsWith(n)) {
+ if (symbolMatches[2].startsWith(n))
+ {
found = true;
return;
}
});
- if(!found) {
+ if (!found)
+ {
continue;
}
}
// add the symbol to the list
- if((symbolMatches[2] in symbols) is null) {
+ if ((symbolMatches[2] in symbols) is null)
+ {
symbols[symbolMatches[2]] = [];
namespaceCount++;
}
- symbols[symbolMatches[2]] ~= Symbol(
- symbolMatches[3],
- symbolMatches[1],
- symbolMatches[4].split(","),
- matches[3]
- );
+ symbols[symbolMatches[2]] ~= Symbol(symbolMatches[3], symbolMatches[1],
+ symbolMatches[4].split(","), matches[3]);
symbolCount++;
}
}
@@ -180,56 +186,65 @@ void collect(string file) {
* Performs output of collected symbol data. Depending on the parameters specified by
* the user, output will either be to standard out, a file on disk or both.
*/
-void output() {
+void output()
+{
// clear output file if it already exists
- if(__opt_outFile.length > 0 && exists(__opt_outFile) && isFile(__opt_outFile)) {
+ if (__opt_outFile.length > 0 && exists(__opt_outFile) && isFile(__opt_outFile))
+ {
write(__opt_outFile, "");
}
// write file name, symbol count and namespace count to output file header
writeToFile(baseName(inputFile));
- writeToFile(to!string(symbolCount) ~ " symbols in " ~ to!string(namespaceCount)
- ~ " namespaces");
+ writeToFile(to!string(symbolCount) ~ " symbols in " ~ to!string(namespaceCount) ~ " namespaces");
// write namespaces from user input to output file header
- if(__opt_nameSpaces.length > 0) {
+ if (__opt_nameSpaces.length > 0)
+ {
writeToFile(to!string(__opt_nameSpaces.split(",")));
}
writeToFile("------------------------------------\n");
- foreach(namespace; sort(symbols.keys)) {
+ foreach (namespace; sort(symbols.keys))
+ {
string length = to!string(symbols[namespace].length);
string consoleStr = "\033[0;92m" ~ namespace ~ " (" ~ length ~ ")\033[0m\n{\n";
string fileStr = namespace ~ " (" ~ length ~ ")\n{\n";
- if(__opt_extraSpacing) {
+ if (__opt_extraSpacing)
+ {
consoleStr ~= "\n";
fileStr ~= "\n";
}
// sort the list of symbols in the current namespace
symbols[namespace].sort!("a.name < b.name");
- foreach(item; symbols[namespace]) {
- consoleStr ~= " \033[0;95m" ~ item.type ~ "\033[0m : \033[0;36m"
- ~ item.name ~ "\033[0m ( ";
- foreach(key, param; item.params) {
+ foreach (item; symbols[namespace])
+ {
+ consoleStr ~= " \033[0;95m" ~ item.type ~ "\033[0m : \033[0;36m" ~ item.name ~ "\033[0m ( ";
+ foreach (key, param; item.params)
+ {
consoleStr ~= "\033[0;34m" ~ param;
- if((key + 1) < item.params.length) {
+ if ((key + 1) < item.params.length)
+ {
consoleStr ~= "\033[0m,";
}
}
consoleStr ~= "\033[0m )\n";
- if(__opt_showMangle) {
+ if (__opt_showMangle)
+ {
consoleStr ~= " \033[0;37m" ~ item.mangle ~ "\n";
}
- fileStr ~= " " ~ item.type ~ " : " ~ item.name ~ " ( "
- ~ item.params.join(",") ~ " )\n";
- if(__opt_showMangle) {
+ fileStr ~= " " ~ item.type ~ " : " ~ item.name ~ " ( " ~ item.params.join(",") ~ " )\n";
+ if (__opt_showMangle)
+ {
fileStr ~= " " ~ item.mangle ~ "\n";
}
- if(__opt_extraSpacing) {
+ if (__opt_extraSpacing)
+ {
consoleStr ~= "\n";
fileStr ~= "\n";
}
}
consoleStr ~= "\033[0m}\033[0;31m End of " ~ namespace;
fileStr ~= "} End of " ~ namespace;
- if(__opt_extraSpacing) {
+ if (__opt_extraSpacing)
+ {
consoleStr ~= "\n";
fileStr ~= "\n";
}
@@ -245,8 +260,10 @@ void output() {
* Params:
* args = Variable length list of arguments to write to the console.
*/
-void writeToConsole(A...)(A args) {
- if(__opt_quiet) {
+void writeToConsole(A...)(A args)
+{
+ if (__opt_quiet)
+ {
return;
}
writeln(args);
@@ -258,11 +275,14 @@ void writeToConsole(A...)(A args) {
* Params:
* contents = The contents to write to the file.
*/
-void writeToFile(string contents) {
- if(__opt_outFile.length == 0) {
+void writeToFile(string contents)
+{
+ if (__opt_outFile.length == 0)
+ {
return;
}
- if(exists(__opt_outFile) && !isFile(__opt_outFile)) {
+ if (exists(__opt_outFile) && !isFile(__opt_outFile))
+ {
return;
}
append(__opt_outFile, contents ~ "\n");