]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - Ada95/gen/gen.c
ncurses 5.9 - patch 20140201
[ncurses.git] / Ada95 / gen / gen.c
index 24c0f08715634881ad85d56bb39937a0c5630960..082315b6ceee042fa397139dd19550687d626e90 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998,2009,2010 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -32,7 +32,7 @@
 
 /*
     Version Control
-    $Id: gen.c,v 1.52 2010/02/20 21:59:56 tom Exp $
+    $Id: gen.c,v 1.64 2014/02/01 19:52:47 tom Exp $
   --------------------------------------------------------------------------*/
 /*
   This program generates various record structures and constants from the
 #include <menu.h>
 #include <form.h>
 
+#undef UCHAR
+#undef UINT
+#define UChar(c)       ((UCHAR)(c))
 #define RES_NAME "Reserved"
 
+typedef unsigned char UCHAR;
+typedef unsigned int UINT;
+
 static const char *model = "";
 static int little_endian = 0;
 
@@ -69,45 +75,43 @@ typedef struct
   }
 name_attribute_pair;
 
+static UCHAR
+bit_is_set(const UCHAR * const data,
+          const UINT offset)
+{
+  const UCHAR byte = data[offset >> 3];
+  UINT bit;
+
+  if (little_endian)
+    bit = offset;              /* offset */
+  else                         /* or */
+    bit = ~offset;             /* 7 - offset */
+  bit &= 7;                    /* modulo 8 */
+  return byte & (UCHAR) (1 << bit);
+}
+
+/* Find lowest and highest used offset in a byte array. */
+/* Returns 0 if and only if all bits are unset. */
 static int
-find_pos(char *s, unsigned len, int *low, int *high)
+find_pos(const UCHAR * const data,
+        const UINT sizeof_data,
+        UINT * const low,
+        UINT * const high)
 {
-  unsigned int i, j;
-  int l = 0;
+  const UINT last = (sizeof_data << 3) - 1;
+  UINT offset;
 
-  *high = -1;
-  *low = (int)(8 * len);
+  for (offset = last; !bit_is_set(data, offset); offset--)
+    if (!offset)               /* All bits are 0. */
+      return 0;
+  *high = offset;
 
-  for (i = 0; i < len; i++, s++)
+  for (offset = 0; !bit_is_set(data, offset); offset++)
     {
-      if (*s)
-       {
-         for (j = 0; j < 8 * sizeof(char); j++)
-
-           {
-             if (((little_endian && ((*s) & 0x01)) ||
-                  (!little_endian && ((*s) & 0x80))))
-               {
-                 if (l > *high)
-                   *high = l;
-                 if (l < *low)
-                   *low = l;
-               }
-             l++;
-             if (little_endian)
-               {
-                 *s >>= 1;
-               }
-             else
-               {
-                 *s = (char)(*s << 1);
-               }
-           }
-       }
-      else
-       l += 8;
     }
-  return (*high >= 0 && (*low <= *high)) ? *low : -1;
+  *low = offset;
+
+  return -1;
 }
 
 /*
@@ -115,36 +119,38 @@ find_pos(char *s, unsigned len, int *low, int *high)
  * record type defined in the binding.
  * We are only dealing with record types which are of 32 or 16
  * bit size, i.e. they fit into an (u)int or a (u)short.
+ * Any pair with a 0 attr field will be ignored.
  */
 static void
 gen_reps(
          const name_attribute_pair * nap,      /* array of name_attribute_pair records */
          const char *name,     /* name of the represented record type  */
-         int len,              /* size of the record in bytes          */
-         int bias)
+         const UINT len,       /* size of the record in bytes          */
+         const UINT bias)
 {
-  int i, n, l, cnt = 0, low, high;
+  const UINT len_bits = len << 3;
+  int i, l;
+  UINT low, high;
   int width = strlen(RES_NAME) + 3;
-  unsigned long a;
-  unsigned long mask = 0;
 
   assert(nap != NULL);
 
   for (i = 0; nap[i].name != (char *)0; i++)
-    {
-      cnt++;
-      l = (int)strlen(nap[i].name);
-      if (l > width)
-       width = l;
-    }
+    if (nap[i].attr)
+      {
+       l = (int)strlen(nap[i].name);
+       if (l > width)
+         width = l;
+      }
   assert(width > 0);
 
   printf("   type %s is\n", name);
   printf("      record\n");
   for (i = 0; nap[i].name != (char *)0; i++)
-    {
-      printf("         %-*s : Boolean;\n", width, nap[i].name);
-    }
+    if (nap[i].attr)
+      {
+       printf("         %-*s : Boolean;\n", width, nap[i].name);
+      }
   printf("      end record;\n");
   printf("   pragma Convention (C, %s);\n\n", name);
 
@@ -152,18 +158,16 @@ gen_reps(
   printf("      record\n");
 
   for (i = 0; nap[i].name != (char *)0; i++)
-    {
-      a = nap[i].attr;
-      mask |= a;
-      l = find_pos((char *)&a, sizeof(a), &low, &high);
-      if (l >= 0)
-       printf("         %-*s at 0 range %2d .. %2d;\n", width, nap[i].name,
-              low - bias, high - bias);
-    }
-  i = 1;
-  n = cnt;
+    if (nap[i].attr)
+      {
+       if (find_pos((const UCHAR *)(&(nap[i].attr)) + bias, len, &low, &high))
+         printf("         %-*s at 0 range %2d .. %2d;\n", width, nap[i].name,
+                low, high);
+      }
   printf("      end record;\n");
-  printf("   for %s'Size use %d;\n", name, 8 * len);
+  printf("   pragma Warnings (Off);");
+  printf("   for %s'Size use %d;\n", name, len_bits);
+  printf("   pragma Warnings (On);\n");
   printf("   --  Please note: this rep. clause is generated and may be\n");
   printf("   --               different on your system.");
 }
@@ -173,10 +177,9 @@ chtype_rep(const char *name, attr_t mask)
 {
   attr_t x = (attr_t)-1;
   attr_t t = x & mask;
-  int low, high;
-  int l = find_pos((char *)&t, sizeof(t), &low, &high);
+  UINT low, high;
 
-  if (l >= 0)
+  if (find_pos((const UCHAR *)(&t), sizeof(t), &low, &high))
     printf("         %-5s at 0 range %2d .. %2d;\n", name, low, high);
 }
 
@@ -197,10 +200,9 @@ gen_chtype_rep(const char *name)
 static void
 mrep_rep(const char *name, void *rec)
 {
-  int low, high;
-  int l = find_pos((char *)rec, sizeof(MEVENT), &low, &high);
+  UINT low, high;
 
-  if (l >= 0)
+  if (find_pos((const UCHAR *)rec, sizeof(MEVENT), &low, &high))
     printf("         %-7s at 0 range %3d .. %3d;\n", name, low, high);
 }
 
@@ -240,55 +242,30 @@ static void
 gen_attr_set(const char *name)
 {
   /* All of the A_xxx symbols are defined in ncurses, but not all are nonzero
-   * if "configure --enable-widec" is specified.
+   * if "configure --enable-widec" is not specified.  Originally (in
+   * 1999-2000), the ifdef's also were needed since the proposed bit-layout
+   * for wide characters allocated 16-bits for A_CHARTEXT, leaving too few
+   * bits for a few of the A_xxx symbols.
+   * Some preprocessors are not able to test the values because they
+   * now (2014) contain an explicit cast to chtype, so we avoid ifdef.
    */
   static const name_attribute_pair nap[] =
   {
-#if A_STANDOUT
     {"Stand_Out", A_STANDOUT},
-#endif
-#if A_UNDERLINE
     {"Under_Line", A_UNDERLINE},
-#endif
-#if A_REVERSE
     {"Reverse_Video", A_REVERSE},
-#endif
-#if A_BLINK
     {"Blink", A_BLINK},
-#endif
-#if A_DIM
     {"Dim_Character", A_DIM},
-#endif
-#if A_BOLD
     {"Bold_Character", A_BOLD},
-#endif
-#if A_ALTCHARSET
     {"Alternate_Character_Set", A_ALTCHARSET},
-#endif
-#if A_INVIS
     {"Invisible_Character", A_INVIS},
-#endif
-#if A_PROTECT
     {"Protected_Character", A_PROTECT},
-#endif
-#if A_HORIZONTAL
     {"Horizontal", A_HORIZONTAL},
-#endif
-#if A_LEFT
     {"Left", A_LEFT},
-#endif
-#if A_LOW
     {"Low", A_LOW},
-#endif
-#if A_RIGHT
     {"Right", A_RIGHT},
-#endif
-#if A_TOP
     {"Top", A_TOP},
-#endif
-#if A_VERTICAL
     {"Vertical", A_VERTICAL},
-#endif
     {(char *)0, 0}
   };
   chtype attr = A_ATTRIBUTES & ~A_COLOR;
@@ -311,7 +288,9 @@ gen_attr_set(const char *name)
        }
       attr = attr >> 1;
     }
-  gen_reps(nap, name, (len + 7) / 8, little_endian ? start : 0);
+  gen_reps(nap, name,
+          (UINT) ((len + 7) / 8),
+          (UINT) (little_endian ? start >> 3 : 0));
 }
 
 static void
@@ -331,9 +310,12 @@ gen_trace(const char *name)
     {"Internal_Calls", TRACE_ICALLS},
     {"Character_Calls", TRACE_CCALLS},
     {"Termcap_TermInfo", TRACE_DATABASE},
+    {"Attributes_And_Colors", TRACE_ATTRS},
     {(char *)0, 0}
   };
-  gen_reps(nap, name, sizeof(int), 0);
+
+  gen_reps(nap, name, sizeof(UINT),
+          little_endian ? 0 : sizeof(nap[0].attr) - sizeof(UINT));
 }
 
 static void
@@ -361,7 +343,9 @@ gen_menu_opt_rep(const char *name)
 #endif
     {(char *)0, 0}
   };
-  gen_reps(nap, name, sizeof(int), 0);
+
+  gen_reps(nap, name, sizeof(Menu_Options),
+          little_endian ? 0 : sizeof(nap[0].attr) - sizeof(Menu_Options));
 }
 
 static void
@@ -374,7 +358,9 @@ gen_item_opt_rep(const char *name)
 #endif
     {(char *)0, 0}
   };
-  gen_reps(nap, name, sizeof(int), 0);
+
+  gen_reps(nap, name, sizeof(Item_Options),
+          little_endian ? 0 : sizeof(nap[0].attr) - sizeof(Item_Options));
 }
 
 static void
@@ -390,7 +376,9 @@ gen_form_opt_rep(const char *name)
 #endif
     {(char *)0, 0}
   };
-  gen_reps(nap, name, sizeof(int), 0);
+
+  gen_reps(nap, name, sizeof(Form_Options),
+          little_endian ? 0 : sizeof(nap[0].attr) - sizeof(Form_Options));
 }
 
 /*
@@ -433,7 +421,9 @@ gen_field_opt_rep(const char *name)
 #endif
     {(char *)0, 0}
   };
-  gen_reps(nap, name, sizeof(int), 0);
+
+  gen_reps(nap, name, sizeof(Field_Options),
+          little_endian ? 0 : sizeof(nap[0].attr) - sizeof(Field_Options));
 }
 
 /*
@@ -445,13 +435,14 @@ keydef(const char *name, const char *old_name, int value, int mode)
   if (mode == 0)               /* Generate the new name */
     printf("   %-30s : constant Special_Key_Code := 8#%3o#;\n", name, value);
   else
-    {                          /* generate the old name, but only if it doesn't conflict with the old
-                                * name (Ada95 isn't case sensitive!)
-                                */
+    {
       const char *s = old_name;
       const char *t = name;
 
-      while (*s && *t && (toupper(*s++) == toupper(*t++)));
+      /* generate the old name, but only if it doesn't conflict with the old
+       * name (Ada95 isn't case sensitive!)
+       */
+      while (*s && *t && (toupper(UChar(*s++)) == toupper(UChar(*t++))));
       if (*s || *t)
        printf("   %-16s : Special_Key_Code renames %s;\n", old_name, name);
     }
@@ -768,10 +759,10 @@ gen_keydefs(int mode)
 static void
 acs_def(const char *name, chtype *a)
 {
-  int c = a - &acs_map[0];
+  int c = (int)(a - &acs_map[0]);
 
   printf("   %-24s : constant Character := ", name);
-  if (isprint(c) && (c != '`'))
+  if (isprint(UChar(c)) && (c != '`'))
     printf("'%c';\n", c);
   else
     printf("Character'Val (%d);\n", c);
@@ -1276,12 +1267,6 @@ gen_offsets(void)
   printf("   Sizeof%-*s : constant Natural := %2ld; --  %s\n",
         12, "_bool", (long)sizeof(bool), "bool");
 
-  /* In ncurses _maxy and _maxx needs an offset for the "public"
-   * value
-   */
-  printf("   Offset%-*s : constant Natural := %2d; --  %s\n",
-        12, "_XY", 1, "int");
-  printf("\n");
   printf("   type Curses_Bool is mod 2 ** Interfaces.C.%s'Size;\n", s_bool);
 }
 
@@ -1495,7 +1480,7 @@ main(int argc, char *argv[])
              }
            printf("   subtype Eti_Error is C_Int range %d .. %d;\n\n",
                   etimin, etimax);
-           printf(buf);
+           printf("%s", buf);
          }
          break;
        default: