int main(int argc, char *argv[]) { int count; seq_t seq1, seq2; hash_env_t he; collec_t res, rev_res; #if defined(DEBUG) && (DEBUG > 1) mcheck(NULL); mtrace(); #endif argv0 = argv[0]; if (setlocale(LC_ALL, "POSIX") == NULL) fprintf(stderr, "%s: Warning: could not set locale to POSIX\n", argv[0]); signal(SIGSEGV, bug_handler); #ifndef __MINGW32__ signal(SIGBUS, bug_handler); #endif /* Default options. */ options.C = DEFAULT_C; options.cutoff = DIST_CUTOFF; options.gapPct = DEFAULT_GAPPCT; options.intron_window = 6; options.K = DEFAULT_K; options.splice_type_list = "GTAG,GCAG,GTAC,ATAC"; options.nbSplice = 4; options.scoreSplice_window = 10; options.mismatchScore = MISMATCH; options.reverse = 2; options.matchScore = MATCH; options.W = DEFAULT_W; options.X = DEFAULT_X; options.filterPct = DEFAULT_FILTER; options.minScore_cutoff = MATCH_CUTOFF; while (1) { int c = getopt(argc, argv, "A:C:c:E:f:g:I:K:L:M:o:q:R:r:W:X:"); if (c == -1) break; switch (c) { case 'A': options.ali_flag = atoi(optarg); if (options.ali_flag < 0 || options.ali_flag > 4) fatal("A must be one of 0, 1, 2, 3, or 4.\n"); break; case 'C': { int val = atoi(optarg); if (val < 0) fatal("Value for option C must be non-negative.\n"); options.C = val; break; } case 'c': { int val = atoi(optarg); if (val < 0) fatal("Value for option c must be non-negative.\n"); options.minScore_cutoff = val; break; } case 'E': options.cutoff = atoi(optarg); if (options.cutoff < 3 || options.cutoff > 10) fatal("Cutoff (E) must be within [3,10].\n"); break; case 'f': options.filterPct = atoi(optarg); if (options.filterPct > 100) fatal("Filter in percent (f) must be within [0,100].\n"); break; case 'g': options.gapPct = atoi(optarg); break; case 'I': options.intron_window = atoi(optarg); break; case 'K': { int val = atoi(optarg); if (val < 0) fatal("Value for option K must be non-negative.\n"); options.K = val; break; } case 'L': { size_t i; size_t len = strlen(optarg); options.splice_type_list = optarg; options.nbSplice = 1; if (len % 5 != 4) fatal("Splice types list has illegal length (%zu)\n", len); for (i = 0; i < len; i++) if (i % 5 == 4) { if (options.splice_type_list[i] != ',') fatal("Comma expected instead of %c at position %zu" "in splice types list.\n", options.splice_type_list[i], i); options.nbSplice += 1; } else { if (options.splice_type_list[i] != 'A' && options.splice_type_list[i] != 'C' && options.splice_type_list[i] != 'G' && options.splice_type_list[i] != 'T') fatal("Expected 'A', 'C', 'G' or 'T' instead of '%c' at" "position %zu in splice types list.\n", options.splice_type_list[i], i); } break; } case 'M': { int val = atoi(optarg); if (val < 0) fatal("Value for option M must be non-negative.\n"); options.scoreSplice_window = val; break; } case 'o': options.dnaOffset = atoi(optarg); break; case 'q': options.mismatchScore = atoi(optarg); break; case 'R': options.reverse = atoi(optarg); if (options.reverse < 0 || options.reverse > 2) fatal("R must be one of 0, 1, or 2.\n"); break; case 'r': options.matchScore = atoi(optarg); break; case 'W': options.W = atoi(optarg); if (options.W < 1 || options.W > 15) fatal("W must be within [1,15].\n"); break; case 'X': options.X = atoi(optarg); if (options.X < 1) fatal("X must be positive.\n"); break; case '?': break; default: fprintf(stderr, "?? getopt returned character code 0%o ??\n", c); } } if (optind + 2 != argc) { fprintf(stderr, Usage, argv[0], options.ali_flag, options.C, options.minScore_cutoff, options.cutoff, options.filterPct, options.gapPct, options.intron_window, options.K, options.splice_type_list, options.scoreSplice_window, options.dnaOffset, options.mismatchScore, options.reverse, options.matchScore, options.W, options.X); return 1; } /* read seq1 */ init_seq(argv[optind], &seq1); if (get_next_seq(&seq1, options.dnaOffset, 1) != 0) fatal("Cannot read sequence from %s.\n", argv[optind]); strncpy(dna_seq_head, seq1.header, 256); /* read seq2 */ init_seq(argv[optind + 1], &seq2); if (get_next_seq(&seq2, 0, 0) != 0) fatal("Cannot read sequence from %s.\n", argv[optind + 1]); init_encoding(); init_hash_env(&he, options.W, seq1.seq, seq1.len); init_col(&res, 1); init_col(&rev_res, 1); bld_table(&he); init_splice_junctions(); count = 0; while (!count || get_next_seq(&seq2, 0, 0) == 0) { unsigned int curRes; strncpy(rna_seq_head, seq2.header, 256); ++count; switch (options.reverse) { case 0: SIM4(&he, &seq2, &res); break; case 2: SIM4(&he, &seq2, &res); case 1: seq_revcomp_inplace(&seq2); SIM4(&he, &seq2, &rev_res); break; default: fatal ("Unrecognized request for EST orientation.\n"); } /* Keep only the best matches, according to filterPct. */ if (options.filterPct > 0) { unsigned int max_nmatches = 0; for (curRes = 0; curRes < rev_res.nb; curRes++) { result_p_t r = rev_res.e.result[curRes]; if (r->st.nmatches > max_nmatches) max_nmatches = r->st.nmatches; } for (curRes = 0; curRes < res.nb; curRes++) { result_p_t r = res.e.result[curRes]; if (r->st.nmatches > max_nmatches) max_nmatches = r->st.nmatches; } max_nmatches = (max_nmatches * options.filterPct) / 100; for (curRes = 0; curRes < rev_res.nb; curRes++) { result_p_t r = rev_res.e.result[curRes]; if (r->st.nmatches < max_nmatches) r->st.nmatches = 0; } for (curRes = 0; curRes < res.nb; curRes++) { result_p_t r = res.e.result[curRes]; if (r->st.nmatches < max_nmatches) r->st.nmatches = 0; } } /* Now, print results. */ for (curRes = 0; curRes < rev_res.nb; curRes++) print_res(rev_res.e.result[curRes], 1, &seq1, &seq2); rev_res.nb = 0; if (options.reverse && options.ali_flag) /* reverse-complement back seq2 for alignment */ seq_revcomp_inplace(&seq2); for (curRes = 0; curRes < res.nb; curRes++) print_res(res.e.result[curRes], 0, &seq1, &seq2); res.nb = 0; } #ifdef DEBUG fprintf(stderr, "DEBUG mode: freeing all memory...\n"); fflush(stdout); fflush(stderr); free_hash_env(&he); free_seq(&seq1); free_seq(&seq2); free(options.splice); free(res.e.elt); free(rev_res.e.elt); #endif return 0; }
int main(int argc_orig, char **argv) #endif { int i; /* We want the current value of argc to persist across a LONGJMP from int_error(). * Without this the compiler may put it on the stack, which LONGJMP clobbers. * Here we try make it a volatile variable that optimization will not affect. * Why do we not have to do the same for argv? I don't know. * But the test cases that broke with generic argc seem fine with generic argv. */ static volatile int argc; argc = argc_orig; #ifdef LINUXVGA LINUX_setup(); /* setup VGA before dropping privilege DBT 4/5/99 */ drop_privilege(); #endif /* make sure that we really have revoked root access, this might happen if gnuplot is compiled without vga support but is installed suid by mistake */ #ifdef __linux__ if (setuid(getuid()) != 0) { fprintf(stderr,"gnuplot: refusing to run at elevated privilege\n"); exit(EXIT_FAILURE); } #endif /* HBB: Seems this isn't needed any more for DJGPP V2? */ /* HBB: disable all floating point exceptions, just keep running... */ #if defined(DJGPP) && (DJGPP!=2) _control87(MCW_EM, MCW_EM); #endif #if defined(OS2) { int rc; #ifdef OS2_IPC char semInputReadyName[40]; sprintf(semInputReadyName, "\\SEM32\\GP%i_Input_Ready", getpid()); rc = DosCreateEventSem(semInputReadyName, &semInputReady, 0, 0); if (rc != 0) fputs("DosCreateEventSem error\n", stderr); #endif rc = RexxRegisterSubcomExe("GNUPLOT", (PFN) RexxInterface, NULL); } #endif /* malloc large blocks, otherwise problems with fragmented mem */ #ifdef MALLOCDEBUG malloc_debug(7); #endif /* init progpath and get helpfile from executable directory */ #if defined(MSDOS) || defined(OS2) { char *s; #ifdef __EMX__ _execname(progpath, sizeof(progpath)); #else safe_strncpy(progpath, argv[0], sizeof(progpath)); #endif /* convert '/' to '\\' */ for (s = progpath; *s != NUL; s++) if (*s == DIRSEP2) *s = DIRSEP1; /* cut program name */ s = strrchr(progpath, DIRSEP1); if (s != NULL) s++; else s = progpath; *s = NUL; /* init HelpFile */ strcpy(HelpFile, progpath); strcat(HelpFile, "gnuplot.gih"); /* remove trailing "bin/" from progpath */ if ((s != NULL) && (s - progpath >= 4)) { s -= 4; if (strncasecmp(s, "bin", 3) == 0) *s = NUL; } } #endif /* DJGPP */ #if (defined(PIPE_IPC) || defined(_WIN32)) && (defined(HAVE_LIBREADLINE) || (defined(HAVE_LIBEDITLINE) && defined(X11))) /* Editline needs this to be set before the very first call to readline(). */ /* Support for rl_getc_function is broken for utf-8 in editline. Since it is only really required for X11, disable this section when building without X11. */ rl_getc_function = getc_wrapper; #endif #if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDITLINE) /* T.Walter 1999-06-24: 'rl_readline_name' must be this fix name. * It is used to parse a 'gnuplot' specific section in '~/.inputrc' * or gnuplot specific commands in '.editrc' (when using editline * instead of readline) */ rl_readline_name = "Gnuplot"; rl_terminal_name = getenv("TERM"); #if defined(HAVE_LIBREADLINE) using_history(); #else history_init(); #endif #endif #if defined(HAVE_LIBREADLINE) && !defined(MISSING_RL_TILDE_EXPANSION) rl_complete_with_tilde_expansion = 1; #endif for (i = 1; i < argc; i++) { if (!argv[i]) continue; if (!strcmp(argv[i], "-V") || !strcmp(argv[i], "--version")) { printf("gnuplot %s patchlevel %s\n", gnuplot_version, gnuplot_patchlevel); return 0; } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { printf( "Usage: gnuplot [OPTION] ... [FILE]\n" #ifdef X11 "for X11 options see 'help X11->command-line-options'\n" #endif " -V, --version\n" " -h, --help\n" " -p --persist\n" " -s --slow\n" " -d --default-settings\n" " -c scriptfile ARG1 ARG2 ... \n" " -e \"command1; command2; ...\"\n" "gnuplot %s patchlevel %s\n", gnuplot_version, gnuplot_patchlevel); #ifdef DEVELOPMENT_VERSION printf( #ifdef DIST_CONTACT "Report bugs to "DIST_CONTACT"\n" " or %s\n", #else "Report bugs to %s\n", #endif bug_email); #endif return 0; } else if (!strncmp(argv[i], "-persist", 2) || !strcmp(argv[i], "--persist") #ifdef _WIN32 || !stricmp(argv[i], "-noend") || !stricmp(argv[i], "/noend") #endif ) { persist_cl = TRUE; } else if (!strncmp(argv[i], "-slow", 2) || !strcmp(argv[i], "--slow")) { slow_font_startup = TRUE; } else if (!strncmp(argv[i], "-d", 2) || !strcmp(argv[i], "--default-settings")) { /* Skip local customization read from ~/.gnuplot */ skip_gnuplotrc = TRUE; } } #ifdef X11 /* the X11 terminal removes tokens that it recognizes from argv. */ { int n = X11_args(argc, argv); argv += n; argc -= n; } #endif setbuf(stderr, (char *) NULL); #ifdef HAVE_SETVBUF /* This was once setlinebuf(). Docs say this is * identical to setvbuf(,NULL,_IOLBF,0), but MS C * faults this (size out of range), so we try with * size of 1024 instead. [SAS/C does that, too. -lh] */ if (setvbuf(stdout, (char *) NULL, _IOLBF, (size_t) 1024) != 0) (void) fputs("Could not linebuffer stdout\n", stderr); /* Switching to unbuffered mode causes all characters in the input * buffer to be lost. So the only safe time to do it is on program entry. * Do any non-X platforms suffer from this problem? * EAM - Jan 2013 YES. */ setvbuf(stdin, (char *) NULL, _IONBF, 0); #endif gpoutfile = stdout; /* Initialize pre-loaded user variables */ /* "pi" is hard-wired as the first variable */ (void) add_udv_by_name("GNUTERM"); (void) add_udv_by_name("NaN"); init_constants(); udv_user_head = &(udv_NaN->next_udv); init_memory(); interactive = FALSE; /* April 2017: We used to call init_terminal() here, but now */ /* We defer initialization until error handling has been set up. */ # if defined(_WIN32) && !defined(WGP_CONSOLE) interactive = TRUE; # else interactive = isatty(fileno(stdin)); # endif /* Note: we want to know whether this is an interactive session so that we can * decide whether or not to write status information to stderr. The old test * for this was to see if (argc > 1) but the addition of optional command line * switches broke this. What we really wanted to know was whether any of the * command line arguments are file names or an explicit in-line "-e command". */ for (i = 1; i < argc; i++) { # ifdef _WIN32 if (!stricmp(argv[i], "/noend")) continue; # endif if ((argv[i][0] != '-') || (argv[i][1] == 'e') || (argv[i][1] == 'c') ) { interactive = FALSE; break; } } /* Need this before show_version is called for the first time */ if (interactive) show_version(stderr); else show_version(NULL); /* Only load GPVAL_COMPILE_OPTIONS */ update_gpval_variables(3); /* update GPVAL_ variables available to user */ #ifdef VMS /* initialise screen management routines for command recall */ { unsigned int ierror; if (ierror = smg$create_virtual_keyboard(&vms_vkid) != SS$_NORMAL) done(ierror); if (ierror = smg$create_key_table(&vms_ktid) != SS$_NORMAL) done(ierror); } #endif /* VMS */ if (!SETJMP(command_line_env, 1)) { /* first time */ interrupt_setup(); get_user_env(); init_loadpath(); init_locale(); memset(&sm_palette, 0, sizeof(sm_palette)); init_fit(); /* Initialization of fitting module */ #ifdef READLINE /* When using the built-in readline, we set the initial encoding according to the locale as this is required to properly handle keyboard input. */ init_encoding(); #endif init_gadgets(); /* April 2017: Now that error handling is in place, it is safe parse * GNUTERM during terminal initialization. * atexit processing is done in reverse order. We want * the generic terminal shutdown in term_reset to be executed before * any terminal specific cleanup requested by individual terminals. */ init_terminal(); push_terminal(0); /* remember the initial terminal */ gp_atexit(term_reset); /* Execute commands in ~/.gnuplot */ init_session(); if (interactive && term != 0) { /* not unknown */ #ifdef GNUPLOT_HISTORY #if (defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDITLINE)) && !defined(_WIN32) expanded_history_filename = tilde_expand(GNUPLOT_HISTORY_FILE); #else expanded_history_filename = gp_strdup(GNUPLOT_HISTORY_FILE); gp_expand_tilde(&expanded_history_filename); #endif read_history(expanded_history_filename); /* * It is safe to ignore the return values of 'atexit()' and * 'on_exit()'. In the worst case, there is no history of your * currrent session and you have to type all again in your next * session. */ gp_atexit(wrapper_for_write_history); #endif /* GNUPLOT_HISTORY */ #if defined(READLINE) && defined(WGP_CONSOLE) fprintf(stderr, "Encoding set to '%s'.\n", encoding_names[encoding]); #endif } /* if (interactive && term != 0) */ } else { /* come back here from int_error() */ if (!successful_initialization) { /* Only print the warning once */ successful_initialization = TRUE; fprintf(stderr,"WARNING: Error during initialization\n\n"); } if (interactive == FALSE) exit_status = EXIT_FAILURE; #ifdef HAVE_READLINE_RESET else { /* reset properly readline after a SIGINT+longjmp */ rl_reset_after_signal (); } #endif load_file_error(); /* if we were in load_file(), cleanup */ SET_CURSOR_ARROW; #ifdef VMS /* after catching interrupt */ /* VAX stuffs up stdout on SIGINT while writing to stdout, so reopen stdout. */ if (gpoutfile == stdout) { if ((stdout = freopen("SYS$OUTPUT", "w", stdout)) == NULL) { /* couldn't reopen it so try opening it instead */ if ((stdout = fopen("SYS$OUTPUT", "w")) == NULL) { /* don't use int_error here - causes infinite loop! */ fputs("Error opening SYS$OUTPUT as stdout\n", stderr); } } gpoutfile = stdout; } #endif /* VMS */ /* Why a goto? Because we exited the loop below via int_error */ /* using LONGJMP. The compiler was not expecting this, and */ /* "optimized" the handling of argc and argv such that simply */ /* entering the loop again from the top finds them messed up. */ /* If we reenter the loop via a goto then there is some hope */ /* that code reordering does not hurt us. */ if (reading_from_dash && interactive) goto RECOVER_FROM_ERROR_IN_DASH; reading_from_dash = FALSE; if (!interactive && !noinputfiles) { term_reset(); gp_exit(EXIT_FAILURE); /* exit on non-interactive error */ } } /* load filenames given as arguments */ while (--argc > 0) { ++argv; c_token = 0; if (!strncmp(*argv, "-persist", 2) || !strcmp(*argv, "--persist") #ifdef _WIN32 || !stricmp(*argv, "-noend") || !stricmp(*argv, "/noend") #endif ) { FPRINTF((stderr,"'persist' command line option recognized\n")); } else if (strcmp(*argv, "-") == 0) { #if defined(_WIN32) && !defined(WGP_CONSOLE) TextShow(&textwin); interactive = TRUE; #else interactive = isatty(fileno(stdin)); #endif RECOVER_FROM_ERROR_IN_DASH: reading_from_dash = TRUE; while (!com_line()); reading_from_dash = FALSE; interactive = FALSE; noinputfiles = FALSE; } else if (strcmp(*argv, "-e") == 0) { int save_state = interactive; --argc; ++argv; if (argc <= 0) { fprintf(stderr, "syntax: gnuplot -e \"commands\"\n"); return 0; } interactive = FALSE; noinputfiles = FALSE; do_string(*argv); interactive = save_state; } else if (!strncmp(*argv, "-slow", 2) || !strcmp(*argv, "--slow")) { slow_font_startup = TRUE; } else if (!strncmp(*argv, "-d", 2) || !strcmp(*argv, "--default-settings")) { /* Ignore this; it already had its effect */ FPRINTF((stderr, "ignoring -d\n")); } else if (strcmp(*argv, "-c") == 0) { /* Pass command line arguments to the gnuplot script in the next * argument. This consumes the remainder of the command line */ interactive = FALSE; noinputfiles = FALSE; --argc; ++argv; if (argc <= 0) { fprintf(stderr, "syntax: gnuplot -c scriptname args\n"); gp_exit(EXIT_FAILURE); } call_argc = GPMIN(9, argc - 1); for (i=0; i<=call_argc; i++) { /* Need to stash argv[i] somewhere visible to load_file() */ call_args[i] = gp_strdup(argv[i+1]); } load_file(loadpath_fopen(*argv, "r"), gp_strdup(*argv), 5); gp_exit(EXIT_SUCCESS); } else if (*argv[0] == '-') { fprintf(stderr, "unrecognized option %s\n", *argv); } else { interactive = FALSE; noinputfiles = FALSE; load_file(loadpath_fopen(*argv, "r"), gp_strdup(*argv), 4); } } /* take commands from stdin */ if (noinputfiles) { while (!com_line()) ctrlc_flag = FALSE; /* reset asynchronous Ctrl-C flag */ } #ifdef _WIN32 /* On Windows, handle 'persist' by keeping the main input loop running (windows/wxt), */ /* but only if there are any windows open. Note that qt handles this properly. */ if (persist_cl) { if (WinAnyWindowOpen()) { #ifdef WGP_CONSOLE if (!interactive) { /* no further input from pipe */ while (WinAnyWindowOpen()) win_sleep(100); } else #endif { interactive = TRUE; while (!com_line()) ctrlc_flag = FALSE; /* reset asynchronous Ctrl-C flag */ interactive = FALSE; } } } #endif #if (defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDITLINE)) && defined(GNUPLOT_HISTORY) #if !defined(HAVE_ATEXIT) && !defined(HAVE_ON_EXIT) /* You should be here if you neither have 'atexit()' nor 'on_exit()' */ wrapper_for_write_history(); #endif /* !HAVE_ATEXIT && !HAVE_ON_EXIT */ #endif /* (HAVE_LIBREADLINE || HAVE_LIBEDITLINE) && GNUPLOT_HISTORY */ #ifdef OS2 RexxDeregisterSubcom("GNUPLOT", NULL); #endif /* HBB 20040223: Not all compilers like exit() to end main() */ /* exit(exit_status); */ #if ! defined(_WIN32) /* Windows does the cleanup later */ gp_exit_cleanup(); #endif return exit_status; }