int main () { int i1, i2, i3; #if O_BINARY SET_BINARY(fileno(stdout)); #endif /* Range 0x0000..0x007f */ for (i1 = 0; i1 < 0x80; i1++) printf("0x%02X\t0x%04X\n", i1, i1); /* Range 0x0080..0x07ff */ for (i1 = 2; i1 < 32; i1++) for (i2 = 0; i2 < 64; i2++) printf("0x%02X%02X\t0x%04X\n", 0xc0+i1,0x80+i2, (i1<<6)+i2); /* Range 0x0800..0xffff */ for (i1 = 0; i1 < 16; i1++) for (i2 = (i1==0 ? 32 : 0); i2 < 64; i2++) for (i3 = 0; i3 < 64; i3++) printf("0x%02X%02X%02X\t0x%04X\n", 0xe0+i1,0x80+i2,0x80+i3, (i1<<12)+(i2<<6)+i3); if (ferror(stdout) || fclose(stdout)) exit(1); exit(0); }
int main () { /* Test the O_BINARY macro. */ { int fd = open ("t-bin-out2.tmp", O_CREAT | O_TRUNC | O_RDWR | O_BINARY, 0600); if (write (fd, "Hello\n", 6) < 0) exit (1); close (fd); } { struct stat statbuf; if (stat ("t-bin-out2.tmp", &statbuf) < 0) exit (1); ASSERT (statbuf.st_size == 6); } unlink ("t-bin-out2.tmp"); /* Test the SET_BINARY macro. */ SET_BINARY (1); fputs ("Hello\n", stdout); fclose (stdout); fclose (stderr); { struct stat statbuf; if (stat ("t-bin-out1.tmp", &statbuf) < 0) exit (1); ASSERT (statbuf.st_size == 6); } return 0; }
/* Test yesno. Without arguments, read one line. If first argument is zero, close stdin before attempting to read one line. Otherwise, read the number of lines specified by first argument. */ int main (int argc, char **argv) { int i = 1; program_name = argv[0]; /* yesno recommends that all clients use close_stdin in main. */ atexit (close_stdin); /* But on mingw, close_stdin leaves stdin's file descriptor at the expected position (i.e. where this program left off reading) only if its mode has been set to O_BINARY. If it has been set to O_TEXT, and the file descriptor is seekable, and stdin is buffered, the MSVCRT runtime ends up setting the file descriptor's position to the expected position _minus_ the number of LFs not preceded by CR that were read between the expected position and the last filled buffer end position. (I.e. the repositioning from the end-of-buffer to the expected position does not work if the input file contains end-of-line markers in Unix convention.) */ SET_BINARY (0); if (1 < argc) i = atoi (argv[1]); if (!i) { i = 1; close (0); } while (i--) puts (yesno () ? "Y" : "N"); return 0; }
int main (int argc, char *argv[]) { int c; int i; #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) setlocale (LC_MESSAGES, ""); #endif #if defined (HAVE_SETLOCALE) setlocale (LC_CTYPE, ""); #endif bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); if (argc != 1) { int ishelp = 0; int isvers = 0; FILE *stream; if (argc == 2 && argv[1][0] == '-') { const char *opt = &argv[1][1]; if (*opt == '-') ++opt; ishelp = *opt == 'h' || *opt == 'H'; isvers = *opt == 'v' || *opt == 'V'; } if (isvers) print_version ("bin2c"); stream = ishelp ? stdout : stderr; fprintf (stream, _("Usage: %s < input_file > output_file\n"), argv[0]); fprintf (stream, _("Prints bytes from stdin in hex format.\n")); exit (!ishelp); } SET_BINARY (fileno (stdin)); i = 0; while ((c = getc (stdin)) != EOF) { printf ("0x%02x,", c); if (++i == 16) { printf ("\n"); i = 0; } } if (i != 0) printf ("\n"); exit (0); }
static void put_buffer_raw(uint8_t *p, uint_fast32_t c) { uint_fast32_t i = 0; SET_BINARY(1); while (i<c) { ++i; putchar(*p++); } }
int main (int argc, char *argv[]) { int test = atoi (argv[1]); /* Close unused file descriptors. */ close (STDOUT_FILENO); /* STDIN_FILENO was created as binary in the parent process. But since an fd's mode is stored in the process, not in the kernel, on native Windows we need to set it as binary in the child process again. */ SET_BINARY (STDIN_FILENO); main_reader_loop (test, PIPE_DATA_BLOCK_SIZE, STDIN_FILENO); return 0; }
/* With no arguments, do nothing. With arguments, attempt to consume first 6 bytes of stdin. In either case, let exit() take care of closing std streams and changing exit status if ferror(stdin). */ int main (int argc, char **argv) { char buf[7]; atexit (close_stdin); /* close_stdin currently relies on ftell, but mingw ftell is unreliable on text mode input. */ SET_BINARY (0); if (argc > 2) close (0); if (argc > 1) ignore_value (fread (buf, 1, 6, stdin)); return 0; }
lbp_printer::lbp_printer(int ps, double pw, double pl) : fill_pattern(1), fill_mode(0), cur_hpos(-1), cur_font(0), cur_size(0), cur_symbol_set(0), req_linethickness(-1) { SET_BINARY(fileno(stdout)); lbpinit(stdout); lbpputs("\033c\033;\033[2&z\033[7 I\033[?32h\033[?33h\033[11h"); wp54charset(); // Define the new symbol set lbpputs("\033[7 I\033[?32h\033[?33h\033[11h"); // Paper size handling if (orientation < 0) orientation = 0; // Default orientation is portrait papersize = 14; // Default paper size is A4 if (font::papersize) { papersize = set_papersize(font::papersize); paperlength = font::paperlength; paperwidth = font::paperwidth; } if (ps >= 0) { papersize = ps; paperlength = int(pl * font::res + 0.5); paperwidth = int(pw * font::res + 0.5); } if (papersize < 80) // standard paper lbpprintf("\033[%dp", (papersize | orientation)); else // Custom paper lbpprintf("\033[%d;%d;%dp", (papersize | orientation), paperlength, paperwidth); // Number of copies lbpprintf("\033[%dv\n", ncopies); lbpputs("\033[0u\033[1u\033P1y Grolbp\033\\"); lbpmoveabs(0, 0); lbpputs("\033[0t\033[2t"); lbpputs("\033('$2\033)' 1"); // Primary symbol set IBML // Secondary symbol set IBMR1 cur_symbol_set = 0; }
int main (int argc, char *argv[]) { int c; int i; if (argc != 1) { int ishelp = 0; FILE *stream; if (argc == 2 && argv[1][0] == '-') { const char *opt = &argv[1][1]; if (*opt == '-') ++opt; ishelp = *opt == 'h' || *opt == 'H'; } stream = ishelp ? stdout : stderr; fprintf (stream, "Usage: %s < input_file > output_file\n", argv[0]); fprintf (stream, "Prints bytes from stdin in hex format.\n"); exit (!ishelp); } SET_BINARY (fileno (stdin)); i = 0; while ((c = getc (stdin)) != EOF) { printf ("0x%02x,", c); if (++i == 16) { printf ("\n"); i = 0; } } if (i != 0) printf ("\n"); exit (0); }
int main (int argc, char *argv[]) { /* Test the O_BINARY macro. */ { int fd = open ("t-bin-out0.tmp", O_CREAT | O_TRUNC | O_RDWR | O_BINARY, 0600); if (write (fd, "Hello\n", 6) < 0) exit (1); close (fd); } { struct stat statbuf; if (stat ("t-bin-out0.tmp", &statbuf) < 0) exit (1); ASSERT (statbuf.st_size == 6); } switch (argv[1][0]) { case '1': /* Test the set_binary_mode() function. */ set_binary_mode (1, O_BINARY); fputs ("Hello\n", stdout); break; case '2': /* Test the SET_BINARY macro. */ SET_BINARY (1); fputs ("Hello\n", stdout); break; default: break; } return 0; }
int main () { int i1, i2, i3, i4, uc; #if O_BINARY SET_BINARY(fileno(stdout)); #endif uc = 0x10000; for (i1 = 0x90; i1 <= 0xe3; i1++) for (i2 = 0x30; i2 <= 0x39; i2++) for (i3 = 0x81; i3 <= 0xfe; i3++) for (i4 = 0x30; i4 <= 0x39; i4++) { printf("0x%02X%02X%02X%02X\t0x%X\n", i1, i2, i3, i4, uc); uc++; if (uc == 0x110000) goto done; } done: if (ferror(stdout) || fclose(stdout)) exit(1); exit(0); }
int main(int argc, char **argv) { setlocale(LC_NUMERIC, "C"); program_name = argv[0]; string env; static char stderr_buf[BUFSIZ]; setbuf(stderr, stderr_buf); int c; static const struct option long_options[] = { { "help", no_argument, 0, CHAR_MAX + 1 }, { "version", no_argument, 0, 'v' }, { NULL, 0, 0, 0 } }; while ((c = getopt_long(argc, argv, "b:c:F:gI:lmp:P:vw:", long_options, NULL)) != EOF) switch(c) { case 'b': // XXX check this broken_flags = atoi(optarg); bflag = 1; break; case 'c': if (sscanf(optarg, "%d", &ncopies) != 1 || ncopies <= 0) { error("bad number of copies `%s'", optarg); ncopies = 1; } break; case 'F': font::command_line_font_dir(optarg); break; case 'g': guess_flag = 1; break; case 'I': include_search_path.command_line_dir(optarg); break; case 'l': landscape_flag = 1; break; case 'm': manual_feed_flag = 1; break; case 'p': if (!font::scan_papersize(optarg, 0, &user_paper_length, &user_paper_width)) error("invalid custom paper size `%1' ignored", optarg); break; case 'P': env = "GROPS_PROLOGUE"; env += '='; env += optarg; env += '\0'; if (putenv(strsave(env.contents()))) fatal("putenv failed"); break; case 'v': printf("GNU grops (groff) version %s\n", Version_string); exit(0); break; case 'w': if (sscanf(optarg, "%d", &linewidth) != 1 || linewidth < 0) { error("bad linewidth `%1'", optarg); linewidth = -1; } break; case CHAR_MAX + 1: // --help usage(stdout); exit(0); break; case '?': usage(stderr); exit(1); break; default: assert(0); } font::set_unknown_desc_command_handler(handle_unknown_desc_command); SET_BINARY(fileno(stdout)); if (optind >= argc) do_file("-"); else { for (int i = optind; i < argc; i++) do_file(argv[i]); } return 0; }
int main (int argc, char **argv) { int c; /* Avoid the well-known bugs of fflush() on streams in O_TEXT mode on native Windows platforms. */ SET_BINARY (0); if (argc > 1) switch (argv[1][0]) { case '1': /* Check fflush after a backup ungetc() call. This is case 1a in terms of <http://lists.gnu.org/archive/html/bug-gnulib/2008-03/msg00131.html>, according to the Austin Group's resolution on 2009-01-08. */ c = fgetc (stdin); ASSERT (c == '#'); c = fgetc (stdin); ASSERT (c == '!'); /* Here the file-position indicator must be 2. */ c = ungetc ('!', stdin); ASSERT (c == '!'); fflush (stdin); /* Here the file-position indicator must be 1. */ c = fgetc (stdin); ASSERT (c == '!'); c = fgetc (stdin); ASSERT (c == '/'); return 0; case '2': /* Check fflush after a non-backup ungetc() call. This is case 2a in terms of <http://lists.gnu.org/archive/html/bug-gnulib/2008-03/msg00131.html>, according to the Austin Group's resolution on 2009-01-08. */ /* Check that fflush after a non-backup ungetc() call discards the ungetc buffer. This is mandated by POSIX <http://www.opengroup.org/susv3/functions/ungetc.html>: "The value of the file-position indicator for the stream after reading or discarding all pushed-back bytes shall be the same as it was before the bytes were pushed back." <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> "[After fflush(),] the file offset of the underlying open file description shall be set to the file position of the stream, and any characters pushed back onto the stream by ungetc() or ungetwc() that have not subsequently been read from the stream shall be discarded." */ c = fgetc (stdin); ASSERT (c == '#'); c = fgetc (stdin); ASSERT (c == '!'); /* Here the file-position indicator must be 2. */ c = ungetc ('@', stdin); ASSERT (c == '@'); fflush (stdin); /* Here the file-position indicator must be 1. */ c = fgetc (stdin); ASSERT (c == '!'); c = fgetc (stdin); ASSERT (c == '/'); return 0; } return 1; }
int main(int argc, char **argv) { int opt; static const struct option long_options[] = { { "help", no_argument, 0, CHAR_MAX + 1 }, { "version", no_argument, 0, 'v' }, { NULL, 0, 0, 0 } }; program_name = argv[0]; while ((opt = getopt_long(argc, argv, "v", long_options, NULL)) != EOF) { switch (opt) { case 'v': printf("GNU pfbtops (groff) version %s\n", Version_string); exit(0); break; case CHAR_MAX + 1: /* --help */ usage(stdout); exit(0); break; case '?': usage(stderr); exit(1); break; } } if (argc - optind > 1) { usage(stderr); exit(1); } if (argc > optind && !freopen(argv[optind], "r", stdin)) { perror(argv[optind]); exit(1); } SET_BINARY(fileno(stdin)); for (;;) { int type, c, i; long n; c = getchar(); if (c != 0x80) error("first byte of packet not 0x80"); type = getchar(); if (type == 3) break; if (type != 1 && type != 2) error("bad packet type"); n = 0; for (i = 0; i < 4; i++) { c = getchar(); if (c == EOF) error("end of file in packet header"); n |= (long)c << (i << 3); } if (n < 0) error("negative packet length"); if (type == 1) get_text(n); else get_binary(n); } exit(0); }
int main (int argc, char* argv[]) { const char* charset; iconv_t cd; int bmp_only; if (argc != 2) { fprintf(stderr,"Usage: table-to charset\n"); exit(1); } charset = argv[1]; #if O_BINARY SET_BINARY(fileno(stdout)); #endif cd = iconv_open(charset,"UCS-4-INTERNAL"); if (cd == (iconv_t)(-1)) { perror("iconv_open"); exit(1); } /* When testing UTF-8, stop at 0x10000, otherwise the output file gets too big. */ bmp_only = (strcmp(charset,"UTF-8") == 0); { unsigned int i; unsigned char buf[10]; for (i = 0; i < (bmp_only ? 0x10000 : 0x110000); i++) { unsigned int in = i; const char* inbuf = (const char*) ∈ size_t inbytesleft = sizeof(unsigned int); char* outbuf = (char*)buf; size_t outbytesleft = sizeof(buf); size_t result; size_t result2 = 0; iconv(cd,NULL,NULL,NULL,NULL); result = iconv(cd,(ICONV_CONST char**)&inbuf,&inbytesleft,&outbuf,&outbytesleft); if (result != (size_t)(-1)) result2 = iconv(cd,NULL,NULL,&outbuf,&outbytesleft); if (result == (size_t)(-1) || result2 == (size_t)(-1)) { if (errno != EILSEQ) { int saved_errno = errno; fprintf(stderr,"0x%02X: iconv error: ",i); errno = saved_errno; perror(""); exit(1); } } else if (result == 0) { /* ignore conversions with transliteration */ if (inbytesleft == 0 && outbytesleft < sizeof(buf)) { unsigned int jmax = sizeof(buf) - outbytesleft; unsigned int j; printf("0x"); for (j = 0; j < jmax; j++) printf("%02X",buf[j]); printf("\t0x%04X\n",i); } else if (inbytesleft == 0 && i >= 0xe0000 && i < 0xe0080) { /* Language tags may silently be dropped. */ } else { fprintf(stderr,"0x%02X: inbytes = %ld, outbytes = %ld\n",i,(long)(sizeof(unsigned int)-inbytesleft),(long)(sizeof(buf)-outbytesleft)); exit(1); } } } } if (iconv_close(cd) < 0) { perror("iconv_close"); exit(1); } if (ferror(stdin) || ferror(stdout) || fclose(stdout)) { fprintf(stderr,"I/O error\n"); exit(1); } exit(0); }
/* Reads an existing .mo file and adds the messages to mlp. */ void read_mo_file (message_list_ty *mlp, const char *filename) { FILE *fp; struct binary_mo_file bf; struct mo_file_header header; unsigned int i; static lex_pos_ty pos = { __FILE__, __LINE__ }; if (strcmp (filename, "-") == 0 || strcmp (filename, "/dev/stdin") == 0) { fp = stdin; SET_BINARY (fileno (fp)); } else { fp = fopen (filename, "rb"); if (fp == NULL) error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"), filename); } /* Read the file contents into memory. */ read_binary_mo_file (&bf, fp, filename); /* Get a 32-bit number from the file header. */ # define GET_HEADER_FIELD(field) \ get_uint32 (&bf, offsetof (struct mo_file_header, field)) /* We must grope the file to determine which endian it is. Perversity of the universe tends towards maximum, so it will probably not match the currently executing architecture. */ bf.endian = MO_BIG_ENDIAN; header.magic = GET_HEADER_FIELD (magic); if (header.magic != _MAGIC) { bf.endian = MO_LITTLE_ENDIAN; header.magic = GET_HEADER_FIELD (magic); if (header.magic != _MAGIC) { unrecognised: error (EXIT_FAILURE, 0, _("file \"%s\" is not in GNU .mo format"), filename); } } header.revision = GET_HEADER_FIELD (revision); /* We support only the major revisions 0 and 1. */ switch (header.revision >> 16) { case 0: case 1: /* Fill the header parts that apply to major revisions 0 and 1. */ header.nstrings = GET_HEADER_FIELD (nstrings); header.orig_tab_offset = GET_HEADER_FIELD (orig_tab_offset); header.trans_tab_offset = GET_HEADER_FIELD (trans_tab_offset); header.hash_tab_size = GET_HEADER_FIELD (hash_tab_size); header.hash_tab_offset = GET_HEADER_FIELD (hash_tab_offset); for (i = 0; i < header.nstrings; i++) { message_ty *mp; char *msgid; size_t msgid_len; char *msgstr; size_t msgstr_len; /* Read the msgid. */ msgid = get_string (&bf, header.orig_tab_offset + i * 8, &msgid_len); /* Read the msgstr. */ msgstr = get_string (&bf, header.trans_tab_offset + i * 8, &msgstr_len); mp = message_alloc (msgid, (strlen (msgid) + 1 < msgid_len ? msgid + strlen (msgid) + 1 : NULL), msgstr, msgstr_len, &pos); message_list_append (mlp, mp); } switch (header.revision & 0xffff) { case 0: break; case 1: default: /* Fill the header parts that apply to minor revision >= 1. */ header.n_sysdep_segments = GET_HEADER_FIELD (n_sysdep_segments); header.sysdep_segments_offset = GET_HEADER_FIELD (sysdep_segments_offset); header.n_sysdep_strings = GET_HEADER_FIELD (n_sysdep_strings); header.orig_sysdep_tab_offset = GET_HEADER_FIELD (orig_sysdep_tab_offset); header.trans_sysdep_tab_offset = GET_HEADER_FIELD (trans_sysdep_tab_offset); for (i = 0; i < header.n_sysdep_strings; i++) { message_ty *mp; char *msgid; size_t msgid_len; char *msgstr; size_t msgstr_len; nls_uint32 offset; /* Read the msgid. */ offset = get_uint32 (&bf, header.orig_sysdep_tab_offset + i * 4); msgid = get_sysdep_string (&bf, offset, &header, &msgid_len); /* Read the msgstr. */ offset = get_uint32 (&bf, header.trans_sysdep_tab_offset + i * 4); msgstr = get_sysdep_string (&bf, offset, &header, &msgstr_len); mp = message_alloc (msgid, (strlen (msgid) + 1 < msgid_len ? msgid + strlen (msgid) + 1 : NULL), msgstr, msgstr_len, &pos); message_list_append (mlp, mp); } break; } break; default: goto unrecognised; } if (fp != stdin) fclose (fp); }
/* * Scan an initial sequence of comment lines looking for font and memory * usage specifications. This does not handle the "atend" construction. */ void scanfontcomments(const char *filename) { char p[500]; char *r; FILE *f; integer truecost = pagecost; Boolean trueknown = 0; fontdesctype *oldcf = curfnt; #ifdef DEBUG if (dd(D_FONTS)) fprintf(stderr, "Checking for fonts in '%s'\n",filename); #endif /* DEBUG */ if (*filename == '`') { /* * Allow scanning of ` commands. Better return same results both times. */ f = popen(filename+1, "r"); SET_BINARY(fileno(f)); to_close = USE_PCLOSE; } else { f = search(figpath, filename, READ); } if (f) { SET_BINARY(fileno(f)); fc_state = 0; check_atend = 0; while (fgets(p,500,f) && p[0]=='%' && (p[1]=='!' || p[1]=='%' || p[1]=='*')) { if (strncmp(p, "%*Font:", 7) == 0) { scan1fontcomment(p+7); } else if (strncmp(p, "%%VMusage:", 9) == 0) { truecost += scanvm(p+10); trueknown = 1; } scanfontusage(p,filename); } if (trueknown) pagecost = truecost; if(check_atend) { #ifdef DEBUG if (dd(D_FONTS)) fprintf(stderr, "Checking for (atend) fonts in '%s'\n",filename); #endif /* DEBUG */ fc_state = 0; fseek(f,-4096,2); /* seek to 4096 bytes before EOF. */ fgets(p,500,f); /* throw away a partial line. */ /* find %%Trailer */ while((r=fgets(p,500,f)) && strncmp(p,"%%Trailer",9)); /* look for specs that were deferred to the trailer. */ if(r != NULL) { while(fgets(p,500,f)) { if(p[0]=='%' && p[1]=='%') scanfontusage(p,filename); } } #ifdef DEBUG else { /* r == NULL */ if (dd(D_FONTS)) fprintf(stderr, "Did not find %%%%Trailer in included file '%s'.\n", filename); } #endif /* DEBUG */ } close_file(f); } curfnt = oldcf; }
int main (int argc, char **argv) { int ch; /* Assume stdin is seekable iff argc > 1. */ if (argc == 1) { ASSERT (ftell (stdin) == -1); return 0; } /* mingw ftell is unreliable on text mode input. */ SET_BINARY (0); /* Simple tests. */ ASSERT (ftell (stdin) == 0); ch = fgetc (stdin); ASSERT (ch == '#'); ASSERT (ftell (stdin) == 1); /* Test ftell after ungetc of read input. */ ch = ungetc ('#', stdin); ASSERT (ch == '#'); ASSERT (ftell (stdin) == 0); ch = fgetc (stdin); ASSERT (ch == '#'); ASSERT (ftell (stdin) == 1); /* Test ftell after fseek. */ ASSERT (fseek (stdin, 2, SEEK_SET) == 0); ASSERT (ftell (stdin) == 2); /* Test ftell after random ungetc. */ ch = fgetc (stdin); ASSERT (ch == '/'); ch = ungetc ('@', stdin); ASSERT (ch == '@'); ASSERT (ftell (stdin) == 2); ch = fgetc (stdin); ASSERT (ch == '@'); ASSERT (ftell (stdin) == 3); if (2 < argc) { if (FUNC_UNGETC_BROKEN) { fputs ("Skipping test: ungetc cannot handle arbitrary bytes\n", stderr); return 77; } /* Test ftell after ungetc without read. */ ASSERT (fseek (stdin, 0, SEEK_CUR) == 0); ASSERT (ftell (stdin) == 3); ch = ungetc ('~', stdin); ASSERT (ch == '~'); ASSERT (ftell (stdin) == 2); } #if !defined __MINT__ /* FreeMiNT has problems seeking past end of file */ /* Test ftell beyond end of file. */ ASSERT (fseek (stdin, 0, SEEK_END) == 0); ch = ftell (stdin); ASSERT (fseek (stdin, 10, SEEK_END) == 0); ASSERT (ftell (stdin) == ch + 10); #endif return 0; }
int main(int argc, char *argv[]) { int result; TIFF *tiff; u_long width, height; int bwidth; u_char *raster = 0; int r; int i; png_structp png_ptr; png_infop info_ptr; png_bytep row_pointer, row; int page = 0; int angle = 0; int nwidth = -1; int lr = 0; int tb = 0; int antialias = 0; int xisy = 0; int xisflipped = 0; int yisflipped = 0; uint16 bitsPerSample; char *tiffFile = 0; char *pngFile = 0; FILE *out; int y; u_long num; u_long denom; char error[1024]; for (i = 1; (i < argc); i++) { if (!strcmp(argv[i], "-p")) { if (argc == (i + 1)) { die("-p expects a page number"); } page = atoi(argv[i + 1]) - 1; i++; if (page < 0) { die("-p expects a page number >= 1"); } } else if (!strcmp(argv[i], "-r")) { if (argc == (i + 1)) { die("-r expects an angle"); } angle = atoi(argv[i + 1]); i++; if ((angle < 0) || (angle > 270) || (angle % 90)) { die("-a expects an angle of 0, 90, 180, or 270 degrees"); } } else if (!strcmp(argv[i], "-w")) { if (argc == (i + 1)) { die("-w expects a width in pixels"); } nwidth = atoi(argv[i + 1]); i++; if (nwidth <= 0) { die("-w expects a positive width in pixels"); } } else if (!strcmp(argv[i], "-lr")) { lr = 1; } else if (!strcmp(argv[i], "-tb")) { tb = 1; } else if (!strcmp(argv[i], "-a")) { antialias = 1; } else if ((!tiffFile) && (argv[i][0] != '-')) { tiffFile = argv[i]; } else if ((!pngFile) && (argv[i][0] != '-')) { pngFile = argv[i]; } else { usage("unknown parameter"); } } if (!tiffFile) { usage("tiff filename is required"); } tiff = TIFFOpen(tiffFile, "rb"); if (!tiff) { die("Can't open file"); } if (!TIFFSetDirectory(tiff, page)) { die("Can't access page number requested"); } (void) TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width); if (nwidth == -1) { num = 1; denom = 1; } else { if (!xisy) { num = width; denom = nwidth; } else { num = height; denom = nwidth; } } if ((!num) || (!denom)) { die("Width and height must both be nonzero"); } (void) TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height); (void) TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bitsPerSample); if (bitsPerSample != 1) { die("Sorry, only 1-bit (scan/fax) TIFFs are supported"); } bwidth = (int) TIFFScanlineSize(tiff); raster = calloc(1, bwidth * height); if (!raster) { die("Memory allocation error"); } for (y = 0; (y < height); y++) { TIFFReadScanline(tiff, raster + bwidth * y, y, 0); } switch (angle) { case 0: break; case 90: xisy = !xisy; yisflipped = !yisflipped; break; case 180: xisflipped = !xisflipped; yisflipped = !yisflipped; break; case 270: xisy = !xisy; xisflipped = !xisflipped; break; } if (lr) { xisflipped = !xisflipped; } if (tb) { yisflipped = !yisflipped; } row = calloc(sizeof(u_char), (xisy) ? height : width); if (!row) { die("Memory allocation error"); } png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0); if (!png_ptr) { die("Cannot allocate png_structp"); } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { die("Cannot allocate png_infop"); } if (setjmp(png_jmpbuf(png_ptr))) { die("Error on png write"); } if (pngFile) { out = fopen(pngFile, "wb"); if (!out) { die("Cannot create output file"); } } else { out = stdout; SET_BINARY(STDOUT_FILENO); } png_init_io(png_ptr, out); /* Turning off filtering yields a large speed improvement at a modest price in file size */ png_set_filter(png_ptr, 0, PNG_FILTER_NONE); if (nwidth == -1) { nwidth = width; } png_set_IHDR(png_ptr, info_ptr, ((xisy) ? height : width) * denom / num, ((xisy) ? width : height) * denom / num, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png_ptr, info_ptr); for (y = 0; (y < info_ptr->height); y++) { int x; u_char *p; row_pointer = row; p = row; for (x = 0; (x < info_ptr->width); x++) { int tx, ty; int accum = 0, total = 0; int ty1 = (xisy ? x : y) * num / denom; int tx1 = (xisy ? y : x) * num / denom; int tx2, ty2; int xsteps, ysteps; if (!antialias) { tx2 = tx1 + 1; ty2 = ty1 + 1; } else { ty2 = (xisy ? (x + 1) : (y + 1)) * num / denom; tx2 = (xisy ? (y + 1) : (x + 1)) * num / denom; } ysteps = abs(ty2 - ty1); xsteps = abs(tx2 - tx1); if (xisflipped) { tx1 = width - 1 - tx1; tx2 = width - 1 - tx2; } if (yisflipped) { ty1 = height - 1 - ty1; ty2 = height - 1 - ty2; } ty = ty1; while (ty != ty2) { tx = tx1; while (tx != tx2) { accum += GetBWPixel(raster, ty, tx); total++; tx += sign(tx2 - tx1); } ty += sign(ty2 - ty1); } if (total > 0) { *p = accum / total; } p++; } png_write_row(png_ptr, row_pointer); } png_write_end(png_ptr, info_ptr); png_destroy_write_struct(&png_ptr, &info_ptr); if (out != stdout) { fclose(out); } return 0; }
int main (int argc, char **argv) { int ch; /* Assume stdin is seekable iff argc > 1. */ if (argc == 1) { ASSERT (ftell (stdin) == -1); ASSERT (ftello (stdin) == -1); return 0; } /* mingw ftell is unreliable on text mode input. */ SET_BINARY (0); /* Simple tests. For each test, make sure ftell and ftello agree. */ ASSERT (ftell (stdin) == 0); ASSERT (ftello (stdin) == 0); ch = fgetc (stdin); ASSERT (ch == '#'); ASSERT (ftell (stdin) == 1); ASSERT (ftello (stdin) == 1); /* Test ftell after ungetc of read input. */ ch = ungetc ('#', stdin); ASSERT (ch == '#'); ASSERT (ftell (stdin) == 0); ASSERT (ftello (stdin) == 0); ch = fgetc (stdin); ASSERT (ch == '#'); ASSERT (ftell (stdin) == 1); ASSERT (ftello (stdin) == 1); /* Test ftell after fseek. */ ASSERT (fseek (stdin, 2, SEEK_SET) == 0); ASSERT (ftell (stdin) == 2); ASSERT (ftello (stdin) == 2); /* Test ftell after random ungetc. */ ch = fgetc (stdin); ASSERT (ch == '/'); ch = ungetc ('@', stdin); ASSERT (ch == '@'); ASSERT (ftell (stdin) == 2); ASSERT (ftello (stdin) == 2); ch = fgetc (stdin); ASSERT (ch == '@'); ASSERT (ftell (stdin) == 3); ASSERT (ftello (stdin) == 3); #if !defined __hpux /* HP-UX 11 has a known bug here */ /* Test ftell after ungetc without read. */ ASSERT (fseek (stdin, 0, SEEK_CUR) == 0); ASSERT (ftell (stdin) == 3); ASSERT (ftello (stdin) == 3); #endif ch = ungetc ('~', stdin); ASSERT (ch == '~'); ASSERT (ftell (stdin) == 2); ASSERT (ftello (stdin) == 2); /* Test ftell beyond end of file. */ ASSERT (fseek (stdin, 0, SEEK_END) == 0); ch = ftello (stdin); ASSERT (fseek (stdin, 10, SEEK_END) == 0); ASSERT (ftell (stdin) == ch + 10); ASSERT (ftello (stdin) == ch + 10); return 0; }
*/ static REBFLG Get_GOB_Var(REBGOB *gob, REBVAL *word, REBVAL *val) /* ***********************************************************************/ { switch (VAL_WORD_CANON(word)) { case SYM_OFFSET: SET_PAIR(val, GOB_X(gob), GOB_Y(gob)); break; case SYM_SIZE: SET_PAIR(val, GOB_W(gob), GOB_H(gob)); break; case SYM_IMAGE: if (GOB_TYPE(gob) == GOBT_IMAGE) { // image } else goto is_none; break; case SYM_DRAW: if (GOB_TYPE(gob) == GOBT_DRAW) { Set_Block(val, GOB_CONTENT(gob)); // Note: compiler optimizes SET_BLOCKs below } else goto is_none; break; case SYM_TEXT: if (GOB_TYPE(gob) == GOBT_TEXT) { Set_Block(val, GOB_CONTENT(gob)); } else if (GOB_TYPE(gob) == GOBT_STRING) { Set_String(val, GOB_CONTENT(gob)); } else goto is_none; break; case SYM_EFFECT: if (GOB_TYPE(gob) == GOBT_EFFECT) { Set_Block(val, GOB_CONTENT(gob)); } else goto is_none; break; case SYM_COLOR: if (GOB_TYPE(gob) == GOBT_COLOR) { Set_Tuple_Pixel((REBYTE*)&GOB_CONTENT(gob), val); } else goto is_none; break; case SYM_ALPHA: SET_INTEGER(val, GOB_ALPHA(gob)); break; case SYM_PANE: if (GOB_PANE(gob)) Set_Block(val, Pane_To_Block(gob, 0, -1)); else Set_Block(val, Make_Block(0)); break; case SYM_PARENT: if (GOB_PARENT(gob)) { SET_GOB(val, GOB_PARENT(gob)); } else is_none: SET_NONE(val); break; case SYM_DATA: if (GOB_DTYPE(gob) == GOBD_OBJECT) { SET_OBJECT(val, GOB_DATA(gob)); } else if (GOB_DTYPE(gob) == GOBD_BLOCK) { Set_Block(val, GOB_DATA(gob)); } else if (GOB_DTYPE(gob) == GOBD_STRING) { Set_String(val, GOB_DATA(gob)); } else if (GOB_DTYPE(gob) == GOBD_BINARY) { SET_BINARY(val, GOB_DATA(gob)); } else if (GOB_DTYPE(gob) == GOBD_INTEGER) { SET_INTEGER(val, (REBIPT)GOB_DATA(gob)); } else goto is_none; break; case SYM_FLAGS: Set_Block(val, Flags_To_Block(gob)); break; default: return FALSE; } return TRUE; }
/* Reads an existing .mo file and adds the messages to mlp. */ void read_mo_file (message_list_ty *mlp, const char *filename) { FILE *fp; struct binary_mo_file bf; struct mo_file_header header; unsigned int i; static lex_pos_ty pos = { __FILE__, __LINE__ }; if (strcmp (filename, "-") == 0 || strcmp (filename, "/dev/stdin") == 0) { fp = stdin; SET_BINARY (fileno (fp)); } else { fp = fopen (filename, "rb"); if (fp == NULL) error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"), filename); } /* Read the file contents into memory. */ read_binary_mo_file (&bf, fp, filename); /* Get a 32-bit number from the file header. */ # define GET_HEADER_FIELD(field) \ get_uint32 (&bf, offsetof (struct mo_file_header, field)) /* We must grope the file to determine which endian it is. Perversity of the universe tends towards maximum, so it will probably not match the currently executing architecture. */ bf.endian = MO_BIG_ENDIAN; header.magic = GET_HEADER_FIELD (magic); if (header.magic != _MAGIC) { bf.endian = MO_LITTLE_ENDIAN; header.magic = GET_HEADER_FIELD (magic); if (header.magic != _MAGIC) { unrecognised: error (EXIT_FAILURE, 0, _("file \"%s\" is not in GNU .mo format"), filename); } } header.revision = GET_HEADER_FIELD (revision); /* We support only the major revisions 0 and 1. */ switch (header.revision >> 16) { case 0: case 1: /* Fill the header parts that apply to major revisions 0 and 1. */ header.nstrings = GET_HEADER_FIELD (nstrings); header.orig_tab_offset = GET_HEADER_FIELD (orig_tab_offset); header.trans_tab_offset = GET_HEADER_FIELD (trans_tab_offset); header.hash_tab_size = GET_HEADER_FIELD (hash_tab_size); header.hash_tab_offset = GET_HEADER_FIELD (hash_tab_offset); for (i = 0; i < header.nstrings; i++) { message_ty *mp; char *msgctxt; char *msgid; size_t msgid_len; char *separator; char *msgstr; size_t msgstr_len; /* Read the msgctxt and msgid. */ msgid = get_string (&bf, header.orig_tab_offset + i * 8, &msgid_len); /* Split into msgctxt and msgid. */ separator = strchr (msgid, MSGCTXT_SEPARATOR); if (separator != NULL) { /* The part before the MSGCTXT_SEPARATOR is the msgctxt. */ *separator = '\0'; msgctxt = msgid; msgid = separator + 1; msgid_len -= msgid - msgctxt; } else msgctxt = NULL; /* Read the msgstr. */ msgstr = get_string (&bf, header.trans_tab_offset + i * 8, &msgstr_len); mp = message_alloc (msgctxt, msgid, (strlen (msgid) + 1 < msgid_len ? msgid + strlen (msgid) + 1 : NULL), msgstr, msgstr_len, &pos); message_list_append (mlp, mp); } switch (header.revision & 0xffff) { case 0: break; case 1: default: /* Fill the header parts that apply to minor revision >= 1. */ header.n_sysdep_segments = GET_HEADER_FIELD (n_sysdep_segments); header.sysdep_segments_offset = GET_HEADER_FIELD (sysdep_segments_offset); header.n_sysdep_strings = GET_HEADER_FIELD (n_sysdep_strings); header.orig_sysdep_tab_offset = GET_HEADER_FIELD (orig_sysdep_tab_offset); header.trans_sysdep_tab_offset = GET_HEADER_FIELD (trans_sysdep_tab_offset); for (i = 0; i < header.n_sysdep_strings; i++) { message_ty *mp; char *msgctxt; char *msgid; size_t msgid_len; char *separator; char *msgstr; size_t msgstr_len; nls_uint32 offset; size_t f; /* Read the msgctxt and msgid. */ offset = get_uint32 (&bf, header.orig_sysdep_tab_offset + i * 4); msgid = get_sysdep_string (&bf, offset, &header, &msgid_len); /* Split into msgctxt and msgid. */ separator = strchr (msgid, MSGCTXT_SEPARATOR); if (separator != NULL) { /* The part before the MSGCTXT_SEPARATOR is the msgctxt. */ *separator = '\0'; msgctxt = msgid; msgid = separator + 1; msgid_len -= msgid - msgctxt; } else msgctxt = NULL; /* Read the msgstr. */ offset = get_uint32 (&bf, header.trans_sysdep_tab_offset + i * 4); msgstr = get_sysdep_string (&bf, offset, &header, &msgstr_len); mp = message_alloc (msgctxt, msgid, (strlen (msgid) + 1 < msgid_len ? msgid + strlen (msgid) + 1 : NULL), msgstr, msgstr_len, &pos); /* Only messages with c-format or objc-format annotation are recognized as having system-dependent strings by msgfmt. Which one of the two, we don't know. We have to guess, assuming that c-format is more probable than objc-format and that the .mo was likely produced by "msgfmt -c". */ for (f = format_c; ; f = format_objc) { bool valid = true; struct formatstring_parser *parser = formatstring_parsers[f]; const char *str_end; const char *str; str_end = msgid + msgid_len; for (str = msgid; str < str_end; str += strlen (str) + 1) { char *invalid_reason = NULL; void *descr = parser->parse (str, false, NULL, &invalid_reason); if (descr != NULL) parser->free (descr); else { free (invalid_reason); valid = false; break; } } if (valid) { str_end = msgstr + msgstr_len; for (str = msgstr; str < str_end; str += strlen (str) + 1) { char *invalid_reason = NULL; void *descr = parser->parse (str, true, NULL, &invalid_reason); if (descr != NULL) parser->free (descr); else { free (invalid_reason); valid = false; break; } } } if (valid) { /* Found the most likely among c-format, objc-format. */ mp->is_format[f] = yes; break; } /* Try next f. */ if (f == format_objc) break; } message_list_append (mlp, mp); } break; } break; default: goto unrecognised; } if (fp != stdin) fclose (fp); }
int main(int argc, char **argv) { setlocale(LC_NUMERIC, "C"); program_name = argv[0]; static char stderr_buf[BUFSIZ]; setbuf(stderr, stderr_buf); int c; static const struct option long_options[] = { { "help", no_argument, 0, CHAR_MAX + 1 }, { "version", no_argument, 0, 'v' }, { NULL, 0, 0, 0 } }; while ((c = getopt_long(argc, argv, "c:d:F:I:lp:vw:", long_options, NULL)) != EOF) switch(c) { case 'l': landscape_flag = 1; break; case 'I': // ignore include search path break; case ':': if (optopt == 'd') { fprintf(stderr, "duplex assumed to be long-side\n"); duplex_flag = 1; } else fprintf(stderr, "option -%c requires an argument\n", optopt); fflush(stderr); break; case 'd': if (!isdigit(*optarg)) // this ugly hack prevents -d without optind--; // args from messing up the arg list duplex_flag = atoi(optarg); if (duplex_flag != 1 && duplex_flag != 2) { fprintf(stderr, "odd value for duplex; assumed to be long-side\n"); duplex_flag = 1; } break; case 'p': { int n = lookup_paper_size(optarg); if (n < 0) error("unknown paper size `%1'", optarg); else user_paper_size = n; break; } case 'v': printf("GNU grolj4 (groff) version %s\n", Version_string); exit(0); break; case 'F': font::command_line_font_dir(optarg); break; case 'c': { char *ptr; long n = strtol(optarg, &ptr, 10); if (n == 0 && ptr == optarg) error("argument for -c must be a positive integer"); else if (n <= 0 || n > 32767) error("out of range argument for -c"); else ncopies = unsigned(n); break; } case 'w': { char *ptr; long n = strtol(optarg, &ptr, 10); if (n == 0 && ptr == optarg) error("argument for -w must be a non-negative integer"); else if (n < 0 || n > INT_MAX) error("out of range argument for -w"); else line_width_factor = int(n); break; } case CHAR_MAX + 1: // --help usage(stdout); exit(0); break; case '?': usage(stderr); exit(1); break; default: assert(0); } SET_BINARY(fileno(stdout)); if (optind >= argc) do_file("-"); else { for (int i = optind; i < argc; i++) do_file(argv[i]); } return 0; }
*/ static REBFLG Get_GOB_Var(REBGOB *gob, REBVAL *word, REBVAL *val) /* ***********************************************************************/ { REBSER *data; switch (VAL_WORD_CANON(word)) { case SYM_OFFSET: SET_PAIR(val, GOB_X(gob), GOB_Y(gob)); break; case SYM_SIZE: SET_PAIR(val, GOB_W(gob), GOB_H(gob)); break; case SYM_IMAGE: if (GOB_TYPE(gob) == GOBT_IMAGE) { // image } else goto is_none; break; #ifdef HAS_WIDGET_GOB case SYM_WIDGET: data = VAL_SERIES(GOB_WIDGET_SPEC(gob)); Init_Word(val, VAL_WORD_CANON(BLK_HEAD(data))); VAL_SET(val, REB_LIT_WORD); break; #endif case SYM_DRAW: if (GOB_TYPE(gob) == GOBT_DRAW) { Set_Block(val, GOB_CONTENT(gob)); // Note: compiler optimizes SET_BLOCKs below } else goto is_none; break; case SYM_TEXT: if (GOB_TYPE(gob) == GOBT_TEXT) { Set_Block(val, GOB_CONTENT(gob)); } else if (GOB_TYPE(gob) == GOBT_STRING) { Set_String(val, GOB_CONTENT(gob)); } else goto is_none; break; case SYM_EFFECT: if (GOB_TYPE(gob) == GOBT_EFFECT) { Set_Block(val, GOB_CONTENT(gob)); } else goto is_none; break; case SYM_COLOR: if (GOB_TYPE(gob) == GOBT_COLOR) { Set_Tuple_Pixel((REBYTE*)&GOB_CONTENT(gob), val); } else goto is_none; break; case SYM_ALPHA: SET_INTEGER(val, GOB_ALPHA(gob)); break; case SYM_PANE: if (GOB_PANE(gob)) Set_Block(val, Pane_To_Block(gob, 0, -1)); else Set_Block(val, Make_Block(0)); break; case SYM_PARENT: if (GOB_PARENT(gob)) { SET_GOB(val, GOB_PARENT(gob)); } else is_none: SET_NONE(val); break; case SYM_DATA: #ifdef HAS_WIDGET_GOB if (GOB_TYPE(gob) == GOBT_WIDGET) { return OS_GET_WIDGET_DATA(gob, val); } #endif data = GOB_DATA(gob); if (GOB_DTYPE(gob) == GOBD_OBJECT) { SET_OBJECT(val, data); } else if (GOB_DTYPE(gob) == GOBD_BLOCK) { Set_Block(val, data); } else if (GOB_DTYPE(gob) == GOBD_STRING) { Set_String(val, data); } else if (GOB_DTYPE(gob) == GOBD_BINARY) { SET_BINARY(val, data); } else if (GOB_DTYPE(gob) == GOBD_INTEGER) { SET_INTEGER(val, (REBIPT)data); } else goto is_none; break; case SYM_FLAGS: Set_Block(val, Flags_To_Block(gob)); break; default: return FALSE; } return TRUE; }