int main(int argc, char *argv[]) { char configFileName[255]; const int pid = getpid(); int createflag; extern int logflag, quietflag; int create_f, quiet_f; /* log_f is a static global */ createflag = logflag = quietflag = FALSE; create_f = log_f = quiet_f = FLAG_NOT_SET; // Begin command line parsing *********************************************** if ( (checkForOption("--help", argc, argv) != FLAG_NOT_SET) || (checkForOption("-h", argc, argv) != FLAG_NOT_SET) || (checkForOption("-help", argc, argv) != FLAG_NOT_SET) ) { print_help(); } get_asf_share_dir_with_argv0(argv[0]); handle_license_and_version_args(argc, argv, ASF_NAME_STRING); // This is an undocumented option, for internal use (by the GUI) int save_dem = extract_flag_options(&argc, &argv, "-save-dem", "--save-dem", NULL); // Check which options were provided create_f = checkForOption("-create", argc, argv); log_f = checkForOption("-log", argc, argv); quiet_f = checkForOption("-quiet", argc, argv); // We need to make sure the user specified the proper number of arguments int needed_args = 1 + REQUIRED_ARGS; // command & REQUIRED_ARGS int num_flags = 0; if (create_f != FLAG_NOT_SET) {needed_args += 1; num_flags++;} // option if (log_f != FLAG_NOT_SET) {needed_args += 2; num_flags++;} // option & param if (quiet_f != FLAG_NOT_SET) {needed_args += 1; num_flags++;} // option // Make sure we have the right number of args if(argc != needed_args) { print_usage(); } // Make sure argument for each flag (that requires an arg) is not another // option flag & is not a required argument if (log_f != FLAG_NOT_SET) { if ( (argv[log_f+1][0]=='-') || (log_f>=(argc-REQUIRED_ARGS)) ) { print_usage(); } } // Make sure all options occur before the config file name argument if (num_flags == 1 && (create_f > 1 || log_f > 1 || quiet_f > 1)) { print_usage(); } else if (num_flags > 1 && (create_f >= argc - REQUIRED_ARGS - 1 || log_f >= argc - REQUIRED_ARGS - 1 || quiet_f >= argc - REQUIRED_ARGS - 1)) { print_usage(); } // Do the actual flagging & such for each flag if (create_f != FLAG_NOT_SET) { createflag = TRUE; } if (log_f != FLAG_NOT_SET) { strcpy(logFile, argv[log_f+1]); } else { // default behavior: log to tmp<pid>.log sprintf(logFile, "tmp%i.log", pid); } logflag = TRUE; fLog = FOPEN(logFile, "a"); // Set old school quiet flag (for use in our libraries) quietflag = quiet_f != FLAG_NOT_SET; // Fetch required arguments strcpy(configFileName, argv[argc-1]); // Report the command line asfSplashScreen(argc, argv); // End command line parsing ************************************************* asf_convert_ext(createflag, configFileName, save_dem); // remove log file if we created it (leave it if the user asked for it) FCLOSE(fLog); if (log_f == FLAG_NOT_SET) remove(logFile); return(EXIT_SUCCESS); }
static char * do_convert(int pid, GtkTreeIter *iter, char *cfg_file, int save_dem, int keep_files, char **intermediates_file) { FILE *output; char *logFile = appendExt(cfg_file, ".log"); gtk_list_store_set(list_store, iter, COL_STATUS, "Processing...", -1); #ifdef win32 STARTUPINFO si; PROCESS_INFORMATION pi; memset(&si, 0, sizeof(si)); memset(&pi, 0, sizeof(pi)); si.cb = sizeof(si); char *cmd = MALLOC(sizeof(char)* (strlen(cfg_file) + strlen(logFile) + strlen(get_asf_bin_dir_win()) + 256)); sprintf(cmd, "\"%s/asf_mapready.exe\" %s-log \"%s\" \"%s\"", get_asf_bin_dir_win(), save_dem ? "--save-dem " : "", logFile, cfg_file); fLog = fopen(logFile, "a"); log_summary_text(fLog); FCLOSE(fLog); //printf("Running command> %s\n", cmd); if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { DWORD dw = GetLastError(); //printf( "CreateProcess failed (%ld)\n", dw ); LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); printf("CreateProcess() failed with error %ld: %s\n", dw, (char*)lpMsgBuf); printf("Failed command: %s\n", cmd); } char *statFile = appendExt(cfg_file, ".status"); DWORD dwWaitResult; int counter = 1; // now wait for process to finish do { while (gtk_events_pending()) gtk_main_iteration(); if (++counter % 200 == 0) { // check status file char buf[256]; FILE *fStat = fopen(statFile, "r"); if (fStat) { fgets(buf, sizeof(buf), fStat); fclose(fStat); gtk_list_store_set(list_store, iter, COL_STATUS, buf, -1); } } dwWaitResult = WaitForSingleObject(pi.hProcess, 50); } while (dwWaitResult == WAIT_TIMEOUT); remove_file(statFile); free(statFile); // Don't do this, CreateProcess() takes it //free(cmd); #else extern int logflag; extern FILE *fLog; pid = fork(); if (pid == 0) { /* child */ logflag = TRUE; fLog = fopen(logFile, "a"); asfPrintStatus("Running MapReady with configuration file: %s\n", cfg_file); log_summary_text(fLog); asf_convert_ext(FALSE, cfg_file, save_dem); FCLOSE(fLog); exit(EXIT_SUCCESS); } else { /* parent */ int counter = 1; char *statFile = appendExt(cfg_file, ".status"); while (waitpid(pid, NULL, WNOHANG) == 0) { while (gtk_events_pending()) gtk_main_iteration(); g_usleep(50); if (++counter % 200 == 0) { /* check status file */ char buf[256]; FILE *fStat = fopen(statFile, "r"); if (fStat) { if (fgets(buf, sizeof(buf), fStat)) fclose(fStat); else strcpy(buf,""); gtk_list_store_set(list_store, iter, COL_STATUS, buf, -1); if (strcmp(buf, "Done")==0 || strcmp(buf, "Error")==0) { // kludge: // Status file says "Done" but we're still here. // This could happen because it *just* finished // during the most recent g_usleep(), but a much more // likely reason is that the waitpid() detection // failed. (I say "much more likely" because waitpid // is checked 200x more often than the status file) // UPDATE! // We figured out why this is occurring on Linux, // it is a bug in the GtkFileChooser. Since the // chooser is much nicer than the older FileSelector, // we'll keep this kludge... seems to be no other // side effects... hopefully... // Expanded the kludge to update the status file // with "Error", so that even if the process exits // with an error we'll still be ok. This only // leaves the core-dump case as a loose end. #ifdef linux // On Linux (which is actually the only platform // I have seen this problem), we can try to kill // the zombie process. kill(pid, 9); #endif // We'll break out of the loop now, and // possibly leave a zombie process around, if this // were to occur somewhere other than Linux break; } } } } remove_file(statFile); free(statFile); } #endif gchar *the_output = NULL; output = fopen(logFile, "r"); // see if we got a file containing a list of useful intermediate files *intermediates_file = appendExt(cfg_file, ".files"); if (!fileExists(*intermediates_file)) { free(*intermediates_file); *intermediates_file = NULL; } if (!output) { the_output = (gchar *)g_malloc(512); sprintf(the_output, "Error Opening Log File: %s\n", strerror(errno)); } else { gchar buffer[4096]; gchar *p = fgets(buffer, sizeof(buffer), output); while (!feof(output)) { if (p && !g_str_has_prefix(p, "Processing ")) { if (the_output) { the_output = (gchar *)g_realloc(the_output, sizeof(gchar) * (strlen(the_output) + strlen(buffer) + 1)); strcat(the_output, buffer); } else { the_output = (gchar *) g_malloc(sizeof(gchar) * (strlen(buffer) + 1)); strcpy(the_output, buffer); } } p = fgets(buffer, sizeof(buffer), output); } fclose(output); if (!keep_files) remove_file(logFile); } if (!the_output) { /* Log file existed, but had immediate EOF */ /* This is most likely caused by a "Disk Full" situation... */ /* ... or a segmentation fault! */ the_output = g_strdup("Error Opening Log File: Disk Full?\n"); } free(logFile); return the_output; }