#include #include #include #include #include int processOneFile(char *thecommand, int run); int main(int argc, char *argv[]) { char command[8192]; int i=0; int run = 0; char classpath[2048]; strcpy(command,getenv( "JAVABIN" )); i = 1; // Start at first if ( strcmp( argv[i], "-run" ) == 0 ) { i++; // Skip it strcat(command,"\\java "); run = 1; printf( "Using JHook to call JDK 1.1 VM\n" ); } else { strcat(command,"\\javac "); printf( "Using JHook to call JDK 1.1 Compiler\n" ); } // Hard code the ClassPath sprintf( classpath, "-classpath .;%s ", getenv( "JAVACLASS" )); strcat( command, classpath ); for (/* i is set above */; i < argc; i++) { char *arg = argv[i]; if ( strncmp( arg, "/w", 2 ) == 0 ) // ignore warning level = switch continue; if ( strcmp(arg, "/g") == 0 ) { strcat(command, "-g "); continue; } if (strcmp(arg, "/cp:p") == 0) { ++i; // Skip J++ ideas about classpath completely continue; } // Taking filenames from file - passed by 'compiler' if (arg[0] == '@') { FILE *fp; if ((fp = fopen(&arg[1], "r")) == NULL) { printf("Unable to open filespec file"); return 1; } char file[4096]; while (fscanf(fp, "%s\n", file) == 1) { char newCommand[8192]; sprintf( newCommand, "%s %s", command, file ); if (processOneFile(newCommand, run) != 0) { break; } } if (fclose(fp) != 0) { printf("Problem closing filespec file\n"); } continue; } if ( strcmp( arg, "-run" ) != 0 ) // Copy over anything else (note errors!) sprintf( command, "%s %s", command, arg ); } if ( run ) { char wait[256]; printf( "Running '%s'\n", command ); system( command ); gets( wait ); } return 0; } int processOneFile(char *thecommand, int run) { const int lineBufSize = 255; char command[8192]; strcpy(command, thecommand); int foundError = 0; SECURITY_ATTRIBUTES sa = {0}; STARTUPINFO si = {0}; PROCESS_INFORMATION pi = {0}; HANDLE hPipeWrite = NULL; HANDLE hPipeRead = NULL; CHAR Buffer[lineBufSize]; sa.nLength = sizeof(sa); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; CreatePipe(&hPipeRead, // read handle &hPipeWrite, // write handle &sa, // security attributes 0 // number of bytes reserved for pipe ); si.cb = sizeof(si); si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; si.wShowWindow = SW_HIDE; si.hStdInput = NULL; si.hStdOutput = hPipeWrite; si.hStdError = hPipeWrite; CreateProcess (NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); CloseHandle(hPipeWrite); int pos = 0; char output_line[8192]; int bytesInBuffer = 0; int bytesToRead = lineBufSize; while(TRUE) { DWORD NumberOfBytesRead = 0; BOOL bTest=ReadFile(hPipeRead, // handle of the read = end of our pipe &Buffer[bytesInBuffer], // address of buffer that receives data bytesToRead, // number of bytes to read &NumberOfBytesRead,// address of number of bytes read NULL // non-overlapped. ); if (!bTest) { if (bytesInBuffer == 0) { return 0; } } NumberOfBytesRead += bytesInBuffer; if (NumberOfBytesRead == 0) { break; } int foundNewLine = 0; pos = 0; for (DWORD i = 0; i < NumberOfBytesRead; i++) { output_line[pos++] = Buffer[i]; if (Buffer[i] == '\n') { foundNewLine = 1; output_line[pos] = '\0'; if (output_line[1] == ':') { // good chance this is = an error message // translate line to jvc format char *cp = &output_line[2]; // skip over drive letter and colon while (*cp++ != ':'); // found start of line = number *(cp - 1) = '\0'; // change ':' to null char int error_line_num = 0; sscanf(cp, "%d", &error_line_num); while (*cp++ != ':'); // use any number we like for error number, char pos, etc. printf("%s(%d,20) : error J0049:%s", output_line, error_line_num, cp); foundError = 1; } else { printf("Javac: %s", output_line); } // shift remainder of buffer down for (DWORD j = i + 1; j < NumberOfBytesRead; j++) { Buffer[j - i - 1] = Buffer[j]; } bytesInBuffer = NumberOfBytesRead - i - 1; bytesToRead = lineBufSize - bytesInBuffer; break; } } // for if (!foundNewLine) { if (NumberOfBytesRead == lineBufSize) { printf("Line longer than buffer found: increase buffer size\n"); } else { bytesInBuffer = NumberOfBytesRead; bytesToRead = lineBufSize - bytesInBuffer; } } } WaitForSingleObject (pi.hProcess, INFINITE); CloseHandle (pi.hProcess); CloseHandle (hPipeRead); return foundError; }