-/* --- @getstring@ --- *
- *
- * Arguments: @FILE *fp@ = stream from which to read
- * @dstr *d@ = destination string
- * @unsigned raw@ = raw or cooked read
- *
- * Returns: Zero if OK, nonzero on end-of-file.
- *
- * Use: Reads a filename (or something similar) from a stream.
- */
-
-static int getstring(FILE *fp, dstr *d, unsigned raw)
-{
- int ch;
- int q = 0;
-
- /* --- Raw: just read exactly what's written up to a null byte --- */
-
- if (raw) {
- if ((ch = getc(fp)) == EOF)
- return (EOF);
- for (;;) {
- if (!ch)
- break;
- DPUTC(d, ch);
- if ((ch = getc(fp)) == EOF)
- break;
- }
- DPUTZ(d);
- return (0);
- }
-
- /* --- Skip as far as whitespace --- *
- *
- * Also skip past comments.
- */
-
-again:
- ch = getc(fp);
- while (isspace(ch))
- ch = getc(fp);
- if (ch == '#') {
- do ch = getc(fp); while (ch != '\n' && ch != EOF);
- goto again;
- }
- if (ch == EOF)
- return (EOF);
-
- /* --- If the character is a quote then read a quoted string --- */
-
- switch (ch) {
- case '`':
- ch = '\'';
- case '\'':
- case '\"':
- q = ch;
- ch = getc(fp);
- break;
- }
-
- /* --- Now read all sorts of interesting things --- */
-
- for (;;) {
-
- /* --- Handle an escaped thing --- */
-
- if (ch == '\\') {
- ch = getc(fp);
- if (ch == EOF)
- break;
- switch (ch) {
- case 'a': ch = '\a'; break;
- case 'b': ch = '\b'; break;
- case 'f': ch = '\f'; break;
- case 'n': ch = '\n'; break;
- case 'r': ch = '\r'; break;
- case 't': ch = '\t'; break;
- case 'v': ch = '\v'; break;
- }
- DPUTC(d, ch);
- ch = getc(fp);
- continue;
- }
-
- /* --- If it's a quote or some other end marker then stop --- */
-
- if (ch == q || (!q && isspace((unsigned char)ch)))
- break;
-
- /* --- Otherwise contribute and continue --- */
-
- DPUTC(d, ch);
- if ((ch = getc(fp)) == EOF)
- break;
- }
-
- /* --- Done --- */
-
- DPUTZ(d);
- return (0);
-}
-
-/* --- @putstring@ --- *
- *
- * Arguments: @FILE *fp@ = stream to write on
- * @const char *p@ = pointer to text
- * @unsigned raw@ = whether the string is to be written raw
- *
- * Returns: ---
- *
- * Use: Emits a string to a stream.
- */
-
-static void putstring(FILE *fp, const char *p, unsigned raw)
-{
- size_t sz = strlen(p);
- unsigned qq;
- const char *q;
-
- /* --- Just write the string null terminated if raw --- */
-
- if (raw) {
- fwrite(p, 1, sz + 1, fp);
- return;
- }
-
- /* --- Check for any dodgy characters --- */
-
- qq = 0;
- for (q = p; *q; q++) {
- if (isspace((unsigned char)*q)) {
- qq = '\"';
- break;
- }
- }
-
- if (qq)
- putc(qq, fp);
-
- /* --- Emit the string --- */
-
- for (q = p; *q; q++) {
- switch (*q) {
- case '\a': fputc('\\', fp); fputc('a', fp); break;
- case '\b': fputc('\\', fp); fputc('b', fp); break;
- case '\f': fputc('\\', fp); fputc('f', fp); break;
- case '\n': fputc('\\', fp); fputc('n', fp); break;
- case '\r': fputc('\\', fp); fputc('r', fp); break;
- case '\t': fputc('\\', fp); fputc('t', fp); break;
- case '\v': fputc('\\', fp); fputc('v', fp); break;
- case '`': fputc('\\', fp); fputc('`', fp); break;
- case '\'': fputc('\\', fp); fputc('\'', fp); break;
- case '\"': fputc('\\', fp); fputc('\"', fp); break;
- default:
- putc(*q, fp);
- break;
- }
- }
-
- /* --- Done --- */
-
- if (qq)
- putc(qq, fp);
-}
-