Subversion Repositories Kolibri OS

Rev

Rev 7495 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2.  
  3. #include "func.h"
  4. #include "parser.h"
  5. //#include <math.h>
  6. //#include <stdlib.h>
  7. //#include <stdio.h>
  8.  
  9. // token types
  10. #define DELIMITER 1
  11. #define VARIABLE 2
  12. #define NUMBER 3
  13. #define FUNCTION 4
  14. #define FINISHED 10
  15.  
  16. //#define allocmem(x) malloc(x)
  17. //#define freemem(x) free(x)
  18.  
  19. double epsilon = 1e-6;
  20.  
  21. // structure for most parser functions
  22.  
  23.         char token[80];
  24.         int token_type;
  25.         char *prog;
  26.  
  27.         int code;    // error code
  28.  
  29.  
  30. variable_callback *find_var;
  31.  
  32. struct double_list  
  33. {
  34.         double val;
  35.         int code;                       // êîä îøèáêè
  36.         double_list *next;
  37. };
  38.  
  39. double tg(double d)
  40. {
  41.         double cosd = cos(d);
  42.         if (fabs(cosd) < epsilon)
  43.         {
  44.                 serror(ERR_OVERFLOW);
  45.                 return 0.0;
  46.         }
  47.         return sin(d) / cosd;
  48. }
  49.  
  50. double ctg(double d)
  51. {
  52.         double sind = sin(d);
  53.         if (fabs(sind) < epsilon)
  54.         {
  55.                 serror(ERR_OVERFLOW);
  56.                 return 0.0;
  57.         }
  58.         return cos(d) / sind;
  59. }
  60.  
  61. double exp(double x)
  62. {
  63.         __asm {
  64.                 fld     x
  65.                 FLDL2E
  66.                 FMUL
  67.  
  68.                 FLD st(0)
  69.  
  70.                 FLD1
  71.  
  72.                 FXCH
  73.                 FPREM
  74.                 F2XM1
  75.                 fadd
  76.                 FSCALE
  77.                 FSTP st(1)
  78.         }
  79.        
  80. }
  81.  
  82. double log(double x)
  83. {
  84.         //return 0.0;
  85.         if (x <= 0)
  86.         {
  87.                 serror(ERR_OVERFLOW);
  88.                 //return 0.0;
  89.                 __asm {
  90.                         fldz
  91.                 }
  92.         }
  93.         __asm {
  94.                 FLD1
  95.                 FLD     x
  96.                 FYL2X
  97.                 FLDLN2
  98.                 FMUL
  99.         }
  100. }
  101.  
  102. double sqrt(double x)
  103. {
  104.         if (x < 0)
  105.         {
  106.                 serror(ERR_BADPARAM);
  107.                 __asm {
  108.                         fldz
  109.                 }
  110.         }
  111.         __asm {
  112.                 fld x
  113.                 fsqrt
  114.         }
  115. }
  116.  
  117. double atan(double x)
  118. {
  119.         serror(ERR_GENERAL);
  120.         return 0.0; // â ëîì
  121. }
  122.  
  123. double pow(double x, double y)
  124. {
  125.         return exp(y * log(x)); //
  126. }
  127.  
  128. double func_pi()
  129. {
  130.         return 3.14159265358979;
  131. }
  132.  
  133. double func_eps()
  134. {
  135.         return epsilon;
  136. }
  137.  
  138. double func_if(double_list *p)
  139. {
  140.         double_list *a, *b, *c;
  141.         a = p;
  142.         b = a->next;
  143.         if (!b)
  144.         {
  145.                 serror(ERR_BADPARAM);
  146.                 return 0.0;
  147.         }
  148.         c = b->next;
  149.         if (!c || c->next)
  150.         {
  151.                 serror(ERR_BADPARAM);
  152.                 return 0.0;
  153.         }
  154.         if (a->val != 0.0)
  155.         {
  156.                 if (b->code)
  157.                         code = b->code;
  158.                 return b->val;
  159.         }
  160.         else
  161.         {
  162.                 if (c->code)
  163.                         code = c->code;
  164.                 return c->val;
  165.         }
  166. }
  167.  
  168. double sum(double_list *p)
  169. {
  170.         double res = 0.0;
  171.         while (p)
  172.         {
  173.                 res += p->val;
  174.                 if (p->code)
  175.                         code = p->code;
  176.                 p = p->next;
  177.         }
  178.         return res;
  179. }
  180.  
  181. double func_min(double_list *p)
  182. {
  183.         if (!p)
  184.                 serror(ERR_BADPARAM);
  185.         double res = p->val;
  186.         p = p->next;
  187.         while (p)
  188.         {
  189.                 if (p->code)
  190.                         code = p->code;
  191.                 if (p->val < res)
  192.                         res = p->val;
  193.                 p = p->next;
  194.         }
  195.         return res;
  196. }
  197.  
  198. double func_max(double_list *p)
  199. {
  200.         if (!p)
  201.                 serror(ERR_BADPARAM);
  202.         double res = p->val;
  203.         p = p->next;
  204.         while (p)
  205.         {
  206.                 if (p->code)
  207.                         code = p->code;
  208.                 if (p->val > res)
  209.                         res = p->val;
  210.                 p = p->next;
  211.         }
  212.         return res;
  213. }
  214.  
  215. double avg(double_list *p)
  216. {
  217.         double res = 0.0;
  218.         int count = 0;
  219.         while (p)
  220.         {
  221.                 if (p->code)
  222.                         code = p->code;
  223.                 res += p->val;
  224.                 count++;
  225.                 p = p->next;
  226.         }
  227.         return res / count;
  228. }
  229.  
  230. double func_isnull(char *str)
  231. {
  232.         if (code != 0)
  233.                 return 0.0;
  234.         double tmp = find_var(str);
  235.         int c = code;
  236.         code = 0;
  237.         if (c != 0)
  238.                 return 1.0;
  239.         return 0.0;
  240. }
  241.  
  242. const double HALF = 0.5;
  243. double func_ceil(double val)    // õîòåë round, à ïîëó÷èëñÿ ceil...
  244. {
  245.         int x;
  246.         __asm fld val
  247.         __asm fld HALF                          // äà, êðèâîðóêî ^_^
  248.         __asm fadd
  249.         __asm fistp x
  250.         __asm fild x
  251. }
  252.  
  253. double func_round(double val)
  254. {
  255.         int x;
  256.         __asm fld val
  257.         __asm fld epsilon
  258.         __asm fadd
  259.         __asm fistp x
  260.         __asm fild x
  261. }
  262.  
  263. const double ALMOST_HALF = 0.5 - epsilon;
  264. double func_floor(double val)
  265. {
  266.         int x;
  267.         __asm fld val
  268.         __asm fld ALMOST_HALF
  269.         __asm fsub
  270.         __asm fistp x
  271.         __asm fild x
  272. }
  273.  
  274. double logic_xor(double a, double b)
  275. {
  276.         if (a == 0.0)
  277.                 if (b == 0.0)
  278.                         return 0.0;
  279.                 else
  280.                         return 1.0;
  281.         else
  282.                 if (b == 0.0)
  283.                         return 1.0;
  284.                 else
  285.                         return 0.0;
  286. }
  287.  
  288. double logic_and(double a, double b)
  289. {
  290.         if (a == 0.0)
  291.                 return 0.0;
  292.         else
  293.                 if (b == 0.0)
  294.                         return 0.0;
  295.                 else
  296.                         return 1.0;
  297. }
  298.  
  299. double logic_or(double a, double b)
  300. {
  301.         if (a == 0.0)
  302.                 if (b == 0.0)
  303.                         return 0.0;
  304.                 else
  305.                         return 1.1;
  306.         else
  307.                 return 1.0;
  308. }
  309.  
  310. double rand_seed;
  311. double func_rand(double max)
  312. {
  313.         double q = (257.0 * rand_seed + 739.0); // ÷èñëà îò áàëäû. íàäî âñòàâèòü ïðàâèëüíûå.
  314.         rand_seed = q - 65536.0 * func_floor(q / 65536.0); // äëÿ õîðîøåãî ðàñïðåäåëåíèÿ
  315.         return q - max * func_floor(q / max);    // äëÿ ìîäóëÿ
  316. }
  317.  
  318. double func_case(double_list *p)
  319. {
  320.         if (!p || !p->next)
  321.         {
  322.                 serror(ERR_BADPARAM);
  323.                 return 0.0;
  324.         }
  325.         double x = p->val;
  326.         int count = (int)p->next->val;
  327.         int i, k;
  328.  
  329.         double_list *cur = p->next->next;
  330.         k = count;
  331.         for (i = 0; i < count; i++)
  332.         {
  333.                 if (!cur)
  334.                 {
  335.                         serror(ERR_BADPARAM);
  336.                         return 0.0;
  337.                 }
  338.                 if (fabs(x - cur->val) < epsilon)
  339.                 {
  340.                         if (k != count + 1)
  341.                         {
  342.                                 serror(ERR_GENERAL);
  343.                                 return 0.0;
  344.                         }
  345.                         k = i;
  346.                 }
  347.                 cur = cur->next;
  348.         }
  349.  
  350.         for (i = 0; i < k; i++)
  351.         {
  352.                 if (!cur)
  353.                 {
  354.                         serror(ERR_BADPARAM);
  355.                         return 0.0;
  356.                 }
  357.                 cur = cur->next;
  358.         }
  359.         if (!cur)                                       // ïðîâåðêè áèï. äîñòàëè áèï.
  360.         {
  361.                 serror(ERR_BADPARAM);
  362.                 return 0.0;
  363.         }
  364.         if (cur->code)
  365.                 code = cur->code;
  366.         return cur->val;
  367. }
  368.  
  369. #define INF_ARGS -1
  370. #define STR_ARG -2
  371.  
  372. // represents general mathematical function
  373. typedef double(*matfunc0)();
  374. typedef double(*matfunc)(double);
  375. typedef double(*matfunc2)(double,double);
  376. typedef double(*matfunc3)(double,double,double);
  377. typedef double(*matfunc_inf)(double_list*);
  378. typedef double(*matfunc_str)(char*);
  379.  
  380. // used to link function name to the function
  381. typedef struct  
  382. {
  383.         char name[10];
  384.         int args;
  385.         void * f;
  386. } func;
  387.  
  388. // the list of functions
  389. const int max_func = 29;
  390. func functions[max_func] =
  391. {
  392.         "", 1, NULL,                                                            // íå ïîìíþ, ñ êàêîé öåëüþ
  393.         "sin", 1, &sin,
  394.         "cos", 1, &cos,
  395.         "exp", 1, &exp,
  396.         "sqrt", 1, &sqrt,
  397.         "log", 1, &log,
  398.         "tg", 1, &tg,
  399.         "ctg", 1, &ctg,
  400.         "arcsin", 1, &asin,
  401.         "arccos", 1, &acos,
  402.         "arctg", 1, &atan,                                                      // íå ðåàëèçîâàíî. âîçâðàùàåò îøèáêó ERR_GENERAL
  403.         "abs", 1, &fabs,
  404.         "pow", 2, &pow,
  405.         "if", INF_ARGS, &func_if,
  406.         "sum",INF_ARGS,&sum,
  407.         "isnull",STR_ARG,&func_isnull,                          // ñëåãêà ÷/æ
  408.         "min",INF_ARGS,&func_min,
  409.         "max",INF_ARGS,&func_max,
  410.         "avg",INF_ARGS,&avg,
  411.         "ceil",1,&func_ceil,
  412.         "round",1,&func_round,
  413.         "floor",1,&func_floor,
  414.         "and",2,&logic_and,
  415.         "or",2,&logic_or,
  416.         "xor",2,&logic_xor,
  417.         "rand",1,&func_rand,
  418.         "case",INF_ARGS,&func_case,
  419.         "pi",0,&func_pi,
  420.         "eps",0,&func_eps
  421. };
  422.  
  423. // all delimiters
  424. #define MAXDELIM 17
  425. const char delim[MAXDELIM]="+-*^/%=;(),><#! ";          // not bad words
  426.  
  427.  
  428. int isdelim(char c)
  429. {
  430.         //return strchr(delim, c) != 0;
  431.         for (int i = 0; i < MAXDELIM; i++)
  432.                 if (c == delim[i])
  433.                         return 1;
  434.         return 0;
  435. }
  436.  
  437. int isdigit(char c)
  438. {
  439.         return (c >= '0' && c <= '9');
  440. }
  441.  
  442. int isalpha2(char c)
  443. {
  444.         return ((c >= 'a' && c <= 'z')
  445.                 || (c >= 'A' && c <= 'Z') || (c=='$'));
  446. }
  447.  
  448. int iswhite(char c)
  449. {
  450.         return (c==' ' || c=='\t');
  451. }
  452.  
  453.  
  454. void serror(int acode)
  455. {
  456.         if (acode != 0)
  457.                 code = acode;
  458. }
  459.  
  460. void set_exp(char *exp)
  461. {
  462.         prog = exp;
  463. }
  464.  
  465. int get_token()
  466. {
  467.         int tok;
  468.         char *temp;
  469.         (token_type) = 0;
  470.         tok = 0;
  471.         temp = (token);
  472.  
  473.         if (*(prog) == '\0')
  474.         {
  475.                 *(token) = 0;
  476.                 tok = FINISHED;
  477.                 return ((token_type) = DELIMITER);
  478.         }
  479.         while (iswhite(*(prog))) ++(prog);
  480.         if (isdelim(*(prog)))
  481.         {
  482.                 char t=*temp = *(prog);
  483.                 (prog)++;
  484.                 temp++;
  485.                 if ((t == '>' || t == '<' || t == '!') && (*prog) && (*prog == '='))
  486.                 {
  487.                         *temp = *(prog);
  488.                         (prog)++;
  489.                         temp++;
  490.                 }
  491.                 *temp = 0;
  492.                 return ((token_type) = DELIMITER);
  493.         }
  494.         if (isdigit(*(prog)))
  495.         {
  496.                 while (!isdelim(*(prog)))
  497.                         *temp++=*(prog)++;
  498.                 *temp = '\0';
  499.                 return ((token_type) = NUMBER);
  500.         }
  501.         if (isalpha2(*(prog)))
  502.         {
  503.                 while (!isdelim(*(prog)))
  504.                         *temp++=*(prog)++;
  505.                 (token_type) = VARIABLE;
  506.         }
  507.         *temp = '\0';
  508.         if ((token_type) == VARIABLE)
  509.         {
  510.                 tok = look_up((token));
  511.                 if (tok)
  512.                         (token_type) = FUNCTION;
  513.         }
  514.         return (token_type);
  515. }
  516.  
  517. double sign(double d)
  518. {
  519.   if (d > 0.0)
  520.     return 1.0;
  521.   if (d < 0.0)
  522.     return -1.0;
  523.   return 0.0;
  524. }
  525.  
  526. void putback()
  527. {
  528.         char *t;
  529.         t = (token);
  530.         for (;*t;t++)
  531.                 (prog)--;
  532. }
  533.  
  534. int get_exp(double *hold)
  535. {
  536.         code = 0;
  537.  
  538.         get_token();
  539.         if (!*(token))
  540.         {
  541.                 return 0;
  542.         }
  543.         level1( hold);
  544.         putback();
  545.   return code==0;
  546. }
  547.  
  548. void level1(double *hold)
  549. {
  550.         char op[2];
  551.         double h;
  552.  
  553.         level1_5( hold);
  554.         while (op[0] = *token, op[1] = (*(token+1)) ? *(token + 1) : 0,
  555.                 *op == '<' || *op == '>' || *op == '=' || *op == '#' || *op == '!')
  556.         {
  557.                 get_token();
  558.                 level1_5( &h);
  559.                 logic(op, hold, &h);
  560.         }
  561. }
  562.  
  563. void level1_5(double *hold)
  564. {
  565.         char op;
  566.  
  567.         op = 0;
  568.         if (((token_type) == DELIMITER) && *(token) == '!')
  569.         {
  570.                 op = *(token);
  571.                 get_token();
  572.         }
  573.         level2( hold);
  574.  
  575.         if (op)
  576.         {
  577.                 if (*hold == 0.0)
  578.                         *hold = 1.0;
  579.                 else
  580.                         *hold = 0.0;
  581.         }
  582. }
  583.  
  584. void level2(double *hold)
  585. {
  586.         char op;
  587.         double h;
  588.  
  589.         level3( hold);
  590.         while ((op=*(token)) == '+' || op == '-')
  591.         {
  592.                 get_token();
  593.                 level3( &h);
  594.                 arith(op, hold, &h);
  595.         }
  596. }
  597.  
  598. void level3(double *hold)
  599. {
  600.         char op;
  601.         double h;
  602.  
  603.         level4( hold);
  604.         while ((op=*(token)) == '*' || op == '/' || op == '%')
  605.         {
  606.                 get_token();
  607.                 level4( &h);
  608.                 arith( op, hold, &h);
  609.         }
  610. }
  611.  
  612. void level4(double *hold)
  613. {
  614.         double h;
  615.         level5( hold);
  616.  
  617.         if (*(token) == '^')
  618.         {
  619.                 get_token();
  620.                 level5( &h);
  621.                 arith( '^', hold, &h);
  622.         }
  623. }
  624.  
  625. void level5(double *hold)
  626. {
  627.         char op;
  628.  
  629.         op = 0;
  630.         if (((token_type) == DELIMITER) && *(token) == '+' || *(token) == '-')
  631.         {
  632.                 op = *(token);
  633.                 get_token();
  634.         }
  635.         level6( hold);
  636.  
  637.         if (op)
  638.                 unary(op, hold);
  639. }
  640.  
  641. void level6(double *hold)
  642. {
  643.         if ((*(token) == '(') && ((token_type) == DELIMITER))
  644.         {
  645.                 get_token();
  646.                 level1( hold);
  647.                 if (*(token) != ')')
  648.                           serror( ERR_NOBRACKET);
  649.                 get_token();
  650.         }
  651.         else
  652.                 primitive( hold);
  653. }
  654.  
  655. void calc_function(double *hold)
  656. {
  657.   double_list *args = NULL, *last = NULL, *t;          
  658.   double d;
  659.         int i,argc=0,save_code;
  660.  
  661.         save_code = code;
  662.         code = 0;
  663.         i = look_up(token);
  664.  
  665.         if (i == 0)
  666.                 serror(ERR_BADFUNCTION);        // error
  667.  
  668.         get_token();
  669.         if (*(token) != '(')
  670.                 serror(ERR_NOBRACKET);  // error
  671.         //get_token();
  672.         if (functions[i].args == STR_ARG)
  673.         {
  674.                 get_token();
  675.                 d = ((matfunc_str)(functions[i].f))(token);
  676.                 *hold = d;
  677.                 get_token();
  678.                 if (save_code)
  679.                         code = save_code;
  680.                 return;
  681.         }
  682.  
  683.         //last = args = (double_list*)malloc(sizeof(double_list));
  684.         //args->next = NULL;
  685.         //level1(&args->val);
  686.         //get_token();
  687.         argc=0;
  688.         do
  689.         {
  690.                 get_token();
  691.                 if (*token == ')')
  692.                         break;
  693.                 t = (double_list*)allocmem(sizeof(double_list));
  694.                 code = 0;
  695.                 level1(&t->val);
  696.                 t->code = code;
  697.                 t->next = NULL;
  698.                 if (last)
  699.                         last->next = t;
  700.                 else
  701.                         args = t;
  702.                 last = t;
  703.                 argc++;
  704.         } while (*token == ',');
  705.  
  706.         code = save_code;
  707.  
  708.         if (argc != functions[i].args && functions[i].args >= 0)
  709.         {
  710.                 serror(ERR_BADPARAM);
  711.         }
  712.         else
  713.         {
  714.                 switch (functions[i].args)
  715.                 {
  716.                         case 0:
  717.                                 d = ((matfunc0)(functions[i].f))();
  718.                                 break;
  719.                         case 1:
  720.                                 d = ((matfunc)(functions[i].f))(args->val);
  721.                                 break;                 
  722.                         case 2:
  723.                                 d = ((matfunc2)(functions[i].f))(args->val,args->next->val);
  724.                                 break;                 
  725.                         case 3:
  726.                                 d = ((matfunc3)(functions[i].f))(args->val,args->next->val,args->next->next->val);
  727.                                 break;         
  728.                         case INF_ARGS:
  729.                                 d = ((matfunc_inf)(functions[i].f))(args);
  730.                                 break;
  731.                 }
  732.         }
  733.  
  734.         t = args;
  735.         while (t)
  736.         {
  737.                 args = t->next;
  738.                 freemem(t);
  739.                 t = args;
  740.         }
  741.        
  742.   *hold = d;
  743. //  else
  744. //    serror( ERR_OVERFLOW);
  745.  
  746. }
  747.  
  748. void primitive(double *hold)
  749. {
  750.         switch (token_type)
  751.         {
  752.         case VARIABLE:
  753.                 *hold = find_var(token);
  754.                 get_token();
  755.                 return;
  756.         case NUMBER:
  757.     //
  758.                 *hold = atof((token));
  759.     //if (sscanf(token, "%lf", hold) != 1)
  760.         *hold = convert(token);
  761.         if (convert_error == ERROR)
  762.       serror( ERR_BADNUMER);
  763.                 get_token();
  764.                 return;
  765.         case FUNCTION:
  766.                 calc_function( hold);
  767.                 if (*token != ')')
  768.                         serror(ERR_NOBRACKET);
  769.                 get_token();
  770.                 return;
  771.         default:        // error
  772.                 return;
  773.         }
  774. }
  775.  
  776. void arith(char op, double *r, double *h)
  777. {
  778.         double t;
  779.         switch(op)
  780.         {
  781.         case '-':
  782.                 *r = *r - *h;
  783.                 break;
  784.         case '+':
  785.                 *r = *r + *h;
  786.                 break;
  787.         case '*':
  788.                 *r = *r * *h;
  789.                 break;
  790.         case '/':
  791.             if (fabs(*h) < epsilon)
  792.                         serror( ERR_OVERFLOW);
  793.                 else
  794.                   *r = (*r) / (*h);
  795.                 break;
  796.         case '%':
  797.             if (fabs(*h) < epsilon)
  798.                         serror( ERR_OVERFLOW);
  799.                 else
  800.                 {
  801.                         t = func_floor ((*r) / (*h));
  802.                         *r = *r - (t * (*h));
  803.                 }
  804.                 break;
  805.         case '^':
  806.                 *r = pow(*r, *h);
  807.                 break;
  808.         }
  809. }
  810.  
  811. void logic(char *op, double *r, double *h)
  812. {
  813.         double t;
  814.         switch (*op)
  815.         {
  816.         case '<':
  817.                 if (*(op+1) && *(op+1) == '=')
  818.                         t = *r <= *h + epsilon ? 1.0 : 0.0;
  819.                 else                           
  820.                         t = *r < *h - epsilon? 1.0 : 0.0;      
  821.                 break;
  822.         case '>':
  823.                 if (*(op+1) && *(op+1) == '=')
  824.                         t = *r >= *h - epsilon ? 1.0 : 0.0;
  825.                 else                           
  826.                         t = *r > *h + epsilon ? 1.0 : 0.0;
  827.                 break;
  828.         case '=':
  829.                 t = fabs(*r - *h) <= epsilon ? 1.0 : 0.0;
  830.                 break;
  831.         case '#':
  832.                 t = fabs(*r - *h) > epsilon ? 1.0 : 0.0;
  833.                 break;
  834.         case '!':
  835.                 if (*(op+1) && *(op+1) == '=')
  836.                         t = fabs(*r - *h) > epsilon ? 1.0 : 0.0;
  837.                 else
  838.                         serror(ERR_GENERAL);
  839.                 break;
  840.         }
  841.         *r = t;
  842. }
  843.  
  844.  
  845. void unary(char op, double *r)
  846. {
  847.         if (op == '-')
  848.                 *r = -(*r);
  849. }
  850.  
  851. bool strcmp(char *s1, char *s2)
  852. {
  853.         int i;
  854.  
  855.         if (s1 == NULL)
  856.                 if (s2 == NULL)
  857.                         return 0;
  858.                 else
  859.                         return 1;
  860.         else
  861.                 if (s2 == NULL)
  862.                         return 1;
  863.  
  864.         for (i = 0;;i++)
  865.         {
  866.                 if (s1[i] == '\0')
  867.                         if (s2[i] == '\0')
  868.                                 return 0;
  869.                         else
  870.                                 return 1;
  871.                 else
  872.                         if (s2[i] == '\0')
  873.                                 return 1;
  874.                         else
  875.                         {
  876.                                 if (s1[i] != s2[i])
  877.                                         return 1;
  878.                         }
  879.         }
  880.         return 0;
  881. }
  882.  
  883.  
  884. bool strncmp(char *s1, char *s2, int n)
  885. {
  886.         int i;
  887.  
  888.         if (s1 == NULL)
  889.                 if (s2 == NULL)
  890.                         return 0;
  891.                 else
  892.                         return 1;
  893.         else
  894.                 if (s2 == NULL)
  895.                         return 1;
  896.  
  897.         for (i = 0;i<n;i++)
  898.         {
  899.                 if (s1[i] == '\0')
  900.                         if (s2[i] == '\0')
  901.                                 return 0;
  902.                         else
  903.                                 return 1;
  904.                 else
  905.                         if (s2[i] == '\0')
  906.                                 return 1;
  907.                         else
  908.                         {
  909.                                 if (s1[i] != s2[i])
  910.                                         return 1;
  911.                         }
  912.         }
  913.         return 0;
  914. }
  915.  
  916. int look_up(char *s)
  917. {
  918.         int i;
  919.  
  920.         for (i = 0; i < max_func; i++)
  921.                 if (strcmp(s, functions[i].name) == 0)
  922.                         return i;
  923.         return 0;       // search command/function name
  924. }
  925.  
  926.