#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 (const char *, const char *, tFixDesc *) ;
typedef struct {
const char* fix_name;
t_fix_proc* fix_proc;
} fix_entry_t;
#define FIXUP_TABLE \
_FT_( "char_macro_def", char_macro_def_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 (const char* filname ATTRIBUTE_UNUSED , \
const char* text ATTRIBUTE_UNUSED , \
tFixDesc* p_fixd ATTRIBUTE_UNUSED )
#ifdef NEED_PRINT_QUOTE
static char*
print_quote(char q, char* text )
{
fputc( q, stdout );
for (;;)
{
char ch = *(text++);
fputc( ch, stdout );
switch (ch)
{
case '\\':
if (*text == NUL)
goto quote_done;
fputc( *(text++), stdout );
break;
case '"':
case '\'':
if (ch != q)
break;
case '\n':
case NUL:
goto quote_done;
}
} quote_done:;
return text;
}
#endif
static const char*
emit_gnu_type (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;
}
{
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;
}
static void
format_write (tCC* format, tCC* text, regmatch_t av[] )
{
int c;
while ((c = (unsigned)*(format++)) != NUL) {
if (c != '%')
{
putchar(c);
continue;
}
c = (unsigned)*(format++);
if (! ISDIGIT ( c ))
{
putchar( '%' );
switch (c) {
case NUL:
return;
case '%':
break;
default:
putchar(c);
}
}
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);
}
}
}
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);
if (pz_fmt == (tCC*)NULL)
{
fprintf( stderr, zNeedsArg, p_fixd->fix_name, "replacement format", 0 );
exit (EXIT_BROKEN);
}
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;
}
pTD++;
}
}
compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
while (xregexec (&re, text, 10, rm, 0) == 0)
{
fwrite( text, rm[0].rm_so, 1, stdout );
format_write( pz_fmt, text, rm );
text += rm[0].rm_eo;
}
fputs (text, stdout);
}
FIX_PROC_HEAD( char_macro_use_fix )
{
static const char pat[] =
"^#[ \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;
size_t len;
IGNORE_ARG(filname);
if (str == NULL)
{
fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
exit (EXIT_BROKEN);
}
len = strlen (str);
compile_re (pat, &re, 1, "macro pattern", "char_macro_use_fix");
for (p = text;
xregexec (&re, p, 1, rm, 0) == 0;
p = limit + 1)
{
p += rm[0].rm_eo;
limit = p - 1;
do
{
limit = strchr (limit + 1, '\n');
if (!limit)
goto done;
}
while (limit[-1] == '\\');
do
{
if (*p == str[0] && !strncmp (p+1, str+1, len-1))
goto found;
}
while (++p < limit - len);
continue;
found:
while ((ISSPACE (*p) || ISUPPER (*p)) && p < limit) p++;
if (*p++ != '(')
continue;
if (!ISALPHA (*p))
continue;
if (ISIDNUM (p[1]))
continue;
fwrite (text, 1, p - text, stdout);
putchar ('\'');
putchar (*p);
putchar ('\'');
text = p + 1;
}
done:
fputs (text, stdout);
}
FIX_PROC_HEAD( char_macro_def_fix )
{
static const char pat[] =
"^#[ \t]*define[ \t]+";
static regex_t re;
const char* str = p_fixd->patch_args[1];
regmatch_t rm[1];
const char *p, *limit;
char arg;
size_t len;
IGNORE_ARG(filname);
if (str == NULL)
{
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;
xregexec (&re, p, 1, rm, 0) == 0;
p = limit + 1)
{
p += rm[0].rm_eo;
limit = p - 1;
do
{
limit = strchr (limit + 1, '\n');
if (!limit)
goto done;
}
while (limit[-1] == '\\');
do
{
if (*p == str[0] && !strncmp (p+1, str+1, len-1))
goto found;
p++;
}
while (ISIDNUM (*p));
continue;
found:
while (ISUPPER (*p) && p < limit) p++;
if (*p++ != '(')
continue;
if (!ISALPHA (*p))
continue;
if (ISIDNUM (p[1]))
continue;
arg = *p;
p += 2;
while (p < limit)
{
if (p[-1] == '\'' && p[0] == arg && p[1] == '\'')
{
p--;
fwrite (text, 1, p - text, stdout);
putchar (arg);
p += 3;
text = p;
}
else
p++;
}
}
done:
fputs (text, stdout);
}
#define SCRATCHSZ 64
FIX_PROC_HEAD( machine_name_fix )
{
regmatch_t match[2];
const char *line, *base, *limit, *p, *q;
regex_t *label_re, *name_re;
char scratch[SCRATCHSZ];
size_t len;
IGNORE_ARG(filname);
IGNORE_ARG(p_fixd);
if (!mn_get_regexps (&label_re, &name_re, "machine_name_fix"))
{
fputs( "The target machine has no needed machine name fixes\n", stderr );
goto done;
}
scratch[0] = '_';
scratch[1] = '_';
for (base = text;
xregexec (label_re, base, 2, match, 0) == 0;
base = limit)
{
base += match[0].rm_eo;
line = limit = base;
do
{
limit++;
limit = strchr (limit, '\n');
if (!limit)
goto done;
}
while (limit[-1] == '\\');
for (;;)
{
again:
if (base == limit)
break;
if (xregexec (name_re, base, 1, match, REG_NOTBOL))
goto done;
if (match[0].rm_eo > limit - base)
break;
p = base + match[0].rm_so;
base += match[0].rm_eo;
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:
fputs (text, stdout);
}
FIX_PROC_HEAD( wrap_fix )
{
tSCC z_no_wrap_pat[] = "^#if.*__need_";
static regex_t no_wrapping_re;
tCC* pz_name = NULL;
if (no_wrapping_re.allocated == 0)
compile_re( z_no_wrap_pat, &no_wrapping_re, 0, "no-wrap pattern",
"wrap-fix" );
if (xregexec( &no_wrapping_re, text, 0, NULL, 0 ) != 0)
{
size_t ln = strlen( filname ) + strlen( p_fixd->fix_name ) + 14;
char* pz = XNEWVEC (char, ln);
pz_name = pz;
sprintf( pz, "FIXINC_WRAP_%s-%s", filname, p_fixd->fix_name );
for (pz += 12; 1; pz++) {
char ch = *pz;
if (ch == NUL)
break;
if (! ISALNUM( ch )) {
*pz = '_';
}
else {
*pz = TOUPPER( ch );
}
}
printf( "#ifndef %s\n", pz_name );
printf( "#define %s 1\n\n", pz_name );
}
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 (pz_name != NULL) {
printf( "\n#endif /* %s */\n", pz_name );
free( (void*)pz_name );
}
}
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 (xregexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
{
text = emit_gnu_type (text, rm);
}
fputs (text, stdout);
}
void
apply_fix( tFixDesc* p_fixd, tCC* filname )
{
#define _FT_(n,p) { n, p },
static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
#undef _FT_
#define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
tCC* fixname = p_fixd->patch_args[0];
char* buf;
int ct = FIX_TABLE_CT;
fix_entry_t* pfe = fix_table;
for (;;)
{
if (strcmp (pfe->fix_name, fixname) == 0)
break;
if (--ct <= 0)
{
fprintf (stderr, "fixincl error: the `%s' fix is unknown\n",
fixname );
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( 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;
}
initialize_opts ();
{
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 = XNEWVEC (char, strlen (argv[4]) + 5);
strcpy( pz_tmptmp, argv[4] );
pz_tmp_base = basename( pz_tmptmp );
pz_tmp_dot = strchr( pz_tmp_base, '.' );
#ifdef _PC_NAME_MAX
if (pathconf( pz_tmptmp, _PC_NAME_MAX ) <= 12
&& pz_tmp_dot != (char*)NULL)
strcpy (pz_tmp_dot+1, "X");
else
#endif
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