source: git/old_modgen/modgen/creat_top.cc @ a3f0fea

spielwiese
Last change on this file since a3f0fea was a3f0fea, checked in by Reimer Behrends <behrends@…>, 5 years ago
Modify variable declarions for pSingular.
  • Property mode set to 100644
File size: 6.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: lib parsing
6*/
7
8#include "modgen.h"
9#include <sys/stat.h>
10#include <sys/types.h>
11#include <fcntl.h>
12#include <unistd.h>
13
14
15EXTERN_VAR int yylineno;
16EXTERN_VAR int do_create_srcdir;
17
18void enter_id(FILE *fp, char *name, char *value);
19/*========================================================================*/
20void  mod_copy_tmp(
21  FILE *fp_out,
22  FILE *fp_in
23  )
24{
25  int nread;
26  char buf[1024];
27
28  fseek(fp_in, 0L, 0);
29  do
30  {
31    memset(buf, 0, sizeof(buf));
32    fgets(buf, sizeof(buf), fp_in);
33    modlineno++;
34    //nread = fread (buf, sizeof (char), sizeof(buf)-1, fp_in);
35    if(strncmp(buf, "#line @d ", 8)==0)
36    {
37      buf[6]='%';
38      fprintf(fp_out, buf, modlineno);
39    }
40    else
41      fwrite(buf, sizeof(char), strlen(buf)/*nread*/, fp_out);
42  } while(!feof(fp_in));
43  fputs("\n", fp_out);
44}
45
46/*========================================================================*/
47void write_enter_id(FILE *fp)
48{
49  fprintf(fp, "\ntypedef int idtyp;\n");
50  fprintf(fp, "\nidhdl enter_id(char *name, char *value, idtyp t)\n");
51  fprintf(fp, "{\n");
52  fprintf(fp, "  idhdl h;\n");
53  fprintf(fp, "\n");
54  fprintf(fp, "  h=enterid(name,0, t, &(currPack->idroot), TRUE/*FALSE*/);\n");
55  fprintf(fp, "  if(h!=NULL)\n");
56  fprintf(fp, "  {\n");
57  fprintf(fp, "    switch(t)\n");
58  fprintf(fp, "    {\n");
59  fprintf(fp, "      case STRING_CMD: \n");
60  fprintf(fp, "        omFree(IDSTRING(h));\n");
61  fprintf(fp, "        IDSTRING(h) = omStrDup(value);\n");
62  fprintf(fp, "        break;\n");
63  fprintf(fp, "      case PACKAGE_CMD: break;\n");
64  fprintf(fp, "      case PROC_CMD: break;\n");
65  fprintf(fp, "    }\n");
66  fprintf(fp, "  }\n");
67  fprintf(fp, "  else\n");
68  fprintf(fp, "    Warn(\"Cannot create '%%s'\\n\", name);\n");
69  fprintf(fp, "  return(h);\n");
70  fprintf(fp, "}\n");
71  modlineno+=19;
72}
73
74/*========================================================================*/
75 void write_add_singular_proc(FILE *fp)
76 {
77   fprintf(fp, "\nidhdl add_singular_proc(FILE* binfp, char *procname,int line,\n");
78   fprintf(fp, "                       long pos, long end, BOOLEAN pstatic)\n");
79   fprintf(fp, "{\n");
80   fprintf(fp, "  idhdl h;\n");
81   fprintf(fp, "  procinfov pi;\n");
82   fprintf(fp, "  char *tempstr;\n");
83   fprintf(fp, "  h = enter_id(procname, NULL, PROC_CMD);\n");
84   fprintf(fp, "  if(h == NULL) return NULL;\n");
85   fprintf(fp, "\n");
86   fprintf(fp, "  pi = IDPROC(h);\n");
87   fprintf(fp, "\n");
88   fprintf(fp, "  pi->libname = omStrDup(currPack->libname);\n");
89   fprintf(fp, "  pi->procname = omStrDup(procname);\n");
90   fprintf(fp, "  pi->language = LANG_SINGULAR;\n");
91   fprintf(fp, "  pi->ref = 1;\n");
92   fprintf(fp, "  pi->is_static = pstatic;\n");
93   fprintf(fp, "  pi->data.s.proc_start = pos;\n");
94   fprintf(fp, "  pi->data.s.def_end    = pos;\n");
95   fprintf(fp, "  pi->data.s.help_start = 0L;\n");
96   fprintf(fp, "  pi->data.s.help_end   = 0L;\n");
97   fprintf(fp, "  pi->data.s.body_start = pos;\n");
98   fprintf(fp, "  pi->data.s.body_end   = end;\n");
99   fprintf(fp, "  pi->data.s.proc_end   = end;\n");
100   fprintf(fp, "  pi->data.s.example_start = 0L;\n");
101   fprintf(fp, "  pi->data.s.proc_lineno = line;\n");
102   fprintf(fp, "  pi->data.s.body_lineno = line;\n");
103   fprintf(fp, "  pi->data.s.example_lineno = 0;\n");
104   fprintf(fp, "  pi->data.s.help_chksum = 0;\n");
105   fprintf(fp, \n");
106   fprintf(fp, "  rewind(binfp);\n");
107   fprintf(fp, "  fseek(binfp,pos,SEEK_CUR);\n");
108   fprintf(fp, "  tempstr=(char *)omAlloc(end-pos+2);\n");
109   fprintf(fp, "  memset(tempstr,0,end-pos+2);\n");
110   fprintf(fp, "  fread(tempstr,sizeof(char),end-pos+1,binfp);\n");
111   fprintf(fp, "  pi->data.s.body = omStrDup(tempstr);\n");
112   fprintf(fp, "  omFree(tempstr);\n\n");
113   fprintf(fp, "  return(h);\n");
114   fprintf(fp, "}\n");
115   modlineno+=40;
116 }
117
118/*========================================================================*/
119void write_mod_init( moddefv module, FILE *fp)
120{
121  fprintf(fp, "\n\n");
122  fprintf(fp, "#line @d \"%s.cc\"\n", module->name);
123  fprintf(fp, "extern \"C\" {\n");
124#if 0
125  fprintf(fp, "int mod_init(\n  int (*iiAddCproc)(");
126#if 1
127  fprintf(fp, "char *libname, char *procname, BOOLEAN pstatic,\n");
128  fprintf(fp, "              BOOLEAN(*func)(leftv res, leftv v)");
129#endif
130  fprintf(fp, ")\n  )\n{\n");
131#else
132  fprintf(fp, "int mod_init(\n  SModulFunctions* psModulFunctions");
133  fprintf(fp, "\n  )\n{\n");
134#endif
135  fprintf(fp, "  idhdl h;\n");
136  fprintf(fp, "  char * tempstr;\n");
137  fprintf(fp, "  char * tailstr;\n");
138  fprintf(fp, "  FILE * binfp; \n");
139  fprintf(fp, "  int ret;\n");
140  fprintf(fp, "  struct stat sb; \n\n");
141  fprintf(fp, "  tempstr = (char *)omAlloc(strlen(currPack->libname)+5);\n");
142  fprintf(fp, "  memset(tempstr,0,strlen(currPack->libname)+5);\n");
143  fprintf(fp, "  tailstr = strrchr(currPack->libname,'.');\n");
144  fprintf(fp, "  memcpy(tempstr,currPack->libname,strlen(currPack->libname)-strlen(tailstr));\n");
145  fprintf(fp, "  memcpy(tempstr+strlen(tempstr),\".bin\",4);\n");
146  fprintf(fp, "  ret=stat(tempstr,&sb);\n");
147  fprintf(fp, "  if(ret==0)\n");
148  fprintf(fp, "  {\n");
149  fprintf(fp, "    if ((sb.st_mode & S_IFMT) == S_IFREG)\n");
150  fprintf(fp, "    {\n");
151  fprintf(fp, "      if (crccheck(tempstr)!=crcsum)\n");
152  fprintf(fp, "      {   Warn(\"file %%s does not agree with module version - ignoring file\",tempstr);\n");
153  fprintf(fp, "          ret=-1;\n      }\n");
154  fprintf(fp, "      if ((binfp = fopen(tempstr,\"r\")) == NULL) return -1;\n");
155  fprintf(fp, "    } \n    else \n      ret=-1; \n  }\n\n");
156  fprintf(fp, "  fill_help_package();\n");
157  fprintf(fp, "  fill_example_package();\n\n");
158}
159
160/*========================================================================*/
161int write_intro(
162    moddefv module
163    )
164{
165  char filename[512];
166
167  if(do_create_srcdir) mkdir(module->name, 0755);
168  strcpy(filename, build_filename(module, module->name, 1));
169
170  fflush(module->fmtfp);
171
172  if( (module->modfp = fopen(filename, "w")) == NULL) {
173    //free(filename);
174    return -1;
175  }
176  if(trace)printf("Creating %s, ", filename);fflush(stdout);
177  mod_write_header(module->modfp, module->name, 'c');
178  mod_copy_tmp(module->modfp, module->fmtfp);
179  if(trace)printf("\n");fflush(stdout);
180  fclose(module->fmtfp); module->fmtfp = NULL;
181  if(create_tmpfile(module)) return -1;
182  if(create_tmpfile(module, 1)) return -1;
183  if(create_tmpfile(module, 2)) return -1;
184  if(module->fmtfp2 == NULL) { printf("Cannot write HELP\n"); return -1; }
185  if(module->fmtfp3 == NULL) { printf("Cannot write EXAMPLE\n"); return -1; }
186
187  strcpy(filename, build_filename(module, module->name, 2));
188  //sprintf(filename, "%s/%s.h", module->name, module->name);
189  if( (module->modfp_h = fopen(filename, "w")) == NULL) {
190    //free(filename);
191    return -1;
192  }
193  if(trace)printf("Creating %s, ", filename);fflush(stdout);
194  mod_write_header(module->modfp_h, module->name, 'h');
195  if(trace)printf("\n");fflush(stdout);
196
197  //free(filename);
198//  write_enter_id(module->modfp);
199  return 0;
200}
Note: See TracBrowser for help on using the repository browser.