| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Patch fontconfig src/fcint.h and src/fccompat.c for Solaris without xlocale.""" |
| 3 |
import sys, os |
| 4 |
|
| 5 |
fcint = sys.argv[1] |
| 6 |
fccompat = sys.argv[2] |
| 7 |
|
| 8 |
# ---- Patch fcint.h ---- |
| 9 |
with open(fcint) as f: |
| 10 |
content = f.read() |
| 11 |
|
| 12 |
solaris_block = """\ |
| 13 |
#elif defined(__sun) |
| 14 |
typedef int FcLocale; |
| 15 |
typedef enum _FcLocaleMask { |
| 16 |
FC_LC_CTYPE = 0x01, |
| 17 |
FC_LC_NUMERIC = 0x02, |
| 18 |
FC_LC_TIME = 0x04, |
| 19 |
FC_LC_COLLATE = 0x08, |
| 20 |
FC_LC_MONETARY = 0x10, |
| 21 |
FC_LC_MESSAGES = 0x20, |
| 22 |
FC_LC_PAPER = 0x80, |
| 23 |
FC_LC_NAME = 0x80, |
| 24 |
FC_LC_ADDRESS = 0x80, |
| 25 |
FC_LC_TELEPHONE = 0x80, |
| 26 |
FC_LC_MEASUREMENT = 0x80, |
| 27 |
FC_LC_IDENTIFICATION = 0x80, |
| 28 |
FC_LC_ALL = 0xff, |
| 29 |
} FcLocaleMask; |
| 30 |
""" |
| 31 |
|
| 32 |
# Insert Solaris block before the #else that starts the locale_t section |
| 33 |
target = '} FcLocaleMask;\n#else\ntypedef locale_t FcLocale;' |
| 34 |
if target in content: |
| 35 |
content = content.replace(target, |
| 36 |
'} FcLocaleMask;\n' + solaris_block + '#else\ntypedef locale_t FcLocale;', 1) |
| 37 |
with open(fcint, 'w') as f: |
| 38 |
f.write(content) |
| 39 |
print("fcint.h patched OK") |
| 40 |
else: |
| 41 |
print("fcint.h: target not found, dumping nearby lines:") |
| 42 |
for i, line in enumerate(content.splitlines()): |
| 43 |
if 'FcLocaleMask' in line or 'locale_t' in line: |
| 44 |
print(f" {i+1}: {repr(line)}") |
| 45 |
|
| 46 |
# ---- Patch fccompat.c ---- |
| 47 |
with open(fccompat) as f: |
| 48 |
content = f.read() |
| 49 |
|
| 50 |
# Patch FcLocaleCreate: replace #else...newlocale with #elif sun stub + #else |
| 51 |
old = ('#else\n' |
| 52 |
' return newlocale (mask, locale, (locale_t)0);\n') |
| 53 |
new = ('#elif defined(__sun)\n' |
| 54 |
' return 0; /* no newlocale on this Solaris */\n' |
| 55 |
'#else\n' |
| 56 |
' return newlocale (mask, locale, (locale_t)0);\n') |
| 57 |
if old in content: |
| 58 |
content = content.replace(old, new, 1) |
| 59 |
print("fccompat.c FcLocaleCreate patched OK") |
| 60 |
else: |
| 61 |
print("fccompat.c: FcLocaleCreate pattern not found") |
| 62 |
|
| 63 |
# Patch FcLocaleSetCurrent: provide stub for Solaris instead of hiding it |
| 64 |
old = '#ifndef _WIN32\nFcLocale\nFcLocaleSetCurrent (FcLocale loc)\n{\n return uselocale (loc);\n}\n#endif' |
| 65 |
new = ('#ifndef _WIN32\n' |
| 66 |
'FcLocale\n' |
| 67 |
'FcLocaleSetCurrent (FcLocale loc)\n' |
| 68 |
'{\n' |
| 69 |
'# ifdef __sun\n' |
| 70 |
' return 0; /* no uselocale on this Solaris */\n' |
| 71 |
'# else\n' |
| 72 |
' return uselocale (loc);\n' |
| 73 |
'# endif\n' |
| 74 |
'}\n' |
| 75 |
'#endif') |
| 76 |
if old in content: |
| 77 |
content = content.replace(old, new, 1) |
| 78 |
print("fccompat.c FcLocaleSetCurrent patched OK") |
| 79 |
else: |
| 80 |
print("fccompat.c: FcLocaleSetCurrent pattern not found") |
| 81 |
|
| 82 |
# Patch FcLocaleDestroy: add Solaris stub |
| 83 |
old = '#else\n freelocale (locale);\n#endif' |
| 84 |
new = '#elif !defined(__sun)\n freelocale (locale);\n#endif' |
| 85 |
if old in content: |
| 86 |
content = content.replace(old, new, 1) |
| 87 |
print("fccompat.c FcLocaleDestroy patched OK") |
| 88 |
else: |
| 89 |
print("fccompat.c: FcLocaleDestroy pattern not found") |
| 90 |
|
| 91 |
with open(fccompat, 'w') as f: |
| 92 |
f.write(content) |