2024-03-25 20:17:59 +01:00
|
|
|
#!@PYTHON@
|
2001-04-29 05:04:27 +02:00
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
# If the code below looks horrible and unpythonic, do not panic.
|
|
|
|
#
|
|
|
|
# It is.
|
|
|
|
#
|
|
|
|
# This is a manual conversion from the original Perl script to
|
|
|
|
# Python. Improvements are welcome.
|
2017-07-15 15:21:11 +02:00
|
|
|
#
|
|
|
|
from __future__ import print_function, unicode_literals
|
2009-06-29 15:28:22 +02:00
|
|
|
|
2017-07-15 15:22:20 +02:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import tempfile
|
2017-07-19 11:43:47 +02:00
|
|
|
import io
|
2017-07-19 11:49:04 +02:00
|
|
|
import errno
|
2017-07-26 11:26:00 +02:00
|
|
|
import codecs
|
|
|
|
import locale
|
2017-03-21 17:42:19 +01:00
|
|
|
|
2022-08-13 21:56:20 +02:00
|
|
|
# Non-english locale systems might complain to unrecognized character
|
|
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')
|
|
|
|
|
2017-07-17 11:54:28 +02:00
|
|
|
VERSION_STR = '''glib-mkenums version @VERSION@
|
2017-10-17 15:02:27 +02:00
|
|
|
glib-mkenums comes with ABSOLUTELY NO WARRANTY.
|
|
|
|
You may redistribute copies of glib-mkenums under the terms of
|
2017-07-17 11:54:28 +02:00
|
|
|
the GNU General Public License which can be found in the
|
|
|
|
GLib source package. Sources, examples and contact
|
|
|
|
information are available at http://www.gtk.org'''
|
|
|
|
|
2017-07-17 11:32:33 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class Color:
|
|
|
|
'''ANSI Terminal colors'''
|
|
|
|
GREEN = '\033[1;32m'
|
|
|
|
BLUE = '\033[1;34m'
|
|
|
|
YELLOW = '\033[1;33m'
|
|
|
|
RED = '\033[1;31m'
|
|
|
|
END = '\033[0m'
|
|
|
|
|
|
|
|
|
|
|
|
def print_color(msg, color=Color.END, prefix='MESSAGE'):
|
|
|
|
'''Print a string with a color prefix'''
|
|
|
|
if os.isatty(sys.stderr.fileno()):
|
|
|
|
real_prefix = '{start}{prefix}{end}'.format(start=color, prefix=prefix, end=Color.END)
|
|
|
|
else:
|
|
|
|
real_prefix = prefix
|
|
|
|
print('{prefix}: {msg}'.format(prefix=real_prefix, msg=msg), file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
def print_error(msg):
|
|
|
|
'''Print an error, and terminate'''
|
|
|
|
print_color(msg, color=Color.RED, prefix='ERROR')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def print_warning(msg, fatal=False):
|
|
|
|
'''Print a warning, and optionally terminate'''
|
|
|
|
if fatal:
|
|
|
|
color = Color.RED
|
|
|
|
prefix = 'ERROR'
|
|
|
|
else:
|
|
|
|
color = Color.YELLOW
|
|
|
|
prefix = 'WARNING'
|
|
|
|
print_color(msg, color, prefix)
|
|
|
|
if fatal:
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def print_info(msg):
|
|
|
|
'''Print a message'''
|
|
|
|
print_color(msg, color=Color.GREEN, prefix='INFO')
|
|
|
|
|
|
|
|
|
2018-11-20 09:55:23 +01:00
|
|
|
def get_rspfile_args(rspfile):
|
|
|
|
'''
|
|
|
|
Response files are useful on Windows where there is a command-line character
|
|
|
|
limit of 8191 because when passing sources as arguments to glib-mkenums this
|
|
|
|
limit can be exceeded in large codebases.
|
|
|
|
|
|
|
|
There is no specification for response files and each tool that supports it
|
|
|
|
generally writes them out in slightly different ways, but some sources are:
|
|
|
|
https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-response-files
|
|
|
|
https://docs.microsoft.com/en-us/windows/desktop/midl/the-response-file-command
|
|
|
|
'''
|
|
|
|
import shlex
|
|
|
|
if not os.path.isfile(rspfile):
|
|
|
|
sys.exit('Response file {!r} does not exist'.format(rspfile))
|
|
|
|
try:
|
|
|
|
with open(rspfile, 'r') as f:
|
|
|
|
cmdline = f.read()
|
|
|
|
except OSError as e:
|
|
|
|
sys.exit('Response file {!r} could not be read: {}'
|
|
|
|
.format(rspfile, e.strerror))
|
|
|
|
return shlex.split(cmdline)
|
|
|
|
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
def write_output(output):
|
|
|
|
global output_stream
|
|
|
|
print(output, file=output_stream)
|
|
|
|
|
2017-10-23 14:59:58 +02:00
|
|
|
|
|
|
|
# Python 2 defaults to ASCII in case stdout is redirected.
|
|
|
|
# This should make it match Python 3, which uses the locale encoding.
|
|
|
|
if sys.stdout.encoding is None:
|
|
|
|
output_stream = codecs.getwriter(
|
|
|
|
locale.getpreferredencoding())(sys.stdout)
|
|
|
|
else:
|
|
|
|
output_stream = sys.stdout
|
|
|
|
|
|
|
|
|
|
|
|
# Some source files aren't UTF-8 and the old perl version didn't care.
|
|
|
|
# Replace invalid data with a replacement character to keep things working.
|
|
|
|
# https://bugzilla.gnome.org/show_bug.cgi?id=785113#c20
|
|
|
|
def replace_and_warn(err):
|
|
|
|
# 7 characters of context either side of the offending character
|
|
|
|
print_warning('UnicodeWarning: {} at {} ({})'.format(
|
|
|
|
err.reason, err.start,
|
|
|
|
err.object[err.start - 7:err.end + 7]))
|
|
|
|
return ('?', err.end)
|
|
|
|
|
|
|
|
codecs.register_error('replace_and_warn', replace_and_warn)
|
|
|
|
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
# glib-mkenums.py
|
2001-04-29 05:04:27 +02:00
|
|
|
# Information about the current enumeration
|
2017-08-01 11:11:09 +02:00
|
|
|
flags = None # Is enumeration a bitmask?
|
2018-03-21 16:03:14 +01:00
|
|
|
option_underscore_name = '' # Overridden underscore variant of the enum name
|
2017-03-21 17:42:19 +01:00
|
|
|
# for example to fix the cases we don't get the
|
|
|
|
# mixed-case -> underscorized transform right.
|
|
|
|
option_lowercase_name = '' # DEPRECATED. A lower case name to use as part
|
|
|
|
# of the *_get_type() function, instead of the
|
|
|
|
# one that we guess. For instance, when an enum
|
|
|
|
# uses abnormal capitalization and we can not
|
|
|
|
# guess where to put the underscores.
|
2020-05-12 21:56:02 +02:00
|
|
|
option_since = '' # User provided version info for the enum.
|
2021-01-10 18:59:45 +01:00
|
|
|
seenbitshift = 0 # Have we seen bitshift operators?
|
2021-01-10 19:41:07 +01:00
|
|
|
seenprivate = False # Have we seen a private option?
|
2021-01-10 18:59:45 +01:00
|
|
|
enum_prefix = None # Prefix for this enumeration
|
|
|
|
enumname = '' # Name for this enumeration
|
|
|
|
enumshort = '' # $enumname without prefix
|
|
|
|
enumname_prefix = '' # prefix of $enumname
|
|
|
|
enumindex = 0 # Global enum counter
|
|
|
|
firstenum = 1 # Is this the first enumeration per file?
|
|
|
|
entries = [] # [ name, val ] for each entry
|
2022-11-01 02:12:26 +01:00
|
|
|
c_namespace = {} # C symbols namespace.
|
2021-01-10 18:59:45 +01:00
|
|
|
|
|
|
|
output = '' # Filename to write result into
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
def parse_trigraph(opts):
|
|
|
|
result = {}
|
2023-05-26 11:07:09 +02:00
|
|
|
for opt in re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', opts):
|
2017-03-21 17:42:19 +01:00
|
|
|
opt = re.sub(r'^\s*', '', opt)
|
|
|
|
opt = re.sub(r'\s*$', '', opt)
|
|
|
|
m = re.search(r'(\w+)(?:=(.+))?', opt)
|
2017-07-15 15:22:20 +02:00
|
|
|
assert m is not None
|
2017-03-21 17:42:19 +01:00
|
|
|
groups = m.groups()
|
|
|
|
key = groups[0]
|
|
|
|
if len(groups) > 1:
|
|
|
|
val = groups[1]
|
|
|
|
else:
|
|
|
|
val = 1
|
2023-05-26 11:07:09 +02:00
|
|
|
result[key] = val.strip('"') if val is not None else None
|
2017-03-21 17:42:19 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
def parse_entries(file, file_name):
|
2021-01-10 19:41:07 +01:00
|
|
|
global entries, enumindex, enumname, seenbitshift, seenprivate, flags
|
2017-03-21 17:42:19 +01:00
|
|
|
looking_for_name = False
|
|
|
|
|
2017-07-17 17:29:40 +02:00
|
|
|
while True:
|
|
|
|
line = file.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
|
|
|
|
line = line.strip()
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
# read lines until we have no open comments
|
|
|
|
while re.search(r'/\*([^*]|\*(?!/))*$', line):
|
2017-05-28 22:29:20 +02:00
|
|
|
line += file.readline()
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
# strip comments w/o options
|
|
|
|
line = re.sub(r'''/\*(?!<)
|
|
|
|
([^*]+|\*(?!/))*
|
|
|
|
\*/''', '', line, flags=re.X)
|
|
|
|
|
|
|
|
line = line.rstrip()
|
|
|
|
|
|
|
|
# skip empty lines
|
|
|
|
if len(line.strip()) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if looking_for_name:
|
2017-07-15 15:26:54 +02:00
|
|
|
m = re.match(r'\s*(\w+)', line)
|
2017-03-21 17:42:19 +01:00
|
|
|
if m:
|
2017-07-15 15:22:20 +02:00
|
|
|
enumname = m.group(1)
|
2017-03-21 17:42:19 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
# Handle include files
|
|
|
|
m = re.match(r'\#include\s*<([^>]*)>', line)
|
|
|
|
if m:
|
|
|
|
newfilename = os.path.join("..", m.group(1))
|
2017-10-23 14:59:58 +02:00
|
|
|
newfile = io.open(newfilename, encoding="utf-8",
|
|
|
|
errors="replace_and_warn")
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
if not parse_entries(newfile, newfilename):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
m = re.match(r'\s*\}\s*(\w+)', line)
|
|
|
|
if m:
|
|
|
|
enumname = m.group(1)
|
2017-07-15 15:22:20 +02:00
|
|
|
enumindex += 1
|
|
|
|
return 1
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
m = re.match(r'\s*\}', line)
|
|
|
|
if m:
|
2017-07-15 15:22:20 +02:00
|
|
|
enumindex += 1
|
2017-03-21 17:42:19 +01:00
|
|
|
looking_for_name = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
m = re.match(r'''\s*
|
2001-04-29 05:04:27 +02:00
|
|
|
(\w+)\s* # name
|
2021-03-10 20:48:22 +01:00
|
|
|
(\s+[A-Z]+_(?:AVAILABLE|DEPRECATED)_ENUMERATOR_IN_[0-9_]+(?:_FOR\s*\(\s*\w+\s*\))?\s*)? # availability
|
2001-04-29 05:04:27 +02:00
|
|
|
(?:=( # value
|
2023-11-21 12:54:42 +01:00
|
|
|
\s*'[^']*'\s* # char
|
|
|
|
| # OR
|
2017-03-21 17:42:19 +01:00
|
|
|
\s*\w+\s*\(.*\)\s* # macro with multiple args
|
|
|
|
| # OR
|
2004-02-27 01:49:56 +01:00
|
|
|
(?:[^,/]|/(?!\*))* # anything but a comma or comment
|
2001-04-29 05:04:27 +02:00
|
|
|
))?,?\s*
|
|
|
|
(?:/\*< # options
|
|
|
|
(([^*]|\*(?!/))*)
|
|
|
|
>\s*\*/)?,?
|
2017-03-21 17:42:19 +01:00
|
|
|
\s*$''', line, flags=re.X)
|
|
|
|
if m:
|
|
|
|
groups = m.groups()
|
|
|
|
name = groups[0]
|
2021-03-10 20:48:22 +01:00
|
|
|
availability = None
|
2017-03-21 17:42:19 +01:00
|
|
|
value = None
|
|
|
|
options = None
|
|
|
|
if len(groups) > 1:
|
2021-03-10 20:48:22 +01:00
|
|
|
availability = groups[1]
|
2017-03-21 17:42:19 +01:00
|
|
|
if len(groups) > 2:
|
2021-03-10 20:48:22 +01:00
|
|
|
value = groups[2]
|
|
|
|
if len(groups) > 3:
|
|
|
|
options = groups[3]
|
2017-08-01 11:11:09 +02:00
|
|
|
if flags is None and value is not None and '<<' in value:
|
2017-03-21 17:42:19 +01:00
|
|
|
seenbitshift = 1
|
|
|
|
|
|
|
|
if options is not None:
|
|
|
|
options = parse_trigraph(options)
|
2017-06-27 00:50:01 +02:00
|
|
|
if 'skip' not in options:
|
2022-11-01 15:46:13 +01:00
|
|
|
entries.append((name, value, seenprivate, options.get('nick')))
|
2017-03-21 17:42:19 +01:00
|
|
|
else:
|
2022-11-01 15:46:13 +01:00
|
|
|
entries.append((name, value, seenprivate))
|
2017-03-21 17:42:19 +01:00
|
|
|
else:
|
2021-01-10 19:41:07 +01:00
|
|
|
m = re.match(r'''\s*
|
|
|
|
/\*< (([^*]|\*(?!/))*) >\s*\*/
|
|
|
|
\s*$''', line, flags=re.X)
|
|
|
|
if m:
|
|
|
|
options = m.groups()[0]
|
|
|
|
if options is not None:
|
|
|
|
options = parse_trigraph(options)
|
|
|
|
if 'private' in options:
|
|
|
|
seenprivate = True
|
|
|
|
continue
|
|
|
|
if 'public' in options:
|
|
|
|
seenprivate = False
|
|
|
|
continue
|
|
|
|
if re.match(r's*\#', line):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print_warning('Failed to parse "{}" in {}'.format(line, file_name))
|
2017-03-21 17:42:19 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
help_epilog = '''Production text substitutions:
|
|
|
|
\u0040EnumName\u0040 PrefixTheXEnum
|
|
|
|
\u0040enum_name\u0040 prefix_the_xenum
|
|
|
|
\u0040ENUMNAME\u0040 PREFIX_THE_XENUM
|
|
|
|
\u0040ENUMSHORT\u0040 THE_XENUM
|
|
|
|
\u0040ENUMPREFIX\u0040 PREFIX
|
2020-05-12 21:56:02 +02:00
|
|
|
\u0040enumsince\u0040 the user-provided since value given
|
2017-03-21 17:42:19 +01:00
|
|
|
\u0040VALUENAME\u0040 PREFIX_THE_XVALUE
|
|
|
|
\u0040valuenick\u0040 the-xvalue
|
|
|
|
\u0040valuenum\u0040 the integer value (limited support, Since: 2.26)
|
|
|
|
\u0040type\u0040 either enum or flags
|
|
|
|
\u0040Type\u0040 either Enum or Flags
|
|
|
|
\u0040TYPE\u0040 either ENUM or FLAGS
|
|
|
|
\u0040filename\u0040 name of current input file
|
|
|
|
\u0040basename\u0040 base name of the current input file (Since: 2.22)
|
|
|
|
'''
|
2001-04-29 05:04:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
# production variables:
|
2017-03-21 17:42:19 +01:00
|
|
|
idprefix = "" # "G", "Gtk", etc
|
|
|
|
symprefix = "" # "g", "gtk", etc, if not just lc($idprefix)
|
|
|
|
fhead = "" # output file header
|
|
|
|
fprod = "" # per input file production
|
|
|
|
ftail = "" # output file trailer
|
|
|
|
eprod = "" # per enum text (produced prior to value itarations)
|
|
|
|
vhead = "" # value header, produced before iterating over enum values
|
|
|
|
vprod = "" # value text, produced for each enum value
|
|
|
|
vtail = "" # value tail, produced after iterating over enum values
|
|
|
|
comment_tmpl = "" # comment template
|
|
|
|
|
|
|
|
def read_template_file(file):
|
|
|
|
global idprefix, symprefix, fhead, fprod, ftail, eprod, vhead, vprod, vtail, comment_tmpl
|
|
|
|
tmpl = {'file-header': fhead,
|
|
|
|
'file-production': fprod,
|
|
|
|
'file-tail': ftail,
|
|
|
|
'enumeration-production': eprod,
|
|
|
|
'value-header': vhead,
|
|
|
|
'value-production': vprod,
|
|
|
|
'value-tail': vtail,
|
|
|
|
'comment': comment_tmpl,
|
2017-07-15 15:22:20 +02:00
|
|
|
}
|
2017-03-21 17:42:19 +01:00
|
|
|
in_ = 'junk'
|
|
|
|
|
2017-10-23 14:59:58 +02:00
|
|
|
ifile = io.open(file, encoding="utf-8", errors="replace_and_warn")
|
2017-03-21 17:42:19 +01:00
|
|
|
for line in ifile:
|
|
|
|
m = re.match(r'\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\/', line)
|
|
|
|
if m:
|
|
|
|
if in_ == 'junk' and m.group(1) == 'BEGIN' and m.group(2) in tmpl:
|
|
|
|
in_ = m.group(2)
|
|
|
|
continue
|
|
|
|
elif in_ == m.group(2) and m.group(1) == 'END' and m.group(2) in tmpl:
|
|
|
|
in_ = 'junk'
|
|
|
|
continue
|
|
|
|
else:
|
2017-07-15 15:22:20 +02:00
|
|
|
sys.exit("Malformed template file " + file)
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
if in_ != 'junk':
|
|
|
|
tmpl[in_] += line
|
|
|
|
|
|
|
|
if in_ != 'junk':
|
|
|
|
sys.exit("Malformed template file " + file)
|
|
|
|
|
|
|
|
fhead = tmpl['file-header']
|
|
|
|
fprod = tmpl['file-production']
|
|
|
|
ftail = tmpl['file-tail']
|
|
|
|
eprod = tmpl['enumeration-production']
|
|
|
|
vhead = tmpl['value-header']
|
|
|
|
vprod = tmpl['value-production']
|
|
|
|
vtail = tmpl['value-tail']
|
|
|
|
comment_tmpl = tmpl['comment']
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(epilog=help_epilog,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
|
|
|
|
|
|
parser.add_argument('--identifier-prefix', default='', dest='idprefix',
|
|
|
|
help='Identifier prefix')
|
|
|
|
parser.add_argument('--symbol-prefix', default='', dest='symprefix',
|
2018-08-10 18:05:22 +02:00
|
|
|
help='Symbol prefix')
|
2017-07-16 12:56:52 +02:00
|
|
|
parser.add_argument('--fhead', default=[], dest='fhead', action='append',
|
2017-03-21 17:42:19 +01:00
|
|
|
help='Output file header')
|
2017-07-16 12:56:52 +02:00
|
|
|
parser.add_argument('--ftail', default=[], dest='ftail', action='append',
|
2018-08-10 18:05:22 +02:00
|
|
|
help='Output file footer')
|
2017-07-16 12:56:52 +02:00
|
|
|
parser.add_argument('--fprod', default=[], dest='fprod', action='append',
|
2018-08-10 18:05:22 +02:00
|
|
|
help='Put out TEXT every time a new input file is being processed.')
|
2017-07-16 12:56:52 +02:00
|
|
|
parser.add_argument('--eprod', default=[], dest='eprod', action='append',
|
2018-08-10 18:05:22 +02:00
|
|
|
help='Per enum text, produced prior to value iterations')
|
2017-07-16 12:56:52 +02:00
|
|
|
parser.add_argument('--vhead', default=[], dest='vhead', action='append',
|
2017-03-21 17:42:19 +01:00
|
|
|
help='Value header, produced before iterating over enum values')
|
2017-07-16 12:56:52 +02:00
|
|
|
parser.add_argument('--vprod', default=[], dest='vprod', action='append',
|
2017-03-21 17:42:19 +01:00
|
|
|
help='Value text, produced for each enum value.')
|
2017-07-16 12:56:52 +02:00
|
|
|
parser.add_argument('--vtail', default=[], dest='vtail', action='append',
|
2017-03-21 17:42:19 +01:00
|
|
|
help='Value tail, produced after iterating over enum values')
|
|
|
|
parser.add_argument('--comments', default='', dest='comment_tmpl',
|
|
|
|
help='Comment structure')
|
|
|
|
parser.add_argument('--template', default='', dest='template',
|
|
|
|
help='Template file')
|
|
|
|
parser.add_argument('--output', default=None, dest='output')
|
|
|
|
parser.add_argument('--version', '-v', default=False, action='store_true', dest='version',
|
2018-08-10 18:05:22 +02:00
|
|
|
help='Print version information')
|
|
|
|
parser.add_argument('args', nargs='*',
|
2018-11-20 09:55:23 +01:00
|
|
|
help='One or more input files, or a single argument @rspfile_path '
|
|
|
|
'pointing to a file that contains the actual arguments')
|
|
|
|
|
|
|
|
# Support reading an rspfile of the form @filename which contains the args
|
|
|
|
# to be parsed
|
|
|
|
if len(sys.argv) == 2 and sys.argv[1].startswith('@'):
|
|
|
|
args = get_rspfile_args(sys.argv[1][1:])
|
|
|
|
else:
|
|
|
|
args = sys.argv[1:]
|
2017-03-21 17:42:19 +01:00
|
|
|
|
2018-11-20 09:55:23 +01:00
|
|
|
options = parser.parse_args(args)
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
if options.version:
|
2017-07-17 11:54:28 +02:00
|
|
|
print(VERSION_STR)
|
|
|
|
sys.exit(0)
|
2017-03-21 17:42:19 +01:00
|
|
|
|
2017-07-15 11:17:48 +02:00
|
|
|
def unescape_cmdline_args(arg):
|
|
|
|
arg = arg.replace('\\n', '\n')
|
|
|
|
arg = arg.replace('\\r', '\r')
|
|
|
|
return arg.replace('\\t', '\t')
|
|
|
|
|
2017-07-17 10:36:13 +02:00
|
|
|
if options.template != '':
|
|
|
|
read_template_file(options.template)
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
idprefix += options.idprefix
|
|
|
|
symprefix += options.symprefix
|
2017-07-17 10:36:13 +02:00
|
|
|
|
|
|
|
# This is a hack to maintain some semblance of backward compatibility with
|
|
|
|
# the old, Perl-based glib-mkenums. The old tool had an implicit ordering
|
|
|
|
# on the arguments and templates; each argument was parsed in order, and
|
|
|
|
# all the strings appended. This allowed developers to write:
|
|
|
|
#
|
|
|
|
# glib-mkenums \
|
|
|
|
# --fhead ... \
|
|
|
|
# --template a-template-file.c.in \
|
|
|
|
# --ftail ...
|
|
|
|
#
|
|
|
|
# And have the fhead be prepended to the file-head stanza in the template,
|
|
|
|
# as well as the ftail be appended to the file-tail stanza in the template.
|
|
|
|
# Short of throwing away ArgumentParser and going over sys.argv[] element
|
|
|
|
# by element, we can simulate that behaviour by ensuring some ordering in
|
|
|
|
# how we build the template strings:
|
|
|
|
#
|
|
|
|
# - the head stanzas are always prepended to the template
|
|
|
|
# - the prod stanzas are always appended to the template
|
|
|
|
# - the tail stanzas are always appended to the template
|
|
|
|
#
|
|
|
|
# Within each instance of the command line argument, we append each value
|
|
|
|
# to the array in the order in which it appears on the command line.
|
|
|
|
fhead = ''.join([unescape_cmdline_args(x) for x in options.fhead]) + fhead
|
|
|
|
vhead = ''.join([unescape_cmdline_args(x) for x in options.vhead]) + vhead
|
|
|
|
|
2017-07-17 12:15:06 +02:00
|
|
|
fprod += ''.join([unescape_cmdline_args(x) for x in options.fprod])
|
2017-07-16 12:56:52 +02:00
|
|
|
eprod += ''.join([unescape_cmdline_args(x) for x in options.eprod])
|
|
|
|
vprod += ''.join([unescape_cmdline_args(x) for x in options.vprod])
|
2017-07-17 10:36:13 +02:00
|
|
|
|
|
|
|
ftail = ftail + ''.join([unescape_cmdline_args(x) for x in options.ftail])
|
|
|
|
vtail = vtail + ''.join([unescape_cmdline_args(x) for x in options.vtail])
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
if options.comment_tmpl != '':
|
2017-07-15 11:17:48 +02:00
|
|
|
comment_tmpl = unescape_cmdline_args(options.comment_tmpl)
|
2017-10-19 11:15:49 +02:00
|
|
|
elif comment_tmpl == "":
|
|
|
|
# default to C-style comments
|
|
|
|
comment_tmpl = "/* \u0040comment\u0040 */"
|
2017-07-17 10:36:13 +02:00
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
output = options.output
|
|
|
|
|
|
|
|
if output is not None:
|
|
|
|
(out_dir, out_fn) = os.path.split(options.output)
|
|
|
|
out_suffix = '_' + os.path.splitext(out_fn)[1]
|
|
|
|
if out_dir == '':
|
|
|
|
out_dir = '.'
|
2017-07-22 20:47:43 +02:00
|
|
|
fd, filename = tempfile.mkstemp(dir=out_dir)
|
|
|
|
os.close(fd)
|
|
|
|
tmpfile = io.open(filename, "w", encoding="utf-8")
|
2017-03-21 17:42:19 +01:00
|
|
|
output_stream = tmpfile
|
|
|
|
else:
|
|
|
|
tmpfile = None
|
2016-08-21 21:03:15 +02:00
|
|
|
|
2001-04-29 05:04:27 +02:00
|
|
|
# put auto-generation comment
|
2017-10-19 11:16:51 +02:00
|
|
|
comment = comment_tmpl.replace('\u0040comment\u0040',
|
|
|
|
'This file is generated by glib-mkenums, do '
|
|
|
|
'not modify it. This code is licensed under '
|
|
|
|
'the same license as the containing project. '
|
|
|
|
'Note that it links to GLib, so must comply '
|
|
|
|
'with the LGPL linking clauses.')
|
2017-03-21 17:42:19 +01:00
|
|
|
write_output("\n" + comment + '\n')
|
|
|
|
|
|
|
|
def replace_specials(prod):
|
|
|
|
prod = prod.replace(r'\\a', r'\a')
|
|
|
|
prod = prod.replace(r'\\b', r'\b')
|
|
|
|
prod = prod.replace(r'\\t', r'\t')
|
|
|
|
prod = prod.replace(r'\\n', r'\n')
|
|
|
|
prod = prod.replace(r'\\f', r'\f')
|
|
|
|
prod = prod.replace(r'\\r', r'\r')
|
|
|
|
prod = prod.rstrip()
|
|
|
|
return prod
|
|
|
|
|
2018-08-10 17:53:23 +02:00
|
|
|
|
|
|
|
def warn_if_filename_basename_used(section, prod):
|
|
|
|
for substitution in ('\u0040filename\u0040',
|
|
|
|
'\u0040basename\u0040'):
|
|
|
|
if substitution in prod:
|
|
|
|
print_warning('{} used in {} section.'.format(substitution,
|
|
|
|
section))
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
if len(fhead) > 0:
|
2017-07-15 15:22:20 +02:00
|
|
|
prod = fhead
|
2018-08-10 17:53:23 +02:00
|
|
|
warn_if_filename_basename_used('file-header', prod)
|
2017-03-21 17:42:19 +01:00
|
|
|
prod = replace_specials(prod)
|
|
|
|
write_output(prod)
|
|
|
|
|
|
|
|
def process_file(curfilename):
|
2022-11-01 02:12:26 +01:00
|
|
|
global entries, flags, seenbitshift, seenprivate, enum_prefix, c_namespace
|
2017-03-21 17:42:19 +01:00
|
|
|
firstenum = True
|
2017-07-17 11:24:32 +02:00
|
|
|
|
|
|
|
try:
|
2017-10-23 14:59:58 +02:00
|
|
|
curfile = io.open(curfilename, encoding="utf-8",
|
|
|
|
errors="replace_and_warn")
|
2017-07-19 11:49:04 +02:00
|
|
|
except IOError as e:
|
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
print_warning('No file "{}" found.'.format(curfilename))
|
|
|
|
return
|
|
|
|
raise
|
2017-07-17 11:24:32 +02:00
|
|
|
|
2017-07-17 17:29:40 +02:00
|
|
|
while True:
|
|
|
|
line = curfile.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
|
|
|
|
line = line.strip()
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
# read lines until we have no open comments
|
|
|
|
while re.search(r'/\*([^*]|\*(?!/))*$', line):
|
2017-05-28 22:29:20 +02:00
|
|
|
line += curfile.readline()
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
# strip comments w/o options
|
|
|
|
line = re.sub(r'''/\*(?!<)
|
|
|
|
([^*]+|\*(?!/))*
|
|
|
|
\*/''', '', line)
|
|
|
|
|
|
|
|
# ignore forward declarations
|
|
|
|
if re.match(r'\s*typedef\s+enum.*;', line):
|
|
|
|
continue
|
|
|
|
|
2018-03-18 21:56:17 +01:00
|
|
|
m = re.match(r'''\s*typedef\s+enum\s*[_A-Za-z]*[_A-Za-z0-9]*\s*
|
2017-03-21 17:42:19 +01:00
|
|
|
({)?\s*
|
|
|
|
(?:/\*<
|
|
|
|
(([^*]|\*(?!/))*)
|
|
|
|
>\s*\*/)?
|
|
|
|
\s*({)?''', line, flags=re.X)
|
|
|
|
if m:
|
|
|
|
groups = m.groups()
|
|
|
|
if len(groups) >= 2 and groups[1] is not None:
|
2017-07-15 15:22:20 +02:00
|
|
|
options = parse_trigraph(groups[1])
|
2017-03-21 17:42:19 +01:00
|
|
|
if 'skip' in options:
|
|
|
|
continue
|
|
|
|
enum_prefix = options.get('prefix', None)
|
2017-08-01 11:11:09 +02:00
|
|
|
flags = options.get('flags', None)
|
2017-08-01 11:18:42 +02:00
|
|
|
if 'flags' in options:
|
|
|
|
if flags is None:
|
|
|
|
flags = 1
|
|
|
|
else:
|
|
|
|
flags = int(flags)
|
2017-03-21 17:42:19 +01:00
|
|
|
option_lowercase_name = options.get('lowercase_name', None)
|
|
|
|
option_underscore_name = options.get('underscore_name', None)
|
2020-05-12 21:56:02 +02:00
|
|
|
option_since = options.get('since', None)
|
2017-03-21 17:42:19 +01:00
|
|
|
else:
|
|
|
|
enum_prefix = None
|
2017-08-01 11:11:09 +02:00
|
|
|
flags = None
|
2017-03-21 17:42:19 +01:00
|
|
|
option_lowercase_name = None
|
|
|
|
option_underscore_name = None
|
2020-05-12 21:56:02 +02:00
|
|
|
option_since = None
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
if option_lowercase_name is not None:
|
|
|
|
if option_underscore_name is not None:
|
2018-03-21 16:03:14 +01:00
|
|
|
print_warning("lowercase_name overridden with underscore_name")
|
2017-03-21 17:42:19 +01:00
|
|
|
option_lowercase_name = None
|
|
|
|
else:
|
2017-07-17 11:32:33 +02:00
|
|
|
print_warning("lowercase_name is deprecated, use underscore_name")
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
# Didn't have trailing '{' look on next lines
|
|
|
|
if groups[0] is None and (len(groups) < 4 or groups[3] is None):
|
|
|
|
while True:
|
|
|
|
line = curfile.readline()
|
2018-03-18 21:58:08 +01:00
|
|
|
if not line:
|
|
|
|
print_error("Syntax error when looking for opening { in enum")
|
2017-03-21 17:42:19 +01:00
|
|
|
if re.match(r'\s*\{', line):
|
|
|
|
break
|
|
|
|
|
2017-07-15 15:22:20 +02:00
|
|
|
seenbitshift = 0
|
2021-01-10 19:41:07 +01:00
|
|
|
seenprivate = False
|
2017-07-15 15:22:20 +02:00
|
|
|
entries = []
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
# Now parse the entries
|
2017-07-15 15:22:20 +02:00
|
|
|
parse_entries(curfile, curfilename)
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
# figure out if this was a flags or enums enumeration
|
2017-08-01 11:11:09 +02:00
|
|
|
if flags is None:
|
2017-03-21 17:42:19 +01:00
|
|
|
flags = seenbitshift
|
|
|
|
|
|
|
|
# Autogenerate a prefix
|
|
|
|
if enum_prefix is None:
|
|
|
|
for entry in entries:
|
2022-11-01 15:46:13 +01:00
|
|
|
if not entry[2] and (len(entry) < 4 or entry[3] is None):
|
2017-07-15 15:22:20 +02:00
|
|
|
name = entry[0]
|
2017-03-21 17:42:19 +01:00
|
|
|
if enum_prefix is not None:
|
|
|
|
enum_prefix = os.path.commonprefix([name, enum_prefix])
|
|
|
|
else:
|
2017-07-15 15:22:20 +02:00
|
|
|
enum_prefix = name
|
2017-03-21 17:42:19 +01:00
|
|
|
if enum_prefix is None:
|
|
|
|
enum_prefix = ""
|
|
|
|
else:
|
|
|
|
# Trim so that it ends in an underscore
|
|
|
|
enum_prefix = re.sub(r'_[^_]*$', '_', enum_prefix)
|
|
|
|
else:
|
|
|
|
# canonicalize user defined prefixes
|
|
|
|
enum_prefix = enum_prefix.upper()
|
|
|
|
enum_prefix = enum_prefix.replace('-', '_')
|
2017-07-15 15:31:05 +02:00
|
|
|
enum_prefix = re.sub(r'(.*)([^_])$', r'\1\2_', enum_prefix)
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
fixed_entries = []
|
|
|
|
for e in entries:
|
|
|
|
name = e[0]
|
|
|
|
num = e[1]
|
2022-11-01 15:46:13 +01:00
|
|
|
private = e[2]
|
|
|
|
if len(e) < 4 or e[3] is None:
|
2017-03-21 17:42:19 +01:00
|
|
|
nick = re.sub(r'^' + enum_prefix, '', name)
|
|
|
|
nick = nick.replace('_', '-').lower()
|
2022-11-01 15:46:13 +01:00
|
|
|
e = (name, num, private, nick)
|
2017-03-21 17:42:19 +01:00
|
|
|
fixed_entries.append(e)
|
|
|
|
entries = fixed_entries
|
|
|
|
|
|
|
|
# Spit out the output
|
|
|
|
if option_underscore_name is not None:
|
|
|
|
enumlong = option_underscore_name.upper()
|
|
|
|
enumsym = option_underscore_name.lower()
|
|
|
|
enumshort = re.sub(r'^[A-Z][A-Z0-9]*_', '', enumlong)
|
|
|
|
|
|
|
|
enumname_prefix = re.sub('_' + enumshort + '$', '', enumlong)
|
|
|
|
elif symprefix == '' and idprefix == '':
|
|
|
|
# enumname is e.g. GMatchType
|
|
|
|
enspace = re.sub(r'^([A-Z][a-z]*).*$', r'\1', enumname)
|
|
|
|
|
|
|
|
enumshort = re.sub(r'^[A-Z][a-z]*', '', enumname)
|
|
|
|
enumshort = re.sub(r'([^A-Z])([A-Z])', r'\1_\2', enumshort)
|
|
|
|
enumshort = re.sub(r'([A-Z][A-Z])([A-Z][0-9a-z])', r'\1_\2', enumshort)
|
|
|
|
enumshort = enumshort.upper()
|
|
|
|
|
|
|
|
enumname_prefix = re.sub(r'^([A-Z][a-z]*).*$', r'\1', enumname).upper()
|
|
|
|
|
|
|
|
enumlong = enspace.upper() + "_" + enumshort
|
|
|
|
enumsym = enspace.lower() + "_" + enumshort.lower()
|
|
|
|
|
|
|
|
if option_lowercase_name is not None:
|
2017-07-15 15:22:20 +02:00
|
|
|
enumsym = option_lowercase_name
|
2017-03-21 17:42:19 +01:00
|
|
|
else:
|
2017-07-15 15:22:20 +02:00
|
|
|
enumshort = enumname
|
2017-03-21 17:42:19 +01:00
|
|
|
if idprefix:
|
|
|
|
enumshort = re.sub(r'^' + idprefix, '', enumshort)
|
|
|
|
else:
|
|
|
|
enumshort = re.sub(r'/^[A-Z][a-z]*', '', enumshort)
|
|
|
|
|
|
|
|
enumshort = re.sub(r'([^A-Z])([A-Z])', r'\1_\2', enumshort)
|
|
|
|
enumshort = re.sub(r'([A-Z][A-Z])([A-Z][0-9a-z])', r'\1_\2', enumshort)
|
2017-07-15 15:22:20 +02:00
|
|
|
enumshort = enumshort.upper()
|
2017-03-21 17:42:19 +01:00
|
|
|
|
2017-07-16 13:06:34 +02:00
|
|
|
if symprefix:
|
2017-03-21 17:42:19 +01:00
|
|
|
enumname_prefix = symprefix.upper()
|
|
|
|
else:
|
|
|
|
enumname_prefix = idprefix.upper()
|
|
|
|
|
|
|
|
enumlong = enumname_prefix + "_" + enumshort
|
|
|
|
enumsym = enumlong.lower()
|
|
|
|
|
2020-05-12 21:56:02 +02:00
|
|
|
if option_since is not None:
|
|
|
|
enumsince = option_since
|
|
|
|
else:
|
|
|
|
enumsince = ""
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
if firstenum:
|
|
|
|
firstenum = False
|
|
|
|
|
|
|
|
if len(fprod) > 0:
|
2017-07-15 15:22:20 +02:00
|
|
|
prod = fprod
|
|
|
|
base = os.path.basename(curfilename)
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
prod = prod.replace('\u0040filename\u0040', curfilename)
|
|
|
|
prod = prod.replace('\u0040basename\u0040', base)
|
|
|
|
prod = replace_specials(prod)
|
|
|
|
|
|
|
|
write_output(prod)
|
|
|
|
|
|
|
|
if len(eprod) > 0:
|
2017-07-15 15:22:20 +02:00
|
|
|
prod = eprod
|
2017-03-21 17:42:19 +01:00
|
|
|
|
|
|
|
prod = prod.replace('\u0040enum_name\u0040', enumsym)
|
|
|
|
prod = prod.replace('\u0040EnumName\u0040', enumname)
|
|
|
|
prod = prod.replace('\u0040ENUMSHORT\u0040', enumshort)
|
|
|
|
prod = prod.replace('\u0040ENUMNAME\u0040', enumlong)
|
|
|
|
prod = prod.replace('\u0040ENUMPREFIX\u0040', enumname_prefix)
|
2020-05-12 21:56:02 +02:00
|
|
|
prod = prod.replace('\u0040enumsince\u0040', enumsince)
|
2017-03-21 17:42:19 +01:00
|
|
|
if flags:
|
2017-07-16 18:25:08 +02:00
|
|
|
prod = prod.replace('\u0040type\u0040', 'flags')
|
2017-03-21 17:42:19 +01:00
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040type\u0040', 'enum')
|
|
|
|
if flags:
|
|
|
|
prod = prod.replace('\u0040Type\u0040', 'Flags')
|
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040Type\u0040', 'Enum')
|
|
|
|
if flags:
|
|
|
|
prod = prod.replace('\u0040TYPE\u0040', 'FLAGS')
|
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040TYPE\u0040', 'ENUM')
|
|
|
|
prod = replace_specials(prod)
|
|
|
|
write_output(prod)
|
|
|
|
|
|
|
|
if len(vhead) > 0:
|
2017-07-15 15:22:20 +02:00
|
|
|
prod = vhead
|
2017-03-21 17:42:19 +01:00
|
|
|
prod = prod.replace('\u0040enum_name\u0040', enumsym)
|
|
|
|
prod = prod.replace('\u0040EnumName\u0040', enumname)
|
|
|
|
prod = prod.replace('\u0040ENUMSHORT\u0040', enumshort)
|
|
|
|
prod = prod.replace('\u0040ENUMNAME\u0040', enumlong)
|
|
|
|
prod = prod.replace('\u0040ENUMPREFIX\u0040', enumname_prefix)
|
2020-05-12 21:56:02 +02:00
|
|
|
prod = prod.replace('\u0040enumsince\u0040', enumsince)
|
2017-03-21 17:42:19 +01:00
|
|
|
if flags:
|
|
|
|
prod = prod.replace('\u0040type\u0040', 'flags')
|
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040type\u0040', 'enum')
|
|
|
|
if flags:
|
|
|
|
prod = prod.replace('\u0040Type\u0040', 'Flags')
|
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040Type\u0040', 'Enum')
|
|
|
|
if flags:
|
|
|
|
prod = prod.replace('\u0040TYPE\u0040', 'FLAGS')
|
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040TYPE\u0040', 'ENUM')
|
|
|
|
prod = replace_specials(prod)
|
|
|
|
write_output(prod)
|
|
|
|
|
|
|
|
if len(vprod) > 0:
|
2017-07-15 15:22:20 +02:00
|
|
|
prod = vprod
|
2017-03-21 17:42:19 +01:00
|
|
|
next_num = 0
|
|
|
|
|
|
|
|
prod = replace_specials(prod)
|
2022-11-01 15:46:13 +01:00
|
|
|
for name, num, private, nick in entries:
|
2017-03-21 17:42:19 +01:00
|
|
|
tmp_prod = prod
|
|
|
|
|
|
|
|
if '\u0040valuenum\u0040' in prod:
|
|
|
|
# only attempt to eval the value if it is requested
|
|
|
|
# this prevents us from throwing errors otherwise
|
|
|
|
if num is not None:
|
|
|
|
# use sandboxed evaluation as a reasonable
|
|
|
|
# approximation to C constant folding
|
2022-11-01 02:12:26 +01:00
|
|
|
inum = eval(num, {}, c_namespace)
|
2017-03-21 17:42:19 +01:00
|
|
|
|
2023-11-21 12:54:42 +01:00
|
|
|
# Support character literals
|
|
|
|
if isinstance(inum, str) and len(inum) == 1:
|
|
|
|
inum = ord(inum)
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
# make sure it parsed to an integer
|
|
|
|
if not isinstance(inum, int):
|
|
|
|
sys.exit("Unable to parse enum value '%s'" % num)
|
|
|
|
num = inum
|
|
|
|
else:
|
|
|
|
num = next_num
|
|
|
|
|
2022-11-01 02:12:26 +01:00
|
|
|
c_namespace[name] = num
|
2017-03-21 17:42:19 +01:00
|
|
|
tmp_prod = tmp_prod.replace('\u0040valuenum\u0040', str(num))
|
|
|
|
next_num = int(num) + 1
|
|
|
|
|
2022-11-01 15:46:13 +01:00
|
|
|
if private:
|
|
|
|
continue
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
tmp_prod = tmp_prod.replace('\u0040VALUENAME\u0040', name)
|
|
|
|
tmp_prod = tmp_prod.replace('\u0040valuenick\u0040', nick)
|
|
|
|
if flags:
|
|
|
|
tmp_prod = tmp_prod.replace('\u0040type\u0040', 'flags')
|
|
|
|
else:
|
|
|
|
tmp_prod = tmp_prod.replace('\u0040type\u0040', 'enum')
|
|
|
|
if flags:
|
|
|
|
tmp_prod = tmp_prod.replace('\u0040Type\u0040', 'Flags')
|
|
|
|
else:
|
|
|
|
tmp_prod = tmp_prod.replace('\u0040Type\u0040', 'Enum')
|
|
|
|
if flags:
|
|
|
|
tmp_prod = tmp_prod.replace('\u0040TYPE\u0040', 'FLAGS')
|
|
|
|
else:
|
|
|
|
tmp_prod = tmp_prod.replace('\u0040TYPE\u0040', 'ENUM')
|
|
|
|
tmp_prod = tmp_prod.rstrip()
|
|
|
|
|
|
|
|
write_output(tmp_prod)
|
|
|
|
|
|
|
|
if len(vtail) > 0:
|
|
|
|
prod = vtail
|
|
|
|
prod = prod.replace('\u0040enum_name\u0040', enumsym)
|
|
|
|
prod = prod.replace('\u0040EnumName\u0040', enumname)
|
|
|
|
prod = prod.replace('\u0040ENUMSHORT\u0040', enumshort)
|
|
|
|
prod = prod.replace('\u0040ENUMNAME\u0040', enumlong)
|
|
|
|
prod = prod.replace('\u0040ENUMPREFIX\u0040', enumname_prefix)
|
2020-05-12 21:56:02 +02:00
|
|
|
prod = prod.replace('\u0040enumsince\u0040', enumsince)
|
2017-03-21 17:42:19 +01:00
|
|
|
if flags:
|
|
|
|
prod = prod.replace('\u0040type\u0040', 'flags')
|
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040type\u0040', 'enum')
|
|
|
|
if flags:
|
|
|
|
prod = prod.replace('\u0040Type\u0040', 'Flags')
|
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040Type\u0040', 'Enum')
|
|
|
|
if flags:
|
|
|
|
prod = prod.replace('\u0040TYPE\u0040', 'FLAGS')
|
|
|
|
else:
|
|
|
|
prod = prod.replace('\u0040TYPE\u0040', 'ENUM')
|
|
|
|
prod = replace_specials(prod)
|
|
|
|
write_output(prod)
|
|
|
|
|
2017-10-24 14:22:40 +02:00
|
|
|
for fname in sorted(options.args):
|
2017-03-21 17:42:19 +01:00
|
|
|
process_file(fname)
|
|
|
|
|
|
|
|
if len(ftail) > 0:
|
|
|
|
prod = ftail
|
2018-08-10 17:53:23 +02:00
|
|
|
warn_if_filename_basename_used('file-tail', prod)
|
2017-03-21 17:42:19 +01:00
|
|
|
prod = replace_specials(prod)
|
|
|
|
write_output(prod)
|
2001-04-29 05:04:27 +02:00
|
|
|
|
|
|
|
# put auto-generation comment
|
2017-07-15 15:22:20 +02:00
|
|
|
comment = comment_tmpl
|
2017-03-21 17:42:19 +01:00
|
|
|
comment = comment.replace('\u0040comment\u0040', 'Generated data ends here')
|
|
|
|
write_output("\n" + comment + "\n")
|
|
|
|
|
|
|
|
if tmpfile is not None:
|
|
|
|
tmpfilename = tmpfile.name
|
|
|
|
tmpfile.close()
|
2017-10-27 15:42:46 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.unlink(options.output)
|
|
|
|
except OSError as error:
|
|
|
|
if error.errno != errno.ENOENT:
|
|
|
|
raise error
|
|
|
|
|
2017-03-21 17:42:19 +01:00
|
|
|
os.rename(tmpfilename, options.output)
|