]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/gen/gen.c
86d3b69748476865fdba05fabda165435c5b0d9f
[ncurses.git] / Ada95 / gen / gen.c
1 /****************************************************************************
2  * Copyright (c) 1998,2010,2011 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *   Author:  Juergen Pfeifer, 1996                                         *
31  ****************************************************************************/
32
33 /*
34     Version Control
35     $Id: gen.c,v 1.55 2011/03/05 20:24:22 tom Exp $
36   --------------------------------------------------------------------------*/
37 /*
38   This program generates various record structures and constants from the
39   ncurses header file for the Ada95 packages. Essentially it produces
40   Ada95 source on stdout, which is then merged using m4 into a template
41   to produce the real source.
42   */
43
44 #ifdef HAVE_CONFIG_H
45 #include <ncurses_cfg.h>
46 #else
47 #include <ncurses.h>
48 #define HAVE_USE_DEFAULT_COLORS 1
49 #endif
50
51 #include <stdlib.h>
52 #include <stddef.h>
53 #include <string.h>
54 #include <assert.h>
55 #include <ctype.h>
56
57 #include <menu.h>
58 #include <form.h>
59
60 #define UChar(c)        ((unsigned char)(c))
61 #define RES_NAME "Reserved"
62
63 static const char *model = "";
64 static int little_endian = 0;
65
66 typedef struct
67   {
68     const char *name;
69     unsigned long attr;
70   }
71 name_attribute_pair;
72
73 static int
74 find_pos(char *s, unsigned len, int *low, int *high)
75 {
76   unsigned int i, j;
77   int l = 0;
78
79   *high = -1;
80   *low = (int)(8 * len);
81
82   for (i = 0; i < len; i++, s++)
83     {
84       if (*s)
85         {
86           for (j = 0; j < 8 * sizeof(char); j++)
87
88             {
89               if (((little_endian && ((*s) & 0x01)) ||
90                    (!little_endian && ((*s) & 0x80))))
91                 {
92                   if (l > *high)
93                     *high = l;
94                   if (l < *low)
95                     *low = l;
96                 }
97               l++;
98               if (little_endian)
99                 {
100                   *s >>= 1;
101                 }
102               else
103                 {
104                   *s = (char)(*s << 1);
105                 }
106             }
107         }
108       else
109         l += 8;
110     }
111   return (*high >= 0 && (*low <= *high)) ? *low : -1;
112 }
113
114 /*
115  * This helper routine generates a representation clause for a
116  * record type defined in the binding.
117  * We are only dealing with record types which are of 32 or 16
118  * bit size, i.e. they fit into an (u)int or a (u)short.
119  */
120 static void
121 gen_reps(
122           const name_attribute_pair * nap,      /* array of name_attribute_pair records */
123           const char *name,     /* name of the represented record type  */
124           int len,              /* size of the record in bytes          */
125           int bias)
126 {
127   const char *unused_name = "Unused";
128   int long_bits = (8 * (int)sizeof(unsigned long));
129   int len_bits = (8 * len);
130   int i, j, n, l, cnt = 0, low, high;
131   int width = strlen(RES_NAME) + 3;
132   unsigned long a;
133   unsigned long mask = 0;
134
135   assert(nap != NULL);
136
137   for (i = 0; nap[i].name != (char *)0; i++)
138     {
139       cnt++;
140       l = (int)strlen(nap[i].name);
141       if (l > width)
142         width = l;
143     }
144   assert(width > 0);
145
146   printf("   type %s is\n", name);
147   printf("      record\n");
148   for (i = 0; nap[i].name != (char *)0; i++)
149     {
150       mask |= nap[i].attr;
151       printf("         %-*s : Boolean;\n", width, nap[i].name);
152     }
153
154   /*
155    * Compute a mask for the unused bits in this target.
156    */
157   mask = ~mask;
158   /*
159    * Bits in the biased area are unused by the target.
160    */
161   for (j = 0; j < bias; ++j)
162     {
163       mask &= (unsigned long)(~(1L << j));
164     }
165   /*
166    * Bits past the target's size are really unused.
167    */
168   for (j = len_bits + bias; j < long_bits; ++j)
169     {
170       mask &= (unsigned long)(~(1L << j));
171     }
172   if (mask != 0)
173     {
174       printf("         %-*s : Boolean;\n", width, unused_name);
175     }
176   printf("      end record;\n");
177   printf("   pragma Convention (C, %s);\n\n", name);
178
179   printf("   for %s use\n", name);
180   printf("      record\n");
181
182   for (i = 0; nap[i].name != (char *)0; i++)
183     {
184       a = nap[i].attr;
185       l = find_pos((char *)&a, sizeof(a), &low, &high);
186       if (l >= 0)
187         printf("         %-*s at 0 range %2d .. %2d;\n", width, nap[i].name,
188                low - bias, high - bias);
189     }
190   if (mask != 0)
191     {
192       l = find_pos((char *)&mask, sizeof(mask), &low, &high);
193       if (l >= 0)
194         printf("         %-*s at 0 range %2d .. %2d;\n", width, unused_name,
195                low - bias, high - bias);
196     }
197   i = 1;
198   n = cnt;
199   printf("      end record;\n");
200   printf("   for %s'Size use %d;\n", name, len_bits);
201   printf("   --  Please note: this rep. clause is generated and may be\n");
202   printf("   --               different on your system.");
203 }
204
205 static void
206 chtype_rep(const char *name, attr_t mask)
207 {
208   attr_t x = (attr_t)-1;
209   attr_t t = x & mask;
210   int low, high;
211   int l = find_pos((char *)&t, sizeof(t), &low, &high);
212
213   if (l >= 0)
214     printf("         %-5s at 0 range %2d .. %2d;\n", name, low, high);
215 }
216
217 static void
218 gen_chtype_rep(const char *name)
219 {
220   printf("   for %s use\n      record\n", name);
221   chtype_rep("Ch", A_CHARTEXT);
222   chtype_rep("Color", A_COLOR);
223   chtype_rep("Attr", (A_ATTRIBUTES & ~A_COLOR));
224   printf("      end record;\n   for %s'Size use %ld;\n",
225          name, (long)(8 * sizeof(chtype)));
226
227   printf("      --  Please note: this rep. clause is generated and may be\n");
228   printf("      --               different on your system.\n");
229 }
230
231 static void
232 mrep_rep(const char *name, void *rec)
233 {
234   int low, high;
235   int l = find_pos((char *)rec, sizeof(MEVENT), &low, &high);
236
237   if (l >= 0)
238     printf("         %-7s at 0 range %3d .. %3d;\n", name, low, high);
239 }
240
241 static void
242 gen_mrep_rep(const char *name)
243 {
244   MEVENT x;
245
246   printf("   for %s use\n      record\n", name);
247
248   memset(&x, 0, sizeof(x));
249   x.id = -1;
250   mrep_rep("Id", &x);
251
252   memset(&x, 0, sizeof(x));
253   x.x = -1;
254   mrep_rep("X", &x);
255
256   memset(&x, 0, sizeof(x));
257   x.y = -1;
258   mrep_rep("Y", &x);
259
260   memset(&x, 0, sizeof(x));
261   x.z = -1;
262   mrep_rep("Z", &x);
263
264   memset(&x, 0, sizeof(x));
265   x.bstate = (mmask_t) - 1;
266   mrep_rep("Bstate", &x);
267
268   printf("      end record;\n");
269   printf("      --  Please note: this rep. clause is generated and may be\n");
270   printf("      --               different on your system.\n");
271 }
272
273 static void
274 gen_attr_set(const char *name)
275 {
276   /* All of the A_xxx symbols are defined in ncurses, but not all are nonzero
277    * if "configure --enable-widec" is specified.
278    */
279   static const name_attribute_pair nap[] =
280   {
281 #if A_STANDOUT
282     {"Stand_Out", A_STANDOUT},
283 #endif
284 #if A_UNDERLINE
285     {"Under_Line", A_UNDERLINE},
286 #endif
287 #if A_REVERSE
288     {"Reverse_Video", A_REVERSE},
289 #endif
290 #if A_BLINK
291     {"Blink", A_BLINK},
292 #endif
293 #if A_DIM
294     {"Dim_Character", A_DIM},
295 #endif
296 #if A_BOLD
297     {"Bold_Character", A_BOLD},
298 #endif
299 #if A_ALTCHARSET
300     {"Alternate_Character_Set", A_ALTCHARSET},
301 #endif
302 #if A_INVIS
303     {"Invisible_Character", A_INVIS},
304 #endif
305 #if A_PROTECT
306     {"Protected_Character", A_PROTECT},
307 #endif
308 #if A_HORIZONTAL
309     {"Horizontal", A_HORIZONTAL},
310 #endif
311 #if A_LEFT
312     {"Left", A_LEFT},
313 #endif
314 #if A_LOW
315     {"Low", A_LOW},
316 #endif
317 #if A_RIGHT
318     {"Right", A_RIGHT},
319 #endif
320 #if A_TOP
321     {"Top", A_TOP},
322 #endif
323 #if A_VERTICAL
324     {"Vertical", A_VERTICAL},
325 #endif
326     {(char *)0, 0}
327   };
328   chtype attr = A_ATTRIBUTES & ~A_COLOR;
329   int start = -1;
330   int len = 0;
331   int i;
332   chtype set;
333   for (i = 0; i < (int)(8 * sizeof(chtype)); i++)
334
335     {
336       set = (attr & 1);
337       if (set)
338         {
339           if (start < 0)
340             start = i;
341           if (start >= 0)
342             {
343               len++;
344             }
345         }
346       attr = attr >> 1;
347     }
348   gen_reps(nap, name, (len + 7) / 8, little_endian ? start : 0);
349 }
350
351 static void
352 gen_trace(const char *name)
353 {
354   static const name_attribute_pair nap[] =
355   {
356     {"Times", TRACE_TIMES},
357     {"Tputs", TRACE_TPUTS},
358     {"Update", TRACE_UPDATE},
359     {"Cursor_Move", TRACE_MOVE},
360     {"Character_Output", TRACE_CHARPUT},
361     {"Calls", TRACE_CALLS},
362     {"Virtual_Puts", TRACE_VIRTPUT},
363     {"Input_Events", TRACE_IEVENT},
364     {"TTY_State", TRACE_BITS},
365     {"Internal_Calls", TRACE_ICALLS},
366     {"Character_Calls", TRACE_CCALLS},
367     {"Termcap_TermInfo", TRACE_DATABASE},
368     {(char *)0, 0}
369   };
370   gen_reps(nap, name, sizeof(int), 0);
371 }
372
373 static void
374 gen_menu_opt_rep(const char *name)
375 {
376   static const name_attribute_pair nap[] =
377   {
378 #ifdef O_ONEVALUE
379     {"One_Valued", O_ONEVALUE},
380 #endif
381 #ifdef O_SHOWDESC
382     {"Show_Descriptions", O_SHOWDESC},
383 #endif
384 #ifdef O_ROWMAJOR
385     {"Row_Major_Order", O_ROWMAJOR},
386 #endif
387 #ifdef O_IGNORECASE
388     {"Ignore_Case", O_IGNORECASE},
389 #endif
390 #ifdef O_SHOWMATCH
391     {"Show_Matches", O_SHOWMATCH},
392 #endif
393 #ifdef O_NONCYCLIC
394     {"Non_Cyclic", O_NONCYCLIC},
395 #endif
396     {(char *)0, 0}
397   };
398   gen_reps(nap, name, sizeof(int), 0);
399 }
400
401 static void
402 gen_item_opt_rep(const char *name)
403 {
404   static const name_attribute_pair nap[] =
405   {
406 #ifdef O_SELECTABLE
407     {"Selectable", O_SELECTABLE},
408 #endif
409     {(char *)0, 0}
410   };
411   gen_reps(nap, name, sizeof(int), 0);
412 }
413
414 static void
415 gen_form_opt_rep(const char *name)
416 {
417   static const name_attribute_pair nap[] =
418   {
419 #ifdef O_NL_OVERLOAD
420     {"NL_Overload", O_NL_OVERLOAD},
421 #endif
422 #ifdef O_BS_OVERLOAD
423     {"BS_Overload", O_BS_OVERLOAD},
424 #endif
425     {(char *)0, 0}
426   };
427   gen_reps(nap, name, sizeof(int), 0);
428 }
429
430 /*
431  * Generate the representation clause for the Field_Option_Set record
432  */
433 static void
434 gen_field_opt_rep(const char *name)
435 {
436   static const name_attribute_pair nap[] =
437   {
438 #ifdef O_VISIBLE
439     {"Visible", O_VISIBLE},
440 #endif
441 #ifdef O_ACTIVE
442     {"Active", O_ACTIVE},
443 #endif
444 #ifdef O_PUBLIC
445     {"Public", O_PUBLIC},
446 #endif
447 #ifdef O_EDIT
448     {"Edit", O_EDIT},
449 #endif
450 #ifdef O_WRAP
451     {"Wrap", O_WRAP},
452 #endif
453 #ifdef O_BLANK
454     {"Blank", O_BLANK},
455 #endif
456 #ifdef O_AUTOSKIP
457     {"Auto_Skip", O_AUTOSKIP},
458 #endif
459 #ifdef O_NULLOK
460     {"Null_Ok", O_NULLOK},
461 #endif
462 #ifdef O_PASSOK
463     {"Pass_Ok", O_PASSOK},
464 #endif
465 #ifdef O_STATIC
466     {"Static", O_STATIC},
467 #endif
468     {(char *)0, 0}
469   };
470   gen_reps(nap, name, sizeof(int), 0);
471 }
472
473 /*
474  * Generate a single key code constant definition.
475  */
476 static void
477 keydef(const char *name, const char *old_name, int value, int mode)
478 {
479   if (mode == 0)                /* Generate the new name */
480     printf("   %-30s : constant Special_Key_Code := 8#%3o#;\n", name, value);
481   else
482     {
483       const char *s = old_name;
484       const char *t = name;
485
486       /* generate the old name, but only if it doesn't conflict with the old
487        * name (Ada95 isn't case sensitive!)
488        */
489       while (*s && *t && (toupper(UChar(*s++)) == toupper(UChar(*t++))));
490       if (*s || *t)
491         printf("   %-16s : Special_Key_Code renames %s;\n", old_name, name);
492     }
493 }
494
495 /*
496  * Generate constants for the key codes. When called with mode==0, a
497  * complete list with nice constant names in proper casing style will
498  * be generated. Otherwise a list of old (i.e. C-style) names will be
499  * generated, given that the name wasn't already defined in the "nice"
500  * list.
501  */
502 static void
503 gen_keydefs(int mode)
504 {
505   char buf[16];
506   char obuf[16];
507   int i;
508
509 #ifdef KEY_CODE_YES
510   keydef("Key_Code_Yes", "KEY_CODE_YES", KEY_CODE_YES, mode);
511 #endif
512 #ifdef KEY_MIN
513   keydef("Key_Min", "KEY_MIN", KEY_MIN, mode);
514 #endif
515 #ifdef KEY_BREAK
516   keydef("Key_Break", "KEY_BREAK", KEY_BREAK, mode);
517 #endif
518 #ifdef KEY_DOWN
519   keydef("Key_Cursor_Down", "KEY_DOWN", KEY_DOWN, mode);
520 #endif
521 #ifdef KEY_UP
522   keydef("Key_Cursor_Up", "KEY_UP", KEY_UP, mode);
523 #endif
524 #ifdef KEY_LEFT
525   keydef("Key_Cursor_Left", "KEY_LEFT", KEY_LEFT, mode);
526 #endif
527 #ifdef KEY_RIGHT
528   keydef("Key_Cursor_Right", "KEY_RIGHT", KEY_RIGHT, mode);
529 #endif
530 #ifdef KEY_HOME
531   keydef("Key_Home", "KEY_HOME", KEY_HOME, mode);
532 #endif
533 #ifdef KEY_BACKSPACE
534   keydef("Key_Backspace", "KEY_BACKSPACE", KEY_BACKSPACE, mode);
535 #endif
536 #ifdef KEY_F0
537   keydef("Key_F0", "KEY_F0", KEY_F0, mode);
538 #endif
539 #ifdef KEY_F
540   for (i = 1; i <= 24; i++)
541     {
542       sprintf(buf, "Key_F%d", i);
543       sprintf(obuf, "KEY_F%d", i);
544       keydef(buf, obuf, KEY_F(i), mode);
545     }
546 #endif
547 #ifdef KEY_DL
548   keydef("Key_Delete_Line", "KEY_DL", KEY_DL, mode);
549 #endif
550 #ifdef KEY_IL
551   keydef("Key_Insert_Line", "KEY_IL", KEY_IL, mode);
552 #endif
553 #ifdef KEY_DC
554   keydef("Key_Delete_Char", "KEY_DC", KEY_DC, mode);
555 #endif
556 #ifdef KEY_IC
557   keydef("Key_Insert_Char", "KEY_IC", KEY_IC, mode);
558 #endif
559 #ifdef KEY_EIC
560   keydef("Key_Exit_Insert_Mode", "KEY_EIC", KEY_EIC, mode);
561 #endif
562 #ifdef KEY_CLEAR
563   keydef("Key_Clear_Screen", "KEY_CLEAR", KEY_CLEAR, mode);
564 #endif
565 #ifdef KEY_EOS
566   keydef("Key_Clear_End_Of_Screen", "KEY_EOS", KEY_EOS, mode);
567 #endif
568 #ifdef KEY_EOL
569   keydef("Key_Clear_End_Of_Line", "KEY_EOL", KEY_EOL, mode);
570 #endif
571 #ifdef KEY_SF
572   keydef("Key_Scroll_1_Forward", "KEY_SF", KEY_SF, mode);
573 #endif
574 #ifdef KEY_SR
575   keydef("Key_Scroll_1_Backward", "KEY_SR", KEY_SR, mode);
576 #endif
577 #ifdef KEY_NPAGE
578   keydef("Key_Next_Page", "KEY_NPAGE", KEY_NPAGE, mode);
579 #endif
580 #ifdef KEY_PPAGE
581   keydef("Key_Previous_Page", "KEY_PPAGE", KEY_PPAGE, mode);
582 #endif
583 #ifdef KEY_STAB
584   keydef("Key_Set_Tab", "KEY_STAB", KEY_STAB, mode);
585 #endif
586 #ifdef KEY_CTAB
587   keydef("Key_Clear_Tab", "KEY_CTAB", KEY_CTAB, mode);
588 #endif
589 #ifdef KEY_CATAB
590   keydef("Key_Clear_All_Tabs", "KEY_CATAB", KEY_CATAB, mode);
591 #endif
592 #ifdef KEY_ENTER
593   keydef("Key_Enter_Or_Send", "KEY_ENTER", KEY_ENTER, mode);
594 #endif
595 #ifdef KEY_SRESET
596   keydef("Key_Soft_Reset", "KEY_SRESET", KEY_SRESET, mode);
597 #endif
598 #ifdef KEY_RESET
599   keydef("Key_Reset", "KEY_RESET", KEY_RESET, mode);
600 #endif
601 #ifdef KEY_PRINT
602   keydef("Key_Print", "KEY_PRINT", KEY_PRINT, mode);
603 #endif
604 #ifdef KEY_LL
605   keydef("Key_Bottom", "KEY_LL", KEY_LL, mode);
606 #endif
607 #ifdef KEY_A1
608   keydef("Key_Upper_Left_Of_Keypad", "KEY_A1", KEY_A1, mode);
609 #endif
610 #ifdef KEY_A3
611   keydef("Key_Upper_Right_Of_Keypad", "KEY_A3", KEY_A3, mode);
612 #endif
613 #ifdef KEY_B2
614   keydef("Key_Center_Of_Keypad", "KEY_B2", KEY_B2, mode);
615 #endif
616 #ifdef KEY_C1
617   keydef("Key_Lower_Left_Of_Keypad", "KEY_C1", KEY_C1, mode);
618 #endif
619 #ifdef KEY_C3
620   keydef("Key_Lower_Right_Of_Keypad", "KEY_C3", KEY_C3, mode);
621 #endif
622 #ifdef KEY_BTAB
623   keydef("Key_Back_Tab", "KEY_BTAB", KEY_BTAB, mode);
624 #endif
625 #ifdef KEY_BEG
626   keydef("Key_Beginning", "KEY_BEG", KEY_BEG, mode);
627 #endif
628 #ifdef KEY_CANCEL
629   keydef("Key_Cancel", "KEY_CANCEL", KEY_CANCEL, mode);
630 #endif
631 #ifdef KEY_CLOSE
632   keydef("Key_Close", "KEY_CLOSE", KEY_CLOSE, mode);
633 #endif
634 #ifdef KEY_COMMAND
635   keydef("Key_Command", "KEY_COMMAND", KEY_COMMAND, mode);
636 #endif
637 #ifdef KEY_COPY
638   keydef("Key_Copy", "KEY_COPY", KEY_COPY, mode);
639 #endif
640 #ifdef KEY_CREATE
641   keydef("Key_Create", "KEY_CREATE", KEY_CREATE, mode);
642 #endif
643 #ifdef KEY_END
644   keydef("Key_End", "KEY_END", KEY_END, mode);
645 #endif
646 #ifdef KEY_EXIT
647   keydef("Key_Exit", "KEY_EXIT", KEY_EXIT, mode);
648 #endif
649 #ifdef KEY_FIND
650   keydef("Key_Find", "KEY_FIND", KEY_FIND, mode);
651 #endif
652 #ifdef KEY_HELP
653   keydef("Key_Help", "KEY_HELP", KEY_HELP, mode);
654 #endif
655 #ifdef KEY_MARK
656   keydef("Key_Mark", "KEY_MARK", KEY_MARK, mode);
657 #endif
658 #ifdef KEY_MESSAGE
659   keydef("Key_Message", "KEY_MESSAGE", KEY_MESSAGE, mode);
660 #endif
661 #ifdef KEY_MOVE
662   keydef("Key_Move", "KEY_MOVE", KEY_MOVE, mode);
663 #endif
664 #ifdef KEY_NEXT
665   keydef("Key_Next", "KEY_NEXT", KEY_NEXT, mode);
666 #endif
667 #ifdef KEY_OPEN
668   keydef("Key_Open", "KEY_OPEN", KEY_OPEN, mode);
669 #endif
670 #ifdef KEY_OPTIONS
671   keydef("Key_Options", "KEY_OPTIONS", KEY_OPTIONS, mode);
672 #endif
673 #ifdef KEY_PREVIOUS
674   keydef("Key_Previous", "KEY_PREVIOUS", KEY_PREVIOUS, mode);
675 #endif
676 #ifdef KEY_REDO
677   keydef("Key_Redo", "KEY_REDO", KEY_REDO, mode);
678 #endif
679 #ifdef KEY_REFERENCE
680   keydef("Key_Reference", "KEY_REFERENCE", KEY_REFERENCE, mode);
681 #endif
682 #ifdef KEY_REFRESH
683   keydef("Key_Refresh", "KEY_REFRESH", KEY_REFRESH, mode);
684 #endif
685 #ifdef KEY_REPLACE
686   keydef("Key_Replace", "KEY_REPLACE", KEY_REPLACE, mode);
687 #endif
688 #ifdef KEY_RESTART
689   keydef("Key_Restart", "KEY_RESTART", KEY_RESTART, mode);
690 #endif
691 #ifdef KEY_RESUME
692   keydef("Key_Resume", "KEY_RESUME", KEY_RESUME, mode);
693 #endif
694 #ifdef KEY_SAVE
695   keydef("Key_Save", "KEY_SAVE", KEY_SAVE, mode);
696 #endif
697 #ifdef KEY_SBEG
698   keydef("Key_Shift_Begin", "KEY_SBEG", KEY_SBEG, mode);
699 #endif
700 #ifdef KEY_SCANCEL
701   keydef("Key_Shift_Cancel", "KEY_SCANCEL", KEY_SCANCEL, mode);
702 #endif
703 #ifdef KEY_SCOMMAND
704   keydef("Key_Shift_Command", "KEY_SCOMMAND", KEY_SCOMMAND, mode);
705 #endif
706 #ifdef KEY_SCOPY
707   keydef("Key_Shift_Copy", "KEY_SCOPY", KEY_SCOPY, mode);
708 #endif
709 #ifdef KEY_SCREATE
710   keydef("Key_Shift_Create", "KEY_SCREATE", KEY_SCREATE, mode);
711 #endif
712 #ifdef KEY_SDC
713   keydef("Key_Shift_Delete_Char", "KEY_SDC", KEY_SDC, mode);
714 #endif
715 #ifdef KEY_SDL
716   keydef("Key_Shift_Delete_Line", "KEY_SDL", KEY_SDL, mode);
717 #endif
718 #ifdef KEY_SELECT
719   keydef("Key_Select", "KEY_SELECT", KEY_SELECT, mode);
720 #endif
721 #ifdef KEY_SEND
722   keydef("Key_Shift_End", "KEY_SEND", KEY_SEND, mode);
723 #endif
724 #ifdef KEY_SEOL
725   keydef("Key_Shift_Clear_End_Of_Line", "KEY_SEOL", KEY_SEOL, mode);
726 #endif
727 #ifdef KEY_SEXIT
728   keydef("Key_Shift_Exit", "KEY_SEXIT", KEY_SEXIT, mode);
729 #endif
730 #ifdef KEY_SFIND
731   keydef("Key_Shift_Find", "KEY_SFIND", KEY_SFIND, mode);
732 #endif
733 #ifdef KEY_SHELP
734   keydef("Key_Shift_Help", "KEY_SHELP", KEY_SHELP, mode);
735 #endif
736 #ifdef KEY_SHOME
737   keydef("Key_Shift_Home", "KEY_SHOME", KEY_SHOME, mode);
738 #endif
739 #ifdef KEY_SIC
740   keydef("Key_Shift_Insert_Char", "KEY_SIC", KEY_SIC, mode);
741 #endif
742 #ifdef KEY_SLEFT
743   keydef("Key_Shift_Cursor_Left", "KEY_SLEFT", KEY_SLEFT, mode);
744 #endif
745 #ifdef KEY_SMESSAGE
746   keydef("Key_Shift_Message", "KEY_SMESSAGE", KEY_SMESSAGE, mode);
747 #endif
748 #ifdef KEY_SMOVE
749   keydef("Key_Shift_Move", "KEY_SMOVE", KEY_SMOVE, mode);
750 #endif
751 #ifdef KEY_SNEXT
752   keydef("Key_Shift_Next_Page", "KEY_SNEXT", KEY_SNEXT, mode);
753 #endif
754 #ifdef KEY_SOPTIONS
755   keydef("Key_Shift_Options", "KEY_SOPTIONS", KEY_SOPTIONS, mode);
756 #endif
757 #ifdef KEY_SPREVIOUS
758   keydef("Key_Shift_Previous_Page", "KEY_SPREVIOUS", KEY_SPREVIOUS, mode);
759 #endif
760 #ifdef KEY_SPRINT
761   keydef("Key_Shift_Print", "KEY_SPRINT", KEY_SPRINT, mode);
762 #endif
763 #ifdef KEY_SREDO
764   keydef("Key_Shift_Redo", "KEY_SREDO", KEY_SREDO, mode);
765 #endif
766 #ifdef KEY_SREPLACE
767   keydef("Key_Shift_Replace", "KEY_SREPLACE", KEY_SREPLACE, mode);
768 #endif
769 #ifdef KEY_SRIGHT
770   keydef("Key_Shift_Cursor_Right", "KEY_SRIGHT", KEY_SRIGHT, mode);
771 #endif
772 #ifdef KEY_SRSUME
773   keydef("Key_Shift_Resume", "KEY_SRSUME", KEY_SRSUME, mode);
774 #endif
775 #ifdef KEY_SSAVE
776   keydef("Key_Shift_Save", "KEY_SSAVE", KEY_SSAVE, mode);
777 #endif
778 #ifdef KEY_SSUSPEND
779   keydef("Key_Shift_Suspend", "KEY_SSUSPEND", KEY_SSUSPEND, mode);
780 #endif
781 #ifdef KEY_SUNDO
782   keydef("Key_Shift_Undo", "KEY_SUNDO", KEY_SUNDO, mode);
783 #endif
784 #ifdef KEY_SUSPEND
785   keydef("Key_Suspend", "KEY_SUSPEND", KEY_SUSPEND, mode);
786 #endif
787 #ifdef KEY_UNDO
788   keydef("Key_Undo", "KEY_UNDO", KEY_UNDO, mode);
789 #endif
790 #ifdef KEY_MOUSE
791   keydef("Key_Mouse", "KEY_MOUSE", KEY_MOUSE, mode);
792 #endif
793 #ifdef KEY_RESIZE
794   keydef("Key_Resize", "KEY_RESIZE", KEY_RESIZE, mode);
795 #endif
796 }
797
798 /*
799  * Generate a constant with the given name. The second parameter
800  * is a reference to the ACS character in the acs_map[] array and
801  * will be translated into an index.
802  */
803 static void
804 acs_def(const char *name, chtype *a)
805 {
806   int c = a - &acs_map[0];
807
808   printf("   %-24s : constant Character := ", name);
809   if (isprint(UChar(c)) && (c != '`'))
810     printf("'%c';\n", c);
811   else
812     printf("Character'Val (%d);\n", c);
813 }
814
815 /*
816  * Generate the constants for the ACS characters
817  */
818 static void
819 gen_acs(void)
820 {
821   printf("   type C_ACS_Map is array (Character'Val (0) .. Character'Val (127))\n");
822   printf("        of Attributed_Character;\n");
823 #if USE_REENTRANT || BROKEN_LINKER
824   printf("   type C_ACS_Ptr is access C_ACS_Map;\n");
825   printf("   function ACS_Map return C_ACS_Ptr;\n");
826   printf("   pragma Import (C, ACS_Map, \""
827          NCURSES_WRAP_PREFIX
828          "acs_map\");\n");
829 #else
830   printf("   ACS_Map : C_ACS_Map;\n");
831   printf("   pragma Import (C, ACS_Map, \"acs_map\");\n");
832 #endif
833   printf("   --\n");
834   printf("   --\n");
835   printf("   --  Constants for several characters from the Alternate Character Set\n");
836   printf("   --  You must use these constants as indices into the ACS_Map array\n");
837   printf("   --  to get the corresponding attributed character at runtime.\n");
838   printf("   --\n");
839
840 #ifdef ACS_ULCORNER
841   acs_def("ACS_Upper_Left_Corner", &ACS_ULCORNER);
842 #endif
843 #ifdef ACS_LLCORNER
844   acs_def("ACS_Lower_Left_Corner", &ACS_LLCORNER);
845 #endif
846 #ifdef ACS_URCORNER
847   acs_def("ACS_Upper_Right_Corner", &ACS_URCORNER);
848 #endif
849 #ifdef ACS_LRCORNER
850   acs_def("ACS_Lower_Right_Corner", &ACS_LRCORNER);
851 #endif
852 #ifdef ACS_LTEE
853   acs_def("ACS_Left_Tee", &ACS_LTEE);
854 #endif
855 #ifdef ACS_RTEE
856   acs_def("ACS_Right_Tee", &ACS_RTEE);
857 #endif
858 #ifdef ACS_BTEE
859   acs_def("ACS_Bottom_Tee", &ACS_BTEE);
860 #endif
861 #ifdef ACS_TTEE
862   acs_def("ACS_Top_Tee", &ACS_TTEE);
863 #endif
864 #ifdef ACS_HLINE
865   acs_def("ACS_Horizontal_Line", &ACS_HLINE);
866 #endif
867 #ifdef ACS_VLINE
868   acs_def("ACS_Vertical_Line", &ACS_VLINE);
869 #endif
870 #ifdef ACS_PLUS
871   acs_def("ACS_Plus_Symbol", &ACS_PLUS);
872 #endif
873 #ifdef ACS_S1
874   acs_def("ACS_Scan_Line_1", &ACS_S1);
875 #endif
876 #ifdef ACS_S9
877   acs_def("ACS_Scan_Line_9", &ACS_S9);
878 #endif
879 #ifdef ACS_DIAMOND
880   acs_def("ACS_Diamond", &ACS_DIAMOND);
881 #endif
882 #ifdef ACS_CKBOARD
883   acs_def("ACS_Checker_Board", &ACS_CKBOARD);
884 #endif
885 #ifdef ACS_DEGREE
886   acs_def("ACS_Degree", &ACS_DEGREE);
887 #endif
888 #ifdef ACS_PLMINUS
889   acs_def("ACS_Plus_Minus", &ACS_PLMINUS);
890 #endif
891 #ifdef ACS_BULLET
892   acs_def("ACS_Bullet", &ACS_BULLET);
893 #endif
894 #ifdef ACS_LARROW
895   acs_def("ACS_Left_Arrow", &ACS_LARROW);
896 #endif
897 #ifdef ACS_RARROW
898   acs_def("ACS_Right_Arrow", &ACS_RARROW);
899 #endif
900 #ifdef ACS_DARROW
901   acs_def("ACS_Down_Arrow", &ACS_DARROW);
902 #endif
903 #ifdef ACS_UARROW
904   acs_def("ACS_Up_Arrow", &ACS_UARROW);
905 #endif
906 #ifdef ACS_BOARD
907   acs_def("ACS_Board_Of_Squares", &ACS_BOARD);
908 #endif
909 #ifdef ACS_LANTERN
910   acs_def("ACS_Lantern", &ACS_LANTERN);
911 #endif
912 #ifdef ACS_BLOCK
913   acs_def("ACS_Solid_Block", &ACS_BLOCK);
914 #endif
915 #ifdef ACS_S3
916   acs_def("ACS_Scan_Line_3", &ACS_S3);
917 #endif
918 #ifdef ACS_S7
919   acs_def("ACS_Scan_Line_7", &ACS_S7);
920 #endif
921 #ifdef ACS_LEQUAL
922   acs_def("ACS_Less_Or_Equal", &ACS_LEQUAL);
923 #endif
924 #ifdef ACS_GEQUAL
925   acs_def("ACS_Greater_Or_Equal", &ACS_GEQUAL);
926 #endif
927 #ifdef ACS_PI
928   acs_def("ACS_PI", &ACS_PI);
929 #endif
930 #ifdef ACS_NEQUAL
931   acs_def("ACS_Not_Equal", &ACS_NEQUAL);
932 #endif
933 #ifdef ACS_STERLING
934   acs_def("ACS_Sterling", &ACS_STERLING);
935 #endif
936 }
937
938 #define GEN_EVENT(name,value) \
939    printf("   %-25s : constant Event_Mask := 8#%011lo#;\n", \
940           #name, value)
941
942 #define GEN_MEVENT(name) \
943    printf("   %-25s : constant Event_Mask := 8#%011lo#;\n", \
944           #name, name)
945
946 static void
947 gen_mouse_events(void)
948 {
949   mmask_t all1 = 0;
950   mmask_t all2 = 0;
951   mmask_t all3 = 0;
952   mmask_t all4 = 0;
953
954 #ifdef BUTTON1_RELEASED
955   GEN_MEVENT(BUTTON1_RELEASED);
956   all1 |= BUTTON1_RELEASED;
957 #endif
958 #ifdef BUTTON1_PRESSED
959   GEN_MEVENT(BUTTON1_PRESSED);
960   all1 |= BUTTON1_PRESSED;
961 #endif
962 #ifdef BUTTON1_CLICKED
963   GEN_MEVENT(BUTTON1_CLICKED);
964   all1 |= BUTTON1_CLICKED;
965 #endif
966 #ifdef BUTTON1_DOUBLE_CLICKED
967   GEN_MEVENT(BUTTON1_DOUBLE_CLICKED);
968   all1 |= BUTTON1_DOUBLE_CLICKED;
969 #endif
970 #ifdef BUTTON1_TRIPLE_CLICKED
971   GEN_MEVENT(BUTTON1_TRIPLE_CLICKED);
972   all1 |= BUTTON1_TRIPLE_CLICKED;
973 #endif
974 #ifdef BUTTON1_RESERVED_EVENT
975   GEN_MEVENT(BUTTON1_RESERVED_EVENT);
976   all1 |= BUTTON1_RESERVED_EVENT;
977 #endif
978 #ifdef BUTTON2_RELEASED
979   GEN_MEVENT(BUTTON2_RELEASED);
980   all2 |= BUTTON2_RELEASED;
981 #endif
982 #ifdef BUTTON2_PRESSED
983   GEN_MEVENT(BUTTON2_PRESSED);
984   all2 |= BUTTON2_PRESSED;
985 #endif
986 #ifdef BUTTON2_CLICKED
987   GEN_MEVENT(BUTTON2_CLICKED);
988   all2 |= BUTTON2_CLICKED;
989 #endif
990 #ifdef BUTTON2_DOUBLE_CLICKED
991   GEN_MEVENT(BUTTON2_DOUBLE_CLICKED);
992   all2 |= BUTTON2_DOUBLE_CLICKED;
993 #endif
994 #ifdef BUTTON2_TRIPLE_CLICKED
995   GEN_MEVENT(BUTTON2_TRIPLE_CLICKED);
996   all2 |= BUTTON2_TRIPLE_CLICKED;
997 #endif
998 #ifdef BUTTON2_RESERVED_EVENT
999   GEN_MEVENT(BUTTON2_RESERVED_EVENT);
1000   all2 |= BUTTON2_RESERVED_EVENT;
1001 #endif
1002 #ifdef BUTTON3_RELEASED
1003   GEN_MEVENT(BUTTON3_RELEASED);
1004   all3 |= BUTTON3_RELEASED;
1005 #endif
1006 #ifdef BUTTON3_PRESSED
1007   GEN_MEVENT(BUTTON3_PRESSED);
1008   all3 |= BUTTON3_PRESSED;
1009 #endif
1010 #ifdef BUTTON3_CLICKED
1011   GEN_MEVENT(BUTTON3_CLICKED);
1012   all3 |= BUTTON3_CLICKED;
1013 #endif
1014 #ifdef BUTTON3_DOUBLE_CLICKED
1015   GEN_MEVENT(BUTTON3_DOUBLE_CLICKED);
1016   all3 |= BUTTON3_DOUBLE_CLICKED;
1017 #endif
1018 #ifdef BUTTON3_TRIPLE_CLICKED
1019   GEN_MEVENT(BUTTON3_TRIPLE_CLICKED);
1020   all3 |= BUTTON3_TRIPLE_CLICKED;
1021 #endif
1022 #ifdef BUTTON3_RESERVED_EVENT
1023   GEN_MEVENT(BUTTON3_RESERVED_EVENT);
1024   all3 |= BUTTON3_RESERVED_EVENT;
1025 #endif
1026 #ifdef BUTTON4_RELEASED
1027   GEN_MEVENT(BUTTON4_RELEASED);
1028   all4 |= BUTTON4_RELEASED;
1029 #endif
1030 #ifdef BUTTON4_PRESSED
1031   GEN_MEVENT(BUTTON4_PRESSED);
1032   all4 |= BUTTON4_PRESSED;
1033 #endif
1034 #ifdef BUTTON4_CLICKED
1035   GEN_MEVENT(BUTTON4_CLICKED);
1036   all4 |= BUTTON4_CLICKED;
1037 #endif
1038 #ifdef BUTTON4_DOUBLE_CLICKED
1039   GEN_MEVENT(BUTTON4_DOUBLE_CLICKED);
1040   all4 |= BUTTON4_DOUBLE_CLICKED;
1041 #endif
1042 #ifdef BUTTON4_TRIPLE_CLICKED
1043   GEN_MEVENT(BUTTON4_TRIPLE_CLICKED);
1044   all4 |= BUTTON4_TRIPLE_CLICKED;
1045 #endif
1046 #ifdef BUTTON4_RESERVED_EVENT
1047   GEN_MEVENT(BUTTON4_RESERVED_EVENT);
1048   all4 |= BUTTON4_RESERVED_EVENT;
1049 #endif
1050 #ifdef BUTTON_CTRL
1051   GEN_MEVENT(BUTTON_CTRL);
1052 #endif
1053 #ifdef BUTTON_SHIFT
1054   GEN_MEVENT(BUTTON_SHIFT);
1055 #endif
1056 #ifdef BUTTON_ALT
1057   GEN_MEVENT(BUTTON_ALT);
1058 #endif
1059 #ifdef REPORT_MOUSE_POSITION
1060   GEN_MEVENT(REPORT_MOUSE_POSITION);
1061 #endif
1062 #ifdef ALL_MOUSE_EVENTS
1063   GEN_MEVENT(ALL_MOUSE_EVENTS);
1064 #endif
1065
1066   GEN_EVENT(BUTTON1_EVENTS, all1);
1067   GEN_EVENT(BUTTON2_EVENTS, all2);
1068   GEN_EVENT(BUTTON3_EVENTS, all3);
1069   GEN_EVENT(BUTTON4_EVENTS, all4);
1070 }
1071
1072 static void
1073 wrap_one_var(const char *c_var,
1074              const char *c_type,
1075              const char *ada_func,
1076              const char *ada_type)
1077 {
1078 #if USE_REENTRANT
1079   /* must wrap variables */
1080   printf("\n");
1081   printf("   function %s return %s\n", ada_func, ada_type);
1082   printf("   is\n");
1083   printf("      function Result return %s;\n", c_type);
1084   printf("      pragma Import (C, Result, \"" NCURSES_WRAP_PREFIX "%s\");\n", c_var);
1085   printf("   begin\n");
1086   if (strcmp(c_type, ada_type))
1087     printf("      return %s (Result);\n", ada_type);
1088   else
1089     printf("      return Result;\n");
1090   printf("   end %s;\n", ada_func);
1091 #else
1092   /* global variables are really global */
1093   printf("\n");
1094   printf("   function %s return %s\n", ada_func, ada_type);
1095   printf("   is\n");
1096   printf("      Result : %s;\n", c_type);
1097   printf("      pragma Import (C, Result, \"%s\");\n", c_var);
1098   printf("   begin\n");
1099   if (strcmp(c_type, ada_type))
1100     printf("      return %s (Result);\n", ada_type);
1101   else
1102     printf("      return Result;\n");
1103   printf("   end %s;\n", ada_func);
1104 #endif
1105 }
1106
1107 #define GEN_PUBLIC_VAR(c_var, c_type, ada_func, ada_type) \
1108         wrap_one_var(#c_var, #c_type, #ada_func, #ada_type)
1109
1110 static void
1111 gen_public_vars(void)
1112 {
1113   GEN_PUBLIC_VAR(stdscr, Window, Standard_Window, Window);
1114   GEN_PUBLIC_VAR(curscr, Window, Current_Window, Window);
1115   GEN_PUBLIC_VAR(LINES, C_Int, Lines, Line_Count);
1116   GEN_PUBLIC_VAR(COLS, C_Int, Columns, Column_Count);
1117   GEN_PUBLIC_VAR(TABSIZE, C_Int, Tab_Size, Natural);
1118   GEN_PUBLIC_VAR(COLORS, C_Int, Number_Of_Colors, Natural);
1119   GEN_PUBLIC_VAR(COLOR_PAIRS, C_Int, Number_Of_Color_Pairs, Natural);
1120 }
1121
1122 /*
1123  * Output some comment lines indicating that the file is generated.
1124  * The name parameter is the name of the facility to be used in
1125  * the comment.
1126  */
1127 static void
1128 prologue(const char *name)
1129 {
1130   printf("--  %s binding.\n", name);
1131   printf("--  This module is generated. Please don't change it manually!\n");
1132   printf("--  Run the generator instead.\n--  |");
1133
1134   printf("define(`M4_BIT_ORDER',`%s_Order_First')",
1135          little_endian ? "Low" : "High");
1136 }
1137
1138 /*
1139  * Write the prologue for the curses facility and make sure that
1140  * KEY_MIN and KEY_MAX are defined for the rest of this source.
1141  */
1142 static void
1143 basedefs(void)
1144 {
1145   prologue("curses");
1146 #ifndef KEY_MAX
1147 #  define KEY_MAX 0777
1148 #endif
1149   printf("define(`M4_KEY_MAX',`8#%o#')", KEY_MAX);
1150 #ifndef KEY_MIN
1151 #  define KEY_MIN 0401
1152 #endif
1153   if (KEY_MIN == 256)
1154     {
1155       fprintf(stderr, "Unexpected value for KEY_MIN: %d\n", KEY_MIN);
1156       exit(1);
1157     }
1158   printf("define(`M4_SPECIAL_FIRST',`8#%o#')", KEY_MIN - 1);
1159 }
1160
1161 /*
1162  * Write out the comment lines for the menu facility
1163  */
1164 static void
1165 menu_basedefs(void)
1166 {
1167   prologue("menu");
1168 }
1169
1170 /*
1171  * Write out the comment lines for the form facility
1172  */
1173 static void
1174 form_basedefs(void)
1175 {
1176   prologue("form");
1177 }
1178
1179 /*
1180  * Write out the comment lines for the mouse facility
1181  */
1182 static void
1183 mouse_basedefs(void)
1184 {
1185   prologue("mouse");
1186 }
1187
1188 /*
1189  * Write the definition of a single color
1190  */
1191 static void
1192 color_def(const char *name, int value)
1193 {
1194   printf("   %-16s : constant Color_Number := %d;\n", name, value);
1195 }
1196
1197 /*
1198  * Generate all color definitions
1199  */
1200 static void
1201 gen_color(void)
1202 {
1203 #if HAVE_USE_DEFAULT_COLORS
1204   color_def("Default_Color", -1);
1205 #endif
1206 #ifdef COLOR_BLACK
1207   color_def("Black", COLOR_BLACK);
1208 #endif
1209 #ifdef COLOR_RED
1210   color_def("Red", COLOR_RED);
1211 #endif
1212 #ifdef COLOR_GREEN
1213   color_def("Green", COLOR_GREEN);
1214 #endif
1215 #ifdef COLOR_YELLOW
1216   color_def("Yellow", COLOR_YELLOW);
1217 #endif
1218 #ifdef COLOR_BLUE
1219   color_def("Blue", COLOR_BLUE);
1220 #endif
1221 #ifdef COLOR_MAGENTA
1222   color_def("Magenta", COLOR_MAGENTA);
1223 #endif
1224 #ifdef COLOR_CYAN
1225   color_def("Cyan", COLOR_CYAN);
1226 #endif
1227 #ifdef COLOR_WHITE
1228   color_def("White", COLOR_WHITE);
1229 #endif
1230 }
1231
1232 /*
1233  * Generate the linker options for the base facility
1234  */
1235 static void
1236 gen_linkopts(void)
1237 {
1238   printf("   pragma Linker_Options (\"-lncurses%s\");\n", model);
1239 }
1240
1241 /*
1242  * Generate the linker options for the menu facility
1243  */
1244 static void
1245 gen_menu_linkopts(void)
1246 {
1247   printf("   pragma Linker_Options (\"-lmenu%s\");\n", model);
1248 }
1249
1250 /*
1251  * Generate the linker options for the form facility
1252  */
1253 static void
1254 gen_form_linkopts(void)
1255 {
1256   printf("   pragma Linker_Options (\"-lform%s\");\n", model);
1257 }
1258
1259 /*
1260  * Generate the linker options for the panel facility
1261  */
1262 static void
1263 gen_panel_linkopts(void)
1264 {
1265   printf("   pragma Linker_Options (\"-lpanel%s\");\n", model);
1266 }
1267
1268 static void
1269 gen_version_info(void)
1270 {
1271   static const char *v1 =
1272   "   NC_Major_Version : constant := %d; --  Major version of the library\n";
1273   static const char *v2 =
1274   "   NC_Minor_Version : constant := %d; --  Minor version of the library\n";
1275   static const char *v3 =
1276   "   NC_Version : constant String := %c%d.%d%c;  --  Version of library\n";
1277
1278   printf(v1, NCURSES_VERSION_MAJOR);
1279   printf(v2, NCURSES_VERSION_MINOR);
1280   printf(v3, '"', NCURSES_VERSION_MAJOR, NCURSES_VERSION_MINOR, '"');
1281 }
1282
1283 static int
1284 eti_gen(char *buf, int code, const char *name, int *etimin, int *etimax)
1285 {
1286   sprintf(buf, "   E_%-16s : constant Eti_Error := %d;\n", name, code);
1287   if (code < *etimin)
1288     *etimin = code;
1289   if (code > *etimax)
1290     *etimax = code;
1291   return (int)strlen(buf);
1292 }
1293
1294 static void
1295 gen_offsets(void)
1296 {
1297   const char *s_bool = "";
1298
1299   if (sizeof(bool) == sizeof(char))
1300     {
1301       s_bool = "char";
1302     }
1303   else if (sizeof(bool) == sizeof(short))
1304     {
1305       s_bool = "short";
1306     }
1307   else if (sizeof(bool) == sizeof(int))
1308     {
1309       s_bool = "int";
1310     }
1311   printf("   Sizeof%-*s : constant Natural := %2ld; --  %s\n",
1312          12, "_bool", (long)sizeof(bool), "bool");
1313
1314   /* In ncurses _maxy and _maxx needs an offset for the "public"
1315    * value
1316    */
1317   printf("   Offset%-*s : constant Natural := %2d; --  %s\n",
1318          12, "_XY", 1, "int");
1319   printf("\n");
1320   printf("   type Curses_Bool is mod 2 ** Interfaces.C.%s'Size;\n", s_bool);
1321 }
1322
1323 /*
1324  * main() expects two arguments on the commandline, both single characters.
1325  * The first character denotes the facility for which we generate output.
1326  * Possible values are
1327  *   B - Base
1328  *   M - Menus
1329  *   F - Forms
1330  *   P - Pointer Device (Mouse)
1331  *   E - ETI base definitions
1332  *
1333  * The second character then denotes the specific output that should be
1334  * generated for the selected facility.
1335  */
1336 int
1337 main(int argc, char *argv[])
1338 {
1339   int x = 0x12345678;
1340   char *s = (char *)&x;
1341
1342   if (*s == 0x78)
1343     little_endian = 1;
1344
1345   if (argc != 4)
1346     exit(1);
1347   model = *++argv;
1348
1349   switch (argv[1][0])
1350     {
1351       /* --------------------------------------------------------------- */
1352     case 'B':                   /* The Base facility */
1353       switch (argv[2][0])
1354         {
1355         case 'A':               /* chtype translation into Ada95 record type */
1356           gen_attr_set("Character_Attribute_Set");
1357           break;
1358         case 'B':               /* write some initial comment lines */
1359           basedefs();
1360           break;
1361         case 'C':               /* generate color constants */
1362           gen_color();
1363           break;
1364         case 'D':               /* generate displacements of fields in WINDOW struct. */
1365           gen_offsets();
1366           break;
1367         case 'E':               /* generate Mouse Event codes */
1368           gen_mouse_events();
1369           break;
1370         case 'K':               /* translation of keycodes */
1371           gen_keydefs(0);
1372           break;
1373         case 'L':               /* generate the Linker_Options pragma */
1374           gen_linkopts();
1375           break;
1376         case 'M':               /* generate constants for the ACS characters */
1377           gen_acs();
1378           break;
1379         case 'O':               /* generate definitions of the old key code names */
1380           gen_keydefs(1);
1381           break;
1382         case 'P':               /* generate definitions of the public variables */
1383           gen_public_vars();
1384           break;
1385         case 'R':               /* generate representation clause for Attributed character */
1386           gen_chtype_rep("Attributed_Character");
1387           break;
1388         case 'T':               /* generate the Trace info */
1389           gen_trace("Trace_Attribute_Set");
1390           break;
1391         case 'V':               /* generate version info */
1392           gen_version_info();
1393           break;
1394         default:
1395           break;
1396         }
1397       break;
1398       /* --------------------------------------------------------------- */
1399     case 'M':                   /* The Menu facility */
1400       switch (argv[2][0])
1401         {
1402         case 'R':               /* generate representation clause for Menu_Option_Set */
1403           gen_menu_opt_rep("Menu_Option_Set");
1404           break;
1405         case 'B':               /* write some initial comment lines */
1406           menu_basedefs();
1407           break;
1408         case 'L':               /* generate the Linker_Options pragma */
1409           gen_menu_linkopts();
1410           break;
1411         case 'I':               /* generate representation clause for Item_Option_Set */
1412           gen_item_opt_rep("Item_Option_Set");
1413           break;
1414         default:
1415           break;
1416         }
1417       break;
1418       /* --------------------------------------------------------------- */
1419     case 'F':                   /* The Form facility */
1420       switch (argv[2][0])
1421         {
1422         case 'R':               /* generate representation clause for Form_Option_Set */
1423           gen_form_opt_rep("Form_Option_Set");
1424           break;
1425         case 'B':               /* write some initial comment lines */
1426           form_basedefs();
1427           break;
1428         case 'L':               /* generate the Linker_Options pragma */
1429           gen_form_linkopts();
1430           break;
1431         case 'I':               /* generate representation clause for Field_Option_Set */
1432           gen_field_opt_rep("Field_Option_Set");
1433           break;
1434         default:
1435           break;
1436         }
1437       break;
1438       /* --------------------------------------------------------------- */
1439     case 'P':                   /* The Pointer(=Mouse) facility */
1440       switch (argv[2][0])
1441         {
1442         case 'B':               /* write some initial comment lines */
1443           mouse_basedefs();
1444           break;
1445         case 'M':               /* generate representation clause for Mouse_Event */
1446           gen_mrep_rep("Mouse_Event");
1447           break;
1448         case 'L':               /* generate the Linker_Options pragma */
1449           gen_panel_linkopts();
1450           break;
1451         default:
1452           break;
1453         }
1454       break;
1455       /* --------------------------------------------------------------- */
1456     case 'E':                   /* chtype size detection */
1457       switch (argv[2][0])
1458         {
1459         case 'C':
1460           {
1461             const char *fmt = "   type    C_Chtype   is new %s;\n";
1462             const char *afmt = "   type    C_AttrType is new %s;\n";
1463
1464             if (sizeof(chtype) == sizeof(int))
1465               {
1466                 if (sizeof(int) == sizeof(long))
1467                     printf(fmt, "C_ULong");
1468
1469                 else
1470                   printf(fmt, "C_UInt");
1471               }
1472             else if (sizeof(chtype) == sizeof(long))
1473               {
1474                 printf(fmt, "C_ULong");
1475               }
1476             else
1477               printf("Error\n");
1478
1479             if (sizeof(attr_t) == sizeof(int))
1480               {
1481                 if (sizeof(int) == sizeof(long))
1482                     printf(afmt, "C_ULong");
1483
1484                 else
1485                   printf(afmt, "C_UInt");
1486               }
1487             else if (sizeof(attr_t) == sizeof(long))
1488               {
1489                 printf(afmt, "C_ULong");
1490               }
1491             else
1492               printf("Error\n");
1493
1494             printf("define(`CF_CURSES_OK',`%d')", OK);
1495             printf("define(`CF_CURSES_ERR',`%d')", ERR);
1496             printf("define(`CF_CURSES_TRUE',`%d')", TRUE);
1497             printf("define(`CF_CURSES_FALSE',`%d')", FALSE);
1498           }
1499           break;
1500         case 'E':
1501           {
1502             char *buf = (char *)malloc(2048);
1503             char *p = buf;
1504             int etimin = E_OK;
1505             int etimax = E_OK;
1506
1507             if (p)
1508               {
1509                 p += eti_gen(p, E_OK, "Ok", &etimin, &etimax);
1510                 p += eti_gen(p, E_SYSTEM_ERROR, "System_Error", &etimin, &etimax);
1511                 p += eti_gen(p, E_BAD_ARGUMENT, "Bad_Argument", &etimin, &etimax);
1512                 p += eti_gen(p, E_POSTED, "Posted", &etimin, &etimax);
1513                 p += eti_gen(p, E_CONNECTED, "Connected", &etimin, &etimax);
1514                 p += eti_gen(p, E_BAD_STATE, "Bad_State", &etimin, &etimax);
1515                 p += eti_gen(p, E_NO_ROOM, "No_Room", &etimin, &etimax);
1516                 p += eti_gen(p, E_NOT_POSTED, "Not_Posted", &etimin, &etimax);
1517                 p += eti_gen(p, E_UNKNOWN_COMMAND,
1518                              "Unknown_Command", &etimin, &etimax);
1519                 p += eti_gen(p, E_NO_MATCH, "No_Match", &etimin, &etimax);
1520                 p += eti_gen(p, E_NOT_SELECTABLE,
1521                              "Not_Selectable", &etimin, &etimax);
1522                 p += eti_gen(p, E_NOT_CONNECTED,
1523                              "Not_Connected", &etimin, &etimax);
1524                 p += eti_gen(p, E_REQUEST_DENIED,
1525                              "Request_Denied", &etimin, &etimax);
1526                 p += eti_gen(p, E_INVALID_FIELD,
1527                              "Invalid_Field", &etimin, &etimax);
1528                 p += eti_gen(p, E_CURRENT,
1529                              "Current", &etimin, &etimax);
1530               }
1531             printf("   subtype Eti_Error is C_Int range %d .. %d;\n\n",
1532                    etimin, etimax);
1533             printf("%s", buf);
1534           }
1535           break;
1536         default:
1537           break;
1538         }
1539       break;
1540       /* --------------------------------------------------------------- */
1541     case 'V':                   /* plain version dump */
1542       {
1543         switch (argv[2][0])
1544           {
1545           case '1':             /* major version */
1546 #ifdef NCURSES_VERSION_MAJOR
1547             printf("%d", NCURSES_VERSION_MAJOR);
1548 #endif
1549             break;
1550           case '2':             /* minor version */
1551 #ifdef NCURSES_VERSION_MINOR
1552             printf("%d", NCURSES_VERSION_MINOR);
1553 #endif
1554             break;
1555           case '3':             /* patch level */
1556 #ifdef NCURSES_VERSION_PATCH
1557             printf("%d", NCURSES_VERSION_PATCH);
1558 #endif
1559             break;
1560           default:
1561             break;
1562           }
1563       }
1564       break;
1565       /* --------------------------------------------------------------- */
1566     default:
1567       break;
1568     }
1569   return 0;
1570 }