ftp.nice.ch/pub/next/text/framemaker/filters/prog2mif.tar.gz#/prog2mif.c

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

/***********************************************************************
**
**   Filename: prog2mif.c
**     Author: Hermann Kneissel, DANET-IS
**      Phone: 0711/13353-24     Email: kneissel@danetis.UUCP
**
**  $Revision: 0.1 $
**      $Date: 91/07/23 07:54:56 $
**
**  <Module-Description>
**
**  This file is based on the txt2mif example from the
**  framemaker source directory.
**
**  Small filter to import program-source into framemaker.
**  Keeps all text in one paragraph named ProgList, keeps
**  Text Format if you use a Corier-Font.
**
***********************************************************************
***********************************************************************
 *                      Modification History
 *
 * $Log:	prog2mif.c,v $
 * Revision 0.1  91/07/23  07:54:56  hermann
 * first public version
*/
#define _PROG2MIF_C
/**********************************************************************/
/* includefiles                 |                                     */
/*--------------------------------------------------------------------*/
#include  <stdio.h>
#include  <ctype.h>
/**********************************************************************/

#define  MAXLINE	50		/* max. length of line        */

char     out_buffer[256];
unsigned bufpos;
unsigned ident;

FILE 	*out;				/* output-stream              */
FILE    *in;				/* input-stream		      */

int      tabstop        = 8;		/* tabstop, 2^n		      */
int      gen_hardspaces = 1;		/* convert space/tab into hs  */
int      gen_return     = 1;		/* make hard-returns          */
const char *para_name   = "ProgList";	/* Paragraph Tag              */

/**********************************************************************/

void put_ident()
{
  unsigned i;

  if ( i=ident )
    while( i-- )
      fputc( ' ', out );
}

void flush_buffer()
{
  if ( bufpos )
  {
    put_ident();
    out_buffer[bufpos] = '\0';
    fprintf( out, "<String `%s' >\n", out_buffer ); 
    bufpos = 0;
  };
}

void put_char( const char *name )
{
  flush_buffer();
  put_ident();
  fprintf( out, "<Char %s >\n", name );
}

int line_to_mif()
{
  int  i;
  int  col;
  char c;

  col    = 0;
  bufpos = 0;

  put_ident();
  fputs( "<ParaLine\n", out );
  ident += 2;
 
  for(;;)		/* loop over input-line */
  {
    c=fgetc( in );

    if ( c=='\n' || c==EOF )
      break;

    switch( c )
    {
      case ' ':
        if ( gen_hardspaces )
          put_char( "HardSpace" );
        else
          out_buffer[bufpos++] = ' ';
	col++;
	break;

      case '\t':
	if ( gen_hardspaces )
	{
          i = ((col + tabstop) & -tabstop ) - col;
          while( i-- )
          {
            put_char( "HardSpace" );
	    col++;
          }
	}
	else
	{
	  strcpy( out_buffer+bufpos, "\\t" );
	  bufpos += 2;
	}
        break;

      case '>':
        strcpy( out_buffer+bufpos, "\\>" );
        bufpos += 2;
        break;

      case '\\':
        strcpy( out_buffer+bufpos, "\\\\" );
        bufpos += 2;
        break;

      case '`':
        strcpy( out_buffer+bufpos, "\\xd4 " );
        bufpos += 5;
        break;

      case '\'':
        strcpy( out_buffer+bufpos, "\\xd5 " );
        bufpos += 5;
        break;

      default:
        if ( isprint( c ) )
	{
	  out_buffer[bufpos++] = c;
	  col++;
        }
    }

    if ( bufpos > MAXLINE )
      flush_buffer();
  } 

  if ( gen_return )
    put_char( "HardReturn" );
  else
    flush_buffer();

  ident -= 2;
  put_ident( ident, out );
  fputs( "> # end of ParaLine\n", out );

  return c;
}

void gen_mif()
{
  ident = 0;

  fputs( "<MIFFile 1.01> # created by ", out );
  fputs( "$RCSfile: prog2mif.c,v $ $Revision: 0.1 $\n\n", out );

  fputs( "\n<Para\n", out );
  ident += 2;

  put_ident();  fprintf( out, "<PgfTag `%s'>\n", para_name );
  
  while( line_to_mif() != EOF )
    ;

  ident -= 2;
  put_ident();
  fputs( "> # end of Para\n", out );

  fputs( "\n# end of MIFFile\n\n", out );
}

void usage(const char *myname)
{
  fprintf( stderr, "\nUsage: %s <options> <files>\n", myname );

  fputs( "   options:\n", stderr );
  fputs( "     -n <name>     uses <name> as Tagname (default 'ProgList').\n", stderr );
  fputs( "     -o <filename> directs output to <filename>.\n", stderr );
  fputs( "     -r            generates softreturns instead of hardreturns.\n", stderr );
  fputs( "     -s            generates softspaces instead of hardspaces.\n", stderr );
  fputs( "     -t <number>   sets tabstop to <number>.\n", stderr );
  fputs( "   <files> is optional. If no files are given, stdin will be used.\n", stderr );

  fputs( "\n", stderr );
  exit( 1 );
}

void main( int argc, char *argv[] )
{
  int i;
  int files_seen = 0;

  in    = stdin;
  out   = stdout;

  i = 1;
  while( i<argc )
  {
    if ( argv[i][0] == '-' )
    {
      switch( argv[i][1] )
      {
        case 'n':
          i++;
          if ( i>=argc )
	  {
	    fprintf( stderr, "%s: missing argument after '-n'\n", argv[0] );
	    exit(1);
	  }
	  para_name = argv[i];
	  break;

        case 'o':
          i++;
          if ( i>=argc )
	  {
	    fprintf( stderr, "%s: missing argument after '-o'\n", argv[0] );
	    exit(1);
	  }
	  out = fopen( argv[i], "w" );
	  if ( !out )
	  {
	    fprintf( stderr, "%s: can't open '%s' for output\n", 
							argv[0], argv[i] );
	    exit(1);
	  }
	  break;

 	case 's':
	  gen_hardspaces = 0;
	  break;

	case 'r':
	  gen_return = 0;
	  break;

	case 't':
          i++;
          if ( i>=argc )
	  {
	    fprintf( stderr, "%s: missing argument after '-t'\n", argv[0] );
	    exit(1);
	  }
	  fprintf( stderr, "%s: option '-t' not supported, sorry ...\n",
				argv[0] );
	  break;

        default:
          fprintf( stderr, "%s: unknown option '%s'\n", argv[0], argv[i] );
          usage(argv[0]);
      }
    }
    else /* a filename */
    {
      files_seen++;
      in = fopen( argv[i], "r" );
      if ( !in )
        fprintf( stderr, "%s: can't open '%s' for input\n", argv[0], argv[i] );
      else
      {
        gen_mif();
	fclose( in );
      } 
    }

    i++;
  } 

  if ( !files_seen )	/* use stdin as source */
    gen_mif();

  exit(0);
}

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