/* Process command line arguments */ int ReadArgs(int argc, char **argv, int *lens_1side_2side, fastf_t *ref_ind, fastf_t *diameter, fastf_t *thickness, fastf_t *focal_length) { int c = 0; char *options="T:r:d:t:f:"; int ltype; float refractive, diam, thick, focal; /* don't report errors */ bu_opterr = 0; while ((c=bu_getopt(argc, argv, options)) != -1) { switch (c) { case 'T' : sscanf(bu_optarg, "%d", <ype); *lens_1side_2side = ltype; break; case 'r': sscanf(bu_optarg, "%f", &refractive); *ref_ind = refractive; break; case 'd': sscanf(bu_optarg, "%f", &diam); *diameter = diam; break; case 't': sscanf(bu_optarg, "%f", &thick); *thickness = thick; break; case 'f': sscanf(bu_optarg, "%f", &focal); *focal_length = focal; break; default: bu_log("%s: illegal option -- %c\n", bu_getprogname(), c); bu_exit(EXIT_SUCCESS, NULL); } } return bu_optind; }
int bu_crashreport(const char *filename) { if (UNLIKELY(!filename || strlen(filename) == 0)) { return 0; } /* vat time ist? */ (void)time(&now); path = bu_argv0_full_path(); /* do our own expansion to avoid heap allocation */ snprintf(buffer, CR_BUFSIZE, "******************************************\n\n" "%s\n" /* version info */ "Command: %s\n" /* program name */ "Process: %d\n" /* pid */ "Path: %s\n" /* which binary */ "Date: %s\n", /* date/time */ brlcad_ident("Crash Report"), bu_getprogname(), bu_process_id(), path ? path : "Unknown", ctime(&now)); fp = fopen(filename, "ab"); if (UNLIKELY(!fp || ferror(fp))) { perror("unable to open crash report file"); bu_log("ERROR: Unable to open crash report file [%s]\n", filename); return 0; } /* make the file stream unbuffered */ if (setvbuf(fp, (char *)NULL, _IONBF, 0) != 0) { perror("unable to make stream unbuffered"); } /* print the report header */ if (fwrite(buffer, 1, strlen(buffer), fp) != strlen(buffer)) { /* cannot bomb */ bu_log("ERROR: Unable to write to crash report file [%s]\n", filename); (void)fclose(fp); fp = NULL; return 0; } /* write out the backtrace */ fprintf(fp, "Call stack backtrace (thread %d):\n", bu_parallel_id()); fflush(fp); if (bu_backtrace(fp) == 0) { bu_log("WARNING: Unable to obtain a call stack backtrace\n"); } /* write out operating system information */ path = bu_which("uname"); if (path) { snprintf(buffer, CR_BUFSIZE, "%s -a 2>&1", path); #if defined(HAVE_POPEN) && !defined(STRICT_FLAGS) popenfp = popen(buffer, "r"); if (!popenfp) { perror("unable to popen uname"); bu_log("WARNING: Unable to obtain uname information\n"); } #endif if (popenfp) { fprintf(fp, "\nSystem characteristics:\n"); fflush(fp); while (bu_fgets(buffer, CR_BUFSIZE, popenfp)) { size_t ret; size_t len; len = strlen(buffer); ret = fwrite(buffer, 1, len, fp); if (ret != len) perror("fwrite failed"); } } #if defined(HAVE_POPEN) && !defined(STRICT_FLAGS) (void)pclose(popenfp); #endif popenfp = NULL; path = NULL; } /* write out kernel and hardware information */ path = bu_which("sysctl"); if (path) { /* need 2>&1 to capture stderr junk from sysctl on Mac OS X for kern.exec */ snprintf(buffer, CR_BUFSIZE, "%s -a 2>&1", path); #if defined(HAVE_POPEN) && !defined(STRICT_FLAGS) popenfp = popen(buffer, "r"); if (popenfp == (FILE *)NULL) { perror("unable to popen sysctl"); bu_log("WARNING: Unable to obtain sysctl information\n"); } #endif if (popenfp != (FILE *)NULL) { fprintf(fp, "\nSystem information:\n"); fflush(fp); while (bu_fgets(buffer, CR_BUFSIZE, popenfp)) { size_t ret; size_t len; len = strlen(buffer); if ((len == 0) || ((len == 1) && (buffer[0] == '\n'))) { continue; } ret = fwrite(buffer, 1, len, fp); if (ret != len) perror("fwrite failed"); } } #if defined(HAVE_POPEN) && !defined(STRICT_FLAGS) (void)pclose(popenfp); #endif popenfp = NULL; path = NULL; } memset(buffer, 0, CR_BUFSIZE); fprintf(fp, "\n"); fflush(fp); (void)fclose(fp); fp = NULL; /* success */ return 1; }
int get_args(int argc, char **argv, FILE **ifp, FILE **ofp, double *angle) { int c; char *in_file_name = NULL; char *out_file_name = NULL; if (!ifp || !ofp || !angle) bu_exit(1, "bwrot: internal error processing arguments\n"); if (isatty(fileno(stdin)) && isatty(fileno(stdout)) && argc == 1) return 0; while ((c = bu_getopt(argc, argv, "fbri#:a:s:o:w:n:S:W:N:h?")) != -1) { switch (c) { case 'f': minus90++; break; case 'b': plus90++; break; case 'r': reverse++; break; case 'i': invert++; break; case '#': pixbytes = atoi(bu_optarg); break; case 'S': case 's': /* square size */ nxin = nyin = atoi(bu_optarg); break; case 'W': case 'w': nxin = atoi(bu_optarg); break; case 'N': case 'n': nyin = atoi(bu_optarg); break; case 'a': *angle = atof(bu_optarg); break; case 'o': out_file_name = bu_optarg; *ofp = fopen(out_file_name, "wb+"); if (*ofp == NULL) { bu_log("ERROR: %s cannot open \"%s\" for writing\n", bu_getprogname(), out_file_name); return 0; } break; default: /* '?' */ /* Disabled next line; illegal-option message is already provided, and "c" variable would only show '?'. */ /* bu_log("ERROR: %s encountered unrecognized '-%c' option\n", bu_getprogname(), c); */ return 0; } } /* XXX - backward compatibility hack */ if (bu_optind+2 == argc) { nxin = atoi(argv[bu_optind++]); nyin = atoi(argv[bu_optind++]); } if (bu_optind >= argc) { in_file_name = hyphen; } else { in_file_name = argv[bu_optind]; } if (BU_STR_EQUAL(in_file_name, "-")) { *ifp = stdin; } else { *ifp = fopen(in_file_name, "rb"); if (*ifp == NULL) { bu_log("ERROR: %s cannot open \"%s\" for reading\n", bu_getprogname(), in_file_name); return 0; } } /* sanity */ if (isatty(fileno(*ifp))) { bu_log("ERROR: %s will not read bw data from a tty\nRedirect input or specify an input file.\n", bu_getprogname()); return 0; } if (isatty(fileno(*ofp))) { bu_log("ERROR: %s will not write bw data to a tty\nRedirect output or use the -o output option.\n", bu_getprogname()); return 0; } if (argc > ++bu_optind) { bu_log("bwrot: excess argument(s) ignored\n"); } return 1; /* OK */ }
void usage() { bu_exit(1, "Usage: %s [options] db.g object\nOptions:\n -p #\t\tPopulation size\n -g #\t\tNumber of generations\n -r #\t\tResolution \n -u #\t\tUpper percent of individuals to keep\n -l #\t\tLower percent of individuals to kill\n", bu_getprogname()); }
int main(int argc, char **argv) { const size_t MAXBUFBYTES = 1280*1024; char usage[] = "Usage: bwrot [-rifb | -a angle] [-s squaresize] [-w width] [-n height] [-o output.bw] input.bw [> output.bw]\n"; ssize_t x, y; size_t j; int ret = 0; off_t outbyte, outplace; FILE *ifp, *ofp; unsigned char *obuf; unsigned char *buffer; double angle = 0.0; ssize_t io; ifp = stdin; ofp = stdout; bu_setprogname(argv[0]); if (!get_args(argc, argv, &ifp, &ofp, &angle)) { bu_exit(1, "%s", usage); } scanbytes = nxin * pixbytes; buflines = MAXBUFBYTES / scanbytes; if (buflines <= 0) { bu_exit(1, "%s", "bwrot: I'm not compiled to do a scanline that long!\n"); } if (buflines > nyin) buflines = nyin; buffer = (unsigned char *)bu_malloc(buflines * scanbytes, "buffer"); obuf = (unsigned char *)bu_malloc((nyin > nxin) ? nyin*pixbytes : nxin*pixbytes, "obuf"); /* * Break out to added arbitrary angle routine */ if (angle > 0.0) { arbrot(angle, ifp, buffer); goto done; } /* * Clear our "file pointer." We need to maintain this * In order to tell if seeking is required. ftell() always * fails on pipes, so we can't use it. */ outplace = 0; yin = 0; while (yin < nyin) { /* Fill buffer */ fill_buffer(ifp, buffer); if (!buflines) break; if (reverse) reverse_buffer(buffer); if (plus90) { for (x = 0; x < nxin; x++) { obp = obuf; bp = &buffer[ (lasty-firsty)*scanbytes + x*pixbytes ]; for (y = lasty+1; y > yin; y--) { /* firsty? */ for (j = 0; j < pixbytes; j++) *obp++ = *bp++; bp -= scanbytes + pixbytes; } yout = x; xout = (nyin - 1) - lasty; outbyte = ((yout * nyin) + xout) * pixbytes; if (outplace != outbyte) { if (bu_fseek(ofp, outbyte, SEEK_SET) < 0) { ret = 3; perror("fseek"); bu_log("ERROR: %s can't seek on output (ofp=%p, outbyte=%zd)\n", bu_getprogname(), (void *)ofp, outbyte); goto done; } outplace = outbyte; } io = fwrite(obuf, pixbytes, buflines, ofp); if (io < buflines) perror("fwrite"); outplace += buflines*pixbytes; } } else if (minus90) { for (x = nxin; x > 0; x--) { obp = obuf; bp = &buffer[ (x-1)*pixbytes ]; for (y = firsty+1; (ssize_t)y < lasty; y++) { for (j = 0; j < pixbytes; j++) *obp++ = *bp++; bp += scanbytes - pixbytes; } yout = (nxin - 1) - x + 1; xout = yin; outbyte = ((yout * nyin) + xout) * pixbytes; if (outplace != outbyte) { if (bu_fseek(ofp, outbyte, SEEK_SET) < 0) { ret = 3; perror("fseek"); bu_log("ERROR: %s can't seek on output (ofp=%p, outbyte=%zd)\n", bu_getprogname(), (void *)ofp, outbyte); goto done; } outplace = outbyte; } io = fwrite(obuf, pixbytes, buflines, ofp); if (io < buflines) perror("fwrite"); outplace += buflines*pixbytes; } } else if (invert) { for (y = lasty+1; (ssize_t)y > firsty; y--) { yout = (nyin - 1) - y + 1; outbyte = yout * scanbytes; if (outplace != outbyte) { if (bu_fseek(ofp, outbyte, SEEK_SET) < 0) { ret = 3; perror("fseek"); bu_log("ERROR: %s can't seek on output (ofp=%p, outbyte=%zd)\n", bu_getprogname(), (void *)ofp, outbyte); goto done; } outplace = outbyte; } io = fwrite(&buffer[(y-firsty-1)*scanbytes], 1, scanbytes, ofp); if (io < scanbytes) perror("fwrite"); outplace += scanbytes; } } else { /* Reverse only */ for (y = 0; y < buflines; y++) { io = fwrite(&buffer[y*scanbytes], 1, scanbytes, ofp); if (io < scanbytes) perror("fwrite"); } } yin += buflines; } done: bu_free(buffer, "buffer"); bu_free(obuf, "obuf"); return ret; }