2. Use the "FIX_PROC_HEAD()" macro _with_ the "_fix" suffix
(I cannot use the ## magic from ANSI C) for defining your entry point.
-3. Put your test name into the FIXUP_TABLE
+3. Put your test name into the FIXUP_TABLE.
4. Do not read anything from stdin. It is closed.
5. Write to stderr only in the event of a reportable error
- In such an event, call "exit(1)".
+ In such an event, call "exit (EXIT_FAILURE)".
6. You have access to the fixDescList entry for the fix in question.
- This may be useful, for example, if there are pre-compiled
- selection expressions stored there.
-
- For example, you may do this if you know that the first
- test contains a useful regex. This is okay because, remember,
- this code perforce works closely with the inclhack.def fixes!!
-
- tFixDesc* pMyDesc = fixDescList + MY_FIX_NAME_FIXIDX;
- tTestDesc* pTestList = pMyDesc->p_test_desc;
-
- regexec (pTestList->p_test_regex, ...)
+ This may be useful, for example, if there are interesting strings
+ or pre-compiled regular expressions stored there.
= = = = = = = = = = = = = = = = = = = = = = = = =
Boston, MA 02111-1307, USA. */
#include "fixlib.h"
+#define GTYPE_SE_CT 1
+
+#ifdef SEPARATE_FIX_PROC
+#include "fixincl.x"
+#endif
+tSCC zNeedsArg[] = "fixincl error: `%s' needs %s argument (c_fix_arg[%d])\n";
+
+typedef void t_fix_proc PARAMS ((const char *, const char *, tFixDesc *));
typedef struct {
const char* fix_name;
- void (*fix_proc)();
+ t_fix_proc* fix_proc;
} fix_entry_t;
#define FIXUP_TABLE \
- _FT_( "format", format_fix ) \
- _FT_( "char_macro_use", char_macro_use_fix ) \
_FT_( "char_macro_def", char_macro_def_fix ) \
- _FT_( "machine_name", machine_name_fix )
+ _FT_( "char_macro_use", char_macro_use_fix ) \
+ _FT_( "format", format_fix ) \
+ _FT_( "machine_name", machine_name_fix ) \
+ _FT_( "wrap", wrap_fix ) \
+ _FT_( "gnu_type", gnu_type_fix )
-#define FIX_PROC_HEAD( fix ) \
-static void fix ( filname, text, p_fixd ) \
- const char* filname; \
- const char* text; \
+#define FIX_PROC_HEAD( fix ) \
+static void fix PARAMS ((const char *, const char *, tFixDesc *)); /* avoid warning */ \
+static void fix ( filname, text, p_fixd ) \
+ const char* filname; \
+ const char* text; \
tFixDesc* p_fixd;
-
+#ifdef NEED_PRINT_QUOTE
/*
* Skip over a quoted string. Single quote strings may
* contain multiple characters if the first character is
return text;
}
+#endif /* NEED_PRINT_QUOTE */
+
+
+/*
+ * Emit the GNU standard type wrapped up in such a way that
+ * this thing can be encountered countless times during a compile
+ * and not cause even a warning.
+ */
+static const char *emit_gnu_type PARAMS ((const char *, regmatch_t *));
+static const char*
+emit_gnu_type ( text, rm )
+ const char* text;
+ regmatch_t* rm;
+{
+ char z_TYPE[ 64 ];
+ char z_type[ 64 ];
+
+ fwrite (text, rm[0].rm_so, 1, stdout);
+
+ {
+ const char* ps = text + rm[1].rm_so;
+ const char* pe = text + rm[1].rm_eo;
+ char* pd = z_type;
+ char* pD = z_TYPE;
+
+ while (ps < pe)
+ *(pD++) = TOUPPER( *(pd++) = *(ps++) );
+ *pD = *pd = NUL;
+ }
+
+ /*
+ * Now print out the reformed typedef,
+ * with a C++ guard for WCHAR
+ */
+ {
+ tSCC z_fmt[] = "\
+#if !defined(_GCC_%s_T)%s\n\
+#define _GCC_%s_T\n\
+typedef __%s_TYPE__ %s_t;\n\
+#endif\n";
+
+ const char *const pz_guard = (strcmp (z_type, "wchar") == 0)
+ ? " && ! defined(__cplusplus)" : "";
+
+ printf (z_fmt, z_TYPE, pz_guard, z_TYPE, z_TYPE, z_type);
+ }
+
+ return text += rm[0].rm_eo;
+}
+
+
+/*
+ * Copy the `format' string to std out, replacing `%n' expressions
+ * with the matched text from a regular expression evaluation.
+ * Doubled '%' characters will be replaced with a single copy.
+ * '%' characters in other contexts and all other characters are
+ * copied out verbatim.
+ */
+static void format_write PARAMS ((tCC *, tCC *, regmatch_t[]));
static void
format_write (format, text, av)
tCC* format;
tCC* text;
regmatch_t av[];
{
- tCC *p, *str;
- int c;
- size_t len;
-
- for (p = 0; *p; p++) {
- c = *p;
- if (c != '%') {
- putchar(c);
- continue;
- }
-
- c = *++p;
- if (c == '%') {
- putchar(c);
- continue;
- } else if (c < '0' || c > '9') {
- abort();
- }
-
- c -= '0';
- str = text + av[c].rm_so;
- len = av[c].rm_eo - av[c].rm_so;
- fwrite(str, len, 1, stdout);
- }
-}
+ int c;
-FIX_PROC_HEAD( format_fix )
-{
- tSCC zBad[] = "fixincl error: `%s' needs %s c_fix_arg\n";
- tCC* pz_pat = p_fixd->patch_args[2];
- tCC* pz_fmt = p_fixd->patch_args[1];
- const char *p;
- regex_t re;
- regmatch_t rm[10];
+ while ((c = (unsigned)*(format++)) != NUL) {
+
+ if (c != '%')
+ {
+ putchar(c);
+ continue;
+ }
+
+ c = (unsigned)*(format++);
/*
- * We must have a format
+ * IF the character following a '%' is not a digit,
+ * THEN we will always emit a '%' and we may or may
+ * not emit the following character. We will end on
+ * a NUL and we will emit only one of a pair of '%'.
*/
- if (pz_fmt == (tCC*)NULL) {
- fprintf( stderr, zBad, p_fixd->fix_name, "replacement-format" );
- exit( 3 );
- }
+ if (! ISDIGIT ( c ))
+ {
+ putchar( '%' );
+ switch (c) {
+ case NUL:
+ return;
+ case '%':
+ break;
+ default:
+ putchar(c);
+ }
+ }
/*
- * IF we don't have a search text, then go find the first
- * regular expression among the tests.
+ * Emit the matched subexpression numbered 'c'.
+ * IF, of course, there was such a match...
*/
- if (pz_pat == (tCC*)NULL) {
- tTestDesc* pTD = p_fixd->p_test_desc;
- int ct = p_fixd->test_ct;
- for (;;) {
- if (ct-- <= 0) {
- fprintf( stderr, zBad, p_fixd->fix_name, "search-text" );
- exit( 3 );
+ else {
+ regmatch_t* pRM = av + (c - (unsigned)'0');
+ size_t len;
+
+ if (pRM->rm_so < 0)
+ continue;
+
+ len = pRM->rm_eo - pRM->rm_so;
+ if (len > 0)
+ fwrite(text + pRM->rm_so, len, 1, stdout);
+ }
+ }
+}
+
+
+/*
+ * Search for multiple copies of a regular expression. Each block
+ * of matched text is replaced with the format string, as described
+ * above in `format_write'.
+ */
+FIX_PROC_HEAD( format_fix )
+{
+ tCC* pz_pat = p_fixd->patch_args[2];
+ tCC* pz_fmt = p_fixd->patch_args[1];
+ regex_t re;
+ regmatch_t rm[10];
+ IGNORE_ARG(filname);
+
+ /*
+ * We must have a format
+ */
+ if (pz_fmt == (tCC*)NULL)
+ {
+ fprintf( stderr, zNeedsArg, p_fixd->fix_name, "replacement format", 0 );
+ exit (EXIT_BROKEN);
+ }
+
+ /*
+ * IF we don't have a search text, then go find the first
+ * regular expression among the tests.
+ */
+ if (pz_pat == (tCC*)NULL)
+ {
+ tTestDesc* pTD = p_fixd->p_test_desc;
+ int ct = p_fixd->test_ct;
+ for (;;)
+ {
+ if (ct-- <= 0)
+ {
+ fprintf( stderr, zNeedsArg, p_fixd->fix_name, "search text", 1 );
+ exit (EXIT_BROKEN);
}
- if (pTD->type == TT_EGREP) {
- pz_pat = pTD->pz_test_text;
- break;
+ if (pTD->type == TT_EGREP)
+ {
+ pz_pat = pTD->pz_test_text;
+ break;
}
- pTD++;
+ pTD++;
}
}
- /*
- * Replace every copy of the text we find
- */
- compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
- while (regexec (&re, text, 10, rm, 0) == 0)
+ /*
+ * Replace every copy of the text we find
+ */
+ compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
+ while (regexec (&re, text, 10, rm, 0) == 0)
{
- char* apz[10];
- int i;
-
- fwrite( text, rm[0].rm_so, 1, stdout );
- format_write( pz_fmt, text, rm );
- text += rm[0].rm_eo;
+ fwrite( text, rm[0].rm_so, 1, stdout );
+ format_write( pz_fmt, text, rm );
+ text += rm[0].rm_eo;
}
- /*
- * Dump out the rest of the file
- */
- fputs (text, stdout);
+ /*
+ * Dump out the rest of the file
+ */
+ fputs (text, stdout);
}
+
/* Scan the input file for all occurrences of text like this:
#define TIOCCONS _IO(T, 12)
which is the required syntax per the C standard. (The definition of
_IO also has to be tweaked - see below.) 'IO' is actually whatever you
- provide in the STR argument. */
+ provide as the `c_fix_arg' argument. */
FIX_PROC_HEAD( char_macro_use_fix )
{
"^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
static regex_t re;
+ const char* str = p_fixd->patch_args[1];
regmatch_t rm[1];
const char *p, *limit;
- const char *str = p_fixd->patch_args[0];
size_t len;
+ IGNORE_ARG(filname);
if (str == NULL)
{
- fprintf (stderr, "%s needs macro-name-string argument",
- p_fixd->fix_name);
- exit(3);
+ fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
+ exit (EXIT_BROKEN);
}
len = strlen (str);
- compile_re (pat, &re, 1, "macro pattern", "fix_char_macro_uses");
+ compile_re (pat, &re, 1, "macro pattern", "char_macro_use_fix");
for (p = text;
regexec (&re, p, 1, rm, 0) == 0;
/* Found STR on this line. If the macro needs fixing,
the next few chars will be whitespace or uppercase,
then an open paren, then a single letter. */
- while ((isspace (*p) || isupper (*p)) && p < limit) p++;
+ while ((ISSPACE (*p) || ISUPPER (*p)) && p < limit) p++;
if (*p++ != '(')
continue;
- if (!isalpha (*p))
+ if (!ISALPHA (*p))
continue;
- if (isalnum (p[1]) || p[1] == '_')
+ if (ISIDNUM (p[1]))
continue;
/* Splat all preceding text into the output buffer,
fputs (text, stdout);
}
+
/* Scan the input file for all occurrences of text like this:
- #define _IO(x, y) ('x'<<16+y)
+ #define xxxIOxx(x, y) (....'x'<<16....)
and change them to read like this:
- #define _IO(x, y) (x<<16+y)
+ #define xxxIOxx(x, y) (....x<<16....)
which is the required syntax per the C standard. (The uses of _IO
- also have to be tweaked - see above.) 'IO' is actually whatever
- you provide in the STR argument. */
+ also has to be tweaked - see above.) 'IO' is actually whatever
+ you provide as the `c_fix_arg' argument. */
FIX_PROC_HEAD( char_macro_def_fix )
{
/* This regexp looks for any traditional-syntax #define (# in column 1). */
"^#[ \t]*define[ \t]+";
static regex_t re;
+ const char* str = p_fixd->patch_args[1];
regmatch_t rm[1];
const char *p, *limit;
- const char *str = p_fixd->patch_args[0];
- size_t len;
char arg;
+ size_t len;
+ IGNORE_ARG(filname);
if (str == NULL)
{
- fprintf (stderr, "%s needs macro-name-string argument",
- p_fixd->fix_name);
- exit(3);
+ fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
+ exit (EXIT_BROKEN);
}
+ len = strlen (str);
compile_re (pat, &re, 1, "macro pattern", "fix_char_macro_defines");
for (p = text;
goto found;
p++;
}
- while (isalpha (*p) || isalnum (*p) || *p == '_');
+ while (ISIDNUM (*p));
/* Hit end of macro name without finding the string. */
continue;
there may be a few uppercase letters, then there will be an
open paren with _no_ intervening whitespace, and then a
single letter. */
- while (isupper (*p) && p < limit) p++;
+ while (ISUPPER (*p) && p < limit) p++;
if (*p++ != '(')
continue;
- if (!isalpha (*p))
+ if (!ISALPHA (*p))
continue;
- if (isalnum (p[1]) || p[1] == '_')
+ if (ISIDNUM (p[1]))
continue;
/* The character at P is the one to look for in the following
regex_t *label_re, *name_re;
char scratch[SCRATCHSZ];
size_t len;
+ IGNORE_ARG(filname);
+ IGNORE_ARG(p_fixd);
mn_get_regexps (&label_re, &name_re, "machine_name_fix");
{
base += match[0].rm_eo;
/* We're looking at an #if or #ifdef. Scan forward for the
- next non-escaped newline. */
+ next non-escaped newline. */
line = limit = base;
do
- {
- limit++;
- limit = strchr (limit, '\n');
- if (!limit)
- goto done;
- }
+ {
+ limit++;
+ limit = strchr (limit, '\n');
+ if (!limit)
+ goto done;
+ }
while (limit[-1] == '\\');
/* If the 'name_pat' matches in between base and limit, we have
- a bogon. It is not worth the hassle of excluding comments
- because comments on #if/#ifdef lines are rare, and strings on
- such lines are illegal.
+ a bogon. It is not worth the hassle of excluding comments
+ because comments on #if/#ifdef lines are rare, and strings on
+ such lines are illegal.
- REG_NOTBOL means 'base' is not at the beginning of a line, which
- shouldn't matter since the name_re has no ^ anchor, but let's
- be accurate anyway. */
+ REG_NOTBOL means 'base' is not at the beginning of a line, which
+ shouldn't matter since the name_re has no ^ anchor, but let's
+ be accurate anyway. */
for (;;)
- {
- again:
- if (base == limit)
- break;
+ {
+ again:
+ if (base == limit)
+ break;
- if (regexec (name_re, base, 1, match, REG_NOTBOL))
- goto done; /* No remaining match in this file */
+ if (regexec (name_re, base, 1, match, REG_NOTBOL))
+ goto done; /* No remaining match in this file */
- /* Match; is it on the line? */
- if (match[0].rm_eo > limit - base)
- break;
+ /* Match; is it on the line? */
+ if (match[0].rm_eo > limit - base)
+ break;
- p = base + match[0].rm_so;
- base += match[0].rm_eo;
+ p = base + match[0].rm_so;
+ base += match[0].rm_eo;
- /* One more test: if on the same line we have the same string
- with the appropriate underscores, then leave it alone.
- We want exactly two leading and trailing underscores. */
- if (*p == '_')
- {
- len = base - p - ((*base == '_') ? 2 : 1);
- q = p + 1;
- }
- else
- {
- len = base - p - ((*base == '_') ? 1 : 0);
- q = p;
- }
- if (len + 4 > SCRATCHSZ)
- abort ();
- memcpy (&scratch[2], q, len);
- len += 2;
- scratch[len++] = '_';
- scratch[len++] = '_';
-
- for (q = line; q <= limit - len; q++)
- if (*q == '_' && !strncmp (q, scratch, len))
- goto again;
-
- fwrite (text, 1, p - text, stdout);
- fwrite (scratch, 1, len, stdout);
-
- text = base;
- }
+ /* One more test: if on the same line we have the same string
+ with the appropriate underscores, then leave it alone.
+ We want exactly two leading and trailing underscores. */
+ if (*p == '_')
+ {
+ len = base - p - ((*base == '_') ? 2 : 1);
+ q = p + 1;
+ }
+ else
+ {
+ len = base - p - ((*base == '_') ? 1 : 0);
+ q = p;
+ }
+ if (len + 4 > SCRATCHSZ)
+ abort ();
+ memcpy (&scratch[2], q, len);
+ len += 2;
+ scratch[len++] = '_';
+ scratch[len++] = '_';
+
+ for (q = line; q <= limit - len; q++)
+ if (*q == '_' && !strncmp (q, scratch, len))
+ goto again;
+
+ fwrite (text, 1, p - text, stdout);
+ fwrite (scratch, 1, len, stdout);
+
+ text = base;
+ }
}
done:
#endif
}
+FIX_PROC_HEAD( wrap_fix )
+{
+ tSCC z_no_wrap_pat[] = "^#if.*__need_";
+ static regex_t no_wrapping_re; /* assume zeroed data */
+
+ char z_fixname[ 64 ];
+ tCC* pz_src = p_fixd->fix_name;
+ tCC* pz_name = z_fixname;
+ char* pz_dst = z_fixname;
+ int do_end = 0;
+ size_t len = 0;
+ IGNORE_ARG(filname);
+
+ if (no_wrapping_re.allocated == 0)
+ compile_re( z_no_wrap_pat, &no_wrapping_re, 0, "no-wrap pattern",
+ "wrap-fix" );
+
+ for (;;) {
+ char ch = *pz_src++;
+
+ if (ch == NUL) {
+ *pz_dst++ = ch;
+ break;
+ } else if (! ISALNUM (ch)) {
+ *pz_dst++ = '_';
+ } else {
+ *pz_dst++ = TOUPPER (ch);
+ }
+
+ if (++len >= sizeof( z_fixname )) {
+ void* p = xmalloc( len + strlen( pz_src ) + 1 );
+ memcpy( p, (void*)z_fixname, len );
+ pz_name = (tCC*)p;
+ pz_dst = (char*)pz_name + len;
+ }
+ }
+
+ /*
+ * IF we do *not* match the no-wrap re, then we have a double negative.
+ * A double negative means YES.
+ */
+ if (regexec (&no_wrapping_re, text, 0, NULL, 0) != 0)
+ {
+ printf( "#ifndef FIXINC_%s_CHECK\n", pz_name );
+ printf( "#define FIXINC_%s_CHECK 1\n\n", pz_name );
+ do_end = 1;
+ }
+
+ if (p_fixd->patch_args[1] == (tCC*)NULL)
+ fputs( text, stdout );
+
+ else {
+ fputs( p_fixd->patch_args[1], stdout );
+ fputs( text, stdout );
+ if (p_fixd->patch_args[2] != (tCC*)NULL)
+ fputs( p_fixd->patch_args[2], stdout );
+ }
+
+ if (do_end != 0)
+ printf( "\n#endif /* FIXINC_%s_CHECK */\n", pz_name );
+
+ if (pz_name != z_fixname)
+ free( (void*)pz_name );
+}
+
+
+/*
+ * Search for multiple copies of a regular expression. Each block
+ * of matched text is replaced with the format string, as described
+ * above in `format_write'.
+ */
+FIX_PROC_HEAD( gnu_type_fix )
+{
+ const char* pz_pat;
+ regex_t re;
+ regmatch_t rm[GTYPE_SE_CT+1];
+ IGNORE_ARG(filname);
+
+ {
+ tTestDesc* pTD = p_fixd->p_test_desc;
+ int ct = p_fixd->test_ct;
+ for (;;)
+ {
+ if (ct-- <= 0)
+ {
+ fprintf (stderr, zNeedsArg, p_fixd->fix_name, "search text", 1);
+ exit (EXIT_BROKEN);
+ }
+
+ if (pTD->type == TT_EGREP)
+ {
+ pz_pat = pTD->pz_test_text;
+ break;
+ }
+
+ pTD++;
+ }
+ }
+
+ compile_re (pz_pat, &re, 1, "gnu type typedef", "gnu_type_fix");
+
+ while (regexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
+ {
+ text = emit_gnu_type (text, rm);
+ }
+
+ /*
+ * Dump out the rest of the file
+ */
+ fputs (text, stdout);
+}
+
+
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
test for fix selector
#define _FT_(n,p) { n, p },
static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
#undef _FT_
-#define FIX_TABLE_CT ((sizeof(fix_table)/sizeof(fix_table[0]))-1)
+#define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
tCC* fixname = p_fixd->patch_args[0];
char* buf;
break;
if (--ct <= 0)
{
- fprintf (stderr, "fixincludes error: the `%s' fix is unknown\n",
+ fprintf (stderr, "fixincl error: the `%s' fix is unknown\n",
fixname );
- exit (3);
+ exit (EXIT_BROKEN);
}
pfe++;
}
buf = load_file_data (stdin);
(*pfe->fix_proc)( filname, buf, p_fixd );
}
+
+#ifdef SEPARATE_FIX_PROC
+tSCC z_usage[] =
+"USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
+tSCC z_reopen[] =
+"FS error %d (%s) reopening %s as std%s\n";
+
+int
+main( argc, argv )
+ int argc;
+ char** argv;
+{
+ tFixDesc* pFix;
+ char* pz_tmptmp;
+ char* pz_tmp_base;
+ char* pz_tmp_dot;
+
+ if (argc != 5)
+ {
+ usage_failure:
+ fputs (z_usage, stderr);
+ return EXIT_FAILURE;
+ }
+
+ {
+ char* pz = argv[1];
+ long idx;
+
+ if (! ISDIGIT ( *pz ))
+ goto usage_failure;
+
+ idx = strtol (pz, &pz, 10);
+ if ((*pz != NUL) || ((unsigned)idx >= FIX_COUNT))
+ goto usage_failure;
+ pFix = fixDescList + idx;
+ }
+
+ if (freopen (argv[3], "r", stdin) != stdin)
+ {
+ fprintf (stderr, z_reopen, errno, strerror( errno ), argv[3], "in");
+ return EXIT_FAILURE;
+ }
+
+ pz_tmptmp = (char*)xmalloc( strlen( argv[4] ) + 5 );
+ strcpy( pz_tmptmp, argv[4] );
+
+ /* Don't lose because "12345678" and "12345678X" map to the same
+ file under DOS restricted 8+3 file namespace. Note that DOS
+ doesn't allow more than one dot in the trunk of a file name. */
+ pz_tmp_base = basename( pz_tmptmp );
+ pz_tmp_dot = strchr( pz_tmp_base, '.' );
+ if (pathconf( pz_tmptmp, _PC_NAME_MAX ) <= 12 /* is this DOS or Windows9X? */
+ && pz_tmp_dot != (char*)NULL)
+ strcpy (pz_tmp_dot+1, "X"); /* nuke the original extension */
+ else
+ strcat (pz_tmptmp, ".X");
+ if (freopen (pz_tmptmp, "w", stdout) != stdout)
+ {
+ fprintf (stderr, z_reopen, errno, strerror( errno ), pz_tmptmp, "out");
+ return EXIT_FAILURE;
+ }
+
+ apply_fix (pFix, argv[1]);
+ fclose (stdout);
+ fclose (stdin);
+ unlink (argv[4]);
+ if (rename (pz_tmptmp, argv[4]) != 0)
+ {
+ fprintf (stderr, "error %d (%s) renaming %s to %s\n", errno,
+ strerror( errno ), pz_tmptmp, argv[4]);
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}
+#endif