ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

stubgen.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <malloc.h>
00003 #include <string.h>
00004 #include <ctype.h>
00005 
00006 #ifdef _WIN32
00007 #define popen _popen
00008 #define snprintf _snprintf
00009 #endif
00010 
00011 typedef struct _stub {
00012     char *name;
00013     char *origin;
00014     struct _stub *next;
00015 } stub;
00016 
00017 void usage( char *name ) {
00018     fprintf( stderr,
00019         "Usage: %s [-n nm] [-m make] libs...\n"
00020         "nm   -- The command used to run nm on reactos objects\n"
00021         "make -- The command used to build reactos\n\n"
00022         "libs are import libraries (.a files) typically from\n"
00023         "dk/lib/nkm and dk/lib/w32\n",
00024         name );
00025 }
00026 
00027 int main( int argc, char **argv ) {
00028     char line[1024];
00029     char *make = "make";
00030     char *nm = "nm";
00031     char *origin = "unknown.a";
00032     stub *functions = NULL, *new_f, *imports = NULL, *search;
00033     FILE *make_f, *nm_f;
00034     int i, libstart = argc;
00035     FILE *out = fopen("tests/stubs.tst","w");
00036 
00037     if( argc == 1 ) {
00038         if( out ) fclose( out );
00039         usage(argv[0]);
00040         return 1;
00041     }
00042 
00043     if( !out ) {
00044         fprintf( stderr, "Could not write file tests/stubs.tst\n" );
00045         return 1;
00046     }
00047 
00048     fprintf( out, "# Automatically generated by stubgen\n" );
00049 
00050     for( i = 1; i < argc; i++ ) {
00051         if( !strcmp( argv[i], "-m" ) ) {
00052             make = argv[i+1];
00053             i++;
00054         } else if( !strcmp( argv[i], "-n" ) ) {
00055             nm = argv[i+1];
00056             i++;
00057         } else { libstart = i; break; }
00058     }
00059 
00060     snprintf( line, sizeof(line), "%s test 2>&1", make );
00061     make_f = popen( line, "r" );
00062 
00063     if( !make_f ) {
00064         fclose( out );
00065         fprintf( stderr, "Could not run %s test\n", make );
00066         return 1;
00067     }
00068 
00069     while( fgets( line, sizeof(line), make_f ) ) {
00070         char *end_of_location;
00071         char *begin_q, *end_q;
00072 
00073         if( !strstr( line, "undefined reference to" ) ) continue;
00074 
00075         end_of_location = strrchr( line, ':' );
00076 
00077         if( !end_of_location ) continue;
00078 
00079         begin_q = strchr( end_of_location, '`' );
00080         end_q = strchr( end_of_location, '\'' );
00081 
00082         if( !begin_q || !end_q ) continue;
00083 
00084         begin_q += 2; /* skip `_ */
00085 
00086         memmove( line, begin_q, end_q - begin_q );
00087         line[end_q - begin_q] = 0;
00088 
00089         for( new_f = functions; new_f; new_f = new_f->next )
00090             if( !strcmp( new_f->name, line ) ) break;
00091 
00092         if( new_f ) continue;
00093 
00094         new_f = (stub *)malloc( sizeof(stub) );
00095         if( !new_f ) 
00096         {
00097             fprintf( stderr, "Out of memory\n" ); 
00098             fclose( out );
00099             pclose( make_f );
00100             return 1;
00101         }
00102 
00103         new_f->name = strdup( line );
00104         new_f->next = functions;
00105         functions = new_f;
00106     }
00107 
00108     /* Scan libraries and collect available import sections */
00109     for( i = libstart; i < argc; i++ ) {
00110         snprintf( line, sizeof(line), "%s %s", nm, argv[i] );
00111         nm_f = popen( line, "r" );
00112 
00113         for( origin = argv[i]; *argv[i]; argv[i]++ )
00114             if( *argv[i] == '/' || *argv[i] == '\\' )
00115                 origin = argv[i] + 1;
00116 
00117 
00118         if( !nm_f ) {
00119             fprintf( stderr, "Could not run %s\n", line );
00120             continue;
00121         }
00122 
00123         while( fgets( line, sizeof(line), nm_f ) ) {
00124             char *import_sign, *eol;
00125 
00126             if( !(import_sign = strstr( line, " I " )) ) continue;
00127 
00128             import_sign += 3;
00129             while( *import_sign && isspace(*import_sign) ) import_sign++;
00130 
00131             /* Strip ws after name */
00132             for( eol = import_sign; *eol && !isspace(*eol); eol++ );
00133 
00134             *eol = 0;
00135 
00136             for( new_f = imports; new_f; new_f = new_f->next )
00137                 if( !strcmp( new_f->name, import_sign ) ) break;
00138 
00139             if( new_f ) continue;
00140 
00141             new_f = (stub *)malloc( sizeof(stub) );
00142             if( !new_f ) 
00143             {
00144                 fprintf( stderr, "Out of memory\n" ); 
00145                 fclose( out );
00146                 pclose( make_f );
00147                 pclose( nm_f );
00148                 return 1;
00149             }
00150 
00151             new_f->name   = strdup( import_sign + 1 );
00152             new_f->origin = origin;
00153             new_f->next   = imports;
00154             imports = new_f;
00155         }
00156 
00157         pclose( nm_f );
00158     }
00159 
00160     /* Now we have a list of unique functions and a list of imports,
00161     lookup each function and output the entry from the import list. */
00162     for( new_f = functions; new_f; new_f = new_f->next ) {
00163         for( search = imports; search; search = search->next ) {
00164             if( !strcmp( new_f->name, search->name ) ) {
00165                 fprintf( out, "%s %s\n", search->origin, search->name );
00166                 continue;
00167             }
00168         }
00169     }
00170 
00171     fclose( out );
00172     pclose( make_f );
00173     return 0;
00174 }

Generated on Sun May 27 2012 04:37:48 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.