1 | /* ------------------------------------------------------------------- */
|
---|
2 | /* D2Mcgi.c : CGI program for DC -> MARC conversion */
|
---|
3 | /* Author : Ole Husby */
|
---|
4 | /* Updated : 1998-09-30 */
|
---|
5 | /* ------------------------------------------------------------------- */
|
---|
6 |
|
---|
7 | /* -------------------------------------------------------------------
|
---|
8 | D2MCgi() will take metadata input from a parameter, or fetch an
|
---|
9 | HTML file using HTTP.
|
---|
10 |
|
---|
11 | DC metadata will be converted into MARC, and presented in one of
|
---|
12 | several formats.
|
---|
13 |
|
---|
14 | The program will look for the following QUERY_STRING:
|
---|
15 |
|
---|
16 | url=xxx&fmt=xxx&prs=xxx&txt=xxx&par=xxx
|
---|
17 |
|
---|
18 | where:
|
---|
19 |
|
---|
20 | url
|
---|
21 | xxx = the URL for the file containing the metadata
|
---|
22 | fmt
|
---|
23 | The MARC format. xxx =
|
---|
24 | DANMARC | FINMARC | ISMARC | NORMARC (default) | SWEMARC | USMARC.
|
---|
25 | prs
|
---|
26 | The presentation format. xxx =
|
---|
27 | plain (default) | html | 2709 | binary.
|
---|
28 |
|
---|
29 | txt
|
---|
30 | If present, this field will be parsed for metadata, and the only
|
---|
31 | use of the URL (if entered) will be used to produce the 856 field
|
---|
32 | of the MARC record.
|
---|
33 |
|
---|
34 | par
|
---|
35 | If xxx = on, the program will additionally display the tags as
|
---|
36 | they are recognized. Will only work together with the html format.
|
---|
37 | ------------------------------------------------------------------- */
|
---|
38 |
|
---|
39 |
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <string.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <malloc.h>
|
---|
44 | #include <ctype.h>
|
---|
45 | #include <unistd.h>
|
---|
46 | #include "d2m.h"
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 | /* ------------------------------------------------------------------- */
|
---|
52 | /* main() program */
|
---|
53 | /* ------------------------------------------------------------------- */
|
---|
54 |
|
---|
55 | int main ()
|
---|
56 | {
|
---|
57 | int rc, i, source, textarea, f2709, tracing, mformat, marcfile;
|
---|
58 | int linel, llen, slen;
|
---|
59 | int *meta;
|
---|
60 |
|
---|
61 | FILE *df, *of;
|
---|
62 | char contype[64], dfilename[64], tfilename[64], line[50000];
|
---|
63 | char *pq, *p, *q, pre[16], *l, *u;
|
---|
64 | char marcformat[256], url[1000], method[32];
|
---|
65 | char *buffer[50000];
|
---|
66 | char syntax[64];
|
---|
67 |
|
---|
68 | char recstatus = 'n';
|
---|
69 | char rectype = 'm';
|
---|
70 | char biblevel = ' ';
|
---|
71 |
|
---|
72 | /* Initializing */
|
---|
73 |
|
---|
74 | of = stdout;
|
---|
75 | *url = 0;
|
---|
76 | marcfile = 0;
|
---|
77 |
|
---|
78 | strcpy(marcformat, D2M_DEFAULT_FORMAT);
|
---|
79 | strcpy(syntax, D2M_DEFAULT_SYNTAX);
|
---|
80 | sprintf(dfilename, "%s.%d", D2M_TEMPFILE, getpid());
|
---|
81 |
|
---|
82 | textarea = tracing = f2709 = 0;
|
---|
83 | *line = 0;
|
---|
84 |
|
---|
85 |
|
---|
86 | /* Decide parameters for line format formating */
|
---|
87 |
|
---|
88 | linel = D2M_LINELEN;
|
---|
89 | if (!linel)
|
---|
90 | linel = 80;
|
---|
91 |
|
---|
92 | if (linel > 200)
|
---|
93 | linel = 200;
|
---|
94 |
|
---|
95 | llen = linel - 9;
|
---|
96 | slen = linel - 30;
|
---|
97 | if (slen < 10)
|
---|
98 | slen = 10;
|
---|
99 |
|
---|
100 |
|
---|
101 | /* Check request method (POST or GET) */
|
---|
102 |
|
---|
103 | strcpy(method, getenv("REQUEST_METHOD"));
|
---|
104 |
|
---|
105 | if (strcmp(method, "GET") == 0)
|
---|
106 | {
|
---|
107 | strcpy(line, getenv("QUERY_STRING"));
|
---|
108 | pq = (char *) strdup(line);
|
---|
109 | }
|
---|
110 |
|
---|
111 | else if (strcmp(method, "POST") == 0)
|
---|
112 | pq = poq();
|
---|
113 |
|
---|
114 | else
|
---|
115 | pq = (char *) NULL; /* Error exit is missing here ! */
|
---|
116 |
|
---|
117 |
|
---|
118 | /* Parse parameters */
|
---|
119 |
|
---|
120 | if (pq)
|
---|
121 | {
|
---|
122 | strcpy(line, pq);
|
---|
123 | free(pq);
|
---|
124 |
|
---|
125 | l = (char *) line;
|
---|
126 |
|
---|
127 | u = strtok(l, "&");
|
---|
128 | while (u)
|
---|
129 | {
|
---|
130 | unescape(u);
|
---|
131 | if (strncasecmp(u, "url=", 4) == 0)
|
---|
132 | strcpy(url, u + 4);
|
---|
133 | else if (strncasecmp(u, "fmt=", 4) == 0)
|
---|
134 | strcpy(marcformat, u + 4);
|
---|
135 | else if (strncasecmp(u, "prs=", 4) == 0)
|
---|
136 | strcpy(syntax, u + 4);
|
---|
137 | else if (strncasecmp(u, "par=", 4) == 0)
|
---|
138 | {
|
---|
139 | if (strcmp(u + 4, "on") == 0)
|
---|
140 | tracing = 1;
|
---|
141 | }
|
---|
142 | else if (strncasecmp(u, "txt=", 4) == 0)
|
---|
143 | {
|
---|
144 | if (strlen(u + 4))
|
---|
145 | {
|
---|
146 | if ( df = fopen(dfilename, "w"))
|
---|
147 | {
|
---|
148 | fprintf(df, "%s", u + 4);
|
---|
149 | textarea = 1;
|
---|
150 | fclose(df);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | u = strtok(NULL, "&");
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 | /* Check the syntax */
|
---|
161 |
|
---|
162 | if (strcmp(syntax, "2709") == 0)
|
---|
163 | {
|
---|
164 | strcpy(contype, "text/plain");
|
---|
165 | tracing = 0;
|
---|
166 | f2709 = 1;
|
---|
167 | }
|
---|
168 |
|
---|
169 | else if (strcmp(syntax, "binary") == 0)
|
---|
170 | {
|
---|
171 | strcpy(contype, "application/marc");
|
---|
172 | tracing = 0;
|
---|
173 | f2709 = 1;
|
---|
174 | }
|
---|
175 |
|
---|
176 | else if (strcmp(syntax, "html") == 0)
|
---|
177 | {
|
---|
178 | strcpy(contype, "text/html");
|
---|
179 | }
|
---|
180 |
|
---|
181 | else if (strcmp(syntax, "plain") == 0)
|
---|
182 | {
|
---|
183 | strcpy(contype, "text/plain");
|
---|
184 | tracing = 0;
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /* Write HTTP header and HTML <HEAD>: */
|
---|
189 |
|
---|
190 | printf("Content-type: %s\n\n", contype);
|
---|
191 | if (strcmp(syntax, "html") == 0)
|
---|
192 | {
|
---|
193 | printf("<html><head><title>\n");
|
---|
194 | printf("%s\n", HTML_TITLE);
|
---|
195 | printf("</title></head>\n\n<body bgcolor=\"#ffffbb\">\n");
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 | /* Check if metadata in input (source = TEXTAREA) or in URL */
|
---|
201 |
|
---|
202 | if (!textarea && !*url)
|
---|
203 | {
|
---|
204 | printf("1: ");
|
---|
205 | printf("Error: Insufficient input\n");
|
---|
206 | exit(0);
|
---|
207 | }
|
---|
208 |
|
---|
209 | else if (textarea)
|
---|
210 | source = TEXTAREA;
|
---|
211 |
|
---|
212 | else
|
---|
213 | source = URL;
|
---|
214 |
|
---|
215 | if (!textarea && !*url)
|
---|
216 | {
|
---|
217 | printf("2: ");
|
---|
218 | printf("Error: Insufficient input\n");
|
---|
219 | exit(0);
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /* Check the marcformat */
|
---|
224 |
|
---|
225 | if (strncasecmp(marcformat, "DAN", 3) == 0)
|
---|
226 | mformat = DANMARC;
|
---|
227 | else if (strncasecmp(marcformat, "FIN", 3) == 0)
|
---|
228 | mformat = FINMARC;
|
---|
229 | else if (strncasecmp(marcformat, "IS", 2) == 0)
|
---|
230 | mformat = ISMARC;
|
---|
231 | else if (strncasecmp(marcformat, "US", 2) == 0)
|
---|
232 | mformat = USMARC;
|
---|
233 | else if (strncasecmp(marcformat, "NOR", 3) == 0)
|
---|
234 | mformat = NORMARC;
|
---|
235 | else if (strncasecmp(marcformat, "SWE", 3) == 0)
|
---|
236 | mformat = SWEMARC;
|
---|
237 | else
|
---|
238 | {
|
---|
239 | printf("Error: This MARC format is not supported!\n");
|
---|
240 | exit(0);
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | /* Fetch URL if necessary */
|
---|
245 |
|
---|
246 | if (source == URL)
|
---|
247 | {
|
---|
248 | if (strcmp(url, "test") == 0)
|
---|
249 | strcpy(url, D2M_DEFAULT_URL);
|
---|
250 | rc = wfetch(&url, &dfilename);
|
---|
251 | if (!rc)
|
---|
252 | {
|
---|
253 | printf("Error %d: Unable to fetch file %s\n", rc, url);
|
---|
254 | exit(0);
|
---|
255 | }
|
---|
256 | if (rc == 2)
|
---|
257 | marcfile = 1;
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (*url && !textarea && ( strcmp(syntax, "html") == 0 ) )
|
---|
261 | {
|
---|
262 | printf("<h2><a href=\"%s\">%s</a></h2>\n", url, url);
|
---|
263 | printf("<h3>%s record:</h3><hr>\n", marcformat);
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (strcmp(syntax, "html") == 0)
|
---|
267 | printf("<pre>\n");
|
---|
268 |
|
---|
269 | if (tracing)
|
---|
270 | strcpy(tfilename, D2M_TRACEFILE);
|
---|
271 | else
|
---|
272 | *tfilename = 0;
|
---|
273 |
|
---|
274 | /* Convert to MARC */
|
---|
275 | /* NOTE: M2Mconv() will even convert from MARC 2709 to line format */
|
---|
276 |
|
---|
277 | if (marcfile)
|
---|
278 | rc = M2Mconv((char *) dfilename, (char *) buffer,
|
---|
279 | (char *) tfilename, mformat);
|
---|
280 | else
|
---|
281 | rc = D2Mconv((char *) dfilename, (char *) buffer,
|
---|
282 | (char *) tfilename, (char *) url, mformat);
|
---|
283 |
|
---|
284 | remove(dfilename);
|
---|
285 |
|
---|
286 |
|
---|
287 | /* Produce output */
|
---|
288 |
|
---|
289 | if (!rc)
|
---|
290 | printf("Error: Unable to parse data\n");
|
---|
291 |
|
---|
292 | else
|
---|
293 | {
|
---|
294 | if (f2709)
|
---|
295 | printf("%s",
|
---|
296 | (char *) MARC2709(buffer, 0, 6, recstatus, rectype, biblevel));
|
---|
297 | else
|
---|
298 |
|
---|
299 | {
|
---|
300 | l = strtok((char *) buffer, "\n");
|
---|
301 |
|
---|
302 | while (l)
|
---|
303 | {
|
---|
304 | write_field(l, of, linel, llen, slen);
|
---|
305 | l = strtok(NULL, "\n");
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (*url && !textarea && ( strcmp(syntax, "html") == 0 ) )
|
---|
311 | printf("<hr>\n");
|
---|
312 |
|
---|
313 | if (tracing)
|
---|
314 | {
|
---|
315 | printf("</pre>\n");
|
---|
316 | printf("<h3>Parsed DC metadata:</h3>\n");
|
---|
317 | printf("<pre>\n");
|
---|
318 | write_trace(tfilename, of);
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (strcmp(syntax, "html") == 0)
|
---|
322 | {
|
---|
323 | printf("</pre>\n");
|
---|
324 | printf("</body></html>\n");
|
---|
325 | }
|
---|
326 | }
|
---|