Debian Codegraph @

w4/command.c

raw
1 /* File: command.c
2 * Author: Danielle et jean Thierry-Mieg ([email protected])
3 * Copyright (C) J Thierry-Mieg and R Durbin, 1995
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 not, 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: Command language for acedb
25 * Exported functions:
26 * HISTORY:
27 * Last edited: Jun 13 14:36 2007 (edgrif)
28 * * Dec 16 11:28 1999 (edgrif): Added more admin cmds.
29 * * Nov 23 09:45 1999 (edgrif): Insert new choixmenu values for admin cmds.
30 * * Oct 15 12:38 1999 (fw): switch to second version of ACEIN API
31 * affected large portions of parse command code
32 * * Dec 3 14:41 1998 (edgrif): Change calls to new interface to aceversion.
33 * * Oct 8 11:44 1998 (fw): changed calls to helpPrintf to
34 * helpOn() call. The helpsystem now uses
35 * a registered funcion for whatever display
36 * funcion we want to get at the help.
37 * The default is obviously a textual dump.
38 * * Aug 14 12:26 1998 (fw): AQL: context var for active keyset @active
39 * * Aug 5 17:18 1998 (fw): added AQL functionality
40 * * Nov 18 19:27 1997 (rd)
41 * - Read .zip files Assumes that xyz.zip file (only)
42 * contains a xyz.ace file; minor menu changes for "p" option
43 * - Used aceInPath() (see filsubs.c) instead of aceInWord() for filename
44 * parses in commands "write", "Keyset read","parse","table reads" etc.
45 * * Jun 11 15:40 (rbrusk): #include "acedb.h" for name(), className() etc.
46 * * Apr 23 16:45 1996 (srk)
47 * Created: Sun Nov 5 17:41:29 1995 (mieg)
48 * CVS info: $Id: command.c,v 1.253 2007/06/19 11:12:56 edgrif Exp $
49 *-------------------------------------------------------------------
50 */
51
52 #define CHRONO
53
54 #include <wh/acedb.h>
55 #include <wh/aceio.h>
56 #include <wh/call.h>
57 #include <wh/bs.h>
58 #include <w4/command_.h>
59 #include <wh/spread_.h> /* YUCK!!! HACK HACK HACK */
60 #include <wh/java.h> /* for freejavaprotect() */
61 #include <wh/session.h>
62 #include <wh/a.h>
63 #include <wh/dna.h>
64 #include <wh/peptide.h>
65 #include <wh/dump.h>
66 #include <wh/lex.h>
67 #include <wh/pick.h>
68 #include <wh/biblio.h>
69 #include <wh/table.h>
70 #include <wh/parse.h>
71 #include <wh/aql.h>
72 #include <wh/bindex.h>
73 #include <wh/help.h>
74 #include <wh/alignment.h>
75 #include <wh/model.h>
76 #include <wh/longtext.h> /* for longGrep */
77 #include <wh/status.h>
78 #include <whooks/sysclass.h>
79 #include <whooks/systags.h>
80 #include <whooks/tags.h>
81 #include <whooks/classes.h>
82 #include <wh/dbpath.h>
83 #include <wh/basepad.h>
84 #include <wh/smap.h>
85 #include <wh/chrono.h>
86
87 /************************************************************/
88
89
90 static void taceTest (ACEIN command_in, ACEOUT res_out) ;
91 static BOOL aceCommandExists (AceCommand look);
92 static void showModel(ACEOUT fo, KEY key);
93 static BOOL doShowModel (ACEIN model_in, char *name, ACEOUT result_out) ;
94 static char getOnOff (ACEIN fi);
95 static void printUsage (ACEOUT fo, FREEOPT *qmenu, KEY option);
96 static void printCmd(ACEOUT fo, FREEOPT *qmenu, KEY option) ;
97 static int getDataVersion (void);
98
99
100 /* A small set of routines for supporting a "last modifed" timestamp for the */
101 /* database. If this proves too crude or we need access from elsewhere then */
102 /* these could be moved into a separate module. For multi-threading this */
103 /* would all require locking. */
104 void setLastModified(void) ;
105 char *getLastModified(void) ;
106 void lastModified(char **new_time) ;
107
108
109 static void smapCmds(ACEIN command_in, ACEOUT result_out, int option) ;
110
111
112 /************************************************************/
113
114 static magic_t AceCommand_MAGIC = "AceCommand";
115
116
117 GifEntryFunc gifEntry = NULL ; /* global, set in giface code */
118
119
120 /******************************************************************
121 ************************ public functions ***********************
122 ******************************************************************/
123
124 void commandExecute (ACEIN fi, ACEOUT fo,
125 unsigned int choix, unsigned int perms, KEYSET activeKs)
126 /* warapper function for a command main-loop */
127 {
128 AceCommand look;
129 int maxChar = 0 ;
130 int option ;
131
132 look = aceCommandCreate(choix, perms) ;
133
134 /* This is essential for at least Aquila, which needs the sub shells to */
135 /* $echo its logging info. to the screen/files, otherwise this logging info */
136 /* is passed through to acedb !!!!!!!!!! */
137 if (!getenv("ACEDB_SUBSHELLS"))
138 aceInSpecial (fi,"\n\t\\/@%") ; /* forbid sub shells */
139 else
140 aceInSpecial (fi,"\n\t\\/@%$") ; /* allow sub shells */
141
142
143 if (aceInIsInteractive(fi) && !getenv("ACEDB_NO_BANNER"))
144 aceOutPrint (fo, "\n\n// Type ? for a list of options\n\n") ;
145
146 option = 0 ;
147 look->ksNew = activeKs ? keySetCopy (activeKs) : keySetCreate() ;
148 while (TRUE)
149 {
150 option = aceCommandExecute (look, fi, fo, option, maxChar) ;
151
152 switch (option)
153 {
154 case 'q':
155 goto fin ;
156
157 case 'B':
158 case 'D':
159 case 'm':
160 case 'M': /* automatic looping */
161 break ;
162
163 default:
164 option = 0 ;
165 break ;
166 }
167 }
168
169 fin:
170 aceCommandDestroy (look) ;
171
172 return;
173 } /* commandExecute */
174
175 /*************************************************************/
176 /*************************************************************/
177 /* Ace-C: simply exe the command and return the result on the stack */
178
179 Stack commandStackExecute (AceCommand look, char *command)
180 {
181 int option = 0, maxChar = 0 ;
182 ACEIN fi = aceInCreateFromText (command, "", 0) ;
183 ACEOUT fo ;
184 Stack s = stackCreate (3000) ;
185
186 stackTextOnly (s) ;
187 fo = aceOutCreateToStack (s, 0) ;
188 fi = aceInCreateFromText (command, "", 0) ;
189 aceInSpecial (fi, "\n\t\\/%") ; /* forbid sub shells and includescommand_ct*/
190
191 option = 0 ;
192 while (TRUE)
193 {
194 option = aceCommandExecute (look, fi, fo, option, maxChar) ;
195
196 switch (option)
197 {
198 case 'q':
199 goto fin ;
200 case 'B':
201 case 'D':
202 case 'm':
203 case 'M': /* automatic looping */
204 break ;
205 default:
206 option = 0 ;
207 break ;
208 }
209 }
210 fin:
211
212 aceInDestroy (fi) ;
213 aceOutDestroy (fo) ;
214
215 return s ;
216 } /* commandStackExecute */
217
218 /*************************************************************/
219 /* Ace-C */
220 void aceCommandSwitchActiveSet (AceCommand look, KEYSET ks, KEYSET ks2)
221 {
222 if (ks)
223 {
224 keySetDestroy (look->ksNew) ;
225 look->ksNew = keySetCopy (ks) ;
226 }
227 if (look->ksNew && ks2)
228 {
229 int i = keySetMax (look->ksNew) ;
230 while (i--)
231 keySet (ks2, i) = keySet (look->ksNew, i) ;
232 }
233 return ;
234 } /* aceCommandSwitchActiveSet */
235
236 /*************************************************************/
237 /*************************************************************/
238
239 AceCommand aceCommandCreate (unsigned int choix, unsigned int perms)
240 {
241 AceCommand look;
242
243 look = (AceCommand)messalloc(sizeof(AceCommandStruct)) ;
244
245 look->magic = &AceCommand_MAGIC ;
246 look->choix = choix ;
247 look->perms = perms ;
248 look->aMenu = uAceCommandChoisirMenu(choix, perms) ;
249 look->ksNew = keySetCreate () ;
250 look->ksStack = stackCreate(5) ;
251
252 look->showStatus = FALSE ;
253 look->quiet = FALSE ;
254
255 look->allowMismatches = FALSE ;
256 look->spliced = TRUE ;
257 look->cds_only = FALSE ;
258
259 return look ;
260 } /* aceCommandCreate */
261
262
263 void aceCommandDestroy (AceCommand look)
264 {
265 KEYSET ks ;
266
267 if (!aceCommandExists(look))
268 messcrash("aceCommandDestroy() - received non-magic look pointer");
269
270 arrayDestroy (look->aMenu) ;
271 keySetDestroy (look->ksNew) ;
272 keySetDestroy (look->ksOld) ;
273 keySetDestroy (look->kA) ;
274
275 while (!stackAtEnd (look->ksStack))
276 {
277 ks = pop (look->ksStack,KEYSET) ;
278 if (keySetExists (ks))
279 keySetDestroy (ks) ;
280 }
281 stackDestroy (look->ksStack) ;
282 if (look->spread)
283 spreadDestroy (look->spread);
284
285 if (look->showCond)
286 condDestroy (look->showCond) ;
287
288 if (look->dump_out)
289 aceOutDestroy (look->dump_out);
290
291 look->magic = 0 ;
292 messfree (look) ;
293
294 return;
295 } /* aceCommandDestroy */
296
297
298
299 /*************************************************************/
300 /*************************************************************/
301
302 #define CHECK if (!look->ksNew || !keySetMax (look->ksNew)) \
303 { \
304 aceOutPrint (result_out, "// Active list is empty\n"); \
305 break; \
306 }
307
308 #define CHECK_WRITE if (look->noWrite) /* in readonly mode */ \
309 { \
310 aceOutPrint (result_out, "// Sorry, you do not have Write Access.\n") ; \
311 break ; \
312 } \
313 if (!sessionGainWriteAccess()) /* try to grab it */ \
314 { \
315 /* may occur is somebody else grabbed it */ \
316 aceOutPrint (result_out, "// Sorry, you cannot gain Write Access\n") ; \
317 break ; \
318 }
319
320 #define UNDO { keySetDestroy(look->ksOld) ; look->ksOld = look->ksNew ; look->ksNew = 0 ; } /* save undo info */
321
322
323 /********************* MAIN LOOP *****************************/
324
325 KEY aceCommandExecute (AceCommand look, ACEIN command_in, ACEOUT result_out, int option, int maxChar)
326 /* used also by aceserver, jadeserver */
327
328 /* PARAMETERS:
329 *
330 * if option == 0 we need to get a new command
331 * else we already have an option and can execute it straight away
332 */
333 {
334 KEYSET ksTmp = 0 ;
335 int nn = 0 , i, ii = 0, j ;
336 KEY key, *kp, tag ;
337 char *cp = 0, *cq =0, c ;
338 BOOL isLongGrep = FALSE;
339 BOOL parseKeepGoing = FALSE ;
340 FREEOPT *qmenu = arrp (look->aMenu, 0, FREEOPT) ;
341 STORE_HANDLE tmp_handle = handleCreate();
342 ACEIN tmp_command_in = NULL ;
343
344 if (!aceCommandExists(look))
345 messcrash("aceCommandExecute() - received non-magic look pointer");
346
347 if (!option)
348 {
349 aceInOptPrompt (command_in, &option, qmenu);
350 /* NOTE: that if option is still ZERO, no choice was made */
351 }
352
353
354 if (!option)
355 {
356 /* No choice made, or unknown command or ambiguous abbrev. */
357 if (aceInIsInteractive(command_in))
358 aceOutPrint (result_out, "// Please type ? for a list of commands.\n") ;
359 }
360 else
361 /* Valid command. */
362 switch (option)
363 {
364 /**** end of input / quit command ****/
365 case -1:
366 #ifdef KS_ADDED
367 {
368 extern KEYSET ksAdded ; /* from dnasubs.c */
369 if (ksAdded)
370 { FILE *f = fopen ("seqused.ace","w") ;
371 keySetDump (f, 0, ksAdded) ;
372 }
373 }
374 #endif
375 if (aceInIsInteractive (command_in))
376 aceOutPrint (result_out, "\n\n") ;
377
378 option = 'q';
379 break;
380
381 case 'H': /* Chrono */
382 cp = aceInWord (command_in) ;
383 if (cp && !strcasecmp (cp, "start"))
384 chronoStart () ;
385 else if (cp && !strcasecmp (cp, "stop"))
386 chronoStop () ;
387 else if (cp && !strcasecmp (cp, "show"))
388 chronoReport () ;
389 else
390 aceOutPrint (result_out, "// Usage: Chrono { start | stop | show}\n") ;
391 break ;
392
393 /**** list commands / print usage ****/
394 case '?':
395 {
396 char *arg;
397 KEY command_key_in_qmenu;
398
399 arg = aceInPos(command_in) ;
400
401 if (arg && strlen(arg) > 0)
402 {
403 /* User typed "? <some_cmd>", so try to print usage for it. */
404
405 if (aceInKey(command_in, &command_key_in_qmenu, qmenu))
406 {
407 /* Found unambiguous single command. */
408 aceOutPrint(result_out, "// ACEDB %scommand: %s\n",
409 ((look->choix & CHOIX_SERVER) ? "Server " : ""),
410 arg);
411 printCmd(result_out, qmenu, command_key_in_qmenu);
412 }
413 else
414 {
415 /* Look for any commands which partially match. */
416 char *cq = messprintf("%s*", arg) ;
417 int cmds_index[250] ; /* Should be tied to command array size */
418 int i, j ;
419
420 i = 1 ;
421 j = 0 ;
422 while (qmenu[i].text != NULL)
423 {
424 if (pickMatch (qmenu[i].text, cq))
425 {
426 cmds_index[j] = i ;
427 j++ ;
428 }
429 i++ ;
430 }
431
432 if (j > 0)
433 {
434 aceOutPrint(result_out, "// ACEDB %slist of commands matching %s:\n",
435 ((look->choix & CHOIX_SERVER) ? "Server " : ""),
436 cq) ;
437
438 for (i = 0 ; i < j ; i++)
439 {
440 aceOutPrint(result_out, "%s\n", qmenu[cmds_index[i]].text) ;
441 }
442 }
443 else if (aceInIsInteractive(command_in))
444 {
445 aceOutPrint(result_out,
446 "// Cannot describe command %s.\n"
447 "// Please type ? for a list of commands.\n",
448 arg) ;
449 }
450 }
451 }
452 else
453 {
454 /* User typed "?", so print description of all commands. */
455 aceOutPrint(result_out,
456 "// ACEDB %slist of commands:\n",
457 ((look->choix & CHOIX_SERVER) ? "Server " : "")) ;
458
459 for (i = 1 ; i <= qmenu[0].key ; i++)
460 aceOutPrint(result_out, "%s\n", qmenu[i].text) ;
461
462 aceOutPrint(result_out, "// ? command for options of just that command\n") ;
463 }
464 } /* end-case '?' */
465 break ;
466
467
468
469
470 /**** help ****/
471 case 'h':
472 if ((cp = aceInWord(command_in)))
473 helpOn (cp) ;
474 else
475 {
476 aceOutPrint
477 (result_out,
478 "This program maintains an internal list of current objects. \n "
479 "You can\n"
480 "-Create and modify the list with the simple commands: \n"
481 " Find, Follow, Grep, LongGrep, Biblio, Is, Remove, Undo \n"
482 "-Visualise in several format the content of the list with:\n"
483 " List, Show, Count, Dna, Dump\n"
484 "-Perform complex queries and print relational tables with: \n"
485 " Query, Table \n"
486 "-Modify the data with:\n"
487 " New, Edit, EEdit, Parse, PParse, Kill\n"
488 "-See the schema and the dataload with\n"
489 " Model, Classes, Status, Time_stamps\n"
490 "-Manipulate several lists a la polonaise with\n"
491 " clear, spush, spop, swap, sand, sor, sxor, sminus\n"
492 " \n"
493 "Commands are not case sensitive and can be abbreviated.\n"
494 "They can be read from a command file with parameters (noted %%1 %%2...) with\n"
495 " @file_name parm1 parm2 parm3...\n"
496 "if ACEDB_SUBSHELLS is set, lines starting with $ run as an interactive subshell.\n"
497 "Everything following // on a line is ignored (comment).\n"
498 "To escape any of the special characters use a backslash, eg \\@.\n\n"
499
500 "To see the syntax of all commands type: ?\n"
501 "For further help type: help topic\n"
502 );
503 }
504 break ;
505
506
507 case 'u': /* undo */
508 if (look->ksOld)
509 {
510 keySetDestroy (look->ksNew) ;
511 look->ksNew = look->ksOld ;
512 look->ksOld = 0 ;
513 aceOutPrint (result_out, "\n// Recovered %d objects\n", keySetMax(look->ksNew)) ;
514 }
515 else
516 aceOutPrint (result_out, "// No more previous list available\n") ;
517 break ;
518
519
520 case '/': /* ignore comments */
521 break ;
522
523
524 case 'N': /* Count, always part of stdout */
525 keySetDestroy(look->kA) ;
526 if (look->ksNew)
527 look->kA = keySetAlphaHeap(look->ksNew, keySetMax(look->ksNew)) ;
528 aceOutPrint (result_out, "// Active KeySet holds %d object names\n", look->kA ? keySetMax(look->kA) : 0);
529 keySetDestroy(look->kA) ;
530 break ;
531
532 case '<': /* Drop write access permanently */
533 {
534 BOOL previousIsWriteAccess = isWriteAccess();
535
536 sessionReleaseWriteAccess();
537 sessionForbidWriteAccess(); /* can never re-gain */
538
539
540 /* this used to call sessionDropWriteAccess(),
541 which calls sessionDoClose(FALSE), which in turn
542 will just release write access and return
543 and then it would set the dropAccess flag to true
544 so we can never re-gain write access again,
545 so logic is preserved */
546
547 if (isWriteAccess())
548 messerror ("// Write access couldn't be dropped\n");
549 else
550 {
551
552 if (!previousIsWriteAccess)
553 aceOutPrint (result_out, "// Write access dropped permanently\n") ;
554 else
555 aceOutPrint (result_out, "// Write access dropped permanently, recent modifs will not be saved\n") ;
556 }
557 break ;
558 }
559
560 case 'S': /* Save */
561 {
562 BOOL regain = FALSE ;
563
564 while ((cp = aceInWord(command_in)))
565 {
566 if (strcmp (cp, "-drop") == 0)
567 regain = FALSE ;
568 else if (strcmp(cp, "-regain") == 0)
569 regain = TRUE ;
570 }
571
572 CHECK_WRITE ;
573 if (!isWriteAccess())
574 {
575 option = 0 ;
576 aceOutPrint (result_out, "// Nothing to save\n") ;
577 }
578 else
579 {
580 sessionMark (TRUE, 0) ;
581 setLastModified() ;
582
583 if (regain)
584 {
585 /* There is a small window where someone else could grab write access. */
586 if (!sessionGainWriteAccess())
587 aceOutPrint (result_out, "// Sorry, you cannot gain Write Access\n") ;
588 }
589 }
590
591 break ;
592 }
593
594 case CMD_NOSAVE: /* NoSave */
595 {
596 BOOL save = FALSE ;
597
598 while ((cp = aceInWord(command_in)))
599 {
600 if (strcmp (cp, "-stop") == 0)
601 save = FALSE ;
602 else if (strcmp(cp, "-start") == 0)
603 save = TRUE ;
604 }
605
606 if (!writeAccessPossible())
607 {
608 option = 0 ;
609 aceOutPrint (result_out, "// You cannot get write access to save anyway.\n") ;
610 }
611 else
612 {
613 sessionSetSaveStatus(save) ;
614 }
615
616 break ;
617 }
618
619 case CMD_WRITEACCESS: /* Writeaccess */
620 {
621 BOOL gain = TRUE, save = TRUE ;
622 char *msg = NULL ;
623
624 while ((cp = aceInWord(command_in)))
625 {
626 if (strcmp(cp, "-gain") == 0)
627 gain = TRUE ;
628 else if (strcmp (cp, "-drop") == 0)
629 {
630 gain = FALSE ;
631 while ((cp = aceInWord(command_in)))
632 {
633 if (strcmp(cp, "save") == 0)
634 save = TRUE ;
635 else if (strcmp(cp, "nosave") == 0)
636 save = FALSE ;
637 }
638 }
639 }
640
641 if (gain)
642 {
643 if (sessionGainWriteAccess())
644 msg = "// You have write access\n" ;
645 else
646 msg = "// Sorry, you cannot gain Write Access\n" ;
647 }
648 else
649 {
650 if (save)
651 {
652 sessionMark (TRUE, 0) ;
653 setLastModified() ;
654 }
655 else
656 sessionReleaseWriteAccess() ;
657 msg = "// Write access released\n" ;
658 }
659 aceOutPrint (result_out, msg) ;
660 break ;
661 }
662
663 case 'V': /* Test */
664 taceTest (command_in, result_out) ;
665 break ;
666
667 case 'Z': /* Status command */
668 {
669 BOOL usage = FALSE ;
670 StatusType type = STATUS_EMPTY ;
671 BOOL onoff_set = FALSE ;
672
673 while ((cp = aceInWord(command_in)))
674 {
675 if (strcmp (cp, "on") == 0)
676 {
677 onoff_set = TRUE ;
678 look->showStatus = TRUE ;
679 }
680 else if (strcmp(cp, "off") == 0)
681 {
682 onoff_set = TRUE ;
683 look->showStatus = FALSE ;
684 }
685 else if (!(tStatusSetOption(cp, &type)))
686 usage = TRUE ;
687 }
688
689 if (usage)
690 {
691 printUsage(result_out, qmenu, option) ;
692 }
693 else
694 {
695 if (type != STATUS_EMPTY)
696 tStatusSelect(result_out, type) ;
697 else
698 tStatus (result_out) ;
699
700 if (onoff_set && look->showStatus)
701 aceOutPrint (result_out, "// Memory statistics on, some commands will dump statistics periodically\n") ;
702 else if (onoff_set && !look->showStatus)
703 aceOutPrint (result_out, "// Memory statistics off\n") ;
704 }
705 }
706 break ;
707
708 case 'J': /* Date */
709 aceOutPrint (result_out, "\n// %s \n",timeShow(timeNow())) ;
710 break ;
711
712 case 'I': /* Time Stamps creation */
713 switch (getOnOff(command_in))
714 {
715 case 't':
716 isTimeStamps = TRUE ; /* GLOBAL for using timeStamps */
717 aceOutPrint (result_out, "// Time-stamps now ON: Each modified data will be time stamped\n") ;
718 break ;
719
720 case 'f':
721 isTimeStamps = FALSE ; /* GLOBAL for using timeStamps */
722 aceOutPrint (result_out, "// Time-stamps now OFF\n") ;
723 break ;
724
725 case '?':
726 printUsage(result_out, qmenu, option) ;
727 break ;
728
729 default: /* No cmdline flag. */
730 aceOutPrint (result_out, "// Time-stamps currently %s\n",
731 isTimeStamps ? "ON" : "OFF") ;
732 }
733 break ;
734
735 /******************* Stand Alone *****************************/
736
737 case 'c': /* "Classes" */
738 aceOutPrint (result_out,
739 "These are the known classes and "
740 "the number of objects in each class\n") ;
741
742 for (i = 0; i < 256; i++)
743 {
744 if (pickClass2Word(i)
745 && !pickList[i].private
746 && lexMaxVisible(i)) /* 2nd call to lexMaxVisible()
747 * is cheap */
748 aceOutPrint (result_out, "%35s %d\n",
749 pickClass2Word(i), lexMaxVisible(i)) ;
750
751 }
752 break ;
753
754
755 case 'z': /* "Model" */
756 cp = aceInWord(command_in) ;
757 if (!cp)
758 {
759 aceOutPrint (result_out,
760 "// Usage Model [-jaq] Class : "
761 "Shows the model of given class,\n"
762 "//accepts wildcards\n") ;
763 break ;
764 }
765 { BOOL isJaq = FALSE ;
766 BOOL found = FALSE ;
767
768 if (strcmp (cp, "-jaq") == 0)
769 { isJaq = TRUE ;
770 cp = aceInWord(command_in) ;
771 if (!cp)
772 {
773 aceOutPrint (result_out,
774 "// Usage Model [-jaq] Class : Shows the model of given class,\n"
775 "// accepts wildcards\n") ;
776 break ;
777 }
778 }
779
780 i = 0 ;
781 key = 0;
782 while (lexNext(_VModel, &key))
783 {
784 if (pickMatch (name(key)+1, cp))
785 {
786 if (isJaq)
787 showModelJaq (result_out, key) ;
788 else
789 showModel (result_out, key);
790 found = TRUE ;
791 }
792 }
793
794 if (!isJaq)
795 for (key = 0 ; lexNext(_VClass, &key) ; )
796 if (pickMatch (name(key), cp) )
797 {
798 KEY classe = key ;
799 unsigned char mask ;
800
801 pickIsA (&classe, &mask) ;
802 if (mask)
803 {
804 found = TRUE ;
805 aceOutPrint
806 (result_out,
807 "// %s is a sub class of %s,"
808 " here is its filter\n",
809 name(key), pickClass2Word(classe)) ;
810 {
811 OBJ obj = bsCreate (key) ;
812 if (obj)
813 {
814 if (bsGetData (obj, _Filter, _Text, &cp))
815 aceOutPrint (result_out,
816 "// %s\n\n", cp);
817 bsDestroy (obj) ;
818 }
819 }
820 }
821 }
822
823 if (!found)
824 aceOutPrint (result_out,
825 "// Sorry, I can t find a model for : %s\n",
826 cp);
827 }
828 break ;
829
830
831
832 case 'R': /* "Read-models" */
833 if (look->choix & CHOIX_SERVER)
834 {
835 /* To remove this restriction will mean a number of changes to */
836 /* code in session.c and bindex.c because of the user inter- */
837 /* action required by these routines. */
838 aceOutPrint (result_out, "// Sorry, you must use tace or xace to Read-models\n") ;
839 break ;
840 }
841
842 if (!isWriteAccess())
843 {
844 CHECK_WRITE;
845 sessionReleaseWriteAccess () ; /* don t get it just before read-mo */
846 }
847
848 cp = aceInWord(command_in) ;
849 if (cp)
850 {
851 if (strcmp(cp,"-index") == 0)
852 {
853 /* calls readModels() ; which eventually calls bIndexInit() */
854 bIndexInit(BINDEX_AFTER_READMODELS) ;
855 }
856 else
857 {
858 aceOutPrint (result_out, "// Usage: Read-models [-index]\n") ;
859 break ;
860 }
861 }
862 else
863 readModels() ;
864 setLastModified() ;
865 aceOutPrint (result_out, "// New models read, save your work before quitting\n") ;
866 break ;
867
868 case 'F': /* New Class format */
869 {
870 char *class_name;
871 KEY class_key;
872 int class_index;
873 char *format;
874 Stack textStack;
875 BOOL isAddOK;
876 KEY added_key;
877
878 class_name = aceInWord(command_in) ;
879 if (!class_name)
880 {
881 aceOutPrint (result_out, "// Usage New Class Format: Construct a new name"
882 " according to C-language printf(format,n) \n") ;
883 aceOutPrint (result_out, "// Example: New Oligo xxx.\\%%d\n") ;
884 break ;
885 }
886
887 if (!lexword2key(class_name, &class_key, _VMainClasses))
888 {
889 aceOutPrint (result_out, "//! Unkown class %s\n", class_name) ;
890 break ;
891 }
892
893 class_index = KEYKEY(class_key) ;
894 if (pickList[class_index].protected)
895 {
896 aceOutPrint (result_out, "//! Protected class\n", class_name) ;
897 break ;
898 }
899
900 format = aceInWord(command_in) ;
901 if (!format)
902 {
903 aceOutPrint (result_out, "// Usage New Class Format: Construct a new name"
904 " according to C-language printf(format,n) \n") ;
905 aceOutPrint (result_out, "// Example: New Oligo xxx.\\%%d\n") ;
906 break ;
907 }
908
909 CHECK_WRITE ;
910
911 textStack = stackCreate(80) ;
912 pushText(textStack, format) ;
913
914 i = -2 ; /* important flag */
915 cp = stackText(textStack,0) ;
916 while (*cp && *cp != '%') cp++ ;
917 if (*cp++ == '%')
918 {
919 while(*cp && isdigit((int)*cp)) cp++ ;
920 if (*cp++ == 'd')
921 {
922 i = 1 ;
923 while (*cp && *cp != '%') cp++ ;
924 if (!*cp)
925 goto okformat ;
926 }
927 aceOutPrint (result_out, "// Only allowed format should contain zero or once %%[number]d \n") ;
928 stackDestroy (textStack);
929 break ; /* end-case 'F' */
930 }
931 okformat:
932 added_key = 0;
933 isAddOK = FALSE;
934 cp = stackText(textStack,0);
935 while (i < lexMax(class_index) + 3 && /* obvious format obvious problem */
936 !(isAddOK = lexaddkey(messprintf(cp,i), &added_key, class_index)))
937 if (i++ < 0) break ; /* so i break at zero f format has no %d */
938
939 if (isAddOK)
940 {
941 aceOutPrint (result_out, "%s \"%s\"\n", className(added_key), name(added_key)) ;
942 setLastModified() ;
943 }
944 else
945 aceOutPrint (result_out, "// Name %s already exists\n", cp) ;
946
947 stackDestroy (textStack);
948 }
949 break; /* end-case 'F' new class format */
950
951 /******************* Parsers *****************************/
952
953 case 'K': /* "KeySet-Read" */
954 {
955 ACEIN keyset_in = NULL;
956 char *arg, *assigned_text = 0;
957
958 if (!aceInCheck(command_in, "w"))
959 /* no other word on command line - read from stdin then */
960 {
961 if (aceInIsInteractive(command_in))
962 {
963 if (look->choix & CHOIX_SERVER)
964 messcrash("KeySet-Read: server cannot open stdin");
965
966 keyset_in = aceInCreateFromStdin (TRUE, "", 0) ;
967 }
968 else
969 {
970 aceOutPrint
971 (result_out,
972 "// Sorry, I cannot read from terminal\n"
973 "// please specify the keyset on the command line (after = sign).\n"
974 "// or give a file name as command line argument\n");
975 printUsage (result_out, qmenu, option);
976 }
977 }
978 else
979 /* there is an argument on the command line */
980 {
981 arg = aceInWord (command_in);
982 if (strcmp (arg, "=") == 0)
983 /* direct assignment on command-line */
984 /* we currently have a dilemma here - it would be nice
985 * to be able to submit tace-commands with aceInSpecial ';'
986 * so a whole list of commands can be submitted on one line.
987 * This means we can do 'find sequence ; show -a' even from
988 * xace -remote.
989 * This however makes it impossible for this command
990 * to separate multiple keys by the ';' as that was
991 * already seen as the end of the whole command-line. */
992 {
993 aceInNext(command_in) ;
994 cp = aceInPos(command_in) ;
995 if (cp && strlen(cp) > 0)
996 assigned_text = strnew(cp, tmp_handle);
997 else
998 /* reset active list to empty */
999 assigned_text = strnew(" ", tmp_handle);
1000
1001 keyset_in = aceInCreateFromText (assigned_text, "", 0);
1002 /* recognise \n and ; as line-breaker
1003 * recognise \ as line-continuation
1004 * and parse // as comments
1005 */
1006 aceInSpecial (keyset_in,"\n\\/;");
1007 }
1008 else
1009 /* arg must be filename - read from file */
1010 {
1011 aceInBack (command_in);
1012 arg = aceInPath (command_in);
1013
1014 keyset_in = aceInCreateFromFile (arg, "r", "", 0);
1015
1016 if (!keyset_in)
1017 aceOutPrint (result_out,
1018 "// Sorry, I could not open file %s\n", arg) ;
1019 }
1020 }
1021
1022 if (keyset_in)
1023 {
1024 UNDO ;
1025 look->ksNew = keySetRead (keyset_in, result_out);
1026 aceInDestroy(keyset_in);
1027
1028 aceOutPrint (result_out, "// Recognised %d objects\n",
1029 keySetMax(look->ksNew)) ;
1030 }
1031 }
1032 break ;
1033
1034 /* I do not KNOW HOW THIS CASE GETS CALLED...ITS BEYOND ME.... */
1035 case '_': /* server parser */
1036 CHECK_WRITE ;
1037 UNDO ;
1038 look->ksNew = keySetCreate () ;
1039
1040 parseAceCBuffer (aceInPos (command_in), 0, result_out, look->ksNew) ;
1041 setLastModified() ;
1042
1043 break ;
1044
1045 /* admin users can get files parsed on the server side, note that the */
1046 /* client will prevent any normal parse/pparse requests coming through,*/
1047 /* they get translated into raw ace data being passed across. */
1048 /* ACTUALLY, this won't work, we will have to do the "parse =" trick */
1049 /* for this as well, except that we need to allow for the xref flag */
1050 /* which Jean does not....actually code here will be all right, its */
1051 /* the client code that doesn't do it properly.... */
1052 /* Note also that the final parse at the bottom of this will need to */
1053 /* call doParseAceIn with an alternative output, this is not correct */
1054 /* at the moment, we need to set a flag for server type comms, also */
1055 /* all the prints should be either logged or sent to the alternative */
1056 /* output place, actually looks like we just use "result_out" to do */
1057 /* this. */
1058
1059 case CMD_REMOTEPPARSE:
1060 case 'P':
1061 parseKeepGoing = TRUE ;
1062 /* fall thru */
1063
1064 case CMD_REMOTEPARSE:
1065 case 'p':
1066 {
1067 char *assigned_text = NULL;
1068 char *filename = NULL;
1069 BOOL isAssign = FALSE;
1070 ACEIN parse_io = NULL;
1071 BOOL full_stats = FALSE ;
1072
1073 look->noXref = FALSE ;
1074
1075 while (TRUE)
1076 {
1077 /* treat all -options in random order */
1078 aceInNext(command_in) ;
1079
1080 /* OK, here is where the "parse =" inserted by the client code */
1081 /* gets picked up, this allows grabbing of the text to be */
1082 /* parsed from a string, not a file, the original text came */
1083 /* from stdin on the _client_ side. */
1084 if (aceInStep(command_in,'='))
1085 {
1086 isAssign = TRUE;
1087 aceInNext(command_in) ;
1088 if ((cp = aceInPos(command_in)))
1089 assigned_text = strnew(cp, tmp_handle);
1090 break ; /* N.B. leaves loop... */
1091 }
1092
1093 if (aceInStep(command_in,'-'))
1094 {
1095 cp = aceInWord(command_in) ;
1096 if (cp && strcmp(cp,"NOXREF") == 0)
1097 look->noXref = TRUE ;
1098 else if (cp && strcmp(cp,"v") == 0)
1099 full_stats = TRUE ;
1100 continue ;
1101 }
1102
1103 /* word after the -options is a filename */
1104 if ((cp = aceInPath(command_in)))
1105 filename = strnew(cp, tmp_handle);
1106 break ;
1107 }
1108
1109 CHECK_WRITE ;
1110 UNDO ;
1111 look->ksNew = keySetCreate () ;
1112
1113 if (isAssign)
1114 {
1115 /* Can only be from client/server.... */
1116 if (assigned_text && strlen(assigned_text) > 0)
1117 {
1118 parse_io = aceInCreateFromText(assigned_text, "", 0);
1119 aceInSpecial (parse_io, "\n\t\\;/") ; /* recognize ';' as separator, allow continuation lines */
1120 }
1121
1122 if (parse_io)
1123 aceOutPrint (result_out, "// Accepting data on command line\n") ;
1124 }
1125 else if (filename)
1126 /* filename is full absolute pathname of .ace-file */
1127 {
1128
1129 if (filCheckName(filename, "", "r")) /* check to avoid warning by filopen */
1130 #if defined(WIN32) /* WIN32 has pkunzip? */
1131 /* Assumes that xyz.zip file (only) contains a xyz.ace file */
1132 {
1133 cq = filename + strlen(filename) - 4;
1134 if (cq > filename && strcmp(cq, ".zip") == 0)
1135 {
1136 cq[0] = '\0' ; /* chop off the .zip in 'filename', leaving root path */
1137 if (callScript ("pkunzip",messprintf( " -o %s >%s.err", filename, filename)) != -1)
1138 parse_io = aceInCreateFromScriptPipe ("type", messprintf(" %s.ace", filename), "", 0) ;
1139 }
1140 else
1141 parse_io = aceInCreateFromFile (filename, "r", "", 0);
1142 }
1143 #else /* UNIX has zcat and gunzip? */
1144 {
1145 cq = filename + strlen(filename) - 2;
1146 if (cq > filename && strcmp(cq, ".Z") == 0) /* a compressed file */
1147 parse_io = aceInCreateFromScriptPipe ("zcat", filename, "", 0) ;
1148 else if (cq-- && cq > filename && strcmp(cq, ".gz") == 0)
1149 parse_io = aceInCreateFromScriptPipe ("gunzip", messprintf(" -c %s", filename), "", 0) ;
1150 else
1151 parse_io = aceInCreateFromFile (filename, "r", "", 0);
1152 }
1153 #endif /* UNIX */
1154
1155 if (parse_io)
1156 {
1157 messdump("// Parsing file %s %s\n",
1158 look->noXref ? "NOXREF" : "",
1159 filename) ;
1160 aceOutPrint (result_out, "// Parsing file %s%s\n",
1161 look->noXref ? "NOXREF " : "",
1162 filename);
1163 }
1164 else
1165 aceOutPrint (result_out, "// Sorry, I could not open file %s\n", filename) ;
1166 }
1167 else
1168 /* no command argument and no assigned parse-text
1169 * we'll parse STDIN, this is for _non_ client/server only */
1170 {
1171 if (aceInIsInteractive (command_in))
1172 {
1173 if (look->choix & CHOIX_SERVER) /* shouldn't happen */
1174 messcrash("server can't open stdin");
1175
1176 parse_io = aceInCreateFromStdin (TRUE, "", 0);
1177 #if defined(WIN32)
1178 aceOutPrint (result_out, "// Type in your data in .ace format, then CTRL-Z<enter> to finish\n");
1179 #else /* UNIX */
1180 aceOutPrint (result_out, "// Type in your data in .ace format, then CTRL-D to finish\n");
1181 #endif /* UNIX */
1182 }
1183 else
1184 {
1185 aceOutPrint (result_out, "// Sorry, cannot parse from terminal input.\n");
1186 if (strstr (messGetErrorProgram(), "server") != NULL)
1187 aceOutPrint (result_out, "// Use '-ace_in' option to read data on the client side\n");
1188 }
1189
1190 if (parse_io)
1191 messdump("// Parsing stdin\n") ;
1192 }
1193
1194 /* Finally we can actually do the parsing. */
1195 if (parse_io)
1196 {
1197 /* xref stuff is a global in bssubs.c...purpose unknown to me. */
1198 BOOL oldXref = XREF_DISABLED_FOR_UPDATE ;
1199
1200 XREF_DISABLED_FOR_UPDATE = look->noXref ;
1201 doParseAceIn(parse_io, NULL, look->ksNew, parseKeepGoing, full_stats) ;
1202 XREF_DISABLED_FOR_UPDATE = oldXref;
1203
1204 setLastModified() ;
1205 }
1206
1207 parseKeepGoing = FALSE ;
1208 }
1209 break ; /* end-parse */
1210
1211 /******************* Constructors *****************************/
1212
1213 case 'C': /* Clear */
1214 UNDO ;
1215 look->ksNew = keySetCreate () ;
1216 break ;
1217
1218 case 'j': /* Dump */
1219 {
1220 BOOL isCommandLineError = FALSE;
1221 BOOL isSplit = FALSE ;
1222 char dumpDir[DIR_BUFFER_SIZE] = "" ;
1223
1224 /* get full-path of default dir, so we can report back
1225 * to user, where the dump will go if no other dir given */
1226 if (filCheckName(dumpDir, "", "rd"))
1227 {
1228 strcpy (dumpDir, cp = filGetName(dumpDir, "", "rd", 0));
1229 messfree(cp);
1230 }
1231
1232 while ((cp = aceInWord(command_in)))
1233 {
1234 if (strcmp (cp, "-s") == 0)
1235 isSplit = TRUE ;
1236 else if (strcmp(cp, "-T") == 0)
1237 dumpTimeStamps = TRUE ;
1238 else if (strcmp(cp, "-C") == 0)
1239 dumpComments = FALSE ;
1240 else
1241 {
1242 aceInBack (command_in);
1243 cp = aceInPath (command_in);
1244
1245 /* if it is a directory continue */
1246 if (filCheckName(cp, "", "rd"))
1247 {
1248 strcpy (dumpDir, cp);
1249
1250 isCommandLineError = FALSE;
1251 }
1252 else
1253 {
1254 aceOutPrint (result_out,
1255 "// Error: Could not open directory %s\n",
1256 cp) ;
1257 isCommandLineError = TRUE;
1258 }
1259 break;
1260 }
1261 }
1262
1263 if (isCommandLineError)
1264 printUsage (result_out, qmenu, option);
1265 else
1266 {
1267 aceOutPrint(result_out, "// Dump directory : %s\n",
1268 dumpDir);
1269
1270 /* make sure the directory ends with a '/' */
1271 if (dumpDir[strlen(dumpDir)-1] != SUBDIR_DELIMITER)
1272 strcat (dumpDir, SUBDIR_DELIMITER_STR);
1273 dumpAllNonInteractive (dumpDir, isSplit) ;
1274 }
1275 }
1276 break; /* end-case 'j' "Dump" */
1277
1278 case 'Q': /* Dumpread */
1279 {
1280 if (!(cp = aceInWord(command_in)))
1281 printUsage(result_out, qmenu, option);
1282 else
1283 {
1284 BOOL result = FALSE ;
1285 ACEIN parse_in = NULL ;
1286
1287 parse_in = aceInCreateFromFile(cp, "r", NULL, 0) ; /* Gets destroyed by dump call. */
1288 if (parse_in)
1289 {
1290 char *dirname = NULL ;
1291
1292 dirname = filGetDirname(cp) ;
1293 if (dirname == NULL)
1294 dirname = "." ;
1295
1296 result = dumpDoReadAll(parse_in, result_out, aceInIsInteractive(command_in),
1297 filGetDirname(cp), filGetFilename(cp)) ;
1298 }
1299 }
1300 }
1301 break; /* end-case 'Q' "Dumpread" */
1302
1303 case 'n': /* FIND */
1304 {
1305 char *class_name;
1306 KEY class_key;
1307 Stack queryStack;
1308
1309 class_name = aceInWord(command_in) ;
1310 if (!class_name)
1311 {
1312 aceOutPrint
1313 (result_out,
1314 "// Usage Find Class [name]: Construct a new current list\n"
1315 " // formed with all objects belonging to class \n"
1316 " // Example: New Sequence a\\%%.b // may give a7.b\n") ;
1317 break ;
1318 }
1319
1320 queryStack = stackCreate(80) ;
1321 class_key = 0 ;
1322 lexword2key (class_name, &class_key, _VClass) ;
1323 pushText(queryStack, class_name) ;
1324 aceInNext(command_in) ;
1325 i = 0 ; /* no name */
1326
1327 cp = aceInPos(command_in);
1328 if (cp && strlen(cp) > 0)
1329 {
1330 BOOL isProtected = FALSE ;
1331
1332 if (*cp == '"') { isProtected = TRUE ; } /* remove flanking " if there */
1333 cq = cp + strlen(cp) - 1 ;
1334 while (cq > cp && (*cq == ' ' || *cq == '\t')) *cq-- = 0 ;
1335 i = 1 ; /* a name */
1336 cq = cp - 1 ;
1337 while (*++cq)
1338 if (*cq == '?' || *cq == '*') i = 2 ; /* a regexp */
1339 if (class_key && i == 1)
1340 {
1341 stackClear (queryStack) ;
1342 pushText (queryStack, isProtected ? freeunprotect (cp) : cp) ;
1343 }
1344 else
1345 {
1346 catText (queryStack, " IS ") ;
1347 catText (queryStack, isProtected ? cp : freeunprotect(cp)) ;
1348 }
1349 }
1350
1351 UNDO ;
1352
1353 switch (i)
1354 {
1355 case 0: case 2:
1356 look->ksNew = query(0, messprintf("FIND %s", stackText(queryStack, 0))) ;
1357 break ;
1358 case 1: /* exact word */
1359 look->ksNew = keySetCreate () ;
1360 if (lexword2key (stackText(queryStack, 0), &key, class_key))
1361 keySet (look->ksNew, 0) = key ;
1362 break ;
1363 }
1364 aceOutPrint (result_out, "\n// Found %d objects in this class\n", keySetMax(look->ksNew)) ;
1365
1366 stackDestroy (queryStack);
1367 }
1368 break; /* end-case 'n' "find" */
1369
1370 case 'x': /* Performs a complex query */
1371 {
1372 char *query_text;
1373 if (!(cp = aceInWordCut(command_in, "",&c)))
1374 break ;
1375 query_text = strnew (cp, tmp_handle) ;
1376 UNDO ; /* back up ksNew into ksOld */
1377 look->ksNew = query(look->ksOld, query_text) ;
1378 aceOutPrint (result_out, "\n// Found %d objects\n", keySetMax(look->ksNew)) ;
1379 }
1380 break ;
1381
1382 case 'G':
1383 isLongGrep = TRUE ;
1384
1385 /* NOTE: fall-through */
1386
1387 case 'g': /* Match == Grep */
1388 {
1389 char *grep_query;
1390 UNDO ;
1391 if (!(cp = aceInWord(command_in)))
1392 {
1393 printUsage (result_out, qmenu, option);
1394 break ;
1395 }
1396 if (strcmp(cp, "-active") == 0)
1397 {
1398 ksTmp = look->ksOld ;
1399 aceInNext(command_in) ;
1400 }
1401 else
1402 {
1403 aceInBack(command_in) ;
1404 ksTmp = 0 ;
1405 }
1406
1407 if (!(cp = aceInWordCut(command_in,"",&c)))
1408 break ;
1409 grep_query = strnew (messprintf("*%s*",cp), tmp_handle);
1410
1411 aceOutPrint (result_out, "// I search for texts or object names matching %s\n", grep_query);
1412
1413 look->ksNew = queryGrep(ksTmp, grep_query);
1414 ksTmp = 0 ;
1415
1416 if (isLongGrep)
1417 {
1418 KEYSET ks1 = look->ksNew, ks2 = longGrep(grep_query);
1419 look->ksNew = keySetOR(ks1, ks2) ;
1420 keySetDestroy(ks1) ;
1421 keySetDestroy(ks2) ;
1422 isLongGrep = FALSE ;
1423 }
1424
1425 aceOutPrint (result_out, "\n// Found %d objects\n", keySetMax(look->ksNew)) ;
1426 }
1427 break ;
1428
1429 case 'T': /* Table-Maker */
1430 {
1431 char *tableSaveObjName = 0;
1432 Stack defStack = 0; /* read defs from text on command line */
1433 KEY tableDefKey = 0; /* read defs from obj in database */
1434 char *tableDefFileName = 0; /* read defs from file */
1435 SPREAD spread = 0;
1436 BOOL isCommandLineError = FALSE;
1437 char *tableParams = 0;
1438 BOOL useActiveKeySet = FALSE;
1439
1440 if (look->dump_out)
1441 aceOutDestroy (look->dump_out);
1442 /* by default write results to the same stream as
1443 * the rest of the command interactions */
1444 look->dump_out = aceOutCopy (result_out, 0);
1445
1446 cp = 0 ;
1447
1448 look->minN = 0 ;
1449 look->maxN = -1 ;
1450 look->beginTable = TRUE ;
1451 look->beauty = '\0'; /* default format - like 'a'(.ace-style),
1452 * but no format line */
1453 look->showTableTitle = FALSE;
1454
1455 aceInNext(command_in) ;
1456 while (aceInStep(command_in,'-'))
1457 {
1458 aceInNext(command_in) ;
1459 cp = aceInWord(command_in) ;
1460 if (!cp)
1461 break ;
1462
1463 if (strcasecmp(cp, "active") == 0)
1464 /* use active keyset as column 1 */
1465 useActiveKeySet = TRUE;
1466 else if (strcasecmp(cp, "title") == 0)
1467 /* output table-title as well as data when dumping */
1468 look->showTableTitle = TRUE;
1469 else if (strcasecmp(cp, "j") == 0) /* java style */
1470 look->beauty = 'j' ;
1471 else if (strcasecmp(cp, "k") == 0) /* fixed 'java' style for aceperl */
1472 look->beauty = 'k' ;
1473 else if (strcasecmp(cp, "p") == 0 || /* perl style */
1474 strcasecmp(cp, "perl") == 0)
1475 look->beauty = 'p' ;
1476 else if (strcasecmp(cp, "h") == 0) /* human style */
1477 look->beauty = 'h' ;
1478 else if (strcasecmp(cp, "a") == 0) /* ace style */
1479 look->beauty = 'a' ;
1480 else if (strcmp(cp, "-x") == 0) /* default xml, content only */
1481 look->beauty = 'x' ;
1482 else if (strcmp(cp, "-xclass") == 0) /* xml giving class as attribute */
1483 look->beauty = 'X' ;
1484 else if (strcmp(cp, "-xall") == 0) /* xml with class and value attributes and content */
1485 look->beauty = 'y' ;
1486 else if (strcmp(cp, "-xnocontent") == 0) /* xml with class and value attributes, no content */
1487 look->beauty = 'Y' ;
1488 else if (strcasecmp(cp, "n") == 0) /* load def from _VTable object */
1489 {
1490 cp = aceInWord(command_in); /* the name object to load defs from */
1491
1492 if (!cp || *cp == '-')
1493 {
1494 aceOutPrint (result_out, "// Error : missing obj-name for -n option\n");
1495 printUsage (result_out, qmenu, option);
1496 isCommandLineError = TRUE;
1497 break ;
1498 }
1499
1500 if (!lexword2key (cp, &tableDefKey, _VTable) ||
1501 !iskey (tableDefKey))
1502 {
1503 aceOutPrint (result_out, "// Sorry, I could not find table %s\n", cp);
1504 isCommandLineError = TRUE;
1505 break ;
1506 }
1507 }
1508 else if (strcasecmp(cp, "s") == 0) /* store in _VTable object */
1509 {
1510 cp = aceInWord(command_in); /* name of the object to store defs in */
1511
1512 if (!cp || *cp == '-')
1513 {
1514 aceOutPrint (result_out, "// Error : missing obj-name for -s option\n");
1515 printUsage (result_out, qmenu, option);
1516 isCommandLineError = TRUE;
1517 break ;
1518 }
1519
1520 tableSaveObjName = strnew(cp, tmp_handle);
1521 }
1522 else if (strcmp(cp, "C") == 0)
1523 look->beauty = 'C';
1524 else if (strcmp(cp, "c") == 0) /* max count */
1525 {
1526 if (aceInInt (command_in,&i) && i >= 0)
1527 look->maxN = i ;
1528 else
1529 {
1530 aceOutPrint (result_out, "// Error : no number after -c option\n");
1531 printUsage (result_out, qmenu, option);
1532 isCommandLineError = TRUE;
1533 break;
1534 }
1535 }
1536 else if (strcasecmp(cp, "b") == 0) /* start count */
1537 {
1538 if (aceInInt (command_in,&i) && i >= 0)
1539 look->minN = i ;
1540 else
1541 {
1542 aceOutPrint (result_out, "// Error : no number after -b option\n");
1543 printUsage (result_out, qmenu, option);
1544 isCommandLineError = TRUE;
1545 break;
1546 }
1547 }
1548 else if (strcasecmp(cp, "o") == 0) /* output results to file */
1549 {
1550 cp = aceInPath (command_in); /* the filename */
1551
1552 if (!cp)
1553 {
1554 aceOutPrint (result_out, "// Error : missing filename for -o option\n") ;
1555 printUsage (result_out, qmenu, option);
1556 isCommandLineError = TRUE;
1557 break ;
1558 }
1559 else
1560 {
1561 ACEOUT dump_out;
1562
1563 dump_out = aceOutCreateToFile (cp, "w", 0);
1564 if (!dump_out)
1565 {
1566 aceOutPrint (result_out, "// Sorry, cannot write output to %s\n",
1567 cp) ;
1568 isCommandLineError = TRUE;
1569 break ;
1570 }
1571
1572 /* replace default output stream with fileoutput */
1573 aceOutDestroy (look->dump_out);
1574 look->dump_out = dump_out;
1575 }
1576 }
1577 aceInNext(command_in) ;
1578 }
1579
1580 if (isCommandLineError) /* there was an error */
1581 break;
1582
1583 /****** get the definitions and its parameters *********/
1584
1585 if (aceInStep(command_in, '='))
1586 {
1587 /* text following the = sign is read as a literal
1588 * table definition from the command line */
1589
1590 aceInNext(command_in) ;
1591 cp = aceInPos(command_in) ;
1592
1593 if (!cp || strlen(cp) == 0)
1594 {
1595 aceOutPrint (result_out, "// Error : no definitions specified after = sign\n");
1596 break;
1597 }
1598
1599 /* create a stack containing the definition
1600 * from the command line */
1601 defStack = stackHandleCreate(100, tmp_handle) ;
1602 pushText (defStack, cp);
1603
1604 /* command-line assign cannot specify parameters
1605 * eventual params will be substituted with blank string */
1606 tableParams = strnew("", tmp_handle);
1607 }
1608 else
1609 /* read defs from object, or from file */
1610 {
1611 if (!tableDefKey)
1612 {
1613 /* the next word has to be a file name then */
1614 cp = aceInPath(command_in);
1615 if (!cp)
1616 {
1617 /* This means we neither have an object name nor a file name */
1618 aceOutPrint (result_out, "// Error, no table definition specified\n");
1619 printUsage (result_out, qmenu, option);
1620 break;
1621 }
1622
1623 tableDefFileName = strnew (cp, tmp_handle);
1624 }
1625
1626 /* get parameters */
1627 aceInNext(command_in) ;
1628 cp = aceInPos(command_in) ;
1629 tableParams = strnew (cp ? cp : "", tmp_handle) ;
1630 }
1631
1632 if (tableSaveObjName)
1633 /* if saving the definition straight to an object,
1634 * no parameter substitution should be performed */
1635 tableParams = (char*)NULL;
1636
1637 /***************************************************************/
1638 /* we have now established the source of the table definitions
1639 * - read from either defStack/tableDefKey/tableDefFilename */
1640
1641 /******** read the table definitions ************/
1642
1643 if (defStack)
1644 {
1645 spread = spreadCreateFromStack (defStack, tableParams);
1646 if (!spread)
1647 {
1648 aceOutPrint (result_out, "// Sorry, Bad table definition following = sign\n") ;
1649 break ;
1650 }
1651 }
1652 else if (tableDefFileName)
1653 {
1654 spread = spreadCreateFromFile (tableDefFileName, "r", tableParams);
1655 if (!spread)
1656 {
1657 aceOutPrint (result_out, "// Sorry, could not read table definitions file %s\n",
1658 tableDefFileName);
1659 break;
1660 }
1661 }
1662 else if (tableDefKey)
1663 {
1664 spread = spreadCreateFromObj (tableDefKey, tableParams);
1665 if (!spread)
1666 {
1667 aceOutPrint (result_out, "// Sorry, could not read table definitions obj %s\n",
1668 name(tableDefKey));
1669 break;
1670 }
1671 }
1672 else
1673 {
1674 /* We shouldn't be able to get here ... */
1675 aceOutPrint (result_out, "// Error, no table definition specified\n");
1676 printUsage (result_out, qmenu, option);
1677 break;
1678 }
1679
1680 if (tableSaveObjName) /* set by -s option to save */
1681 {
1682 KEY tableSaveKey;
1683
1684 lexaddkey (tableSaveObjName, &tableSaveKey, _VTable) ;
1685
1686 CHECK_WRITE ;
1687
1688 spreadSaveDefInObj (spread, tableSaveKey) ;
1689
1690 aceOutPrint (result_out, "\n// Table saved\n") ;
1691 spreadDestroy (spread);
1692
1693 setLastModified() ;
1694
1695 option = 'T';
1696
1697 break ;
1698 }
1699
1700
1701 /* now get the active Keyset (look->kA)
1702 * over which we can execute the table-maker definition */
1703 keySetDestroy (look->kA) ;
1704
1705 if (useActiveKeySet) /* search active keyset (option -active) */
1706 {
1707 spread->precompute = FALSE ; /* cannot store precomputed
1708 * table, if active keyset is
1709 * used for master column */
1710 spread->isActiveKeySet = TRUE ;
1711 keySetDestroy (ksTmp) ;
1712 ksTmp = spreadFilterKeySet (spread, look->ksNew) ;
1713 look->kA = keySetAlphaHeap(ksTmp, keySetMax(ksTmp)) ;
1714 keySetDestroy (ksTmp) ;
1715 }
1716 else /* search whole keyset as defined in colonne 1 */
1717 {
1718 UNDO ;
1719
1720 if (spreadGetPrecomputation(spread))
1721 /* we have the precomputed spread->values
1722 * we now also need the active keyset to
1723 * be the first column of key-values in that table */
1724 look->kA = keySetCreateFromTable(spread->values, 0);
1725 else
1726 look->kA = spreadGetMasterKeyset (spread) ;
1727
1728 /*
1729 * the tace-active-keyset should now be the first
1730 * object-typed colonne of values for the table-definitions
1731 */
1732
1733 if (!look->kA)
1734 {
1735 aceOutPrint (result_out, "// This table has a problem, "
1736 "please debug it in graphic mode\n") ;
1737 look->kA = keySetCreate () ;
1738 }
1739 look->ksNew = keySetCopy (look->kA) ;
1740 keySetSort (look->ksNew) ;
1741 }
1742
1743 if (look->spread)
1744 /* kill old spread-defs */
1745 spreadDestroy(look->spread);
1746
1747 look->spread = spread ;
1748
1749 look->lastSpread = look->minN + 1 ;
1750 if (look->maxN >= 0)
1751 look->maxN += look->minN ;
1752 look->cumulatedTableLength = 0 ;
1753
1754
1755 if (keySetMax(look->kA) == 0)
1756 break; /* no need to dump table (is empty) */
1757
1758 look->lastCommand = 'B' ;
1759 /* fall thru to case encore to dump table */
1760 }
1761
1762 case 'B': /* Table (encore) : dump the table */
1763 {
1764 int last = look->spread ? look->lastSpread : 0 ;
1765
1766 if (look->lastCommand != 'B')
1767 break ;
1768
1769 if (last && /* if we've fallen right through
1770 * here from option 'T', last == 1 */
1771 look->spread)
1772 {
1773 if (look->spread->precompute
1774 && look->spread->precomputeAvailable
1775 && look->spread->values)
1776 /* table of values was already loaded from
1777 * precomputed results, so we can just output that */
1778 {
1779 if (look->beginTable)
1780 {
1781 aceOutPrint(result_out, "\n");
1782 tableHeaderOut (result_out,
1783 look->spread->values,
1784 look->beauty) ;
1785 }
1786
1787 look->cumulatedTableLength +=
1788 tablePartOut (look->dump_out,
1789 &last, look->spread->values, '\t',
1790 look->beauty) ;
1791 }
1792 else
1793 /* we need to compute the results */
1794 {
1795 spreadCalculateOverKeySet (look->spread,
1796 look->kA, &last) ;
1797
1798 if (look->beginTable)
1799 {
1800 aceOutPrint(result_out, "\n");
1801 tableHeaderOut (result_out,
1802 look->spread->values,
1803 look->beauty) ;
1804 }
1805 look->cumulatedTableLength +=
1806 tableSliceOut(look->dump_out,
1807 0, tableMax(look->spread->values),
1808 look->spread->values,
1809 '\t', look->beauty) ;
1810 }
1811
1812 look->beginTable = FALSE ;
1813 look->showTableTitle = FALSE ;
1814 }
1815
1816 if (look->maxN >= 0 &&
1817 look->cumulatedTableLength >= look->maxN)
1818 {
1819 /* if we have a maxmimum line-count set and
1820 * the table-length has reached it, we stop dumping */
1821 last = 0 ;
1822 look->spread->precompute = FALSE ; /* cannot store ! */
1823 }
1824
1825
1826 /* finished with this table */
1827 if (last == 0)
1828 {
1829 tableFooterOut (result_out, look->spread->values, look->beauty) ;
1830
1831 if (look->spread)
1832 spreadDestroy(look->spread);
1833
1834 aceOutPrint(result_out, "\n//# %d lines in this table\n",
1835 look->cumulatedTableLength) ;
1836
1837 /* Close the file so the results get written out. */
1838 if (look->dump_out)
1839 {
1840 aceOutDestroy(look->dump_out);
1841 look->dump_out = NULL ;
1842 }
1843 }
1844
1845 look->lastSpread = last ;
1846 if (last > 0)
1847 option = 'B'; /* we can do another 'encore' */
1848 else
1849 option = 'T';
1850 }
1851 break ;
1852
1853 /******************* Server *****************************/
1854
1855 case CMD_SHUTDOWN: /* shUtdown [now] */
1856 case CMD_WHO: /* who */
1857 case CMD_PASSWD:
1858 case CMD_USER:
1859 case CMD_GLOBAL:
1860 case CMD_DOMAIN:
1861 case CMD_SERVERLOG:
1862 break ;
1863 case CMD_VERSION: /* data release */
1864 aceOutPrint (result_out,
1865 "version %d = data_version, see wspec/server.wrm\n",
1866 getDataVersion ()) ;
1867 break ;
1868
1869 /******************* Editors *****************************/
1870
1871 case 'E': /* EEdit */
1872 CHECK ;
1873 parseKeepGoing = TRUE ;
1874 case 'e': /* edit */
1875 {
1876 char *edit_text;
1877 ACEIN edit_io;
1878 Stack textStack;
1879
1880 CHECK ;
1881
1882 aceInNext(command_in) ;
1883 cp = aceInPos(command_in) ;
1884 if (!cp || !*cp)
1885 {
1886 aceOutPrint (result_out, "// Usage Edit ace_command e.g. \n"
1887 "// Edit Author Tom, adds author tom to the whole active set\n") ;
1888 parseKeepGoing = FALSE ;
1889 break ;
1890 }
1891 edit_text = strnew (cp, tmp_handle) ;
1892
1893 CHECK_WRITE ;
1894
1895 edit_io = aceInCreateFromText(edit_text, "", 0);
1896 tag = 0 ;
1897 if (aceInCard(edit_io)
1898 && (cp = aceInWord(edit_io))
1899 && strcmp (cp, "-D") == 0
1900 && (cp = aceInWord(edit_io)))
1901 tag = str2tag (cp) ;
1902
1903 aceInDestroy (edit_io) ;
1904
1905 i = keySetMax (look->ksNew) ;
1906 textStack = stackCreate (40*i) ;
1907 kp = arrp (look->ksNew, 0, KEY) - 1 ;
1908 while (kp++, i--)
1909 if (pickType(*kp) == 'B') /* don t edit array this way */
1910 {
1911 if (tag && !bIndexTag(*kp, tag)) /* no deletion needed */
1912 continue ;
1913 catText (textStack, className (*kp)) ;
1914 catText (textStack, " ") ;
1915 catText (textStack, freeprotect(name (*kp))) ;
1916 catText (textStack, "\n") ;
1917 catText (textStack, edit_text) ;
1918 catText (textStack, "\n\n") ;
1919 }
1920 ksTmp = keySetReCreate (ksTmp) ; /* do not UNDO, keep same active set */
1921
1922 if (strlen(stackText (textStack,0)) > 0)
1923 parseBuffer (stackText (textStack,0), ksTmp, parseKeepGoing) ;
1924
1925 setLastModified() ;
1926
1927 keySetDestroy (ksTmp) ;
1928 stackDestroy (textStack) ; /* it can be quite big */
1929
1930 j = keySetMax(look->ksNew) ;
1931 aceOutPrint (result_out, "// I updated %d objects with command %s\n", j, edit_text);
1932 parseKeepGoing = FALSE ;
1933 sessionMark (FALSE, 0) ;
1934 break ;
1935 } /* end-case 'e' edit */
1936
1937 case 'k': /* Kill objects */
1938 {
1939 int num;
1940
1941 CHECK ;
1942 CHECK_WRITE ;
1943
1944 num = keySetMax (look->ksNew);
1945
1946 /* interactive inputs are given the chance to break here */
1947 if (aceInIsInteractive(command_in)
1948 && !messQuery
1949 ("// Do you really want to destroy %s %d object%s",
1950 num == 1 ? "this" : "these", num, num == 1 ? "" : "s"))
1951 break ;
1952 keySetKill (look->ksNew) ;
1953
1954 setLastModified() ;
1955
1956 look->ksNew = keySetReCreate (look->ksNew) ; /* no possible undo */
1957 }
1958 break ;
1959
1960 case 'i': /* IS : keeps names matching template */
1961 {
1962 char *query_text;
1963
1964 CHECK ;
1965 UNDO ;
1966 if ((cp = aceInPos(command_in)))
1967 {
1968 query_text = strnew (messprintf ("IS %s", cp), tmp_handle);
1969 look->ksNew = query (look->ksOld, query_text) ;
1970 }
1971 else
1972 aceOutPrint (result_out,
1973 "Usage IS [ < <= > >= ~ ] template :"
1974 " keeps names from current list matching template\n") ;
1975 }
1976 break ;
1977
1978 case 'r': /* Remove : names matching template */
1979 {
1980 char *query_text;
1981
1982 CHECK ;
1983 UNDO ;
1984 if ((cp = aceInWord(command_in)))
1985 {
1986 query_text = strnew (messprintf ("IS %s", cp), tmp_handle);
1987
1988 keySetDestroy (ksTmp) ;
1989 ksTmp = query (look->ksOld, query_text);
1990 look->ksNew = keySetMINUS (look->ksOld, ksTmp) ;
1991 keySetDestroy (ksTmp) ;
1992 }
1993 else
1994 aceOutPrint (result_out, "// Usage Remove [ < <= > >= ~ ] template : remove names matching template\n") ;
1995 }
1996 break ;
1997
1998 case 't': /* Tag follow */
1999 CHECK ;
2000 UNDO ;
2001 tag = 0 ;
2002 if ((cp = aceInWord(command_in)))
2003 { /* Autocomplete to a tag */
2004 while (lexNext(0, &tag))
2005 if(pickMatch(name(tag), cp))
2006 { if (strcmp(cp, name(tag)))
2007 aceOutPrint (result_out, "// Autocompleting %s to %s\n",
2008 cp, name(tag)) ;
2009 goto goodTag ;
2010 }
2011 aceOutPrint (result_out, "// Usage Follow tag-name, change to list of objects pointed by tag \n") ;
2012 aceOutPrint (result_out, "// Sorry, Tag %s unknown \n", cp) ;
2013 aceOutPrint (result_out, "// type: model class to see the model of the class\n\n") ;
2014 break ;
2015 }
2016 else
2017 { aceOutPrint (result_out, "// Follow without tag-name, enters active keySets\n") ;
2018 }
2019 goodTag:
2020 ksTmp = keySetReCreate (ksTmp) ;
2021 for (i = 0, j = 0 ; i < keySetMax(look->ksOld) ; i++)
2022 if (class(keySet(look->ksOld,i)) == _VKeySet)
2023 { KEY k = keySet (look->ksOld, i) ;
2024 int i1 = 0 ;
2025 KEYSET ks = arrayGet (k, KEY, "k") ;
2026
2027 if (ks)
2028 {
2029 for (i1 = 0 ; i1 < keySetMax(ks) ; i1++)
2030 keySet(ksTmp, j++) = keySet(ks, i1) ;
2031 }
2032 keySetDestroy (ks) ;
2033 }
2034 look->ksNew = tag ? query (look->ksOld, messprintf(">%s", name(tag))) : keySetCreate() ;
2035 if (j)
2036 {
2037 KEYSET ks = keySetOR (look->ksNew, ksTmp) ;
2038 keySetDestroy (look->ksNew) ;
2039 look->ksNew = ks ;
2040 keySetSort (look->ksNew) ;
2041 keySetCompress (look->ksNew) ;
2042 }
2043 keySetDestroy (ksTmp) ;
2044 aceOutPrint (result_out, "\n// Found %d objects\n", keySetMax(look->ksNew)) ;
2045 break ;
2046
2047 case 'b': /* biblio */
2048 {
2049 BOOL isAbstracts = FALSE;
2050 CHECK ;
2051 UNDO ;
2052
2053 if ((cp = aceInWord(command_in)))
2054 {
2055 if (!strcmp(cp,"-abstracts"))
2056 isAbstracts = TRUE ;
2057 else
2058 aceInBack(command_in) ;
2059 }
2060 look->kA = biblioFollow (look->ksOld) ;
2061 look->ksNew = keySetCopy (look->kA) ;
2062 keySetSort (look->ksNew) ; /* ! backwards */
2063
2064 biblioDump (result_out, look->kA, isAbstracts, 80) ;
2065 aceOutPrint (result_out, "\n");
2066 }
2067 break ;
2068
2069 /******************** Dumpers *******************/
2070
2071 case 'l': /* List : names matching template */
2072 {
2073 BOOL isCommandLineError = FALSE;
2074 char *template = 0;
2075
2076 CHECK ; /* break if active keyset is empty */
2077
2078 if (look->dump_out)
2079 aceOutDestroy (look->dump_out);
2080
2081 /* by default write to same output as all other interactions */
2082 look->dump_out = aceOutCopy (result_out, 0);
2083
2084 look->minN = 0 ;
2085 look->maxN = -1 ;
2086 look->notListable = 0 ;
2087 look->beauty = 'h' ;
2088
2089 cp = aceInWord(command_in);
2090 while(cp)
2091 {
2092 if (strncmp(cp, "-a", 2) == 0) /* ace format required */
2093 look->beauty = 'a' ;
2094 else if (strcmp(cp, "-p") == 0 || strcmp(cp, "-perl") == 0) /* perl format required */
2095 look->beauty = 'p' ;
2096 else if (strncmp(cp, "-j", 2) == 0) /* java format required */
2097 look->beauty = 'j' ;
2098 else if (strncmp(cp, "-J", 2) == 0) /* java format required */
2099 look->beauty = 'J' ;
2100 else if (strncmp(cp, "-k", 2) == 0) /* fixed 'java' format for aceperl */
2101 look->beauty = 'k' ;
2102 else if (strncmp(cp, "-K", 2) == 0) /* fixed 'java' format for aceperl */
2103 look->beauty = 'K' ;
2104 else if (strncmp(cp, "-h", 2) == 0) /* human format explicitly required */
2105 look->beauty = 'H' ;
2106 else if (strncmp(cp, "-C", 2) == 0) /* Ace C binary format */
2107 look->beauty = 'C' ;
2108 else if (strcmp(cp, "-x") == 0) /* default xml, content only */
2109 look->beauty = 'x' ;
2110 else if (strcmp(cp, "-xclass") == 0) /* xml giving class as attribute */
2111 look->beauty = 'X' ;
2112 else if (strcmp(cp, "-xall") == 0) /* xml with class and value attributes and content */
2113 look->beauty = 'y' ;
2114 else if (strcmp(cp, "-xnocontent") == 0) /* xml with class and value attributes, no content */
2115 look->beauty = 'Y' ;
2116 else if (strncmp(cp, "-b", 2) == 0) /* minN required */
2117 {
2118 if (aceInInt (command_in,&i) && i >= 0)
2119 look->minN = i ;
2120 }
2121 else if (strncmp(cp, "-c", 2) == 0) /* maxN required */
2122 {
2123 if (aceInInt (command_in,&i) && i >= 0)
2124 look->maxN = i ;
2125 }
2126 else if (strncmp(cp, "-f", 2) == 0) /* -f filename */
2127 {
2128 cp = aceInPath(command_in); /* output filename */
2129 if (!cp)
2130 {
2131 aceOutPrint (result_out, "// Error : missing file name for -f option\n") ;
2132 printUsage (result_out, qmenu, option);
2133 isCommandLineError = TRUE;
2134 break ;
2135 }
2136 else
2137 {
2138 char *outfilename;
2139 ACEOUT dump_out;
2140
2141 if (look->beauty == 'h') /* change default */
2142 look->beauty = 'a' ;
2143 if (strcmp(cp, "-") == 0)
2144 goto toutpret ;
2145
2146 outfilename = strnew(cp, tmp_handle);
2147
2148 if ((dump_out = aceOutCreateToFile (outfilename, "w", 0)))
2149 {
2150 aceOutDestroy (look->dump_out); /* kill default output */
2151 look->dump_out = dump_out; /* replace with this file output */
2152 }
2153 else
2154 {
2155 aceOutPrint (result_out, "// Error : can't write to file %s\n",
2156 outfilename) ;
2157 isCommandLineError = TRUE;
2158 break ;
2159 }
2160 }
2161 }
2162 else if (strncmp(cp, "-t", 2) == 0) /* -t [template] */
2163 {
2164 if ((cp = aceInWord(command_in)))
2165 template = strnew (cp, tmp_handle) ;
2166 }
2167 else /* assume a tag */
2168 template = strnew (cp, tmp_handle) ;
2169
2170 aceInNext(command_in) ;
2171 cp = aceInWord(command_in);
2172 }
2173
2174 toutpret:
2175 if (isCommandLineError) /* there was an error */
2176 break;
2177
2178 /* We now have an output-handle look->dump_out
2179 * to which the answer is written */
2180
2181 keySetDestroy(look->kA) ;
2182 if (template)
2183 {
2184 ksTmp = arrayCreate (keySetMax (look->ksNew), KEY) ;
2185 for (i = 0, j = 0 ; i < keySetMax(look->ksNew) ; i++)
2186 if (pickMatch(name(keySet(look->ksNew,i)), template))
2187 keySet(ksTmp, j++) = keySet(look->ksNew, i) ;
2188 look->kA = keySetAlphaHeap(ksTmp, keySetMax(ksTmp)) ;
2189 keySetDestroy (ksTmp) ;
2190 }
2191 else
2192 look->kA = keySetAlphaHeap(look->ksNew, keySetMax(look->ksNew)) ;
2193
2194 look->nextObj = look->minN;
2195 if (look->maxN >= 0) look->maxN += look->minN ;
2196
2197 if (look->beauty != 'C')
2198 aceOutPrint (look->dump_out, "\nKeySet : Answer_%d\n",++nn) ;
2199 else
2200 {
2201 int n;
2202 aceOutBinary( look->dump_out, "c", 1);
2203 n = keySetMax(look->kA);
2204 aceOutBinary( look->dump_out, (char *)&n, 4);
2205 }
2206
2207 look->lastCommand = 'm';
2208 } /* end-case 'l' */
2209
2210 /* FALL THROUGH */
2211
2212 case 'm': /* List (encore) */
2213 if (look->lastCommand != 'm')
2214 break ;
2215 /* stolen from keySetDump so can count char */
2216 {
2217 int oldc = -1, newc, ii = 0 ;
2218
2219 i = look->nextObj ; kp = arrp (look->kA,i,KEY) ;
2220 if (i) oldc = class(*kp) ;
2221 ii = aceOutByte(look->dump_out) + maxChar ;
2222 for (;i < keySetMax(look->kA) ; i++, kp++)
2223 {
2224 char *class_name, *class_value ;
2225
2226 if (maxChar && aceOutByte (look->dump_out) > ii)
2227 break;
2228 if (look->maxN >=0 && i >= look->maxN)
2229 break ;
2230 if (look->beauty != 'C' && !lexIsKeyVisible (*kp))
2231 { look->notListable++ ; continue ; }
2232
2233 switch (look->beauty)
2234 {
2235
2236 case 'a':
2237 case 'p':
2238 {
2239 cp = messprintf("%s : %s\n", className(*kp),
2240 freeprotect(name(*kp))) ;
2241 aceOutPrint (look->dump_out, "%s", cp);
2242 }
2243 break ;
2244
2245 case 'C':
2246 {
2247 char buf0[2] = {0, '\n'} ;
2248 char buf [3] ;
2249
2250 if (i == look->minN)
2251 buf[0] = '>' ;
2252 else
2253 buf[0] = '.' ;
2254 if (iskey (*kp) == 2)
2255 buf[1] = 'K' ;
2256 else
2257 buf[1] = 'k' ;
2258 buf[2] = class (*kp) ;
2259 aceOutBinary ( look->dump_out, buf, 3) ;
2260 cp = name (*kp) ;
2261 aceOutBinary ( look->dump_out, cp, strlen(cp)) ;
2262 aceOutBinary ( look->dump_out, buf0, 2) ;
2263 }
2264 break ;
2265
2266 case 'j': case 'k':
2267 {
2268 cp = messprintf("?%s?%s?\n",className(*kp),
2269 freejavaprotect(name(*kp))) ;
2270 aceOutPrint (look->dump_out, "%s", cp);
2271 }
2272 break ;
2273
2274 case 'J': case 'K':
2275 {
2276 newc = class (*kp) ;
2277 if (oldc != newc)
2278 { oldc = newc ;
2279 aceOutPrint (look->dump_out, "?%s", className (*kp)) ;
2280 }
2281 else
2282 aceOutPrint (look->dump_out, "#") ;
2283 if (iskey(*kp)==2)
2284 cp = messprintf("?%s?\n",
2285 freejavaprotect(name(*kp))) ;
2286 else
2287 cp = messprintf("!%s?\n",
2288 freejavaprotect(name(*kp))) ;
2289 aceOutPrint (look->dump_out, "%s", cp);
2290 }
2291 break ;
2292
2293 case 'x': case 'X': case 'y': case 'Y': /* XML formats. */
2294 {
2295 class_name = className(*kp) ;
2296 class_value = name(*kp) ;
2297
2298 switch(look->beauty)
2299 {
2300 case 'x':
2301 cp = messprintf("<%s>%s</%s>\n", class_name, class_value, class_name) ;
2302 break ;
2303 case 'X':
2304 cp = messprintf("<%s class=\"%s\">%s</%s>\n",
2305 class_name, class_name, class_value, class_name) ;
2306 break ;
2307 case 'y':
2308 cp = messprintf("<%s class=\"%s\" value=\"%s\">%s</%s>\n",
2309 class_name, class_name, class_value, class_value, class_name) ;
2310 break ;
2311 case 'Y':
2312 cp = messprintf("<%s class=\"%s\" value=\"%s\" />\n",
2313 class_name, class_name, class_value) ;
2314 break ;
2315 }
2316 aceOutPrint (look->dump_out, "%s", cp);
2317 }
2318 break ;
2319
2320 default:
2321 {
2322 newc = class (*kp) ;
2323 if (oldc != newc)
2324 {
2325 oldc = newc ;
2326 aceOutPrint (look->dump_out, "%s:\n", className (*kp)) ;
2327 }
2328 cp = messprintf (" %s\n", name (*kp)) ;
2329 aceOutPrint (look->dump_out, "%s", cp);
2330 }
2331 break ;
2332
2333 }
2334 }
2335
2336 if (i < keySetMax(look->kA) && (look->maxN <0 || i < look->maxN))
2337 {
2338 look->nextObj = i; /* start here on more */
2339 option = 'm' ;
2340 }
2341 else
2342 {
2343 option = 'l' ;
2344
2345 if (look->beauty != 'C')
2346 {
2347 aceOutPrint (look->dump_out, "\n\n");
2348 aceOutDestroy (look->dump_out);
2349
2350 if (look->notListable)
2351 aceOutPrint (result_out, "// %d object listed, %d not listable\n",
2352 i - look->minN - look->notListable, look->notListable) ;
2353 else
2354 aceOutPrint (result_out, "// %d object listed\n", i - look->minN) ;
2355 }
2356 }
2357 }
2358 break; /* end-case 'm' list more */
2359
2360 case 'w': /* Write : now synonym to show -a -f */
2361 /* Horribleness...original code used aceInForceCard to stick the args*/
2362 /* for "show" into command_in as below: */
2363 /* aceInForceCard (command_in, messprintf ("-a -f %s", cp)) ; */
2364 /* - this doesn't work now because*/
2365 /* aceInForceCard does not work like freeforcecard does. Hence this */
2366 /* disgusting hack where we create a false command_in...sigh... */
2367 cp = aceInPos(command_in) ;
2368 tmp_command_in = aceInCreateFromText(messprintf ("-a -f %s", cp), "", 0) ;
2369
2370 /* Must empty original command_in of words so OK for next call... */
2371 while(cp)
2372 cp = aceInWord(command_in) ;
2373
2374 /* Substitute our hacked up command_in for the original. */
2375 command_in = tmp_command_in ;
2376 cp = aceInCard(command_in);
2377
2378 /* NOTE fall through setting "-a -f <rest of old line>" */
2379
2380 case 's': /* Show : [-p | -j | -k | -a | -h ] [-f filename] [tag] */
2381 {
2382 BOOL isCommandLineError = FALSE;
2383
2384 CHECK ; /* break if active list is empty */
2385
2386 keySetDestroy(look->kA) ;
2387 if (look->ksNew && keySetMax(look->ksNew))
2388 look->kA = keySetAlphaHeap(look->ksNew, keySetMax(look->ksNew)) ;
2389 if (!look->kA)
2390 {
2391 aceOutPrint (result_out, "// Active list is empty\n") ;
2392 break ;
2393 }
2394
2395 if (look->dump_out)
2396 aceOutDestroy (look->dump_out);
2397 /* by default output table results to the same stream
2398 * as the rest of the command interactions */
2399 look->dump_out = aceOutCopy (result_out, 0);
2400
2401 look->minN = 0 ;
2402 look->maxN = -1 ;
2403 look->beauty = 'h' ;
2404 look->dumpTimeStamps = FALSE ;
2405 look->dumpComments = TRUE ;
2406 if (look->showCond)
2407 {
2408 condDestroy (look->showCond) ;
2409 look->showCond = 0 ;
2410 }
2411
2412 cp = aceInWord(command_in);
2413 while (cp)
2414 {
2415 if (strcmp (cp, "-p") == 0
2416 || strcmp (cp, "-perl") == 0)
2417 look->beauty = 'p' ;
2418 else if (strcmp (cp, "-C") == 0)
2419 look->beauty = 'C' ;
2420 else if (strcmp (cp, "-j") == 0)
2421 look->beauty = 'j' ;
2422 else if (strcmp (cp, "-J") == 0)
2423 look->beauty = 'J' ;
2424 else if (strcmp (cp, "-k") == 0)
2425 look->beauty = 'k' ;
2426 else if (strcmp (cp, "-K") == 0)
2427 look->beauty = 'K' ;
2428 else if (strcmp (cp, "-a") == 0)
2429 look->beauty = 'a' ;
2430 else if (strcmp (cp, "-h") == 0)
2431 look->beauty = 'H' ;
2432 else if (strcmp(cp, "-x") == 0) /* default xml, content only */
2433 look->beauty = 'x' ;
2434 else if (strcmp(cp, "-xclass") == 0) /* xml giving class as attribute */
2435 look->beauty = 'X' ;
2436 else if (strcmp(cp, "-xall") == 0) /* xml with class and value attributes and content */
2437 look->beauty = 'y' ;
2438 else if (strcmp(cp, "-xnocontent") == 0) /* xml with class and value attributes, no content */
2439 look->beauty = 'Y' ;
2440 else if (strcmp(cp, "-T") == 0) /* time stamps */
2441 look->dumpTimeStamps = TRUE ;
2442 else if (strcmp(cp, "-C") == 0) /* comments */
2443 look->dumpComments = FALSE ;
2444 else if (strcmp(cp, "-b") == 0) /* maxN required */
2445 {
2446 if (aceInInt (command_in, &i) && i >= 0)
2447 look->minN = i ;
2448 else
2449 {
2450 aceOutPrint (result_out, "// Error: integer required after -b\n") ;
2451 isCommandLineError = TRUE;
2452 break ;
2453 }
2454 }
2455 else if (strcmp(cp, "-c") == 0) /* maxN required */
2456 {
2457 if (aceInInt (command_in, &i) && i >= 0)
2458 look->maxN = i ;
2459 else
2460 {
2461 aceOutPrint (result_out, "// Error: integer required after -c\n") ;
2462 isCommandLineError = TRUE;
2463 break ;
2464 }
2465 }
2466 else if (strcmp(cp, "-t") == 0) /* -t [tag] */
2467 {
2468 if ((cp = aceInWord(command_in)))
2469 {
2470 if (!condConstruct (cp, &look->showCond))
2471 {
2472 aceOutPrint (result_out, "// Unknown tag -t %s \n", cp) ;
2473 isCommandLineError = TRUE;
2474 keySetMax(look->kA) = 0 ;
2475 }
2476 }
2477 else
2478 {
2479 aceOutPrint (result_out, "// Error: tag name required after -t\n") ;
2480 isCommandLineError = TRUE;
2481 break ;
2482 }
2483 }
2484 else if (strcmp(cp, "-f") == 0) /* -f filename */
2485 {
2486 cq = aceInPath(command_in);
2487 if (!cq) /* filename */
2488 {
2489 aceOutPrint (result_out, "// Error: option -f requires filename argument\n") ;
2490 isCommandLineError = TRUE;
2491 break ;
2492 }
2493 else
2494 {
2495 ACEOUT dump_out;
2496
2497 dump_out = aceOutCreateToFile (cq, "w", 0);
2498 if (!dump_out)
2499 {
2500 aceOutPrint (result_out, "// Error: cannot write to %s\n",
2501 cq) ;
2502 isCommandLineError = TRUE;
2503 break ;
2504 }
2505
2506 /* replace default output stream with fileoutput */
2507 aceOutDestroy (look->dump_out);
2508 look->dump_out = dump_out;
2509 }
2510 }
2511 else /* assume a tag */
2512 {
2513 if (cp[0] == '-')
2514 {
2515 aceOutPrint (result_out, "// Error: invalid option %s \n"
2516 " // valid options are: -a -h -p -j -J -k -K -x -xclass -xall -xnocontent -C -b -c -t -f\n",cp);
2517 isCommandLineError = TRUE;
2518 break;
2519 }
2520
2521 if (!condConstruct (cp, &look->showCond))
2522 {
2523 aceOutPrint (result_out, "// Error: invalid condition %s \n", cp) ;
2524 keySetMax(look->kA) = 0 ;
2525 isCommandLineError = TRUE;
2526 }
2527 }
2528
2529 cp = aceInWord(command_in);
2530 }
2531
2532 if (isCommandLineError) /* there was an error in command line */
2533 break ;
2534
2535 aceOutPrint (look->dump_out, "\n") ;
2536
2537 look->nextObj = look->minN ;
2538 if (look->maxN >= 0) look->maxN += look->minN ;
2539 look->lastCommand = 'M' ;
2540 } /* end-case 's' - show */
2541
2542 /* NOTE: Fall-through to case m */
2543
2544 case 'M': /* Show (encore) */
2545 if (look->lastCommand != 'M')
2546 break ;
2547
2548 /* set globals */
2549 dumpTimeStamps = look->dumpTimeStamps ;
2550 dumpComments = look->dumpComments ;
2551
2552 ii = aceOutByte (look->dump_out) + maxChar ;
2553
2554 /* dump all object in the active keyset */
2555 for (j = 0 , i = look->nextObj ; i < keySetMax(look->kA) ; ++i)
2556 {
2557 if (maxChar && aceOutByte (look->dump_out) > ii)
2558 break;
2559 if (look->maxN >=0 && i >= look->maxN)
2560 break ;
2561
2562 if (look->showStatus && ! (j%1000))
2563 {
2564 ACEOUT status_out = aceOutCreateToStderr (0);
2565 aceOutPrint (status_out, "// %d: %s\n",
2566 i, name(keySet(look->kA,i))) ;
2567 tStatus (status_out) ;
2568 aceOutDestroy (status_out);
2569 }
2570
2571 dumpKeyBeautifully (look->dump_out,
2572 keySet(look->kA,i),
2573 look->beauty,
2574 look->showCond) ;
2575 }
2576
2577 /* reset globals */
2578 dumpTimeStamps = FALSE ;
2579 dumpComments = TRUE ;
2580
2581 if (i < keySetMax(look->kA) && (look->maxN <0 || i < look->maxN))
2582 {
2583 look->nextObj = i;
2584
2585 option = 'M' ;
2586 }
2587 else
2588 {
2589 int num_dumped = i - look->minN;
2590
2591 aceOutPrint (result_out,
2592 "// %d object%s dumped\n",
2593 num_dumped,
2594 num_dumped == 1 ? "" : "s") ;
2595
2596 aceOutDestroy (look->dump_out);
2597
2598 option = 's' ;
2599 }
2600 break; /* end-case 'M' more(show) */
2601
2602
2603
2604 case 'd': /* DNA : dump to screen or (-f) filename */
2605 case 'v': /* Peptide : dump to screen or (-f) filename */
2606 {
2607 char *out_file ;
2608 CHECK ;
2609
2610 keySetDestroy(look->kA) ;
2611 if (look->ksNew && keySetMax(look->ksNew))
2612 look->kA = keySetAlphaHeap(look->ksNew, keySetMax(look->ksNew)) ;
2613 if (!look->kA)
2614 {
2615 aceOutPrint (result_out, "// Active list is empty\n") ;
2616 break ;
2617 }
2618 look->nextObj = 0;
2619 look->beauty = 'a' ;
2620 look->allowMismatches = FALSE ;
2621 look->spliced = TRUE, look->cds_only = FALSE ;
2622 look->minN = look->maxN = 0 ;
2623
2624 /* User is allowed to put "-f filename" or just "filename", sadly.... */
2625 out_file = NULL ;
2626 while ((cp = aceInWord(command_in)))
2627 {
2628 if (strcmp (cp, "-p") == 0
2629 || strcmp (cp, "-perl") == 0)
2630 look->beauty = 'p' ;
2631 else if (!strcmp (cp, "-C"))
2632 look->beauty = 'C' ;
2633 else if (!strcmp (cp, "-u"))
2634 look->beauty = 'u' ;
2635 else if (strcmp (cp, "-mismatch") == 0)
2636 look->allowMismatches = TRUE ;
2637 else if (strcmp (cp, "-unspliced") == 0)
2638 look->spliced = FALSE, look->cds_only = FALSE ;
2639 else if (strcmp (cp, "-spliced") == 0)
2640 look->spliced = TRUE, look->cds_only = FALSE ;
2641 else if (strcmp (cp, "-cds") == 0)
2642 look->spliced = TRUE, look->cds_only = TRUE ;
2643 else if (!strcmp (cp, "-x1"))
2644 {
2645 if (!aceInInt (command_in, &look->minN))
2646 {
2647 if ((cp = aceInWord(command_in)))
2648 {
2649 if (!strcasecmp (cp, "begin"))
2650 look->minN = 1 ;
2651 else if (!strcasecmp (cp, "end"))
2652 look->minN = -1 ;
2653 else
2654 {
2655 aceOutPrint (result_out, "-x1 should be \"begin\" OR \"end\" OR a positive number") ;
2656 look->minN = -2 ;
2657 break ;
2658 }
2659 }
2660 }
2661 else
2662 {
2663 if (look->minN <= 0)
2664 {
2665 aceOutPrint (result_out, "-x2 should be \"begin\" OR \"end\" OR a positive number") ;
2666 look->minN = -2 ;
2667 break ;
2668 }
2669 }
2670 }
2671 else if (!strcmp (cp, "-x2"))
2672 {
2673 if (!aceInInt (command_in, &look->maxN))
2674 {
2675 if ((cp = aceInWord(command_in)))
2676 {
2677 if (!strcasecmp (cp, "begin"))
2678 look->maxN = 1 ;
2679 else if (!strcasecmp (cp, "end"))
2680 look->maxN = -1 ;
2681 else
2682 {
2683 aceOutPrint (result_out, "-x2 should be begin | end | positive number") ;
2684 look->maxN = -2 ;
2685 break ;
2686 }
2687 }
2688 }
2689 else
2690 {
2691 if (look->maxN <= 0)
2692 {
2693 aceOutPrint (result_out, "-x2 should be begin | end | positive number") ;
2694 look->maxN = -2 ;
2695 break ;
2696 }
2697 }
2698 }
2699 else if (strcmp(cp, "-f") == 0)
2700 out_file = strnew(aceInPath(command_in), 0) ;
2701 else if (cp)
2702 {
2703 aceInBack(command_in) ;
2704 out_file = strnew(aceInPath(command_in), 0) ;
2705 }
2706 }
2707
2708 if (look->minN == -2 || look->maxN == -2)
2709 break ;
2710 if (look->minN && look->minN == look->maxN)
2711 {
2712 aceOutPrint (result_out, "-x1 -x2 are equal, i cannot determine orientation") ;
2713 break ;
2714 }
2715 if ((look->minN && !look->maxN) || (!look->minN && look->maxN))
2716 {
2717 aceOutPrint (result_out, "-x1 -x2 must be both specified or absent");
2718 break ;
2719 }
2720
2721 /* by default write results to the same stream as the rest of the command interactions */
2722 if (look->dump_out)
2723 aceOutDestroy (look->dump_out);
2724 look->dump_out = aceOutCopy (result_out, 0);
2725
2726 if (out_file)
2727 {
2728 /* replace default output stream with fileoutput */
2729 ACEOUT dump_out;
2730
2731 dump_out = aceOutCreateToFile(out_file, "w", 0);
2732 messfree(out_file) ;
2733
2734 if (!dump_out)
2735 {
2736 aceOutPrint (result_out, "// Error: cannot write to %s\n", cp) ;
2737 break ;
2738 }
2739
2740 aceOutDestroy (look->dump_out);
2741 look->dump_out = dump_out;
2742 }
2743
2744 look->fastaCommand = option ;
2745 look->lastCommand = 'D' ;
2746 }
2747
2748 /****** Fall thru to case d ******/
2749
2750 case 'D':
2751 if (look->lastCommand != 'D')
2752 break ;
2753
2754 ii = aceOutByte (look->dump_out) + maxChar ;
2755 for (i = look->nextObj ; i < keySetMax(look->kA) ; ++i)
2756 {
2757 if (maxChar && aceOutByte (look->dump_out) > ii)
2758 break;
2759
2760 key = keySet (look->kA, i) ;
2761
2762 if (look->beauty == 'p')
2763 {
2764 if (look->fastaCommand == 'd')
2765 {
2766 Array dna = 0 ;
2767 if (!(dna = dnaGet (key)))
2768 continue ;
2769 dnaDecodeArray (dna) ;
2770 array(dna, arrayMax(dna), char) = 0 ; /* safeguard */
2771 if (!strlen(arrp(dna, 0, char)))
2772 {
2773 arrayDestroy (dna) ;
2774 continue ;
2775 }
2776 aceOutPrint (look->dump_out, "{ty=>dna,va=>%s,DNA=>", name(key)) ;
2777 aceOutPrint (look->dump_out, arrp(dna, 0, char)) ;
2778 aceOutPrint (look->dump_out, "}\n") ;
2779 arrayDestroy (dna) ;
2780 }
2781 else
2782 {
2783 Array pep = 0 ;
2784 if (!(pep = peptideGet (key)))
2785 continue ;
2786 pepDecodeArray (pep) ;
2787 array(pep, arrayMax(pep), char) = 0 ; /* safeguard */
2788 if (!strlen(arrp(pep, 0, char)))
2789 {
2790 arrayDestroy (pep) ;
2791 continue ;
2792 }
2793 aceOutPrint (look->dump_out, "{ty=>pep,va=>%s,PEPTIDE=>", name(key)) ;
2794 aceOutPrint (look->dump_out, arrp(pep, 0, char)) ;
2795 aceOutPrint (look->dump_out, "}\n") ;
2796 arrayDestroy (pep) ;
2797 }
2798 }
2799 else if (look->fastaCommand == 'd')
2800 {
2801 if (!look->quiet && look->beauty != 'u')
2802 aceOutPrint(look->dump_out, "\n") ;
2803
2804 dnaZoneDumpFastAKey(look->dump_out, key, look->allowMismatches,
2805 look->cds_only, look->spliced, look->minN, look->maxN,
2806 look->beauty) ;
2807 }
2808 else
2809 {
2810 pepDumpFastAKey (look->dump_out, key);
2811 }
2812 }
2813
2814 if (i < keySetMax(look->kA))
2815 {
2816 option = 'D' ;
2817 look->nextObj = i;
2818 }
2819 else
2820 {
2821 /* finished */
2822 option = look->fastaCommand ;
2823 look->fastaCommand = 0 ;
2824
2825 aceOutDestroy (look->dump_out);
2826
2827 if (!look->quiet)
2828 aceOutPrint (result_out, "// %d object dumped\n", keySetMax (look->kA));
2829 }
2830
2831 break ;
2832
2833 case 'a': /* Array class:name -
2834 * formatted acedump of A class objs */
2835 CHECK ;
2836 /*
2837 if (lexClassKey (aceInWord(fi), &key)
2838 && (fmt = aceInWord(fi))
2839 && (a = uArrayGet(key, sizeOfFormattedArray(fmt), fmt)))
2840 { dumpFormattedArray (.. should be freeOut...myFile, ...myStack, a, fmt) ;
2841 arrayDestroy (a) ;
2842 }
2843 */
2844 break ;
2845
2846 case 'L': /* Alignment : dump alignments in fancy format */
2847 {
2848 BOOL isCommandLineError = FALSE;
2849 char *outfilename;
2850 CHECK ;
2851
2852 if (look->dump_out)
2853 aceOutDestroy (look->dump_out);
2854 look->dump_out = aceOutCopy (result_out, 0);
2855
2856 outfilename = aceInPath(command_in) ;
2857 if (outfilename)
2858 {
2859 ACEOUT dump_out;
2860
2861 dump_out = aceOutCreateToFile (outfilename, "w", 0);
2862 if (dump_out)
2863 {
2864 aceOutDestroy (look->dump_out);
2865 look->dump_out = dump_out;
2866 }
2867 else
2868 {
2869 aceOutPrint (result_out,
2870 "// Error : cannot write to %s\n",
2871 outfilename);
2872 isCommandLineError = TRUE;
2873 }
2874 }
2875
2876 if (!isCommandLineError)
2877 {
2878 keySetDestroy(look->kA) ;
2879 look->kA = keySetAlphaHeap(look->ksNew, keySetMax(look->ksNew));
2880 alignDumpKeySet (look->dump_out, look->kA);
2881 }
2882
2883 aceOutDestroy (look->dump_out);
2884 }
2885 break ;
2886
2887 case '-': /* depad an assembly */
2888 {
2889 char *arg = 0;
2890
2891 cp = aceInWord(command_in) ;
2892 if (cp)
2893 arg = strnew (cp, tmp_handle);
2894
2895 CHECK_WRITE ;
2896
2897 depad (arg) ;
2898
2899 setLastModified() ;
2900 }
2901 break ;
2902
2903 case '*': /* pad an assembly */
2904 {
2905 char *arg = 0;
2906
2907 cp = aceInWord(command_in) ;
2908 if (cp)
2909 arg = strnew (cp, tmp_handle);
2910
2911 CHECK_WRITE ;
2912
2913 pad (arg) ;
2914
2915 setLastModified() ;
2916 }
2917 break ;
2918
2919 case CMD_WSPEC: /* export wspec */
2920 aceOutPrint (result_out, "\n// wspec directory exported\n") ;
2921
2922 break ;
2923 case 401: /* export layout */
2924 {
2925 char *filename = dbPathStrictFilName("wspec", "layout","wrm","r", 0);
2926 ACEIN layout_io = NULL;
2927
2928 if (filename)
2929 {
2930 layout_io = aceInCreateFromFile(filename, "r", "", 0);
2931 messfree(filename);
2932 }
2933
2934 if (layout_io)
2935 {
2936 while (aceInCard(layout_io))
2937 aceOutPrint (result_out, "%s\n", aceInPos(layout_io)) ;
2938 aceOutPrint (result_out, "\n// end of layout file\n") ;
2939 aceInDestroy(layout_io);
2940 }
2941 else
2942 aceOutPrint (result_out, "\n// Sorry, no file wspec/layout.wrm available \n") ;
2943 }
2944 break ;
2945
2946 case '#': /* GIF : entry point to drawing submenu */
2947 /* If the command set was initialised to contain this item then we */
2948 /* MUST have the code to do it. */
2949 if (!gifEntry)
2950 {
2951 messcrash("internal error: code is trying to run the gif command sub-menu,\n"
2952 "// but the sub-menu callback routine has not been set.") ;
2953 }
2954 else
2955 {
2956 ACEIN gifcommand_in = NULL ;
2957
2958 /* commands on one line separated by ';' */
2959 if (aceInCheck (command_in,"w"))
2960 {
2961 char *gif_command_text = NULL ;
2962 char *cp = NULL ;
2963
2964 gif_command_text = strnew(aceInPos(command_in), tmp_handle);
2965 cp = gif_command_text;
2966
2967 /* Nasty hack: in order to be able to get semicolons in the
2968 gifcommand, they have to be escaped with \.
2969 Strip off the \ here so that they are interpreted by
2970 gifcommand processing. */
2971 while (*cp)
2972 {
2973 if ((*cp == '\\') && (*(cp+1) == ';'))
2974 *cp = ' ';
2975 cp++;
2976 }
2977
2978 gifcommand_in = aceInCreateFromText (gif_command_text, "", 0) ;
2979 aceInSpecial (gifcommand_in, "\n\\/%;") ; /* recognize ';' as line breaker */
2980 }
2981 else if (aceInIsInteractive(command_in))
2982 {
2983 /* user entered "gif" so set up gif sub menu... */
2984 if (look->choix & CHOIX_SERVER) /* shouldn't happen */
2985 messcrash("server cannot open stdin");
2986
2987 gifcommand_in = aceInCreateFromStdin (TRUE, "", 0) ;
2988 }
2989 else
2990 {
2991 aceOutPrint(result_out, "// Please provide the full gif command on a single line\n") ;
2992
2993 gifcommand_in = aceInCreateFromText (" ? ", "", 0) ;
2994 }
2995
2996 (*gifEntry)(look->ksNew, gifcommand_in, result_out, look->quiet) ;
2997
2998 aceInDestroy (gifcommand_in) ;
2999 }
3000 break ;
3001
3002 case CMD_LASTMODIFIED: /* Return DB last modified time. */
3003 aceOutPrint(result_out, "%s\n", getLastModified()) ;
3004 break ;
3005
3006 case CMD_QUIET: /* Set quiet mode for commands a la Unix. */
3007 while ((cp = aceInWord(command_in)))
3008 {
3009 if (strcmp (cp, "-on") == 0)
3010 look->quiet = TRUE ;
3011 else if (strcmp(cp, "-off") == 0)
3012 look->quiet = FALSE ;
3013 }
3014 break ;
3015
3016 case CMD_IDSUBMENU: /* ID : entry point to id submenu */
3017 {
3018 extern void idControl (KEYSET ks, ACEIN fi, ACEOUT fo) ;
3019 idControl (look->ksNew, command_in, result_out);
3020 }
3021 break ;
3022
3023 case CMD_NEWLOG: /* Start a new log or reopen current */
3024 { /* log. */
3025 BOOL reopen = FALSE ;
3026 char *logname = NULL ;
3027
3028 cp = aceInWord(command_in);
3029 if (cp)
3030 {
3031 if (strncmp(cp, "-f", 2) == 0) /* -f log_name */
3032 {
3033 if ((cp = aceInWord(command_in)))
3034 logname = cp ;
3035 else
3036 {
3037 aceOutPrint (result_out, "// Error: option -f requires filename argument\n") ;
3038 printUsage(result_out, qmenu, option) ;
3039 break ;
3040 }
3041 }
3042 else if (strcmp(cp, "-reopen") == 0)
3043 reopen = TRUE ;
3044 else
3045 {
3046 aceOutPrint (result_out, "// Error: unknown option.\n") ;
3047 printUsage(result_out, qmenu, option) ;
3048 break ;
3049 }
3050
3051
3052 }
3053 if (reopen)
3054 {
3055 if (!sessionReopenLogFile())
3056 messerror("Could not reopen existing log file.") ;
3057 }
3058 else
3059 {
3060 if (!sessionNewLogFile(logname))
3061 messerror("Could not start a new log file.") ;
3062 }
3063 }
3064 break ;
3065
3066 /******************* Stack operations *****************************/
3067
3068 case '1': /* stack push */
3069 look->kA = keySetCopy(look->ksNew) ;
3070 aceOutPrint (result_out, "// Pushed the active set on stack\n") ;
3071 goto finStack ;
3072
3073 case '2': case '3': case '4': case '5': case '6': case '7': case '8':
3074 if (!look->nns)
3075 {
3076 aceOutPrint (result_out, "// Sorry, the stack is empty\n") ;
3077 break ;
3078 }
3079 switch (option)
3080 {
3081 case '2': /* stack pop */
3082 UNDO ;
3083 look->ksNew = pop(look->ksStack,KEYSET) ; look->nns-- ;
3084 if (look->nns)
3085 { look->kA = pop(look->ksStack,KEYSET) ; look->nns-- ;
3086 }
3087 else
3088 look->kA = 0 ;
3089 break ;
3090
3091 case '3': /* stack swap */
3092 look->kA = look->ksNew ;
3093 look->ksNew = pop(look->ksStack,KEYSET) ; look->nns-- ;
3094 break ;
3095
3096 case '4': case '5': case '6': case '7':
3097 ksTmp = pop (look->ksStack,KEYSET) ; look->nns-- ;
3098 switch (option)
3099 {
3100 case '4': /* stack AND */
3101 look->kA = keySetAND (ksTmp, look->ksNew) ;
3102 break ;
3103 case '5': /* stack OR */
3104 look->kA = keySetOR (ksTmp, look->ksNew) ;
3105 break ;
3106 case '6': /* stack XOR */
3107 look->kA = keySetXOR (ksTmp, look->ksNew) ;
3108 break ;
3109 case '7': /* stack MINUS */
3110 look->kA = keySetMINUS (ksTmp, look->ksNew) ;
3111 break ;
3112 }
3113 keySetDestroy (ksTmp) ;
3114 break ;
3115
3116 case '8': /* stack pick */
3117 if (!aceInInt (command_in, &i)){
3118 aceOutPrint (result_out,"// Error: missing integer for spick\n") ;
3119 break;
3120 }
3121 if (i < 0 ) {
3122 aceOutPrint (result_out, "// Error: spick argument must be >= 0\n") ;
3123 break ;
3124 }
3125 if (i >= look->nns ) {
3126 aceOutPrint (result_out, "// Error: spick argument greater than size of stack\n") ;
3127 break ;
3128 }
3129
3130 rot(look->ksStack,KEYSET,i);
3131 look->ksNew = pop(look->ksStack,KEYSET) ;
3132 look->nns-- ;
3133
3134 if (look->nns > 0) {
3135 look->kA = pop(look->ksStack,KEYSET) ;
3136 look->nns-- ;
3137 } else {
3138 look->kA = 0;
3139 }
3140
3141 break;
3142 }
3143
3144
3145 finStack:
3146 if (look->kA)
3147 { push(look->ksStack, look->kA, KEYSET) ; look->nns++ ;
3148 aceOutPrint (result_out, "// The stack now holds %d keyset(s), top of stack holds %d objects\n",
3149 look->nns, look->kA ? keySetMax (look->kA) : 0 ) ;
3150 look->kA = 0 ;
3151 }
3152 else if (look->nns == 0)
3153 aceOutPrint (result_out, "// The stack is now empty \n") ;
3154 break ;
3155
3156 case '9': /* subset */
3157 {
3158 int x0, nx, i, j ;
3159
3160 if (!aceInInt (command_in,&x0) ||
3161 !aceInInt (command_in,&nx) ||
3162 x0 < 1 ||
3163 nx < 1)
3164 aceOutPrint (result_out, "// Usage: subset x0 nx, : returns the subset of the alphabetic active keyset from x0>=1 of length nx") ;
3165 else
3166 {
3167 keySetDestroy(look->kA) ;
3168 if (look->ksNew)
3169 {
3170 if (x0 < 1)
3171 x0 = 1 ;
3172 if (nx > keySetMax (look->ksNew) - x0 + 1)
3173 nx = keySetMax (look->ksNew) - x0 + 1 ;
3174 look->kA = keySetAlphaHeap(look->ksNew, x0 + nx - 1) ;
3175 }
3176 else
3177 nx = 0 ;
3178 UNDO ;
3179 look->ksNew = keySetCreate () ;
3180 for (i = x0 - 1, j = 0 ; nx > 0 ; i++, j++, nx--)
3181 keySet (look->ksNew, j) = keySet (look->kA, i) ;
3182 keySetSort (look->ksNew) ;
3183 keySetDestroy (look->kA) ;
3184 }
3185 }
3186 break ;
3187
3188 /******************* named Stack operations *****************************/
3189
3190 /*
3191 * kstore - save the active keyset in a named place.
3192 * (This code copied from NCBI acedb 5/2003 by Mark S. I guessed
3193 * at the changes to make it work here.)
3194 */
3195 case 701: /* kstore name */
3196 {
3197 cp = aceInWord(command_in);
3198 if (!cp)
3199 aceOutPrint ( result_out, "// usage: kstore name : store active keyset in internal tmp space as name\n") ;
3200 else
3201 {
3202 KEYSET nKs = arrayHandleCopy (look->ksNew, NULL_HANDLE) ;
3203
3204 if (!look->kget_dict)
3205 look->kget_dict = dictHandleCreate (32, NULL_HANDLE) ;
3206 if (!look->kget_namedSets)
3207 look->kget_namedSets = arrayHandleCreate (32, Array, NULL_HANDLE) ;
3208 if (dictFind (look->kget_dict, cp, &nn) &&
3209 nn < arrayMax (look->kget_namedSets))
3210 keySetDestroy (arr (look->kget_namedSets, nn, Array)) ;
3211 else
3212 dictAdd (look->kget_dict, cp, &nn) ;
3213 array (look->kget_namedSets, nn, Array) = nKs ;
3214
3215 aceOutPrint ( result_out, "// Stored named set %s\n", cp) ;
3216 }
3217 }
3218 break ;
3219
3220 /*
3221 * kget name - load the active keyset from a named keyset.
3222 * (This code copied from NCBI acedb 5/2003 by Mark S. I guessed
3223 * at the changes to make it work here.)
3224 */
3225 case 702:
3226 {
3227 cp = aceInWord(command_in);
3228 if (!cp)
3229 aceOutPrint ( result_out, "// usage: kget name : copy named set as active keyset\n");
3230 else
3231 {
3232 int nn = 0 ;
3233 KEYSET nKs = 0 ;
3234
3235 if (look->kget_dict && dictFind (look->kget_dict, cp, &nn) &&
3236 look->kget_namedSets &&
3237 nn < arrayMax (look->kget_namedSets) &&
3238 (nKs = arr (look->kget_namedSets, nn, Array)) &&
3239 keySetExists (nKs))
3240 {
3241 UNDO ;
3242 keySetDestroy (look->kA) ;
3243 look->ksNew = keySetCopy (nKs) ;
3244 aceOutPrint ( result_out, "// Recovered named set %s\n", cp) ;
3245 }
3246 else
3247 {
3248 aceOutPrint ( result_out, "// empty set %s\n", cp) ;
3249 UNDO ;
3250 keySetDestroy (look->kA) ;
3251 look->kA = keySetCreate();
3252 }
3253 }
3254 }
3255 break ;
3256
3257 /*
3258 * kclear name - destroy a named keyset
3259 * (This code copied from NCBI acedb 5/2003 by Mark S. I guessed
3260 * at the changes to make it work here.)
3261 */
3262 case 703:
3263 {
3264 cp = aceInWord(command_in);
3265 if (!cp)
3266 aceOutPrint ( result_out, "// usage: clear named set\n") ;
3267 else
3268 {
3269 int nn = 0 ;
3270
3271 if (look->kget_dict && dictFind (look->kget_dict, cp, &nn) &&
3272 look->kget_namedSets &&
3273 nn < arrayMax (look->kget_namedSets) &&
3274 (ksTmp = arr (look->kget_namedSets, nn, Array)) &&
3275 keySetExists (ksTmp))
3276 {
3277 keySetDestroy (ksTmp) ;
3278 arr (look->kget_namedSets, nn, Array) = 0 ;
3279 aceOutPrint ( result_out, "// Cleared named set %s\n", cp) ;
3280 }
3281 else
3282 aceOutPrint (result_out, "// Sorry, unkown set %s\n", cp) ;
3283 }
3284 }
3285 break ;
3286
3287
3288 /* SMap calls...... */
3289 case CMD_SMAP:
3290 case CMD_SMAPDUMP:
3291 case CMD_SMAPLENGTH:
3292 {
3293 /* by default write to same output as all other interactions */
3294 if (look->dump_out)
3295 aceOutDestroy (look->dump_out);
3296 look->dump_out = aceOutCopy (result_out, 0);
3297
3298 if (option == CMD_SMAPDUMP)
3299 {
3300 if ((cp = aceInWord(command_in)) && strncmp(cp, "-f", 2) != 0) /* -f filename */
3301 {
3302 aceInBack(command_in) ; /* Put the first word back if its not -f */
3303 }
3304 else
3305 {
3306 if (!(cp = aceInPath(command_in))) /* output filename */
3307 {
3308 aceOutPrint (result_out, "// Error : missing file name for -f option\n") ;
3309 printUsage (result_out, qmenu, option);
3310
3311 break ;
3312 }
3313 else
3314 {
3315 char *outfilename;
3316 ACEOUT dump_out;
3317
3318 outfilename = strnew(cp, tmp_handle);
3319
3320 if ((dump_out = aceOutCreateToFile(outfilename, "w", 0)))
3321 {
3322 aceOutDestroy(look->dump_out); /* kill default output */
3323 look->dump_out = dump_out; /* replace with this file output */
3324 }
3325 else
3326 {
3327 aceOutPrint(result_out, "// Error : can't write to file %s\n",
3328 outfilename) ;
3329
3330 break ;
3331 }
3332 }
3333 }
3334 }
3335
3336 smapCmds(command_in, look->dump_out, option) ;
3337
3338 /* Close the file so the results get written out. */
3339 if (look->dump_out)
3340 {
3341 aceOutDestroy(look->dump_out);
3342 look->dump_out = NULL ;
3343 }
3344
3345 break ;
3346 }
3347
3348
3349 /******************* external calls *****************************/
3350
3351 case 'Y': /* "tace" - run command file */
3352 /* universal + non-server */
3353 {
3354 ACEIN tace_cmd_in;
3355 char *filename = NULL;
3356 char *params = NULL;
3357 BOOL isCommandLineError = FALSE;
3358
3359 if (look->choix & CHOIX_SERVER) /* not possible for server */
3360 break ;
3361
3362 /* -f filename */
3363 if ((cp = aceInWord(command_in)) &&
3364 strncmp(cp, "-f", 2) == 0)
3365 {
3366 if ((cp = aceInPath(command_in)))
3367 filename = strnew(cp, tmp_handle);
3368 else
3369 isCommandLineError = TRUE;
3370 }
3371 else
3372 isCommandLineError = TRUE;
3373
3374 if (isCommandLineError)
3375 {
3376 printUsage (result_out, qmenu, option);
3377 }
3378 else
3379 {
3380 aceInNext(command_in) ; /* get parameters */
3381 if ((cp = aceInPos(command_in)) && strlen(cp) > 0)
3382 params = strnew(cp, tmp_handle) ;
3383
3384 if ((tace_cmd_in = aceInCreateFromFile(filename, "r", params, 0)))
3385 {
3386 /* forbid sub shells */
3387 aceInSpecial (tace_cmd_in, "\n\t\\/@%");
3388
3389 aceOutPrint (result_out,
3390 "// Executing tace -f %s\n", filename) ;
3391
3392 /* why isn't the choix stuff set to choix->look ?? */
3393 commandExecute (tace_cmd_in, result_out,
3394 CHOIX_UNIVERSAL | CHOIX_NONSERVER,
3395 look->perms,
3396 look->ksNew) ;
3397 setLastModified() ;
3398 aceInDestroy (tace_cmd_in);
3399 }
3400 else
3401 {
3402 aceOutPrint (result_out,
3403 "// Sorry, I cannot read file \"%s\"\n",
3404 filename) ;
3405 }
3406 }
3407 }
3408 break ;
3409
3410 #ifdef ACEMBLY
3411 case 'A':
3412 {
3413 ACEIN acembly_in = NULL;
3414 char *filename = NULL, *params = NULL;
3415 BOOL oldIsTS = isTimeStamps ;
3416
3417 if (look->choix & CHOIX_SERVER) /* server should never open stdin */
3418 break ;
3419
3420 if ((cp = aceInWord(command_in))) /* -f filename */
3421 {
3422 if (strncmp(cp, "-f", 2) == 0)
3423 {
3424 if ((cp = aceInPath(command_in))) /* get filename */
3425 filename = strnew(cp, tmp_handle);
3426 else
3427 {
3428 aceOutPrint (result_out, "// Error : no filename after -f option\n");
3429 break;
3430 }
3431 }
3432 else
3433 aceInBack(command_in) ;
3434 }
3435
3436 aceInNext(command_in) ; /* get parameters */
3437 params = strnew(aceInPos(command_in), tmp_handle) ;
3438 CHECK_WRITE ;
3439
3440 if (filename)
3441 {
3442 acembly_in = aceInCreateFromFile (filename, "r", params, tmp_handle);
3443 if (!acembly_in)
3444 {
3445 aceOutPrint (result_out, "Sorry: I cannot read file \"%s\"\n",
3446 filename) ;
3447 break ;
3448 }
3449 }
3450 else
3451 acembly_in = aceInCreateFromStdin (aceInIsInteractive(command_in), params, tmp_handle);
3452
3453 isTimeStamps = FALSE ; /* to accelerate the code */
3454
3455 defComputeTace (acembly_in, result_out, look->ksNew) ;
3456
3457 setLastModified() ;
3458
3459 isTimeStamps = oldIsTS ;
3460 }
3461 break ;
3462 #endif /* ACEMBLY */
3463
3464 /****************************************************************/
3465
3466 case 450: /* AQL */
3467 case 451:
3468 {
3469 char *infilename = 0;
3470 char *querytext = 0;
3471 BOOL isCommandLineError = FALSE;
3472 AQL aql;
3473 TABLE *activeTable;
3474 TABLE *result;
3475
3476 if (look->dump_out)
3477 aceOutDestroy (look->dump_out);
3478 /* by default output table results to the same stream
3479 * as the rest of the command interactions */
3480 look->dump_out = aceOutCopy (result_out, 0);
3481
3482 aceInNext(command_in) ;
3483 look->beauty = 'a' ;
3484
3485 if (option == 450)
3486 while ((cp = aceInWord(command_in))) /* get aql options */
3487 {
3488 if (*cp != '-')
3489 { aceInBack (command_in) ; break ; }
3490 if (strncmp (cp, "-s", 2) == 0) /* silent */
3491 look->beauty = 0;
3492 else if (strncmp (cp, "-a", 2) == 0)
3493 look->beauty = 'a';
3494 else if (strncmp (cp, "-A", 2) == 0)
3495 look->beauty = 'A';
3496 else if (strncmp (cp, "-h", 2) == 0)
3497 look->beauty = 'h';
3498 else if (strncmp (cp, "-j", 2) == 0)
3499 look->beauty = 'j';
3500 else if (strncmp (cp, "-J", 2) == 0)
3501 look->beauty = 'J';
3502 else if (strncmp (cp, "-k", 2) == 0)
3503 look->beauty = 'k';
3504 else if (strncmp (cp, "-K", 2) == 0)
3505 look->beauty = 'K';
3506 else if (strncmp (cp, "-C", 2) == 0)
3507 look->beauty = 'C';
3508 else if (strcmp(cp, "-x") == 0) /* default xml, content only */
3509 look->beauty = 'x' ;
3510 else if (strcmp(cp, "-xclass") == 0) /* xml giving class as attribute */
3511 look->beauty = 'X' ;
3512 else if (strcmp(cp, "-xall") == 0) /* xml with class and value attributes and content */
3513 look->beauty = 'y' ;
3514 else if (strcmp(cp, "-xnocontent") == 0) /* xml with class and value attributes, no content */
3515 look->beauty = 'Y' ;
3516 else if (strncmp (cp, "-f", 2) == 0) /* -f infilename */
3517 {
3518 if ((cq = aceInPath(command_in)))
3519 infilename = strnew(cq, tmp_handle);
3520 else
3521 {
3522 aceOutPrint (result_out, "// Error: option -f requires filename argument\n") ;
3523 isCommandLineError = TRUE;
3524 break;
3525 }
3526 }
3527 else if (strncmp (cp, "-o", 2) == 0)
3528 {
3529 char *outfilename = aceInPath(command_in);
3530 if (outfilename)
3531 {
3532 ACEOUT dump_out;
3533
3534 dump_out = aceOutCreateToFile (outfilename, "w", 0);
3535 if (!dump_out)
3536 {
3537 aceOutPrint (result_out,
3538 "// Error: cannot write to %s\n",
3539 outfilename) ;
3540 isCommandLineError = TRUE;
3541 break ;
3542 }
3543
3544 /* replace default output stream with fileoutput */
3545 aceOutDestroy (look->dump_out);
3546 look->dump_out = dump_out;
3547 }
3548 else
3549 {
3550 aceOutPrint (result_out, "// Error: option -o requires filename argument\n") ;
3551 isCommandLineError = TRUE;
3552 break;
3553 }
3554 }
3555 else
3556 isCommandLineError = TRUE;
3557 } /* end while options */
3558
3559 if (isCommandLineError)
3560 {
3561 printUsage (result_out, qmenu, option);
3562 aceOutDestroy (look->dump_out);
3563 break;
3564 }
3565
3566 if (infilename)
3567 /**** get query from input file ****/
3568 {
3569
3570 ACEIN aql_in = NULL;
3571
3572 aql_in = aceInCreateFromFile (infilename, "r", "", 0);
3573 if (aql_in)
3574 {
3575 Stack s = stackCreate(100);
3576 char *line;
3577
3578 aceInSpecial(aql_in, "\n");
3579 while ((line = aceInCard(aql_in)))
3580 {
3581 catText(s, line);
3582 catText(s, " ");
3583 }
3584 querytext = strnew(stackText(s, 0), tmp_handle);
3585 stackDestroy(s);
3586
3587 aceInDestroy(aql_in);
3588 }
3589 else
3590 {
3591 aceOutPrint (result_out, "// Error : cannot open query-file %s\n",
3592 infilename);
3593 isCommandLineError = TRUE;
3594 }
3595 }
3596 else
3597 /**** get query from command line ****/
3598 {
3599 cp = aceInPos (command_in) ; /* query is rest-of-line */
3600
3601 if (option == 450)
3602 querytext = strnew(cp, tmp_handle);
3603 else if (option == 451)
3604 /* select has already been swallowed, so replace */
3605 querytext = strnew(messprintf("select %s", cp), tmp_handle);
3606 }
3607
3608 if (isCommandLineError)
3609 {
3610 printUsage (result_out, qmenu, option);
3611 aceOutDestroy (look->dump_out);
3612 break;
3613 }
3614
3615 if (strlen(querytext) == 0)
3616 {
3617 aceOutPrint (result_out, "// Error: no query\n") ;
3618 aceOutDestroy (look->dump_out);
3619 break;
3620 }
3621
3622 activeTable = tableCreateFromKeySet(look->ksNew, 0);/*ksNew, mieg aug 5, 2004 */
3623
3624 {
3625 static int aql_debug_level = -1;
3626
3627 if (aql_debug_level < 0)
3628 {
3629 char *ad_string = getenv("AQL_DEBUG_LEVEL");
3630 if (ad_string != NULL)
3631 {
3632 aql_debug_level = atoi (ad_string);
3633 }
3634 else
3635 {
3636 aql_debug_level = 0;
3637 }
3638 }
3639
3640
3641 #ifdef ED_G_NEVER_INCLUDE_THIS_CODE
3642 aql = aqlCreate(querytext, look->dump_out,
3643 look->beauty, aql_debug_level, aql_debug_level, "@active", 0) ;
3644
3645 result = aqlExecute(aql, 0, &(look->ksNew), 0, "TABLE @active", activeTable, 0);
3646 #endif /* ED_G_NEVER_INCLUDE_THIS_CODE */
3647
3648 aql = aqlCreate(querytext, look->dump_out,
3649 look->beauty, aql_debug_level, aql_debug_level, "@active", (char *)NULL) ;
3650
3651 result = aqlExecute(aql, NULL, &(look->ksNew), NULL, "TABLE @active", activeTable, (char *)NULL);
3652 }
3653
3654 tableDestroy (activeTable);
3655
3656 if (!(aqlIsError(aql)))
3657 {
3658 /* dump the table */
3659 /* beauty == 0 means silent */
3660 if (look->beauty)
3661 tableOut (look->dump_out, result, '\t', look->beauty) ;
3662
3663 UNDO ;
3664 look->ksNew = keySetCreateFromTable (result, 0) ;
3665
3666 tableDestroy (result) ;
3667 }
3668 else
3669 {
3670 aceOutPrint (result_out, "%s", aqlGetErrorReport(aql));
3671 }
3672
3673 aqlDestroy (aql) ;
3674
3675 aceOutDestroy (look->dump_out);
3676 }
3677 break; /* end-case 450/451 AQL query */
3678
3679 case 402: /* AceC system_list */
3680 {
3681 KEY tag, cl ;
3682
3683 char buf0[2] = {0, '\n'} ;
3684 char buft [2] ;
3685 unsigned int ii ;
3686 char *cp ;
3687
3688 ii = 0x12345678 ;
3689 aceOutBinary (result_out, (char*)&ii,4) ;
3690 ii = lexMax(0) ;
3691 aceOutBinary (result_out, (char*)&ii,4) ;
3692
3693 buft[0] = '>' ;
3694 buft[1] = 'g' ;
3695 for (tag = 0 ; tag < lexMax(0) ; tag++)
3696 {
3697 aceOutBinary (result_out, buft,2) ;
3698 cp = name(tag) ;
3699 aceOutBinary (result_out, cp, strlen(cp)) ;
3700 aceOutBinary (result_out, buf0,2) ;
3701 buft[0] = '.' ;
3702 }
3703 buft[1] = 'k' ;
3704 for (cl = 0 ; cl < 256 ; cl++)
3705 {
3706
3707 aceOutBinary (result_out, buft,2) ;
3708 if (pickList[cl].name)
3709 {
3710 cp = pickClass2Word (cl) ;
3711 aceOutBinary (result_out, cp, strlen(cp)) ;
3712 }
3713 aceOutBinary (result_out, buf0,2) ;
3714 }
3715 aceOutBinary (result_out, "#\n",2) ;
3716 }
3717 break;
3718
3719 default:
3720 aceOutPrint(result_out, "// Sorry, option not written yet\n") ;
3721 break;
3722 } /* end-switch (option) */
3723
3724 look->lastCommand = option ;
3725
3726 switch (option)
3727 {
3728 case 'B': case 'm': case 'M': case 'D':
3729 /* these are the options for 'more' statements */
3730 break ;
3731
3732 case 'q':
3733 /* quit command */
3734 break;
3735
3736 default:
3737 /* after any other command we output the number of objects
3738 * in the active keyset */
3739 if (!look->quiet)
3740 aceOutPrint(result_out, "// %d Active Objects\n",
3741 keySetCountVisible(look->ksNew)) ;
3742 }
3743
3744 /* Clean up. */
3745 handleDestroy (tmp_handle);
3746
3747 /* End of a disgusting hack, see case 'w' above, truly yuck... */
3748 if (tmp_command_in)
3749 aceInDestroy(tmp_command_in) ;
3750
3751
3752 return option ;
3753 } /* aceCommandExecute */
3754
3755 /************************************************************/
3756
3757 /* drop write access for this look */
3758 void aceCommandNoWrite (AceCommand look)
3759 {
3760 if (!aceCommandExists(look))
3761 messcrash("aceCommandNoWrite() - received non-magic look pointer");
3762
3763 look->noWrite = TRUE ;
3764 } /* aceCommandNoWrite */
3765
3766
3767
3768 /*************************************************************/
3769 /******************************************************************
3770 ************************ private functions ***********************
3771 ******************************************************************/
3772
3773
3774 /****************************************************************/
3775
3776 static BOOL aceCommandExists (AceCommand look)
3777 {
3778 if (look && look->magic == &AceCommand_MAGIC)
3779 return TRUE;
3780
3781 return FALSE;
3782 } /* aceCommandExists */
3783
3784 /****************************************************************/
3785
3786 static void taceTest (ACEIN command_in, ACEOUT res_out)
3787 {
3788 /* don't leave anything dodgy in here when you check it back in */
3789 KEY key;
3790 STORE_HANDLE handle = handleCreate();
3791 int length;
3792 SMap *smap;
3793 char *word = aceInWord(command_in);
3794
3795 if (lexword2key(word, &key, _VSequence))
3796 {
3797 length = sMapLength(key);
3798 smap = sMapCreate(handle, key, 1, length, NULL);
3799 sMapDump(smap, res_out);
3800 }
3801
3802 handleDestroy(handle);
3803 return ;
3804 }
3805
3806 /****************************************************************/
3807
3808 /* printUsage puts a "// " in front of every line of a command description, */
3809 /* this is different from the way "?" usually displays this information, so */
3810 /* I have added printCmd() which doesn't add the "// ". */
3811 /* */
3812 /* Note that both of these routines are expecting to find the command, */
3813 /* really they should return a BOOL if they can't find it... */
3814 /* */
3815 static void printUsage (ACEOUT fo, FREEOPT *qmenu, KEY option)
3816 {
3817 int i;
3818 char *line;
3819 ACEIN usage_io;
3820
3821 for (i = 1; i < (qmenu[0].key+1); i++)
3822 if (qmenu[i].key == option)
3823 {
3824 aceOutPrint (fo, "// Usage : \n");
3825
3826 usage_io = aceInCreateFromText(qmenu[i].text, "", 0);
3827 aceInSpecial(usage_io,"\n");
3828 while ((line = aceInCard(usage_io)))
3829 aceOutPrint (fo, "// %s\n", line);
3830 aceInDestroy(usage_io);
3831
3832 break;
3833 }
3834
3835 return;
3836 } /* printUsage */
3837
3838 static void printCmd(ACEOUT fo, FREEOPT *qmenu, KEY option)
3839 {
3840 int i ;
3841
3842 for (i = 1; i < (qmenu[0].key+1); i++)
3843 {
3844 if (qmenu[i].key == option)
3845 {
3846 aceOutPrint (fo, "%s\n", qmenu[i].text) ;
3847 break ;
3848 }
3849 }
3850
3851 return ;
3852 }
3853
3854 /****************************************************************/
3855
3856 static int getDataVersion (void)
3857 /* utility for client server */
3858 {
3859 static int version = -1 ;
3860
3861 if (version == -1)
3862 {
3863 char *cp ;
3864 int ii;
3865 ACEIN server_in = NULL;
3866 char *filename = dbPathStrictFilName("wspec", "server", "wrm", "r", 0) ;
3867
3868 if (filename)
3869 {
3870 server_in = aceInCreateFromFile (filename, "r", "", 0);
3871 messfree(filename);
3872 }
3873
3874 version = 0 ;
3875
3876 if (server_in)
3877 {
3878 while (aceInCard(server_in))
3879 {
3880 cp = aceInWord(server_in);
3881 if (cp
3882 && strcmp (cp,"DATA_VERSION") == 0
3883 && aceInInt (server_in, &ii))
3884 { version = ii ; break ; }
3885 }
3886 aceInDestroy (server_in);
3887 }
3888 }
3889
3890 return version ;
3891 } /* getDataVersion */
3892
3893 /****************************************************************/
3894
3895 static char getOnOff (ACEIN fi)
3896 {
3897 char *cp ;
3898
3899 aceInNext (fi) ;
3900 if ((cp = aceInWord (fi)))
3901 {
3902 if (strcasecmp (cp, "off") == 0)
3903 return 'f' ;
3904 else if (strcasecmp (cp, "on") == 0)
3905 return 't' ;
3906 else
3907 return '?' ;
3908 }
3909 return '\0' ;
3910 } /* getOnOff */
3911
3912 /************************************************************/
3913
3914 static BOOL doShowModel (ACEIN model_in, char *name, ACEOUT result_out)
3915 {
3916 BOOL result ;
3917 char *card, *word ;
3918
3919 aceInSpecial(model_in,"\n") ;
3920
3921 /* look for ?className minus the leading "?" */
3922 while ((card = aceInCard (model_in)))
3923 if ((word = aceInWord(model_in)) && strcmp (word+1, name) == 0)
3924 break ;
3925
3926 /* If found, output the whole class which will be terminated by a blank line,
3927 * the start of a new class, or # definition. */
3928 if (card)
3929 {
3930 do
3931 aceOutPrint (result_out, "%s\n", card) ;
3932 while ((card = aceInCard(model_in)) && aceInWord(model_in)
3933 && !(*card == '?' || *card == '#')) ;
3934
3935 result = TRUE;
3936 }
3937 else
3938 result = FALSE;
3939
3940 return result ;
3941 }
3942
3943 static void showModel (ACEOUT result_out, KEY key)
3944 {
3945 #ifdef JUNK
3946 /* this methos ios much simpler and does export the system models
3947 but the layout is a bit off and the comments are lost
3948
3949 it is equivalent to
3950 find model ?mmm ; show
3951 */
3952
3953 dumpKeyBeautifully (result_out, key, 'h', 0) ;
3954 #else
3955 ACEIN model_in = 0 ;
3956 char *filename;
3957 Stack s, sysModelStack;
3958 BOOL found = FALSE;
3959 char *linep;
3960
3961 /* sysModels() returns a braindead stack where each line is pushed
3962 * onto the stack rather than concatenated with newline at EOL */
3963 s = sysModels ();
3964 sysModelStack = stackCreate(1000); pushText (sysModelStack, "\n");
3965 stackCursor (s, 0) ;
3966 while ((linep = stackNextText(s)))
3967 {
3968 catText (sysModelStack, linep) ;
3969 catText (sysModelStack, "\n") ;
3970 }
3971 stackDestroy (s);
3972
3973 model_in = aceInCreateFromText (stackText(sysModelStack, 0), "", 0);
3974 found = doShowModel (model_in, name(key)+1, result_out);
3975 stackDestroy (sysModelStack);
3976
3977 if (!found)
3978 {
3979 filename = dbPathStrictFilName("wspec", "models", "wrm", "r", 0);
3980 if (filename)
3981 {
3982 model_in = aceInCreateFromFile (filename, "r", "", 0);
3983 messfree(filename);
3984 }
3985 else
3986 model_in = NULL;
3987
3988 if (model_in)
3989 {
3990 found = doShowModel (model_in, name(key)+1, result_out);
3991 aceInDestroy (model_in) ;
3992 }
3993 else
3994 {
3995 /* How did we get so far ? models.wrm is part of the
3996 * database specification and although an initialised
3997 * database can quite happily exist without it and rely
3998 * in internal Model-objects, the user should be using
3999 * the database without it. */
4000 messerror ("The database must have a valid model file "
4001 "in wspec/models.wrm !");
4002 return; /* no need to continue */
4003 }
4004 }
4005
4006 if (!found)
4007 {
4008 /* This is disturbing! A model for which there exists a valid key
4009 * should be found in sys-models or models.wrm !
4010 * The models.wrm file may have been updated without Read-models
4011 * But we can still dump the internal representation
4012 * of the model. */
4013 dumpKeyBeautifully (result_out, key, 'h', 0) ;
4014
4015 /* Although we have provided an answer to the user it
4016 * may still be handy to signal that things have gone stale
4017 * let's assume that sysmodels are always up-to-date and blame
4018 * all inconsistencies on models.wrm */
4019 aceOutPrint (result_out, "// File wspec/models.wrm out of sync, "
4020 "consider to 'Read-models'.");
4021 }
4022 #endif
4023
4024 return;
4025 } /* showModel */
4026
4027
4028 /* This set of routines implements a crude "last modified" timestamp for the */
4029 /* database. This WILL NOT WORK for xace because these routines are only */
4030 /* called from command.c, i.e. for the command interface. A more thorough */
4031 /* way to do this would be to call these routines from somewhere like */
4032 /* bsSave(), _but_ there are some subtleties in that route that require */
4033 /* some work to make sure its reliable... */
4034 /* */
4035 /* So, lastModified() actually holds the time, to be threadsafe this routine */
4036 /* would need to mutex etc., set/getLastModified call lastModified to set/get*/
4037 /* the last modified time. At various points in aceCommandExecute() we */
4038 /* call setLastModified(), and every time the routine receives the */
4039 /* "lastmodified" command it calls getLastModified() to return the time. */
4040 /* */
4041 void setLastModified(void)
4042 {
4043
4044 lastModified(NULL) ;
4045
4046 return ;
4047 }
4048
4049
4050 char *getLastModified(void)
4051 {
4052 char *result = NULL ;
4053
4054 lastModified(&result) ;
4055
4056 return result ;
4057 }
4058
4059 void lastModified(char **new_time)
4060 {
4061 static char time_last_modified[25] = "" ;
4062
4063 if (new_time == NULL)
4064 timeShow2(time_last_modified, timeNow()) ;
4065 else
4066 {
4067 if (!*time_last_modified)
4068 timeShow2(time_last_modified, timeNow()) ;
4069 *new_time = time_last_modified ;
4070 }
4071
4072 return ;
4073 }
4074
4075
4076 /* This function implements the various commands for the command line interface to smap. */
4077 static void smapCmds(ACEIN command_in, ACEOUT result_out, int option)
4078 {
4079
4080 switch (option)
4081 {
4082 case CMD_SMAP:
4083 {
4084 commandDoSMap(command_in, result_out) ;
4085
4086 break ;
4087 }
4088 case CMD_SMAPLENGTH:
4089 case CMD_SMAPDUMP:
4090 {
4091 KEY key = KEY_UNDEFINED ;
4092 int length ;
4093 char *word, *cmd ;
4094 STORE_HANDLE handle ;
4095
4096 handle = handleCreate() ;
4097 cmd = (option == CMD_SMAPDUMP ? "dump" : "length") ;
4098
4099 if (!(word = strnew(aceInWord(command_in), handle)))
4100 aceOutPrint(result_out, "// smap%s error: no object specified\n", cmd) ;
4101 else if (!lexClassKey(word, &key))
4102 aceOutPrint(result_out, "// smap%s error: object \"%s\" does not exist.\n", cmd, word) ;
4103 else
4104 {
4105 length = sMapLength(key) ;
4106
4107 if (option == CMD_SMAPDUMP)
4108 {
4109 SMap *smap ;
4110
4111 smap = sMapCreate(handle, key, 1, length, NULL) ;
4112 sMapDump(smap, result_out) ;
4113 }
4114 else
4115 {
4116 aceOutPrint(result_out, "SMAPLENGTH %s %d\n",
4117 nameWithClassDecorate(key), length) ;
4118 }
4119 }
4120
4121 handleDestroy(handle);
4122
4123 break ;
4124 }
4125 default:
4126 {
4127 messcrash("Bad option in smapCmds()") ;
4128 }
4129 }
4130
4131
4132 return ;
4133 }
4134
4135
4136
4137 /* Called from gif submenu currently, could be made static if we could get rid of this dependency.. */
4138 void commandDoSMap(ACEIN command_in, ACEOUT result_out)
4139 {
4140 char *cp, *cp1;
4141 BOOL gotCoords = FALSE, dump = FALSE, gotArea=FALSE;
4142 int x1, x2, y1, y2, nx1, nx2, a1, a2;
4143 STORE_HANDLE handle = handleCreate();
4144 KEY to = KEY_UNDEFINED, from = KEY_UNDEFINED ;
4145 SMap* smap = NULL;
4146 SMapKeyInfo *info;
4147 SMapStatus status;
4148 int out_x = 0, out_y = 0 ;
4149
4150
4151 messAssert(command_in && result_out) ;
4152
4153 while ((cp = strnew(aceInWord(command_in), handle)))
4154 {
4155 if (strcmp(cp, "-dump") == 0)
4156 dump = TRUE;
4157 else if (strcmp(cp, "-coords") == 0)
4158 {
4159 if (!aceInInt (command_in, &x1) || !aceInInt (command_in, &x2) || (x2 == x1))
4160 goto usage ;
4161 gotCoords = TRUE;
4162 }
4163 else if (strcmp(cp, "-area") == 0)
4164 {
4165 if (!aceInInt (command_in, &a1) || !aceInInt (command_in, &a2))
4166 goto usage ;
4167 gotArea = TRUE;
4168 }
4169 else if (strcmp(cp, "-to") == 0)
4170 {
4171 if (!(cp1 = strnew(aceInWord(command_in), handle)))
4172 goto usage ;
4173 if (!lexClassKey(cp1, &to))
4174 {
4175 aceOutPrint (result_out, "// smap error: invalid to object\n") ;
4176 goto abort ;
4177 }
4178 }
4179 else if (strcmp(cp, "-from") == 0)
4180 {
4181 if (!(cp1 = strnew(aceInWord(command_in), handle)))
4182 goto usage ;
4183 if (!lexClassKey(cp1, &from))
4184 {
4185 aceOutPrint (result_out, "// smap error: invalid from object\n") ;
4186 goto abort ;
4187 }
4188 }
4189 else
4190 goto usage ;
4191 }
4192
4193 if (from == KEY_UNDEFINED)
4194 {
4195 aceOutPrint (result_out, "// smap error: must have \"-from\"\n") ;
4196 goto abort ;
4197 }
4198
4199 if (!gotCoords)
4200 {
4201 x1 = 1 ;
4202 x2 = 0 ; /* Drop through to code to find length. */
4203 }
4204
4205 if (x2 == 0)
4206 {
4207 if (!(x2 = sMapLength(from)))
4208 {
4209 aceOutPrint (result_out,
4210 "// smap error: Cannot find length of \"-from\" object: \"%s\"\n",
4211 name(from)) ;
4212 goto abort ;
4213 }
4214 }
4215
4216
4217 if (dump)
4218 {
4219 if (!gotArea)
4220 {
4221 a1 = 1;
4222 a2 = x2 - x1 + 1;
4223 }
4224
4225 if (!(smap = sMapCreateEx(handle, from, x1, x2, a1, a2, NULL)))
4226 {
4227 aceOutPrint (result_out, "// smap error: Cannot create smap\n") ;
4228 goto abort ;
4229 }
4230
4231 sMapDump(smap, result_out);
4232 goto abort;
4233
4234 }
4235
4236
4237 /* Only create the smap over the area we want, _not_ the full smap which can be v. slow. */
4238 if (to == KEY_UNDEFINED)
4239 {
4240 if (!sMapTreeRoot(from, x1, x2, &to, &out_x, &out_y))
4241 {
4242 aceOutPrint (result_out,
4243 "// smap error: Cannot find position of \"-from %s\""
4244 " (coords %d, %d)%s%s\n",
4245 nameWithClass(from), x1, x2,
4246 (to ? " in parent " : ""),
4247 (to ? name(to) : "")) ;
4248 goto abort ;
4249 }
4250 }
4251 else
4252 {
4253 if (!sMapTreeCoords(from, x1, x2, &to, &out_x, &out_y))
4254 {
4255 aceOutPrint (result_out,
4256 "// smap error: Cannot find position of \"-from %s\""
4257 " (coords %d, %d) in \"-to %s\"\n",
4258 nameWithClass(from), x1, x2, nameWithClass(to)) ;
4259 goto abort ;
4260 }
4261 }
4262
4263 /* I have inserted this optimisation because its very expensive to do the the mapping if
4264 * the object is big as in a chromosome and the caller may not always know what the top
4265 * object is and so may ask for a mapping of the object to itself inadvertantly. */
4266 if (from == to)
4267 {
4268 y1 = nx1 = x1 ;
4269 y2 = nx2 = x2 ;
4270 }
4271 else
4272 {
4273 if (!(smap = sMapCreateEx(handle, to, 1, sMapLength(to), out_x, out_y, NULL)))
4274 {
4275 aceOutPrint (result_out, "// smap error: Cannot create smap\n") ;
4276 goto abort ;
4277 }
4278
4279 if (!(info = sMapKeyInfo(smap, from)))
4280 {
4281 aceOutPrint (result_out, "// smap error: -from object not in sMap\n") ;
4282 goto abort ;
4283 }
4284
4285 status = sMapMap(info, x1, x2, &y1, &y2, &nx1, &nx2);
4286 }
4287
4288 aceOutPrint(result_out, "SMAP %s", className(to));
4289 aceOutPrint(result_out, ":%s", name(to));
4290 aceOutPrint(result_out, " %d %d %d %d ", y1, y2, nx1, nx2);
4291 if (status == SMAP_STATUS_PERFECT_MAP)
4292 aceOutPrint(result_out, "%s", sMapErrorString(status));
4293 else
4294 {
4295 int bit = 1;
4296 char *sep="";
4297 while (bit)
4298 {
4299 char *string;
4300 if ((status & bit) && (string = sMapErrorString(status&bit)))
4301 {
4302 aceOutPrint(result_out, "%s%s", sep, string);
4303 sep = ",";
4304 }
4305 bit = bit<<1;
4306 }
4307 }
4308 aceOutPrint(result_out, "\n");
4309 handleDestroy(handle);
4310 return;
4311
4312 usage:
4313 aceOutPrint (result_out, "// smap error: usage: "
4314 "smap -coords x1 x2 -from class:object -to class:object\n") ;
4315 abort:
4316 handleDestroy(handle);
4317
4318 return ;
4319 }
4320
4321
4322
4323
4324
4325 /****************************** eof *****************************/