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

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

///*
// main.c of 
//
// 2rtf: a facility to convert files in the ATK file format to files compatible with the RTF format.
//
// Scribetext is copyright (c) 1989, 1990 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.
//
// 2rtf was written by Scott Rixner, rixner@ATHENA.MIT.EDU and Jeremy Paul Kirby, jpkirby@ATHENA.MIT.EDU
//
// $Header: /usr2/multimedia/RCS/MMEdit//2rtf_main.c,v 3.2 93/01/04 11:17:12 suzuki Exp $
//*/
//
//
#include <stdio.h>
#include <sys/file.h>  /* access() */
#include <sys/errno.h>
#import  <defaults/defaults.h>
#include <strings.h>
#include "2rtf.h"
#import <streams/streams.h>
#import "data_types.h"


struct TableStruct *Table=NULL;
struct ValuesStruct *Values=NULL;
/* struct FileStackStruct *FileStack=NULL; */
struct IdStackStruct *IdStack=NULL;
struct StyleStackStruct *Style=NULL;
static char *launchDir = NULL;
int ClaunchDirOk();
int NXRunAlertPanel();
char *doLocalString();


#define LoStr(key)      doLocalString(NULL,key,NULL)
#define fgetc(x) NXGetc(x)
#define ungetc(x,y) NXUngetc(y)
#define fputc(x,stream) NXPutc(stream,x)

NXStream *ez2rtf(NXStream *stream)
/*
 *
 *  Handle command line arguments, and get pointers to the
 *  appropriate files.  Pass control on to ParseMain,
 *  with the names of the input and output files as
 *  arguments.
 *
 */
{
  char *GetInstruction(), *getenv(), path[1000];
  int ExecuteSpecial();
  int atoi();
  void usage(), CloseFiles(), MakeTable(), TempPrintList(), ParseMain(),
  SetupEnvironment(), InitStateVector(), exit();

  NXStream *outstream;

  /*  Get date and time into char *datestamp as quick as posssible */

  InitStateVector();
  paragraph = 1;

 outstream = NXOpenMemory(NULL, 0, NX_READWRITE);
 fout = outstream;
 fin = stream;
   NXSeek(fin, (long)0, NX_FROMSTART);
        if ( !ClaunchDirOk()) {
                sprintf(path, "%s/2rtf.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/2rtf.trans" ,NX_READONLY);
 ferr = NXOpenMemory(NULL, 0, NX_READWRITE);

  MakeTable();

  ParseMain();

  NXCloseMemory(ferr, NX_FREEBUFFER);
  return(outstream);

}


NXStream *richtext2rtf(NXStream *stream)
{
  char  *GetInstruction(), *getenv(), path[1000];
  int ExecuteSpecial();
  void usage(), CloseFiles(), MakeTable(), TempPrintList(), richParseMain(),
  SetupEnvironment(), InitStateVector(), exit();

  NXStream *outstream;

  /*  Get date and time into char *datestamp as quick as posssible */

  InitStateVector();
  paragraph = 1;

 outstream = NXOpenMemory(NULL, 0, NX_READWRITE);
 fout = outstream;
 fin = stream;
   NXSeek(fin, (long)0, NX_FROMSTART);
        if ( !ClaunchDirOk()) {
                sprintf(path, "%s/rich2rtf.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);
        }

        }
 ferr = NXOpenMemory(NULL, 0, NX_READWRITE);

  MakeTable();

  richParseMain();
  NXCloseMemory(ferr, NX_FREEBUFFER);

  return(outstream);

}


NXStream *plain2rtf(NXStream *stream)
{
  char  *GetInstruction(), *getenv();
  int ExecuteSpecial();
  void usage(), CloseFiles(), MakeTable(), TempPrintList(), plainParseMain(),
  SetupEnvironment(), InitStateVector();

  NXStream *outstream;

  /*  Get date and time into char *datestamp as quick as posssible */

  InitStateVector();
  paragraph = 1;

 outstream = NXOpenMemory(NULL, 0, NX_READWRITE);
 fout = outstream;
 fin = stream;
   NXSeek(fin, (long)0, NX_FROMSTART);
 ferr = NXOpenMemory(NULL, 0, NX_READWRITE);

  plainParseMain();

  NXCloseMemory(ferr, NX_FREEBUFFER);

  return(outstream);

}

//static int ClaunchDirOk()
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 MakeTable()
/*
 *
 *  Create internal translation table by reading in ftrans.
 *
 */
{
  struct TableStruct *tmp;
  char ch, *ezword, rtfword[TMP_SIZE], *makelower();  
  extern FP AssignFunc();
  int in, len, rv = 0, dsv=0 , atoi(), exit();

  ezword = (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", ezword, rtfword)==EOF)
      if(NXScanf(ftrans, "%s%s", ezword, rtfword)==EOF)
	break;

      ezword = makelower(ezword);

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

          if(!strcmp(rtfword, "~RTFVersion"))
            {
              RTFVersion = atoi(ezword);
              rv = 1;
	    }
	  else if(!strcmp(rtfword, "~TextDSVersion"))
	    {
	      TextDSVersion = atoi(ezword);
	      dsv = 1;
	    }
	  else
	    {
	      fprintf(stderr, "\n* Error!\n* %s: %s\n%s%s%s%s\n", me,
		      "Corrupt translation table:  Presence of invalid environment entry:",
		      "Corrupt entry:  \"", ezword, "\" --> ", rtfword);
	    }
	}
      else
	{
	  tmp = (struct TableStruct *) malloc (sizeof(struct TableStruct));
	  tmp->ezword = (char *) calloc (strlen(ezword) + 1, 
					     sizeof(char));
	  strcpy(tmp->ezword, ezword);
	  tmp->mode = 0;

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

  if(!rv || !dsv)
    {
      fprintf(stderr, "\n* Error!\n* %s: %s\n\t%s %s\n%s", me, 
	      "Fatal error in translation table:  environment entry:",
	      !rv ? "~RTFVersion" : "",
	      !dsv ? "~TextDSVersion" : "", 
	      "weren't found and are necessary for operation.");
      exit(0);    
    }
  NXCloseMemory(ftrans, NX_FREEBUFFER);
}


void SetupEnvironment(rootfile)
     char *rootfile;
/*
 *
 *  This adds a bunch of values.  I'm not sure why, because
 *  they never get used.
 *
 */
{
  void AddValue();
  char *username, *wd, *getlogin(), *getwd(), *trimroot, *fullman;

  /*  Go through all predefined string fields and define them
      and add them to the transtable   */

  /*  Take care of device, devicename, filedate, fullmanuscript,
      genericdevice, manuscript, rootfiledate, site, sitename, time,
      timestamp, username, scribeversion and scribetextversion */

  wd = (char *) malloc(1024 * sizeof(char));
  trimroot = (char *) malloc(1024 * sizeof(char));
  fullman = (char *) malloc(1024 * sizeof(char));

  AddValue("device", "PostScript");
  AddValue("devicename",  "PostScript Page Description Language");
  AddValue("genericdevice",  "ScaleableLaser");
  AddValue("scribeversion", VERSION);
  AddValue("scribetextversion", VERSION);
  AddValue("site", "MIT");
  AddValue("sitename", "Project Athena");

  /* and now for filedate, fullmanuscript, manuscript, rootfiledate,
     time, timestamp, and username */

  username=getlogin();
  wd = getwd(wd);

//  if(rootfile[0]=='/')
//    fullman = rootfile;
//  else
//    fullman = strcat(wd, rootfile);
//
//  trimroot = &rootfile[roffset(rootfile, '/')];

  AddValue("username", username);
//  AddValue("fullmanuscript", fullman);
//  AddValue("manuscript", trimroot);

}

void AddValue(name, value)
     char *name, *value;
/*
 *
 *  This adds a value to Values.
 *
 */
{
  struct ValuesStruct *tmp;

  tmp = (struct ValuesStruct *) malloc(sizeof(struct ValuesStruct));

  tmp->name = (char *) calloc(strlen(name) + 1, sizeof(char));
  tmp->name = name;
  if (value == NULL) 
    {
      tmp->value = (char *) malloc (sizeof(char));
      tmp->value[0] = '\0';
    }
  else
    {
      tmp->value = (char *) calloc(strlen(value) + 1, sizeof(char));
      tmp->value = value;
    }

  if(Values == NULL)
    {
      Values = tmp;
      Values->next = NULL;
    }
  else
    {
      tmp->next = Values;
      Values = tmp;
    }
}

void InitStateVector()
/*
 *
 *  This procedure initializes all the parts of the
 *  state vector to their standard values.
 *
 */
{
    const char *fontNameAndSize;
    char fontName[128];
    float fontSize;

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

    State.CurLeftMargin = 0;
    State.CurRightMargin = 0;
    State.CurRightEdge = 1140;
    State.CurLeftEdge = 1140;
    State.CurTopMargin = 0;
    State.CurBottomMargin = 0;
    State.CurScriptMovement = 0;
//    State.CurFontSize = 24;
    State.CurFontSize = 2*(int)fontSize;
    State.CurLeftIndentation = 0;
    State.CurRightIndentation = 0;
    State.CurJustification = style_LeftJustified;
    State.CurFontFamily = "andy";
}

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