From 6fa870629612c6d70eed57461e414a4d4492d65b Mon Sep 17 00:00:00 2001 From: Taco Hoekwater Date: Wed, 11 Jun 2008 08:20:50 +0000 Subject: [PATCH] Implement HAVE_SNPRINTF and a default function git-svn-id: svn+ssh://scm.foundry.supelec.fr/svn/metapost/mplib/trunk@540 b0617d17-b707-0410-b22c-fd2634e05cc4 --- src/texk/web2c/mpdir/mp.w | 87 +++++++++++++++++++++++++++++++----- src/texk/web2c/mpdir/psout.w | 76 +++++++++++++++---------------- 2 files changed, 114 insertions(+), 49 deletions(-) diff --git a/src/texk/web2c/mpdir/mp.w b/src/texk/web2c/mpdir/mp.w index 56d6fb4..ffaad74 100644 --- a/src/texk/web2c/mpdir/mp.w +++ b/src/texk/web2c/mpdir/mp.w @@ -203,7 +203,7 @@ int mp_initialize (MP mp) { /* this procedure gets things started properly */ @; if ( mp->bad>0 ) { char ss[256]; - snprintf(ss,256,"Ouch---my internal constants have been clobbered!\n" + mp_snprintf(ss,256,"Ouch---my internal constants have been clobbered!\n" "---case %i",(int)mp->bad); do_fprintf(mp->err_out,(char *)ss); @.Ouch...clobbered@> @@ -3886,6 +3886,7 @@ void mp_xfree (void *x); void *mp_xrealloc (MP mp, void *p, size_t nmem, size_t size) ; void *mp_xmalloc (MP mp, size_t nmem, size_t size) ; char *mp_xstrdup(MP mp, const char *s); +void mp_do_snprintf(char *str, int size, const char *fmt, ...); @ The |max_size_test| guards against overflow, on the assumption that |size_t| is at least 31bits wide. @@ -3934,6 +3935,72 @@ char *mp_xstrdup(MP mp, const char *s) { return w; } +@ @= +#ifdef HAVE_SNPRINTF +#define mp_snprintf (void)snprintf +#else +#define mp_snprintf mp_do_snprintf +#endif + +@ This internal version is rather stupid, but good enough for its purpose. + +@c +void mp_do_snprintf (char *str, int size, const char *format, ...) { + const char *fmt; + char *res, *work; + char workbuf[32]; + va_list ap; + work = (char *)workbuf; + va_start(ap, format); + res = str; + for (fmt=format;*fmt!='\0';fmt++) { + if (*fmt=='%') { + fmt++; + switch(*fmt) { + case 's': + { + char *s = va_arg(ap, char *); + while (*s) { + *res = *s++; + if (size-->0) res++; + } + } + break; + case 'i': + case 'd': + { + sprintf(work,"%i",va_arg(ap, int)); + while (*work) { + *res = *work++; + if (size-->0) res++; + } + } + break; + case 'g': + { + sprintf(work,"%g",va_arg(ap, double)); + while (*work) { + *res = *work++; + if (size-->0) res++; + } + } + break; + case '%': + *res = '%'; + if (size-->0) res++; + break; + default: + /* hm .. */ + break; + } + } else { + *res = *fmt; + if (size-->0) res++; + } + } + *res = '\0'; + va_end(ap); +} @ @= @@ -24462,7 +24529,7 @@ if ( mp->tfm_changed>0 ) { char s[200]; wlog_ln(" "); if ( mp->bch_labelnl); - snprintf(s,128,"(You used %iw,%ih,%id,%ii,%il,%ik,%ie,%ip metric file positions)", + mp_snprintf(s,128,"(You used %iw,%ih,%id,%ii,%il,%ik,%ie,%ip metric file positions)", mp->nw, mp->nh, mp->nd, mp->ni, mp->nl, mp->nk, mp->ne,mp->np); wlog_ln(s); } @@ -25091,7 +25158,7 @@ extreme cases so it may have to be shortened on some systems. @= { s = xmalloc(7,1); - snprintf(s,7,".%i",(int)c); + mp_snprintf(s,7,".%i",(int)c); } @ The user won't want to see all the output file names so we only save the @@ -25782,7 +25849,7 @@ if (x!=69073) goto OFF_BASE { xfree(mp->mem_ident); mp->mem_ident = xmalloc(256,1); - snprintf(mp->mem_ident,256," (mem=%s %i.%i.%i)", + mp_snprintf(mp->mem_ident,256," (mem=%s %i.%i.%i)", mp->job_name, (int)(mp_round_unscaled(mp, mp->internal[mp_year]) % 100), (int)mp_round_unscaled(mp, mp->internal[mp_month]), @@ -25946,26 +26013,26 @@ if ( mp->log_opened ) { wlog_ln(" "); wlog_ln("Here is how much of MetaPost's memory you used:"); @.Here is how much...@> - snprintf(s,128," %i string%s out of %i",(int)mp->max_strs_used-mp->init_str_use, + mp_snprintf(s,128," %i string%s out of %i",(int)mp->max_strs_used-mp->init_str_use, (mp->max_strs_used!=mp->init_str_use+1 ? "s" : ""), (int)(mp->max_strings-1-mp->init_str_use)); wlog_ln(s); - snprintf(s,128," %i string characters out of %i", + mp_snprintf(s,128," %i string characters out of %i", (int)mp->max_pl_used-mp->init_pool_ptr, (int)mp->pool_size-mp->init_pool_ptr); wlog_ln(s); - snprintf(s,128," %i words of memory out of %i", + mp_snprintf(s,128," %i words of memory out of %i", (int)mp->lo_mem_max+mp->mem_end-mp->hi_mem_min+2, (int)mp->mem_end); wlog_ln(s); - snprintf(s,128," %i symbolic tokens out of %i", (int)mp->st_count, (int)mp->hash_size); + mp_snprintf(s,128," %i symbolic tokens out of %i", (int)mp->st_count, (int)mp->hash_size); wlog_ln(s); - snprintf(s,128," %ii,%in,%ip,%ib stack positions out of %ii,%in,%ip,%ib", + mp_snprintf(s,128," %ii,%in,%ip,%ib stack positions out of %ii,%in,%ip,%ib", (int)mp->max_in_stack,(int)mp->int_ptr, (int)mp->max_param_stack,(int)mp->max_buf_stack+1, (int)mp->stack_size,(int)mp->max_internal,(int)mp->param_size,(int)mp->buf_size); wlog_ln(s); - snprintf(s,128," %i string compactions (moved %i characters, %i strings)", + mp_snprintf(s,128," %i string compactions (moved %i characters, %i strings)", (int)mp->pact_count,(int)mp->pact_chars,(int)mp->pact_strs); wlog_ln(s); } diff --git a/src/texk/web2c/mpdir/psout.w b/src/texk/web2c/mpdir/psout.w index ffc7d64..94fd844 100644 --- a/src/texk/web2c/mpdir/psout.w +++ b/src/texk/web2c/mpdir/psout.w @@ -265,7 +265,7 @@ First, here are a few helpers for parsing files @d check_buf(size, buf_size) if ((unsigned)(size) > (unsigned)(buf_size)) { char S[128]; - snprintf(S,128,"buffer overflow: (%d,%d) at file %s, line %d", + mp_snprintf(S,128,"buffer overflow: (%d,%d) at file %s, line %d", size,buf_size, __FILE__, __LINE__ ); mp_fatal_error(mp,S); } @@ -836,13 +836,13 @@ static int avl_do_entry (MP mp, fm_entry * fp, int mode) { p = (fm_entry *) avl_find (mp->ps->tfm_tree, fp); if (p != NULL) { if (mode == FM_DUPIGNORE) { - snprintf(s,128,"fontmap entry for `%s' already exists, duplicates ignored", + mp_snprintf(s,128,"fontmap entry for `%s' already exists, duplicates ignored", fp->tfm_name); mp_warn(mp,s); goto exit; } else { /* mode == |FM_REPLACE| / |FM_DELETE| */ if (mp_has_font_size(mp,p->tfm_num)) { - snprintf(s,128, + mp_snprintf(s,128, "fontmap entry for `%s' has been used, replace/delete not allowed", fp->tfm_name); mp_warn(mp,s); @@ -869,7 +869,7 @@ static int avl_do_entry (MP mp, fm_entry * fp, int mode) { p = (fm_entry *) avl_find (mp->ps->ps_tree, fp); if (p != NULL) { if (mode == FM_DUPIGNORE) { - snprintf(s,128, + mp_snprintf(s,128, "ps_name entry for `%s' already exists, duplicates ignored", fp->ps_name); mp_warn(mp,s); @@ -877,7 +877,7 @@ static int avl_do_entry (MP mp, fm_entry * fp, int mode) { } else { /* mode == |FM_REPLACE| / |FM_DELETE| */ if (mp_has_font_size(mp,p->tfm_num)) { /* REPLACE/DELETE not allowed */ - snprintf(s,128, + mp_snprintf(s,128, "fontmap entry for `%s' has been used, replace/delete not allowed", p->tfm_name); mp_warn(mp,s); @@ -914,7 +914,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { if (is_basefont (fm)) { if (is_fontfile (fm) && !is_included (fm)) { if (warn) { - snprintf(s,128, "invalid entry for `%s': " + mp_snprintf(s,128, "invalid entry for `%s': " "font file must be included or omitted for base fonts", fm->tfm_name); mp_warn(mp,s); @@ -925,7 +925,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { /* if no font file given, drop this entry */ /* |if (!is_fontfile (fm)) { if (warn) { - snprintf(s,128, + mp_snprintf(s,128, "invalid entry for `%s': font file missing", fm->tfm_name); mp_warn(mp,s); @@ -937,7 +937,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { } if (is_truetype (fm) && is_reencoded (fm) && !is_subsetted (fm)) { if (warn) { - snprintf(s,128, + mp_snprintf(s,128, "invalid entry for `%s': only subsetted TrueType font can be reencoded", fm->tfm_name); mp_warn(mp,s); @@ -947,7 +947,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { if ((fm->slant != 0 || fm->extend != 0) && (is_truetype (fm))) { if (warn) { - snprintf(s,128, + mp_snprintf(s,128, "invalid entry for `%s': " "SlantFont/ExtendFont can be used only with embedded T1 fonts", fm->tfm_name); @@ -957,7 +957,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { } if (abs (fm->slant) > 1000) { if (warn) { - snprintf(s,128, + mp_snprintf(s,128, "invalid entry for `%s': too big value of SlantFont (%g)", fm->tfm_name, fm->slant / 1000.0); mp_warn(mp,s); @@ -966,7 +966,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { } if (abs (fm->extend) > 2000) { if (warn) { - snprintf(s,128, + mp_snprintf(s,128, "invalid entry for `%s': too big value of ExtendFont (%g)", fm->tfm_name, fm->extend / 1000.0); mp_warn(mp,s); @@ -977,7 +977,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { !(is_truetype (fm) && is_included (fm) && is_subsetted (fm) && !is_reencoded (fm))) { if (warn) { - snprintf(s,128, + mp_snprintf(s,128, "invalid entry for `%s': " "PidEid can be used only with subsetted non-reencoded TrueType fonts", fm->tfm_name); @@ -1108,7 +1108,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { r++); /* jump over name */ c = *r; /* remember char for temporary end of string */ *r = '\0'; - snprintf(warn_s,128, + mp_snprintf(warn_s,128, "invalid entry for `%s': unknown name `%s' ignored", fm->tfm_name, s); mp_warn(mp,warn_s); @@ -1121,7 +1121,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { if (*r == '"') /* closing quote */ r++; else { - snprintf(warn_s,128, + mp_snprintf(warn_s,128, "invalid entry for `%s': closing quote missing", fm->tfm_name); mp_warn(mp,warn_s); @@ -1200,7 +1200,7 @@ static int check_fm_entry (MP mp, fm_entry * fm, boolean warn) { n = mp->ps->mitem->map_line; mp->ps->fm_file = (mp->open_file)(mp, n, "r", mp_filetype_fontmap); if (!mp->ps->fm_file) { - snprintf(s,256,"cannot open font map file %s",n); + mp_snprintf(s,256,"cannot open font map file %s",n); mp_warn(mp,s); } else { int save_selector = mp->selector; @@ -1492,7 +1492,6 @@ mp->ps->job_id_string = NULL; void mp_set_job_id (MP mp) { char *name_string, *format_string, *s; size_t slen; - int i; if (mp->ps->job_id_string != NULL) return; if ( mp->job_name==NULL ) @@ -1503,14 +1502,13 @@ void mp_set_job_id (MP mp) { strlen (name_string) + strlen (format_string); s = mp_xmalloc (mp,slen, sizeof (char)); - i = snprintf (s, slen, - "%.4d/%.2d/%.2d %.2d:%.2d %s %s", - (mp->internal[mp_year]>>16), - (mp->internal[mp_month]>>16), - (mp->internal[mp_day]>>16), - (mp->internal[mp_time]>>16) / 60, - (mp->internal[mp_time]>>16) % 60, - name_string, format_string); + sprintf (s,"%.4d/%.2d/%.2d %.2d:%.2d %s %s", + (mp->internal[mp_year]>>16), + (mp->internal[mp_month]>>16), + (mp->internal[mp_day]>>16), + (mp->internal[mp_time]>>16) / 60, + (mp->internal[mp_time]>>16) % 60, + name_string, format_string); mp->ps->job_id_string = mp_xstrdup (mp,s); mp_xfree (s); mp_xfree (name_string); @@ -1916,7 +1914,7 @@ static float t1_scan_num (MP mp, char *p, char **r) skip (p, ' '); if (sscanf (p, "%g", &f) != 1) { remove_eol (p, mp->ps->t1_line_array); - snprintf(s,128, "a number expected: `%s'", mp->ps->t1_line_array); + mp_snprintf(s,128, "a number expected: `%s'", mp->ps->t1_line_array); mp_fatal_error(mp,s); } if (r != NULL) { @@ -2092,7 +2090,7 @@ static void t1_check_block_len (MP mp, boolean decrypt) { c = edecrypt (mp,c); l = mp->ps->t1_block_length; if (!(l == 0 && (c == 10 || c == 13))) { - snprintf(s,128,"%i bytes more than expected were ignored", l+ 1); + mp_snprintf(s,128,"%i bytes more than expected were ignored", l+ 1); mp_warn(mp,s); while (l-- > 0) t1_getbyte (mp); @@ -2196,7 +2194,7 @@ static void t1_scan_keys (MP mp, int tex_font,fm_entry *fm_cur) { p = mp->ps->t1_line_array + strlen ("FontType") + 1; if ((i = t1_scan_num (mp,p, 0)) != 1) { char s[128]; - snprintf(s,125,"Type%d fonts unsupported by metapost", i); + mp_snprintf(s,125,"Type%d fonts unsupported by metapost", i); mp_fatal_error(mp,s); } return; @@ -2213,7 +2211,7 @@ static void t1_scan_keys (MP mp, int tex_font,fm_entry *fm_cur) { if (*p != '/') { char s[128]; remove_eol (p, mp->ps->t1_line_array); - snprintf(s,128,"a name expected: `%s'", mp->ps->t1_line_array); + mp_snprintf(s,128,"a name expected: `%s'", mp->ps->t1_line_array); mp_fatal_error(mp,s); } r = ++p; /* skip the slash */ @@ -2293,7 +2291,7 @@ static void t1_builtin_enc (MP mp) { mp->ps->t1_encoding = ENC_STANDARD; } else { char s[128]; - snprintf(s,128, "cannot subset font (unknown predefined encoding `%s')", + mp_snprintf(s,128, "cannot subset font (unknown predefined encoding `%s')", mp->ps->t1_buf_array); mp_fatal_error(mp,s); } @@ -2343,7 +2341,7 @@ static void t1_builtin_enc (MP mp) { else { char s[128]; remove_eol (r, mp->ps->t1_line_array); - snprintf(s,128,"a name or `] def' or `] readonly def' expected: `%s'", + mp_snprintf(s,128,"a name or `] def' or `] readonly def' expected: `%s'", mp->ps->t1_line_array); mp_fatal_error(mp,s); } @@ -2485,7 +2483,7 @@ static void t1_include (MP mp, int tex_font, fm_entry *fm_cur) { @ @d check_subr(SUBR) if (SUBR >= mp->ps->subr_size || SUBR < 0) { char s[128]; - snprintf(s,128,"Subrs array: entry index out of range (%i)",SUBR); + mp_snprintf(s,128,"Subrs array: entry index out of range (%i)",SUBR); mp_fatal_error(mp,s); } @@ -2513,7 +2511,7 @@ static void cs_store (MP mp, boolean is_subr) { ptr = mp->ps->cs_ptr++; if (mp->ps->cs_ptr - mp->ps->cs_tab > mp->ps->cs_size) { char s[128]; - snprintf(s,128,"CharStrings dict: more entries than dict size (%i)",mp->ps->cs_size); + mp_snprintf(s,128,"CharStrings dict: more entries than dict size (%i)",mp->ps->cs_size); mp_fatal_error(mp,s); } if (strcmp (mp->ps->t1_buf_array + 1, notdef) == 0) /* skip the slash */ @@ -2554,7 +2552,7 @@ static boolean is_cc_init = false; #define stack_error(N) { \ char s[256]; \ - snprintf(s,255,"CharString: invalid access (%i) to stack (%i entries)", \ + mp_snprintf(s,255,"CharString: invalid access (%i) to stack (%i entries)", \ (int) N, (int)(stack_ptr - cc_stack)); \ mp_warn(mp,s); \ goto cs_error; \ @@ -2626,9 +2624,9 @@ static void cs_warn (MP mp, const char *cs_name, int subr, const char *fmt, ...) vsprintf (buf, fmt, args); va_end (args); if (cs_name == NULL) { - snprintf(s,299,"Subr (%i): %s", (int) subr, buf); + mp_snprintf(s,299,"Subr (%i): %s", (int) subr, buf); } else { - snprintf(s,299,"CharString (/%s): %s", cs_name, buf); + mp_snprintf(s,299,"CharString (/%s): %s", cs_name, buf); } mp_warn(mp,s); } @@ -2658,7 +2656,7 @@ static void cs_mark (MP mp, const char *cs_name, int subr) break; if (ptr == mp->ps->cs_ptr) { char s[128]; - snprintf (s,128,"glyph `%s' undefined", cs_name); + mp_snprintf (s,128,"glyph `%s' undefined", cs_name); mp_warn(mp,s); return; } @@ -3026,7 +3024,7 @@ static void t1_mark_glyphs (MP mp, int tex_font) if (is_used_char (i)) { if (mp->ps->t1_glyph_names[i] == notdef) { char S[128]; - snprintf(S,128, "character %i is mapped to %s", i, notdef); + mp_snprintf(S,128, "character %i is mapped to %s", i, notdef); mp_warn(mp,S); } else mark_cs (mp,mp->ps->t1_glyph_names[i]); @@ -3323,7 +3321,7 @@ static char * mp_fm_font_subset_name (MP mp, int f) { if (fm != NULL && (fm->ps_name != NULL)) { if (is_subsetted(fm)) { char *s = mp_xmalloc(mp,strlen(fm->ps_name)+8,1); - snprintf(s,strlen(fm->ps_name)+8,"%s-%s",fm->subset_tag,fm->ps_name); + mp_snprintf(s,strlen(fm->ps_name)+8,"%s-%s",fm->subset_tag,fm->ps_name); return s; } else { return mp_xstrdup(mp,fm->ps_name); @@ -3783,7 +3781,7 @@ void mp_print_improved_prologue (MP mp, mp_edge_object *h, int prologues, int pr mp_ps_print(mp, " def"); } else { char s[256]; - snprintf(s,256,"font %s cannot be found in any fontmapfile!", mp->font_name[f]); + mp_snprintf(s,256,"font %s cannot be found in any fontmapfile!", mp->font_name[f]); mp_warn(mp,s); mp_ps_name_out(mp, mp->font_name[f],true); mp_ps_name_out(mp, mp->font_name[f],true); -- 2.32.0.93.g670b81a890