3 * $Id: qqlib.c,v 1.1 1999/04/28 19:58:07 mdw Exp $
5 * Useful library of tools for quines
7 * (c) 1999 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Quine
14 * Quine is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Quine is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Quine; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * However, when this source text is embedded in a file by the Quine
29 * program, you may do anything you like with the resulting text;
30 * distribution and modification are not limited by the General Public
34 /*----- Revision history --------------------------------------------------*
37 * Revision 1.1 1999/04/28 19:58:07 mdw
42 /*----- Header files ------------------------------------------------------*/
44 /* --- ANSI headers --- */
52 /* --- POSIX headers --- */
54 #ifndef QUINE_PORTABLE
55 # include <sys/types.h>
56 # include <sys/fcntl.h>
57 # include <sys/stat.h>
61 /* --- Local headers --- */
65 /*----- Macro hacking -----------------------------------------------------*/
75 /*----- Main code ---------------------------------------------------------*/
77 /* --- @qq_xlate@ --- *
79 * Arguments: @FILE *fp@ = output file handle
80 * @const char *p@ = pointer to encoded text
84 * Use: Writes the decoded string to the given file handle.
87 void qq_xlate(FILE *fp, const char *p)
93 case 'q': fputc('\"', fp); break;
94 case 'b': fputc('\\', fp); break;
95 case 't': fputc('\t', fp); break;
96 case 'n': fputc('\n', fp); break;
97 case 'a': fputc('\a', fp); break;
98 case 'r': fputc('\r', fp); break;
99 case '0': fputc('\0', fp); break;
100 case '%': fputc('%', fp); break;
102 unsigned char ch = 0;
105 unsigned int x = *p++;
107 if (x >= 10) x -= 'a' - '0' - 10;
108 if (x >= 16) x -= 'A' - 'a';
123 /* --- @qq_file@ --- *
125 * Arguments: @FILE *fp@ = output file handle
126 * @const char **p@ = pointer to the output array
130 * Use: Writes the contents of a file to the output.
133 void qq_file(FILE *fp, const char **p)
139 /* --- @qq_head@ --- *
141 * Arguments: @FILE *fp@ = output file handle
145 * Use: Writes the head of a `qqout.c' file.
148 void qq_head(FILE *fp)
150 fputs("/* quine output file */\n\n", fp);
153 /* --- @qq_body@ --- *
155 * Arguments: @FILE *fp@ = output file handle
156 * @const char ***p@ = pointer to main table
160 * Use: Writes the body table of a `qqout.c' file.
163 void qq_body(FILE *fp, const char ***p)
168 fputs("/* --- file contents tables --- */\n\n", fp);
171 fprintf(fp, "static const char *qq__c_%lx[] = {\n", (unsigned long)fno);
172 fprintf(fp, "/* Filename: */ \"%s\",\n", *q++);
173 if (strncmp(*q, "%!", 2) == 0)
174 fprintf(fp, "/* Mode: */ \"%s\",\n", *q++);
176 fprintf(fp, "\"%s\",\n", *q++);
177 fputs("0\n};\n\n", fp);
183 /* --- @qq_tail@ --- *
185 * Arguments: @FILE *fp@ = output file handle
186 * @const char **qql@ = pointer to qqlib file array
187 * @size_t fno@ = number of files written
188 * @const char *fn@ = name of `qqout.c' file
192 * Use: Writes the head of a `qqout.c' file.
195 void qq_tail(FILE *fp, const char **qql, size_t fno, const char *fn)
200 /* --- Write out the qqlib array --- */
202 fputs("/* --- qqlib code --- */\n\n"
203 "const char *qq_qqlib[] = {\n", fp);
204 for (p = qql; *p; p++)
205 fprintf(fp, "\"%s\",\n", *p);
206 fputs("0\n};\n\n", fp);
208 /* --- Build the main array --- */
210 fputs("/* --- table of files to output --- */\n\n"
211 "const char **qq_table[] = {\n", fp);
212 for (i = 0; i < fno; i++)
213 fprintf(fp, " qq__c_%lx,\n", (unsigned long)i);
214 fputs(" 0\n};\n\n", fp);
216 /* --- Now output the qqlib code --- */
218 fputs("#define QQ_OUT\n\n", fp);
221 /* --- Finally write the code to write everything out --- */
224 "/* --- quine output routines --- */\n\n"
225 "void qq_dump(FILE *fp) {\n"
226 " qq__dump(fp, qq_table, qq_qqlib, \"%s\", %lu);\n"
228 "int qq_create(void) {\n"
229 " return (qq__create(qq_table, qq_qqlib, \"%s\", %lu));\n"
231 fn, (unsigned long)fno, fn, (unsigned long)fno);
234 /* --- @qq_mkfile@ --- *
236 * Arguments: @const char *fn@ = pointer to a filename
237 * @int mode@ = file mode to use (only in Unix-specific version)
239 * Returns: A handle for the created file.
241 * Use: Creates a file, and leading directories and stuff.
244 #ifdef QUINE_PORTABLE
245 FILE *qq_mkfile(const char *fn)
247 return (fopen(fn, "w"));
250 FILE *qq_mkfile(const char *fn, int mode)
252 char buf[FILENAME_MAX];
257 p = strchr(buf, '/');
264 p = strchr(p + 1, '/');
267 fd = open(fn, O_WRONLY|O_CREAT|O_TRUNC, mode);
268 return (fd >=0 ? fdopen(fd, "w") : 0);
274 /* --- @qq__dump@ --- *
276 * Arguments: @FILE *fp@ = file to dump on
277 * @const char ***p@ = pointer to main table
278 * @const char **qql@ = pointer to qqlib code
279 * @const char *fn@ = name of `qqout.c' file
280 * @size_t fno@ = number of files to be output
284 * Use: Outputs the entire source code to a given file.
287 static void qq__dump(FILE *fp, const char ***p, const char **qql,
288 const char *fn, size_t fno)
293 fprintf(fp, "******** %s\n\n", **q);
297 fprintf(fp, "******** %s\n\n", fn);
300 qq_tail(fp, qql, fno, fn);
303 /* --- @qq__create@ --- *
305 * Arguments: @const char ***p@ = pointer to main table
306 * @const char **qql@ = pointer to qqlib code
307 * @const char *fn@ = name of `qqout.c' file
308 * @size_t fno@ = number of files to be output
310 * Returns: Zero if all went well, else nonzero.
312 * Use: Rebuilds the original source distribution.
315 static int qq__create(const char ***p, const char **qql,
316 const char *fn, size_t fno)
322 const char **qq = *q + 1;
324 #ifdef QUINE_PORTABLE
325 if (strncmp(*qq, "%!", 2) == 0)
329 if (strncmp(*qq, "%!", 2) == 0) {
330 fp = qq_mkfile(**q, (int)strtol(*qq + 2, 0, 8));
333 fp = qq_mkfile(**q, 0666);
343 fp = qq_mkfile(fn, 0666);
348 qq_tail(fp, qql, fno, fn);
356 /*----- That's all, folks -------------------------------------------------*/