1% This file parses an ncurses Caps file, which determines the terminfo
2% binary layout.
3
4% Caps file format:
5% Column 1: terminfo variable name
6% Column 2: terminfo capability name
7% Column 3: capability type (boolean, numeric, or string)
8% Column 4: termcap capability name
9% Column 5: KEY_xxx name, if any, `-' otherwise
10% Column 6: value for KEY_xxx name, if any, `-' otherwise
11% Column 7: Lead with `Y' if capability should be emitted in termcap
12%           translations, `-' otherwise
13% Column 8: capability description
14% Note: Stop parsing the file when a line contains %%-STOP-HERE-%%
15
16private variable Stop_String = "%%-STOP-HERE-%%";
17
18define read_caps_file (file)
19{
20   variable fp = fopen (file, "r");
21   if (fp == NULL)
22     {
23	() = fprintf (stderr, "Unable to open %s\n", file);
24	exit (1);
25     }
26   variable lines = fgetslines (fp);
27   variable i = wherefirst (1 == array_map (Int_Type, &is_substr, lines, Stop_String));
28   if (i != NULL)
29     lines = lines[[:i]];
30
31   variable s = struct
32     {
33	name, ti_cap, type, tc_cap, keyname, keyval, flags, desc
34     };
35
36   variable fmt = "%s %s %s %s %s %s %s %[^\n]";
37   () = readascii (lines, &s.name, &s.ti_cap, &s.type, &s.tc_cap,
38		   &s.keyname, &s.keyval, &s.flags, &s.desc;
39		   format=fmt, comment="#");
40
41   return s;
42}
43
44private define write_table (fp, s, type, tblname)
45{
46
47   variable i = where (s.type == type);
48   variable cap = s.tc_cap[i];
49   variable comment = s.desc[i];
50   variable ofs = [0:length(cap)-1];
51   i = array_sort (cap); cap = cap[i]; comment = comment[i]; ofs = ofs[i];
52
53   () = fprintf (fp, "static Tgetstr_Map_Type %s[] = \n{\n", tblname);
54   _for i (0, length (cap)-1, 1)
55     {
56	() = fprintf (fp, "   {\"%s\", %3d\t\tUNTIC_COMMENT(\"%s\")},\n",
57		     cap[i], ofs[i], comment[i]);
58     }
59   () = fprintf (fp, "   {\"%s\", %3d\t\tUNTIC_COMMENT(\"%s\")},\n",
60		 "", -1, "NULL");
61   () = fprintf (fp, "};\n\n\n");
62}
63
64define slsh_main ()
65{
66   if (__argc != 2)
67     {
68	() = fprintf (stderr, "Usage: %s Caps-file > outfile\n", __argv[0]);
69	exit (1);
70     }
71   variable s = read_caps_file (__argv[1]);
72   variable fp = stdout;
73
74   () = fprintf (stdout, "/* This file was autogenerated using %s */\n\n",
75		 strjoin (__argv, " "));
76
77   write_table (fp, s, "str", "Tgetstr_Map");
78   write_table (fp, s, "num", "Tgetnum_Map");
79   write_table (fp, s, "bool", "Tgetflag_Map");
80}
81