/* This subroutine implements Conditional Assembly.
 *
 * $Log: if.c,v $
 * Revision 1.2  1997/06/04 01:10:57  david
 * ANSI-fied the program.
 *
 */

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

static char *RcsId = "$Id: if.c,v 1.2 1997/06/04 01:10:57 david Exp $";

void aif( char *label, char *opcode, char *operand, char *lptr )
{
static char *strerr = "aif: Unexpected End-of-File while searching for 'else' or 'endif'.";

labproc( label );
	
if ( atoh( proper( label, opcode, operand, NULL ) ) )
   { /* Keep going until ENDIF or ELSE */
   printl( (long) -1, NULL, 1, lptr ); /* Print saved line */
   while ( 1 )
      {
      if ( (lptr = getline()) == NULL )
	 {
         xerror( strerr );
	 return;
         }
      doparse( lptr, &label, &opcode, &operand );
      lower( opcode );
      if ( strcmp( opcode, "else" ) == 0 ) /* Skip ELSE statements */
         {
	 do {
	    if ( (lptr = getline()) == NULL )
	       {
	       xerror( strerr );
	       return;
               }
	    doparse( lptr, &label, &opcode, &operand );
	    lower( opcode );
            } while ( strcmp( opcode, "endif" ) != 0 );
	 }
      if ( strcmp( opcode, "endif" ) == 0 )
	 {
	 printl( (long) -1, NULL, 1, lptr );
	 return;
	 }
      doline( lptr ); /* Go ahead and try to assemble it */
      } /* END FOREVER LOOP */
   }
 else /* Skip until ELSE or ENDIF */
   {
   printl( (long) -1, NULL, 1, lptr ); /* Print saved line */
   do {
      if ( (lptr = getline()) == NULL )
	 {
	 xerror( strerr );
	 return;
	 }
      doparse( lptr, &label, &opcode, &operand );
      lower( opcode );
      if ( strcmp( opcode, "else" ) == 0 )
	 {
	 printl( (long) -1, NULL, 1, lptr );
	 while ( 1 ) /* Do until "ENDIF" */
	    {
	    if ( (lptr = getline()) == NULL )
	       {
	       xerror( strerr );
	       return;
	       }
	    doparse( lptr, &label, &opcode, &operand );
	    lower( opcode );
	    if ( strcmp( opcode, "endif" ) == 0 )
	       {
               printl( (long) -1, NULL, 1, lptr );
	       return;
	       }
	    doline( lptr ); /* Process ELSE statements */
	    }
	 } /* End IF else */
      } while ( strcmp( opcode, "endif" ) != 0 );
   printl( (long) -1, NULL, 1, lptr );
   }
}
