source: git/old_modgen/modgen/misc.cc @ c7ae4d

spielwiese
Last change on this file since c7ae4d was c7ae4d, checked in by Hans Schoenemann <hannes@…>, 8 years ago
Singular_4_1: parent(..)
  • Property mode set to 100644
File size: 21.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: lib parsing
6*/
7
8#include <stdlib.h>
9#include <regex.h>
10
11#include "modgen.h"
12#include "typmap.h"
13
14#define NEW_PARAM 1
15
16#if 1
17#  define logx printf
18#else
19#  define logx
20#endif
21
22/* This is the same structure as in Singular's kernel/structs.h.
23 * Duplicating the following code sniplets is save, because it is used
24 * independently here.
25 * Moreover, it is recommended not to include structs.h here, because otherwise
26 * changing the latter could break modgen also.
27 */
28struct _scmdnames
29{
30  char *name;
31  short alias;
32  short tokval;
33  short toktype;
34};
35typedef struct _scmdnames cmdnames;
36
37
38
39
40typedef struct {
41  unsigned  nCmdUsed;          /**< number of commands used */
42  unsigned nCmdAllocated;      /**< number of commands-slots allocated */
43  unsigned nLastIdentifier;
44  cmdnames *sCmds;             /**< array of existing commands */
45
46#ifndef GENTABLE
47  struct sValCmd1 *psValCmd1;
48  struct sValCmd2 *psValCmd2;
49  struct sValCmd3 *psValCmd3;
50  struct sValCmdM *psValCmdM;
51#endif /* GENTABLE */
52} SArithBase;
53
54
55/*---------------------------------------------------------------------*
56 * File scope Variables (Variables share by several functions in
57 *                       the same file )
58 *
59 *---------------------------------------------------------------------*/
60static SArithBase sArithBase;  /**< Base entry for arithmetic */
61
62
63BOOLEAN    expected_parms;
64int        cmdtok;
65BOOLEAN siq=FALSE;
66const char *lastreserved=NULL;
67#define SELF_CMD MAX_TOK+1
68
69/*---------------------------------------------------------------------*
70 * Extern Functions declarations
71 *
72 *---------------------------------------------------------------------*/
73static int _gentable_sort_cmds(const void *a, const void *b);
74extern int iiArithRemoveCmd(char *szName);
75extern int iiArithAddCmd(char *szName, short nAlias, short nTokval,
76                         short nToktype, short nPos=-1);
77extern void enter_id(FILE *fp, idtyp t, char *name, char *value,
78                     int lineno, char *file);
79extern void write_enter_id(FILE *fp);
80extern void write_add_singular_proc(FILE *fp);
81
82static void  mod_write_ctext(FILE *fp, FILE *fp_in);
83char type_conv[MAX_TOK][32];
84
85extern void write_intro(moddefv module);
86
87/*=============== types =====================*/
88struct sValCmdTab
89{
90  short cmd;
91  short start;
92};
93
94/* todo: ensure compiler picks the right header, without using paths here */
95/*#include "iparith.inc"*/
96#include "../../Singular/iparith.inc"
97
98/*=================== general utilities ============================*/
99int IsCmd(char *n, int & tok)
100{
101  int an=1;
102  int i,v;
103  int en=sArithBase.nLastIdentifier;
104
105  if( strcmp(n, "SELF") == 0)
106  {
107    tok = SELF_CMD;
108    logx("IsCmd: [%d] %s\n", tok, n);
109    return tok;
110  }
111
112  if( strcmp(n, "none") == 0)
113  {
114    tok = NONE;
115    logx("IsCmd: [%d] %s\n", tok, n);
116    return tok;
117  }
118
119  for(an=0; an<sArithBase.nCmdUsed; )
120  {
121    if(an>=en-1)
122    {
123      if (strcmp(n, sArithBase.sCmds[an].name) == 0)
124      {
125        i=an;
126        break;
127      }
128      else if ((an!=en) && (strcmp(n, sArithBase.sCmds[en].name) == 0))
129      {
130        i=en;
131        break;
132      }
133      else
134      {
135        return 0;
136      }
137    }
138    i=(an+en)/2;
139    if (*n < *(sArithBase.sCmds[i].name))
140    {
141      en=i-1;
142    }
143    else if (*n > *(sArithBase.sCmds[i].name))
144    {
145      an=i+1;
146    }
147    else
148    {
149      v=strcmp(n,sArithBase.sCmds[i].name);
150    if(v<0)
151    {
152        en=i-1;
153    }
154    else if(v>0)
155    {
156        an=i+1;
157    }
158    else /*v==0*/
159    {
160      break;
161    }
162  }
163  }
164  lastreserved=sArithBase.sCmds[i].name;
165  tok=sArithBase.sCmds[i].tokval;
166  if(sArithBase.sCmds[i].alias==2)
167    {
168    if(trace)printf("outdated identifier `%s` used - please change your code",
169    sArithBase.sCmds[i].name);
170    sArithBase.sCmds[i].alias=1;
171  }
172
173  logx("IsCmd: [%d] %s\n", tok, n);
174
175  if( (sArithBase.sCmds[i].toktype==ROOT_DECL) ||
176      (sArithBase.sCmds[i].toktype==ROOT_DECL_LIST) ||
177      (sArithBase.sCmds[i].toktype==RING_DECL) ||
178      (sArithBase.sCmds[i].toktype==IDEAL_CMD) ||
179      (sArithBase.sCmds[i].toktype==INTMAT_CMD) ||
180      (sArithBase.sCmds[i].toktype==MODUL_CMD) ||
181      (sArithBase.sCmds[i].toktype==MATRIX_CMD))// ||
182//      ((csArithBase.sCds[i].toktype>=DRING_CMD) && (cmds[i].toktype<=VECTOR_CMD)))
183    return sArithBase.sCmds[i].toktype;
184  return 0;
185}
186
187
188char * decl2str(int n, char *name)
189{
190  switch(n)
191  {
192#   include "decl.inc"
193
194    /* first and last entry of tok.h cannot be grepped */
195    case MAX_TOK: strcpy(name,"MAX_TOK");       break;
196    case ALIAS_CMD: strcpy(name,"ALIAS_CMD");       break;
197    default: strcpy(name,"(null)");
198  }
199  return(name);
200}
201
202/*========================================================================*/
203struct valid_cmds_def
204{
205  char *name;
206  void (*write_cmd)(moddefv module, procdefv pi, void *arg);
207  cmd_token id;
208  cmd_type  type;
209  int args;
210} valid_cmds[] =
211{
212  { "declaration",  write_function_declaration, CMD_DECL,   CMDT_SINGLE, 0 },
213  { "error",        write_function_error,       CMD_ERROR,  CMDT_ANY,    0 },
214  { "nodeclaration",write_function_nodecl,      CMD_NODECL, CMDT_SINGLE, 0 },
215  { "typecheck",    write_function_typecheck,   CMD_CHECK,  CMDT_SINGLE, 0 },
216  { "return",       write_function_result,      CMD_RETURN, CMDT_EQ,     0 },
217//{ "return",       write_function_return,      CMD_RETURN, CMDT_SINGLE, 1 },
218  { "return",       write_function_return,      CMD_RETURN, CMDT_0,     1 },
219  { "return",       write_function_return,      CMD_RETURN, CMDT_ANY,   1 },
220  { "singularcmd",  write_function_singularcmd, CMD_SINGULAR, CMDT_ANY, 1 },
221  { NULL,           0, CMD_NONE, CMDT_ANY, 0 }
222};
223
224cmd_token checkcmd(
225  char *cmdname,
226  void (**write_cmd)(moddefv module, procdefv pi, void *arg),
227  cmd_type  type,
228  int args
229  )
230{
231  int i;
232  cmd_token rc = CMD_NONE;
233
234  for(i=0; valid_cmds[i].name!=NULL; i++)
235  {
236    if(strcmp(valid_cmds[i].name, cmdname)==0)
237    {
238      rc = CMD_BADSYNTAX;
239      if(valid_cmds[i].type == type)
240      {
241        *write_cmd = valid_cmds[i].write_cmd;
242        return valid_cmds[i].id;
243      }
244    }
245  }
246  return rc;
247}
248
249/*========================================================================*/
250struct valid_vars_def
251{
252  char *name;
253  var_type type;
254  var_token id;
255  void (*write_cmd)(moddefv module, var_token type,
256                    idtyp t, void *arg1, void *arg2);
257} valid_vars[] =
258{
259  { "help",         VAR_STRING,  VAR_HELP,     write_main_variable },
260  { "info",         VAR_STRING,  VAR_INFO,     write_main_variable },
261  { "package",      VAR_STRING,  VAR_MODULE,   0 },
262  { "version",      VAR_STRING,  VAR_VERSION,  write_main_variable },
263  { "category",     VAR_STRING,  VAR_CATEGORY, write_main_variable },
264  { NULL,           VAR_UNKNOWN, VAR_NONE, 0 }
265};
266
267var_token checkvar(
268  char *varname,
269  var_type type,
270  void (**write_cmd)(moddefv module, var_token type,
271                    idtyp t, void *arg1, void *arg2)
272  )
273{
274  int i;
275  for(i=0; valid_vars[i].name!=NULL; i++)
276    if((strcmp(valid_vars[i].name, varname)==0) &&
277       (valid_vars[i].type == type) ) {
278      *write_cmd = valid_vars[i].write_cmd;
279      return valid_vars[i].id;
280    }
281  return VAR_NONE;
282}
283
284/*========================================================================*/
285void write_main_variable(
286  moddefv module,
287  var_token type,
288  idtyp t,
289  void *arg1,
290  void *arg2
291  )
292{
293  enter_id(module->fmtfp, t, (char *)arg1, (char *)arg2, yylineno,
294           module->filename);
295  switch(type)
296  {
297    case VAR_INFO:
298          module->info = (char *)malloc(strlen((char *)arg2)+1);
299          memset(module->info, '\0', strlen((char *)arg2)+1);
300          memcpy(module->info,(char *)arg2,strlen((char *)arg2));
301          break;
302    case VAR_VERSION:
303          module->version = (char *)malloc(strlen((char *)arg2)+1);
304          memset(module->version, '\0', strlen((char *)arg2)+1);
305          memcpy(module->version,(char *)arg2,strlen((char *)arg2));
306          break;
307    case VAR_CATEGORY:
308          module->category = (char *)malloc(strlen((char *)arg2)+1);
309          memset(module->category, '\0', strlen((char *)arg2)+1);
310          memcpy(module->category,(char *)arg2,strlen((char *)arg2));
311          break;
312    default: break;
313  }
314}
315
316/*========================================================================*/
317void PrintProc(
318  procdefv pi
319  )
320{
321  int i;
322
323  printf("%4d proc: %s(", pi->lineno, pi->procname);
324
325  for(i=0; i<pi->paramcnt; i++)
326  {
327    printf("%s (%s)", (pi->param[i]).name, (pi->param[i]).typname);
328    if(i < (pi->paramcnt-1)) printf(",");
329  }
330  printf(")\n");
331  if(pi->return_val.typ!=0)
332  {
333    printf("\treturn = %s (%s)\n", pi->return_val.name,
334           pi->return_val.typname);
335  }
336  printf("{%s}\n", pi->c_code);
337}
338
339/*========================================================================*/
340void make_version(char *p, moddefv module)
341{
342  char ver[10];
343  char date[16];
344  char libnamebuf[128];
345
346  module->major = 0;
347  module->minor = 0;
348  module->level = 0;
349
350  ver[0]='0'; ver[1]='.'; ver[2]='0'; ver[3]='.'; ver[4]='0'; ver[5]='\0';
351  date[0]='?'; date[1]='\0';
352  sscanf(p,"%*s %*s %10s %16s",ver,date);
353  sscanf(ver, "%d.%d.%d", &module->major, &module->minor, &module->level);
354
355  sprintf(libnamebuf,"(%s,%s)", ver, date);
356  if(strcmp(libnamebuf, "(0.0.0,?)")==0)
357  {
358    sscanf(p,"%*[^\"]\"%[^\"]\"",libnamebuf);
359  }
360  module->revision = (char *)malloc(strlen(libnamebuf)+1);
361  memset(module->revision, '\0', strlen(libnamebuf)+1);
362  memcpy(module->revision, libnamebuf, strlen(libnamebuf));
363}
364
365/*========================================================================*/
366void make_module_name(char *p, moddefv module)
367{
368  if(strlen(p)>=1)
369  {
370    module->targetname = (char *)malloc(strlen(p)+1);
371    memset(module->targetname, '\0', strlen(p)+1);
372    memcpy(module->targetname,p,strlen(p));
373  }
374  else
375  {
376    module->targetname = (char *)malloc(strlen(module->name)+1);
377    memset(module->targetname, '\0', strlen(module->name)+1);
378    memcpy(module->targetname,module->name,strlen(module->name));
379  }
380
381}
382
383/*========================================================================*/
384void Add2files(
385  moddefv module,
386  char *name
387  )
388{
389  cfiles cfnew;
390  memset((void *)&cfnew, '\0', sizeof(cfiles));
391
392  cfnew.filename = (char *)malloc(strlen(name)+1);
393  memset(cfnew.filename, '\0', strlen(name)+1);
394  memcpy(cfnew.filename, name, strlen(name));
395
396  if(module->filecnt==0)
397  {
398    module->files = (cfilesv)malloc(sizeof(cfiles)+1);
399  }
400  else
401  {
402    module->files = (cfilesv)realloc(module->files,
403                                   (module->filecnt+1)*sizeof(cfiles));
404  }
405  if(module->files == NULL) { printf("ERROR\n"); return; }
406
407  memset((void *) &module->files[module->filecnt], '\0', sizeof(cfiles));
408  memcpy((void *)(&(module->files[module->filecnt])),
409         (void *)&cfnew, sizeof(cfiles));
410  (module->filecnt)++;
411}
412
413/*========================================================================*/
414void PrintProclist(
415  moddefv module
416  )
417{
418  logx("PrintProclist()\n");
419  int j;
420  for(j=0; j<module->proccnt; j++)
421  {
422    PrintProc(&(module->procs[j]));
423  }
424}
425
426/*========================================================================*/
427void generate_mod(
428  moddefv module,
429  int section
430  )
431{
432  procdefv v = NULL;
433  cfilesv c_filelist = NULL;
434  int proccnt;
435  FILE *fp_h;
436  char *filename;
437
438  switch(section)
439  {
440      case 1:
441        write_intro(module);
442        return;
443
444      case 2:
445        printf("Writing %s, section %d", filename, section);fflush(stdout);
446        break;
447  }
448
449  sprintf(filename, "%s.h", module->name);
450  fp_h = fopen(filename, "w");
451  write_header(fp_h, module->name);
452  printf("%s  ...", filename);fflush(stdout);
453
454  /* building mod_init() */
455
456  for(proccnt=0; proccnt<module->proccnt; proccnt++)
457  {
458    printf("->%s, %s\n", module->procs[proccnt].procname,
459           module->procs[proccnt].funcname);
460    fprintf(module->modfp, "  psModulFunctions->iiAddCproc(\"%s\",\"%s\",%s, mod_%s);\n",
461            module->name, module->procs[proccnt].procname,
462            module->procs[proccnt].is_static ? "TRUE" : "FALSE",
463            module->procs[proccnt].funcname);
464  }
465  fprintf(module->modfp, "  return 0;\n}\n\n");
466
467  /* building entry-functions */
468  for(proccnt=0; proccnt<module->proccnt; proccnt++)
469  {
470    //generate_function(&module->procs[proccnt], module->modfp);
471    //generate_header(&module->procs[proccnt], fp_h);
472  }
473  printf("  done.\n");fflush(stdout);
474  fclose(module->modfp);
475  fclose(fp_h);
476}
477
478
479
480
481/*========================================================================*/
482void  write_procedure_text(
483  moddefv module,
484  int lineno
485)
486{
487  int i;
488  procdefv pi = &module->procs[module->proccnt-1];
489
490  fprintf(module->fmtfp, "#line %d \"%s\"\n", lineno, module->filename);
491  fprintf(module->fmtfp, "%s\n", module->procs[module->proccnt-1].c_code);
492  free(module->procs[module->proccnt-1].c_code);
493  module->procs[module->proccnt-1].c_code = NULL;
494
495#if 0
496  if(pi->paramcnt>0)
497  {
498  }
499  else
500  {
501    switch( pi->return_val.typ)
502    {
503        case SELF_CMD:
504          fprintf(module->fmtfp, "  return(%s(res));", pi->funcname);
505          break;
506
507        case NONE:
508          fprintf(module->fmtfp, "  return FALSE;");
509          break;
510
511        default:
512          fprintf(module->fmtfp, "  res->rtyp = %s;\n",
513                  pi->return_val.typname);
514          fprintf(module->fmtfp, "  res->data = (void *)%s();\n",
515                  pi->funcname);
516          fprintf(module->fmtfp, "  return FALSE;");
517    }
518  }
519#endif
520  fprintf(module->fmtfp, "/*\n}\n\n*/");
521
522}
523
524
525/*========================================================================*/
526void  mod_write_header(FILE *fp, char *module, char what)
527{
528
529  write_header(fp, module);
530  fprintf(fp, "#line %d \"%s.cc\"\n", modlineno++, module);
531  if(what != 'h')
532  {
533    fprintf(fp, "#include <stdlib.h>\n");
534    fprintf(fp, "#include <stdio.h>\n");
535    fprintf(fp, "#include <string.h>\n");
536    fprintf(fp, "#include <ctype.h>\n");
537
538    fprintf(fp, "#if defined(HPUX_9) || defined(HPUX_10)\n");
539    fprintf(fp, "#  include <dl.h>\n");
540    fprintf(fp, "#else\n");
541    fprintf(fp, "#  include <dlfcn.h>\n");
542    fprintf(fp, "#endif\n");
543
544    fprintf(fp, "#include <unistd.h>\n");
545    fprintf(fp, "#include <sys/stat.h>");
546    fprintf(fp, "\n");
547    fprintf(fp, "#include <kernel/mod2.h>\n");
548    fprintf(fp, "#include <Singular/tok.h>\n");
549    fprintf(fp, "#include <kernel/structs.h>\n");
550    fprintf(fp, "#include <Singular/ipid.h>\n\n");
551    fprintf(fp, "#include <Singular/locals.h>\n");
552    fprintf(fp, "#include <omalloc.h>\n");
553    fprintf(fp, "#include \"%s.h\"\n", module);
554    modlineno+=8;
555
556    fprintf(fp, "#line %d \"%s.cc\"\n", modlineno++, module);
557    write_enter_id(fp);
558    fprintf(fp, "\n");    modlineno+=1;
559    fprintf(fp, "#line %d \"%s.cc\"\n", modlineno++, module);
560    write_add_singular_proc(fp);
561    write_crccheck(fp);
562    fprintf(fp, "\n");
563
564    fprintf(fp, "void fill_help_package();\n");
565    fprintf(fp, "void fill_example_package();\n");
566    modlineno+=3;
567  }
568  fprintf(fp, "\n");
569
570}
571
572/*========================================================================*/
573void write_header(FILE *fp, char *module, char *comment)
574{
575  fprintf(fp, "%s/*\n%s * This was automatically generated by modgen\n",
576          comment, comment);
577  fprintf(fp, "%s * version %s\n", comment, MOD_GEN_VERSION);
578  fprintf(fp, "%s * module %s\n", comment, module);
579  fprintf(fp, "%s * Don't edit this file\n%s */\n", comment, comment);
580  fprintf(fp, "%s\n", comment);
581  fprintf(fp, "%s\n", comment);
582  if(strlen(comment)==1)modlineno+=10;
583}
584
585/*========================================================================*/
586void enter_id(
587  FILE *fp,
588  idtyp t,
589  char *name,
590  char *value,
591  int lineno,
592  char *file
593  )
594{
595  unsigned int i;
596  char tname[32];
597
598  if(lineno)
599    fprintf(fp, "#line %d \"%s\"\n", lineno, file);
600  if(strcmp(decl2str(t, tname),"STRING_CMD")==0)
601  {
602    fprintf(fp, "  enter_id(\"%s\",\"",name);
603    for(i=0;i<strlen(value);i++)
604    {
605       if(value[i]=='\n') fprintf(fp,"\\n");
606       else fprintf(fp,"%c",value[i]);
607    }
608    fprintf(fp,"\", %s);\n",decl2str(t,tname));
609  }
610  else
611  {
612    fprintf(fp, "  enter_id(\"%s\",\"%s\", %s);\n",name, value,
613          decl2str(t, tname));
614  }
615}
616
617/*========================================================================*/
618void init_type_conv()
619{
620  strcpy(type_conv[NONE], "none");
621//  strcpy(type_conv[NONE], "void");
622  strcpy(type_conv[INT_CMD], "int");
623  strcpy(type_conv[RING_CMD], "ring");
624  strcpy(type_conv[QRING_CMD], "ring");
625  strcpy(type_conv[POLY_CMD], "poly");
626  strcpy(type_conv[NUMBER_CMD], "number");
627  strcpy(type_conv[MODUL_CMD], "ideal");
628  strcpy(type_conv[VECTOR_CMD], "poly");
629  strcpy(type_conv[IDEAL_CMD], "ideal");
630  strcpy(type_conv[MAP_CMD], "map");
631  strcpy(type_conv[MATRIX_CMD], "matrix");
632  strcpy(type_conv[STRING_CMD], "char *");
633  strcpy(type_conv[INTMAT_CMD], "intvec *");
634  strcpy(type_conv[INTVEC_CMD], "intvec *");
635  strcpy(type_conv[LIST_CMD], "lists");
636  strcpy(type_conv[LINK_CMD], "si_link");
637  strcpy(type_conv[PACKAGE_CMD], "package");
638  strcpy(type_conv[PROC_CMD], "procinfo *");
639  strcpy(type_conv[RESOLUTION_CMD], "syStrategy");
640/*
641  strcpy(type_conv[], "");
642  strcpy(type_conv[], "");
643  strcpy(type_conv[], "");
644  strcpy(type_conv[], "");
645  strcpy(type_conv[], "");
646  strcpy(type_conv[], "");
647  strcpy(type_conv[], "");
648  printf("[%d] %s\n", INT_CMD, type_conv[INT_CMD]);
649  printf("[%d] %s\n", MODUL_CMD, type_conv[MODUL_CMD]);
650  printf("[%d] %s\n", STRING_CMD, type_conv[STRING_CMD]);
651  printf("[%d] %s\n", LINK_CMD, type_conv[LINK_CMD]);
652  printf("[%d] %s\n", PACKAGE_CMD, type_conv[PACKAGE_CMD]);
653  / **/
654}
655
656/*========================================================================*/
657/*---------------------------------------------------------------------*/
658/**
659 * @brief compares to entry of cmdsname-list
660
661 @param[in] a
662 @param[in] b
663
664 @return <ReturnValue>
665**/
666/*---------------------------------------------------------------------*/
667static int _gentable_sort_cmds(
668  const void *a,
669  const void *b
670  )
671{
672  cmdnames *pCmdL = (cmdnames*)a;
673  cmdnames *pCmdR = (cmdnames*)b;
674
675  if(a==NULL || b==NULL)             return 0;
676
677  /* empty entries goes to the end of the list for later reuse */
678  if(pCmdL->name==NULL) return 1;
679  if(pCmdR->name==NULL) return -1;
680
681  /* $INVALID$ must come first */
682  if(strcmp(pCmdL->name, "$INVALID$")==0) return -1;
683  if(strcmp(pCmdR->name, "$INVALID$")==0) return  1;
684
685  /* tokval=-1 are reserved names at the end */
686  if( (pCmdL->tokval==-1) && pCmdR->tokval==-1)
687    return strcmp(pCmdL->name, pCmdR->name);
688
689  /* pCmdL->tokval==-1, pCmdL goes at the end */
690  if(pCmdL->tokval==-1) return 1;
691  /* pCmdR->tokval==-1, pCmdR goes at the end */
692  if(pCmdR->tokval==-1) return -1;
693
694  return strcmp(pCmdL->name, pCmdR->name);
695}
696
697/*---------------------------------------------------------------------*/
698/**
699 * @brief initialisation of arithmetic structured data
700
701 @retval 0 on success
702
703**/
704/*---------------------------------------------------------------------*/
705int iiInitArithmetic()
706{
707  int i;
708  //printf("iiInitArithmetic()\n");
709#ifndef GENTABLE
710  memset(&sArithBase, 0, sizeof(sArithBase));
711  iiInitCmdName();
712  /* fix last-identifier */
713#endif   /* !GENTABLE */
714}
715
716int iiArithFindCmd(const char *szName)
717{
718  int an=0;
719  int i = 0,v = 0;
720#ifndef GENTABLE
721  int en=sArithBase.nLastIdentifier;
722
723  for(an=0; an<sArithBase.nCmdUsed; )
724  {
725    if(an>=en-1)
726    {
727      if (strcmp(szName, sArithBase.sCmds[an].name) == 0)
728      {
729        //Print("RET-an=%d %s\n", an, sArithBase.sCmds[an].name);
730        return an;
731      }
732      else if (strcmp(szName, sArithBase.sCmds[en].name) == 0)
733      {
734        //Print("RET-en=%d %s\n", en, sArithBase.sCmds[en].name);
735        return en;
736      }
737      else
738      {
739        //Print("RET- 1\n");
740        return -1;
741      }
742    }
743    i=(an+en)/2;
744    if (*szName < *(sArithBase.sCmds[i].name))
745    {
746      en=i-1;
747    }
748    else if (*szName > *(sArithBase.sCmds[i].name))
749    {
750      an=i+1;
751    }
752    else
753    {
754      v=strcmp(szName,sArithBase.sCmds[i].name);
755      if(v<0)
756      {
757        en=i-1;
758      }
759      else if(v>0)
760      {
761        an=i+1;
762      }
763      else /*v==0*/
764      {
765        //Print("RET-i=%d %s\n", i, sArithBase.sCmds[i].name);
766        return i;
767      }
768    }
769  }
770  //if(i>=0 && i<sArithBase.nCmdUsed)
771  //  return i;
772  //Print("RET-2\n");
773  return -2;
774#else
775  return 0;
776#endif
777}
778
779int iiArithAddCmd(
780  char *szName,
781  short nAlias,
782  short nTokval,
783  short nToktype,
784  short nPos
785  )
786{
787  //printf("AddCmd(%s, %d, %d, %d, %d)\n", szName, nAlias,
788  //       nTokval, nToktype, nPos);
789  if(nPos>=0)
790  {
791    if(nPos>=sArithBase.nCmdAllocated) return -1;
792    if(szName!=NULL) sArithBase.sCmds[nPos].name    = strdup(szName);
793    else sArithBase.sCmds[nPos].name = NULL;
794    sArithBase.sCmds[nPos].alias   = nAlias;
795    sArithBase.sCmds[nPos].tokval  = nTokval;
796    sArithBase.sCmds[nPos].toktype = nToktype;
797    sArithBase.nCmdUsed++;
798    //if(nTokval>0) sArithBase.nLastIdentifier++;
799  }
800  else
801  {
802    if(szName==NULL) return -1;
803    int nIndex = iiArithFindCmd(szName);
804    if(nIndex>=0)
805    {
806      printf("'%s' already exists at %d\n", szName, nIndex);
807      return -1;
808    }
809
810    if(sArithBase.nCmdUsed>=sArithBase.nCmdAllocated)
811    {
812      /* needs to create new slots */
813      unsigned long nSize = (sArithBase.nCmdAllocated+1)*sizeof(cmdnames);
814      sArithBase.sCmds = (cmdnames *)realloc(sArithBase.sCmds, nSize);
815      if(sArithBase.sCmds==NULL) return -1;
816      sArithBase.nCmdAllocated++;
817    }
818    /* still free slots available */
819    sArithBase.sCmds[sArithBase.nCmdUsed].name    = strdup(szName);
820    sArithBase.sCmds[sArithBase.nCmdUsed].alias   = nAlias;
821    sArithBase.sCmds[sArithBase.nCmdUsed].tokval  = nTokval;
822    sArithBase.sCmds[sArithBase.nCmdUsed].toktype = nToktype;
823    sArithBase.nCmdUsed++;
824
825    qsort(sArithBase.sCmds, sArithBase.nCmdUsed, sizeof(cmdnames),
826          (&_gentable_sort_cmds));
827    for(sArithBase.nLastIdentifier=sArithBase.nCmdUsed-1;
828        sArithBase.nLastIdentifier>0; sArithBase.nLastIdentifier--)
829    {
830      if(sArithBase.sCmds[sArithBase.nLastIdentifier].tokval>=0) break;
831    }
832    //Print("L=%d\n", sArithBase.nLastIdentifier);
833  }
834  return 0;
835}
836
Note: See TracBrowser for help on using the repository browser.