source: git/dyn_modules/modgen/creat_top.cc @ 3c473c

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