ftp.nice.ch/pub/next/connectivity/news/NewsBase.3.02.s.tar.gz#/NewsBase302.source/MMEdit/rtf2_main.c

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

 /*
 main.c of 

 Rtf2: a facility to convert RTF files to files 
 compatible with the ATK file format.

 Rtf2 is copyright (c) 1991 by the Massachusetts Institute of
 Technology.

 RTF is a product of the Microsoft Corporation

 Permission to use, copy, modify, and distribute this software and
 its documentation for any purpose and without fee is hereby granted,
 provided that the above copyright notice and the name of the author(s)
 appear in all copies; that both that copyright notice, the name of
 the author(s) and this permission notice appear in supporting
 documentation; and that the name of the Massachusetts Institute of
 Technology not be used in advertising or publicity pertaining to
 distribution of the software without specific, written prior
 permission.  The Massachusetts Institute of Technology makes no
 representations about the suitability of this software for any purpose.
 It is provided "as is" without express or implied warranty.

 Rtf2 was written by Jeremy Paul Kirby, jpkirby@ATHENA.MIT.EDU and Scott Rixner, rixner@ATHENA.MIT.EDU

 $Header: /usr2/multimedia/RCS/MMEdit//rtf2_main.c,v 3.5 93/01/04 11:17:56 suzuki Exp $
*/


#include <stdio.h>
#include <sys/file.h>  /* access() */
#include <sys/errno.h>
#include <strings.h>
#import <defaults/defaults.h>
#import <streams/streams.h>
#import "mem_file_desc.h"
#import "data_types.h"
#import "rtf2.h"

#define LoStr(key)      doLocalString(NULL,key,NULL)
#define fgetc(x) NXGetc(x)
#define fputc(x,stream) NXPutc(stream,x)
#define fputs(x,stream) NXPrintf(stream,x)
#define ungetc(x,y) NXUngetc(y)
#define fprintf NXPrintf
#define fscanf NXScanf

/*char *AndrewDir(); */

rtf2_TABLE rtf2_Table=NULL;
rtf2_VALUES rtf2_Values=NULL;
FILESTACK FileStack=NULL;
rtf2_FONT rtf2_Font=NULL;
char *Filein, *Fileout;
double LeftMargin = .75;
double RightMargin = 6.5;
extern   bold_flag, italic_flag, supers_flag, subs_flag, center_flag;
extern   fleft_flag, fright_flag, underl_flag;
static char *launchDir = NULL;
static int ClaunchDirOk();
extern int NXRunAlertPanel(), doLocalString(), exit();

int RTFparse(file_desc_head, MimeArticleStream, bound, countO)
struct mem_file_desc *file_desc_head;
NXStream *MimeArticleStream;
char *bound;
int countO;
{
  char *rtf2_GetInstruction();
  int rtf2_ExecuteSpecial();
  long int rtf2_ParseText();
  void rtf2_usage(), rtf2_CloseFiles(), rtf2_MakeTable(), rtf2_TempPrintList(), rtf2_ParseMain();
    const char *fontNameAndSize;
    char fontName[128], path[200];
    float fontSize;

struct mem_file_desc *adpp = file_desc_head;

   Adpp = &file_desc_head;
   Boundary=bound;

/* void SetupEnvironment();*/

//  FontSize = 24;
    if ((fontNameAndSize = NXGetDefaultValue(OWNER, FONT)) != 0 ){
        sscanf(fontNameAndSize, "%127[^:]:%f", fontName, &fontSize);
    } else {
        fontSize = 14.0;
        strcpy(fontName, "FixedRyuminCourier-Light");
    }
  FontSize = 2*(int)fontSize;
  he = FontSize;

  flag = 0;
  indented = 0;
  fnote = 0;
  tabs = 0;
  bold_flag = 0;
  italic_flag = 0;
  supers_flag = 0;
  subs_flag = 0;
  center_flag = 0;
  fleft_flag = 0;
  fright_flag = 0;
  underl_flag = 0;
  Levels=0;
  Levels1=0;

  multip=1; 


    for ( ; adpp != NULL; adpp = adpp->fd_nextp) {
	if ( !strcmp(INDEX_RTF, adpp->fd_name) ){ 

	 fout = MimeArticleStream;
	 fin = adpp->fd_stream;

	   NXSeek(fin, (long)0, NX_FROMSTART);
	if ( !ClaunchDirOk()) {
		sprintf(path, "%s/rtf2.trans", launchDir);
	}
	 ftrans = NXMapFile(path ,NX_READONLY);
	if (!ftrans){
		NXRunAlertPanel(LoStr("NewsBase"),
	LoStr("There is no translation table file."),LoStr("OK"),NULL,NULL);
		exit(1);
	}
	 //ftrans = NXMapFile("/usr2/suzuki/rtf2.trans" ,NX_READONLY);
	 ferr = NXOpenMemory(NULL, 0, NX_READWRITE);

	  rtf2_MakeTable();

	  rtf2_ParseMain(countO);
	  NXCloseMemory(ftrans, NX_FREEBUFFER);
	  NXCloseMemory(ferr, NX_FREEBUFFER);
 	 return(0);
       }
   }
 	 return(0);
}

static int ClaunchDirOk()
{
    const char *slash;

    if (launchDir) return(0);

    slash = strrchr(NXArgv[0], '/');
    if (slash && slash-NXArgv[0]) {
        launchDir = malloc((slash-NXArgv[0]+1)*sizeof(char));
        strncpy(launchDir, NXArgv[0], slash-NXArgv[0]);
        launchDir[slash-NXArgv[0]] = '\0';
        return(0);
    }

    return(1);
}



void rtf2_MakeTable()
{
  rtf2_TABLE tmp;
  char ch, *rtfword, ezword[TMP_SIZE], *makelower();  
  extern rtf2_FP rtf2_AssignFunc();
  int in, len, rtfc=0, dsv=0, rtfvc=0, atoi(), exit();

  rtfword = (char *) calloc(TMP_SIZE, sizeof(char));

//  printf("* Creating internal translation table...");
  while(1)
    {
      in = fgetc(ftrans);
      if(in==EOF)
	break;
      ch = (char) in;
      if(ch == '#' || ch == '\n' || ch == '\r' || ch == ' ' || ch == '\t')
	{
	  while(ch != '\n' && ch != '\r')
	    ch = (char) fgetc(ftrans);
	  continue;
	}
      else
	ungetc(ch, ftrans);

      if(fscanf(ftrans, "%s%s", rtfword, ezword)==EOF)
	break;

      rtfword = makelower(rtfword);

      if(ezword[0]=='~')
	{
	  len = strlen(rtfword) + 1;

	  if(!strcmp(ezword, "~RTFchars"))
	    {
	      RTFchars = (char *) malloc (len * sizeof(char));
	      strcpy(RTFchars, rtfword);
	      rtfc = 1;
	    }
	  else if(!strcmp(ezword, "~TextDSVersion"))
	    {
	      TextDSVersion = atoi(rtfword);
	      dsv = 1;
	    }
	  else if(!strcmp(ezword, "~RTFverceiling"))
	    {
	      RTFverceiling = atoi(rtfword);
	      rtfvc = 1;
	    }
	  else
	    {
		;
	    }
	}
      else
	{
	  tmp = (rtf2_TABLE) malloc (sizeof(struct rtf2_TableStruct));
	  tmp->rtfword = (char *) calloc (strlen(rtfword) + 1, 
					     sizeof(char));
	  strcpy(tmp->rtfword, rtfword);
	  tmp->mode = 0;

	  if( (ezword[0]=='@') || (ezword[0]=='!') || (ezword[0]=='#') || 
	     (ezword[0]=='$') || (ezword[0]=='^'))
	    {
	      switch ((char) ezword[0])
		{
		case '@':
		  tmp->mode=COMMAND | RTFCOMMAND;
		  break;
		case '!':
		  tmp->mode=COMMAND | EZCOMMAND;
		  break;
      		case '#':
		  tmp->mode=COMMAND | RTFCOMMAND | NAKED | SUPPRESSDELS;
		  break;
		case '$':
		  tmp->mode=COMMAND | RTFCOMMAND | NAKED | USEDELS;
		  break;
		case '^':
		  tmp->mode=QUOTEDCHAR;
		}
	      if(ezword[0]=='^')
		tmp->ez.quote = (char) atoi(&ezword[1]);
	      else
		tmp->ez.fun = rtf2_AssignFunc(&ezword[1]);
	    }
	  else
	    {
	      tmp->mode = WORD;
	      tmp->ez.word = (char *) calloc(strlen(ezword) + 1, sizeof(char));
	      strcpy(tmp->ez.word, ezword);
	    }
	  if(rtf2_Table == NULL)
	    {
	      rtf2_Table = tmp;
	      rtf2_Table->next = NULL;
	    }
	  else
	    {
	      tmp->next = rtf2_Table;
	      rtf2_Table = tmp;
	    }
	}
    }

  if(!rtfc || !dsv || !rtfvc)
    {
      exit(0);    
    }
}

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