This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
die() no longer propagates outside of a Safe compartment
[perl5.git] / generate_uudmap.c
1 /* Originally this program just generated uudmap.h
2    However, when we later wanted to generate bitcount.h, it was easier to
3    refactor it and keep the same name, than either alternative - rename it,
4    or duplicate all of the Makefile logic for a second program.  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 /* If it turns out that we need to make this conditional on config.sh derived
9    values, it might be easier just to rip out the use of strerrer().  */
10 #include <string.h>
11 /* If a platform doesn't support errno.h, it's probably so strange that
12    "hello world" won't port easily to it.  */
13 #include <errno.h>
14
15 void output_block_to_file(const char *progname, const char *filename,
16                           const char *block, size_t count) {
17   FILE *const out = fopen(filename, "w");
18
19   if (!out) {
20     fprintf(stderr, "%s: Could not open '%s': %s\n", progname, filename,
21             strerror(errno));
22     exit(1);
23   }
24
25   fputs("{\n    ", out);
26   while (count--) {
27     fprintf(out, "%d", *block);
28     block++;
29     if (count) {
30       fputs(", ", out);
31       if (!(count & 15)) {
32         fputs("\n    ", out);
33       }
34     }
35   }
36   fputs("\n}\n", out);
37
38   if (fclose(out)) {
39     fprintf(stderr, "%s: Could not close '%s': %s\n", progname, filename,
40             strerror(errno));
41     exit(1);
42   }
43 }
44
45
46 static const char PL_uuemap[]
47 = "`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
48
49 typedef unsigned char U8;
50
51 /* This will ensure it is all zeros.  */
52 static char PL_uudmap[256];
53 static char PL_bitcount[256];
54
55 int main(int argc, char **argv) {
56   size_t i;
57   int bits;
58
59   if (argc < 3 || argv[1][0] == '\0' || argv[2][0] == '\0') {
60     fprintf(stderr, "Usage: %s uudemap.h bitcount.h\n", argv[0]);
61     return 1;
62   }
63
64   for (i = 0; i < sizeof(PL_uuemap) - 1; ++i)
65     PL_uudmap[(U8)PL_uuemap[i]] = (char)i;
66   /*
67    * Because ' ' and '`' map to the same value,
68    * we need to decode them both the same.
69    */
70   PL_uudmap[(U8)' '] = 0;
71
72   output_block_to_file(argv[0], argv[1], PL_uudmap, sizeof(PL_uudmap));
73
74   for (bits = 1; bits < 256; bits++) {
75     if (bits & 1)       PL_bitcount[bits]++;
76     if (bits & 2)       PL_bitcount[bits]++;
77     if (bits & 4)       PL_bitcount[bits]++;
78     if (bits & 8)       PL_bitcount[bits]++;
79     if (bits & 16)      PL_bitcount[bits]++;
80     if (bits & 32)      PL_bitcount[bits]++;
81     if (bits & 64)      PL_bitcount[bits]++;
82     if (bits & 128)     PL_bitcount[bits]++;
83   }
84
85   output_block_to_file(argv[0], argv[2], PL_bitcount, sizeof(PL_bitcount));
86
87   return 0;
88 }
89
90