1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/***
File melt-make-string.c [making C constant strings from arguments]
Copyright (C) 2011, 2012 Free Software Foundation, Inc.
Indented with GNU indent.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>.
***/
/**
This is a self contained utility program. When given a list of pairs
as program argument, it outputs on stdout a sequence of C constant
strings. So, if invoked as:
melt-make-string foo abc bar bcd
it produces on stdout:
const char foo[]="abc";
const char bar[]="bcd";
However, it handles special characters correctly, e.g.
melt-make-string foo 'a"'
gives rightly, since the single quote is handled by the shell
const char foo[]="a\"";
and so on.
**/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void output_cstr (const char *s);
/* output a C string with escapes when needed. */
void
output_cstr (const char *s)
{
char c;
for (s = s; (c = (*s)) != (char) 0; s++)
{
switch (c)
{
case '\n':
fputs ("\\n", stdout);
break;
case '\r':
fputs ("\\r", stdout);
break;
case '\t':
fputs ("\\t", stdout);
break;
case '\b':
fputs ("\\b", stdout);
break;
case '\v':
fputs ("\\v", stdout);
break;
case '\f':
fputs ("\\f", stdout);
break;
case '\\':
fputs ("\\\\", stdout);
break;
case '\'':
fputs ("\\\'", stdout);
break;
case '\"':
fputs ("\\\"", stdout);
break;
case '/':
case '*':
case '+':
case '-':
case '%':
case '$':
case ' ':
putc (c, stdout);
break;
default:
if (c < 0x7f && isprint (c))
putc (c, stdout);
else
printf ("\\%03o", (int) (c & 0xff));
break;
}
}
}
int
main (int argc, char **argv)
{
int ix = 0;
for (ix = 1; (ix + 1) < argc; ix += 2)
{
const char *name = argv[ix];
const char *str = argv[ix + 1];
const char *ps = NULL;
int badname = 0;
if (!name || !str)
break;
badname = !isalpha (name[0]);
for (ps = name; !badname && *ps; ps++)
if (isalnum (*ps) || *ps == '_')
continue;
else
badname = 1;
if (badname)
{
printf ("#error bad name %s\n", name);
fprintf (stderr, "bad name #%d: %s\n", ix, name);
exit (1);
}
puts ("#ifdef __cplusplus");
puts (" extern \"C\"");
puts ("#endif /*__cplusplus*/");
printf ("const char %s[]=\"", name);
output_cstr (str);
fputs ("\";\n", stdout);
}
if (fflush (stdout))
{
perror ("failed to flush stdout");
exit (1);
};
return 0;
}
|