]> granicus.if.org Git - file/commitdiff
Print the filename and linenumber on syntax errors.
authorChristos Zoulas <christos@zoulas.com>
Sat, 13 Nov 2004 08:11:39 +0000 (08:11 +0000)
committerChristos Zoulas <christos@zoulas.com>
Sat, 13 Nov 2004 08:11:39 +0000 (08:11 +0000)
src/apprentice.c
src/file.h
src/print.c

index 54270ac4f0b219e083d7b2f0a548128206185288..38f9d51edbd8c825e5bc7d73436db2ad460235bc 100644 (file)
@@ -45,7 +45,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$Id: apprentice.c,v 1.79 2004/09/11 19:15:57 christos Exp $")
+FILE_RCSID("@(#)$Id: apprentice.c,v 1.80 2004/11/13 08:11:39 christos Exp $")
 #endif /* lint */
 
 #define        EATAB {while (isascii((unsigned char) *l) && \
@@ -91,7 +91,7 @@ private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
     const char *);
 private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
     const char *);
-private int check_format(struct magic *);
+private int check_format(struct magic_set *, struct magic *);
 
 private size_t maxmagic = 0;
 private size_t magicsize = sizeof(struct magic);
@@ -161,7 +161,7 @@ apprentice_1(struct magic_set *ms, const char *fn, int action,
 #ifndef COMPILE_ONLY
        if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
                if (ms->flags & MAGIC_CHECK)
-                       file_magwarn("using regular magic file `%s'", fn);
+                       file_magwarn(ms, "using regular magic file `%s'", fn);
                rv = apprentice_file(ms, &magic, &nmagic, fn, action);
                if (rv != 0)
                        return -1;
@@ -292,10 +292,9 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
                "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
        FILE *f;
        char line[BUFSIZ+1];
-       int lineno;
        int errs = 0;
 
-       f = fopen(fn, "r");
+       f = fopen(ms->file = fn, "r");
        if (f == NULL) {
                if (errno != ENOENT)
                        file_error(ms, errno, "cannot read magic file `%s'",
@@ -316,12 +315,14 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
                (void)fprintf(stderr, "%s\n", hdr);
 
        /* parse it */
-       for (lineno = 1; fgets(line, BUFSIZ, f) != NULL; lineno++) {
+       for (ms->line = 1; fgets(line, BUFSIZ, f) != NULL; ms->line++) {
+               size_t len;
                if (line[0]=='#')       /* comment, do not parse */
                        continue;
-               if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */
+               len = strlen(line);
+               if (len < 2) /* null line, garbage, etc */
                        continue;
-               line[strlen(line)-1] = '\0'; /* delete newline */
+               line[len - 1] = '\0'; /* delete newline */
                if (parse(ms, magicp, nmagicp, line, action) != 0)
                        errs = 1;
        }
@@ -374,7 +375,7 @@ file_signextend(struct magic_set *ms, struct magic *m, uint32_t v)
                        break;
                default:
                        if (ms->flags & MAGIC_CHECK)
-                           file_magwarn("cannot happen: m->type=%d\n",
+                           file_magwarn(ms, "cannot happen: m->type=%d\n",
                                    m->type);
                        return ~0U;
                }
@@ -430,7 +431,7 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
        m->offset = (uint32_t)strtoul(l, &t, 0);
         if (l == t)
                if (ms->flags & MAGIC_CHECK)
-                       file_magwarn("offset %s invalid", l);
+                       file_magwarn(ms, "offset `%s' invalid", l);
         l = t;
 
        if (m->flag & INDIR) {
@@ -464,8 +465,8 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
                                break;
                        default:
                                if (ms->flags & MAGIC_CHECK)
-                                       file_magwarn(
-                                           "indirect offset type %c invalid",
+                                       file_magwarn(ms,
+                                           "indirect offset type `%c' invalid",
                                            *l);
                                break;
                        }
@@ -515,7 +516,8 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
                        t = l;
                if (*t++ != ')') 
                        if (ms->flags & MAGIC_CHECK)
-                               file_magwarn("missing ')' in indirect offset");
+                               file_magwarn(ms,
+                                   "missing ')' in indirect offset");
                l = t;
        }
 
@@ -600,7 +602,7 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
                l += sizeof("regex");
        } else {
                if (ms->flags & MAGIC_CHECK)
-                       file_magwarn("type %s invalid", l);
+                       file_magwarn(ms, "type `%s' invalid", l);
                return -1;
        }
        /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
@@ -635,8 +637,8 @@ parse(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, char *l,
                                        break;
                                default:
                                        if (ms->flags & MAGIC_CHECK)
-                                               file_magwarn(
-                                               "string extension %c invalid",
+                                               file_magwarn(ms,
+                                               "string extension `%c' invalid",
                                                *l);
                                        return -1;
                                }
@@ -708,7 +710,7 @@ GetDesc:
                /* NULLBODY */;
 
        if (ms->flags & MAGIC_CHECK) {
-               if (!check_format(m))
+               if (!check_format(ms, m))
                        return -1;
        }
 #ifndef COMPILE_ONLY
@@ -725,7 +727,7 @@ GetDesc:
  * the type of the magic.
  */
 private int
-check_format(struct magic *m)
+check_format(struct magic_set *ms, struct magic *m)
 {
        static const char *formats[] = { FILE_FORMAT_STRING };
        static const char *names[] = { FILE_FORMAT_NAME };
@@ -739,13 +741,13 @@ check_format(struct magic *m)
                return 1;
        }
        if (m->type >= sizeof(formats)/sizeof(formats[0])) {
-               file_magwarn("Internal error inconsistency between m->type"
+               file_magwarn(ms, "Internal error inconsistency between m->type"
                    " and format strings");
                return 0;
        }
        if (formats[m->type] == NULL) {
-               file_magwarn("No format string for `%s' with description `%s'",
-                   m->desc, names[m->type]);
+               file_magwarn(ms, "No format string for `%s' with description "
+                   "`%s'", m->desc, names[m->type]);
                return 0;
        }
        for (; *ptr; ptr++) {
@@ -758,12 +760,12 @@ check_format(struct magic *m)
        }
        if (*ptr == '\0') {
                /* Missing format string; bad */
-               file_magwarn("Invalid format `%s' for type `%s'",
+               file_magwarn(ms, "Invalid format `%s' for type `%s'",
                        m->desc, names[m->type]);
                return 0;
        }
        if (strchr(formats[m->type], *ptr) == NULL) {
-               file_magwarn("Printf format `%c' is not valid for type `%s'"
+               file_magwarn(ms, "Printf format `%c' is not valid for type `%s'"
                    " in description `%s'",
                        *ptr, names[m->type], m->desc);
                return 0;
@@ -788,7 +790,7 @@ getvalue(struct magic_set *ms, struct magic *m, char **p)
                *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
                if (*p == NULL) {
                        if (ms->flags & MAGIC_CHECK)
-                               file_magwarn("cannot get string from `%s'",
+                               file_magwarn(ms, "cannot get string from `%s'",
                                    m->value.s);
                        return -1;
                }
index 4cbd834f53fa9ce1a98990af8d0f9676a550c3dd..2f10939944e453c148bee3ec6b42a4ad712b599c 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.62 2004/09/11 19:15:57 christos Exp $
+ * @(#)$Id: file.h,v 1.63 2004/11/13 08:11:39 christos Exp $
  */
 
 #ifndef __file_h__
@@ -229,6 +229,8 @@ struct magic_set {
     int error;
     int flags;
     int haderr;
+    const char *file;
+    size_t line;
 };
 
 struct stat;
@@ -250,7 +252,7 @@ protected void file_badread(struct magic_set *);
 protected void file_badseek(struct magic_set *);
 protected void file_oomem(struct magic_set *);
 protected void file_error(struct magic_set *, int, const char *, ...);
-protected void file_magwarn(const char *, ...);
+protected void file_magwarn(struct magic_set *, const char *, ...);
 protected void file_mdump(struct magic *);
 protected void file_showstr(FILE *, const char *, size_t);
 protected size_t file_mbswidth(const char *);
index da26007d6082c03d9a5b891a3827d51d328591df..81eb7895d0dd100ffe1046e1e2c71507d08d8260 100644 (file)
@@ -41,7 +41,7 @@
 #include <time.h>
 
 #ifndef lint
-FILE_RCSID("@(#)$Id: print.c,v 1.45 2004/09/11 19:15:57 christos Exp $")
+FILE_RCSID("@(#)$Id: print.c,v 1.46 2004/11/13 08:11:39 christos Exp $")
 #endif  /* lint */
 
 #define SZOF(a)        (sizeof(a) / sizeof(a[0]))
@@ -137,7 +137,7 @@ file_mdump(struct magic *m)
 
 /*VARARGS*/
 protected void
-file_magwarn(const char *f, ...)
+file_magwarn(struct magic_set *ms, const char *f, ...)
 {
        va_list va;
        va_start(va, f);
@@ -145,7 +145,8 @@ file_magwarn(const char *f, ...)
        /* cuz we use stdout for most, stderr here */
        (void) fflush(stdout); 
 
-       (void) fprintf(stderr, "WARNING: ");
+       (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
+           (unsigned long)ms->line);
        (void) vfprintf(stderr, f, va);
        va_end(va);
        fputc('\n', stderr);