/*--------------------------------------------------------------------------*/ void C2F(texmacsin)(char buffer[], int *buf_size, int *len_line, int *eof, long int dummy1) { #define STDIN_ID 5 #define TEXMACSLIB "texmacslib" int nr = 0, info = 0; if (first == 1) { if (isNamedVarExist(pvApiCtx, TEXMACSLIB) == 0) { if (getWarningMode()) { fprintf(stdout, _("Please install texmacs ATOMS module: atomsInstall('texmacs')\n\n"), _("Warning")); } } fprintf(stdout, "%cverbatim:", DATA_BEGIN); } next_input (); first = 0; *eof = 0; info = LineRead(STDIN_ID, buffer, *buf_size, len_line, &nr); fprintf(stdout, "%cverbatim:", DATA_BEGIN); *len_line = *len_line - 1; }
int main () { cout << DATA_BEGIN << "verbatim:"; cout << "A LaTeX -> TeXmacs converter"; next_input (); cout << DATA_END; fflush (stdout); while (true) { char buffer[100]; cin.getline (buffer, 100, '\n'); cout << DATA_BEGIN << "verbatim:"; cout << DATA_BEGIN; cout << "latex:$" << buffer << "$"; cout << DATA_END; next_input (); cout << DATA_END; fflush (stdout); } return 0; }
int main (void) { char *linebuf = NULL; size_t linebuflen = 0; int ntests = 0; int nfailed = 0; char *escinput = NULL; size_t escinputlen = 0; char *escpattern = NULL; size_t escpatternlen = 0; int nr = 0; mtrace (); /* Read lines from stdin with the following format: locale input-string match-string flags result where `result' is either 0 or 1. If the first character of a string is '"' we read until the next '"' and handled escaped '"'. */ while (! feof (stdin)) { ssize_t n = getline (&linebuf, &linebuflen, stdin); char *cp; const char *locale; const char *input; const char *pattern; const char *result_str; int result; const char *flags; int flags_val; int fnmres; char numbuf[24]; if (n == -1) break; if (n == 0) /* Maybe an empty line. */ continue; /* Skip over all leading white spaces. */ cp = linebuf; locale = next_input (&cp, 1, 0); if (locale == NULL) continue; input = next_input (&cp, 0, 0); if (input == NULL) continue; pattern = next_input (&cp, 0, 0); if (pattern == NULL) continue; result_str = next_input (&cp, 0, 0); if (result_str == NULL) continue; if (strcmp (result_str, "0") == 0) result = 0; else if (strcasecmp (result_str, "NOMATCH") == 0) result = FNM_NOMATCH; else { char *endp; result = strtol (result_str, &endp, 0); if (*endp != '\0') continue; } flags = next_input (&cp, 0, 1); if (flags == NULL) /* We allow the flags missing. */ flags = ""; /* Convert the text describing the flags in a numeric value. */ flags_val = convert_flags (flags); if (flags_val == -1) /* Something went wrong. */ continue; /* Now run the actual test. */ ++ntests; if (setlocale (LC_COLLATE, locale) == NULL || setlocale (LC_CTYPE, locale) == NULL) { puts ("*** Cannot set locale"); ++nfailed; continue; } fnmres = fnmatch (pattern, input, flags_val); printf ("%3d: fnmatch (\"%s\", \"%s\", %s) = %s%c", ++nr, escape (pattern, &escpatternlen, &escpattern), escape (input, &escinputlen, &escinput), flag_output (flags_val), (fnmres == 0 ? "0" : (fnmres == FNM_NOMATCH ? "FNM_NOMATCH" : (sprintf (numbuf, "%d", fnmres), numbuf))), (fnmres != 0) != (result != 0) ? ' ' : '\n'); if ((fnmres != 0) != (result != 0)) { printf ("(FAIL, expected %s) ***\n", result == 0 ? "0" : (result == FNM_NOMATCH ? "FNM_NOMATCH" : (sprintf (numbuf, "%d", result), numbuf))); ++nfailed; } } printf ("=====================\n%3d tests, %3d failed\n", ntests, nfailed); free (escpattern); free (escinput); free (linebuf); return nfailed != 0; }
int main(int argc, char **argv) { int c; int i; buffer_t buffer; boolean_t first = B_TRUE; boolean_t doall = B_FALSE; boolean_t same = B_FALSE; boolean_t newarg = B_FALSE; off_t offset = 0; off_t skip = 0; char *eptr; char *offstr = 0; input = stdin; (void) setlocale(LC_ALL, ""); (void) textdomain(TEXT_DOMAIN); while ((c = getopt(argc, argv, "A:bCcdDfFj:N:oOsSxXvt:")) != EOF) { switch (c) { case 'A': newarg = B_TRUE; if (strlen(optarg) > 1) { afmt = NULL; } switch (*optarg) { case 'o': afmt = "%07llo"; cfmt = " "; break; case 'd': afmt = "%07lld"; cfmt = " "; break; case 'x': afmt = "%07llx"; cfmt = " "; break; case 'n': /* * You could argue that the code should * use the same 7 spaces. Legacy uses 8 * though. Oh well. Better to avoid * gratuitous change. */ afmt = " "; cfmt = " "; break; default: afmt = NULL; break; } if (strlen(optarg) != 1) { afmt = NULL; } if (afmt == NULL) warnx(_("invalid address base, " "must be o, d, x, or n")); break; case 'b': add_out(&output_oct_b); break; case 'c': case 'C': add_out(&output_char); break; case 'f': add_out(&output_float); break; case 'F': add_out(&output_double); break; case 'd': add_out(&output_dec_w); break; case 'D': add_out(&output_dec_d); break; case 't': newarg = B_TRUE; do_type_string(optarg); break; case 'o': add_out(&output_oct_w); break; case 'O': add_out(&output_oct_d); break; case 's': add_out(&output_sig_w); break; case 'S': add_out(&output_sig_d); break; case 'x': add_out(&output_hex_w); break; case 'X': add_out(&output_hex_d); break; case 'v': doall = B_TRUE; break; case 'j': newarg = B_TRUE; skip = strtoll(optarg, &eptr, 0); if (*eptr == 'b') { skip <<= 9; /* 512 bytes */ eptr++; } else if (*eptr == 'k') { skip <<= 10; /* 1k */ eptr++; } else if (*eptr == 'm') { skip <<= 20; /* 1m */ eptr++; } else if (*eptr == 'g') { skip <<= 30; /* 1g */ eptr++; } if ((skip < 0) || (eptr[0] != 0)) { warnx(_("invalid skip count '%s' specified"), optarg); exit(1); } break; case 'N': newarg = B_TRUE; limit = strtoll(optarg, &eptr, 0); /* * POSIX doesn't specify this, but I think these * may be helpful. */ if (*eptr == 'b') { limit <<= 9; eptr++; } else if (*eptr == 'k') { limit <<= 10; eptr++; } else if (*eptr == 'm') { limit <<= 20; eptr++; } else if (*eptr == 'g') { limit <<= 30; eptr++; } if ((limit < 0) || (eptr[0] != 0)) { warnx(_("invalid byte count '%s' specified"), optarg); exit(1); } break; default: usage(); break; } } /* this finds the smallest power of two size we can use */ buffer.mask = (1 << (ffs(blocksize * 3) + 1)) - 1; buffer.data = memalign(16, buffer.mask + 1); if (buffer.data == NULL) { err(1, "memalign"); } /* * Wow. This option parsing is hideous. * * If the we've not seen a new option, and there is just one * operand, if it starts with a "+", then treat it as an * offset. Otherwise if two operands, and the second operand * starts with + or a digit, then it is an offset. */ if (!newarg) { if (((argc - optind) == 1) && (argv[optind][0] == '+')) { offstr = argv[optind]; argc--; } else if (((argc - optind) == 2) && (strchr("+0123456789", (argv[optind + 1][0])) != NULL)) { offstr = argv[optind + 1]; argc--; } } if (offstr) { int base = 0; int mult = 1; int l; if (*offstr == '+') { offstr++; } l = strlen(offstr); if ((strncmp(offstr, "0x", 2) == 0)) { afmt = "%07llx"; base = 16; offstr += 2; if (offstr[l - 1] == 'B') { offstr[l - 1] = 0; l--; mult = 512; } } else { base = 8; afmt = "%07llo"; if ((offstr[l - 1] == 'B') || (offstr[l - 1] == 'b')) { offstr[l - 1] = 0; l--; mult = 512; } if (offstr[l - 1] == '.') { offstr[l - 1] = 0; base = 10; afmt = "%07lld"; } } skip = strtoll(offstr, &eptr, base); if (*eptr != '\0') { errx(1, _("invalid offset string specified")); } skip *= mult; offset += skip; } /* * Allocate an array for all the input files. */ if (argc > optind) { files = calloc(sizeof (char *), argc - optind); for (i = 0; i < argc - optind; i++) { files[i] = argv[optind + i]; numfiles++; } input = next_input(); } else { input = stdin; } /* * We need to seek ahead. fseek would be faster. */ while (skip && (input != NULL)) { struct stat sbuf; /* * Only fseek() on regular files. (Others * we have to read(). */ if (fstat(fileno(input), &sbuf) < 0) { warn("fstat: %s", files[curfile-1]); input = next_input(); continue; } if (S_ISREG(sbuf.st_mode)) { /* * No point in seeking a file that is too * short to begin with. */ if (sbuf.st_size < skip) { skip -= sbuf.st_size; input = next_input(); continue; } if (fseeko(input, skip, SEEK_SET) < 0) { err(1, "fseek:%s", files[curfile-1]); } /* Done seeking. */ skip = 0; break; } /* * fgetc seems like it would be slow, but it uses * buffered I/O, so it should be fast enough. */ flockfile(input); while (skip) { if (getc_unlocked(input) == EOF) { funlockfile(input); if (ferror(input)) { warn("read: %s", files[curfile-1]); } input = next_input(); if (input != NULL) { flockfile(input); } break; } skip--; } if (input != NULL) funlockfile(input); } if (head == NULL) { add_out(&output_oct_w); } buffer.navail = 0; buffer.prod = 0; buffer.cons = 0; for (refill(&buffer); buffer.navail > 0; refill(&buffer)) { output_t *out; int mx; int j, k; /* * If this buffer was the same as last, then just * dump an asterisk. */ if ((!first) && (buffer.navail >= blocksize) && (!doall)) { j = buffer.cons; k = j - blocksize; for (i = 0; i < blocksize; i++) { if (buffer.data[j & buffer.mask] != buffer.data[k & buffer.mask]) { break; } j++; k++; } if (i == blocksize) { if (!same) { (void) fputs("*\n", stdout); same = B_TRUE; } buffer.navail -= blocksize; offset += blocksize; buffer.cons += blocksize; buffer.cons &= buffer.mask; continue; } } first = B_FALSE; same = B_FALSE; mx = (buffer.navail > blocksize) ? blocksize : buffer.navail; for (out = head; out != NULL; out = out->next) { if (out == head) { /*LINTED E_SEC_PRINTF_VAR_FMT*/ (void) printf(afmt, offset); } else { (void) fputs(cfmt, stdout); } for (i = 0, j = buffer.cons; i < mx; i += out->width) { out->func(&buffer, j); j += out->width; j &= buffer.mask; } (void) fputs("\n", stdout); } buffer.cons += mx; buffer.cons &= buffer.mask; offset += mx; buffer.navail -= mx; } /*LINTED E_SEC_PRINTF_VAR_FMT*/ (void) printf(afmt, offset); (void) fputs("\n", stdout); return (0); }
static void refill(buffer_t *b) { int n; int want; int zero; /* * If we have 2 blocks of bytes available, we're done. Note * that each iteration usually loads up 16 bytes, unless we * run out of data. */ while ((input != NULL) && (b->navail < (2 * blocksize))) { /* we preload the next one in advance */ if (limit == 0) { (void) fclose(input); input = NULL; continue; } /* we want to read a whole block if possible */ want = blocksize; if ((limit >= 0) && (want > limit)) { want = limit; } zero = blocksize; while (want && input) { int c; b->prod &= b->mask; c = (b->prod + want > (b->mask + 1)) ? b->mask - b->prod : want; n = fread(b->data + b->prod, 1, c, input); if (n < 0) { warn("read: %s", files ? files[curfile-1] : "stdin"); input = next_input(); continue; } if (n == 0) { input = next_input(); continue; } if (limit >= 0) limit -= n; b->navail += n; b->prod += n; want -= n; zero -= n; } while (zero) { b->data[b->prod & b->mask] = 0; b->prod++; b->prod &= b->mask; zero--; } } }