ftp.nice.ch/pub/next/unix/mail/tnextmail.1.0.s.tar.gz#/tnextmail/msgtortf.c

This is msgtortf.c in view mode; [Download] [Up]

/* $Id: msgtortf.c,v 1.2 91/05/25 15:08:20 cap Exp $
 * Read an input stream, which was prepared by the user, and write out an
 * RTF file that references any attachments.
 */

#include <stdio.h>
#include <ctype.h>
#ifndef USG
#include <sys/param.h>
#endif
#include "config.h"

extern char rtf_name[];
extern int rtfpos;
extern char fatalerrors;

/* 
 * msgtortf - translate an ascii message to trivial RTF. Support the 
 *            \attach command.
 */

msgtortf(in, out)
     FILE *in, *out;
{
    int c;

    while ((c = getc(in)) != EOF) {
	switch (c) {
	  case '\\':
	    if (handlebslash(in, out) < 0)
		return;		/* error occurred */
	    break;
	  case '{':
	  case '}':
	  case '\n':
	    putc('\\', out);
	  default:
	    putc(c, out); rtfpos++;
	}
    }
}

/* 
 * handlebslash - We have seen a backslash in the input. If the user 
 *                is using the \attach command, we parse the command 
 *                out of the input stream. Otherwise, we have to quote 
 *                the backslash and continue normal processing.
 */

int handlebslash(in, out)
     FILE *in, *out;
{
    static char attach[] = "attach";
    char *ap = attach;
    char sourceword[MAXPATHLEN], *sp = sourceword;
    int si;
    /* indicators for sightings of braces */
    char bracelevel = 0;
    int c;

    while (*ap) {
	*sp = getc(in);
	if (*sp++ != *ap++)	/* no match */
	    goto nomatch;
    }
    /* we've matched "attach", but make sure we're at the end of a word */
    *sp = getc(in);
    if (*sp == '{')
	bracelevel++;
    else if (!isspace(*sp)) {
	/* 
	 * user said something like \attachmumble, which we pass 
	 * through as plain text.
	 */
      nomatch:
	fprintf(out, "\\\\");
	fwrite(sourceword, sp + 1 - sourceword, 1, out);
	rtfpos += sp + 2 - sourceword;
	return 0;
    }
    /* 
     * At this point we're sure we have an attach command. It is now 
     * an error to have anything but whitespace until we see an open 
     * brace. We also are sure that we don't have to punt the command 
     * parsing and output \attablah . . ., so we can use sourceword 
     * for something else. We will use it for collecting the filename 
     * of the attachment.
     */
    sp = sourceword;
    while (!bracelevel) {
	*sp = getc(in);
	if (*sp == '{')
	    bracelevel++;
	else if (!isspace(*sp)) {
	    fatalerrors++;
	    fprintf(stderr, "Malformed \\attach command\n");
	    return -1;
	}
    }
    /* 
     * Now we've reached the argument of the attach---the filename. We 
     * copy filename information until bracelevel returns to zero.
     */
    for (si = 0; bracelevel; si++) {
	int c;
	
	if (si == sizeof sourceword) {
	    fprintf(stderr, "Argument to \\attach too long\n");
	    fatalerrors++;
	    return -1;
	}
	c = getc(in);
	if (c == '}') {
	    bracelevel--;
	    *sp = '\0'; /* terminate filename */
	} else if (c == EOF) {
	    fatalerrors++;
	    fprintf(stderr, "Premature EOF in \\attach command\n");
	    return -1;
	} else
	    *sp++ = c;
    }
    *sp = '\0';
    /* 
     * We now have the attach argument in sourceword. We send it to 
     * the attach handler.
     */
    return rtfattach(out, sourceword);
}

/* rtf_start - write the rtf header to the rtf file */

rtf_start(file)
     FILE *file;
{
    fprintf(file, "{\\rtf0\\ansi{\\fonttbl\\f1\\fnil %s;}\\f1\\fs%d\n",
	    FONTNAME, FONTSIZE * 2);
}

/* rtf_end - write trailing information to the rtf file */

rtf_end(file)
     FILE *file;
{
    fprintf(file, "}\n");
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.