source: git/old_modgen/modgen/utils.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: 10.7 KB
Line 
1/*
2 */
3
4#include <stdio.h>
5#include <string.h>
6#include <stdlib.h>
7#include <stdarg.h>
8#include <ctype.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <fcntl.h>
12#include <unistd.h>
13
14#include "config.h"
15
16#include "crctab.h"
17
18VAR int modlineno;    /* lineno within module */
19
20#include "modgen.h"
21
22EXTERN_VAR int do_create_srcdir;
23
24/*========================================================================*/
25int init_modgen(
26  moddefv module_def,
27  char *filename
28)
29{
30  char tmpfile[64];
31  char *p, *q;
32
33  modlineno = 0;
34
35  if(module_def == NULL) return -1;
36  memset(module_def, '\0', sizeof(moddef));
37
38  if ( (q=strrchr(filename, '/')) == NULL) q = filename;
39  else q++;
40
41  module_def->filename = (char *)malloc(strlen(q)+1);
42  if(module_def->filename != NULL ) {
43    memset(module_def->filename, '\0', strlen(q)+1);
44    memcpy(module_def->filename, q, strlen(q));
45  }
46  strncpy(tmpfile, q, sizeof(tmpfile));
47  p=strrchr(tmpfile, '.');
48  if(p!=NULL) *p='\0';
49  module_def->name = (char *)malloc(strlen(tmpfile)+1);
50  memset(module_def->name, '\0', strlen(tmpfile)+1);
51  memcpy(module_def->name, tmpfile, strlen(tmpfile));
52
53  return (create_tmpfile(module_def));
54}
55
56/*========================================================================*/
57int create_tmpfile(
58  moddefv module_def,
59  int which
60)
61{
62  char tmpfile[64];
63  FILE *fp;
64
65  memset(tmpfile, '\0', sizeof(tmpfile));
66  snprintf(tmpfile, sizeof(tmpfile), "modgen.tmpXXXXXX");
67#ifdef HAVE_MKSTEMP
68  mkstemp(tmpfile);
69#else /* HAVE_MKSTEMP */
70  mktemp(tmpfile);
71#endif /* HAVE_MKSTEMP */
72
73  if(debug)printf("create_tmpfile '%s'\n", tmpfile );
74
75  if (close(creat(tmpfile, 0600)) < 0) {
76    (void) unlink (tmpfile);        /*  Blow it away!!  */
77    return -1;
78  } else if ((fp = fopen(tmpfile, "a+")) == NULL) {
79    (void) unlink (tmpfile);        /*  Blow it away!!  */
80    return -1;
81  } else {
82    (void) unlink (tmpfile); /* delete now to avoid turds... */
83  }
84
85  switch(which) {
86      case 0: module_def->fmtfp  = fp; break;
87      case 1: module_def->fmtfp2 = fp; break;
88      case 2: module_def->fmtfp3 = fp; break;
89      default:
90        fclose(fp);
91        return -1;
92  }
93
94  return 0;
95}
96
97/*========================================================================*/
98char *build_filename(
99  moddefv module,
100  char *text,
101  int what
102)
103{
104  STATIC_VAR char p[512];
105
106  if(do_create_srcdir)
107  {
108    switch(what)
109    {
110        case 1:
111          snprintf(p, sizeof(p), "%s/%s.cc", module->name, text);
112          break;
113        case 2:
114          snprintf(p, sizeof(p), "%s/%s.h", module->name, text);
115          break;
116        case 3:
117          snprintf(p, sizeof(p), "%s/%s.bin", module->name, text);
118          break;
119        case 4:
120          snprintf(p, sizeof(p), "%s/%s.pl", module->name, text);
121          break;
122        default:
123          snprintf(p, sizeof(p), "%s/%s", module->name, text);
124          break;
125    }
126  } else {
127    switch(what)
128    {
129      case 1:
130        snprintf(p, sizeof(p), "%s.cc",text);
131        break;
132      case 2:
133        snprintf(p, sizeof(p), "%s.h",text);
134        break;
135      case 3:
136        snprintf(p, sizeof(p), "%s.bin",text);
137        break;
138      case 4:
139        snprintf(p, sizeof(p), "%s.pl",text);
140        break;
141      default:
142        snprintf(p, sizeof(p), "%s",text);
143        break;
144    }
145  }
146  return p;
147}
148
149/*========================================================================*/
150int myyyerror(
151  char *fmt, ...
152  )
153{
154  va_list ap;
155  va_start(ap, fmt);
156  vprintf(fmt, ap);
157  va_end(ap);
158  return 2;
159}
160
161/*========================================================================*
162 * crc checksum of a file - as in POSIX cksum programm                    *
163 *========================================================================*/
164unsigned long crccheck(
165  char *file
166  )
167{
168  unsigned char buf[BUFLEN1];
169  unsigned long crc = 0;
170  long length = 0;
171  long bytes_read;
172  register FILE *fp;
173
174  // open the file
175  fp = fopen (file, "rb");
176  if (fp == NULL)
177  {
178    return 0;
179  }
180
181  // read the file chunk by chunk, determine the lenght and
182  // start computing the checksum
183  while ((bytes_read = fread (buf, 1, BUFLEN1, fp)) > 0)
184  {
185    unsigned char *cp = buf;
186    length += bytes_read;
187    while (bytes_read--)
188      crc = (crc << 8) ^ crctab[((crc >> 24) ^ *(cp++)) & 0xFF];
189  }
190
191  // check if something went wrong and close the file
192  if (ferror (fp)) return 0;
193  if (fclose(fp)==EOF) return 0;
194
195  // second part of checksum computation
196  bytes_read = length;
197  while (bytes_read > 0)
198  {
199    crc = (crc << 8) ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF];
200    bytes_read >>= 8;
201  }
202
203  crc = ~crc & 0xFFFFFFFF;
204
205  return(crc);
206}
207
208/*========================================================================*
209 * write function to compute crc checksum of a file into the automatically*
210 * generated  file                                                        *
211 *========================================================================*/
212void write_crccheck(FILE *fp)
213{
214  fprintf(fp, "\nunsigned long crccheck(\n");
215  fprintf(fp, "  char *file\n");
216  fprintf(fp, "  ) \n{\n  unsigned char buf[BUFLEN1];\n");
217  fprintf(fp, "  unsigned long crc = 0;\n");
218  fprintf(fp, "  long length = 0;\n");
219  fprintf(fp, "  long bytes_read;\n");
220  fprintf(fp, "  register FILE *fp;\n\n");
221  fprintf(fp, "  // open the file\n  fp = fopen (file, \"rb\");\n");
222  fprintf(fp, "  if (fp == NULL) return 0;\n\n");
223  fprintf(fp, "  // read the file chunk by chunk, determine the lenght and\n");
224  fprintf(fp, "  // start computing the checksum\n");
225  fprintf(fp, "  while ((bytes_read = fread (buf, 1, BUFLEN1, fp)) > 0)\n");
226  fprintf(fp, "  {\n    unsigned char *cp = buf;\n");
227  fprintf(fp, "    length += bytes_read;\n");
228  fprintf(fp, "    while (bytes_read--)\n");
229  fprintf(fp, "      crc = (crc << 8) ^ crctab[((crc >> 24) ^ *(cp++)) & 0xFF];\n");
230  fprintf(fp, "    }\n\n");
231  fprintf(fp, "  // check if something went wrong and close the file\n");
232  fprintf(fp, "  if (ferror (fp)) return 0; \n");
233  fprintf(fp, "  if (fclose(fp)==EOF) return 0;\n\n");
234  fprintf(fp, "  // second part of checksum computation\n");
235  fprintf(fp, "  bytes_read = length;\n");
236  fprintf(fp, "  while (bytes_read > 0) {\n");
237  fprintf(fp, "    crc = (crc << 8) ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF];\n");
238  fprintf(fp, "    bytes_read >>= 8;\n  }\n\n");
239  fprintf(fp, "  crc = ~crc & 0xFFFFFFFF;\n\n");
240  fprintf(fp, "  return(crc);\n}\n");
241  modlineno+=37;
242}
243void write_crctable(FILE *fp)
244{
245
246fprintf(fp, "/*********************************************************************\n");
247fprintf(fp, " * crctab.h crctable necessary for the use of crccheck from utils.cc *\n");
248fprintf(fp, " *********************************************************************/\n");
249fprintf(fp, "# define BUFLEN1 (1 << 16)\n\n");
250
251fprintf(fp, "static unsigned long const crctab[256] =\n");
252fprintf(fp, "{\n");
253fprintf(fp, "  0x0,\n");
254fprintf(fp, "  0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B,\n");
255fprintf(fp, "  0x1A864DB2, 0x1E475005, 0x2608EDB8, 0x22C9F00F, 0x2F8AD6D6,\n");
256fprintf(fp, "  0x2B4BCB61, 0x350C9B64, 0x31CD86D3, 0x3C8EA00A, 0x384FBDBD,\n");
257fprintf(fp, "  0x4C11DB70, 0x48D0C6C7, 0x4593E01E, 0x4152FDA9, 0x5F15ADAC,\n");
258fprintf(fp, "  0x5BD4B01B, 0x569796C2, 0x52568B75, 0x6A1936C8, 0x6ED82B7F,\n");
259fprintf(fp, "  0x639B0DA6, 0x675A1011, 0x791D4014, 0x7DDC5DA3, 0x709F7B7A,\n");
260fprintf(fp, "  0x745E66CD, 0x9823B6E0, 0x9CE2AB57, 0x91A18D8E, 0x95609039,\n");
261fprintf(fp, "  0x8B27C03C, 0x8FE6DD8B, 0x82A5FB52, 0x8664E6E5, 0xBE2B5B58,\n");
262fprintf(fp, "  0xBAEA46EF, 0xB7A96036, 0xB3687D81, 0xAD2F2D84, 0xA9EE3033,\n");
263fprintf(fp, "  0xA4AD16EA, 0xA06C0B5D, 0xD4326D90, 0xD0F37027, 0xDDB056FE,\n");
264fprintf(fp, "  0xD9714B49, 0xC7361B4C, 0xC3F706FB, 0xCEB42022, 0xCA753D95,\n");
265fprintf(fp, "  0xF23A8028, 0xF6FB9D9F, 0xFBB8BB46, 0xFF79A6F1, 0xE13EF6F4,\n");
266fprintf(fp, "  0xE5FFEB43, 0xE8BCCD9A, 0xEC7DD02D, 0x34867077, 0x30476DC0,\n");
267fprintf(fp, "  0x3D044B19, 0x39C556AE, 0x278206AB, 0x23431B1C, 0x2E003DC5,\n");
268fprintf(fp, "  0x2AC12072, 0x128E9DCF, 0x164F8078, 0x1B0CA6A1, 0x1FCDBB16,\n");
269fprintf(fp, "  0x018AEB13, 0x054BF6A4, 0x0808D07D, 0x0CC9CDCA, 0x7897AB07,\n");
270fprintf(fp, "  0x7C56B6B0, 0x71159069, 0x75D48DDE, 0x6B93DDDB, 0x6F52C06C,\n");
271fprintf(fp, "  0x6211E6B5, 0x66D0FB02, 0x5E9F46BF, 0x5A5E5B08, 0x571D7DD1,\n");
272fprintf(fp, "  0x53DC6066, 0x4D9B3063, 0x495A2DD4, 0x44190B0D, 0x40D816BA,\n");
273fprintf(fp, "  0xACA5C697, 0xA864DB20, 0xA527FDF9, 0xA1E6E04E, 0xBFA1B04B,\n");
274fprintf(fp, "  0xBB60ADFC, 0xB6238B25, 0xB2E29692, 0x8AAD2B2F, 0x8E6C3698,\n");
275fprintf(fp, "  0x832F1041, 0x87EE0DF6, 0x99A95DF3, 0x9D684044, 0x902B669D,\n");
276fprintf(fp, "  0x94EA7B2A, 0xE0B41DE7, 0xE4750050, 0xE9362689, 0xEDF73B3E,\n");
277fprintf(fp, "  0xF3B06B3B, 0xF771768C, 0xFA325055, 0xFEF34DE2, 0xC6BCF05F,\n");
278fprintf(fp, "  0xC27DEDE8, 0xCF3ECB31, 0xCBFFD686, 0xD5B88683, 0xD1799B34,\n");
279fprintf(fp, "  0xDC3ABDED, 0xD8FBA05A, 0x690CE0EE, 0x6DCDFD59, 0x608EDB80,\n");
280fprintf(fp, "  0x644FC637, 0x7A089632, 0x7EC98B85, 0x738AAD5C, 0x774BB0EB,\n");
281fprintf(fp, "  0x4F040D56, 0x4BC510E1, 0x46863638, 0x42472B8F, 0x5C007B8A,\n");
282fprintf(fp, "  0x58C1663D, 0x558240E4, 0x51435D53, 0x251D3B9E, 0x21DC2629,\n");
283fprintf(fp, "  0x2C9F00F0, 0x285E1D47, 0x36194D42, 0x32D850F5, 0x3F9B762C,\n");
284fprintf(fp, "  0x3B5A6B9B, 0x0315D626, 0x07D4CB91, 0x0A97ED48, 0x0E56F0FF,\n");
285fprintf(fp, "  0x1011A0FA, 0x14D0BD4D, 0x19939B94, 0x1D528623, 0xF12F560E,\n");
286fprintf(fp, "  0xF5EE4BB9, 0xF8AD6D60, 0xFC6C70D7, 0xE22B20D2, 0xE6EA3D65,\n");
287fprintf(fp, "  0xEBA91BBC, 0xEF68060B, 0xD727BBB6, 0xD3E6A601, 0xDEA580D8,\n");
288fprintf(fp, "  0xDA649D6F, 0xC423CD6A, 0xC0E2D0DD, 0xCDA1F604, 0xC960EBB3,\n");
289fprintf(fp, "  0xBD3E8D7E, 0xB9FF90C9, 0xB4BCB610, 0xB07DABA7, 0xAE3AFBA2,\n");
290fprintf(fp, "  0xAAFBE615, 0xA7B8C0CC, 0xA379DD7B, 0x9B3660C6, 0x9FF77D71,\n");
291fprintf(fp, "  0x92B45BA8, 0x9675461F, 0x8832161A, 0x8CF30BAD, 0x81B02D74,\n");
292fprintf(fp, "  0x857130C3, 0x5D8A9099, 0x594B8D2E, 0x5408ABF7, 0x50C9B640,\n");
293fprintf(fp, "  0x4E8EE645, 0x4A4FFBF2, 0x470CDD2B, 0x43CDC09C, 0x7B827D21,\n");
294fprintf(fp, "  0x7F436096, 0x7200464F, 0x76C15BF8, 0x68860BFD, 0x6C47164A,\n");
295fprintf(fp, "  0x61043093, 0x65C52D24, 0x119B4BE9, 0x155A565E, 0x18197087,\n");
296fprintf(fp, "  0x1CD86D30, 0x029F3D35, 0x065E2082, 0x0B1D065B, 0x0FDC1BEC,\n");
297fprintf(fp, "  0x3793A651, 0x3352BBE6, 0x3E119D3F, 0x3AD08088, 0x2497D08D,\n");
298fprintf(fp, "  0x2056CD3A, 0x2D15EBE3, 0x29D4F654, 0xC5A92679, 0xC1683BCE,\n");
299fprintf(fp, "  0xCC2B1D17, 0xC8EA00A0, 0xD6AD50A5, 0xD26C4D12, 0xDF2F6BCB,\n");
300fprintf(fp, "  0xDBEE767C, 0xE3A1CBC1, 0xE760D676, 0xEA23F0AF, 0xEEE2ED18,\n");
301fprintf(fp, "  0xF0A5BD1D, 0xF464A0AA, 0xF9278673, 0xFDE69BC4, 0x89B8FD09,\n");
302fprintf(fp, "  0x8D79E0BE, 0x803AC667, 0x84FBDBD0, 0x9ABC8BD5, 0x9E7D9662,\n");
303fprintf(fp, "  0x933EB0BB, 0x97FFAD0C, 0xAFB010B1, 0xAB710D06, 0xA6322BDF,\n");
304fprintf(fp, "  0xA2F33668, 0xBCB4666D, 0xB8757BDA, 0xB5365D03, 0xB1F740B4\n");
305fprintf(fp, "};\n");
306}
Note: See TracBrowser for help on using the repository browser.