/*
 * pstokenize.c
 *
 * Copyright (c) 1989-1991 Adobe Systems Incorporated.
 * All rights reserved.
 *
 * This file may be freely copied and redistributed as long as:
 *   1) This entire notice continues to be included in the file, 
 *   2) If the file has been modified in any way, a notice of such
 *      modification is conspicuously indicated.
 *
 * PostScript, Display PostScript, and Adobe are registered trademarks of
 * Adobe Systems Incorporated.
 * 
 * ************************************************************************
 * THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO CHANGE WITHOUT
 * NOTICE, AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY ADOBE SYSTEMS
 * INCORPORATED. ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY OR 
 * LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO WARRANTY OF ANY 
 * KIND (EXPRESS, IMPLIED OR STATUTORY) WITH RESPECT TO THIS INFORMATION, 
 * AND EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR PARTICULAR PURPOSES AND NONINFINGEMENT OF THIRD PARTY RIGHTS.
 * ************************************************************************
 */

#include <stdio.h>
#include "encoding.h"

#ifndef	DEBUG
#define	DEBUG 0
#endif	DEBUG

char	*psFile = NULL;
char	*tokenFile = NULL;
int	 formatSpecified = false;
int	 byteOrderSpecified = false;

FatalError (message, arg1, arg2, arg3, arg4)
  char *message;
 /*
   Print message with prefix and followed by newline.  Exit indicating error.
  */
{
  fprintf (stderr, "pstokenize: ");
  fprintf (stderr, message, arg1, arg2, arg3, arg4);
  fprintf (stderr, "\n");
  exit (1);
}

ParseArguments (argc, argv)
  int argc;
  char *argv[];
{
  while (--argc > 0) {
    char   *arg = *++argv;

    if (arg[0] == '-') {
      if (arg[2] != (char) NULL)
	FatalError ("Arguments must be single character: %s", arg);
      else {
	switch (arg[1]) {
	 case 'c':
	  {
	    extern int keepComments;	/* parser.l */

	    keepComments = true;
	    break;
	  }

	 case 'i':
	 case 'n':
	  if (!formatSpecified) {
	    ieeeFormat = (*arg == 'i');
	    formatSpecified = true;
	  } else
	    FatalError ("%s floating point format already specified (%s)", (ieeeFormat ? "IEEE" : "Native"), arg);
	  break;

	 case 'h':
	 case 'l':
	  if (!byteOrderSpecified) {
	    highByteFirst = (*arg == 'h');
	    byteOrderSpecified = true;
	  } else
	    FatalError ("%s-byte-first already specified (%s).", (highByteFirst ? "High" : "Low"), arg);
	  break;

	 case 'o':
	  if (tokenFile == NULL) {
	    if (--argc > 0) {
	      tokenFile = *++argv;
	      if (*tokenFile == '-')	/* Do not assume "-" begins filename */
		tokenFile = NULL;
	    }
	    if (tokenFile == NULL)
	      FatalError ("Output filename missing (-o).");
	  } else
	    FatalError ("Only one output file allowed (%s).", tokenFile);
	  break;

	 default:
	  fprintf (stderr, "usage: pstokenize [-o outputfile] [-{i|n}] [-{h|l}] inputfile\n");
	  exit (1);

	}
      }
    } else if (psFile == NULL)
      psFile = arg;
    else
      FatalError ("Only one input file allowed (%s).", psFile);
  }

#if	DEBUG
  fprintf (stderr, "Input: %s, output: %s, %s byte first, %s floating point.\n",
	   (psFile == NULL ? "stdin" : psFile),
	   (tokenFile == NULL ? "stdout" : tokenFile),
	   (highByteFirst ? "high" : "low"),
	   (ieeeFormat ? "IEEE" : "native"));
#endif	DEBUG
}

main (argc, argv)
  int argc;
  char *argv[];
{
  ParseArguments (argc, argv);

  InitParser ();
  InitEncoding ();

  if (psFile == NULL)
    psFile = "stdin";
  else if (freopen (psFile, "r", stdin) == NULL)
    FatalError ("Failed to open %s for input.", psFile);

  if (tokenFile == NULL)
    tokenFile == "stdout";
  else if (freopen (tokenFile, "w", stdout) == NULL)
    FatalError ("Failed to open %s for output.", tokenFile);

  while (yylex ());    

  putchar ('\n');	/* Finish with a newline */

  exit (0);
}
