This source file includes following definitions.
- scanWhite
- trimWhite
- nm2tsv
- initOptions
- cleanOptions
- showHelp
- main
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define ERROR_REPORT(num, val, msg) fprintf(stderr, "error(%d):\t\"%s\"\t%s\n", (num), (val), (msg));
#define CLEANUP(ptr) do { if(NULL != ptr) { free(ptr); ptr = NULL; } } while(0)
typedef struct __struct_Options
{
const char* mProgramName;
FILE* mInput;
char* mInputName;
FILE* mOutput;
char* mOutputName;
int mHelp;
}
Options;
typedef struct __struct_Switch
{
const char* mLongName;
const char* mShortName;
int mHasValue;
const char* mValue;
const char* mDescription;
}
Switch;
#define DESC_NEWLINE "\n\t\t"
static Switch gInputSwitch = {"--input", "-i", 1, NULL, "Specify input file." DESC_NEWLINE "stdin is default."};
static Switch gOutputSwitch = {"--output", "-o", 1, NULL, "Specify output file." DESC_NEWLINE "Appends if file exists." DESC_NEWLINE "stdout is default."};
static Switch gHelpSwitch = {"--help", "-h", 0, NULL, "Information on usage."};
static Switch* gSwitches[] = {
&gInputSwitch,
&gOutputSwitch,
&gHelpSwitch
};
char* scanWhite(char* inScan)
{
char* retval = inScan;
while('\0' != *retval && 0 == isspace(*retval))
{
retval++;
}
return retval;
}
void trimWhite(char* inString)
{
int len = strlen(inString);
while(len)
{
len--;
if(isspace(*(inString + len)))
{
*(inString + len) = '\0';
}
else
{
break;
}
}
}
int nm2tsv(Options* inOptions)
{
int retval = 0;
char lineBuffer[4096];
char* module = NULL;
char* size = NULL;
char* type = NULL;
char* symbol = NULL;
while(0 == retval && NULL != fgets(lineBuffer, sizeof(lineBuffer), inOptions->mInput))
{
trimWhite(lineBuffer);
size = strchr(lineBuffer, ':');
if(NULL != size)
{
*size = '\0';
size++;
module = strrchr(lineBuffer, '/');
if(NULL == module)
{
module = lineBuffer;
}
else
{
*module = '\0';
module++;
}
type = scanWhite(size);
*type = '\0';
type++;
symbol = type + 1;
*symbol = '\0';
symbol++;
switch(*type)
{
case '-':
continue;
break;
default:
break;
}
fprintf(inOptions->mOutput, "%s\t", size);
switch(toupper(*type))
{
case 'T':
case 'W':
fprintf(inOptions->mOutput, "CODE\t");
break;
default:
fprintf(inOptions->mOutput, "DATA\t");
break;
}
if(islower(*type))
{
fprintf(inOptions->mOutput, "STATIC\t");
}
else
{
switch(*type)
{
case '?':
fprintf(inOptions->mOutput, "UNDEF\t");
break;
default:
fprintf(inOptions->mOutput, "PUBLIC\t");
break;
}
}
fprintf(inOptions->mOutput, "%s\t", module);
fprintf(inOptions->mOutput, "%c\t", toupper(*type));
fprintf(inOptions->mOutput, "UNDEF:%s:%c\t", module, toupper(*type));
fprintf(inOptions->mOutput, "%s\n", symbol);
}
else
{
retval = __LINE__;
ERROR_REPORT(retval, lineBuffer, "Malformed input line.");
}
}
if(0 == retval && 0 != ferror(inOptions->mInput))
{
retval = __LINE__;
ERROR_REPORT(retval, inOptions->mInputName, "Unable to read file.");
}
return retval;
}
int initOptions(Options* outOptions, int inArgc, char** inArgv)
{
int retval = 0;
int loop = 0;
int switchLoop = 0;
int match = 0;
const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]);
Switch* current = NULL;
memset(outOptions, 0, sizeof(Options));
outOptions->mProgramName = inArgv[0];
outOptions->mInput = stdin;
outOptions->mInputName = strdup("stdin");
outOptions->mOutput = stdout;
outOptions->mOutputName = strdup("stdout");
if(NULL == outOptions->mOutputName || NULL == outOptions->mInputName)
{
retval = __LINE__;
ERROR_REPORT(retval, "stdin/stdout", "Unable to strdup.");
}
for(loop = 1; loop < inArgc && 0 == retval; loop++)
{
match = 0;
current = NULL;
for(switchLoop = 0; switchLoop < switchCount && 0 == retval; switchLoop++)
{
if(0 == strcmp(gSwitches[switchLoop]->mLongName, inArgv[loop]))
{
match = __LINE__;
}
else if(0 == strcmp(gSwitches[switchLoop]->mShortName, inArgv[loop]))
{
match = __LINE__;
}
if(match)
{
if(gSwitches[switchLoop]->mHasValue)
{
if(loop + 1 < inArgc)
{
loop++;
current = gSwitches[switchLoop];
current->mValue = inArgv[loop];
}
}
else
{
current = gSwitches[switchLoop];
}
break;
}
}
if(0 == match)
{
outOptions->mHelp = __LINE__;
retval = __LINE__;
ERROR_REPORT(retval, inArgv[loop], "Unknown command line switch.");
}
else if(NULL == current)
{
outOptions->mHelp = __LINE__;
retval = __LINE__;
ERROR_REPORT(retval, inArgv[loop], "Command line switch requires a value.");
}
else
{
if(current == &gInputSwitch)
{
CLEANUP(outOptions->mInputName);
if(NULL != outOptions->mInput && stdin != outOptions->mInput)
{
fclose(outOptions->mInput);
outOptions->mInput = NULL;
}
outOptions->mInput = fopen(current->mValue, "r");
if(NULL == outOptions->mInput)
{
retval = __LINE__;
ERROR_REPORT(retval, current->mValue, "Unable to open input file.");
}
else
{
outOptions->mInputName = strdup(current->mValue);
if(NULL == outOptions->mInputName)
{
retval = __LINE__;
ERROR_REPORT(retval, current->mValue, "Unable to strdup.");
}
}
}
else if(current == &gOutputSwitch)
{
CLEANUP(outOptions->mOutputName);
if(NULL != outOptions->mOutput && stdout != outOptions->mOutput)
{
fclose(outOptions->mOutput);
outOptions->mOutput = NULL;
}
outOptions->mOutput = fopen(current->mValue, "a");
if(NULL == outOptions->mOutput)
{
retval = __LINE__;
ERROR_REPORT(retval, current->mValue, "Unable to open output file.");
}
else
{
outOptions->mOutputName = strdup(current->mValue);
if(NULL == outOptions->mOutputName)
{
retval = __LINE__;
ERROR_REPORT(retval, current->mValue, "Unable to strdup.");
}
}
}
else if(current == &gHelpSwitch)
{
outOptions->mHelp = __LINE__;
}
else
{
retval = __LINE__;
ERROR_REPORT(retval, current->mLongName, "No handler for command line switch.");
}
}
}
return retval;
}
void cleanOptions(Options* inOptions)
{
CLEANUP(inOptions->mInputName);
if(NULL != inOptions->mInput && stdin != inOptions->mInput)
{
fclose(inOptions->mInput);
}
CLEANUP(inOptions->mOutputName);
if(NULL != inOptions->mOutput && stdout != inOptions->mOutput)
{
fclose(inOptions->mOutput);
}
memset(inOptions, 0, sizeof(Options));
}
void showHelp(Options* inOptions)
{
int loop = 0;
const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]);
const char* valueText = NULL;
printf("usage:\t%s [arguments]\n", inOptions->mProgramName);
printf("\n");
printf("arguments:\n");
for(loop = 0; loop < switchCount; loop++)
{
if(gSwitches[loop]->mHasValue)
{
valueText = " <value>";
}
else
{
valueText = "";
}
printf("\t%s%s\n", gSwitches[loop]->mLongName, valueText);
printf("\t %s%s", gSwitches[loop]->mShortName, valueText);
printf(DESC_NEWLINE "%s\n\n", gSwitches[loop]->mDescription);
}
printf("This tool normalizes nm output for use by other tools.\n");
printf("GNU nm is assumed for symbol type determination.\n");
printf("i.e. Use this tool to parse the output of:\n");
printf("\t/usr/bin/nm --format=bsd --size-sort --print-file-name --demangle <exefile>\n");
}
int main(int inArgc, char** inArgv)
{
int retval = 0;
Options options;
retval = initOptions(&options, inArgc, inArgv);
if(options.mHelp)
{
showHelp(&options);
}
else if(0 == retval)
{
retval = nm2tsv(&options);
}
cleanOptions(&options);
return retval;
}