From 843e39d84d4a3d85df1b83a5941538776ab302a7 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Wed, 18 Mar 2009 15:19:23 +0000 Subject: [PATCH] - allow escaping relation characters. - fix troff and fortran magic. --- ChangeLog | 8 +++++++ magic/Magdir/fortran | 2 +- magic/Magdir/troff | 4 ++++ src/apprentice.c | 53 +++++++++++++++++++++++++------------------- 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8b6fb9cc..d04169d3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2009-03-18 16:50 Christos Zoulas + + * Allow escaping of relation characters, so that we can say \^[A-Z] + and the ^ is not eaten as a relation char. + + * Fix troff and fortran to their previous glory using + regex. This was broken since their removel from ascmagic. + 2009-03-10 16:50 Christos Zoulas * don't use strlen in strndup() (Toby Peterson) diff --git a/magic/Magdir/fortran b/magic/Magdir/fortran index 3e490333..f42c7c8a 100644 --- a/magic/Magdir/fortran +++ b/magic/Magdir/fortran @@ -1,3 +1,3 @@ # FORTRAN source -0 string/c c\ FORTRAN program +0 regex/100 \^[Cc][\ \t] FORTRAN program !:mime text/x-fortran diff --git a/magic/Magdir/troff b/magic/Magdir/troff index 337ca683..73731a89 100644 --- a/magic/Magdir/troff +++ b/magic/Magdir/troff @@ -14,6 +14,10 @@ !:mime text/troff 0 search/1 ''' troff or preprocessor input text !:mime text/troff +0 regex/20 \^\\.[A-Za-z0-9][A-Za-z0-9][\ \t] troff or preprocessor input text +!:mime text/troff +0 regex/20 \^\\.[A-Za-z0-9][A-Za-z0-9]$ troff or preprocessor input text +!:mime text/troff # ditroff intermediate output text 0 search/1 x\ T ditroff output text diff --git a/src/apprentice.c b/src/apprentice.c index e8813a15..bbadc0c2 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.150 2009/02/17 16:29:03 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.151 2009/03/18 15:19:23 christos Exp $") #endif /* lint */ #include "magic.h" @@ -89,8 +89,8 @@ const size_t file_nnames = FILE_NAMES_SIZE; private int getvalue(struct magic_set *ms, struct magic *, const char **, int); private int hextoint(int); -private const char *getstr(struct magic_set *, const char *, char *, int, - int *, int); +private const char *getstr(struct magic_set *, struct magic *, const char *, + int); private int parse(struct magic_set *, struct magic_entry **, uint32_t *, const char *, size_t, int); private void eatsize(const char **); @@ -1741,8 +1741,6 @@ check_format(struct magic_set *ms, struct magic *m) private int getvalue(struct magic_set *ms, struct magic *m, const char **p, int action) { - int slen; - switch (m->type) { case FILE_BESTRING16: case FILE_LESTRING16: @@ -1750,16 +1748,13 @@ getvalue(struct magic_set *ms, struct magic *m, const char **p, int action) case FILE_PSTRING: case FILE_REGEX: case FILE_SEARCH: - *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen, action); + *p = getstr(ms, m, *p, action == FILE_COMPILE); if (*p == NULL) { if (ms->flags & MAGIC_CHECK) file_magwarn(ms, "cannot get string from `%s'", m->value.s); return -1; } - m->vallen = slen; - if (m->type == FILE_PSTRING) - m->vallen++; return 0; case FILE_FLOAT: case FILE_BEFLOAT: @@ -1798,13 +1793,15 @@ getvalue(struct magic_set *ms, struct magic *m, const char **p, int action) /* * Convert a string containing C character escapes. Stop at an unescaped * space or tab. - * Copy the converted version to "p", returning its length in *slen. - * Return updated scan pointer as function result. + * Copy the converted version to "m->value.s", and the length in m->vallen. + * Return updated scan pointer as function result. Warn if set. */ private const char * -getstr(struct magic_set *ms, const char *s, char *p, int plen, int *slen, int action) +getstr(struct magic_set *ms, struct magic *m, const char *s, int warn) { const char *origs = s; + char *p = m->value.s; + size_t plen = sizeof(m->value.s); char *origp = p; char *pmax = p + plen - 1; int c; @@ -1821,25 +1818,33 @@ getstr(struct magic_set *ms, const char *s, char *p, int plen, int *slen, int ac switch(c = *s++) { case '\0': - if (action == FILE_COMPILE) + if (warn) file_magwarn(ms, "incomplete escape"); goto out; case '\t': - if (action == FILE_COMPILE) { + if (warn) { file_magwarn(ms, "escaped tab found, use \\t instead"); - action++; + warn = 0; /* already did */ } /*FALLTHROUGH*/ default: - if (action == FILE_COMPILE) { - if (isprint((unsigned char)c)) - file_magwarn(ms, - "no need to escape `%c'", c); - else - file_magwarn(ms, - "unknown escape sequence: \\%03o", c); + if (warn) { + if (isprint((unsigned char)c)) { + /* Allow escaping of + * ``relations'' */ + if (strchr("<>&^=!", c) + == NULL) { + file_magwarn(ms, "no " + "need to escape " + "`%c'", c); + } + } else { + file_magwarn(ms, + "unknown escape sequence: " + "\\%03o", c); + } } /*FALLTHROUGH*/ /* space, perhaps force people to use \040? */ @@ -1938,7 +1943,9 @@ getstr(struct magic_set *ms, const char *s, char *p, int plen, int *slen, int ac } out: *p = '\0'; - *slen = p - origp; + m->vallen = p - origp; + if (m->type == FILE_PSTRING) + m->vallen++; return s; } -- 2.50.1