3 # text2c.sh - convert a text file to C code
5 # Copyright (C) 2008 Phil Sutter <phil@nwl.cc>
7 # This program is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation, either version 3 of the License, or (at your option)
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 # You should have received a copy of the GNU General Public License along
18 # with this program. If not, see <http://www.gnu.org/licenses/>.
21 # Invocation is as follows:
24 # $3: name of variable
26 # The output will be a char **, with each field containing a single line of $1
27 # additionally, a macro with the name print_$3 will be defined, with acts as
28 # a parameter-less function, printing the text to stdout
31 echo "Usage: `basename $0` <inputfile> <outputfile> <variablename>"
35 outupper="`basename "$2" | tr '[a-z-.]' '[A-Z__]'`"
38 printf "#ifndef __%s\n" "$outupper"
39 printf "#define __%s\n" "$outupper"
40 printf "const char *%s[] = {\n" "$3"
41 sed -e 's/"/\\"/g' -e 's/^/"/' -e 's/$/",/' $1
43 printf "#define print_%s() { \\" $3
44 printf "\n\tconst char **__s = %s; \\" $3
45 printf "\n\tfor (; *__s; __s++) \\"
46 printf "\n\t\tputs(*__s); \\"
48 printf "#endif /* __%s */\n" "$outupper"