// ==============================================================
//#+ This module generated by Mumps Compiler
//#+ Wed Apr  2 13:08:07 2025
//#+ Portions of this code are part of the Mumps Compiler
//#+
//#+ Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005,
//#+ 2010, 2017, 2020, 2022, 2024 by Kevin C. O'Kane
//#+
//#+ Kevin C. O'Kane, Ph.D.
//#+ Professor Emeritus
//#+ Computer Science Department
//#+ University of Northern Iowa
//#+ Cedar Falls, IA 50614-0507
//#+ kc.okane@gmail.com
//#+ okane@uni.edu
//#+ https://threadsafebooks.com
//#+ https://www.cs.uni.edu/~okane
//#+ -----------------------------------
//#+
//#+ Consult individual modules for copyright details
//#+ The runtime libraries are covered by the following license:
//#+
//#+ This library is free software; you can redistribute it and/or
//#+ modify it under the terms of the GNU Lesser General Public
//#+ License as published by the Free Software Foundation; either
//#+ version 2.1 of the License, or (at your option) any later version.
//#+
//#+ This library is distributed in the hope that it will be useful,
//#+ but WITHOUT ANY WARRANTY; without even the implied warranty of
//#+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//#+ Lesser General Public License for more details.
//#+
//#+ You should have received a copy of the GNU Lesser General Public
//#+ License along with this library; if not, write to the Free Software
//#+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//#+
//#+ This code is unverified - use at your own risk
//#+==============================================================

#undef _MDH_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <setjmp.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <mumpsc/inline.h>
#include <mumpsc/defines.h>
#include <mumpsc/strmax.h>
#include <mumpsc/libmpscpp.h>
#include <mumpsc/sym.h>
#include <mumpsc/builtin.h>
#include <mumpsc/compiler.h>
#define FORPARM STR_MAX  /* Size of For loop parameter */
#define CSTR (char *)
#define UNS unsigned char *
#define uns unsigned char *
#define CCHR const char *
#include <mumpsc/keyfix.h>
#include <mumpsc/globalOrder.h>
#include <mumpsc/stateVector.h>
int (*__label_lookup)(char *);
const char * (*__text_function)(int);
void fcn(MSV *);
void zfcn(unsigned char *, unsigned char *, struct MSV *); /* z functions */
char * sym_(int, unsigned char *, unsigned char *, struct MSV *); /* symbol table */
char * makeLock(char *, char *, char *, char, char *); /* global array lock service */
char * cleanLocks(char *); /* global array lock service */
char * releaseLock(char *, char *, char *); /* global array lock service */
char * eraseLocks(char *); /* global array lock service */
void add(const char *a,const char *b,char *c); /* math function */
void sub(const char *a,const char *b,char *c); /* math function */
void mult(const char *a,const char *b,char *c); /* math function */
void divx(const char *a,const char *b,char *c); /* math function */
void divi(const char *a,const char *b,char *c); /* math function */
void _fnumber(unsigned char *,unsigned char *,
              unsigned char *,unsigned char *, MSV *); /* math function */
int DBSem = 0; /* deadlock prevention */

static inline int floatstring(const char * p) {
    int f=0;
    for (; *p!=0; p++) {
        if (    isdigit(*p) ||
                *p == '-' ||
                *p == '+' ) continue;
        if (    *p=='.' ||
                tolower(*p)=='e' ) {
            f=1;
            continue;
            }
        return 0;
        }
    return f;
    }

inline int numcomp2(unsigned char *aa) {
    if (floatstring((char *) aa)) return (atof( (char *) aa) != 0.0);
    return atoi((char *) aa);
    }

inline char * strxcat(char * a, char *b) {
    while (*a) a++;
    while( (*(a) = *(b)) ) {
        a++;
        b++;
        }
    return a;
    }

inline char * strcatz(char * out, char *in) {
    while (*out) out++;
    while (*(out) = *(in)) {
        out++;
        in++;
        }
    *(out++) = '';
    *out = 0;
    return (char *) out;
    }

void SyncClock() {
    DBSem = 1;
    }

unsigned int set_alarm (unsigned int seconds) {
    struct itimerval old, xnew;
    xnew.it_interval.tv_usec = 0;
    xnew.it_interval.tv_sec = 0 ; // interval between alarms
    xnew.it_value.tv_usec = 0;
    xnew.it_value.tv_sec = (long int) 0;
    if (setitimer (ITIMER_REAL, &xnew, &old) < 0)
        return 0;
    else
        return old.it_value.tv_sec;
    }

#include <mumpsc/global.h>  /* Global array definitions */
/* Interface to Mglobal(): */
/* g = command */
/* key = key sought or stored */
/* bd = value returned or stored */
/* svPtr = address of state vector */
#ifndef SUB
#include <mumpsc/fcns.h>  /* System support functions */
#include <mumpsc/sysfunc.h>  /* Many common global variables */
#endif

// !/usr/bin/mumps

//=================================================================================
//  zmain
//=================================================================================

int main(int argc, char *argv[]) {

//#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//#+ Intermediate temporary variables used during execution of string expressions
//#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    char * catx; // intermediate concat ptr
    char *_ftmp = new char[STR_MAX]; /* intermediate temp */
    /*** Temporary variables */
    char for1_incr[FORPARM], for1_lim[FORPARM], for1_init[FORPARM];
    int for1F;
    int for1T;
    char for2_incr[FORPARM], for2_lim[FORPARM], for2_init[FORPARM];
    int for2F;
    int for2T;
    jmp_buf for0J;
    jmp_buf for1J;
    jmp_buf for2J;
    jmp_buf Efor0J;
    int tpx0;
    unsigned char *tmp0 = new unsigned char[STR_MAX];
    unsigned char *tmp1 = new unsigned char[STR_MAX];
    unsigned char *tmp2 = new unsigned char[STR_MAX];
    unsigned char *tmp3 = new unsigned char[STR_MAX];
    unsigned char *_dummy = new unsigned char[STR_MAX]; /* temporary variable */
    unsigned char *atmp =new unsigned char[STR_MAX]; /* temporary variable */
    unsigned char *xgtmp = new unsigned char [STR_MAX]; /* temporary variable */
    unsigned char *gtmpL = new unsigned char[STR_MAX]; /* temporary variable */
    unsigned char *tmp = new unsigned char[STR_MAX]; /* temporary variable */
    unsigned char *gtmp = new unsigned char [2*STR_MAX]; /* temporary variable */
    int _label_lookup(char *); /* resolution of labels */
    const char * _text_function(int);
    static unsigned char _ReturnVal[STR_MAX]; /* function returns */
    int _ArgCount=0; /* Nbr of args to extrinsic */
    long _i,_jj,_j,_k,_new;
    int _SymPush=0,f=0;
    int g=0;
    jmp_buf _doStack[128]; /* <do> return stack mechanism */
    short   _doStackN[128]= {0}; /* <do> return stack mechanism */
    static int     _doStx=0; /* <do> return stack mechanism */
    char _Refs[10][32];
    int _Refsx=0;

    if (svPtr == NULL) svPtr = AllocSV();

    _SymPush=0; // zap pushed tables

    svPtr->in_file[5]=stdin; // console default/
    svPtr->out_file[5]=stdout; // console default

    strcpy(svPtr->UDAT,"data.dat");
    strcpy(svPtr->UKEY,"key.dat");

    if ( 0) { // no alarm if slice is zero
        signal(SIGALRM, SyncClock);
        set_alarm(1);
        }

    __label_lookup = _label_lookup;
    __text_function = _text_function;

    $SymPut("%",argv[0]);

    if (argc > 0) {
        for (int i = 0; i < argc; i++) {
            char t[STR_MAX];
            sprintf(t,"%%%d", i);
            $SymPut(t, argv[i]);
            }
        }

    try {

//=================================================================================
        svPtr->LineNumber=4; //  s t=$zd1
//=================================================================================

//--------------
//       Set
//--------------

        strcpy((char *) &gtmp[1],(const char *) "$zd1");
        zfcn(gtmp,tmp1,svPtr);
        sym_(SYMSTORE, (UNS) "t", (UNS) tmp1, svPtr); // Store result s274

//=================================================================================
        svPtr->LineNumber=5; //  for i=1:1:10000 s ^a(i)=i
//=================================================================================

//--------------
//       For
//--------------

        strcpy(for1_lim, "");
        strmove((UNS) for1_init, (UNS) "1"); // init value m2534
        strmove( (UNS) for1_incr, (UNS) "1"); // inc/dec m2575
        strmove((UNS) for1_lim, (UNS) "10000"); // limit value

        if (strchr((char *) for1_incr, '.')) // inc/dec is float
            for1F = atof(for1_incr) < 0.0 ? 1 : 0; // incr or decr?
        else // inc/dec is integer
            for1F = atol(for1_incr) < 0 ? 1 : 0; // incr or decr?

        for ( sym_(SYMSTORE, (UNS) "i", (UNS) for1_init, svPtr); // init 2618

                for1F ? // loop limit test direction
                numcomp((UNS) sym_(11, (UNS) "i",
                                   _dummy, svPtr),
                        (UNS) for1_lim) >= 0 // GE limit value?
                : // ternary operator
                numcomp((UNS) sym_(11, (UNS) "i",
                                   _dummy,svPtr),
                        (UNS) for1_lim) <= 0; // LE limit value

                add( sym_(11, (UNS) "i", _dummy, svPtr), // inc/dec loop var
                     for1_incr, (char *) _dummy),
                sym_(SYMSTORE, (UNS) "i", (UNS) _dummy, svPtr) ) // store rslt

            if ((_i = setjmp(for1J)) == 0) goto For1; // else continue

            else if (_i == 100) goto For1Exit; // f273

        goto For1Exit; // bypass block

For1: // for loop body entry point B

        do { // for loop body
            svPtr->LineNumber = 5;

//--------------
//       Set
//--------------

            strmove((UNS) tmp1, (UNS) "a\xce"); // M3701
            if (sym_(SYMGET, (UNS) "i", (UNS) tmp0, svPtr) == NULL) // get value
                VariableNotFound1(svPtr->LineNumber, (UNS) "i"); // not found
            catx = strxcat((char *) tmp1, (char *) tmp0); // a2926
            strcat((char *) catx, (char *) "\xcf"); //a3956

                {
                if (sym_(SYMGET, (UNS) "i", (UNS) tmp2, svPtr) == NULL) // get value
                    VariableNotFound1(svPtr->LineNumber, (UNS) "i"); // not found
                f = Mglobal(STORE, tmp1, tmp2, svPtr);
                }

            }
        while (0);
        longjmp(for1J, _i); // FOR loop 1 close

For1Exit: ; // exit for loop


//=================================================================================
        svPtr->LineNumber=6; //  for i=1:1:10000 s k=^a(i)
//=================================================================================

//--------------
//       For
//--------------

        strcpy(for2_lim, "");
        strmove((UNS) for2_init, (UNS) "1"); // init value m2534
        strmove( (UNS) for2_incr, (UNS) "1"); // inc/dec m2575
        strmove((UNS) for2_lim, (UNS) "10000"); // limit value

        if (strchr((char *) for2_incr, '.')) // inc/dec is float
            for2F = atof(for2_incr) < 0.0 ? 1 : 0; // incr or decr?
        else // inc/dec is integer
            for2F = atol(for2_incr) < 0 ? 1 : 0; // incr or decr?

        for ( sym_(SYMSTORE, (UNS) "i", (UNS) for2_init, svPtr); // init 2618

                for2F ? // loop limit test direction
                numcomp((UNS) sym_(11, (UNS) "i",
                                   _dummy, svPtr),
                        (UNS) for2_lim) >= 0 // GE limit value?
                : // ternary operator
                numcomp((UNS) sym_(11, (UNS) "i",
                                   _dummy,svPtr),
                        (UNS) for2_lim) <= 0; // LE limit value

                add( sym_(11, (UNS) "i", _dummy, svPtr), // inc/dec loop var
                     for2_incr, (char *) _dummy),
                sym_(SYMSTORE, (UNS) "i", (UNS) _dummy, svPtr) ) // store rslt

            if ((_i = setjmp(for2J)) == 0) goto For2; // else continue

            else if (_i == 100) goto For2Exit; // f273

        goto For2Exit; // bypass block

For2: // for loop body entry point B

        do { // for loop body
            svPtr->LineNumber = 6;

//--------------
//       Set
//--------------

            strmove((UNS) tmp2, (UNS) "^a\x01"); // init p674
            if (sym_(SYMGET, (UNS) "i", (UNS) tmp1, svPtr) == NULL) // get value
                VariableNotFound1(svPtr->LineNumber, (UNS) "i"); // not found
            strcatx((UNS) tmp2, (UNS) tmp1);
            if (!Mglobal(RETRIEVE, tmp2, tmp3, svPtr)) // retrieve global
                VariableNotFound(svPtr->LineNumber, tmp2);
            sym_(SYMSTORE, (UNS) "k", (UNS) tmp3, svPtr); // Store result s274
            }
        while (0);
        longjmp(for2J, _i); // FOR loop 1 close

For2Exit: ; // exit for loop


//=================================================================================
        svPtr->LineNumber=7; //  w $zd1-t,!
//=================================================================================

//--------------
//       Write
//--------------

        if (svPtr->out_file[svPtr->io]==NULL)
            ErrorMessage("Write to input file", svPtr->LineNumber); // m1944
        strcpy((char *) &gtmp[1],(const char *) "$zd1");
        zfcn(gtmp,tmp0,svPtr);
        if (sym_(SYMGET, (UNS) "t", (UNS) tmp1, svPtr) == NULL) // get value
            VariableNotFound1(svPtr->LineNumber, (UNS) "t"); // not found
        sub((char *) tmp0,(char *) tmp1,(char *) tmp2);
        svPtr->hor[svPtr->io] += fprintf(svPtr->out_file[svPtr->io], "%s",
                                         tmp2);
        fprintf(svPtr->out_file[svPtr->io], "\n");
        svPtr->hor[svPtr->io]=0;
        svPtr->ver[svPtr->io]++;

//=================================================================================
_epilogue:
        tmp0[0]=0;
        tmp1[0]=0;  /*  Function Epilogue */
        Mltoa(getpid(),atmp);
        if (svPtr->LockDone) cleanLocks((char *) atmp);
        Mglobal(CLOSE,tmp0,tmp1,svPtr);  /* Close Globals */

        } // end of try block

    catch(...) {
        if (GlobalExceptionCode == INPUT_ERROR) cout << "Input Error" << endl;
        else if (GlobalExceptionCode == INPUT_LENGTH) cout << "Input Length Error" << endl;
        else if (GlobalExceptionCode == SYMTAB_ERROR) cout << "Symbol Table Error" << endl;
        else if (GlobalExceptionCode == GLOBAL_NOT_FOUND) cout << "Global Not Found Error" << endl;
        else if (GlobalExceptionCode == INTERPRETER_ERROR) cout << "Interprerer Error" << endl;
        else if (GlobalExceptionCode == INVALID_CONVERSION) cout << "Invalid Conversion Error" << endl;
        else if (GlobalExceptionCode == DATA_RANGE_ERROR) cout << "Data Range Error" << endl;
        else if (GlobalExceptionCode == NUMERIC_RANGE) cout << "Numeric Range Error" << endl;
        else if (GlobalExceptionCode == UNSPECIFIED) cout << "Unspecified Error" << endl;
        cout << "Uncaught error in or near line "
             << svPtr->LineNumber << endl;
        Mglobal(CLOSE,tmp0,tmp1,svPtr);  /* Close Globals */
        }

    return EXIT_SUCCESS;
    }  /* Terminate */
//=================================================================================


int _label_lookup(char *label) {
    return -1;
    }
const char * _text_function(int disp) {
    return NULL;
    }

