Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
5.4 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. ws2hdr.c
  5. Abstract:
  6. Munges the WinSock 2.0 header file.
  7. This program scans stdin, searching for begin and end tags. Lines of
  8. text between these tags are assumed to be function prototypes of the
  9. form:
  10. function_linkage
  11. return_type
  12. calling_convention
  13. function_name(
  14. parameters,
  15. parameters,
  16. ...
  17. );
  18. For each such function prototype found, the following is output:
  19. #if INCL_WINSOCK_API_PROTOTYPES
  20. function_linkage
  21. return_type
  22. calling_convention
  23. function_name(
  24. parameters,
  25. parameters,
  26. ...
  27. );
  28. #endif
  29. #if INCL_WINSOCK_API_TYPEDEFS
  30. typedef
  31. return_type
  32. (calling_convention * LPFN_FUNCTION_NAME)(
  33. parameters,
  34. parameters,
  35. ...
  36. );
  37. #endif
  38. Author:
  39. Keith Moore (keithmo) 09-Dec-1995
  40. Revision History:
  41. --*/
  42. #include <windows.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. //
  46. // Private constants.
  47. //
  48. #define MAX_HEADER_LINE 128
  49. #define MAX_API_LINES 32
  50. #define BEGIN_APIS "/*BEGIN_APIS*/"
  51. #define END_APIS "/*END_APIS*/"
  52. INT
  53. __cdecl
  54. main(
  55. INT argc,
  56. CHAR * argv[]
  57. )
  58. {
  59. CHAR lineBuffer[MAX_HEADER_LINE];
  60. CHAR apiBuffer[MAX_API_LINES][MAX_HEADER_LINE];
  61. INT i;
  62. INT apiLineNumber = 0;
  63. INT fileLineNumber = 0;
  64. BOOL inApis = FALSE;
  65. BOOL beginApis;
  66. BOOL endApis;
  67. //
  68. // This app takes no command line arguments.
  69. //
  70. if( argc != 1 ) {
  71. fprintf(
  72. stderr,
  73. "WS2HDR v1.01 " __DATE__ "\n"
  74. );
  75. fprintf(
  76. stderr,
  77. "use: ws2hdr < file1 > file2\n"
  78. );
  79. return 1;
  80. }
  81. //
  82. // Read stdin until exhausted.
  83. //
  84. while( fgets( lineBuffer, sizeof(lineBuffer), stdin ) != NULL ) {
  85. fileLineNumber++;
  86. //
  87. // fgets() leaves the terminating '\n' on the string; remove it.
  88. //
  89. lineBuffer[strlen(lineBuffer) - 1] = '\0';
  90. //
  91. // Check for our tags.
  92. //
  93. beginApis = FALSE;
  94. endApis = FALSE;
  95. if( _stricmp( lineBuffer, BEGIN_APIS ) == 0 ) {
  96. beginApis = TRUE;
  97. } else if( _stricmp( lineBuffer, END_APIS ) == 0 ) {
  98. endApis = TRUE;
  99. }
  100. //
  101. // Warn if we got an invalid tag.
  102. //
  103. if( beginApis && inApis ) {
  104. fprintf(
  105. stderr,
  106. "WARNING: unexpected %s, line %d\n",
  107. BEGIN_APIS,
  108. fileLineNumber
  109. );
  110. continue;
  111. }
  112. if( endApis && !inApis ) {
  113. fprintf(
  114. stderr,
  115. "WARNING: unexpected %s, line %d\n",
  116. END_APIS,
  117. fileLineNumber
  118. );
  119. continue;
  120. }
  121. //
  122. // Remember if we're currently between tags.
  123. //
  124. if( beginApis ) {
  125. inApis = TRUE;
  126. continue;
  127. }
  128. if( endApis ) {
  129. inApis = FALSE;
  130. continue;
  131. }
  132. //
  133. // If we're not between tags, or if the line is empty, just
  134. // output the line.
  135. //
  136. if( !inApis ) {
  137. printf( "%s\n", lineBuffer );
  138. continue;
  139. }
  140. if( lineBuffer[0] == '\0' ) {
  141. printf( "\n" );
  142. continue;
  143. }
  144. //
  145. // Add the line to our buffer. If the line doesn't end in ';',
  146. // then we're not at the end of the prototype, so keep reading
  147. // and scanning.
  148. //
  149. strcpy( &apiBuffer[apiLineNumber++][0], lineBuffer );
  150. if( lineBuffer[strlen(lineBuffer) - 1] != ';' ) {
  151. continue;
  152. }
  153. //
  154. // At this point the following are established in apiBuffer:
  155. //
  156. // apiBuffer[0] == function linkage
  157. // apiBuffer[1] == return type
  158. // apiBuffer[2] == calling convention
  159. // apiBuffer[3] == function name (with trailing '(')
  160. // apiBuffer[4..n-1] == parameters
  161. // apiBuffers[n] == ");"
  162. //
  163. //
  164. // First, dump out the prototype with its appropriate CPP protector.
  165. //
  166. printf( "#if INCL_WINSOCK_API_PROTOTYPES\n" );
  167. for( i = 0 ; i < apiLineNumber ; i++ ) {
  168. printf( "%s\n", &apiBuffer[i][0] );
  169. }
  170. printf( "#endif // INCL_WINSOCK_API_PROTOTYPES\n" );
  171. printf( "\n" );
  172. //
  173. // Now dump out the typedef with its appropriate CPP protector.
  174. //
  175. // Note that we must munge the api function name around a bit
  176. // first. Specifically, we remove the trailing '(' and map the
  177. // name to uppercase.
  178. //
  179. printf( "#if INCL_WINSOCK_API_TYPEDEFS\n" );
  180. apiBuffer[3][strlen( &apiBuffer[3][0] ) - 1] = '\0';
  181. _strupr( &apiBuffer[3][0] );
  182. printf( "typedef\n" );
  183. printf( "%s\n", &apiBuffer[1][0] );
  184. printf( "(%s * LPFN_%s)(\n", &apiBuffer[2][0], &apiBuffer[3][0] );
  185. for( i = 4 ; i < apiLineNumber ; i++ ) {
  186. printf( "%s\n", &apiBuffer[i][0] );
  187. }
  188. printf( "#endif // INCL_WINSOCK_API_TYPEDEFS\n" );
  189. //
  190. // Start over at the next input line.
  191. //
  192. apiLineNumber = 0;
  193. }
  194. return 0;
  195. } // main