/* Decode a character string, and return an array of integer symbol */
/* numbers. This routine is responsible for interpreting all escape */
/* sequences. At present the following escape sequences are defined */
/* (the letter following the \ may be either upper or lower case): */

/* \u       :      up one level (returns -1) */
/* \d       :      down one level (returns -2) */
/* \b       :      backspace (returns -3) */
/* \+       :      toggles overline mode (returns -4) */
/* \-       :      toggles underline mode (returns -5) */
/* \\       :      \, returns the code for backslash */
/* \gx      :      greek letter corresponding to roman letter x */
/* \fn      :      switch to Normal font */
/* \fr      :      switch to Roman font */
/* \fi      :      switch to Italic font */
/* \fs      :      switch to Script font */
/* \(nnn)   :      Hershey symbol number nnn (any number of digits) */

#include "plplot.h"
#include <stdio.h>    /* include for definition of NULL */
#include <string.h>

static char font[] = "nrisnris";
static char greek[] = "ABGDEZYHIKLMNCOPRSTUFXQWabgdezyhiklmncoprstufxqw";

extern short int *hersh[];

void pldeco(symbol,length,text)
int symbol[],*length;
char text[];
{
      int ch,icol,ifont,ig,j,lentxt;
      char test;

      /* Initialize parameters. */

      lentxt=strlen(text);
      *length=0;
      j=0;
      gatt(&ifont,&icol);

      /* Get next character; treat non-printing characters as spaces. */

lab100:
      j=j+1;
      if (j>lentxt) return;
      test=text[j-1];
      ch=test;
      if (ch<0)   ch = 32;

      if (ch>175) ch = 32;

      /* Test for escape sequence (\) */

      if (ch=='\\') {
        if ((lentxt-j)>=1) {
          test=text[j];
          if (test=='\\')
            j = j+1;
          else if (test=='U' || test=='u') {
            *length = *length + 1;
            symbol[*length-1] = -1;
            j = j+1;
            goto lab100;
          }
          else if (test=='D' || test=='d') {
            *length = *length + 1;
            symbol[*length-1] = -2;
            j = j+1;
            goto lab100;
          }
          else if (test=='B' || test=='b') {
            *length = *length + 1;
            symbol[*length-1] = -3;
            j = j+1;
            goto lab100;
          }
          else if (test=='+') {
            *length = *length + 1;
            symbol[*length-1] = -4;
            j = j+1;
            goto lab100;
          }
          else if (test=='-') {
            *length = *length + 1;
            symbol[*length-1] = -5;
            j = j+1;
            goto lab100;
          }
          else if (test=='(') {
            *length = *length + 1;
            symbol[*length-1] = 0;
            j = j+2;
lab10:
            if ('0'<=text[j-1] && text[j-1]<='9') {
              symbol[*length-1] = symbol[*length-1]*10 + text[j-1] - '0';
              j = j+1;
              goto lab10;
            }
            if (text[j-1]!=')') j = j-1;
            goto lab100;
          }
          else if (test=='F' || test=='f') {
            test=text[j+1];
            ifont = strpos(font,test) + 1;
            if (ifont>4) ifont = ifont-4;
            if (ifont==0) ifont = 1;
            j = j+2;
            goto lab100;
          }
          else if (test=='G' || test=='g') {
            test=text[j+1];
            ig = strpos(greek,test) + 1;
            *length = *length + 1;
            symbol[*length-1] = *(hersh[ifont-1] + 127 + ig);
            j = j+2;
            goto lab100;
          }
        }
      }

      /* Decode character. */

      *length = *length + 1;
      symbol[*length-1] = *(hersh[ifont-1]+ch);
      goto lab100;
}
