1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is maptsvdifftool.c code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2002
22 * the Initial Developer. All Rights Reserved.
25 * Garrett Arch Blythe, 03-October-2002
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
47 #define ERROR_REPORT(num, val, msg) fprintf(stderr, "error(%d):\t\"%s\"\t%s\n", (num), (val), (msg));
48 #define CLEANUP(ptr) do { if(NULL != ptr) { free(ptr); ptr = NULL; } } while(0)
51 typedef struct __struct_Options
53 ** Options to control how we perform.
55 ** mProgramName Used in help text.
56 ** mInput File to read for input.
58 ** mInputName Name of the file.
59 ** mOutput Output file, append.
61 ** mOutputName Name of the file.
62 ** mHelp Whether or not help should be shown.
63 ** mSummaryOnly Only output a signle line.
64 ** mZeroDrift Output zero drift data.
65 ** mNegation Perform negation heuristics on the symbol drifts.
68 const char* mProgramName;
81 typedef struct __struct_Switch
83 ** Command line options.
86 const char* mLongName;
87 const char* mShortName;
90 const char* mDescription;
94 #define DESC_NEWLINE "\n\t\t"
96 static Switch gInputSwitch = {"--input", "-i", 1, NULL, "Specify input file." DESC_NEWLINE "stdin is default."};
97 static Switch gOutputSwitch = {"--output", "-o", 1, NULL, "Specify output file." DESC_NEWLINE "Appends if file exists." DESC_NEWLINE "stdout is default."};
98 static Switch gSummarySwitch = {"--summary", "-s", 0, NULL, "Only output a single line." DESC_NEWLINE "The cumulative size changes." DESC_NEWLINE "Overrides all other output options."};
99 static Switch gZeroDriftSwitch = {"--zerodrift", "-z", 0, NULL, "Output zero drift data." DESC_NEWLINE "Reports symbol changes even when there is no net drift."};
100 static Switch gNegationSwitch = {"--negation", "-n", 0, NULL, "Use negation heuristics." DESC_NEWLINE "When symbol sizes are inferred by offset, order changes cause noise." DESC_NEWLINE "This helps see through the noise by eliminating equal and opposite drifts."};
101 static Switch gHelpSwitch = {"--help", "-h", 0, NULL, "Information on usage."};
103 static Switch* gSwitches[] = {
113 typedef struct __struct_SizeComposition
115 ** Used to keep which parts positive and negative resulted in the total.
124 typedef struct __struct_SizeStats
126 ** Keep track of sizes.
127 ** Use signed integers so that negatives are valid, in which case we shrunk.
131 SizeComposition mCodeComposition;
134 SizeComposition mDataComposition;
139 typedef enum __enum_SegmentClass
141 ** What type of data a segment holds.
150 typedef struct __struct_SymbolStats
152 ** Symbol level stats.
161 typedef struct __struct_ObjectStats
163 ** Object level stats.
168 SizeComposition mComposition;
169 SymbolStats* mSymbols;
170 unsigned mSymbolCount;
175 typedef struct __struct_SegmentStats
177 ** Segment level stats.
183 SizeComposition mComposition;
184 ObjectStats* mObjects;
185 unsigned mObjectCount;
190 typedef struct __struct_ModuleStats
192 ** Module level stats.
197 SegmentStats* mSegments;
198 unsigned mSegmentCount;
203 static int moduleCompare(const void* in1, const void* in2)
210 ModuleStats* one = (ModuleStats*)in1;
211 ModuleStats* two = (ModuleStats*)in2;
213 int oneSize = (one->mSize.mCode + one->mSize.mData);
214 int twoSize = (two->mSize.mCode + two->mSize.mData);
216 if(oneSize < twoSize)
220 else if(oneSize > twoSize)
226 retval = strcmp(one->mModule, two->mModule);
227 if(0 > oneSize && 0 > twoSize)
237 static int segmentCompare(const void* in1, const void* in2)
244 SegmentStats* one = (SegmentStats*)in1;
245 SegmentStats* two = (SegmentStats*)in2;
247 if(one->mSize < two->mSize)
251 else if(one->mSize > two->mSize)
257 retval = strcmp(one->mSegment, two->mSegment);
258 if(0 > one->mSize && 0 > two->mSize)
268 static int objectCompare(const void* in1, const void* in2)
275 ObjectStats* one = (ObjectStats*)in1;
276 ObjectStats* two = (ObjectStats*)in2;
278 if(one->mSize < two->mSize)
282 else if(one->mSize > two->mSize)
288 retval = strcmp(one->mObject, two->mObject);
289 if(0 > one->mSize && 0 > two->mSize)
299 static int symbolCompare(const void* in1, const void* in2)
306 SymbolStats* one = (SymbolStats*)in1;
307 SymbolStats* two = (SymbolStats*)in2;
309 if(one->mSize < two->mSize)
313 else if(one->mSize > two->mSize)
319 retval = strcmp(one->mSymbol, two->mSymbol);
320 if(0 > one->mSize && 0 > two->mSize)
330 void trimWhite(char* inString)
332 ** Remove any whitespace from the end of the string.
335 int len = strlen(inString);
341 if(isspace(*(inString + len)))
343 *(inString + len) = '\0';
353 int difftool(Options* inOptions)
355 ** Read a diff file and spit out relevant information.
359 char lineBuffer[0x500];
361 ModuleStats* modules = NULL;
362 unsigned moduleCount = 0;
363 unsigned moduleLoop = 0;
364 ModuleStats* theModule = NULL;
365 unsigned segmentLoop = 0;
366 SegmentStats* theSegment = NULL;
367 unsigned objectLoop = 0;
368 ObjectStats* theObject = NULL;
369 unsigned symbolLoop = 0;
370 SymbolStats* theSymbol = NULL;
371 unsigned allSymbolCount = 0;
373 memset(&overall, 0, sizeof(overall));
376 ** Read the entire diff file.
377 ** We're only interested in lines beginning with < or >
379 while(0 == retval && NULL != fgets(lineBuffer, sizeof(lineBuffer), inOptions->mInput))
381 trimWhite(lineBuffer);
383 if(('<' == lineBuffer[0] || '>' == lineBuffer[0]) && ' ' == lineBuffer[1])
386 char* theLine = &lineBuffer[2];
397 ** Figure out if the line adds or subtracts from something.
399 if('>' == lineBuffer[0])
406 ** Scan the line for information.
408 scanRes = sscanf(theLine,
409 "%x\t%s\t%s\t%s\t%s\t%s\t",
419 SegmentClass segmentClass = DATA;
421 symbol = strrchr(theLine, '\t') + 1;
423 if(0 == strcmp(segClass, "CODE"))
427 else if(0 == strcmp(segClass, "DATA"))
434 ERROR_REPORT(retval, segClass, "Unable to determine segment class.");
439 unsigned moduleIndex = 0;
442 ** Find, in succession, the following things:
447 ** Failure to find any one of these means to create it.
450 for(moduleIndex = 0; moduleIndex < moduleCount; moduleIndex++)
452 if(0 == strcmp(modules[moduleIndex].mModule, module))
458 if(moduleIndex == moduleCount)
462 moved = realloc(modules, sizeof(ModuleStats) * (1 + moduleCount));
465 modules = (ModuleStats*)moved;
467 memset(modules + moduleIndex, 0, sizeof(ModuleStats));
469 modules[moduleIndex].mModule = strdup(module);
470 if(NULL == modules[moduleIndex].mModule)
473 ERROR_REPORT(retval, module, "Unable to duplicate string.");
479 ERROR_REPORT(retval, inOptions->mProgramName, "Unable to increase module array.");
485 unsigned segmentIndex = 0;
486 theModule = (modules + moduleIndex);
488 for(segmentIndex = 0; segmentIndex < theModule->mSegmentCount; segmentIndex++)
490 if(0 == strcmp(segment, theModule->mSegments[segmentIndex].mSegment))
496 if(segmentIndex == theModule->mSegmentCount)
500 moved = realloc(theModule->mSegments, sizeof(SegmentStats) * (theModule->mSegmentCount + 1));
503 theModule->mSegments = (SegmentStats*)moved;
504 theModule->mSegmentCount++;
505 memset(theModule->mSegments + segmentIndex, 0, sizeof(SegmentStats));
507 theModule->mSegments[segmentIndex].mClass = segmentClass;
508 theModule->mSegments[segmentIndex].mSegment = strdup(segment);
509 if(NULL == theModule->mSegments[segmentIndex].mSegment)
512 ERROR_REPORT(retval, segment, "Unable to duplicate string.");
518 ERROR_REPORT(retval, inOptions->mProgramName, "Unable to increase segment array.");
524 unsigned objectIndex = 0;
525 theSegment = (theModule->mSegments + segmentIndex);
527 for(objectIndex = 0; objectIndex < theSegment->mObjectCount; objectIndex++)
529 if(0 == strcmp(object, theSegment->mObjects[objectIndex].mObject))
535 if(objectIndex == theSegment->mObjectCount)
539 moved = realloc(theSegment->mObjects, sizeof(ObjectStats) * (1 + theSegment->mObjectCount));
542 theSegment->mObjects = (ObjectStats*)moved;
543 theSegment->mObjectCount++;
544 memset(theSegment->mObjects + objectIndex, 0, sizeof(ObjectStats));
546 theSegment->mObjects[objectIndex].mObject = strdup(object);
547 if(NULL == theSegment->mObjects[objectIndex].mObject)
550 ERROR_REPORT(retval, object, "Unable to duplicate string.");
556 ERROR_REPORT(retval, inOptions->mProgramName, "Unable to increase object array.");
562 unsigned symbolIndex = 0;
563 theObject = (theSegment->mObjects + objectIndex);
565 for(symbolIndex = 0; symbolIndex < theObject->mSymbolCount; symbolIndex++)
567 if(0 == strcmp(symbol, theObject->mSymbols[symbolIndex].mSymbol))
573 if(symbolIndex == theObject->mSymbolCount)
577 moved = realloc(theObject->mSymbols, sizeof(SymbolStats) * (1 + theObject->mSymbolCount));
580 theObject->mSymbols = (SymbolStats*)moved;
581 theObject->mSymbolCount++;
583 memset(theObject->mSymbols + symbolIndex, 0, sizeof(SymbolStats));
585 theObject->mSymbols[symbolIndex].mSymbol = strdup(symbol);
586 if(NULL == theObject->mSymbols[symbolIndex].mSymbol)
589 ERROR_REPORT(retval, symbol, "Unable to duplicate string.");
595 ERROR_REPORT(retval, inOptions->mProgramName, "Unable to increase symbol array.");
601 theSymbol = (theObject->mSymbols + symbolIndex);
604 ** Update our various totals.
608 if(CODE == segmentClass)
610 overall.mCode += size;
611 theModule->mSize.mCode += size;
613 else if(DATA == segmentClass)
615 overall.mData += size;
616 theModule->mSize.mData += size;
619 theSegment->mSize += size;
620 theObject->mSize += size;
621 theSymbol->mSize += size;
625 if(CODE == segmentClass)
627 overall.mCode -= size;
628 theModule->mSize.mCode -= size;
630 else if(DATA == segmentClass)
632 overall.mData -= size;
633 theModule->mSize.mData -= size;
636 theSegment->mSize -= size;
637 theObject->mSize -= size;
638 theSymbol->mSize -= size;
649 ERROR_REPORT(retval, inOptions->mInputName, "Unable to scan line data.");
654 if(0 == retval && 0 != ferror(inOptions->mInput))
657 ERROR_REPORT(retval, inOptions->mInputName, "Unable to read file.");
661 ** Next, it is time to perform revisionist history of sorts.
662 ** If the negation switch is in play, we perfrom the following
665 ** For each section, find size changes which have an equal and
666 ** opposite change, and set them both to zero.
667 ** However, you can only do this if the number of negating changes
668 ** is even, as if it is odd, then any one of the many could be
669 ** at fault for the actual change.
671 ** This orginally exists to make the win32 codesighs reports more
672 ** readable/meaningful.
674 if(0 == retval && 0 != inOptions->mNegation)
676 ObjectStats** objArray = NULL;
677 SymbolStats** symArray = NULL;
680 ** Create arrays big enough to hold all symbols.
681 ** As well as an array to keep the owning object at the same index.
682 ** We will keep the object around as we may need to modify the size.
684 objArray = (ObjectStats**)malloc(allSymbolCount * sizeof(ObjectStats*));
685 symArray = (SymbolStats**)malloc(allSymbolCount * sizeof(SymbolStats*));
686 if(NULL == objArray || NULL == symArray)
689 ERROR_REPORT(retval, inOptions->mProgramName, "Unable to allocate negation array memory.");
693 unsigned arrayCount = 0;
694 unsigned arrayLoop = 0;
697 ** Go through and perform the steps on each section/segment.
699 for(moduleLoop = 0; moduleLoop < moduleCount; moduleLoop++)
701 theModule = modules + moduleLoop;
703 for(segmentLoop = 0; segmentLoop < theModule->mSegmentCount; segmentLoop++)
705 theSegment = theModule->mSegments + segmentLoop;
708 ** Collect all symbols under this section.
709 ** The symbols are spread out between all the objects,
710 ** so keep track of both independently at the
715 for(objectLoop = 0; objectLoop < theSegment->mObjectCount; objectLoop++)
717 theObject = theSegment->mObjects + objectLoop;
719 for(symbolLoop = 0; symbolLoop < theObject->mSymbolCount; symbolLoop++)
721 theSymbol = theObject->mSymbols + symbolLoop;
723 objArray[arrayCount] = theObject;
724 symArray[arrayCount] = theSymbol;
730 ** Now that we have a list of symbols, go through each
731 ** and see if there is a chance of negation.
733 for(arrayLoop = 0; arrayLoop < arrayCount; arrayLoop++)
736 ** If the item is NULL, it was already negated.
737 ** Don't do this for items with a zero size.
739 if(NULL != symArray[arrayLoop] && 0 != symArray[arrayLoop]->mSize)
741 unsigned identicalValues = 0;
742 unsigned oppositeValues = 0;
743 unsigned lookLoop = 0;
744 const int lookingFor = symArray[arrayLoop]->mSize;
747 ** Count the number of items with this value.
748 ** Count the number of items with the opposite equal value.
749 ** If they are equal, go through and negate all sizes.
751 for(lookLoop = arrayLoop; lookLoop < arrayCount; lookLoop++)
754 ** Skip negated items.
755 ** Skip zero length items.
757 if(NULL == symArray[lookLoop] || 0 == symArray[lookLoop]->mSize)
762 if(lookingFor == symArray[lookLoop]->mSize)
766 else if((-1 * lookingFor) == symArray[lookLoop]->mSize)
772 if(0 != identicalValues && identicalValues == oppositeValues)
774 unsigned negationLoop = 0;
776 for(negationLoop = arrayLoop; 0 != identicalValues || 0 != oppositeValues; negationLoop++)
779 ** Skip negated items.
780 ** Skip zero length items.
782 if(NULL == symArray[negationLoop] || 0 == symArray[negationLoop]->mSize)
788 ** Negate any size matches.
789 ** Reflect the change in the object as well.
792 if(lookingFor == symArray[negationLoop]->mSize)
794 objArray[negationLoop]->mSize -= lookingFor;
795 symArray[negationLoop]->mSize = 0;
796 symArray[negationLoop] = NULL;
800 else if((-1 * lookingFor) == symArray[negationLoop]->mSize)
802 objArray[negationLoop]->mSize += lookingFor;
803 symArray[negationLoop]->mSize = 0;
804 symArray[negationLoop] = NULL;
822 ** If all went well, time to report.
827 ** Loop through our data once more, so that the symbols can
828 ** propigate their changes upwards in a positive/negative
830 ** This will help give the composite change more meaning.
832 for(moduleLoop = 0; moduleLoop < moduleCount; moduleLoop++)
834 theModule = modules + moduleLoop;
837 ** Skip if there is zero drift, or no net change.
839 if(0 == inOptions->mZeroDrift && 0 == (theModule->mSize.mCode + theModule->mSize.mData))
844 for(segmentLoop = 0; segmentLoop < theModule->mSegmentCount; segmentLoop++)
846 theSegment = theModule->mSegments + segmentLoop;
849 ** Skip if there is zero drift, or no net change.
851 if(0 == inOptions->mZeroDrift && 0 == theSegment->mSize)
856 for(objectLoop = 0; objectLoop < theSegment->mObjectCount; objectLoop++)
858 theObject = theSegment->mObjects + objectLoop;
861 ** Skip if there is zero drift, or no net change.
863 if(0 == inOptions->mZeroDrift && 0 == theObject->mSize)
868 for(symbolLoop = 0; symbolLoop < theObject->mSymbolCount; symbolLoop++)
870 theSymbol = theObject->mSymbols + symbolLoop;
873 ** Propagate the composition all the way to the top.
874 ** Sizes of zero change are skipped.
876 if(0 < theSymbol->mSize)
878 theObject->mComposition.mPositive += theSymbol->mSize;
879 theSegment->mComposition.mPositive += theSymbol->mSize;
880 if(CODE == theSegment->mClass)
882 overall.mCodeComposition.mPositive += theSymbol->mSize;
883 theModule->mSize.mCodeComposition.mPositive += theSymbol->mSize;
885 else if(DATA == theSegment->mClass)
887 overall.mDataComposition.mPositive += theSymbol->mSize;
888 theModule->mSize.mDataComposition.mPositive += theSymbol->mSize;
891 else if(0 > theSymbol->mSize)
893 theObject->mComposition.mNegative += theSymbol->mSize;
894 theSegment->mComposition.mNegative += theSymbol->mSize;
895 if(CODE == theSegment->mClass)
897 overall.mCodeComposition.mNegative += theSymbol->mSize;
898 theModule->mSize.mCodeComposition.mNegative += theSymbol->mSize;
900 else if(DATA == theSegment->mClass)
902 overall.mDataComposition.mNegative += theSymbol->mSize;
903 theModule->mSize.mDataComposition.mNegative += theSymbol->mSize;
912 if(inOptions->mSummaryOnly)
914 fprintf(inOptions->mOutput, "%+d (%+d/%+d)\n", overall.mCode + overall.mData, overall.mCodeComposition.mPositive + overall.mDataComposition.mPositive, overall.mCodeComposition.mNegative + overall.mDataComposition.mNegative);
918 fprintf(inOptions->mOutput, "Overall Change in Size\n");
919 fprintf(inOptions->mOutput, "\tTotal:\t%+11d (%+d/%+d)\n", overall.mCode + overall.mData, overall.mCodeComposition.mPositive + overall.mDataComposition.mPositive, overall.mCodeComposition.mNegative + overall.mDataComposition.mNegative);
920 fprintf(inOptions->mOutput, "\tCode:\t%+11d (%+d/%+d)\n", overall.mCode, overall.mCodeComposition.mPositive, overall.mCodeComposition.mNegative);
921 fprintf(inOptions->mOutput, "\tData:\t%+11d (%+d/%+d)\n", overall.mData, overall.mDataComposition.mPositive, overall.mDataComposition.mNegative);
925 ** Check what else we should output.
927 if(0 == inOptions->mSummaryOnly && NULL != modules && moduleCount)
929 const char* segmentType = NULL;
932 ** We're going to sort everything.
934 qsort(modules, moduleCount, sizeof(ModuleStats), moduleCompare);
935 for(moduleLoop = 0; moduleLoop < moduleCount; moduleLoop++)
937 theModule = modules + moduleLoop;
939 qsort(theModule->mSegments, theModule->mSegmentCount, sizeof(SegmentStats), segmentCompare);
941 for(segmentLoop = 0; segmentLoop < theModule->mSegmentCount; segmentLoop++)
943 theSegment = theModule->mSegments + segmentLoop;
945 qsort(theSegment->mObjects, theSegment->mObjectCount, sizeof(ObjectStats), objectCompare);
947 for(objectLoop = 0; objectLoop < theSegment->mObjectCount; objectLoop++)
949 theObject = theSegment->mObjects + objectLoop;
951 qsort(theObject->mSymbols, theObject->mSymbolCount, sizeof(SymbolStats), symbolCompare);
957 ** Loop through for output.
959 for(moduleLoop = 0; moduleLoop < moduleCount; moduleLoop++)
961 theModule = modules + moduleLoop;
964 ** Skip if there is zero drift, or no net change.
966 if(0 == inOptions->mZeroDrift && 0 == (theModule->mSize.mCode + theModule->mSize.mData))
971 fprintf(inOptions->mOutput, "\n");
972 fprintf(inOptions->mOutput, "%s\n", theModule->mModule);
973 fprintf(inOptions->mOutput, "\tTotal:\t%+11d (%+d/%+d)\n", theModule->mSize.mCode + theModule->mSize.mData, theModule->mSize.mCodeComposition.mPositive + theModule->mSize.mDataComposition.mPositive, theModule->mSize.mCodeComposition.mNegative + theModule->mSize.mDataComposition.mNegative);
974 fprintf(inOptions->mOutput, "\tCode:\t%+11d (%+d/%+d)\n", theModule->mSize.mCode, theModule->mSize.mCodeComposition.mPositive, theModule->mSize.mCodeComposition.mNegative);
975 fprintf(inOptions->mOutput, "\tData:\t%+11d (%+d/%+d)\n", theModule->mSize.mData, theModule->mSize.mDataComposition.mPositive, theModule->mSize.mDataComposition.mNegative);
977 for(segmentLoop = 0; segmentLoop < theModule->mSegmentCount; segmentLoop++)
979 theSegment = theModule->mSegments + segmentLoop;
982 ** Skip if there is zero drift, or no net change.
984 if(0 == inOptions->mZeroDrift && 0 == theSegment->mSize)
989 if(CODE == theSegment->mClass)
991 segmentType = "CODE";
993 else if(DATA == theSegment->mClass)
995 segmentType = "DATA";
998 fprintf(inOptions->mOutput, "\t%+11d (%+d/%+d)\t%s (%s)\n", theSegment->mSize, theSegment->mComposition.mPositive, theSegment->mComposition.mNegative, theSegment->mSegment, segmentType);
1000 for(objectLoop = 0; objectLoop < theSegment->mObjectCount; objectLoop++)
1002 theObject = theSegment->mObjects + objectLoop;
1005 ** Skip if there is zero drift, or no net change.
1007 if(0 == inOptions->mZeroDrift && 0 == theObject->mSize)
1012 fprintf(inOptions->mOutput, "\t\t%+11d (%+d/%+d)\t%s\n", theObject->mSize, theObject->mComposition.mPositive, theObject->mComposition.mNegative, theObject->mObject);
1014 for(symbolLoop = 0; symbolLoop < theObject->mSymbolCount; symbolLoop++)
1016 theSymbol = theObject->mSymbols + symbolLoop;
1019 ** Skip if there is zero drift, or no net change.
1021 if(0 == inOptions->mZeroDrift && 0 == theSymbol->mSize)
1026 fprintf(inOptions->mOutput, "\t\t\t%+11d\t%s\n", theSymbol->mSize, theSymbol->mSymbol);
1037 for(moduleLoop = 0; moduleLoop < moduleCount; moduleLoop++)
1039 theModule = modules + moduleLoop;
1041 for(segmentLoop = 0; segmentLoop < theModule->mSegmentCount; segmentLoop++)
1043 theSegment = theModule->mSegments + segmentLoop;
1045 for(objectLoop = 0; objectLoop < theSegment->mObjectCount; objectLoop++)
1047 theObject = theSegment->mObjects + objectLoop;
1049 for(symbolLoop = 0; symbolLoop < theObject->mSymbolCount; symbolLoop++)
1051 theSymbol = theObject->mSymbols + symbolLoop;
1053 CLEANUP(theSymbol->mSymbol);
1056 CLEANUP(theObject->mSymbols);
1057 CLEANUP(theObject->mObject);
1060 CLEANUP(theSegment->mObjects);
1061 CLEANUP(theSegment->mSegment);
1064 CLEANUP(theModule->mSegments);
1065 CLEANUP(theModule->mModule);
1073 int initOptions(Options* outOptions, int inArgc, char** inArgv)
1075 ** returns int 0 if successful.
1082 const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]);
1083 Switch* current = NULL;
1086 ** Set any defaults.
1088 memset(outOptions, 0, sizeof(Options));
1089 outOptions->mProgramName = inArgv[0];
1090 outOptions->mInput = stdin;
1091 outOptions->mInputName = strdup("stdin");
1092 outOptions->mOutput = stdout;
1093 outOptions->mOutputName = strdup("stdout");
1095 if(NULL == outOptions->mOutputName || NULL == outOptions->mInputName)
1098 ERROR_REPORT(retval, "stdin/stdout", "Unable to strdup.");
1102 ** Go through and attempt to do the right thing.
1104 for(loop = 1; loop < inArgc && 0 == retval; loop++)
1109 for(switchLoop = 0; switchLoop < switchCount && 0 == retval; switchLoop++)
1111 if(0 == strcmp(gSwitches[switchLoop]->mLongName, inArgv[loop]))
1115 else if(0 == strcmp(gSwitches[switchLoop]->mShortName, inArgv[loop]))
1122 if(gSwitches[switchLoop]->mHasValue)
1125 ** Attempt to absorb next option to fullfill value.
1127 if(loop + 1 < inArgc)
1131 current = gSwitches[switchLoop];
1132 current->mValue = inArgv[loop];
1137 current = gSwitches[switchLoop];
1146 outOptions->mHelp = __LINE__;
1148 ERROR_REPORT(retval, inArgv[loop], "Unknown command line switch.");
1150 else if(NULL == current)
1152 outOptions->mHelp = __LINE__;
1154 ERROR_REPORT(retval, inArgv[loop], "Command line switch requires a value.");
1159 ** Do something based on address/swtich.
1161 if(current == &gInputSwitch)
1163 CLEANUP(outOptions->mInputName);
1164 if(NULL != outOptions->mInput && stdin != outOptions->mInput)
1166 fclose(outOptions->mInput);
1167 outOptions->mInput = NULL;
1170 outOptions->mInput = fopen(current->mValue, "r");
1171 if(NULL == outOptions->mInput)
1174 ERROR_REPORT(retval, current->mValue, "Unable to open input file.");
1178 outOptions->mInputName = strdup(current->mValue);
1179 if(NULL == outOptions->mInputName)
1182 ERROR_REPORT(retval, current->mValue, "Unable to strdup.");
1186 else if(current == &gOutputSwitch)
1188 CLEANUP(outOptions->mOutputName);
1189 if(NULL != outOptions->mOutput && stdout != outOptions->mOutput)
1191 fclose(outOptions->mOutput);
1192 outOptions->mOutput = NULL;
1195 outOptions->mOutput = fopen(current->mValue, "a");
1196 if(NULL == outOptions->mOutput)
1199 ERROR_REPORT(retval, current->mValue, "Unable to open output file.");
1203 outOptions->mOutputName = strdup(current->mValue);
1204 if(NULL == outOptions->mOutputName)
1207 ERROR_REPORT(retval, current->mValue, "Unable to strdup.");
1211 else if(current == &gHelpSwitch)
1213 outOptions->mHelp = __LINE__;
1215 else if(current == &gSummarySwitch)
1217 outOptions->mSummaryOnly = __LINE__;
1219 else if(current == &gZeroDriftSwitch)
1221 outOptions->mZeroDrift = __LINE__;
1223 else if(current == &gNegationSwitch)
1225 outOptions->mNegation = __LINE__;
1230 ERROR_REPORT(retval, current->mLongName, "No handler for command line switch.");
1239 void cleanOptions(Options* inOptions)
1241 ** Clean up any open handles.
1244 CLEANUP(inOptions->mInputName);
1245 if(NULL != inOptions->mInput && stdin != inOptions->mInput)
1247 fclose(inOptions->mInput);
1249 CLEANUP(inOptions->mOutputName);
1250 if(NULL != inOptions->mOutput && stdout != inOptions->mOutput)
1252 fclose(inOptions->mOutput);
1255 memset(inOptions, 0, sizeof(Options));
1259 void showHelp(Options* inOptions)
1261 ** Show some simple help text on usage.
1265 const int switchCount = sizeof(gSwitches) / sizeof(gSwitches[0]);
1266 const char* valueText = NULL;
1268 printf("usage:\t%s [arguments]\n", inOptions->mProgramName);
1270 printf("arguments:\n");
1272 for(loop = 0; loop < switchCount; loop++)
1274 if(gSwitches[loop]->mHasValue)
1276 valueText = " <value>";
1283 printf("\t%s%s\n", gSwitches[loop]->mLongName, valueText);
1284 printf("\t %s%s", gSwitches[loop]->mShortName, valueText);
1285 printf(DESC_NEWLINE "%s\n\n", gSwitches[loop]->mDescription);
1288 printf("This tool takes the diff of two sorted tsv files to form a summary report\n");
1289 printf("of code and data size changes which is hoped to be human readable.\n");
1293 int main(int inArgc, char** inArgv)
1298 retval = initOptions(&options, inArgc, inArgv);
1303 else if(0 == retval)
1305 retval = difftool(&options);
1308 cleanOptions(&options);