Debian Codegraph @

w4/parse.c

raw
1 /* File: parse.c
2 * Author: Richard Durbin ([email protected])
3 * Copyright (C) J Thierry-Mieg and R Durbin, 1992
4 * -------------------------------------------------------------------
5 * Acedb is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if n t, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
19 * -------------------------------------------------------------------
20 * This file is part of the ACEDB genome database package, written by
21 * Richard Durbin (MRC LMB, UK) [email protected], and
22 * Jean Thierry-Mieg (CRBM du CNRS, France) [email protected]
23 *
24 * Description: ace file parser
25 * Exported functions: parseControl, parseFile, parseBuffer.
26 * HISTORY:
27 * Last edited: Jan 22 10:42 2002 (edgrif)
28 * * Jul 6 16:16 2000 (edgrif): Fixed a number of bugs, reporting of
29 * all errors for pparse broken, reporting of final stats
30 * for xace "readall" parsing broken etc. etc.
31 * * Jan 6 09:28 2000 (edgrif): Fix for SANgc03115.
32 * * Oct 28 10:22 1999 (fw): avoid error when parsing class Tag
33 * which may still be present in ACE4 dump files.
34 * * Oct 13 16:04 1999 (fw): parsing only possible from ACEIN
35 * this only leaves doParseLevel and gets rid of
36 * parsePipe, parseFile etc.
37 * * May 12 15:19 1993 (mieg): added protected mode
38 * * Apr 26 15:31 1993 (cgc): removed -T Top option
39 * * Apr 25 23:25 1993 (cgc): added -R replace option: relpos, _bsHere
40 * * Jun 5 16:18 1992 (mieg): added NON_GRAPHIC to parse in non-graphic mode
41 * * Mar 4 02:48 1992 (rd): added -R option for renaming objects
42 * Created: Sun Jan 12 17:52:35 1992 (rd)
43 * CVS info: $Id: parse.c,v 1.58 2004/04/09 22:21:46 mieg Exp $
44 *-------------------------------------------------------------------
45 */
46
47 #include <wh/acedb.h>
48 #include <wh/aceio.h>
49 #include <wh/a.h> /* for arraykill */
50 #include <wh/bs.h>
51 #include <wh/lex.h>
52 #include <whooks/systags.h>
53 #include <whooks/sysclass.h>
54 #include <wh/pick.h>
55 #include <wh/query.h> /* for queryCheckConstraints() */
56 #include <wh/session.h> /* for sessionUserKey() */
57 #include <wh/dbpath.h>
58 #include <w4/parse_.h>
59
60 /******** extra stuff for graphical parse-window **********/
61 /* */
62 #ifndef NON_GRAPHIC
63 #include <wh/display.h>
64 #include <wh/main.h>
65 #include <wh/call.h>
66 #ifdef ACEDB_GTK
67 #include <wh/gex.h>
68 #endif /* ACEDB_GTK */
69
70 static Graph parseGraph = 0 ;
71
72 static void readAll (void);
73 static void parseDraw(void);
74 static void parseDispDestroy(void);
75 static void openFile(void);
76 static void closeFile(void);
77 static void ignoreErrors(void) ;
78 static void readItem(void);
79 static void readLine(void);
80 static void skipItem(void);
81 #ifdef ACEDB_GTK
82 static void parseDispEnterText(void);
83 #endif /* ACEDB_GTK */
84
85 static MENUOPT parseOptsOpen[] = {
86 { openFile, "Open ace File"},
87 #if defined (ACEDB_GTK)
88 { parseDispEnterText,"Enter text"},
89 #endif /* UNIX */
90 { graphDestroy, "Quit"},
91 {0,0}
92 } ;
93
94 #define IGNORE_ERRORS_TXT "Ignore Errors"
95 #define STOP_ERRORS_TXT "Stop on Error"
96 #define ERROR_INDEX 1
97 static MENUOPT parseOptsClose[] = {
98 { closeFile, "Close File"},
99 { ignoreErrors, IGNORE_ERRORS_TXT},
100 #if defined (ACEDB_GTK)
101 { readAll, "Read all"},
102 { graphForceInterrupt ,"Stop"},
103 #elif defined(MACINTOSH)
104 { readAll, "Read all"}, /* MAC */
105 #elif defined(WIN32)
106 { readAll, "Read all (Use \"Esc\" to break in)"}, /* WINDOWS */
107 #else
108 { readAll, "Read all (hit key F4 to break in)"}, /* UNIX-X11 */
109 #endif /* UNIX */
110 { readItem, "Read one Item"},
111 { readLine, "Read one Line"},
112 { skipItem, "Skip to next Item"},
113 {0,0}
114 } ;
115 #endif /* !NON_GRAPHIC */
116
117
118
119 /***********************************************************************/
120 /************* globals exported from this module ***************/
121
122 ParseFuncType parseFunc[256] ; /* == MAXTABLE of lexsubs.c */
123
124 /***********************************************************************/
125 /********** stuff local to the parser routine and initialiser **********/
126
127 static magic_t PF_MAGIC = "PF" ;
128 static char *PARSE_IN_SPECIAL = "\n/\\\t@"; /* \n - end-of-line
129 * // - comments
130 * \ - escape char
131 * \t - 8 spaces
132 * @ - commandfile */
133
134 typedef enum { DONE, OUTSIDE, INSIDE, REF_BLOCKED } ParseStateType;
135
136 #ifndef NON_GRAPHIC
137 typedef enum { READ_ALL=1, READ_ITEM, READ_LINE } ParseUpdateModeType;
138 #endif /* !NON_GRAPHIC */
139
140 /* Note that the enum and string version arrays must be kept in step. */
141 typedef enum {PARSE_NO_UPDATE, PARSE_GENERAL_ERR, PARSE_ARRAY_ERR, PARSE_OBJECT_ERR} ParseErrType ;
142 char *parse_err_typestrings[4] = {"update", "general", "array", "object"} ;
143
144
145
146 typedef struct ParseStruct
147 {
148 magic_t *magic; /* == &PF_MAGIC */
149
150 ACEIN parse_in ;
151 char *filename;
152
153 ParseStateType state ; /* if DONE it's not parsing and
154 shouldn't have a parse_in */
155 BOOL isError ; /* parse vs. pparse */
156
157 /* Stats on object parsing: */
158 /* */
159 /* nob = number of paragraphs (= objects) in the ace data input */
160 /* (in theory nob = nok + nerr) */
161 /* */
162 /* nok = nadded + neditted + ndeleted + naliased + nrenamed */
163 /* + narray_added + narray_empty + narray_deleted */
164 /* */
165 /* nerr = ngen_err + nerror + narray_err */
166 /* */
167 /* I am treating arrays differently because its not really possible to */
168 /* say for all arrays whether they are new, edits of existing arrays or */
169 /* what...its a bit inconsistent at the moment. */
170 BOOL full_stats ; /* TRUE means show everything. */
171 /* total parse stats. */
172 int nob ;
173 int nok ;
174 int nerr ;
175 /* General errors */
176 int ngen_err ;
177 /* normal object stats */
178 int nadded ;
179 int neditted ;
180 int ndeleted ;
181 int naliased ;
182 int nrenamed ;
183 int nerror ;
184 /* array object stats, note how semantics are different from normal obj. */
185 int narray_added ;
186 int narray_empty ;
187 int narray_deleted ;
188 int narray_err ;
189
190
191 char *obname ;
192 BOOL new_obj ; /* Is the object a new object */
193 char *obj_firstline ; /* Keep record of first line of
194 ojb. for error messages. */
195 int curr_line ;
196 BOOL parseBusy ;
197 BOOL doParseProtected; /* TRUE => doing a kernel parse. */
198
199 BOOL parseKeepGoing ; /* Controls parsing behaviour: */
200 /* if FALSE stops parsing on 1st error */
201 BOOL parseLineErrors ; /* Controls error output: */
202 /* if FALSE then individual line errors
203 are not reported. */
204 BOOL parseQuiet ; /* TRUE => no message are output. */
205 ACEOUT output ; /* alternative destination for error
206 messages. */
207
208 /* It's possible for files to get extraneous null chars in them sometimes, */
209 /* this will lead to premature end of parsing with no real warning to the */
210 /* user. We trap this by looking at file lengths. */
211 BOOL has_length ;
212 long int file_length ;
213 long int file_pos ;
214
215 /* the following are used only in parseLine() */
216 int recursion ;
217 BOOL isDelete, isRename, isAlias, isBang ;
218 OBJ obj ;
219 KEY ref, timeStampKey ;
220 BOOL save_error ; /* Signals error on trying to save
221 object after rename etc. */
222
223
224 #ifndef NON_GRAPHIC
225 char *currentLineText;
226 ParseUpdateModeType updateMode ; /* determines which parts of the
227 * display are updated during parse */
228 #endif /* !NON_GRAPHIC */
229 } *PF ;
230
231
232 /* defines how parseLine should procede next. */
233 typedef enum _ParseNext {PARSE_SKIP, PARSE_NOSKIP, PARSE_ABORT} ParseNext ;
234
235 static PF pfCreate (ACEIN parse_in, STORE_HANDLE handle);
236 static void pfFinalise (void *block);
237 #define pfDestroy(pf) messfree(pf)
238 static BOOL pfExists (PF pf);
239 static BOOL realParseAceIn(ACEIN parse_in, ACEOUT output, KEYSET ks,
240 BOOL keepGoing, BOOL full_stats, BOOL silent, BOOL parse_protected) ;
241 static BOOL doParseLevel (PF pf, KEYSET ks);
242 static BOOL parseArray(PF pf, KEY key, int classe, char **err_text) ;
243 static void parseLine(PF pf, ParseNext parse_type, KEYSET ks);
244 static BOOL saveObject(PF pf, KEYSET ks, char **errtext) ;
245
246
247 #ifndef NON_GRAPHIC
248 /* */
249 /* Graphical routines, these should all be done by callback etc. etc. sigh...*/
250 /* */
251
252 static magic_t ParseDisp_MAGIC = "ParseDisp" ;
253
254 typedef struct ParseDispStruct {
255 magic_t *magic; /* == &ParseDisp_MAGIC */
256 PF pf;
257 int buttonBox ;
258 int lineBox, itemBox, nparsedBox, nokBox, nerrorBox ;
259 char itemText[64], lineText[64], nparsedText[64], nokText[64], nerrorText[64] ;
260 char dirSelection[DIR_BUFFER_SIZE];
261 char fileSelection[FIL_BUFFER_SIZE];
262 } *ParseDisp;
263
264 static ParseDisp currentParseDisp (char *caller);
265 static BOOL parseDispExists (ParseDisp pdisp);
266 static void showState(ParseDisp pdisp);
267 #endif /* !NON_GRAPHIC */
268
269
270
271
272 /******************************************************************
273 ************************* public functions ***********************
274 ******************************************************************/
275
276
277 /* The new parseAceIn function which allows the caller to specify an */
278 /* alternative destination for output messages, this is especially needed */
279 /* by the server. */
280 /* will destroy parse_in */
281 BOOL doParseAceIn(ACEIN parse_in, ACEOUT output, KEYSET ks, BOOL keepGoing, BOOL full_stats)
282 {
283 BOOL ok = FALSE ;
284
285 ok = realParseAceIn(parse_in, output, ks, keepGoing, full_stats, FALSE, FALSE) ;
286 /* will destroy parse_in */
287 return ok ;
288 } /* parseAceIn */
289
290
291 BOOL parseBuffer(char *text, KEYSET ks, BOOL keepGoing)
292 /* convenience function to parse a text-buffer */
293 {
294 BOOL ok = FALSE ;
295 ACEIN parse_in;
296
297 if (!text || !*text)
298 messcrash("parseBuffer() - received NULL or empty text-pointer");
299
300 parse_in = aceInCreateFromText (text, "", 0);
301
302 ok = realParseAceIn(parse_in, NULL, ks, keepGoing, FALSE, FALSE, FALSE) ;
303 /* will destroy parse_in */
304 return ok ;
305 } /* parseBuffer */
306
307
308 /* This variation is used by code that silently creates and parses some ace */
309 /* data to fulfill some other function and hence does not want to display */
310 /* the usual parse message. */
311 /* convenience function to parse a text-buffer */
312 BOOL parseBufferInternal(char *text, KEYSET ks, BOOL keepGoing)
313 {
314 BOOL ok = FALSE ;
315 ACEIN parse_in;
316
317 if (!text || !*text)
318 messcrash("parseBuffer() - received NULL or empty text-pointer");
319
320 parse_in = aceInCreateFromText(text, "", 0);
321
322 ok = realParseAceIn(parse_in, NULL, ks, keepGoing, FALSE, TRUE, FALSE) ;
323 /* will destroy parse_in */
324 return ok ;
325 } /* parseBuffer */
326
327 /* This variation is used by AceC/acinside */
328 BOOL parseAceCBuffer (char *text, Stack s, ACEOUT given_out, KEYSET ks)
329 {
330 BOOL ok = FALSE ;
331 ACEIN parse_in ;
332 ACEOUT parse_out ;
333 BOOL keepGoing = TRUE ;
334
335 if (!text || !*text)
336 messcrash("parseBuffer() - received NULL or empty text-pointer");
337
338 parse_in = aceInCreateFromText(text, "", 0);
339 if (s)
340 parse_out = aceOutCreateToStack (s, 0) ;
341 else
342 parse_out = given_out ;
343 ok = realParseAceIn(parse_in, parse_out, ks, keepGoing, FALSE, TRUE, FALSE) ;
344 /* will destroy parse_in */
345 if (s)
346 aceOutDestroy(parse_out) ;
347
348 return ok ;
349 } /* parseAceCBuffer */
350
351
352 /* This old function has been recoded to be simply a call to the new */
353 /* doParseAceIn function with the "output" parameter set to NULL. */
354 /* */
355 /* DON'T USE THIS FUNCTION, USE doParseAceIn INSTEAD. */
356 /* */
357 /* will destroy parse_in */
358 BOOL parseAceIn(ACEIN fi, KEYSET ks, BOOL keepGoing)
359 {
360 BOOL result ;
361
362 result = realParseAceIn(fi, NULL, ks, keepGoing, FALSE, FALSE, FALSE) ;
363
364 return result ;
365 }
366
367 /* this should be in the private header really....and be called uParse...etc.*/
368 /* mustn't be called from user-code, only accessible to
369 * kernel routines */
370 /* will destroy parse_in */
371 BOOL parseAceInProtected (ACEIN parse_in, KEYSET ks)
372 {
373 BOOL ok = FALSE ;
374
375 /* in the future we should check that parse_in is server-side,
376 * so no user-data can be read into protected classes, only
377 * files likes constraints.wrm which should only be accessible
378 * by the server's administrator */
379
380 ok = realParseAceIn(parse_in, NULL, ks, FALSE, FALSE, TRUE, TRUE) ;
381
382 return ok ;
383 } /* parseAceInProtected */
384
385
386 /************************************************************/
387
388
389 /* This is the function that most of the above calls actually end up going */
390 /* through, this stucture enables us to avoid callers having to set parse */
391 /* state data. */
392 /* Will destroy parse_in */
393 /* */
394 static BOOL realParseAceIn(ACEIN parse_in, ACEOUT output, KEYSET ks,
395 BOOL keepGoing, BOOL full_stats, BOOL silent, BOOL parse_protected)
396 {
397 PF pf ;
398 BOOL ok = FALSE ;
399
400 /* Create our global parse control block and initialise it with callers */
401 /* parameters. */
402 pf = pfCreate(parse_in, 0) ;
403 pf->output = output ;
404 pf->parseKeepGoing = keepGoing ;
405 pf->full_stats = full_stats ;
406 if (silent)
407 {
408 pf->parseLineErrors = FALSE ;
409 pf->parseQuiet = TRUE ;
410 }
411 pf->doParseProtected = parse_protected ;
412
413 ok = doParseLevel(pf, ks);
414
415 pfDestroy (pf) ;
416
417 return ok ;
418 } /* realParseAceIn */
419
420
421
422 #ifndef NON_GRAPHIC
423 /* Main function for xace version of file parsing. */
424 /* */
425 void parseControl (void)
426 {
427 if (!checkWriteAccess())
428 return ;
429
430 /* getMutex */
431 if (graphActivate (parseGraph))
432 graphPop () ;
433 else
434 {
435 ParseDisp pdisp;
436 STORE_HANDLE handle = handleCreate();
437
438 parseGraph = displayCreate ("DtAce_Parser");
439
440 pdisp = messalloc (sizeof(struct ParseDispStruct));
441 pdisp->magic = &ParseDisp_MAGIC;
442 pdisp->itemBox = 0;
443 pdisp->lineBox = 0;
444 pdisp->nparsedBox = pdisp->nokBox = pdisp->nerrorBox = 0 ;
445 if (getenv ("ACEDB_DATA"))
446 strcpy (pdisp->dirSelection, getenv("ACEDB_DATA")) ;
447 else if (dbPathStrictFilName("rawdata", "", "", "rd", handle))
448 strcpy (pdisp->dirSelection,
449 dbPathStrictFilName ("rawdata", "", "", "rd", handle)) ;
450 else /* set to root db-dir */
451 strcpy (pdisp->dirSelection, dbPathMakeFilName("", "", "", handle));
452
453 handleDestroy(handle);
454
455 graphAssociate (&ParseDisp_MAGIC, pdisp);
456
457 graphTextBounds (60, 20) ; /* needed for text box sizing */
458 graphRegister (DESTROY, parseDispDestroy) ;
459
460 parseDraw () ;
461 }
462 /* releaseMutex */
463 return ;
464 } /* parseControl */
465
466 #else /* NON_GRAPHIC */
467 /* currently the gifaceserver links in the .ng.o version of this file
468 * but the rest of the gifaceserver is graphical code, so we have to
469 * supply two stubs here to make the link possible. All this is horrible
470 * and needs cleaning up */
471 /*void parseOneFile (FILE *fil, KEYSET ks) {;}*/
472 void parseControl (void) {;}
473 #endif /* NON_GRAPHIC */
474
475
476 /******************************************************************
477 ************************ private functions ***********************
478 ******************************************************************/
479
480 /* constructor */
481 static PF pfCreate (ACEIN parse_in, STORE_HANDLE handle)
482 {
483 PF pf;
484
485 pf = (PF) halloc (sizeof (struct ParseStruct), handle) ;
486 blockSetFinalise (pf, pfFinalise);
487 pf->magic = &PF_MAGIC ;
488 pf->doParseProtected = FALSE; /* mustn't be overridden by normal usercode */
489
490 /* All these can be overridden to change error reporting behaviour. */
491 pf->parseKeepGoing = FALSE ;
492 pf->output = NULL ;
493 pf->parseLineErrors = TRUE ;
494 pf->parseQuiet = FALSE ;
495
496 pf->full_stats = FALSE ;
497 pf->nob = pf->nok = pf->nerr
498 = pf->ngen_err
499 = pf->nadded = pf->neditted = pf->ndeleted = pf->naliased = pf->nrenamed = pf->nerror
500 = pf->narray_added = pf->narray_empty = pf->narray_err = pf->narray_deleted
501 = 0 ;
502
503 pf->state = DONE ;
504
505 pf->save_error = FALSE ;
506
507 pf->parse_in = parse_in;
508 aceInSpecial (pf->parse_in, PARSE_IN_SPECIAL);
509 pf->filename = strnew (aceInGetURL(parse_in), 0);
510
511 pf->curr_line = 0 ;
512
513 /* Can we take the length of the stream ? */
514 if (aceInStreamLength(pf->parse_in, &(pf->file_length)))
515 pf->has_length = TRUE ;
516 else
517 pf->has_length = FALSE ;
518 pf->file_pos = 0 ;
519
520 return pf;
521 } /* pfCreate */
522
523 static void pfFinalise (void *block)
524 {
525 PF pf = (PF)block;
526
527 if (!pfExists(pf))
528 messcrash("pfFinalise() - received invalid block pointer");
529
530 if (pf->state != DONE)
531 parseLine(pf, PARSE_ABORT, 0); /* closes open object & ace-file */
532
533 messfree(pf->filename);
534 messfree(pf->obname);
535 messfree(pf->obj_firstline) ;
536
537 #ifndef NON_GRAPHIC
538 messfree (pf->currentLineText);
539 #endif /* !NON_GRAPHIC */
540
541 pf->magic = 0 ;
542
543 return ;
544 } /* pfFinalise */
545
546 static BOOL pfExists (PF pf)
547 {
548 if (pf && pf->magic == &PF_MAGIC)
549 return TRUE;
550
551 return FALSE;
552 } /* pfExists */
553
554
555 /************************************************************/
556 /* does all parsing of array objects, these are special in that they do not */
557 /* appear in the models file but may be attached to bits of models data via */
558 /* keys with the same name as the class to which they are attached. */
559 /* All array stats incrementing is done here, note that error stats are */
560 /* recorded by calling routine. */
561 /* */
562 static BOOL parseArray(PF pf, KEY key, int classe, char **err_text)
563 {
564 BOOL result = FALSE ;
565
566 if (classe <= 0 || classe >= 256)
567 messcrash ("bad class %d passed to parseArray", classe) ;
568
569 if (parseFunc[classe])
570 {
571 /* a ParseFunc is registered */
572 ParseFuncReturn parsing ;
573
574 *err_text = NULL ;
575 parsing = parseFunc[classe](pf->parse_in, key, err_text) ;
576 if (parsing == PARSEFUNC_OK)
577 {
578 /* we will need to update the stats here...rather than in the caller */
579 /* unless we return the parse result from here as well... */
580 /* BUT if we have seperate array stats we could just do them here. */
581 result = TRUE ;
582 pf->narray_added++ ;
583 }
584 else if (parsing == PARSEFUNC_EMPTY)
585 {
586 result = TRUE ;
587 pf->narray_empty++ ;
588 }
589 else
590 {
591 /* On failure, skip to end of object and report failure. */
592 while (aceInCard(pf->parse_in) && aceInWord(pf->parse_in)) ;
593
594 /* cop out, really the functions should always return an error */
595 /* string if they fail. */
596 if (*err_text == NULL)
597 *err_text = "registered function for parse of array data failed" ;
598
599 pf->narray_err++ ;
600 result = FALSE ;
601 }
602
603 aceInSpecial (pf->parse_in, PARSE_IN_SPECIAL); /* parseFunc may change the special */
604 }
605 else
606 {
607 /* no ParseFunc registered, object not empty so skip over it. */
608 while (aceInCard(pf->parse_in) && aceInWord(pf->parse_in)) ;
609
610 *err_text = messprintf("No parser available for array class %d", classe) ;
611 pf->narray_err++ ;
612 result = FALSE ;
613 }
614
615 return result ;
616 } /* parseArray */
617
618
619 /*******************************************/
620
621 /* deals with one entry */
622 /* uummmm, this is not quite right, actually we dive in and out of this */
623 /* routine to parse a single entry. */
624 /* */
625 /* This routine calls certain other routines (lexDoAlias, graph stuff) that */
626 /* are not re-entrant and so must not be called again if the initial call to */
627 /* routine results in recursion. All such calls should be guarded with */
628 /* pf->recursion. */
629 /* */
630 static void parseLine(PF pf, ParseNext parse_type, KEYSET ks)
631 {
632 int table, ix ;
633 float fx ;
634 mytime_t maDate ;
635 char *word, dummyChar;
636 char *card = 0;
637 KEY key, tag, type, relpos ;
638 char *errtext = 0 ;
639 char *objname = NULL ;
640 #ifndef NON_GRAPHIC
641 ParseDisp pdisp = currentParseDisp("parseLine");
642 #endif /* !NON_GRAPHIC */
643 #ifndef ACEDB4
644 extern BOOL READING_MODELS ;
645 #endif
646 ParseErrType err_type = PARSE_OBJECT_ERR ; /* commonest err is for non-array
647 objects. */
648
649 pf->isError = FALSE ;
650
651
652 /* Clear after error or skip. */
653 /* */
654 if (parse_type == PARSE_ABORT)
655 {
656 /* Current behaviour is to save an empty object if there is some tag */
657 /* error or something minor...don't konw if this is really a good idea.*/
658 goto done ;
659 }
660 else if (parse_type == PARSE_SKIP)
661 {
662 if (pf->state == DONE)
663 goto done ;
664 if (pf->state == OUTSIDE)
665 return ;
666 err_type = PARSE_NO_UPDATE ;
667 goto abort ;
668 }
669
670
671 /*--------------------- Process an object -------------------------------*/
672 /* */
673 switch (pf->state)
674 {
675 /* Ace file is done (perhaps because of error) */
676 case DONE :
677 goto done ;
678
679
680 /* Look for next object */
681 case OUTSIDE :
682
683 /* Find the next paragraph, i.e. the next potential object. */
684 pf->isBang = FALSE ;
685 messfree(pf->obj_firstline) ;
686 while (TRUE)
687 {
688 #ifndef NON_GRAPHIC
689 messfree (pf->currentLineText);
690 #endif /* !NON_GRAPHIC */
691
692
693 /* Record where we are in stream, note that by the time we get to */
694 /* here the file may already have been closed, which case we can't */
695 /* take its length so we cheat and make look like we got to the */
696 /* end. This is because of the aceIn packages horrible implicit */
697 /* close for aceInCard() when we reach the end of a file. */
698 if (pf->has_length)
699 {
700 if (pf->file_pos < pf->file_length)
701 {
702 if (!(aceInStreamPos(pf->parse_in, &(pf->file_pos))))
703 pf->file_pos = pf->file_length ;
704 }
705 }
706
707 /* Note, the logic here is a bit tricky, if this fails then we are */
708 /* already at the end of the file and it has already been closed */
709 /* due to the yucky implicit close that the aceIn package does. */
710 if (!(card = aceInCard(pf->parse_in)))
711 goto done ;
712
713
714 #ifndef NON_GRAPHIC
715 /* keep the current parse line for showState */
716 if (pf->updateMode == READ_LINE)
717 pf->currentLineText = strnew (card, 0);
718 #endif /* !NON_GRAPHIC */
719
720 pf->obj_firstline = strnew(card, 0) ;
721
722 if (!pf->isBang)
723 {
724 word = aceInWord(pf->parse_in) ;
725 if (word && *word != '!') /* Found a paragraph */
726 break ;
727 if (word && *word == '!') /* jump paragraph */
728 pf->isBang = TRUE ;
729 }
730 else
731 {
732 word = aceInWord(pf->parse_in) ;
733 if (!word)
734 pf->isBang = FALSE ; /* end of jumped paragraph */
735 }
736 }
737
738 /* If we got here we must have found a potential object. */
739 (pf->nob)++ ;
740
741 /* Special processing for a delete/rename/alias. */
742 pf->isDelete = pf->isRename = pf->isAlias = FALSE ;
743
744 if (strcmp (word, "-D") == 0) /* symbol for deletion */
745 {
746 pf->isDelete = TRUE ;
747 if (!(word = aceInWord(pf->parse_in)))
748 {
749 errtext = "No class after -D" ;
750 err_type = PARSE_GENERAL_ERR ;
751 goto abort ;
752 }
753 }
754 else if (strcmp (word, "-R") == 0) /* symbol for renaming */
755 {
756 pf->isRename = TRUE ;
757 if (!(word = aceInWord(pf->parse_in)))
758 {
759 errtext = "No class after -R" ;
760 err_type = PARSE_GENERAL_ERR ;
761 goto abort ;
762 }
763 }
764 else if (strcmp (word, "-A") == 0) /* symbol for aliasing */
765 {
766 pf->isAlias = pf->isRename = TRUE ;
767 if (!(word = aceInWord(pf->parse_in)))
768 {
769 errtext = "No class after -A" ;
770 err_type = PARSE_GENERAL_ERR ;
771 goto abort ;
772 }
773 }
774
775
776 /* Get hold of the objects class and name. */
777 /* */
778 messfree(pf->obname);
779 pf->obname = NULL ;
780
781 if (*word == '>')
782 /* recognize > as fasta format for DNA */
783 {
784 table = pickWord2Class("DNA") ;
785
786 /* the FASTA-object name can appear directly glued
787 * to the > character, or in the next word */
788 if (*(word + 1))
789 objname = word + 1 ;
790 else
791 objname = aceInWord (pf->parse_in) ;
792 }
793 else
794 {
795 table = pickWord2Class(word) ; /* word is the class name */
796 /* note - Could search classes to parse subclasses */
797
798 if ((pickList[table].type != 'A') && (pickList[table].type != 'B'))
799 {
800 errtext = messprintf ("Bad class name : %s", word ? word : "(NULL)") ;
801 err_type = PARSE_GENERAL_ERR ;
802 goto abort ;
803 }
804
805 if (!table)
806 {
807 errtext = messprintf("Unrecognised Class %s ", word) ;
808 err_type = PARSE_GENERAL_ERR ;
809 goto abort ;
810 }
811
812 if (pickList[table].protected && !pf->doParseProtected
813 #ifdef ACEDB4
814 /* in ACEDB4 only, we avoid complaints about parsing the Tag class */
815 && strcmp(name(pickList[table].classe), "Tag") != 0
816 #endif
817 )
818 {
819 errtext = messprintf("Trying to read/alter a protected class %s", word) ;
820 err_type = PARSE_GENERAL_ERR ;
821 goto abort ;
822 }
823
824 #ifdef ACEDB4
825 /* old models */
826 if (pickList[table].type == 'B') /* a kludge to check existence of the model */
827 {
828 if ((pf->obj = bsCreate (KEYMAKE (table,0))))
829 bsDestroy (pf->obj) ;
830 else
831 {
832 errtext = messprintf ("Missing model of class %s", word) ;
833 goto abort ;
834 }
835 }
836 #else
837 /* new models */
838 if (!READING_MODELS && pickList[table].type == 'B' && !pickList[table].model)
839 {
840 errtext = messprintf ("Missing model of class %s", word) ;
841 goto abort ;
842 }
843 #endif /* ACEDB4 */
844
845 aceInStep(pf->parse_in, ':') ; /* optional colon after class */
846
847 /* get the object name */
848 objname = aceInWord(pf->parse_in) ;
849 }
850
851 if (!lexIsGoodName(objname))
852 {
853 errtext = messprintf("Object name %s not valid",
854 objname ? objname : "(NULL)") ;
855 err_type = PARSE_GENERAL_ERR ;
856 goto abort ;
857 }
858
859
860 /* we now have the class in 'table' and the object name in objname */
861
862 /* secure object name for showState */
863 pf->obname = strnew (objname, 0);
864
865
866 /* Deal with rename or alias. */
867 if (pf->isRename)
868 {
869 pf->ref = 0 ;
870 if (!lexword2key (pf->obname, &pf->ref, table))
871 {
872 errtext = messprintf("Object %s not found in database", pf->obname) ;
873 goto abort ;
874 }
875
876 /* Replace copy of original object name with copy of new name. */
877 messfree(pf->obname) ;
878 if (!(pf->obname = aceInWord(pf->parse_in)))
879 {
880 errtext = messprintf("No new name for %s",
881 pf->isAlias ? "alias (-A)" : "rename (-R)") ;
882 goto abort ;
883 }
884 else
885 pf->obname = strnew(pf->obname, 0) ;
886
887 #ifndef NON_GRAPHIC
888 switch (pf->updateMode)
889 {
890 case READ_ALL:
891 if (!((pf->nob)%100))
892 showState (pdisp) ;
893 if (!((pf->nob)%5000))
894 graphRedraw () ;
895 break ;
896 case READ_ITEM:
897 case READ_LINE:
898 showState (pdisp);
899 break ;
900 default:
901 break ;
902 }
903 #endif /* !NON_GRAPHIC */
904
905 {
906 char *err_msg = NULL ;
907
908 (pf->recursion)++ ;
909 if (pf->recursion >= 2)
910 messcrash ("Multiple recursive call to lexalias inside parser") ;
911
912 if (!lexDoAlias(pf->ref, word, FALSE, pf->isAlias, &err_msg))
913 {
914 errtext = messprintf("%s, %s",
915 (pf->isAlias ? "Alias failed" : "Rename failed"),
916 err_msg) ;
917 messfree(err_msg) ;
918 (pf->recursion)-- ;
919 goto abort ;
920 }
921
922 /* N.B. after rename we have a new object with a new key, */
923 /* we need to update pf->ref to reflect this, will fail for alias*/
924 if (!pf->isAlias)
925 {
926 pf->ref = 0 ;
927 if (!lexword2key(pf->obname, &pf->ref, table))
928 {
929 messcrash("%s, cannot get a key for new object \"%s\" in database",
930 (pf->isAlias ? "Alias failed" : "Rename failed"),
931 pf->obname) ;
932 }
933 }
934
935 /* Make sure object is saved and keyset updated. */
936 /* I'm really not sure if its OK/necessary to save a */
937 /* renamed/aliased object, it all seems to come out in the wash..*/
938 if (!(saveObject(pf, ks, &errtext)))
939 goto abort ;
940
941 if (pf->isAlias)
942 (pf->naliased)++ ;
943 else
944 (pf->nrenamed)++ ;
945
946 pf->state = OUTSIDE ;
947 (pf->recursion)-- ;
948 }
949
950 return ;
951 }
952
953 /* Deal with a delete. */
954 if (pf->isDelete)
955 {
956 if (!lexword2key(pf->obname, &pf->ref, table))
957 {
958 errtext = messprintf("Object %s not found in database", pf->obname) ;
959 goto abort ;
960 }
961 else
962 {
963 if (lexDestroyAlias (pf->ref)) /* true if this is an alias */
964 {
965 /* Don't need to do anything here, just destroy the alias. */
966 }
967 else
968 {
969 switch (pickType(pf->ref))
970 {
971 case 'A':
972 arrayKill(pf->ref) ;
973 (pf->narray_deleted)++ ;
974 break ;
975 case 'B':
976 if ((pf->obj = bsUpdate(pf->ref)))
977 bsKill(pf->obj) ;
978 (pf->ndeleted)++ ;
979 break ;
980 default:
981 errtext = "Can't handle because unknown class type" ;
982 err_type = PARSE_GENERAL_ERR ;
983 goto abort ;
984 }
985 }
986
987 pf->state = OUTSIDE ;
988 return ;
989 }
990 }
991
992
993
994 /* OK, must be a 'new' object definition. */
995 /* */
996
997 /* generate a key (pf->ref) for the object (pf->obname), which might */
998 /* be a new object to the database. */
999 pf->new_obj = lexaddkey(pf->obname, &(pf->ref), table) ;
1000
1001 #ifndef NON_GRAPHIC
1002 /* just read another object - redraw screen */
1003 switch (pf->updateMode)
1004 {
1005 case READ_ALL:
1006 if (!((pf->nob)%100))
1007 showState (pdisp) ;
1008 if (!((pf->nob)%5000))
1009 graphRedraw () ;
1010 break ;
1011 case READ_ITEM:
1012 case READ_LINE:
1013 showState (pdisp);
1014 break ;
1015 default:
1016 break ;
1017 }
1018 #endif /* !NON_GRAPHIC */
1019
1020 /* read timeStamp of new object */
1021 pf->timeStampKey = 0;
1022 word = aceInWord(pf->parse_in);
1023 if (word && (strcmp(word, "-O") == 0 ))
1024 {
1025 if (!(word = aceInWord(pf->parse_in)))
1026 {
1027 errtext = "No timestamp after -O" ;
1028 goto abort ;
1029 }
1030 lexaddkey(word, &(pf->timeStampKey), _VUserSession);
1031 }
1032
1033
1034 switch (pickType (pf->ref))
1035 {
1036 case 'A':
1037 if (ks)
1038 keySet (ks, keySetMax (ks)) = pf->ref ;
1039 if (parseArray(pf, pf->ref, table, &errtext))
1040 return ;
1041 else
1042 {
1043 err_type = PARSE_ARRAY_ERR ;
1044 goto abort ;
1045 }
1046 case 'B':
1047 break ; /* handled by REF_BLOCKED */
1048
1049 default:
1050 errtext = "Can't handle because unknown class type" ;
1051 goto abort ;
1052 }
1053 pf->state = REF_BLOCKED ;
1054 /* note fall through */
1055
1056 case REF_BLOCKED :
1057 if (!(pf->obj = bsUpdate (pf->ref)))
1058 {
1059 errtext = messprintf ("Object %s is locked - free before continuing",
1060 pf->obname) ;
1061 goto abort ;
1062 }
1063 pf->state = INSIDE ;
1064 return ;
1065
1066
1067 /* Carry on processing current object. */
1068 case INSIDE :
1069 #ifndef NON_GRAPHIC
1070 messfree (pf->currentLineText);
1071 #endif /* !NON_GRAPHIC */
1072
1073 /* Record where we are in stream, must do this before aceInCard */
1074 /* because aceInCard closes file on EOF and we lose the position. */
1075 if (pf->has_length)
1076 {
1077 if (pf->file_pos < pf->file_length)
1078 aceInStreamPos(pf->parse_in, &(pf->file_pos)) ;
1079 }
1080
1081 pf->curr_line = aceInStreamLine(pf->parse_in) - 1 ;
1082
1083
1084 /* File finished, so save object and clear up. */
1085 if (!(card = aceInCard(pf->parse_in)))
1086 {
1087
1088 if (saveObject(pf, ks, &errtext))
1089 {
1090 if (pf->new_obj)
1091 (pf->nadded)++ ;
1092 else
1093 (pf->neditted)++ ;
1094 goto done ;
1095 }
1096 else
1097 goto abort ;
1098 }
1099
1100 #ifndef NON_GRAPHIC
1101 /* keep the current parse line for showState */
1102 if (pf->updateMode == READ_LINE)
1103 pf->currentLineText = strnew (card, 0);
1104 #endif /* !NON_GRAPHIC */
1105
1106 /* blank line, so save object and go on to next one. */
1107 if (!(word = aceInWord (pf->parse_in)))
1108 {
1109 if (!(saveObject(pf, ks, &errtext)))
1110 goto abort ;
1111
1112 if (pf->new_obj)
1113 (pf->nadded)++ ;
1114 else
1115 (pf->neditted)++ ;
1116
1117 pf->state = OUTSIDE ;
1118 return ;
1119 }
1120
1121
1122 /* More of object to process. */
1123 if (*word == '!')
1124 return ;
1125
1126 /* the 'word' is a tag name */
1127
1128 if (strcmp (word, "-D") == 0) /* symbol for deletion */
1129 {
1130 pf->isDelete = TRUE ;
1131 if (!(word = aceInWord(pf->parse_in)))
1132 {
1133 errtext = "No tag after -D" ;
1134 goto abort ;
1135 }
1136 }
1137 else
1138 pf->isDelete = FALSE ;
1139
1140 if (!lexword2key (word,&tag,0)) /* find tag */
1141 {
1142 errtext = messprintf ("Unknown tag \"%s\"",word) ;
1143 goto abort ;
1144 }
1145
1146 aceInNext(pf->parse_in);
1147 aceInStep (pf->parse_in, ':') ; /* optional colon after tag */
1148
1149 bsGoto(pf->obj, 0) ; /* pop out of subobjs
1150 * return to obj-root */
1151
1152 if (bsAddTag (pf->obj,tag)) /* subobjects come on the same line */
1153 while ((word = aceInWord (pf->parse_in)))
1154 {
1155 scanSubObj:
1156 relpos = _bsRight ;
1157 if (*word == '-' && strlen(word) == 2)
1158 switch (word[1])
1159 {
1160 case 'C': /* standard comment */
1161 if (!(word = aceInWord(pf->parse_in)))
1162 {
1163 errtext = "No Comment after -C" ;
1164 goto abort ;
1165 }
1166 bsAddComment (pf->obj, word, 'C') ;
1167 if (!(word = aceInWord(pf->parse_in)))
1168 continue ;
1169 if (strcmp(word,"-O") != 0) /* only a timestamp possible */
1170 {
1171 errtext = "Extra item on -C line" ;
1172 goto abort ;
1173 }
1174
1175 /* NOTE: fall-through if -O after -C */
1176
1177 case 'O': /* timestamp (UserSession class) */
1178 if (!(word = aceInWord(pf->parse_in)))
1179 {
1180 errtext = "No UserSession key after -O" ;
1181 goto abort ;
1182 }
1183 lexaddkey (word, &key, _VUserSession) ;
1184 bsSetTimeStamp (pf->obj, key) ;
1185 continue ;
1186
1187 case 'R': /* Replace */
1188 if (!(word = aceInWord(pf->parse_in)))
1189 continue ;
1190 relpos = _bsHere ;
1191 }
1192
1193 if (!(type = bsType (pf->obj, relpos)))
1194 {
1195 errtext = messprintf ("tag \"%s\" : %s - off end of model\n",
1196 name (tag), word) ;
1197 goto abort ;
1198 }
1199
1200 if (class(type) == _VQuery)
1201 break ;
1202 #ifndef ACEDB4
1203 /* new models */
1204 if (class(type) == _VModel) /* a type */
1205 #else /* ACEDB4 */
1206 /* old models */
1207 if (class(type) && type == KEYMAKE(class(type), 1)) /* a type */
1208 #endif /* ACEDB4 */
1209 {
1210 if (!bsPushObj(pf->obj))
1211 {
1212 errtext = messprintf ("tag \"%s\" : %s - off end of model, "
1213 "addObj failed\n", name(tag), word) ;
1214 goto abort ;
1215 }
1216 goto scanSubObj ;
1217 }
1218
1219 #ifndef ACEDB4
1220 if (class(type) && (table = type, pickIsA(&table, 0), table))
1221 #else /* ACEDB4 */
1222 if ((table = class(type)))
1223 #endif /* ACEDB4 */
1224 {
1225 if (!lexIsGoodName(word)) /* refuse blank strings: " " */
1226 {
1227 errtext = messprintf ("Blank object names names are illicit, sorry") ;
1228 goto abort ;
1229 }
1230 if (pickKnown (table) &&
1231 !lexword2key (word,&key,table))
1232 {
1233 errtext = messprintf ("tag \"%s\" : class: \"%s\" %s - unknown. "
1234 "You can not create an "
1235 "entry in a \"Known\" class in an indirect way (see "
1236 "options.wrm)\n",
1237 name(tag), pickClass2Word(table), word) ;
1238 goto abort ;
1239 }
1240 lexaddkey (word,&key,table) ;
1241 bsAddKey (pf->obj,relpos,key) ;
1242 }
1243 else if (type == _Int)
1244 {
1245 if (sscanf (word,"%d%c",&ix,&dummyChar) == 1)
1246 bsAddData (pf->obj,relpos,_Int,&ix) ;
1247 else
1248 {
1249 errtext = messprintf ("%s not an integer after %s\n", word, name(tag)) ;
1250 goto abort ;
1251 }
1252 }
1253 else if (type == _Float)
1254 {
1255 double bigfloat = 0 ;
1256 if (sscanf (word,"%lf%c",&bigfloat,&dummyChar) == 1 &&
1257 bigfloat < 1e40 && bigfloat > -1e40 )
1258 {
1259 char buf[128] ;
1260
1261 if (bigfloat <= ACE_FLT_MIN && bigfloat >= -ACE_FLT_MIN)
1262 bigfloat = 0 ;
1263 sprintf (buf, "%1.7g", bigfloat) ; sscanf (buf, "%f", &fx) ;
1264 bsAddData (pf->obj,relpos,_Float,&fx) ;
1265 }
1266 else
1267 {
1268 errtext = messprintf ("%s not a float after %s\n",
1269 word,name(tag)) ;
1270 goto abort ;
1271 }
1272 }
1273 else if (type == _DateType)
1274 {
1275 if ((maDate = timeParse (word)))
1276 bsAddData (pf->obj,relpos,_DateType,&maDate) ;
1277 else
1278 {
1279 errtext = messprintf ("%s not a DateType after %s\n",
1280 word,name(tag)) ;
1281 goto abort ;
1282 }
1283 }
1284 else if (type < _LastC)
1285 bsAddData (pf->obj,relpos,type,word) ;
1286 else /* a tag */
1287 {
1288 if (!lexword2key (word,&tag,0)) /* find tag */
1289 {
1290 errtext = messprintf ("Unknown tag \"%s\"",word) ;
1291 goto abort ;
1292 }
1293 if (!bsAddTag(pf->obj, tag))
1294 {
1295 errtext = messprintf ("Tag \"%s\" not in object",word) ;
1296 goto abort ;
1297 }
1298 }
1299 }
1300 else
1301 {
1302 errtext = messprintf ("Tag \"%s\" does not fit model\n", name(tag)) ;
1303 goto abort ;
1304 }
1305
1306 if (pf->isDelete)
1307 bsPrune (pf->obj) ;
1308
1309 bsGoto(pf->obj, 0) ; /* move obj-cursor back to root */
1310
1311 return ;
1312
1313 }
1314 /* end-switch pf->state */
1315 /*--------------------- Process an object (End) -------------------------*/
1316
1317
1318
1319 /* End processing if we hit an error or file is finished. */
1320 /* */
1321 abort:
1322 /* Report parsing errors: */
1323 /* Note that errors are always logged. It's possible to turn off error */
1324 /* reporting, don't know if this is ever used. */
1325 if (errtext)
1326 {
1327 char *out_msg = NULL ;
1328
1329
1330 /* Prepare the message, print at most 50 chars of obj_firstline */
1331 out_msg = hprintf(0, "%s parse error, near line %d while parsing \"%.50s\", error was: %s",
1332 parse_err_typestrings[err_type], aceInStreamLine(pf->parse_in)-1,
1333 pf->obj_firstline,
1334 errtext) ;
1335
1336 /* If we have an output stream then send errors to it, rather than */
1337 /* messerror, e.g. server uses this to send messages back to client. */
1338 if (pf->output)
1339 {
1340 aceOutPrint(pf->output, "// %s\n", out_msg) ; /* N.B. the "// ", message could be
1341 going back to the client. */
1342
1343 messdump(out_msg) ;
1344 }
1345 else
1346 {
1347 if (pf->parseKeepGoing && !pf->parseLineErrors)
1348 messdump(out_msg) ;
1349 else
1350 messerror(out_msg) ; /* N.B. messerror DOES a messdump. */
1351 }
1352
1353 messfree(out_msg) ;
1354 }
1355
1356 /* If save_error = TRUE it means we have just tried to save an object but */
1357 /* failed, this means there is no need to skip to the end of the current */
1358 /* object. Otherwise we do need to skip to the next object. */
1359 if (!pf->save_error)
1360 {
1361 while (aceInCard(pf->parse_in))
1362 {
1363 char *first_word ;
1364
1365 /* Object definitions/edits must be separated by blank lines, */
1366 /* rename, delete or alias do not have to be. */
1367 if ((first_word = aceInWord(pf->parse_in)))
1368 {
1369 if ((strcmp(first_word, "-R") == 0)
1370 || (strcmp(first_word, "-D") == 0)
1371 || (strcmp(first_word, "-A") == 0))
1372 {
1373 aceInCardBack(pf->parse_in) ;
1374 break ;
1375 }
1376 }
1377 else
1378 break ;
1379 }
1380 }
1381
1382 bsDestroy (pf->obj) ;
1383 pf->state = OUTSIDE ;
1384
1385 if (!pf->parseKeepGoing)
1386 pf->isError = TRUE ;
1387
1388 /* parseArray() increments array stats, so only do errors for non-arrays. */
1389 if (err_type == PARSE_GENERAL_ERR)
1390 (pf->ngen_err)++ ;
1391 else if (err_type == PARSE_OBJECT_ERR)
1392 (pf->nerror)++ ;
1393
1394 #ifndef NON_GRAPHIC
1395 showState (pdisp) ;
1396 #endif /* NON_GRAPHIC */
1397
1398 return ;
1399
1400
1401 done:
1402 {
1403 saveObject(pf, ks, &errtext) ;
1404
1405
1406 /* finish this stream */
1407 aceInDestroy (pf->parse_in); /* aceInClose (pf->fi, pf->level) ;*/
1408 pf->state = DONE ; /* must come before showState */
1409
1410 /* Now that we're done we report the final state to the user */
1411 /* Note, the recursion is private to the code, not for the end user */
1412 #ifndef NON_GRAPHIC /* GRAPHIC */
1413 if (!pf->recursion)
1414 parseDraw () ;
1415 #else /* NON_GRAPHIC */
1416
1417 /* NOTHING TO DO FOR NON-GRAPHICAL CODE, see doParseLevel */
1418
1419 #endif /* NON_GRAPHIC */
1420
1421 /* If parsing finished successfully we still need to check if it stopped */
1422 /* part way through a file, this can happen if a NULL char has crept into*/
1423 /* a file (e.g. because of an NFS glitch). NOTE if parse_in is pipe/stdin*/
1424 /* etc. we can't take its length, if file_pos == -1, this says there was */
1425 /* an error in finding position in file. Fix for SANgc03115 */
1426 /* Note that this check is impossible to do correctly under cygwin, */
1427 /* since the length and pos counts are affected by the translation of */
1428 /* line endings from <CR><LF> to <LF>. Since this check is really for */
1429 /* NFS problems, which are irrelevant under windows, the check is not */
1430 /* done under CYGWIN. */
1431 #ifndef __CYGWIN__
1432
1433 #ifdef ED_G_NEVER_INCLUDE_THIS_CODE
1434 /* I have completely disabled this for now because of the following */
1435 /* problem: */
1436 /*
1437 The problem with the check is that it will fail if the last line in the input is
1438 not terminated with a "\n" because in this case the file/text is closed and the
1439 stream level decremented without the caller being able to get the final length
1440 of file/text parsed. In our case we do try to get the length but because the
1441 streamlevel is decremented to 0 by then, we get length zero returned for the
1442 file. This then screws up the below test. We could do a new aceInDoCard() that
1443 takes a BOOL param to specify whether to close the file implicitly. aceInCard()
1444 then become a macro which expanded to a call to aceInDoCard()
1445
1446 This is a basic problem with the acein interface really. The only way round it
1447 is to provide a variant of aceInCard that allows one to specify that an implicit
1448 close is not done. This would be fairly easy to do but not a high priority right
1449 now I think.
1450 */
1451
1452 if (parse_type != PARSE_ABORT && pf->has_length)
1453 {
1454 if (pf->file_length != pf->file_pos)
1455 {
1456 messerror("Although parsing was successful, it stopped before the end of the file. "
1457 "This may be because NULL characters have accidentally "
1458 "been introduced into the file, these will cause the parser to think "
1459 "it has reached the end of the file and stop. "
1460 "File was %ld bytes long, parsing stopped after %ld bytes.",
1461 pf->file_length, pf->file_pos) ;
1462 }
1463 }
1464 #endif /* ED_G_NEVER_INCLUDE_THIS_CODE */
1465
1466 #endif /* !__CYGWIN__ */
1467
1468 return ;
1469 }
1470
1471
1472
1473 return ;
1474 } /* parseLine */
1475
1476
1477 /****************************************************************/
1478 /* If an object has been renamed, editted or added, it needs to be saved, */
1479 /* these are the steps to do it. */
1480 /* Returns TRUE if the save could be done, FALSE if constaint checks failed. */
1481 static BOOL saveObject(PF pf, KEYSET ks, char **errtext)
1482 {
1483 BOOL result = FALSE ;
1484
1485 if (pickList[class(pf->ref)].constraints &&
1486 !queryCheckConstraints (pf->obj, pf->ref, pickList[class(pf->ref)].constraints))
1487 {
1488 *errtext = messprintf ("Object %s does not satisfy constraints", name (pf->ref)) ;
1489 pf->save_error = TRUE ; /* prevent jumping next object */
1490
1491 result = FALSE ;
1492 }
1493 else
1494 {
1495 /* add the object we've just parsed into the user-supplied keyset, */
1496 /* this makes the "Active Objects" correct. */
1497 if (ks)
1498 keySet (ks, keySetMax (ks)) = pf->ref ;
1499
1500 bsSave (pf->obj) ;
1501
1502 /* Only set the creation timestamp from the acefile if the object was created this session */
1503 if (pf->timeStampKey && (lexCreationUserSession(pf->ref) == sessionUserKey()))
1504 lexSetCreationUserSession(pf->ref, pf->timeStampKey);
1505
1506 result = TRUE ;
1507 }
1508
1509 return result ;
1510 }
1511
1512
1513 /****************************************************************/
1514
1515 static BOOL doParseLevel(PF pf, KEYSET ks)
1516 {
1517 BOOL result = FALSE ;
1518 #ifdef ED_G_NEVER_INCLUDE_THIS_CODE
1519 int active_obj = 0 ; /* see comments below. */
1520 #endif /* ED_G_NEVER_INCLUDE_THIS_CODE */
1521 char *parse_msg = NULL ;
1522
1523 if (!pfExists(pf))
1524 messcrash("doParseLevel() - received invalid pf pointer");
1525
1526 pf->nob = pf->nok = pf->nerr
1527 = pf->ngen_err
1528 = pf->nadded = pf->neditted = pf->ndeleted = pf->naliased = pf->nrenamed = pf->nerror
1529 = pf->narray_added = pf->narray_empty = pf->narray_err = pf->narray_deleted
1530 = 0 ;
1531
1532 pf->state = OUTSIDE ;
1533
1534 do {
1535 parseLine (pf, PARSE_NOSKIP, ks);
1536 } while (!pf->isError
1537 && pf->state != REF_BLOCKED
1538 && pf->state != DONE);
1539
1540 if (pf->state == DONE)
1541 {
1542 result = TRUE ;
1543 }
1544 else
1545 {
1546 result = FALSE ;
1547
1548 /* closes open object & ace-file */
1549 pf->state = DONE ;
1550 parseLine (pf, PARSE_ABORT, ks) ;
1551 }
1552
1553 /* Summary of processing, note that for some processing, active_obj could */
1554 /* be zero because ks is NULL, or it could be zero because no objects got */
1555 /* put in the active keyset. Currently this all comes out in the wash, */
1556 /* because ks is only NULL for non-user parsing (e.g. models file). */
1557 if (ks)
1558 {
1559 keySetSort(ks) ;
1560 keySetCompress (ks) ;
1561
1562 #ifdef ED_G_NEVER_INCLUDE_THIS_CODE
1563 /* I'm not sure we want the active object count, its displayed anyway */
1564 /* at the end of each command. */
1565 /* It can always be included later by uncommenting this and including */
1566 /* the count in the stats below. */
1567 active_obj = keySetCountVisible(ks) ;
1568 #endif /* ED_G_NEVER_INCLUDE_THIS_CODE */
1569 }
1570
1571
1572 /* some global stats are derived from sub-stats. */
1573 pf->nok = pf->nadded + pf->neditted + pf->ndeleted + pf->nrenamed + pf->naliased
1574 + pf->narray_added + pf->narray_empty + pf->narray_deleted ;
1575 pf->nerr = pf->ngen_err + pf->nerror + pf->narray_err ;
1576
1577 if (pf->full_stats)
1578 parse_msg
1579 = hprintf(0,
1580 "total processed: %d found, %d parsed ok, %d parse failed\n"
1581 " general: %d errors\n"
1582 " objects: %d added, %d editted, %d deleted, %d renamed, %d aliased, %d errors\n"
1583 " arrays: %d added, %d empty, %d deleted, %d errors\n",
1584 pf->nob, pf->nok, pf->nerr,
1585 pf->ngen_err,
1586 pf->nadded, pf->neditted, pf->ndeleted, pf->nrenamed, pf->naliased, pf->nerror,
1587 pf->narray_added, pf->narray_empty, pf->narray_deleted, pf->narray_err) ;
1588 else
1589 parse_msg = hprintf(0, "objects processed: %d found, %d parsed ok, %d parse failed\n",
1590 pf->nob, pf->nok, pf->nerr) ;
1591
1592 /* Only output the message if its _not_ a kernel parse (for model reading.)*/
1593 if (!(pf->parseQuiet))
1594 {
1595 if (pf->output)
1596 aceOutPrint(pf->output, "// %s\n", parse_msg) ;
1597 else
1598 messout(parse_msg) ;
1599 }
1600
1601 messfree(parse_msg) ;
1602
1603 return result ;
1604 } /* doParseLevel */
1605
1606
1607
1608 /*************** graphical functions ***********************/
1609
1610 #ifndef NON_GRAPHIC
1611 static ParseDisp currentParseDisp (char *caller)
1612 {
1613 ParseDisp pdisp = 0;
1614
1615 if (!graphActivate (parseGraph))
1616 return NULL;
1617
1618 graphAssFind (&ParseDisp_MAGIC, &pdisp) ;
1619
1620 if (!pdisp)
1621 return NULL;
1622
1623 if (!parseDispExists(pdisp))
1624 messcrash("%s() - found non-magic ParseDisp on graph", caller);
1625
1626 return pdisp;
1627 } /* currentParseDisp */
1628
1629 static BOOL parseDispExists (ParseDisp pdisp)
1630 {
1631 if (pdisp && pdisp->magic == &ParseDisp_MAGIC)
1632 return TRUE;
1633
1634 return FALSE;
1635 } /* parseDispExists */
1636
1637 static void readAll (void)
1638 /* parse-window function */
1639 {
1640 ParseDisp pdisp = currentParseDisp("readAll");
1641 BOOL orig_parseLineErrors ;
1642
1643 if (!pdisp || !pdisp->pf || pdisp->pf->parseBusy)
1644 return;
1645
1646 pdisp->pf->parseBusy = TRUE;
1647 parseDraw(); /* redraw screen in its busy state */
1648
1649 pdisp->pf->updateMode = READ_ALL ;
1650
1651 #ifdef ACEDB_GTK
1652 graphBusyCursorAll();
1653 #else /* !ACEDB_GTK */
1654 graphBusyCursorAll(TRUE);
1655 #endif /* !ACEDB_GTK */
1656
1657 graphSetBlockMode(GRAPH_BLOCKING);
1658
1659 /* Don't report multiple errors for graphical parsing (user can instead */
1660 /* step through from error to error). */
1661 orig_parseLineErrors = pdisp->pf->parseLineErrors ;
1662 pdisp->pf->parseLineErrors = FALSE ;
1663
1664 pdisp->pf->isError = pdisp->pf->parseKeepGoing ;
1665
1666 do
1667 {
1668 parseLine (pdisp->pf, PARSE_NOSKIP, 0);
1669 }
1670 while (!messIsInterruptCalled()
1671 && !pdisp->pf->isError
1672 && pdisp->pf->state != REF_BLOCKED
1673 && pdisp->pf->state != DONE) ;
1674
1675 graphSetBlockMode(GRAPH_BLOCKABLE);
1676
1677 pdisp->pf->parseLineErrors = orig_parseLineErrors ;
1678 pdisp->pf->parseBusy = FALSE;
1679
1680 parseDraw (); /* redraw screen in its idle state */
1681
1682 /* are we finished totally ?? */
1683 if (pdisp->pf->state == DONE)
1684 pfDestroy (pdisp->pf);
1685
1686
1687 return;
1688 } /* readAll */
1689
1690 static void readItem (void)
1691 /* parse-window function */
1692 {
1693 ParseDisp pdisp = currentParseDisp("readItem");
1694
1695 if (!pdisp || !pdisp->pf
1696 || pdisp->pf->parseBusy)
1697 return;
1698
1699 pdisp->pf->parseBusy = TRUE;
1700
1701 pdisp->pf->updateMode = READ_ITEM ;
1702 do {
1703 parseLine (pdisp->pf, PARSE_NOSKIP, 0);
1704 } while (pdisp->pf->state == INSIDE) ;
1705
1706 /* start parsing next item, so we have already the name to report
1707 * in the display */
1708 parseLine (pdisp->pf, PARSE_NOSKIP, 0);
1709
1710 pdisp->pf->parseBusy = FALSE;
1711
1712 /* are we finished totally ?? */
1713 if (pdisp->pf->state == DONE)
1714 pfDestroy (pdisp->pf);
1715
1716 parseDraw ();
1717
1718 return;
1719 } /* readItem */
1720
1721 static void readLine (void)
1722 {
1723 ParseDisp pdisp = currentParseDisp("readLine");
1724
1725 if (!pdisp || !pdisp->pf
1726 || pdisp->pf->parseBusy)
1727 return;
1728
1729 pdisp->pf->parseBusy = TRUE;
1730
1731 pdisp->pf->updateMode = READ_LINE ;
1732 parseLine (pdisp->pf, PARSE_NOSKIP, 0);
1733
1734 pdisp->pf->parseBusy = FALSE;
1735
1736 /* are we finished totally ?? */
1737 if (pdisp->pf->state == DONE)
1738 pfDestroy (pdisp->pf);
1739
1740 /* parseDraw ();*/
1741
1742 return;
1743 } /* readLine */
1744
1745 static void skipItem (void)
1746 /* parse-window function */
1747 {
1748 ParseDisp pdisp = currentParseDisp("skipItem");
1749
1750 if (!pdisp || !pdisp->pf
1751 || pdisp->pf->parseBusy)
1752 return;
1753
1754 pdisp->pf->parseBusy = TRUE;
1755
1756 pdisp->pf->updateMode = READ_LINE ;
1757 parseLine (pdisp->pf, PARSE_SKIP, 0); /* skip one item */
1758 parseLine (pdisp->pf, PARSE_NOSKIP, 0);
1759
1760 pdisp->pf->parseBusy = FALSE;
1761
1762 return;
1763 } /* skipItem */
1764
1765 #ifdef ACEDB_GTK
1766 static void parseDispEnterText(void)
1767 /* parse-window function */
1768 {
1769 ParseDisp pdisp = currentParseDisp("parseDispEnterText");
1770 char *buff;
1771
1772 if (!pdisp ||
1773 /* only refuse to parseDispEnterText,
1774 * if an existing parse-action is busy */
1775 (pdisp->pf && pdisp->pf->parseBusy))
1776 return;
1777
1778 buff = gexTextEditor("Type text to be read", NULL, 0);
1779
1780 if (buff)
1781 {
1782 parseBuffer(buff, 0, FALSE);
1783 messfree (buff);
1784 }
1785
1786 return;
1787 } /* parseDispEnterText */
1788 #endif /* ACEDB_GTK */
1789
1790 static void openFile (void)
1791 /* parse-window function */
1792 {
1793 ACEIN parse_in;
1794 ParseDisp pdisp = currentParseDisp("openFile");
1795
1796 if (!sessionGainWriteAccess())
1797 {
1798 /* may occur if somebody else grabbed it */
1799 messerror ("You no longer have write access!");
1800 graphDestroy () ;
1801 return ;
1802 }
1803
1804 if (!pdisp ||
1805 /* only refuse to openFile,
1806 * if an existing parse-action is busy */
1807 (pdisp->pf && pdisp->pf->parseBusy))
1808 return;
1809
1810 /* destroy the old parse-actions, because we'll make a new one */
1811 if (pdisp->pf)
1812 pfDestroy (pdisp->pf);
1813
1814 /* write access may be lost doing a save */
1815
1816 parse_in =
1817 aceInCreateFromChooser ("Which ace file do you wish to parse ?",
1818 pdisp->dirSelection,
1819 pdisp->fileSelection,
1820 "ace", "r", 0);
1821 if (!parse_in)
1822 return ;
1823
1824 pdisp->pf = pfCreate (parse_in, 0);
1825 aceInSpecial (pdisp->pf->parse_in, PARSE_IN_SPECIAL) ;
1826
1827 messdump ("Parsing file %s", pdisp->pf->filename) ;
1828
1829 pdisp->pf->state = OUTSIDE ;
1830
1831 pdisp->pf->nob = pdisp->pf->nok = pdisp->pf->nerror
1832 = pdisp->pf->ngen_err
1833 = pdisp->pf->nadded = pdisp->pf->neditted = pdisp->pf->ndeleted = pdisp->pf->naliased = pdisp->pf->nrenamed
1834 = pdisp->pf->narray_added = pdisp->pf->narray_empty = pdisp->pf->narray_err = pdisp->pf->narray_deleted
1835 = 0 ;
1836
1837 parseDraw () ;
1838
1839 return;
1840 } /* openFile */
1841
1842 static void closeFile (void)
1843 /* parse-window function */
1844 {
1845 ParseDisp pdisp = currentParseDisp("closeFile");
1846
1847 if (!pdisp || (pdisp->pf && pdisp->pf->parseBusy))
1848 return;
1849
1850 if (pfExists(pdisp->pf))
1851 pfDestroy (pdisp->pf); /* kill current parse-action */
1852
1853 parseDraw () ;
1854
1855 return;
1856 } /* closeFile */
1857
1858
1859 /* Toggle error state. */
1860 static void ignoreErrors(void)
1861 {
1862 ParseDisp pdisp = currentParseDisp("parseDispEnterText");
1863
1864 if (pdisp && pdisp->pf && !(pdisp->pf->parseBusy))
1865 {
1866 pdisp->pf->parseKeepGoing = !(pdisp->pf->parseKeepGoing) ;
1867
1868 if (pdisp->pf->parseKeepGoing)
1869 parseOptsClose[ERROR_INDEX].text = STOP_ERRORS_TXT ;
1870 else
1871 parseOptsClose[ERROR_INDEX].text = IGNORE_ERRORS_TXT ;
1872
1873 pdisp->buttonBox = graphButtons (parseOptsClose, 1, 3, 55) ;
1874 if (!pdisp->pf->parseBusy)
1875 graphBoxClear (pdisp->buttonBox + 3); /* remove Stop button */
1876 graphRedraw() ;
1877 }
1878
1879 return ;
1880 }
1881
1882
1883 /********************************************/
1884
1885 static void parseDraw (void)
1886 /* parse-window function */
1887 {
1888 int line = 9 ;
1889 ParseDisp pdisp = currentParseDisp("parseDraw");
1890
1891 if (!pdisp)
1892 return;
1893
1894 graphPop () ;
1895 graphClear () ;
1896
1897 if (pfExists(pdisp->pf))
1898 /* we're parsing at the moment */
1899 {
1900 if (pdisp->pf->filename && strlen(pdisp->pf->filename) > 0)
1901 /* show name of file being parsed */
1902 {
1903 graphText ("Filename:", 1, 1);
1904 graphText (pdisp->pf->filename, 11,1);
1905 }
1906
1907 pdisp->buttonBox = graphButtons (parseOptsClose, 1, 3, 55) ;
1908 if (!pdisp->pf->parseBusy)
1909 graphBoxClear (pdisp->buttonBox + 3); /* remove Stop button */
1910 }
1911 else
1912 /* we're not parsing at the moment */
1913 pdisp->buttonBox = graphButtons (parseOptsOpen, 1, 3, 55) ;
1914
1915 graphText (" Item:",1,line) ;
1916 pdisp->itemBox = graphBoxStart () ;
1917 graphTextPtr (pdisp->itemText, 9, line, 64) ;
1918 graphBoxEnd () ;
1919
1920 line += 1.5 ;
1921 graphText (" Line:",1,line) ;
1922 pdisp->lineBox = graphBoxStart () ;
1923 graphTextPtr (pdisp->lineText, 9, line, 64) ;
1924 graphBoxEnd () ;
1925
1926 line += 1.5 ;
1927 graphText ("Parsed:",1,line) ;
1928 pdisp->nparsedBox = graphBoxStart () ;
1929 graphTextPtr (pdisp->nparsedText, 9, line, 64) ;
1930 graphBoxEnd () ;
1931
1932 line += 1.5 ;
1933 graphText (" OK:",1,line) ;
1934 pdisp->nokBox = graphBoxStart () ;
1935 graphTextPtr (pdisp->nokText, 9, line, 64) ;
1936 graphBoxEnd () ;
1937
1938 line += 1.5 ;
1939 graphText ("Errors:",1,line) ;
1940 pdisp->nerrorBox = graphBoxStart () ;
1941 graphTextPtr (pdisp->nerrorText, 9, line, 64) ;
1942 graphBoxEnd () ;
1943
1944 showState (pdisp) ;
1945
1946 graphRedraw () ;
1947
1948 return;
1949 } /* parseDraw */
1950
1951 /***************************************/
1952
1953 static void parseDispDestroy (void) /* called when parseGraph dies */
1954 /* parse-window function */
1955 {
1956 ParseDisp pdisp = currentParseDisp("parseDispDestroy");
1957
1958 parseGraph = 0 ;
1959
1960 if (pfExists(pdisp->pf))
1961 pfDestroy (pdisp->pf); /* also closes file etc. */
1962
1963 pdisp->magic = 0;
1964 messfree (pdisp);
1965
1966 /* The new data may have influenced the way the main-window
1967 * is drawn, so we simulate a change in writeAccess
1968 * to redraw - if the update has added longtext objects
1969 * (where there were no before) this is useful, to make
1970 * the [even in longtexts] button appear */
1971 (writeAccessChangeRegister(0))();
1972
1973
1974 return;
1975 } /* parseDispDestroy */
1976
1977 /********************************************/
1978
1979 static void showState (ParseDisp pdisp)
1980 /* parse-window function */
1981 {
1982 PF pf ;
1983
1984 if (!parseDispExists(pdisp))
1985 return;
1986
1987 pf = pdisp->pf ; /* shortcut to pf */
1988
1989 /* N.B. we always show the state even when we've finished parsing because */
1990 /* otherwise the window goes blank at the end of parsing, infuriating for */
1991 /* the user. */
1992
1993 if (!pfExists(pf))
1994 strcpy (pdisp->itemText, "");
1995 else
1996 strcpy (pdisp->itemText,
1997 messprintf ("%d %.40s",
1998 pf->nob,
1999 pf->obname ? pf->obname : "")) ;
2000 graphBoxDraw (pdisp->itemBox, BLACK, WHITE) ;
2001
2002 if (!pfExists(pf))
2003 strcpy (pdisp->lineText, "");
2004 else
2005 {
2006 long int pos;
2007 Stack s = stackCreate (64);
2008
2009 if (pf->state != DONE)
2010 {
2011 pushText (s, messprintf ("%d", aceInStreamLine (pf->parse_in)-1));
2012
2013 if (pf->file_length && aceInStreamPos (pf->parse_in, &pos))
2014 {
2015 float percentage = (100.0 * pos) / pf->file_length;
2016 catText (s, messprintf (" (%2.0f%%)", percentage));
2017 }
2018 }
2019 else
2020 {
2021 pushText (s, messprintf ("%d", pf->curr_line)) ;
2022
2023 if (pf->has_length)
2024 {
2025 float percentage ;
2026
2027 percentage = (100.0 * pf->file_pos) / pf->file_length;
2028 catText (s, messprintf (" (%2.0f%%)", percentage));
2029 }
2030 }
2031
2032
2033 if (pf->currentLineText)
2034 {
2035 catText (s, " ");
2036 catText (s, pf->currentLineText);
2037 }
2038
2039 strncpy (pdisp->lineText, stackText(s, 0), 64);
2040 pdisp->lineText[64-1] = '\0';
2041
2042 stackDestroy (s);
2043 }
2044 graphBoxDraw (pdisp->lineBox, BLACK, WHITE) ;
2045
2046
2047 /* Show some stats on parsing, currently we only show the totals, we could */
2048 /* show everything if required... */
2049 /* */
2050 if (!pfExists(pf))
2051 {
2052 strcpy(pdisp->nparsedText, "") ;
2053 strcpy(pdisp->nokText, "") ;
2054 strcpy(pdisp->nerrorText, "") ;
2055 }
2056 else
2057 {
2058 pf->nok = pf->nadded + pf->neditted + pf->ndeleted + pf->nrenamed + pf->naliased
2059 + pf->narray_added + pf->narray_empty + pf->narray_deleted ;
2060 pf->nerr = pf->ngen_err + pf->nerror + pf->narray_err ;
2061
2062 strcpy(pdisp->nparsedText, messprintf("%d", pf->nob)) ;
2063 strcpy(pdisp->nokText, messprintf("%d", pf->nok)) ;
2064 strcpy(pdisp->nerrorText, messprintf("%d", pf->nerr)) ;
2065 }
2066 graphBoxDraw(pdisp->nparsedBox, BLACK, WHITE) ;
2067 graphBoxDraw(pdisp->nokBox, BLACK, WHITE) ;
2068 graphBoxDraw(pdisp->nerrorBox, BLACK, WHITE) ;
2069
2070
2071 graphProcessEvents ();
2072
2073 return;
2074 } /* showState */
2075 #endif /* !NON_GRAPHIC */
2076
2077
2078 /************************************************************/
2079 /************************** end of file *********************/
2080 /************************************************************/