Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2008, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical Algorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
/* ex2ht creates a cover page for structured HyperDoc example pages */
37
38
39
#include "openaxiom-c-macros.h"
40
41
#include "debug.h"
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <unistd.h>
45
#include <string.h>
46
#include <sys/types.h>
47
#include <sys/stat.h>
48
#include <sys/time.h>
49
#include <locale.h>
50
#include "cfuns.h"
51
52
53
using namespace OpenAxiom;
54
55
#define MaxLineLength 512
56
#define MaxFiles 100
57
58
static char *files[MaxFiles];
59
static int numFiles = 0;
60
static struct timeval latest_date[2] ={{0,0},{0,0}};
61
62
static FILE *coverFile;
63
64
65
static void
66
addFile(const char* filename)
67
{
68
FILE *file = fopen(filename, "r");
69
int c;
70
71
if (file == NULL) {
72
fprintf(stderr, "Couln't open %s for reading\n", filename);
73
exit(-1);
74
}
75
while ((c = getc(file)) != EOF)
76
putc(c, coverFile);
77
putc('\n', coverFile);
78
fclose(file);
79
oa_unlink(filename);
80
}
81
82
83
static void
84
emitCoverLink(const char* name, char *title)
85
{
86
fprintf(coverFile, "{\\downlink{%s}{%s}}\n", title, name);
87
}
88
89
static void
90
closeCoverFile()
91
{
92
fclose(coverFile);
93
#ifndef __WIN32__ /* FIXME! */
94
utimes("coverex.ht",latest_date);
95
#endif
96
}
97
98
static void
99
closeCoverPage()
100
{
101
fprintf(coverFile, "}\\endscroll\\end{page}\n\n");
102
}
103
/* cover page functions */
104
105
static void
106
openCoverPage()
107
{
108
coverFile = fopen("coverex.ht", "w");
109
if (coverFile == NULL) {
110
fprintf(stderr, "couldn't open coverex.ht for writing\n");
111
exit(-1);
112
}
113
fprintf(coverFile, "%% DO NOT EDIT! Created by ex2ht.\n\n");
114
fprintf(coverFile, "\\begin{page}{ExampleCoverPage}{Examples Of AXIOM Commands}\n");
115
fprintf(coverFile, "\\beginscroll\\table{\n");
116
}
117
118
static void
119
emitSpadCommand(const char* line, const char* prefix, FILE *outFile)
120
{
121
int braceCount = 1;
122
char command[MaxLineLength], *t = command;
123
124
while (1) {
125
if (*line == '}')
126
braceCount--;
127
if (braceCount == 0)
128
break;
129
if (*line == '{')
130
braceCount++;
131
*t++ = *line++;
132
}
133
*t = '\0';
134
fprintf(outFile, "%s%s}\n", prefix, command);
135
}
136
137
/* s is pageName}{title} */
138
static void
139
emitMenuEntry(const char* line, FILE *outFile)
140
{
141
char pageName[MaxLineLength], title[MaxLineLength];
142
char *p = pageName, *t = title;
143
144
while (*line != '}')
145
*p++ = *line++;
146
*p = '\0';
147
line++;
148
while (*line != '}')
149
*t++ = *line++;
150
*t = '\0';
151
fprintf(outFile, "\\menudownlink%s}{%s}\n", title, pageName);
152
}
153
154
static void
155
emitHeader(FILE *outFile, char *pageName, char *pageTitle)
156
{
157
fprintf(outFile, "\\begin{page}{%s}{%s}\n", pageName, pageTitle);
158
fprintf(outFile, "\\beginscroll\\beginmenu\n");
159
}
160
161
static void
162
emitFooter(FILE *outFile)
163
{
164
fprintf(outFile, "\\endmenu\\endscroll\\end{page}\n");
165
}
166
167
168
static char *
169
strPrefix(const char* prefix, char *s)
170
{
171
while (*prefix != '\0' && *prefix == *s) {
172
prefix++;
173
s++;
174
}
175
if (*prefix == '\0')
176
return s;
177
return NULL;
178
}
179
180
181
static char *
182
allocString(const char* s)
183
{
184
char *t = (char *) malloc(strlen(s) + 1);
185
186
strcpy(t, s);
187
return t;
188
}
189
190
191
static char *
192
getExTitle(FILE *inFile, char* line)
193
{
194
char *title;
195
196
while (fgets(line, MaxLineLength, inFile) != NULL)
197
if ((title = strPrefix("% Title: ", line))) {
198
title[strlen(title) - 1] = '\0';
199
return title;
200
}
201
fprintf(stderr, "No Title title line in the file!\n");
202
return NULL;
203
}
204
205
206
static void
207
exToHt(const char* filename)
208
{
209
char line[MaxLineLength];
210
const char* line2;
211
char *title, *pagename;
212
FILE *inFile = fopen(filename, "r");
213
FILE *outFile;
214
int len, i;
215
struct timeval tvp;
216
struct stat buf;
217
218
if (inFile == NULL) {
219
fprintf(stderr, "couldn't open %s for reading.\n", filename);
220
return;
221
}
222
strcpy(line, "Menu");
223
strcat(line, filename);
224
len = strlen(line);
225
for (i = 0; i < len; i++)
226
if (line[i] == '.') {
227
line[i] = '\0';
228
break;
229
}
230
outFile = fopen(line, "w");
231
if (outFile == NULL) {
232
fprintf(stderr, "couldn't open %s for writing.\n", line);
233
return;
234
}
235
pagename = allocString(line);
236
title = getExTitle(inFile, line);
237
if (title == NULL) {
238
return;
239
}
240
files[numFiles++] = pagename;
241
emitCoverLink(pagename, title);
242
emitHeader(outFile, pagename, title);
243
while (fgets(line, MaxLineLength, inFile) != NULL) {
244
if ((line2 = strPrefix("\\begin{page}{", line)))
245
emitMenuEntry(line2, outFile);
246
else if ((line2 = strPrefix("\\spadcommand{", line)))
247
emitSpadCommand(line2, "\\spadcommand{", outFile);
248
else if ((line2 = strPrefix("\\spadpaste{", line)))
249
emitSpadCommand(line2, "\\spadpaste{", outFile);
250
else if ((line2 = strPrefix("\\example{", line)))
251
emitSpadCommand(line2, "\\example{", outFile);
252
else if ((line2 = strPrefix("\\graphpaste{", line)))
253
emitSpadCommand(line2, "\\graphpaste{", outFile);
254
}
255
emitFooter(outFile);
256
fclose(inFile);
257
fclose(outFile);
258
stat(filename,&buf);
259
tvp.tv_sec =buf.st_mtime;
260
tvp.tv_usec =0;
261
if timercmp(&tvp,&latest_date[1],>){
262
latest_date[1].tv_sec=buf.st_mtime;
263
}
264
}
265
266
267
int
268
main(int argc, char **argv)
269
{
270
using namespace OpenAxiom;
271
int i;
272
273
oa_setenv("LC_ALL", "C");
274
setlocale(LC_ALL, "");
275
if (argc == 1) {
276
fprintf(stderr, "usage: %s exfile.ht ...\n", argv[0]);
277
return (-1);
278
}
279
openCoverPage();
280
for (i = 1; i < argc; i++)
281
exToHt(argv[i]);
282
closeCoverPage();
283
for (i = 0; i < numFiles; i++)
284
addFile(files[i]);
285
closeCoverFile();
286
return 0;
287
}
288
289