/*-------------------------------------------------------------------------
  Program   : CPrint.c
  Function  : Printutility for C-sourcecode, prints bolded keywords 
  Compiler  : Aztec C V3.6 a
  Options   : cc CPrint.c -s +l   ln CPrint.o -lc32
  Hints     : 
  Copyright : Public Domain
  Author    : ASSO 1990                                     
  -------------------------------------------------------------------------*/
#include <intuition/intuition.h>
#include <devices/printer.h>
#include <exec/types.h>
#include <stdio.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   40
#define MAXKEY    30

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],
     *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: CPrint <filename.c>\n"); 
  printf("C-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;
 char current,prev,prev1;

 comment=string=quote=FALSE;
 current=prev=prev1='\0';
 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 */
    PrintString(request,&current);
    if (prev == '*' && current == '/') {
      comment=FALSE;
      ITALICS_OFF;
      current='\0';
     }
   }
   if (string) {                              /* string off */
      PrintString(request,&current);
       if (current == '\"')
        if (prev != '\\') {
         string=FALSE;
         current='\0';
        } else
        if (prev1 == '\\') {
         string=FALSE;
         current='\0';
        }
     }   
     if (quote) {                              /* quote off */
      PrintString(request,&current);
       if (current == '\'')
        if (prev != '\\') {
         quote=FALSE;
         current='\0';
        } else
        if (prev1 == '\\') {
         quote=FALSE;
         current='\0';
        }
     }
    if (!string && !comment && !quote && current != '\0') {
       if (prev == '/' && current == '*') {  /* comment on */
         comment=TRUE;
         if (j>0) {
          word[--j]='\0'; j=0;
          checkword();
         }
         ITALICS_ON;
         PrintString(request,"/*");
       } 
       else
       if (current == '"') {                 /* " string on  */
         string=TRUE;
         if (j>0) {
          checkword(); j=0;
         }
        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  */
   prev1=prev;
  prev=current;
 }/*  while */
}
/*-----------------------------------------------
                                       checkword
 ------------------------------------------------*/
checkword()
{
 int i=0, nokey=1;

while ((nokey != 0) && (i<=MAXKEY))
  nokey = strcmp(word,keyw[i++]);

 if (nokey==0){
     BOLD_ON;
      PrintString(request,word);
     BOLD_OFF;
 } 
  else 
   PrintString(request,word); i=0;
  while(i<=MAXWORD) /* delete word (save version) */
   word[i++]='\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()
{
keyw[ 0]="auto";      keyw[15]="if"; 
keyw[ 1]="break";     keyw[16]="int";
keyw[ 2]="case";      keyw[17]="long";
keyw[ 3]="char";      keyw[18]="register";
keyw[ 4]="continue";  keyw[19]="return";
keyw[ 5]="default";   keyw[20]="short";
keyw[ 6]="do";        keyw[21]="sizeof";
keyw[ 7]="double";    keyw[22]="static";
keyw[ 8]="else";      keyw[23]="struct";
keyw[ 9]="entry";     keyw[24]="switch";
keyw[10]="enum";      keyw[25]="typedef";
keyw[11]="extern";    keyw[26]="union";
keyw[12]="float";     keyw[27]="unsigned";
keyw[13]="for";       keyw[28]="void";
keyw[14]="goto";      keyw[29]="while";
}
/*-----------------------------------------------
                                        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-------------------------------*/