static p_file *CopyProlog(const char *name, const char *title) { p_file *psps= GistOpen("ps.ps"); p_file *file= strcmp(name, "*stdout*")? CREATE_PS(name) : pf_stdout; if (!psps) strcpy(gistError, "unable to open PostScript prolog ps.ps"); if (!file) strcpy(gistError, "unable to create PostScript output file"); if (psps && file) { titleIs= title; needUser= needDate= 1; for (;;) { if (!p_fgets(psps, line, 79) || PutPrologLine(file)<0) { if (file!=pf_stdout) p_fclose(file); file= 0; strcpy(gistError, "bad PostScript prolog format in ps.ps??"); break; } if (strncmp(line, "%%EndSetup", 10L)==0) break; } } else if (file) { if (file!=pf_stdout) p_fclose(file); file= 0; } if (psps) p_fclose(psps); return file; }
static int GetNextLine(p_file *file, int context) { /* assert file!=0 */ if (file && (yg_blocking || y_read_prompt)) return 0; if (!Ygets(&ypBuffer, file)) { if (file) { int hadEOF= Yfeof(file); int hadError= Yferror(file); p_fclose(file); ypIncludes[nYpIncludes-1].file= 0; /* Any errors here are serious enough to warrant a panic stop. */ if (hadError) YError("****ABORTING PARSE**** error reading include file"); if (!hadEOF) YError("****ABORTING PARSE**** include file not ASCII text"); } return 0; /* just a normal EOF */ } if (nYpIncludes) { long lnum= ++ypIncludes[nYpIncludes-1].lastLineRead; if (context==NL_MAIN || context==NL_NOINPUT) ypBeginLine= lnum; } else { if (context==NL_MAIN || context==NL_NOINPUT) ypBeginLine= 0; else ypBeginLine--; } return 1; }
static int PutLine(PSEngine *psEngine) { p_file *file= psEngine->file; char *line= psEngine->line; long nchars= psEngine->nchars; if (!file) { if (psEngine->closed) return 1; file= psEngine->file= CopyProlog(psEngine->filename, psEngine->e.name); if (!file) { psEngine->closed= 1; return 1; } psEngine->currentPage= 1; SetPageDefaults(psEngine); InitBB(&psEngine->docBB); } line[nchars++]= '\n'; line[nchars]= '\0'; if (ps_fputs(file, line)<0) { if (file!=pf_stdout) p_fclose(file); psEngine->file= 0; psEngine->closed= 1; strcpy(gistError, "p_fputs failed writing PostScript file"); return 1; } line[0]= '\0'; psEngine->nchars= 0; return 0; }
static void Kill(Engine *engine) { PSEngine *psEngine= (PSEngine *)engine; long fonts= psEngine->fonts; int i, xll, yll, xur, yur; int bad= 0; if (psEngine->e.marked) bad= EndPage(psEngine); if (psEngine->file) { if (!bad) bad= PutLine(psEngine); if (!bad) bad= Append(psEngine, "%%Trailer"); if (!bad) bad= PutLine(psEngine); sprintf(line, "%%%%Pages: %d", psEngine->currentPage-1); if (!bad) bad= Append(psEngine, line); if (!bad) bad= PutLine(psEngine); xll= psEngine->docBB.xll; xur= psEngine->docBB.xur; if (xll < xur) { yll= psEngine->docBB.yll; yur= psEngine->docBB.yur; } else { xll= yll= 0; xur= 612; yur= 792; } sprintf(line, "%%%%BoundingBox: %d %d %d %d", xll, yll, xur, yur); if (!bad) bad= Append(psEngine, line); if (!bad) bad= PutLine(psEngine); strcpy(line, "%%DocumentFonts: "); for (i=0 ; i<N_PSFONTS ; i++) { if ((1<<i) & fonts) { strcat(line, psFontNames[i]); if (!bad) bad= Append(psEngine, line); if (!bad) bad= PutLine(psEngine); strcpy(line, "%%+ "); } } if (psEngine->file!=pf_stdout) p_fclose(psEngine->file); } GpDelEngine(engine); }
void YpClearIncludes(void) { need_endif= 0; if (nYpIncludes>0) { do { nYpIncludes--; if (ypIncludes[nYpIncludes].file) p_fclose(ypIncludes[nYpIncludes].file); p_free(ypIncludes[nYpIncludes].filename); } while (nYpIncludes); } else { nYpIncludes= 0; } YaltNextLine= 0; if (nYpInputs>0) do p_free(ypInputs[--nYpInputs]); while (nYpInputs); else nYpInputs= 0; /* also clear pending stdin lines */ y_remove_line(1); }
long ReopenSource(long index, int notExtern, long isrc) { long source, position; p_file *file; source = (isrc<0)? FindSource(index) : isrc; if (source<0) return -1; /* source of func unknown */ file= PushInclude(sourceTab.names[source], 0); if (!file) return -2; /* unable to open source file */ position= ScanForFunc(globalTable.names[index], notExtern); if (position<0 || p_fseek(file, position)) { if (ypIncludes[nYpIncludes-1].file) { p_fclose(file); ypIncludes[nYpIncludes-1].file= 0; } if (position<0) return -3; /* func no longer in source file */ else return -4; /* seek error */ } return position; }
static void test_fread_s(void) { static const char test_file[] = "fread_s.tst"; int ret; char buf[10]; FILE *f = fopen(test_file, "w"); if(!f) { skip("Error creating test file\n"); return; } fwrite("test", 1, 4, f); fclose(f); ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL, "Invalid parameter handler was already set\n"); SET_EXPECT(invalid_parameter_handler); errno = 0xdeadbeef; ret = p_fread_s(buf, sizeof(buf), 1, 1, NULL); ok(ret == 0, "fread_s returned %d, expected 0\n", ret); ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno); CHECK_CALLED(invalid_parameter_handler); f = p_fopen(test_file, "r"); errno = 0xdeadbeef; ret = p_fread_s(NULL, sizeof(buf), 0, 1, f); ok(ret == 0, "fread_s returned %d, expected 0\n", ret); ok(errno == 0xdeadbeef, "errno = %d, expected 0xdeadbeef\n", errno); ret = p_fread_s(NULL, sizeof(buf), 1, 0, f); ok(ret == 0, "fread_s returned %d, expected 0\n", ret); ok(errno == 0xdeadbeef, "errno = %d, expected 0xdeadbeef\n", errno); SET_EXPECT(invalid_parameter_handler); errno = 0xdeadbeef; ret = p_fread_s(NULL, sizeof(buf), 1, 1, f); ok(ret == 0, "fread_s returned %d, expected 0\n", ret); ok(errno == EINVAL, "errno = %d, expected EINVAL\n", errno); CHECK_CALLED(invalid_parameter_handler); SET_EXPECT(invalid_parameter_handler); errno = 0xdeadbeef; buf[1] = 'a'; ret = p_fread_s(buf, 3, 1, 10, f); ok(ret == 0, "fread_s returned %d, expected 0\n", ret); ok(buf[0] == 0, "buf[0] = '%c', expected 0\n", buf[0]); ok(buf[1] == 0, "buf[1] = '%c', expected 0\n", buf[1]); ok(errno == ERANGE, "errno = %d, expected ERANGE\n", errno); CHECK_CALLED(invalid_parameter_handler); SET_EXPECT(invalid_parameter_handler); errno = 0xdeadbeef; ret = p_fread_s(buf, 2, 1, 10, f); ok(ret == 0, "fread_s returned %d, expected 0\n", ret); ok(buf[0] == 0, "buf[0] = '%c', expected 0\n", buf[0]); ok(errno == ERANGE, "errno = %d, expected ERANGE\n", errno); CHECK_CALLED(invalid_parameter_handler); memset(buf, 'a', sizeof(buf)); ret = p_fread_s(buf, sizeof(buf), 3, 10, f); ok(ret==1, "fread_s returned %d, expected 1\n", ret); ok(buf[0] == 'e', "buf[0] = '%c', expected 'e'\n", buf[0]); ok(buf[1] == 's', "buf[1] = '%c', expected 's'\n", buf[1]); ok(buf[2] == 't', "buf[2] = '%c', expected 't'\n", buf[2]); ok(buf[3] == 'a', "buf[3] = '%c', expected 'a'\n", buf[3]); p_fclose(f); ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler, "Cannot reset invalid parameter handler\n"); unlink(test_file); }
static void test_fwprintf(void) { static const char file_name[] = "fprintf.tst"; static const WCHAR simple[] = {'s','i','m','p','l','e',' ','t','e','s','t','\n',0}; static const WCHAR cont_fmt[] = {'c','o','n','t','a','i','n','s','%','c','n','u','l','l','\n',0}; static const WCHAR cont[] = {'c','o','n','t','a','i','n','s','\0','n','u','l','l','\n',0}; FILE *fp = p_fopen(file_name, "wb"); wchar_t bufw[1024]; char bufa[1024]; int ret; ret = vfwprintf_wrapper(fp, simple); ok(ret == 12, "ret = %d\n", ret); ret = p_ftell(fp); ok(ret == 24, "ftell returned %d\n", ret); ret = vfwprintf_wrapper(fp, cont_fmt, '\0'); ok(ret == 14, "ret = %d\n", ret); ret = p_ftell(fp); ok(ret == 52, "ftell returned %d\n", ret); p_fclose(fp); fp = p_fopen(file_name, "rb"); p_fgetws(bufw, sizeof(bufw)/sizeof(bufw[0]), fp); ret = p_ftell(fp); ok(ret == 24, "ftell returned %d\n", ret); ok(!wcscmp(bufw, simple), "buf = %s\n", wine_dbgstr_w(bufw)); p_fgetws(bufw, sizeof(bufw)/sizeof(bufw[0]), fp); ret = p_ftell(fp); ok(ret == 52, "ret = %d\n", ret); ok(!memcmp(bufw, cont, 28), "buf = %s\n", wine_dbgstr_w(bufw)); p_fclose(fp); fp = p_fopen(file_name, "wt"); ret = vfwprintf_wrapper(fp, simple); ok(ret == 12, "ret = %d\n", ret); ret = p_ftell(fp); ok(ret == 13, "ftell returned %d\n", ret); ret = vfwprintf_wrapper(fp, cont_fmt, '\0'); ok(ret == 14, "ret = %d\n", ret); ret = p_ftell(fp); ok(ret == 28, "ftell returned %d\n", ret); p_fclose(fp); fp = p_fopen(file_name, "rb"); p_fgets(bufa, sizeof(bufa), fp); ret = p_ftell(fp); ok(ret == 13, "ftell returned %d\n", ret); ok(!strcmp(bufa, "simple test\r\n"), "buf = %s\n", bufa); p_fgets(bufa, sizeof(bufa), fp); ret = p_ftell(fp); ok(ret == 28, "ret = %d\n", ret); ok(!memcmp(bufa, "contains\0null\r\n", 15), "buf = %s\n", bufa); p_fclose(fp); unlink(file_name); ok(p_set_invalid_parameter_handler(test_invalid_parameter_handler) == NULL, "Invalid parameter handler was already set\n"); /* NULL format */ errno = 0xdeadbeef; SET_EXPECT(invalid_parameter_handler); ret = vfwprintf_wrapper(fp, NULL); ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno); ok(ret == -1, "expected ret -1, got %d\n", ret); CHECK_CALLED(invalid_parameter_handler); /* NULL file */ errno = 0xdeadbeef; SET_EXPECT(invalid_parameter_handler); ret = vfwprintf_wrapper(NULL, simple); ok(errno == EINVAL, "expected errno EINVAL, got %d\n", errno); ok(ret == -1, "expected ret -1, got %d\n", ret); CHECK_CALLED(invalid_parameter_handler); /* format using % with NULL arglist*/ /* crashes on Windows */ /* ret = p_vfwprintf(0, fp, cont_fmt, NULL, NULL); */ ok(p_set_invalid_parameter_handler(NULL) == test_invalid_parameter_handler, "Cannot reset invalid parameter handler\n"); }
static void test_fprintf(void) { static const char file_name[] = "fprintf.tst"; FILE *fp = p_fopen(file_name, "wb"); char buf[1024]; int ret; ret = vfprintf_wrapper(fp, "simple test\n"); ok(ret == 12, "ret = %d\n", ret); ret = p_ftell(fp); ok(ret == 12, "ftell returned %d\n", ret); ret = vfprintf_wrapper(fp, "contains%cnull\n", '\0'); ok(ret == 14, "ret = %d\n", ret); ret = p_ftell(fp); ok(ret == 26, "ftell returned %d\n", ret); p_fclose(fp); fp = p_fopen(file_name, "rb"); p_fgets(buf, sizeof(buf), fp); ret = p_ftell(fp); ok(ret == 12, "ftell returned %d\n", ret); ok(!strcmp(buf, "simple test\n"), "buf = %s\n", buf); p_fgets(buf, sizeof(buf), fp); ret = p_ftell(fp); ok(ret == 26, "ret = %d\n", ret); ok(!memcmp(buf, "contains\0null\n", 14), "buf = %s\n", buf); p_fclose(fp); fp = p_fopen(file_name, "wt"); ret = vfprintf_wrapper(fp, "simple test\n"); ok(ret == 12, "ret = %d\n", ret); ret = p_ftell(fp); ok(ret == 13, "ftell returned %d\n", ret); ret = vfprintf_wrapper(fp, "contains%cnull\n", '\0'); ok(ret == 14, "ret = %d\n", ret); ret = p_ftell(fp); ok(ret == 28, "ftell returned %d\n", ret); p_fclose(fp); fp = p_fopen(file_name, "rb"); p_fgets(buf, sizeof(buf), fp); ret = p_ftell(fp); ok(ret == 13, "ftell returned %d\n", ret); ok(!strcmp(buf, "simple test\r\n"), "buf = %s\n", buf); p_fgets(buf, sizeof(buf), fp); ret = p_ftell(fp); ok(ret == 28, "ret = %d\n", ret); ok(!memcmp(buf, "contains\0null\r\n", 15), "buf = %s\n", buf); p_fclose(fp); unlink(file_name); }