FORM  4.2
setfile.c
Go to the documentation of this file.
1 
5 /* #[ License : */
6 /*
7  * Copyright (C) 1984-2017 J.A.M. Vermaseren
8  * When using this file you are requested to refer to the publication
9  * J.A.M.Vermaseren "New features of FORM" math-ph/0010025
10  * This is considered a matter of courtesy as the development was paid
11  * for by FOM the Dutch physics granting agency and we would like to
12  * be able to track its scientific use to convince FOM of its value
13  * for the community.
14  *
15  * This file is part of FORM.
16  *
17  * FORM is free software: you can redistribute it and/or modify it under the
18  * terms of the GNU General Public License as published by the Free Software
19  * Foundation, either version 3 of the License, or (at your option) any later
20  * version.
21  *
22  * FORM is distributed in the hope that it will be useful, but WITHOUT ANY
23  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25  * details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with FORM. If not, see <http://www.gnu.org/licenses/>.
29  */
30 /* #] License : */
31 /*
32  #[ Includes :
33 
34  Routines that deal with settings and the setup file
35 */
36 
37 #include "form3.h"
38 
39 char curdirp[] = ".";
40 char cursortdirp[] = ".";
41 char commentchar[] = "*";
42 char dotchar[] = "_";
43 char highfirst[] = "highfirst";
44 char lowfirst[] = "lowfirst";
45 char procedureextension[] = "prc";
46 
47 #define NUMERICALVALUE 0
48 #define STRINGVALUE 1
49 #define PATHVALUE 2
50 #define ONOFFVALUE 3
51 #define DEFINEVALUE 4
52 
53 SETUPPARAMETERS setupparameters[] =
54 {
55  {(UBYTE *)"bracketindexsize", NUMERICALVALUE, 0, (LONG)MAXBRACKETBUFFERSIZE}
56  ,{(UBYTE *)"commentchar", STRINGVALUE, 0, (LONG)commentchar}
57  ,{(UBYTE *)"compresssize", NUMERICALVALUE, 0, (LONG)COMPRESSBUFFER}
58  ,{(UBYTE *)"constindex", NUMERICALVALUE, 0, (LONG)NUMFIXED}
59  ,{(UBYTE *)"continuationlines", NUMERICALVALUE, 0, (LONG)FORTRANCONTINUATIONLINES}
60  ,{(UBYTE *)"define", DEFINEVALUE, 0, (LONG)0}
61  ,{(UBYTE *)"dotchar", STRINGVALUE, 0, (LONG)dotchar}
62  ,{(UBYTE *)"factorizationcache", NUMERICALVALUE, 0, (LONG)FBUFFERSIZE}
63  ,{(UBYTE *)"filepatches", NUMERICALVALUE, 0, (LONG)MAXFPATCHES}
64  ,{(UBYTE *)"functionlevels", NUMERICALVALUE, 0, (LONG)MAXFLEVELS}
65  ,{(UBYTE *)"hidesize", NUMERICALVALUE, 0, (LONG)0}
66  ,{(UBYTE *)"incdir", PATHVALUE, 0, (LONG)curdirp}
67  ,{(UBYTE *)"indentspace", NUMERICALVALUE, 0, (LONG)INDENTSPACE}
68  ,{(UBYTE *)"insidefirst", ONOFFVALUE, 0, (LONG)1}
69  ,{(UBYTE *)"largepatches", NUMERICALVALUE, 0, (LONG)MAXPATCHES}
70  ,{(UBYTE *)"largesize", NUMERICALVALUE, 0, (LONG)LARGEBUFFER}
71  ,{(UBYTE *)"maxnumbersize", NUMERICALVALUE, 0, (LONG)0}
72 /* ,{(UBYTE *)"maxnumbersize", NUMERICALVALUE, 0, (LONG)MAXNUMBERSIZE} */
73  ,{(UBYTE *)"maxtermsize", NUMERICALVALUE, 0, (LONG)MAXTER}
74  ,{(UBYTE *)"maxwildcards", NUMERICALVALUE, 0, (LONG)MAXWILDC}
75  ,{(UBYTE *)"nospacesinnumbers", ONOFFVALUE, 0, (LONG)0}
76  ,{(UBYTE *)"numstorecaches", NUMERICALVALUE, 0, (LONG)NUMSTORECACHES}
77  ,{(UBYTE *)"nwritefinalstatistics", ONOFFVALUE, 0, (LONG)0}
78  ,{(UBYTE *)"nwriteprocessstatistics", ONOFFVALUE, 0, (LONG)0}
79  ,{(UBYTE *)"nwritestatistics", ONOFFVALUE, 0, (LONG)0}
80  ,{(UBYTE *)"nwritethreadstatistics", ONOFFVALUE, 0, (LONG)0}
81  ,{(UBYTE *)"oldfactarg", ONOFFVALUE, 0, (LONG)NEWFACTARG}
82  ,{(UBYTE *)"oldgcd", ONOFFVALUE, 0, (LONG)1}
83  ,{(UBYTE *)"oldorder", ONOFFVALUE, 0, (LONG)0}
84  ,{(UBYTE *)"oldparallelstatistics", ONOFFVALUE, 0, (LONG)0}
85  ,{(UBYTE *)"parentheses", NUMERICALVALUE, 0, (LONG)MAXPARLEVEL}
86  ,{(UBYTE *)"path", PATHVALUE, 0, (LONG)curdirp}
87  ,{(UBYTE *)"procedureextension", STRINGVALUE, 0, (LONG)procedureextension}
88  ,{(UBYTE *)"processbucketsize", NUMERICALVALUE, 0, (LONG)DEFAULTPROCESSBUCKETSIZE}
89  ,{(UBYTE *)"resettimeonclear", ONOFFVALUE, 0, (LONG)1}
90  ,{(UBYTE *)"scratchsize", NUMERICALVALUE, 0, (LONG)SCRATCHSIZE}
91  ,{(UBYTE *)"shmwinsize", NUMERICALVALUE, 0, (LONG)SHMWINSIZE}
92  ,{(UBYTE *)"sizestorecache", NUMERICALVALUE, 0, (LONG)SIZESTORECACHE}
93  ,{(UBYTE *)"smallextension", NUMERICALVALUE, 0, (LONG)SMALLOVERFLOW}
94  ,{(UBYTE *)"smallsize", NUMERICALVALUE, 0, (LONG)SMALLBUFFER}
95  ,{(UBYTE *)"sortiosize", NUMERICALVALUE, 0, (LONG)SORTIOSIZE}
96  ,{(UBYTE *)"sorttype", STRINGVALUE, 0, (LONG)lowfirst}
97  ,{(UBYTE *)"spectatorsize", NUMERICALVALUE, 0, (LONG)SPECTATORSIZE}
98  ,{(UBYTE *)"subfilepatches", NUMERICALVALUE, 0, (LONG)SMAXFPATCHES}
99  ,{(UBYTE *)"sublargepatches", NUMERICALVALUE, 0, (LONG)SMAXPATCHES}
100  ,{(UBYTE *)"sublargesize", NUMERICALVALUE, 0, (LONG)SLARGEBUFFER}
101  ,{(UBYTE *)"subsmallextension", NUMERICALVALUE, 0, (LONG)SSMALLOVERFLOW}
102  ,{(UBYTE *)"subsmallsize", NUMERICALVALUE, 0, (LONG)SSMALLBUFFER}
103  ,{(UBYTE *)"subsortiosize", NUMERICALVALUE, 0, (LONG)SSORTIOSIZE}
104  ,{(UBYTE *)"subtermsinsmall", NUMERICALVALUE, 0, (LONG)STERMSSMALL}
105  ,{(UBYTE *)"tempdir", STRINGVALUE, 0, (LONG)curdirp}
106  ,{(UBYTE *)"tempsortdir", STRINGVALUE, 0, (LONG)cursortdirp}
107  ,{(UBYTE *)"termsinsmall", NUMERICALVALUE, 0, (LONG)TERMSSMALL}
108  ,{(UBYTE *)"threadbucketsize", NUMERICALVALUE, 0, (LONG)DEFAULTTHREADBUCKETSIZE}
109  ,{(UBYTE *)"threadloadbalancing", ONOFFVALUE, 0, (LONG)DEFAULTTHREADLOADBALANCING}
110  ,{(UBYTE *)"threads", NUMERICALVALUE, 0, (LONG)DEFAULTTHREADS}
111  ,{(UBYTE *)"threadscratchoutsize", NUMERICALVALUE, 0, (LONG)THREADSCRATCHOUTSIZE}
112  ,{(UBYTE *)"threadscratchsize", NUMERICALVALUE, 0, (LONG)THREADSCRATCHSIZE}
113  ,{(UBYTE *)"threadsortfilesynch", ONOFFVALUE, 0, (LONG)0}
114  ,{(UBYTE *)"totalsize", ONOFFVALUE, 0, (LONG)2}
115  ,{(UBYTE *)"workspace", NUMERICALVALUE, 0, (LONG)WORKBUFFER}
116  ,{(UBYTE *)"wtimestats", ONOFFVALUE, 0, (LONG)2}
117 };
118 
119 /*
120  #] Includes :
121  #[ Setups :
122  #[ DoSetups :
123 */
124 
125 int DoSetups()
126 {
127  UBYTE *setbuffer, *s, *t, *u /*, c */;
128  int errors = 0;
129  setbuffer = LoadInputFile((UBYTE *)setupfilename,SETUPFILE);
130  if ( setbuffer ) {
131 /*
132  The contents of the file are now in setbuffer.
133  Each line is commentary or a single command.
134  The buffer is terminated with a zero.
135 */
136  s = setbuffer;
137  while ( *s ) {
138  if ( *s == ' ' || *s == '\t' || *s == '*' || *s == '#' || *s == '\n' ) {
139  while ( *s && *s != '\n' ) s++;
140  }
141  else if ( tolower(*s) < 'a' || tolower(*s) > 'z' ) {
142  t = s;
143  while ( *s && *s != '\n' ) s++;
144 /*
145  c = *s; *s = 0;
146  Error1("Setup file: Illegal statement: ",t);
147  errors++; *s = c;
148 */
149  }
150  else {
151  t = s; /* name of the option */
152  while ( tolower(*s) >= 'a' && tolower(*s) <= 'z' ) s++;
153  *s++ = 0;
154  while ( *s == ' ' || *s == '\t' ) s++;
155  u = s; /* 'value' of the option */
156  while ( *s && *s != '\n' && *s != '\r' ) s++;
157  if ( *s ) *s++ = 0;
158  errors += ProcessOption(t,u,0);
159  }
160  while ( *s == '\n' || *s == '\r' ) s++;
161  }
162  M_free(setbuffer,"setup file buffer");
163  }
164  if ( errors ) return(1);
165  else return(0);
166 }
167 
168 /*
169  #] DoSetups :
170  #[ ProcessOption :
171 */
172 
173 static char *proop1[3] = { "Setup file", "Setups in .frm file", "Setup in environment" };
174 
175 int ProcessOption(UBYTE *s1, UBYTE *s2, int filetype)
176 {
177  SETUPPARAMETERS *sp;
178  int n, giveback = 0, error = 0;
179  UBYTE *s, *t, *s2ret;
180  LONG x;
181  sp = GetSetupPar(s1);
182  if ( sp ) {
183 /*
184  We check now whether there are `' variables to be looked up in the
185  environment. This is new (30-may-2008). This is only allowed in s2.
186 */
187 restart:;
188  {
189  UBYTE *s3,*s4,*s5,*s6, c, *start;
190  int n1,n2,n3;
191  s = s2;
192  while ( *s ) {
193  if ( *s == '\\' ) s += 2;
194  else if ( *s == '`' ) {
195  start = s; s++;
196  while ( *s && *s != '\'' ) {
197  if ( *s == '\\' ) s++;
198  s++;
199  }
200  if ( *s == 0 ) {
201  MesPrint("%s: Illegal use of ` character for parameter %s"
202  ,proop1[filetype],s1);
203  return(1);
204  }
205  c = *s; *s = 0;
206  s3 = (UBYTE *)getenv((char *)(start+1));
207  if ( s3 == 0 ) {
208  MesPrint("%s: Cannot find environment variable %s for parameter %s"
209  ,proop1[filetype],start+1,s1);
210  return(1);
211 
212  }
213  *s = c; s++;
214  n1 = start - s2; s4 = s3; n2 = 0;
215  while ( *s4 ) {
216  if ( *s4 == '\\' ) { s4++; n2++; }
217  s4++; n2++;
218  }
219  s4 = s; n3 = 0;
220  while ( *s4 ) {
221  if ( *s4 == '\\' ) { s4++; n3++; }
222  s4++; n3++;
223  }
224  s4 = (UBYTE *)Malloc1((n1+n2+n3+1)*sizeof(UBYTE),"environment in setup");
225  s5 = s2; s6 = s4;
226  while ( n1-- > 0 ) *s6++ = *s5++;
227  s5 = s3;
228  while ( n2-- > 0 ) *s6++ = *s5++;
229  s5 = s;
230  while ( n3-- > 0 ) *s6++ = *s5++;
231  *s6 = 0;
232  if ( giveback ) M_free(s2,"environment in setup");
233  s2 = s4;
234  giveback = 1;
235  goto restart;
236  }
237  else s++;
238  }
239  }
240  n = sp->type;
241  s2ret = s2;
242  switch ( n ) {
243  case NUMERICALVALUE:
244  ParseNumber(x,s2);
245  if ( *s2 == 'K' ) { x = x * 1000; s2++; }
246  else if ( *s2 == 'M' ) { x = x * 1000000; s2++; }
247  else if ( *s2 == 'G' ) { x = x * 1000000000; s2++; }
248  else if ( *s2 == 'T' ) { x = x * 1000000000000; s2++; }
249  if ( *s2 && *s2 != ' ' && *s2 != '\t' ) {
250  MesPrint("%s: Numerical value expected for parameter %s"
251  ,proop1[filetype],s1);
252  error = 1; break;
253  }
254  sp->value = x;
255  sp->flags = USEDFLAG;
256  break;
257  case STRINGVALUE:
258  if ( StrICmp(s1,(UBYTE *)"tempsortdir") == 0 ) AM.havesortdir = 1;
259  s = s2; t = s2;
260  while ( *s ) {
261  if ( *s == ' ' || *s == '\t' ) break;
262  if ( *s == '\\' ) s++;
263  *t++ = *s++;
264  }
265  *t = 0;
266  if ( sp->flags == USEDFLAG && sp->value != 0 )
267  M_free((VOID *)(sp->value),"Process option");
268  sp->value = (LONG)strDup1(s2,"Process option");
269  sp->flags = USEDFLAG;
270  break;
271  case PATHVALUE:
272  if ( StrICmp(s1,(UBYTE *)"incdir") == 0 ) {
273  AM.IncDir = 0;
274  }
275  else if ( StrICmp(s1,(UBYTE *)"path") == 0 ) {
276  if ( AM.Path ) M_free(AM.Path,"path");
277  AM.Path = 0;
278  }
279  else {
280  MesPrint("Setups: %s not yet implemented",s1);
281  error = 1;
282  break;
283  }
284  if ( sp->flags == USEDFLAG && sp->value != 0 )
285  M_free((VOID *)(sp->value),"Process option");
286  sp->value = (LONG)strDup1(s2,"Process option");
287  sp->flags = USEDFLAG;
288  break;
289  case ONOFFVALUE:
290  if ( tolower(*s2) == 'o' && tolower(s2[1]) == 'n'
291  && ( s2[2] == 0 || s2[2] == ' ' || s2[2] == '\t' ) )
292  sp->value = 1;
293  else if ( tolower(*s2) == 'o' && tolower(s2[1]) == 'f'
294  && tolower(s2[2]) == 'f'
295  && ( s2[3] == 0 || s2[3] == ' ' || s2[3] == '\t' ) )
296  sp->value = 0;
297  else {
298  MesPrint("%s: Unrecognized option for parameter %s: %s"
299  ,proop1[filetype],s1,s2);
300  error = 1; break;
301  }
302  sp->flags = USEDFLAG;
303  break;
304  case DEFINEVALUE:
305 /*
306  if ( sp->value ) M_free((UBYTE *)(sp->value),"Process option");
307  sp->value = (LONG)strDup1(s2,"Process option");
308 */
309  if ( TheDefine(s2,2) ) error = 1;
310  break;
311  default:
312  Error1("Error in setupparameter table for:",s1);
313  error = 1;
314  break;
315  }
316  }
317  else {
318  MesPrint("%s: Keyword not recognized: %s",proop1[filetype],s1);
319  error = 1;
320  }
321  if ( giveback ) M_free(s2ret,"environment in setup");
322  return(error);
323 }
324 
325 /*
326  #] ProcessOption :
327  #[ GetSetupPar :
328 */
329 
330 SETUPPARAMETERS *GetSetupPar(UBYTE *s)
331 {
332  int hi, med, lo, i;
333  lo = 0;
334  hi = sizeof(setupparameters)/sizeof(SETUPPARAMETERS);
335  do {
336  med = ( hi + lo ) / 2;
337  i = StrICmp(s,(UBYTE *)setupparameters[med].parameter);
338  if ( i == 0 ) return(setupparameters+med);
339  if ( i < 0 ) hi = med-1;
340  else lo = med+1;
341  } while ( hi >= lo );
342  return(0);
343 }
344 
345 /*
346  #] GetSetupPar :
347  #[ RecalcSetups :
348 */
349 
350 int RecalcSetups()
351 {
352  SETUPPARAMETERS *sp, *sp1;
353 
354  sp1 = GetSetupPar((UBYTE *)"threads");
355  if ( AM.totalnumberofthreads > 1 ) sp1->value = AM.totalnumberofthreads - 1;
356  else sp1->value = 0;
357 /*
358  if ( sp1->value > 0 ) AM.totalnumberofthreads = sp1->value+1;
359  if ( AM.totalnumberofthreads == 0 ) AM.totalnumberofthreads = 1;
360 */
361  sp = GetSetupPar((UBYTE *)"filepatches");
362  if ( sp->value < AM.totalnumberofthreads-1 )
363  sp->value = AM.totalnumberofthreads - 1;
364 
365  sp = GetSetupPar((UBYTE *)"smallsize");
366  sp1 = GetSetupPar((UBYTE *)"smallextension");
367  if ( 6*sp1->value < 7*sp->value ) sp1->value = (7*sp->value)/6;
368  sp = GetSetupPar((UBYTE *)"termsinsmall");
369  sp->value = ( sp->value + 15 ) & (-16L);
370 #ifdef WITHPTHREADS
371  {
372  SETUPPARAMETERS *sp2;
373  LONG totalsize, minimumsize;
374  sp = GetSetupPar((UBYTE *)"largesize");
375  totalsize = sp1->value+sp->value;
376  sp2 = GetSetupPar((UBYTE *)"maxtermsize");
377  AM.MaxTer = sp2->value*sizeof(WORD);
378  if ( AM.MaxTer < 200*(LONG)(sizeof(WORD)) ) AM.MaxTer = 200*(LONG)(sizeof(WORD));
379  if ( AM.MaxTer > MAXPOSITIVE - 200*(LONG)(sizeof(WORD)) ) AM.MaxTer = MAXPOSITIVE - 200*(LONG)(sizeof(WORD));
380  AM.MaxTer /= sizeof(WORD);
381  AM.MaxTer *= sizeof(WORD);
382  minimumsize = (AM.totalnumberofthreads-1)*(AM.MaxTer+
383  NUMBEROFBLOCKSINSORT*MINIMUMNUMBEROFTERMS*AM.MaxTer);
384  if ( totalsize < minimumsize ) {
385  sp->value = minimumsize - sp1->value;
386  }
387  }
388 #endif
389  return(0);
390 }
391 
392 /*
393  #] RecalcSetups :
394  #[ AllocSetups :
395 */
396 
397 int AllocSetups()
398 {
399  SETUPPARAMETERS *sp;
400  LONG LargeSize, SmallSize, SmallEsize, TermsInSmall, IOsize;
401  int MaxPatches, MaxFpatches, error = 0, i, size;
402  UBYTE *s;
403 #ifndef WITHPTHREADS
404  int j;
405 #endif
406  sp = GetSetupPar((UBYTE *)"threads");
407  if ( sp->value > 0 ) AM.totalnumberofthreads = sp->value+1;
408 
409  AM.OutBuffer = (UBYTE *)Malloc1(AM.OutBufSize+1,"OutputBuffer");
410  AP.PreAssignStack =(LONG *)Malloc1(AP.MaxPreAssignLevel*sizeof(LONG *),"PreAssignStack");
411  for ( i = 0; i < AP.MaxPreAssignLevel; i++ ) AP.PreAssignStack[i] = 0;
412  AC.iBuffer = (UBYTE *)Malloc1(AC.iBufferSize+1,"statement buffer");
413  AC.iStop = AC.iBuffer + AC.iBufferSize-2;
414  AP.preStart = (UBYTE *)Malloc1(AP.pSize,"instruction buffer");
415  AP.preStop = AP.preStart + AP.pSize - 3;
416  /* AP.PreIfStack is already allocated in StartPrepro(), but to be sure we
417  "if" the freeing */
418  if ( AP.PreIfStack ) M_free(AP.PreIfStack,"PreIfStack");
419  AP.PreIfStack = (int *)Malloc1(AP.MaxPreIfLevel*sizeof(int),
420  "Preprocessor if stack");
421  AP.PreIfStack[0] = EXECUTINGIF;
422  sp = GetSetupPar((UBYTE *)"insidefirst");
423  AM.ginsidefirst = AC.minsidefirst = AC.insidefirst = sp->value;
424 /*
425  We need to consider eliminating this variable
426 */
427  sp = GetSetupPar((UBYTE *)"maxtermsize");
428  AM.MaxTer = sp->value*sizeof(WORD);
429  if ( AM.MaxTer < 200*(LONG)(sizeof(WORD)) ) AM.MaxTer = 200*(LONG)(sizeof(WORD));
430  if ( AM.MaxTer > MAXPOSITIVE - 200*(LONG)(sizeof(WORD)) ) AM.MaxTer = MAXPOSITIVE - 200*(LONG)(sizeof(WORD));
431  AM.MaxTer /= (LONG)sizeof(WORD);
432  AM.MaxTer *= (LONG)sizeof(WORD);
433 /*
434  Allocate workspace.
435 */
436  sp = GetSetupPar((UBYTE *)"workspace");
437  AM.WorkSize = sp->value;
438 #ifdef WITHPTHREADS
439 #else
440  AT.WorkSpace = (WORD *)Malloc1(AM.WorkSize*sizeof(WORD),(char *)(sp->parameter));
441  AT.WorkTop = AT.WorkSpace + AM.WorkSize;
442  AT.WorkPointer = AT.WorkSpace;
443 #endif
444 /*
445  Fixed indices
446 */
447  sp = GetSetupPar((UBYTE *)"constindex");
448  if ( ( sp->value+100+5*WILDOFFSET ) > MAXPOSITIVE ) {
449  MesPrint("Setting of %s in setupfile too large","constindex");
450  AM.OffsetIndex = MAXPOSITIVE - 5*WILDOFFSET - 100;
451  MesPrint("value corrected to maximum allowed: %d",AM.OffsetIndex);
452  }
453  else AM.OffsetIndex = sp->value + 1;
454  AC.FixIndices = (WORD *)Malloc1((AM.OffsetIndex)*sizeof(WORD),(char *)(sp->parameter));
455  AM.WilInd = AM.OffsetIndex + WILDOFFSET;
456  AM.DumInd = AM.OffsetIndex + 2*WILDOFFSET;
457  AM.IndDum = AM.DumInd + WILDOFFSET;
458 #ifndef WITHPTHREADS
459  AR.CurDum = AN.IndDum = AM.IndDum;
460 #endif
461  AM.mTraceDum = AM.IndDum + 2*WILDOFFSET;
462 
463  sp = GetSetupPar((UBYTE *)"parentheses");
464  AM.MaxParLevel = sp->value+1;
465  AC.tokenarglevel = (WORD *)Malloc1((sp->value+1)*sizeof(WORD),(char *)(sp->parameter));
466 /*
467  Space during calculations
468 */
469  sp = GetSetupPar((UBYTE *)"maxnumbersize");
470 /*
471  size = ( sp->value + 11 ) & (-4);
472  AM.MaxTal = size - 2;
473  if ( AM.MaxTal > (AM.MaxTer/sizeof(WORD)-2)/2 )
474  AM.MaxTal = (AM.MaxTer/sizeof(WORD)-2)/2;
475  if ( AM.MaxTal < (AM.MaxTer/sizeof(WORD)-2)/4 )
476  AM.MaxTal = (AM.MaxTer/sizeof(WORD)-2)/4;
477 */
478 /*
479  There is too much confusion about MaxTal cq maxnumbersize.
480  It seems better to fix it at its maximum value. This way we only worry
481  about maxtermsize. This can be understood better by the 'innocent' user.
482 */
483  if ( sp->value == 0 ) {
484  AM.MaxTal = (AM.MaxTer/sizeof(WORD)-2)/2;
485  }
486  else {
487  size = ( sp->value + 11 ) & (-4);
488  AM.MaxTal = size - 2;
489  if ( (size_t)AM.MaxTal > (size_t)((AM.MaxTer/sizeof(WORD)-2)/2) )
490  AM.MaxTal = (AM.MaxTer/sizeof(WORD)-2)/2;
491  }
492  AM.MaxTal &= -sizeof(WORD)*2;
493 
494  sp->value = AM.MaxTal;
495  AC.cmod = (UWORD *)Malloc1(AM.MaxTal*4*sizeof(UWORD),(char *)(sp->parameter));
496  AM.gcmod = AC.cmod + AM.MaxTal;
497  AC.powmod = AM.gcmod + AM.MaxTal;
498  AM.gpowmod = AC.powmod + AM.MaxTal;
499 /*
500  The IO buffers for the input and output expressions.
501  Fscr[2] will be assigned in a later stage for hiding expressions from
502  the regular action. That will make the program faster.
503 */
504  sp = GetSetupPar((UBYTE *)"scratchsize");
505  AM.ScratSize = sp->value/sizeof(WORD);
506  if ( AM.ScratSize < 4*AM.MaxTer ) AM.ScratSize = 4*AM.MaxTer;
507  AM.HideSize = AM.ScratSize;
508  sp = GetSetupPar((UBYTE *)"hidesize");
509  if ( sp->value > 0 ) {
510  AM.HideSize = sp->value/sizeof(WORD);
511  if ( AM.HideSize < 4*AM.MaxTer ) AM.HideSize = 4*AM.MaxTer;
512  }
513  sp = GetSetupPar((UBYTE *)"factorizationcache");
514  AM.fbuffersize = sp->value;
515 #ifdef WITHPTHREADS
516  sp = GetSetupPar((UBYTE *)"threadscratchsize");
517  AM.ThreadScratSize = sp->value/sizeof(WORD);
518  sp = GetSetupPar((UBYTE *)"threadscratchoutsize");
519  AM.ThreadScratOutSize = sp->value/sizeof(WORD);
520 #endif
521 #ifndef WITHPTHREADS
522  for ( j = 0; j < 2; j++ ) {
523  WORD *ScratchBuf;
524  ScratchBuf = (WORD *)Malloc1(AM.ScratSize*sizeof(WORD),"scratchsize");
525  AR.Fscr[j].POsize = AM.ScratSize * sizeof(WORD);
526  AR.Fscr[j].POfull = AR.Fscr[j].POfill = AR.Fscr[j].PObuffer = ScratchBuf;
527  AR.Fscr[j].POstop = AR.Fscr[j].PObuffer + AM.ScratSize;
528  PUTZERO(AR.Fscr[j].POposition);
529  }
530  AR.Fscr[2].PObuffer = 0;
531 #endif
532  sp = GetSetupPar((UBYTE *)"threadbucketsize");
533  AC.ThreadBucketSize = AM.gThreadBucketSize = AM.ggThreadBucketSize = sp->value;
534  sp = GetSetupPar((UBYTE *)"threadloadbalancing");
535  AC.ThreadBalancing = AM.gThreadBalancing = AM.ggThreadBalancing = sp->value;
536  sp = GetSetupPar((UBYTE *)"threadsortfilesynch");
537  AC.ThreadSortFileSynch = AM.gThreadSortFileSynch = AM.ggThreadSortFileSynch = sp->value;
538 /*
539  The size for shared memory window for oneside MPI2 communications
540 */
541  sp = GetSetupPar((UBYTE *)"shmwinsize");
542  AM.shmWinSize = sp->value/sizeof(WORD);
543  if ( AM.shmWinSize < 4*AM.MaxTer ) AM.shmWinSize = 4*AM.MaxTer;
544 /*
545  The sort buffer
546 */
547  sp = GetSetupPar((UBYTE *)"smallsize");
548  SmallSize = sp->value;
549  sp = GetSetupPar((UBYTE *)"smallextension");
550  SmallEsize = sp->value;
551  sp = GetSetupPar((UBYTE *)"largesize");
552  LargeSize = sp->value;
553  sp = GetSetupPar((UBYTE *)"termsinsmall");
554  TermsInSmall = sp->value;
555  sp = GetSetupPar((UBYTE *)"largepatches");
556  MaxPatches = sp->value;
557  sp = GetSetupPar((UBYTE *)"filepatches");
558  MaxFpatches = sp->value;
559  sp = GetSetupPar((UBYTE *)"sortiosize");
560  IOsize = sp->value;
561  if ( IOsize < AM.MaxTer ) { IOsize = AM.MaxTer; sp->value = IOsize; }
562 #ifndef WITHPTHREADS
563 #ifdef WITHZLIB
564  for ( j = 0; j < 2; j++ ) { AR.Fscr[j].ziosize = IOsize; }
565 #endif
566 #endif
567  AM.S0 = 0;
568  AM.S0 = AllocSort(LargeSize,SmallSize,SmallEsize,TermsInSmall
569  ,MaxPatches,MaxFpatches,IOsize);
570 #ifdef WITHZLIB
571  AM.S0->file.ziosize = IOsize;
572 #ifndef WITHPTHREADS
573  AR.FoStage4[0].ziosize = IOsize;
574  AR.FoStage4[1].ziosize = IOsize;
575  AT.S0 = AM.S0;
576 #endif
577 #else
578 #ifndef WITHPTHREADS
579  AT.S0 = AM.S0;
580 #endif
581 #endif
582 #ifndef WITHPTHREADS
583  AR.FoStage4[0].POsize = ((IOsize+sizeof(WORD)-1)/sizeof(WORD))*sizeof(WORD);
584  AR.FoStage4[1].POsize = ((IOsize+sizeof(WORD)-1)/sizeof(WORD))*sizeof(WORD);
585 #endif
586  sp = GetSetupPar((UBYTE *)"subsmallsize");
587  AM.SSmallSize = sp->value;
588  sp = GetSetupPar((UBYTE *)"subsmallextension");
589  AM.SSmallEsize = sp->value;
590  sp = GetSetupPar((UBYTE *)"sublargesize");
591  AM.SLargeSize = sp->value;
592  sp = GetSetupPar((UBYTE *)"subtermsinsmall");
593  AM.STermsInSmall = sp->value;
594  sp = GetSetupPar((UBYTE *)"sublargepatches");
595  AM.SMaxPatches = sp->value;
596  sp = GetSetupPar((UBYTE *)"subfilepatches");
597  AM.SMaxFpatches = sp->value;
598  sp = GetSetupPar((UBYTE *)"subsortiosize");
599  AM.SIOsize = sp->value;
600  sp = GetSetupPar((UBYTE *)"spectatorsize");
601  AM.SpectatorSize = sp->value;
602 /*
603  The next code is just for the moment (26-jan-1997) because we have
604  the new parts combined with the old. Once the old parts are gone
605  from the program, we can eliminate this code too.
606 */
607  sp = GetSetupPar((UBYTE *)"functionlevels");
608  AM.maxFlevels = sp->value + 1;
609 #ifdef WITHPTHREADS
610 #else
611  AT.Nest = (NESTING)Malloc1((LONG)sizeof(struct NeStInG)*AM.maxFlevels,"functionlevels");
612  AT.NestStop = AT.Nest + AM.maxFlevels;
613  AT.NestPoin = AT.Nest;
614 #endif
615 
616  sp = GetSetupPar((UBYTE *)"maxwildcards");
617  AM.MaxWildcards = sp->value;
618 #ifdef WITHPTHREADS
619 #else
620  AT.WildMask = (WORD *)Malloc1((LONG)AM.MaxWildcards*sizeof(WORD),"maxwildcards");
621 #endif
622 
623  sp = GetSetupPar((UBYTE *)"compresssize");
624  if ( sp->value < 2*AM.MaxTer ) sp->value = 2*AM.MaxTer;
625  AM.CompressSize = sp->value;
626 #ifndef WITHPTHREADS
627  AR.CompressBuffer = (WORD *)Malloc1((AM.CompressSize+10)*sizeof(WORD),"compresssize");
628  AR.CompressPointer = AR.CompressBuffer;
629  AR.ComprTop = AR.CompressBuffer + AM.CompressSize;
630 #endif
631  sp = GetSetupPar((UBYTE *)"bracketindexsize");
632  if ( sp->value < 20*AM.MaxTer ) sp->value = 20*AM.MaxTer;
633  AM.MaxBracketBufferSize = sp->value/sizeof(WORD);
634 
635  sp = GetSetupPar((UBYTE *)"dotchar");
636  AO.FortDotChar = ((UBYTE *)(sp->value))[0];
637  sp = GetSetupPar((UBYTE *)"commentchar");
638  AP.cComChar = AP.ComChar = ((UBYTE *)(sp->value))[0];
639  sp = GetSetupPar((UBYTE *)"procedureextension");
640 /*
641  Check validity first.
642 */
643  s = (UBYTE *)(sp->value);
644  if ( FG.cTable[*s] != 0 ) {
645  MesPrint(" Illegal string for procedure extension %s",(UBYTE *)sp->value);
646  error = -2;
647  }
648  else {
649  s++;
650  while ( *s ) {
651  if ( *s == ' ' || *s == '\t' || *s == '\n' ) {
652  MesPrint(" Illegal string for procedure extension %s",(UBYTE *)sp->value);
653  error = -2;
654  break;
655  }
656  s++;
657  }
658  }
659  AP.cprocedureExtension = strDup1((UBYTE *)(sp->value),"procedureExtension");
660  AP.procedureExtension = strDup1(AP.cprocedureExtension,"procedureExtension");
661 
662  sp = GetSetupPar((UBYTE *)"totalsize");
663  if ( sp->value != 2 ) AM.PrintTotalSize = sp->value;
664 
665  sp = GetSetupPar((UBYTE *)"continuationlines");
666  AM.FortranCont = sp->value;
667  if ( AM.FortranCont <= 0 ) AM.FortranCont = 1;
668  sp = GetSetupPar((UBYTE *)"oldorder");
669  AM.OldOrderFlag = sp->value;
670  sp = GetSetupPar((UBYTE *)"resettimeonclear");
671  AM.resetTimeOnClear = sp->value;
672  sp = GetSetupPar((UBYTE *)"nospacesinnumbers");
673  AO.NoSpacesInNumbers = AM.gNoSpacesInNumbers = AM.ggNoSpacesInNumbers = sp->value;
674  sp = GetSetupPar((UBYTE *)"indentspace");
675  AO.IndentSpace = AM.gIndentSpace = AM.ggIndentSpace = sp->value;
676  sp = GetSetupPar((UBYTE *)"nwritestatistics");
677  AC.StatsFlag = AM.gStatsFlag = AM.ggStatsFlag = 1-sp->value;
678  sp = GetSetupPar((UBYTE *)"nwritefinalstatistics");
679  AC.FinalStats = AM.gFinalStats = AM.ggFinalStats = 1-sp->value;
680  sp = GetSetupPar((UBYTE *)"nwritethreadstatistics");
681  AC.ThreadStats = AM.gThreadStats = AM.ggThreadStats = 1-sp->value;
682  sp = GetSetupPar((UBYTE *)"nwriteprocessstatistics");
683  AC.ProcessStats = AM.gProcessStats = AM.ggProcessStats = 1-sp->value;
684  sp = GetSetupPar((UBYTE *)"oldparallelstatistics");
685  AC.OldParallelStats = AM.gOldParallelStats = AM.ggOldParallelStats = sp->value;
686  sp = GetSetupPar((UBYTE *)"oldfactarg");
687  AC.OldFactArgFlag = AM.gOldFactArgFlag = AM.ggOldFactArgFlag = sp->value;
688  sp = GetSetupPar((UBYTE *)"oldgcd");
689  AC.OldGCDflag = AM.gOldGCDflag = AM.ggOldGCDflag = sp->value;
690  sp = GetSetupPar((UBYTE *)"wtimestats");
691  if ( sp->value == 2 ) sp->value = AM.ggWTimeStatsFlag;
692  AC.WTimeStatsFlag = AM.gWTimeStatsFlag = AM.ggWTimeStatsFlag = sp->value;
693  sp = GetSetupPar((UBYTE *)"sorttype");
694  if ( StrICmp((UBYTE *)"lowfirst",(UBYTE *)sp->value) == 0 ) {
695  AC.lSortType = SORTLOWFIRST;
696  }
697  else if ( StrICmp((UBYTE *)"highfirst",(UBYTE *)sp->value) == 0 ) {
698  AC.lSortType = SORTHIGHFIRST;
699  }
700  else {
701  MesPrint(" Illegal SortType specification: %s",(UBYTE *)sp->value);
702  error = -2;
703  }
704 
705  sp = GetSetupPar((UBYTE *)"processbucketsize");
706  AM.hProcessBucketSize = AM.gProcessBucketSize =
707  AC.ProcessBucketSize = AC.mProcessBucketSize = sp->value;
708 /*
709  The store caches (code installed 15-aug-2006 JV)
710 */
711  sp = GetSetupPar((UBYTE *)"numstorecaches");
712  AM.NumStoreCaches = sp->value;
713  sp = GetSetupPar((UBYTE *)"sizestorecache");
714  AM.SizeStoreCache = sp->value;
715 #ifndef WITHPTHREADS
716 /*
717  Install the store caches (15-aug-2006 JV)
718  Note that in the case of PTHREADS this is done in InitializeOneThread
719 */
720  AT.StoreCache = AT.StoreCacheAlloc = 0;
721  if ( AM.NumStoreCaches > 0 ) {
722  STORECACHE sa, sb;
723  size = sizeof(struct StOrEcAcHe)+AM.SizeStoreCache;
724  size = ((size-1)/sizeof(size_t)+1)*sizeof(size_t);
725  AT.StoreCacheAlloc = (STORECACHE)Malloc1(size*AM.NumStoreCaches,"StoreCaches");
726  AT.StoreCache = AT.StoreCacheAlloc;
727  sa = AT.StoreCache;
728  for ( j = 0; j < AM.NumStoreCaches; j++ ) {
729  sb = (STORECACHE)(VOID *)((UBYTE *)sa+size);
730  if ( j == AM.NumStoreCaches-1 ) {
731  sa->next = 0;
732  }
733  else {
734  sa->next = sb;
735  }
736  SETBASEPOSITION(sa->position,-1);
737  SETBASEPOSITION(sa->toppos,-1);
738  sa = sb;
739  }
740  }
741 #endif
742 
743 /*
744  And now some order sensitive things
745 */
746  if ( AM.Path == 0 ) {
747  sp = GetSetupPar((UBYTE *)"path");
748  AM.Path = strDup1((UBYTE *)(sp->value),"path");
749  }
750  if ( AM.IncDir == 0 ) {
751  sp = GetSetupPar((UBYTE *)"incdir");
752  AM.IncDir = strDup1((UBYTE *)(sp->value),"incdir");
753  }
754 /*
755  if ( AM.TempDir == 0 ) {
756  sp = GetSetupPar((UBYTE *)"tempdir");
757  AM.TempDir = strDup1((UBYTE *)(sp->value),"tempdir");
758  }
759 */
760  return(error);
761 }
762 
763 /*
764  #] AllocSetups :
765  #[ WriteSetup :
766 
767  The routine writes the values of the setup parameters.
768  We should do this better. (JV, 21-may-2008)
769  The way it should be done is:
770  a: write the raw values.
771  b: give readjusted values.
772  c: give derived values.
773  Because this is a difficult subject, it would be nice to have a LaTeX
774  document that explains this all exactly. There should then be a
775  mechanism to poke the values of the setup into the LaTeX document.
776  probably the easiest way is to make a file with lots of \def definitions
777  and have that included into the LaTeX file.
778 */
779 
780 VOID WriteSetup()
781 {
782  int n = sizeof(setupparameters)/sizeof(SETUPPARAMETERS);
783  SETUPPARAMETERS *sp;
784  MesPrint(" The setup parameters are:");
785  for ( sp = setupparameters; n > 0; n--, sp++ ) {
786  switch(sp->type){
787  case NUMERICALVALUE:
788  MesPrint(" %s: %l",sp->parameter,sp->value);
789  break;
790  case PATHVALUE:
791  if ( StrICmp(sp->parameter,(UBYTE *)"path") == 0 && AM.Path ) {
792  MesPrint(" %s: '%s'",sp->parameter,(UBYTE *)(AM.Path));
793  break;
794  }
795  if ( StrICmp(sp->parameter,(UBYTE *)"incdir") == 0 && AM.IncDir ) {
796  MesPrint(" %s: '%s'",sp->parameter,(UBYTE *)(AM.IncDir));
797  break;
798  }
799  case STRINGVALUE:
800  if ( StrICmp(sp->parameter,(UBYTE *)"tempdir") == 0 && AM.TempDir ) {
801  MesPrint(" %s: '%s'",sp->parameter,(UBYTE *)(AM.TempDir));
802  }
803  else if ( StrICmp(sp->parameter,(UBYTE *)"tempsortdir") == 0 && AM.TempSortDir ) {
804  MesPrint(" %s: '%s'",sp->parameter,(UBYTE *)(AM.TempSortDir));
805  }
806  else {
807  MesPrint(" %s: '%s'",sp->parameter,(UBYTE *)(sp->value));
808  }
809  break;
810  case ONOFFVALUE:
811  if ( sp->value == 0 )
812  MesPrint(" %s: OFF",sp->parameter);
813  else if ( sp->value == 1 )
814  MesPrint(" %s: ON",sp->parameter);
815  break;
816  case DEFINEVALUE:
817 /*
818  MesPrint(" %s: '%s'",sp->parameter,(UBYTE *)(sp->value));
819 */
820  break;
821  }
822  }
823  AC.SetupFlag = 0;
824 }
825 
826 /*
827  #] WriteSetup :
828  #[ AllocSort :
829 
830  Routine allocates a complete struct for sorting.
831  To be used for the main allocation of the sort buffers, and
832  in a later stage for the function and subroutine sort buffers.
833 */
834 
835 SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsInSmall,
836  int MaxPatches, int MaxFpatches, LONG IOsize)
837 {
838  LONG allocation,longer,terms2insmall,sortsize,longerp;
839  LONG IObuffersize = IOsize;
840  LONG IOtry;
841  SORTING *sort;
842  int i = 0, j = 0;
843  char *s;
844  if ( AM.S0 != 0 ) {
845  s = FG.fname2; i = 0;
846  while ( *s ) { s++; i++; }
847  i += 16;
848  }
849  if ( MaxFpatches < 4 ) MaxFpatches = 4;
850  longer = MaxPatches > MaxFpatches ? MaxPatches : MaxFpatches;
851  longerp = longer;
852  while ( (1 << j) < longerp ) j++;
853  longerp = (1 << j) + 1;
854  longerp += sizeof(WORD*) - (longerp%sizeof(WORD *));
855  longer++;
856  longer += sizeof(WORD*) - (longer%sizeof(WORD *));
857  if ( SmallSize < 16*AM.MaxTer ) SmallSize = 16*AM.MaxTer+16;
858  TermsInSmall = (TermsInSmall+15) & (-16L);
859  terms2insmall = 2*TermsInSmall; /* Used to be just + 100 rather than *2 */
860  if ( SmallEsize < (SmallSize*3)/2 ) SmallEsize = (SmallSize*3)/2;
861  if ( LargeSize > 0 && LargeSize < 2*SmallSize ) LargeSize = 2*SmallSize;
862 /* if ( SmallEsize < 3*AM.MaxTer ) SmallEsize = 3*AM.MaxTer; */
863  SmallEsize = (SmallEsize+15) & (-16L);
864  if ( LargeSize < 0 ) LargeSize = 0;
865  sortsize = sizeof(SORTING);
866  sortsize = (sortsize+15)&(-16L);
867  IObuffersize = (IObuffersize+sizeof(WORD)-1)/sizeof(WORD);
868 /*
869  The next statement fixes a bug. In the rare case that we have a
870  problem here, we expand the size of the large buffer or the
871  small extension
872 */
873  if ( (ULONG)( LargeSize+SmallEsize ) < MaxFpatches*((IObuffersize
874  +COMPINC)*sizeof(WORD)+2*AM.MaxTer) ) {
875  if ( LargeSize == 0 )
876  SmallEsize = MaxFpatches*((IObuffersize+COMPINC)*sizeof(WORD)+2*AM.MaxTer);
877  else
878  LargeSize = MaxFpatches*((IObuffersize+COMPINC)*sizeof(WORD)+2*AM.MaxTer)
879  - SmallEsize;
880  }
881 
882  IOtry = ((LargeSize+SmallEsize)/MaxFpatches-2*AM.MaxTer)/sizeof(WORD)-COMPINC;
883 
884  if ( (LONG)(IObuffersize*sizeof(WORD)) < IOtry )
885  IObuffersize = (IOtry+sizeof(WORD)-1)/sizeof(WORD);
886 
887  allocation =
888  3*sizeof(POSITION)*(LONG)longer /* Filepositions!! */
889  +2*sizeof(WORD *)*longer
890  +2*(longerp*(sizeof(WORD *)+sizeof(WORD)))
891  +(3*longerp+2)*sizeof(WORD)
892 #ifdef WITHZLIB
893  +(2*longerp+4)*sizeof(WORD)
894 #endif
895  +terms2insmall*sizeof(WORD *)
896  +terms2insmall*sizeof(WORD *)/2
897  +LargeSize
898  +SmallEsize
899  +sortsize
900  +IObuffersize*sizeof(WORD) + i + 16;
901  sort = (SORTING *)Malloc1(allocation,"sort buffers");
902 
903  sort->LargeSize = LargeSize/sizeof(WORD);
904  sort->SmallSize = SmallSize/sizeof(WORD);
905  sort->SmallEsize = SmallEsize/sizeof(WORD);
906  sort->MaxPatches = MaxPatches;
907  sort->MaxFpatches = MaxFpatches;
908  sort->TermsInSmall = TermsInSmall;
909  sort->Terms2InSmall = terms2insmall;
910 
911  sort->sPointer = (WORD **)(sort+1);
912  sort->SplitScratch = sort->sPointer + terms2insmall;
913  sort->Patches = (WORD **)(sort->SplitScratch + terms2insmall/2);
914  sort->pStop = sort->Patches+longer;
915  sort->poina = sort->pStop+longer;
916  sort->poin2a = sort->poina + longerp;
917  sort->fPatches = (POSITION *)(sort->poin2a+longerp);
918  sort->fPatchesStop = sort->fPatches + longer;
919  sort->inPatches = sort->fPatchesStop + longer;
920  sort->tree = (WORD *)(sort->inPatches + longer);
921  sort->used = sort->tree+longerp;
922 #ifdef WITHZLIB
923  sort->fpcompressed = sort->used+longerp;
924  sort->fpincompressed = sort->fpcompressed+longerp+2;
925  sort->ktoi = sort->fpincompressed+longerp+2;
926  sort->zsparray = 0;
927 #else
928  sort->ktoi = sort->used + longerp;
929 #endif
930  sort->lBuffer = (WORD *)(sort->ktoi + longerp + 2);
931  sort->lTop = sort->lBuffer+sort->LargeSize;
932  sort->sBuffer = sort->lTop;
933  if ( sort->LargeSize == 0 ) { sort->lBuffer = 0; sort->lTop = 0; }
934  sort->sTop = sort->sBuffer + sort->SmallSize;
935  sort->sTop2 = sort->sBuffer + sort->SmallEsize;
936  sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1);
937  sort->file.PObuffer = (WORD *)(sort->sTop2);
938  sort->file.POstop = sort->file.PObuffer+IObuffersize;
939  sort->file.POsize = IObuffersize * sizeof(WORD);
940  sort->file.POfill = sort->file.POfull = sort->file.PObuffer;
941  sort->file.active = 0;
942  sort->file.handle = -1;
943  PUTZERO(sort->file.POposition);
944 #ifdef WITHPTHREADS
945  sort->file.pthreadslock = dummylock;
946 #endif
947 #ifdef WITHZLIB
948 /* sort->file.ziosize = IOsize; */
949  sort->file.ziosize = IObuffersize*sizeof(WORD);
950 #endif
951  if ( AM.S0 != 0 ) {
952  sort->file.name = (char *)(sort->file.PObuffer + IObuffersize);
953  AllocSortFileName(sort);
954  }
955  else sort->file.name = 0;
956  sort->cBuffer = 0;
957  sort->cBufferSize = 0;
958  sort->f = 0;
959 
960  return(sort);
961 }
962 
963 /*
964  #] AllocSort :
965  #[ AllocSortFileName :
966 */
967 
968 VOID AllocSortFileName(SORTING *sort)
969 {
970  GETIDENTITY
971  char *s, *t;
972 /*
973  This is not the allocation before the tempfiles are determined.
974  Hence we can use the name in FG.fname2 and modify the tail
975 */
976  s = FG.fname2; t = sort->file.name;
977  while ( *s ) *t++ = *s++;
978 #ifdef WITHPTHREADS
979  t[-2] = 'F';
980  sprintf(t-1,"%d.%d",identity,AN.filenum);
981 #else
982  t[-2] = 'f';
983  sprintf(t-1,"%d",AN.filenum);
984 #endif
985  AN.filenum++;
986 }
987 
988 /*
989  #] AllocSortFileName :
990  #[ AllocFileHandle :
991 */
992 
993 FILEHANDLE *AllocFileHandle(WORD par,char *name)
994 {
995  GETIDENTITY
996  LONG allocation, Ssize;
997  FILEHANDLE *fh;
998  int i = 0;
999  char *s, *t;
1000 
1001  s = FG.fname2; i = 0;
1002  while ( *s ) { s++; i++; }
1003  if ( par == 0 ) { i += 16; Ssize = AM.SIOsize; }
1004  else { s = name; while ( *s ) { i++; s++; } i+= 2; Ssize = AM.SpectatorSize; }
1005 
1006  allocation = sizeof(FILEHANDLE) + (Ssize+1)*sizeof(WORD) + i*sizeof(char);
1007  fh = (FILEHANDLE *)Malloc1(allocation,"FileHandle");
1008 
1009  fh->PObuffer = (WORD *)(fh+1);
1010  fh->POstop = fh->PObuffer+Ssize;
1011  fh->POsize = Ssize * sizeof(WORD);
1012  fh->active = 0;
1013  fh->handle = -1;
1014  PUTZERO(fh->POposition);
1015 #ifdef WITHPTHREADS
1016  fh->pthreadslock = dummylock;
1017 #endif
1018  if ( par == 0 ) { /* sort file */
1019  if ( AM.S0 != 0 ) {
1020  fh->name = (char *)(fh->POstop + 1);
1021  s = FG.fname2; t = fh->name;
1022  while ( *s ) *t++ = *s++;
1023 #ifdef WITHPTHREADS
1024  t[-2] = 'F';
1025  sprintf(t-1,"%d-%d",identity,AN.filenum);
1026 #else
1027  t[-2] = 'f';
1028  sprintf(t-1,"%d",AN.filenum);
1029 #endif
1030  AN.filenum++;
1031  }
1032  else fh->name = 0;
1033  }
1034  else { /* Spectator file */
1035  fh->name = (char *)(fh->POstop + 1);
1036  s = FG.fname; t = fh->name;
1037  for ( i = 0; i < FG.fnamebase; i++ ) *t++ = *s++;
1038  s = name;
1039  while ( *s ) *t++ = *s++;
1040  *t = 0;
1041  }
1042  fh->POfill = fh->POfull = fh->PObuffer;
1043  return(fh);
1044 }
1045 
1046 /*
1047  #] AllocFileHandle :
1048  #[ DeAllocFileHandle :
1049 
1050  Made to repair deallocation of AN.filenum. 21-sep-2000
1051 */
1052 
1053 void DeAllocFileHandle(FILEHANDLE *fh)
1054 {
1055  GETIDENTITY
1056  if ( fh->handle >= 0 ) {
1057  CloseFile(fh->handle);
1058  fh->handle = -1;
1059  remove(fh->name);
1060  }
1061  AN.filenum--; /* free namespace. was forgotten in first reading */
1062  M_free(fh,"Temporary FileHandle");
1063 }
1064 
1065 /*
1066  #] DeAllocFileHandle :
1067  #[ MakeSetupAllocs :
1068 */
1069 
1070 int MakeSetupAllocs()
1071 {
1072  if ( RecalcSetups() || AllocSetups() ) return(1);
1073  else return(0);
1074 }
1075 
1076 /*
1077  #] MakeSetupAllocs :
1078  #[ TryFileSetups :
1079 
1080  Routine looks in the input file for a start of the type
1081  [#-]
1082  #: setupparameter value
1083  It keeps looking until the first line that does not start with
1084  #-, #+ or #:
1085  Then it rewinds the input.
1086 */
1087 
1088 #define SETBUFSIZE 257
1089 
1090 int TryFileSetups()
1091 {
1092  LONG oldstreamposition;
1093  int oldstream;
1094  int error = 0, eqnum;
1095  int oldNoShowInput = AC.NoShowInput;
1096  UBYTE buff[SETBUFSIZE+1], *s, *t, *u, *settop, c;
1097  LONG linenum, prevline;
1098 
1099  if ( AC.CurrentStream == 0 ) return(error);
1100  oldstream = AC.CurrentStream - AC.Streams;
1101  oldstreamposition = GetStreamPosition(AC.CurrentStream);
1102  linenum = AC.CurrentStream->linenumber;
1103  prevline = AC.CurrentStream->prevline;
1104  eqnum = AC.CurrentStream->eqnum;
1105  AC.NoShowInput = 1;
1106  settop = buff + SETBUFSIZE;
1107  for(;;) {
1108  c = GetInput();
1109  if ( c == '*' || c == '\n' ) {
1110  while ( c != '\n' && c != ENDOFINPUT ) c = GetInput();
1111  if ( c == ENDOFINPUT ) goto eoi;
1112  continue;
1113  }
1114  if ( c == ENDOFINPUT ) goto eoi;
1115  if ( c != '#' ) break;
1116  c = GetInput();
1117  if ( c == ENDOFINPUT ) goto eoi;
1118  if ( c != '-' && c != '+' && c != ':' ) break;
1119  if ( c != ':' ) {
1120  while ( c != '\n' && c != ENDOFINPUT ) c = GetInput();
1121  continue;
1122  }
1123  s = buff;
1124  while ( ( c = GetInput() ) == ' ' || c == '\t' || c == '\r' ) {}
1125  if ( c == ENDOFINPUT ) break;
1126  if ( c == LINEFEED ) continue;
1127  if ( c == 0 || c == ENDOFINPUT ) break;
1128  while ( c != LINEFEED ) {
1129  *s++ = c;
1130  c = GetInput();
1131  if ( c != LINEFEED && c != '\r' ) continue;
1132  if ( s >= settop ) {
1133  while ( c != '\n' && c != ENDOFINPUT ) c = GetInput();
1134  MesPrint("Setups in .frm file: Line too long. setup ignored");
1135  error++; goto nextline;
1136  }
1137  }
1138  *s++ = '\n';
1139  t = s = buff; /* name of the option */
1140  while ( tolower(*s) >= 'a' && tolower(*s) <= 'z' ) s++;
1141  if ( *s != '\n' ) {
1142  *s++ = 0;
1143  while ( *s == ' ' || *s == '\t' ) s++;
1144  u = s; /* 'value' of the option */
1145  while ( *s && *s != '\n' && *s != '\r' ) s++;
1146  if ( *s ) *s++ = 0;
1147  }
1148  else {
1149  /* The value is empty. */
1150  u = s;
1151  *s++ = 0;
1152  }
1153  error += ProcessOption(t,u,1);
1154 nextline:;
1155  }
1156  AC.NoShowInput = oldNoShowInput;
1157  AC.CurrentStream = AC.Streams + oldstream;
1158  PositionStream(AC.CurrentStream,oldstreamposition);
1159  AC.CurrentStream->linenumber = linenum;
1160  AC.CurrentStream->prevline = prevline;
1161  AC.CurrentStream->eqnum = eqnum;
1162  ClearPushback();
1163  return(error);
1164 eoi:
1165  MesPrint("Input file without a program.");
1166  return(-1);
1167 }
1168 
1169 /*
1170  #] TryFileSetups :
1171  #[ TryEnvironment :
1172 */
1173 
1174 int TryEnvironment()
1175 {
1176  char *s, *t, *u, varname[100];
1177  int i,imax = sizeof(setupparameters)/sizeof(SETUPPARAMETERS);
1178  int error = 0;
1179  varname[0] = 'F'; varname[1] = 'O'; varname[2] = 'R'; varname[3] = 'M';
1180  varname[4] = '_'; varname[5] = 0;
1181  for ( i = 0; i < imax; i++ ) {
1182  t = s = (char *)(setupparameters[i].parameter);
1183  u = varname+5;
1184  while ( *s ) { *u++ = (char)(toupper((unsigned char)*s)); s++; }
1185  *u = 0;
1186  s = (char *)(getenv(varname));
1187  if ( s ) {
1188  error += ProcessOption((UBYTE *)t,(UBYTE *)s,2);
1189  }
1190  }
1191  return(error);
1192 }
1193 
1194 /*
1195  #] TryEnvironment :
1196  #] Setups :
1197 */
struct sOrT SORTING
Definition: structs.h:620
struct FiLe FILEHANDLE
Definition: structs.h:1069
struct NeStInG * NESTING
struct StOrEcAcHe * STORECACHE
int TheDefine(UBYTE *, int)
Definition: pre.c:1938
int handle
Definition: structs.h:648