-
Notifications
You must be signed in to change notification settings - Fork 0
/
quinine.sh
executable file
·160 lines (144 loc) · 3.81 KB
/
quinine.sh
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
# Quinine makes a quine using tar. To find out more, call the program with no
# arguments, or with the -h or --help flag.
#
# Author: Peter Boothe <[email protected]> 2 May 2015
# First shown to the world at !!Con 2015
#
# This code is licensed CC:0 - do anything you want with it.
# http://creativecommons.org/publicdomain/zero/1.0/
# I enjoyed writing it, and after having fun, I am done. That said, it would be
# wonderful to find out it was actually useful, so it would be nice to hear
# from you if you use it!
LANGUAGE=C
HEADER=
CODE=
FUNCTION=
HELP=
ERROR=0
if [ $? != 0 ]
then
ERROR=1
fi
while [ "${1:0:1}" = "-" ]
do
case "$1"
in
--help)
HELP=1; shift;;
-l|--lang)
LANGUAGE="$2"; shift; shift;;
-h|--header)
HEADER="$2"; shift; shift;;
-c|--code)
CODE="$2"; shift; shift;;
-f|--function)
FUNCTION="$2"; shift; shift;;
*)
echo "Unknown option: $i" 1>&2; ERROR=2; break;;
esac
done
if [ "x$*" = x ]
then
echo "No files specified" 1>&2
ERROR=1
fi
if [ "x${FUNCTION}" = x ]; then
FUNCTION=quinine_print_src
fi
case $LANGUAGE
in
c|C)
if [ "x${HEADER}" = x ]; then
HEADER=quinine.h
fi
if [ "x${CODE}" = x ]; then
CODE=quinine.c
fi;;
python)
if [ "x${CODE}" = x ]; then
CODE=quinine.py
fi;;
*)
ERROR=1;;
esac
if [ x$HELP != x ] || [ $ERROR != 0 ]
then
cat 1>&2 <<EOF
NAME
quinine
SYNOPSIS
quinine --lang=c --header=quinine.h --code=quinine.c file1 ... fileN
DESCRIPTION
quinine is a script for creating library files that, together
with the contained function quinine_print_src(), which will print,
to stdout, a tar archive of the source that makes up the passed-in
program. This allows open source programs to carry their source
with themselves, which helps add a lot of missing promise to the
idea of open source. It also allows people to see the *exact*
source code which created their program.
The following options are available:
--help Print this help/usage message
-l/--lang LANGUAGE
LANGUAGE can be any of {C, python}. By default it is C.
-h/--header FILE
For languages that have separate definitions and declarations
(e.g. C and C++), the header argument specifies the name
of the header file where the code should be declared (but
not defined).
-c/--code FILE
Specifies the file for the code that defines the print function
-f/--function STRING
The name of the function to create in the code file.
By default, the program acts as if it was invoked as:
quinine --lang C --header quinine.h --code quinine.c \\
--function quinine_print_src
Examples:
Get help
quinine --help
Generate C/C++ code
quinine --lang=C --header=quinine.h --code=quinine.c file1 ... fileN
Generate Python code
quinine --lang=python --code=quinine.py file1 ... fileN
EOF
exit $ERROR
fi
# If we get to here, everything is well-formed and we actually have work to do
case ${LANGUAGE}
in
c|C)
cat > ${HEADER} <<EOF
#ifndef __${HEADER/[.\\]/_}__
#define __${HEADER/[.\\]/_}__
// Print out all of the source code in:
// $*
void ${FUNCTION}();
#endif
EOF
(
echo "#include <stdio.h>"
echo
echo "static char *${FUNCTION}_string = "
tar cz $* | base64 -b 70 | sed -e 's/^/ "/' -e 's/$/"/'
echo " ;"
echo
echo "void ${FUNCTION}() {"
echo " printf(\"%s\", ${FUNCTION}_string);"
echo "}"
) > ${CODE};;
python)
(
echo "def ${FUNCTION}():"
echo " 'Print out base64 encoded, tarred, zipped program'"
printf " print \""
tar cz $* | base64 | sed -e 's/$/"/g'
echo
cat - <<EOF
if __name__ == '__main__':
${FUNCTION}()
EOF
) > ${CODE};;
*)
echo UNKOWN LANGUAGE AFTER CHECK - THIS SHOULD NEVER HAPPEN 1>&2
exit 100
esac