/* Collection of code snippets by Arne VajhÃ¸j */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
/*
 *  NICE - utility
 *
 *  Programmed: Arne Vajhøj (august 1990)
 *
 */

/* main-program for NICE-utility */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define EXTERN
#include "glob.h"

/*  memory managament  */

#ifdef vms
void *lib$vm_malloc(int nb);
void *lib$vm_realloc(void *mp,int nb);
void lib$vm_free(void *mp);
#define malloc lib$vm_malloc
#define realloc lib$vm_realloc
#define free lib$vm_free
#endif

void *nice_malloc(int nb)
{
   char *mp;
   mp = (char *)malloc(nb);
   if (mp!=NULL) {
      return mp;
   } else {
      fprintf(stderr,"Error allocating dynamic memory\n");
      exit(ABORT);
   }
   return NULL;
}

void *nice_realloc(void *mp,int nb)
{
   mp = (char *)realloc(mp,nb);
   if (mp!=NULL) {
      return mp;
   } else {
      fprintf(stderr,"Error reallocating dynamic memory\n");
      exit(ABORT);
   }
   return NULL;
}

void nice_free(void *mp)
{
   free(mp);
   return;
}

void changestate(char *s)
{
   char *s2;
   s2 = (char *) nice_malloc(16);
   cvtupp(s,s2);
   if (strcmp(s2,"ASSIGNSPACE") == 0) { assignspace = TRUE; return; };
   if (strcmp(s2,"NOASSIGNSPACE") == 0) { assignspace = FALSE; return; };
   if (strcmp(s2,"DOCUMENT") == 0) { document = TRUE; return; };
   if (strcmp(s2,"NODOCUMENT") == 0) { document = FALSE; return; };
   if (strcmp(s2,"TRANSLATE") == 0) { translate = TRUE; return; };
   if (strcmp(s2,"NOTRANSLATE") == 0) { translate = FALSE; return; };
   if (strcmp(s2,"DEFINITION") == 0) { definition = TRUE; return; };
   if (strcmp(s2,"NODEFINITION") == 0) { definition = FALSE; return; };
   if (strcmp(s2,"CALLING") == 0) { calling = TRUE; return; };
   if (strcmp(s2,"NOCALLING") == 0) { calling = FALSE; return; };
   if (strcmp(s2,"FORCE") == 0) { forcecomm = TRUE; return; };
   if (strcmp(s2,"NOFORCE") == 0) { forcecomm = FALSE; return; };
   if (strcmp(s2,"COMM1") == 0) { commstyle = 1; return; };
   if (strcmp(s2,"COMM2") == 0) { commstyle = 2; return; };
   if (strcmp(s2,"BEGINBREAK") == 0) { beginbreak = FALSE; return; };
   if (strcmp(s2,"BREAKBEGIN") == 0) { beginbreak = TRUE; return; };
   if (strcmp(s2,"ENDINDENT") == 0) { endind = TRUE; return; };
   if (strcmp(s2,"NOENDINDENT") == 0) { endind = FALSE; return; };
   if (strcmp(s2,"IDLOWER") == 0) { idstyle = LOWCAS; return; };
   if (strcmp(s2,"IDUPPER") == 0) { idstyle = UPPCAS; return; };
   if (strcmp(s2,"IDMIXED") == 0) { idstyle = MIXCAS; return; };
   if (strcmp(s2,"IDCAP") == 0) { idstyle = MIXCAS; return; };
   if (strcmp(s2,"KEYWORDLOWER") == 0) { keywrdstyle = LOWCAS; return; };
   if (strcmp(s2,"KEYWORDUPPER") == 0) { keywrdstyle = UPPCAS; return; };
   if (strcmp(s2,"KEYWORDMIXED") == 0) { keywrdstyle = MIXCAS; return; };
   if (strcmp(s2,"KEYWORDCAP") == 0) { keywrdstyle = MIXCAS; return; };
   if (strcmp(s2,"KWLOWER") == 0) { keywrdstyle = LOWCAS; return; };
   if (strcmp(s2,"KWUPPER") == 0) { keywrdstyle = UPPCAS; return; };
   if (strcmp(s2,"KWMIXED") == 0) { keywrdstyle = MIXCAS; return; };
   if (strcmp(s2,"KWCAP") == 0) { keywrdstyle = MIXCAS; return; };
   if (strcmp(s2,"INDENTTAB") == 0) { indfact = -1; return; };
   if (strcmp(s2,"INDENT0") == 0) { indfact = 0; return; };
   if (strcmp(s2,"INDENT1") == 0) { indfact = 1; return; };
   if (strcmp(s2,"INDENT2") == 0) { indfact = 2; return; };
   if (strcmp(s2,"INDENT3") == 0) { indfact = 3; return; };
   if (strcmp(s2,"INDENT4") == 0) { indfact = 4; return; };
   if (strcmp(s2,"INDENT5") == 0) { indfact = 5; return; };
   if (strcmp(s2,"INDENT6") == 0) { indfact = 6; return; };
   if (strcmp(s2,"INDENT7") == 0) { indfact = 7; return; };
   if (strcmp(s2,"INDENT8") == 0) { indfact = 8; return; };
   if (strcmp(s2,"INDTAB") == 0) { indfact = -1; return; };
   if (strcmp(s2,"IND0") == 0) { indfact = 0; return; };
   if (strcmp(s2,"IND1") == 0) { indfact = 1; return; };
   if (strcmp(s2,"IND2") == 0) { indfact = 2; return; };
   if (strcmp(s2,"IND3") == 0) { indfact = 3; return; };
   if (strcmp(s2,"IND4") == 0) { indfact = 4; return; };
   if (strcmp(s2,"IND5") == 0) { indfact = 5; return; };
   if (strcmp(s2,"IND6") == 0) { indfact = 6; return; };
   if (strcmp(s2,"IND7") == 0) { indfact = 7; return; };
   if (strcmp(s2,"IND8") == 0) { indfact = 8; return; };
   nice_free(s2);
   fprintf(stderr,"WARNING: illegal control-directive\n");
   return;
}

void setup()
{
   FILE *ctrlfp;
   char x[16];
   indfact = def_indfact;
   idstyle = def_idstyle;
   keywrdstyle = def_keywrdstyle;
   beginbreak = def_beginbreak;
   commstyle = def_commstyle;
   forcecomm = def_forcecomm;
   endind = def_endind;
   definition = def_definition;
   calling = def_calling;
   translate = def_translate;
   document = def_document;
   assignspace = def_assignspace;
   ctrlfp = fopen("NICECTRL","r");
   if (ctrlfp == NULL) return;
   while (!feof(ctrlfp)) {
      fscanf(ctrlfp,"%s",&x);
      changestate((char *) &x);
   }
   fclose(ctrlfp);
   return;
}

main (int argc,char *argv[])
{
   int i;
   fprintf(stderr,"%s\n",nice_vers);
   setup();
   if (argc>1) {
      freopen(argv[1],"r",stdin);
      if (argc>2) {
          freopen(argv[2],"w",stdout);
      } else {
#ifndef UNSAFE
          fprintf(stderr,"ERROR: No output-file specified\n");
          return ABORT;
#else
          freopen(argv[1],"w",stdout);
#endif
      }
      for (i=3;i<argc;i++) changestate(argv[i]);
      if (definition || calling) {
         lfp = fopen(nicelist,"w");
         fprintf(lfp,">>>> %s <<<<\n",argv[1]);
      }
      if (document) {
         dfp = fopen(nicedoc,"w");
         fprintf(dfp,">>>> %s <<<<\n",argv[1]);
      }
      ind = 0;
      ind2 = 0;
      dind = 0;
      skipnewl = FALSE;
      notfirst = FALSE;
      fin = FALSE;
      blocked = FALSE;
      misssemi = FALSE;
      if(assignspace) {
         strcpy(asgstr," := ");
      } else {
         strcpy(asgstr,":=");
      }
      ininterf = FALSE;
      misspseudo = FALSE;
      comm = (char *) nice_malloc(1);
      comm[0] = '\0';
      buf = (char *) nice_malloc(256);
      buf[0] = '\0';
      top = -1;
      entryname = (char **) nice_malloc(100*sizeof(char *));
      if (yyparse () == 0) {
         if (definition || calling) fclose(lfp);
         if (document) fclose(dfp);
         return OK;
      } else {
         if (definition || calling) fclose(lfp);
         if (document) fclose(dfp);
         return ABORT;
      }
   } else  {
      fprintf(stderr,"ERROR: No input-file specified\n");
      return ABORT;
   }
}
