generated man pages: build without hard coded extension
[free-sw/xcb/libxcb] / src / c_client.py
index 8382bb5..161cbf5 100644 (file)
@@ -5,6 +5,7 @@ from functools import reduce
 import getopt
 import os
 import sys
+import errno
 import time
 import re
 
@@ -175,6 +176,9 @@ def c_open(self):
     _h('')
     _h('#include "xcb.h"')
 
+    _c('#ifdef HAVE_CONFIG_H')
+    _c('#include "config.h"')
+    _c('#endif')
     _c('#include <stdlib.h>')
     _c('#include <string.h>')
     _c('#include <assert.h>')
@@ -271,7 +275,7 @@ def c_enum(self, name):
         equals = ' = ' if eval != '' else ''
         comma = ',' if count > 0 else ''
         doc = ''
-        if self.doc and enam in self.doc.fields:
+        if hasattr(self, "doc") and self.doc and enam in self.doc.fields:
             doc = '\n/**< %s */\n' % self.doc.fields[enam]
         _h('    %s%s%s%s%s', _n(name + (enam,)).upper(), equals, eval, comma, doc)
 
@@ -299,6 +303,7 @@ def _c_type_setup(self, name, postfix):
     self.c_reply_name = _n(name + ('reply',))
     self.c_reply_type = _t(name + ('reply',))
     self.c_cookie_type = _t(name + ('cookie',))
+    self.c_reply_fds_name = _n(name + ('reply_fds',))
 
     self.need_aux = False
     self.need_serialize = False
@@ -683,10 +688,20 @@ def _c_serialize_helper_switch(context, self, complex_name,
     switch_expr = _c_accessor_get_expr(self.expr, None)
 
     for b in self.bitcases:            
-        bitcase_expr = _c_accessor_get_expr(b.type.expr, None)
-        code_lines.append('    if(%s & %s) {' % (switch_expr, bitcase_expr))
-#        code_lines.append('        printf("switch %s: entering bitcase section %s (mask=%%%%d)...\\n", %s);' % 
-#                          (self.name[-1], b.type.name[-1], bitcase_expr))
+        len_expr = len(b.type.expr)
+        for n, expr in enumerate(b.type.expr):
+            bitcase_expr = _c_accessor_get_expr(expr, None)
+            # only one <enumref> in the <bitcase>
+            if len_expr == 1:
+                code_lines.append('    if(%s & %s) {' % (switch_expr, bitcase_expr))
+            # multiple <enumref> in the <bitcase>
+            elif n == 0: # first
+                code_lines.append('    if((%s & %s) ||' % (switch_expr, bitcase_expr))
+            elif len_expr == (n + 1): # last
+                code_lines.append('       (%s & %s)) {' % (switch_expr, bitcase_expr))
+            else: # between first and last
+                code_lines.append('       (%s & %s) ||' % (switch_expr, bitcase_expr))
+
         b_prefix = prefix
         if b.type.has_name:
             b_prefix = prefix + [(b.c_field_name, '.', b.type)]
@@ -966,15 +981,22 @@ def _c_serialize_helper_fields(context, self,
 
         # fields with variable size
         else:
-            # switch/bitcase: always calculate padding before and after variable sized fields
-            if need_padding or is_bitcase:
+            if field.type.is_pad:
+                # Variable length pad is <pad align= />
+                code_lines.append('%s    xcb_align_to = %d;' % (space, field.type.align))
                 count += _c_serialize_helper_insert_padding(context, code_lines, space, 
+                                                        self.var_followed_by_fixed_fields)
+                continue
+            else:
+                # switch/bitcase: always calculate padding before and after variable sized fields
+                if need_padding or is_bitcase:
+                    count += _c_serialize_helper_insert_padding(context, code_lines, space,
                                                             self.var_followed_by_fixed_fields)
 
-            value, length = _c_serialize_helper_fields_variable_size(context, self, field, 
-                                                                     code_lines, temp_vars, 
-                                                                     space, prefix)
-            prev_field_was_variable = True
+                value, length = _c_serialize_helper_fields_variable_size(context, self, field,
+                                                                         code_lines, temp_vars,
+                                                                         space, prefix)
+                prev_field_was_variable = True
         
         # save (un)serialization C code
         if '' != value:
@@ -1046,8 +1068,8 @@ def _c_serialize_helper(context, complex_type,
         if context in ('unserialize', 'unpack', 'sizeof') and not self.var_followed_by_fixed_fields:
             code_lines.append('%s    xcb_block_len += sizeof(%s);' % (space, self.c_type))
             code_lines.append('%s    xcb_tmp += xcb_block_len;' % space)
-            # probably not needed
-            #_c_serialize_helper_insert_padding(context, code_lines, space, False)
+            code_lines.append('%s    xcb_buffer_len += xcb_block_len;' % space)
+            code_lines.append('%s    xcb_block_len = 0;' % space)
 
         count += _c_serialize_helper_fields(context, self, 
                                             code_lines, temp_vars, 
@@ -1118,11 +1140,11 @@ def _c_serialize(context, self):
             _c('    %s *xcb_out = *_buffer;', self.c_type)
             _c('    unsigned int xcb_out_pad = -sizeof(%s) & 3;', self.c_type)
             _c('    unsigned int xcb_buffer_len = sizeof(%s) + xcb_out_pad;', self.c_type)
-            _c('    unsigned int xcb_align_to;')
+            _c('    unsigned int xcb_align_to = 0;')
         else:
             _c('    char *xcb_out = *_buffer;')
             _c('    unsigned int xcb_buffer_len = 0;')
-            _c('    unsigned int xcb_align_to;')
+            _c('    unsigned int xcb_align_to = 0;')
         prefix = [('_aux', '->', self)]
         aux_ptr = 'xcb_out'
 
@@ -1145,7 +1167,7 @@ def _c_serialize(context, self):
         _c('    unsigned int xcb_buffer_len = 0;')
         _c('    unsigned int xcb_block_len = 0;')
         _c('    unsigned int xcb_pad = 0;')
-        _c('    unsigned int xcb_align_to;')
+        _c('    unsigned int xcb_align_to = 0;')
 
     elif 'sizeof' == context:
         param_names = [p[2] for p in params]
@@ -1190,7 +1212,7 @@ def _c_serialize(context, self):
             _c('    unsigned int xcb_buffer_len = 0;')
             _c('    unsigned int xcb_block_len = 0;')
             _c('    unsigned int xcb_pad = 0;')        
-            _c('    unsigned int xcb_align_to;')
+            _c('    unsigned int xcb_align_to = 0;')
 
     _c('')
     for t in temp_vars:
@@ -1725,10 +1747,11 @@ def _c_accessors(self, name, base):
 #    else:
     if True:
         for field in self.fields:
-            if field.type.is_list and not field.type.fixed_size():
-                _c_accessors_list(self, field)
-            elif field.prev_varsized_field is not None or not field.type.fixed_size():
-                _c_accessors_field(self, field)
+            if not field.type.is_pad:
+                if field.type.is_list and not field.type.fixed_size():
+                    _c_accessors_list(self, field)
+                elif field.prev_varsized_field is not None or not field.type.fixed_size():
+                    _c_accessors_field(self, field)
 
 def c_simple(self, name):
     '''
@@ -1747,7 +1770,7 @@ def c_simple(self, name):
         # Iterator
         _c_iterator(self, name)
 
-def _c_complex(self):
+def _c_complex(self, force_packed = False):
     '''
     Helper function for handling all structure types.
     Called for all structs, requests, replies, events, errors.
@@ -1773,12 +1796,12 @@ def _c_complex(self):
     for field in struct_fields:
         length = len(field.c_field_type)
         # account for '*' pointer_spec
-        if not field.type.fixed_size():
+        if not field.type.fixed_size() and not self.is_union:
             length += 1
         maxtypelen = max(maxtypelen, length)
 
     def _c_complex_field(self, field, space=''):
-        if (field.type.fixed_size() or 
+        if (field.type.fixed_size() or self.is_union or
             # in case of switch with switch children, don't make the field a pointer
             # necessary for unserialize to work
             (self.is_switch and field.type.is_switch)):
@@ -1802,7 +1825,7 @@ def _c_complex(self):
             if b.type.has_name:
                 _h('    } %s;', b.c_field_name)
 
-    _h('} %s;', self.c_type)
+    _h('} %s%s;', 'XCB_PACKED ' if force_packed else '', self.c_type)
 
 def c_struct(self, name):
     '''
@@ -1821,7 +1844,7 @@ def c_union(self, name):
     _c_complex(self)
     _c_iterator(self, name)
 
-def _c_request_helper(self, name, cookie_type, void, regular, aux=False):
+def _c_request_helper(self, name, cookie_type, void, regular, aux=False, reply_fds=False):
     '''
     Declares a request function.
     '''
@@ -1850,6 +1873,12 @@ def _c_request_helper(self, name, cookie_type, void, regular, aux=False):
     # What flag is passed to xcb_request
     func_flags = '0' if (void and regular) or (not void and not regular) else 'XCB_REQUEST_CHECKED'
 
+    if reply_fds:
+        if func_flags == '0':
+            func_flags = 'XCB_REQUEST_REPLY_FDS'
+        else:
+            func_flags = func_flags + '|XCB_REQUEST_REPLY_FDS'
+
     # Global extension id variable or NULL for xproto
     func_ext_global = '&' + _ns.c_ext_global_name if _ns.is_ext else '0'
 
@@ -1890,7 +1919,7 @@ def _c_request_helper(self, name, cookie_type, void, regular, aux=False):
     _c_setlevel(1)
     _h('')
     _h('/**')
-    if self.doc:
+    if hasattr(self, "doc") and self.doc:
         if self.doc.brief:
             _h(' * @brief ' + self.doc.brief)
         else:
@@ -1899,7 +1928,7 @@ def _c_request_helper(self, name, cookie_type, void, regular, aux=False):
     _h(' *')
     _h(' * @param c The connection')
     param_names = [f.c_field_name for f in param_fields]
-    if self.doc:
+    if hasattr(self, "doc") and self.doc:
         for field in param_fields:
             # XXX: hard-coded until we fix xproto.xml
             base_func_name = self.c_request_name if not aux else self.c_aux_name
@@ -1931,7 +1960,7 @@ def _c_request_helper(self, name, cookie_type, void, regular, aux=False):
     _h(' * @return A cookie')
     _h(' *')
 
-    if self.doc:
+    if hasattr(self, "doc") and self.doc:
         if self.doc.description:
             desc = self.doc.description
             for name in param_names:
@@ -2135,6 +2164,10 @@ def _c_request_helper(self, name, cookie_type, void, regular, aux=False):
         # no padding necessary - _serialize() keeps track of padding automatically
 
     _c('    ')
+    for field in param_fields:
+        if field.isfd:
+            _c('    xcb_send_fd(c, %s);', field.c_field_name)
+    
     _c('    xcb_ret.sequence = xcb_send_request(c, %s, xcb_parts + 2, &xcb_req);', func_flags)
     
     # free dyn. all. data, if any
@@ -2238,6 +2271,51 @@ def _c_reply(self, name):
 
     _c('}')
 
+def _c_reply_has_fds(self):
+    for field in self.fields:
+        if field.isfd:
+            return True
+    return False
+
+def _c_reply_fds(self, name):
+    '''
+    Declares the function that returns fds related to the reply.
+    '''
+    spacing1 = ' ' * (len(self.c_reply_type) - len('xcb_connection_t'))
+    spacing3 = ' ' * (len(self.c_reply_fds_name) + 2)
+    _h('')
+    _h('/**')
+    _h(' * Return the reply fds')
+    _h(' * @param c      The connection')
+    _h(' * @param reply  The reply')
+    _h(' *')
+    _h(' * Returns the array of reply fds of the request asked by')
+    _h(' * ')
+    _h(' * The returned value must be freed by the caller using free().')
+    _h(' */')
+    _c('')
+    _hc('')
+    _hc('/*****************************************************************************')
+    _hc(' **')
+    _hc(' ** int * %s', self.c_reply_fds_name)
+    _hc(' ** ')
+    _hc(' ** @param xcb_connection_t%s  *c', spacing1)
+    _hc(' ** @param %s  *reply', self.c_reply_type)
+    _hc(' ** @returns int *')
+    _hc(' **')
+    _hc(' *****************************************************************************/')
+    _hc(' ')
+    _hc('int *')
+    _hc('%s (xcb_connection_t%s  *c  /**< */,', self.c_reply_fds_name, spacing1)
+    _h('%s%s  *reply  /**< */);', spacing3, self.c_reply_type)
+    _c('%s%s  *reply  /**< */)', spacing3, self.c_reply_type)
+    _c('{')
+    
+    _c('    return xcb_get_reply_fds(c, reply, sizeof(%s) + 4 * reply->length);', self.c_reply_type)
+
+    _c('}')
+    
+
 def _c_opcode(name, opcode):
     '''
     Declares the opcode define for requests, events, and errors.
@@ -2281,7 +2359,7 @@ def _man_request(self, name, cookie_type, void, aux):
     # Left-adjust instead of adjusting to both sides
     f.write('.ad l\n')
     f.write('.SH NAME\n')
-    brief = self.doc.brief if self.doc else ''
+    brief = self.doc.brief if hasattr(self, "doc") and self.doc else ''
     f.write('%s \\- %s\n' % (func_name, brief))
     f.write('.SH SYNOPSIS\n')
     # Don't split words (hyphenate)
@@ -2500,7 +2578,7 @@ def _man_request(self, name, cookie_type, void, aux):
             field.enum = 'CW'
         elif base_func_name == 'xcb_create_window' and field.c_field_name == 'value_mask':
             field.enum = 'CW'
-        if field.enum:
+        if hasattr(field, "enum") and field.enum:
             # XXX: why the 'xcb' prefix?
             key = ('xcb', field.enum)
             if key in enums:
@@ -2511,7 +2589,7 @@ def _man_request(self, name, cookie_type, void, aux):
                 for (enam, eval) in enum.values:
                     count = count - 1
                     f.write('.IP \\fI%s\\fP 1i\n' % (_n(key + (enam,)).upper()))
-                    if enum.doc and enam in enum.doc.fields:
+                    if hasattr(enum, "doc") and enum.doc and enam in enum.doc.fields:
                         desc = re.sub(r'`([^`]+)`', r'\\fI\1\\fP', enum.doc.fields[enam])
                         f.write('%s\n' % desc)
                     else:
@@ -2520,7 +2598,7 @@ def _man_request(self, name, cookie_type, void, aux):
                 f.write('.RS 1i\n')
                 printed_enum = True
 
-        if self.doc and field.field_name in self.doc.fields:
+        if hasattr(self, "doc") and self.doc and field.field_name in self.doc.fields:
             desc = self.doc.fields[field.field_name]
             desc = re.sub(r'`([^`]+)`', r'\\fI\1\\fP', desc)
             if printed_enum:
@@ -2555,7 +2633,7 @@ def _man_request(self, name, cookie_type, void, aux):
                 continue
             f.write('.IP \\fI%s\\fP 1i\n' % (field.c_field_name))
             printed_enum = False
-            if field.enum:
+            if hasattr(field, "enum") and field.enum:
                 # XXX: why the 'xcb' prefix?
                 key = ('xcb', field.enum)
                 if key in enums:
@@ -2575,7 +2653,7 @@ def _man_request(self, name, cookie_type, void, aux):
                     f.write('.RS 1i\n')
                     printed_enum = True
 
-            if self.reply.doc and field.field_name in self.reply.doc.fields:
+            if hasattr(self.reply, "doc") and self.reply.doc and field.field_name in self.reply.doc.fields:
                 desc = self.reply.doc.fields[field.field_name]
                 desc = re.sub(r'`([^`]+)`', r'\\fI\1\\fP', desc)
                 if printed_enum:
@@ -2590,7 +2668,7 @@ def _man_request(self, name, cookie_type, void, aux):
 
     # text description
     f.write('.SH DESCRIPTION\n')
-    if self.doc and self.doc.description:
+    if hasattr(self, "doc") and self.doc and self.doc.description:
         desc = self.doc.description
         desc = re.sub(r'`([^`]+)`', r'\\fI\1\\fP', desc)
         lines = desc.split('\n')
@@ -2611,14 +2689,14 @@ def _man_request(self, name, cookie_type, void, aux):
                  'details.\n') %
                 (cookie_type, self.c_reply_name, base_func_name))
     f.write('.SH ERRORS\n')
-    if self.doc:
-        for errtype, errtext in self.doc.errors.iteritems():
+    if hasattr(self, "doc") and self.doc:
+        for errtype, errtext in self.doc.errors.items():
             f.write('.IP \\fI%s\\fP 1i\n' % (_t(('xcb', errtype, 'error'))))
             errtext = re.sub(r'`([^`]+)`', r'\\fI\1\\fP', errtext)
             f.write('%s\n' % (errtext))
-    if not self.doc or len(self.doc.errors) == 0:
+    if not hasattr(self, "doc") or not self.doc or len(self.doc.errors) == 0:
         f.write('This request does never generate any errors.\n')
-    if self.doc and self.doc.example:
+    if hasattr(self, "doc") and self.doc and self.doc.example:
         f.write('.SH EXAMPLE\n')
         f.write('.nf\n')
         f.write('.sp\n')
@@ -2626,11 +2704,11 @@ def _man_request(self, name, cookie_type, void, aux):
         f.write('\n'.join(lines) + '\n')
         f.write('.fi\n')
     f.write('.SH SEE ALSO\n')
-    if self.doc:
+    if hasattr(self, "doc") and self.doc:
         see = ['.BR %s (3)' % 'xcb-requests']
         if self.doc.example:
             see.append('.BR %s (3)' % 'xcb-examples')
-        for seename, seetype in self.doc.see.iteritems():
+        for seename, seetype in self.doc.see.items():
             if seetype == 'program':
                 see.append('.BR %s (1)' % seename)
             elif seetype == 'event':
@@ -2655,7 +2733,7 @@ def _man_event(self, name):
     # Left-adjust instead of adjusting to both sides
     f.write('.ad l\n')
     f.write('.SH NAME\n')
-    brief = self.doc.brief if self.doc else ''
+    brief = self.doc.brief if hasattr(self, "doc") and self.doc else ''
     f.write('%s \\- %s\n' % (self.c_type, brief))
     f.write('.SH SYNOPSIS\n')
     # Don't split words (hyphenate)
@@ -2733,7 +2811,7 @@ def _man_event(self, name):
             if isinstance(field.type, PadType):
                 continue
             f.write('.IP \\fI%s\\fP 1i\n' % (field.c_field_name))
-            if self.doc and field.field_name in self.doc.fields:
+            if hasattr(self, "doc") and self.doc and field.field_name in self.doc.fields:
                 desc = self.doc.fields[field.field_name]
                 desc = re.sub(r'`([^`]+)`', r'\\fI\1\\fP', desc)
                 f.write('%s\n' % desc)
@@ -2742,13 +2820,13 @@ def _man_event(self, name):
 
     # text description
     f.write('.SH DESCRIPTION\n')
-    if self.doc and self.doc.description:
+    if hasattr(self, "doc") and self.doc and self.doc.description:
         desc = self.doc.description
         desc = re.sub(r'`([^`]+)`', r'\\fI\1\\fP', desc)
         lines = desc.split('\n')
         f.write('\n'.join(lines) + '\n')
 
-    if self.doc and self.doc.example:
+    if hasattr(self, "doc") and self.doc and self.doc.example:
         f.write('.SH EXAMPLE\n')
         f.write('.nf\n')
         f.write('.sp\n')
@@ -2756,11 +2834,11 @@ def _man_event(self, name):
         f.write('\n'.join(lines) + '\n')
         f.write('.fi\n')
     f.write('.SH SEE ALSO\n')
-    if self.doc:
+    if hasattr(self, "doc") and self.doc:
         see = ['.BR %s (3)' % 'xcb_generic_event_t']
         if self.doc.example:
             see.append('.BR %s (3)' % 'xcb-examples')
-        for seename, seetype in self.doc.see.iteritems():
+        for seename, seetype in self.doc.see.items():
             if seetype == 'program':
                 see.append('.BR %s (1)' % seename)
             elif seetype == 'event':
@@ -2798,14 +2876,17 @@ def c_request(self, name):
         # Reply structure definition
         _c_complex(self.reply)
         # Request prototypes
-        _c_request_helper(self, name, self.c_cookie_type, False, True)
-        _c_request_helper(self, name, self.c_cookie_type, False, False)
+        has_fds = _c_reply_has_fds(self.reply)
+        _c_request_helper(self, name, self.c_cookie_type, False, True, False, has_fds)
+        _c_request_helper(self, name, self.c_cookie_type, False, False, False, has_fds)
         if self.need_aux:
-            _c_request_helper(self, name, self.c_cookie_type, False, True, True)
-            _c_request_helper(self, name, self.c_cookie_type, False, False, True)
+            _c_request_helper(self, name, self.c_cookie_type, False, True, True, has_fds)
+            _c_request_helper(self, name, self.c_cookie_type, False, False, True, has_fds)
         # Reply accessors
         _c_accessors(self.reply, name + ('reply',), name)
         _c_reply(self, name)
+        if has_fds:
+            _c_reply_fds(self, name)
     else:
         # Request prototypes
         _c_request_helper(self, name, 'xcb_void_cookie_t', True, False)
@@ -2823,6 +2904,29 @@ def c_event(self, name):
     '''
     Exported function that handles event declarations.
     '''
+
+    # The generic event structure xcb_ge_event_t has the full_sequence field
+    # at the 32byte boundary. That's why we've to inject this field into GE
+    # events while generating the structure for them. Otherwise we would read
+    # garbage (the internal full_sequence) when accessing normal event fields
+    # there.
+    force_packed = False
+    if hasattr(self, 'is_ge_event') and self.is_ge_event and self.name == name:
+        event_size = 0
+        for field in self.fields:
+            if field.type.size != None and field.type.nmemb != None:
+                event_size += field.type.size * field.type.nmemb
+            if event_size == 32:
+                full_sequence = Field(tcard32, tcard32.name, 'full_sequence', False, True, True)
+                idx = self.fields.index(field)
+                self.fields.insert(idx + 1, full_sequence)
+
+                # If the event contains any 64-bit extended fields, they need
+                # to remain aligned on a 64-bit boundary.  Adding full_sequence
+                # would normally break that; force the struct to be packed.
+                force_packed = any(f.type.size == 8 and f.type.is_simple for f in self.fields[(idx+1):])
+                break
+
     _c_type_setup(self, name, ('event',))
 
     # Opcode define
@@ -2830,7 +2934,7 @@ def c_event(self, name):
 
     if self.name == name:
         # Structure definition
-        _c_complex(self)
+        _c_complex(self, force_packed)
     else:
         # Typedef
         _h('')
@@ -2902,8 +3006,11 @@ Refer to the README file in xcb/proto for more info.
     raise
 
 # Ensure the man subdirectory exists
-if not os.path.exists('man'):
+try:
     os.mkdir('man')
+except OSError as e:
+    if e.errno != errno.EEXIST:
+        raise
 
 today = time.strftime('%Y-%m-%d', time.gmtime(os.path.getmtime(args[0])))