CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / pkg / ace-5.2 / src / util2.c
Views: 418346
1
2
/**************************************************************************
3
4
util2.c
5
Colin Ramsay ([email protected])
6
6 Dec 00
7
8
ADVANCED COSET ENUMERATOR, Version 3.001
9
10
Copyright 2000
11
Centre for Discrete Mathematics and Computing,
12
Department of Mathematics and
13
Department of Computer Science & Electrical Engineering,
14
The University of Queensland, QLD 4072.
15
(http://staff.itee.uq.edu.au/havas)
16
17
These are the utilities for Level 2 of ACE.
18
19
**************************************************************************/
20
21
#include "al2.h"
22
23
#include <string.h>
24
#include <ctype.h>
25
#include <sys/types.h>
26
#include <time.h>
27
28
/******************************************************************
29
void al2_init(void)
30
31
One-off initialisation of the Level 2 stuff, and all lower levels.
32
Note that there is no need to initialise, for example, intarr[].
33
******************************************************************/
34
35
void al2_init(void)
36
{
37
al1_init();
38
39
okstart = okcont = okredo = FALSE;
40
tabinfo = tabindex = FALSE;
41
lresult = -8192; /* invalid mode ! */
42
43
echo = FALSE;
44
skipnl = TRUE;
45
46
currip = currkey[0] = currname[0] = '\0';
47
48
currword = NULL;
49
currsiz = currexp = 0;
50
51
intcnt = 0;
52
53
srand((unsigned int)time(NULL)); /* Seed rand() with time */
54
}
55
56
/******************************************************************
57
char *al2_strdup(char *s)
58
59
strdup() is not ANSI C, so this is our version. Should we regard
60
an error as fatal, and abort?
61
******************************************************************/
62
63
char *al2_strdup(char *s)
64
{
65
char *t;
66
67
if ((t = malloc(strlen(s)+1)) == NULL)
68
{ al2_continue("out of memory in al2_strdup()"); }
69
70
return(strcpy(t,s));
71
}
72
73
/******************************************************************
74
int al2_outlen(int i)
75
76
Returns the print-length of an integer i (i.e., ~ $\log_{10}i$).
77
The int i is assumed to satisfy i >= 0.
78
******************************************************************/
79
80
int al2_outlen(int i)
81
{
82
int len = 1;
83
84
while ((i /= 10) != 0)
85
{ len++; }
86
87
return(len);
88
}
89
90
/**************************************************************************
91
All Level 2 errors are filtered by one of the following three handlers.
92
These take the appropriate action, and then jump back to the top-level
93
main() routine; ie, the outermost level of Level 2. We swallow the
94
remainder of any input line (possibly losing some commands); so multi-line
95
commands in error may not be properly tidied-up. The question of what
96
exactly to do if fip/fop are not stdin/stdout is put in the `too hard'
97
basket; we simply switch them both back to their defaults. Note that,
98
although the code for _continue() & _restart() is the same, they return to
99
different points (& do different things) in main().
100
101
Warning: The error-handling is fairly basic, since it's not our intent to
102
develop a fully-fledged interactive interface. We simply tidy-up the best
103
we can and carry on.
104
**************************************************************************/
105
106
/******************************************************************
107
void al2_continue(char *msg)
108
109
An error has occurred, but it doesn't affected the ok... flags, or
110
the table's validity.
111
******************************************************************/
112
113
void al2_continue(char *msg)
114
{
115
if (fop != stdout)
116
{
117
if (fop != NULL)
118
{ fclose(fop); }
119
fop = stdout;
120
}
121
if (fip != stdin)
122
{
123
if (fip != NULL)
124
{ fclose(fip); }
125
fip = stdin;
126
currip = '\0';
127
}
128
129
fflush(fop);
130
fprintf(fop, "** ERROR (continuing with next line)\n");
131
fprintf(fop, " %s\n", msg);
132
133
while ( !(currip == '\n' || currip == '\r' || currip == EOF) )
134
{ al2_nextip(); }
135
136
longjmp(env,1);
137
}
138
139
/******************************************************************
140
void al2_restart(char *msg)
141
142
Something nasty has happened & we'll be disallowing continue/redo.
143
******************************************************************/
144
145
void al2_restart(char *msg)
146
{
147
if (fop != stdout)
148
{
149
if (fop != NULL)
150
{ fclose(fop); }
151
fop = stdout;
152
}
153
if (fip != stdin)
154
{
155
if (fip != NULL)
156
{ fclose(fip); }
157
fip = stdin;
158
currip = '\0';
159
}
160
161
fflush(fop);
162
fprintf(fop, "** ERROR (restarting with next line)\n");
163
fprintf(fop, " %s\n", msg);
164
165
while ( !(currip == '\n' || currip == '\r' || currip == EOF) )
166
{ al2_nextip(); }
167
168
longjmp(env,2);
169
}
170
171
/******************************************************************
172
void al2_abort(char *msg)
173
174
No point in being clever here, we're going to stop.
175
******************************************************************/
176
177
void al2_abort(char *msg)
178
{
179
if (fop != stdout)
180
{
181
if (fop != NULL)
182
{ fclose(fop); }
183
fop = stdout;
184
}
185
if (fip != stdin)
186
{
187
if (fip != NULL)
188
{ fclose(fip); }
189
fip = stdin;
190
currip = '\0';
191
}
192
193
fflush(fop);
194
fprintf(fop, "** ERROR (aborting)\n");
195
fprintf(fop, " %s\n", msg);
196
197
longjmp(env,3);
198
}
199
200
/******************************************************************
201
void al2_aip(char *name)
202
203
Switch to a new input file. We abort via _restart() if this is not
204
possible, and that call will reset fip/fop properly.
205
******************************************************************/
206
207
void al2_aip(char *name)
208
{
209
/* Close the current input file (unless it is 'stdin'). */
210
211
if (fip != stdin && fip != NULL)
212
{ fclose(fip); }
213
fip = NULL;
214
215
/* Try to open the new input file (unless it is 'stdin'). */
216
217
if (strcmp(name, "stdin") != 0)
218
{
219
if ((fip = fopen(name, "r")) == NULL)
220
{ al2_restart("can't open new input, using 'stdin'"); }
221
}
222
else
223
{ fip = stdin; }
224
225
currip = '\0'; /* Initialise current i/p char. */
226
}
227
228
/******************************************************************
229
void al2_aop(char *name)
230
231
Switch to a new output file. We abort via _restart() if this is
232
not possible, and that call will reset fip/fop properly. Note
233
that there is no need to run setvbuf() on stdout, since this was
234
done in the call to al0_init().
235
******************************************************************/
236
237
void al2_aop(char *name)
238
{
239
/* Close the current output file (unless it is 'stdout'). */
240
241
if (fop != stdout && fop != NULL)
242
{ fclose(fop); }
243
fop = NULL;
244
245
/* Try to open the new output file (unless it is 'stdout'). */
246
247
if (strcmp(name, "stdout") != 0)
248
{
249
if ((fop = fopen(name, "w")) == NULL)
250
{ fprintf(fop, "can't open new output, using 'stdout'"); }
251
else
252
{ setvbuf(fop, NULL, _IOLBF, 0); } /* line buffered o/p */
253
}
254
else
255
{ fop = stdout; }
256
}
257
258
/******************************************************************
259
void al2_dump(Logic allofit)
260
261
Dump out the internals of Level 2 of ACE, working through al2.h
262
more or less in order.
263
******************************************************************/
264
265
void al2_dump(Logic allofit)
266
{
267
int i;
268
269
fprintf(fop, " #---- %s: Level 2 Dump ----\n", ACE_VER);
270
271
/* env; - nothing (meaningful) we can do here! */
272
273
/* okstart, okcont, okredo; */
274
fprintf(fop, "okstart=%d okcont=%d okredo=%d\n",
275
okstart, okcont, okredo);
276
277
/* tabinfo, tabindex, lresult */
278
fprintf(fop, "tabinfo=%d tabindex=%d lresult=%d\n",
279
tabinfo, tabindex, lresult);
280
281
/* echo, skipnl, currip, currkey, currname; */
282
fprintf(fop, "echo=%d skipnl=%d currip=%d", echo, skipnl, currip);
283
if (isprint(currip))
284
{ fprintf(fop, "(%c)\n", currip); }
285
else
286
{ fprintf(fop, "\n"); }
287
fprintf(fop, "currkey=%s\n", currkey);
288
fprintf(fop, "currname=%s\n", currname);
289
290
/* *currword, currsiz, currexp; */
291
fprintf(fop, "currsize=%d currexp=%d currword=", currsiz, currexp);
292
if (currword == NULL)
293
{ fprintf(fop, "NULL"); }
294
else
295
{
296
if (allofit)
297
{
298
for (i = 0; i < currsiz; i++)
299
{ fprintf(fop, "%d ", currword[i]); }
300
}
301
else
302
{ fprintf(fop, "non-NULL"); }
303
}
304
fprintf(fop, "\n");
305
306
/* intcnt, intarr[32]; */
307
if (intcnt == 0)
308
{ fprintf(fop, "intcnt=0 (empty)\n"); }
309
else
310
{
311
fprintf(fop, "intcnt=%d intarr[]=", intcnt);
312
for (i = 0; i < intcnt; i++)
313
{ fprintf(fop, "%d ", intarr[i]); }
314
fprintf(fop, "\n");
315
}
316
317
fprintf(fop, " #---------------------------------\n");
318
}
319
320
/******************************************************************
321
void al2_opt(void)
322
323
Pretty-print the date of compilation and all the various options
324
included in this build.
325
******************************************************************/
326
327
void al2_opt(void)
328
{
329
#ifdef DATE
330
fprintf(fop, "%s executable built:\n %s\n", ACE_VER, DATE);
331
#else
332
fprintf(fop, "%s executable built: ??\n", ACE_VER);
333
#endif
334
335
fprintf(fop, "Level 0 options:\n");
336
#ifdef AL0_STAT
337
fprintf(fop, " statistics package = on\n");
338
#else
339
fprintf(fop, " statistics package = off\n");
340
#endif
341
#ifdef AL0_CC
342
fprintf(fop, " coinc processing messages = on\n");
343
#else
344
fprintf(fop, " coinc processing messages = off\n");
345
#endif
346
#ifdef AL0_DD
347
fprintf(fop, " dedn processing messages = on\n");
348
#else
349
fprintf(fop, " dedn processing messages = off\n");
350
#endif
351
352
fprintf(fop, "Level 1 options:\n");
353
#ifdef AL1_BINARY
354
fprintf(fop, " workspace multipliers = binary\n");
355
#else
356
fprintf(fop, " workspace multipliers = decimal\n");
357
#endif
358
359
fprintf(fop, "Level 2 options:\n");
360
#ifdef AL2_HINFO
361
fprintf(fop, " host info = on\n");
362
#else
363
fprintf(fop, " host info = off\n");
364
#endif
365
}
366
367
/******************************************************************
368
void al2_help(void)
369
******************************************************************/
370
371
void al2_help(void)
372
{
373
fprintf(fop, " #---- %s: Level 2 Help ----\n", ACE_VER);
374
fprintf(fop, "add gen[erators] / sg : <word list> ;\n");
375
fprintf(fop, "add rel[ators] / rl : <relation list> ;\n");
376
fprintf(fop, "aep : 1..7 ;\n");
377
fprintf(fop, "ai / alter i[nput] : [<filename>] ;\n");
378
fprintf(fop, "ao / alter o[utput] : [<filename>] ;\n");
379
fprintf(fop, "as[is] : [0/1] ;\n");
380
fprintf(fop, "beg[in] / end / start ;\n");
381
fprintf(fop, "bye / exit / q[uit] ;\n");
382
fprintf(fop, "cc / coset coinc[idence] : int ;\n");
383
fprintf(fop, "c[factor] / ct[ factor] : [int] ;\n");
384
fprintf(fop, "check / redo ;\n");
385
fprintf(fop, "com[paction] : [0..100] ;\n");
386
fprintf(fop, "cont[inue] ;\n");
387
fprintf(fop, "cy[cles] ;\n");
388
fprintf(fop, "ded mo[de] / dmod[e] : [0..4] ;\n");
389
fprintf(fop, "ded si[ze] / dsiz[e] : [0/1..] ;\n");
390
fprintf(fop, "def[ault] ;\n");
391
fprintf(fop, "del gen[erators] / ds : <int list> ;\n");
392
fprintf(fop, "del rel[ators] / dr : <int list> ;\n");
393
fprintf(fop, "d[ump] : [0/1/2[,0/1]] ;\n");
394
fprintf(fop, "easy ;\n");
395
fprintf(fop, "echo : [0/1] ;\n");
396
fprintf(fop, "enum[eration] / group name : <string> ;\n");
397
fprintf(fop, "fel[sch] : [0/1] ;\n");
398
fprintf(fop, "f[factor] / fi[ll factor] : [0/1..] ;\n");
399
fprintf(fop, "gen[erators] / subgroup gen[erators] : <word list> ;\n");
400
fprintf(fop, "gr[oup generators]: [<letter list> / int] ;\n");
401
fprintf(fop, "group relators / rel[ators] : <relation list> ;\n");
402
fprintf(fop, "hard ;\n");
403
fprintf(fop, "h[elp] ;\n");
404
fprintf(fop, "hlt ;\n");
405
fprintf(fop, "ho[le limit] : [-1/0..100] ;\n");
406
fprintf(fop, "look[ahead] : [0/1..4] ;\n");
407
fprintf(fop, "loop[ limit] : [0/1..] ;\n");
408
fprintf(fop, "max[ cosets] : [0/2..] ;\n");
409
fprintf(fop, "mend[elsohn] : [0/1] ;\n");
410
fprintf(fop, "mess[ages] / mon[itor] : [int] ;\n");
411
fprintf(fop, "mo[de] ;\n");
412
fprintf(fop, "nc / normal[ closure] : [0/1] ;\n");
413
fprintf(fop, "no[ relators in subgroup] : [-1/0/1..] ;\n");
414
fprintf(fop, "oo / order[ option] : int ;\n");
415
fprintf(fop, "opt[ions] ;\n");
416
fprintf(fop, "par[ameters] ; - old option (ignored)\n");
417
fprintf(fop, "path[ compression] : [0/1] ;\n");
418
fprintf(fop, "pd mo[de] / pmod[e] : [0/1..3] ;\n");
419
fprintf(fop, "pd si[ze] / psiz[e] : [0/2/4/8/...] ;\n");
420
fprintf(fop, "print det[ails] / sr : [int] ;\n");
421
fprintf(fop, "pr[int table] : [[-]int[,int[,int]]] ;\n");
422
fprintf(fop, "pure c[t] ;\n");
423
fprintf(fop, "pure r[t] ;\n");
424
fprintf(fop, "rc / random coinc[idences]: int[,int] ;\n");
425
fprintf(fop, "rec[over] / contig[uous] ;\n");
426
fprintf(fop, "rep : 1..7[,int] ;\n");
427
fprintf(fop, "restart ; - old option (ignored)\n");
428
fprintf(fop, "r[factor] / rt[ factor] : [int] ;\n");
429
fprintf(fop, "row[ filling] : [0/1] ;\n");
430
fprintf(fop, "sc / stabil[ising cosets] : int ;\n");
431
fprintf(fop, "sims : 1/3/5/7/9 ;\n");
432
fprintf(fop, "st[andard table] ;\n");
433
#ifdef AL0_STAT
434
fprintf(fop, "stat[istics] / stats ;\n");
435
#endif
436
fprintf(fop, "style ;\n");
437
fprintf(fop, "subg[roup name] : <string> ;\n");
438
fprintf(fop, "sys[tem] : <string> ;\n");
439
fprintf(fop, "text : <string> ;\n");
440
fprintf(fop, "ti[me limit] : [-1/0/1..] ;\n");
441
fprintf(fop, "tw / trace[ word] : int,<word> ;\n");
442
fprintf(fop, "wo[rkspace] : [int[k/m/g]] ;\n");
443
fprintf(fop, "# ... <newline> - a comment (ignored)\n");
444
fprintf(fop, " #---------------------------------\n");
445
}
446
447
/******************************************************************
448
void al2_nextip(void)
449
450
Primes currip with the next character from fip, if we're not at the
451
end-of-file. Echoes the character if echo is on.
452
******************************************************************/
453
454
void al2_nextip(void)
455
{
456
if (currip != EOF)
457
{
458
currip = fgetc(fip);
459
460
if (echo && currip != EOF)
461
{ fputc(currip, fop); }
462
}
463
}
464
465
/******************************************************************
466
void al2_skipws(void)
467
468
Skip all whitespace characters.
469
******************************************************************/
470
471
void al2_skipws(void)
472
{
473
Logic comment = (currip == '#');
474
475
while ( currip == ' ' || currip == '\t' || comment ||
476
(skipnl && (currip == '\n' || currip == '\r')) )
477
{
478
al2_nextip();
479
comment = (currip == '#' ||
480
(comment && currip != '\n' && currip != '\r' && currip != EOF));
481
}
482
}
483
484
/******************************************************************
485
void al2_nextnw(void)
486
487
Skip to the next non-whitespace character.
488
******************************************************************/
489
490
void al2_nextnw(void)
491
{
492
al2_nextip();
493
al2_skipws();
494
}
495
496
497
498