/*-------------------------------------------------------------------------
  Program   : PascalPrint.c
  Function  : Printutility for Pascal sourcecode
  Compiler  : Aztec C V3.6 a
  Options   : cc PascalPrint.c -s +l   ln PascalPrint.o -lc32
  Hints     : 
  Copyright : Public Domain
  Author    : ASSO 1990                                     
  -------------------------------------------------------------------------*/
#include <intuition/intuition.h>
#include <devices/printer.h>
#include <exec/types.h>
#include <stdio.h>
#include <ctype.h>

#define BOLD_ON     PrintCommand(request,aSGR1 ,0,0,0,0)
#define BOLD_OFF    PrintCommand(request,aSGR22,0,0,0,0)
#define ITALICS_ON  PrintCommand(request,aSGR3 ,0,0,0,0)
#define ITALICS_OFF PrintCommand(request,aSGR23,0,0,0,0)
#define MAXWORD   80 /* maximum length of a string */
#define MAXKEY    52 /* number of reserved words   */

union printerIO {
 struct IOStdReq    ios;
 struct IODRPReq    iodrp;
 struct IOPrtCmdReq iopc;
};
union printerIO *request;
extern struct IORequest *CreateExtIO();
extern struct MsgPort   *CreatePort();
struct MsgPort *printerPort;

int  Error,lines=0;
FILE *fp;
BOOL comment,string,quote;
char word[MAXWORD], upword[MAXWORD],
     *keyw[MAXKEY]; 
/*-----------------------------------------------
                                    main
  -----------------------------------------------*/
main (argc,argv)
int argc;
char *argv[];
{
 int  namebuf[20];
 char *name, *gets();
 
 if ((argc > 1) && (*argv[1] != '?'))  name = argv[1];
 else {
  printf("\033[2mUSAGE: PascalPrint <filename.pas>\n"); 
  printf("Pascal-Source-Print-Utility\n");
  printf("ASSO 1990\033[0m\n");
  printf("Enter filename: ");
  name = gets(namebuf);
 }
 if ((fp = fopen(name,"r")) == NULL) { /* open file */
  printf("Can't open file !\n");
  exit(FALSE);
 }
 InitPrinter();
 InitKeyWords();
 ScanWords();
 CloseAll();
} 
/*-----------------------------------------------
                                       ScanWords
 ------------------------------------------------*/
ScanWords()
{  
 int count,j,com;
 char current,prev;

 comment=string=quote=FALSE;
 current=prev='\0';
 com=j=0;

 PrLnNr();                    /* print first linenumber */
 
 while ((current = getc(fp)) != EOF){

   if (current == '\n'){                      /* new line */
    if (j>0) {
     checkword(); j=0;
    }
    PrLnNr();
    current='\0';
   }

   if (current == '\t'){                      /* tabs  */
    if (j>0) {
     checkword(); j=0;
    }
    PrintString(request,"\t");
    current='\0';
   }

   if (comment) {                             /* comment off */
    if (prev == '*' && current == ')')       
      com--;
    if (prev == '(' && current == '*') 
      com++;
    if (com == 0) {
     comment=FALSE;
     ITALICS_OFF;
     PrintString(request,")");
     current='\0';
    } else
      PrintString(request,&current);
   }

   if (string) {                              /* comment2 off */
      PrintString(request,&current);
       if (current == '}'){
         string=FALSE;
         ITALICS_OFF;
         current='\0';
        } 
     } 
  
     if (quote) {                              /* quote off */
      PrintString(request,&current);
       if (current == '\'')
        if (prev != '\\') {
         quote=FALSE;
         current='\0';
        }
     }

    if (!string && !comment && !quote && current != '\0') {
       if (prev == '(' && current == '*') {  /* comment on */
         comment=TRUE;
         com++;             /* counter for nested comments */
         ITALICS_ON;
         PrintString(request,"*");
       } 
       else
       if (current == '{') {                 /* comment2 on  */
         string=TRUE;
         if (j>0) {
          checkword(); j=0;
         }
        ITALICS_ON;
        PrintString(request,"{");
       } 
       else
       if (current == '\'') {                 /* ' quote on  */
         quote=TRUE;
         if (j>0) {
          checkword(); j=0;
         }
        PrintString(request,"\'");
       } 
       else
       if (strchr("[ ]();,:=#^+-*.<>",current)) {
         checkword(); j=0;
         PrintString(request,&current);
        }
        else 
        {
         word[j++]=current;
         word[j]='\0';    /* add word terminator */
        }
    }/*  if  */
  prev=current;
 }/*  while */
}
/*-----------------------------------------------
                                       checkword
 ------------------------------------------------*/
checkword()
{
 int i=0, nokey=1;

while (word[i] != '\0') {
 upword[i] = toupper(word[i]);
 i++;
}
upword[i++]='\0';
 i=0;
 while ((nokey != 0) && (i <= MAXKEY))
  nokey = strcmp(upword,keyw[i++]);
  if (nokey==0){
     BOLD_ON;
      PrintString(request,upword);
     BOLD_OFF;
  } 
  else 
   PrintString(request,word);
   word[0]='\0'; 
   upword[0]='\0';
}
/*-----------------------------------------------
                                     PrintLineNr
 ------------------------------------------------*/
PrLnNr()
{
 char LineStr[7];
 
 ++lines;
 PrintString(request,"\n");
 sprintf(LineStr,"%4d: ",lines);
 if (!comment) ITALICS_ON;
  PrintString(request,LineStr);
 if (!comment) ITALICS_OFF;
}
/*-----------------------------------------------
                                    InitKeyWords
 ------------------------------------------------*/
InitKeyWords()
{           /* reserved words */
keyw[ 0]="ABSOLUTE";        keyw[25]="NIL"; 
keyw[ 1]="AND";             keyw[26]="NOT";
keyw[ 2]="ARRAY";           keyw[27]="OF";
keyw[ 3]="BEGIN";           keyw[28]="OR";
keyw[ 4]="CASE";            keyw[29]="PACKED";
keyw[ 5]="CONST";           keyw[30]="PROCEDURE";
keyw[ 6]="DIV";             keyw[31]="PROGRAM";
keyw[ 7]="DO";              keyw[32]="RECORD";
keyw[ 8]="DOWNTO";          keyw[33]="REPEAT";
keyw[ 9]="ELSE";            keyw[34]="SET";
keyw[10]="END";             keyw[35]="SHL";
keyw[11]="EXTERNAL";        keyw[36]="SHR";
keyw[12]="FILE";            keyw[37]="STRING";
keyw[13]="FOR";             keyw[38]="THEN";
keyw[14]="FORWARD";         keyw[39]="TO";
keyw[15]="FUNCTION";        keyw[40]="TYPE";
keyw[16]="GOTO";            keyw[41]="UNIT";
keyw[17]="IF";              keyw[42]="UNTIL";
keyw[18]="IMPLEMENTATION";  keyw[43]="USES";
keyw[19]="IN";              keyw[44]="VAR";
keyw[20]="INLINE";          keyw[45]="WHILE";
keyw[21]="INTERFACE";       keyw[46]="WITH";
keyw[22]="INTERRUPT";       keyw[47]="XOR";
keyw[23]="LABEL";           keyw[48]="STRING";
keyw[24]="MOD";             keyw[49]="VIRTUAL";
keyw[50]="CONSTRUCTOR";     keyw[51]="DESTRUCTOR";
}
/*-----------------------------------------------
                                        CloseAll
 ------------------------------------------------*/
CloseAll()
{
 fclose(fp);
 PrintCommand(request,aRIS,0,0,0,0);   /* reset printer */
 ClosePrinter(request);
 DeleteExtIO(request,sizeof(union printerIO));
 DeletePort(printerPort);
}
/*-----------------------------------------------
                                     InitPrinter
 ------------------------------------------------*/
InitPrinter()
{
 printerPort = CreatePort("my.print.port",0);
 request = (union printerIO *)CreateExtIO(printerPort,sizeof(union printerIO));
 if ((Error = OpenPrinter(request)) != NULL) {
  DeleteExtIO(request,sizeof(union printerIO));
  DeletePort(printerPort);
  printf("Can't open prt:\n");
  exit(FALSE);
 }
 PrintString(request,"\x00");      /* send nullbyte for new parameters */
 PrintCommand(request,aDEN1,0,0,0,0);   /* NLQ off  */
 PrintCommand(request,aSHORP2,0,0,0,0); /* Elite on */
}  
/*-----------------------------------------------
                                     OpenPrinter
 ------------------------------------------------*/
int OpenPrinter(request)
union printerIO *request;
{
 return(OpenDevice("printer.device",0,request,0));
}
/*-----------------------------------------------
                                    ClosePrinter
 ------------------------------------------------*/
int ClosePrinter(request)
union printerIO *request;
{
 return(CloseDevice(request));
}
/*-----------------------------------------------
                                     PrintString      
 ------------------------------------------------*/
int PrintString(request,string)
union printerIO *request;
char *string;
{ 
 request -> ios.io_Command = CMD_WRITE;
 request -> ios.io_Data    = (APTR)string;
 request -> ios.io_Length  = -1;
 return(DoIO(request));
}
/*-----------------------------------------------
                                    PrintCommand         
 ------------------------------------------------*/
  int PrintCommand(request,command,p0,p1,p2,p3)
  union printerIO *request;
  int command,p0,p1,p2,p3;
  {
   request -> iopc.io_Command = PRD_PRTCOMMAND;
   request -> iopc.io_PrtCommand = command;
   request -> iopc.io_Parm0 = p0;
   request -> iopc.io_Parm1 = p1;
   request -> iopc.io_Parm2 = p2;
   request -> iopc.io_Parm3 = p3;
   return(DoIO(request));
  }
/*-----------------------------END-of-FILE-------------------------------*/