Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
parse.c
Go to the documentation of this file.
1 /*
2 Copyright 2021 Arizona Board of Regents on behalf of the University of Arizona.
3 
4 For commercial uses, to obtain a license to sell and or sublicense copies of
5 the Software please contact the University of Arizona at Tech Launch Arizona:
6 lewish@tla.arizona.edu.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction for non-commercial use educational and
11 research purposes, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, and to permit persons to whom the Software
13 is furnished to do so, subject to the following conditions:
14 
15 The entire above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 
26 1. Additional Required Provisions
27 
28 1.1. Arbitration. The parties agree that if a dispute arises between them
29 concerning this Agreement, the parties may be required to submit the matter to
30 arbitration pursuant to Arizona law.
31 
32 1.2. Applicable Law and Venue. This Agreement shall be interpreted pursuant
33 to the laws of the State of Arizona. Any arbitration or litigation between the
34 Parties shall be conducted in Pima County, ARIZONA, and LICENSEE hereby
35 submits to venue and jurisdiction in Pima County, ARIZONA.
36 
37 1.3. Non-Discrimination. The Parties agree to be bound by state and federal
38 laws and regulations governing equal opportunity and non-discrimination and
39 immigration.
40 
41 1.4. Appropriation of Funds. The Parties recognize that performance by
42 ARIZONA may depend upon appropriation of funds by the State Legislature of
43 ARIZONA. If the Legislature fails to appropriate the necessary funds, or if
44 ARIZONA’S appropriation is reduced during the fiscal year, ARIZONA may cancel
45 this Agreement without further duty or obligation. ARIZONA will notify
46 LICENSEE as soon as reasonably possible after it knows of the loss of funds.
47 
48 1.5. Conflict of Interest. This Agreement is subject to the provisions of
49 A.R.S. 38-511 and other conflict of interest regulations. Within three years
50 of the EFFECTIVE DATE, ARIZONA may cancel this Agreement if any person
51 significantly involved in initiating, negotiating, drafting, securing, or
52 creating this Agreement for or on behalf of ARIZONA becomes an employee or
53 consultant in any capacity of LICENSEE with respect to the subject matter of
54 this Agreement.
55 
56  */
57 
58 
59 #define INCLUDE_PARSE_FLAGS 1
60 
61 #define DEFINE_COLORS_STRINGS 1
62 
63 #include "extern.h"
64 
65 #define DVAR 1
66 #define GSVAR 2
67 
68 #define LP_IF_TEST_FALSE 0
69 #define LP_IF_TEST_TRUE 1
70 #define LP_IF_TEST_NTRUE 2
71 
72 #define LP_IF_STATE_IDLE 0
73 #define LP_IF_STATE_TRUE 1
74 #define LP_IF_STATE_FALSE 2
75 #define LP_IF_STATE_ELSE 3
76 
79 
80 char lp_if_else_buf[512];
81 
83 int recording = 0;
84 int old_quiet = 0;
86 
90 
91 char myline[4096];
92 char recordingFname[256];
93 
94 extern int getSizeOfGS();
95 extern struct GSKEY getset[];
96 
97 
98 #define VARIABLE_TYPE_NONE 0
99 #define VARIABLE_TYPE_DVAR 1
100 #define VARIABLE_TYPE_GSVAR 2
101 #define VARIABLE_TYPE_HEADER 3
102 #define VARIABLE_TYPE_PLANET 4
103 
104 
105 char last_history[4096];
106 
107 
108 /* like add_history() on;y ignores repeated commands and 'history' */
110 char *buf;
111 {
112  static int first = 1;
113 
114  if(first)
115  {
116  strcpy(last_history, "blah blah");
117  first = 0;
118  }
119 
120  if(!strcmp(buf, last_history) || !strncmp(buf, "history", 7))
121  {
122 // st_report("Ignoring repeat history");
123  return(1);
124  }
125 
126  add_history(buf);
127 
128  strcpy(last_history, buf);
129 
130 
131  return(0);
132 }
133 
134 
135 int setDFormat(buf)
136 char *buf;
137 {
138  if(isSet(PF_0_FLAG))
139  {
140  strcpy(buf, "%.0f");
141  }
142  else
143  if(isSet(PF_1_FLAG))
144  {
145  strcpy(buf, "%.1f");
146  }
147  else
148  if(isSet(PF_2_FLAG))
149  {
150  strcpy(buf, "%.2f");
151  }
152  else
153  if(isSet(PF_3_FLAG))
154  {
155  strcpy(buf, "%.3f");
156  }
157  else
158  if(isSet(PF_4_FLAG))
159  {
160  strcpy(buf, "%.4f");
161  }
162  else
163  if(isSet(PF_5_FLAG))
164  {
165  strcpy(buf, "%.5f");
166  }
167  else
168  if(isSet(PF_6_FLAG))
169  {
170  strcpy(buf, "%.6f");
171  }
172  else
173  if(isSet(PF_7_FLAG))
174  {
175  strcpy(buf, "%.7f");
176  }
177  else
178  if(isSet(PF_8_FLAG))
179  {
180  strcpy(buf, "%.8f");
181  }
182  else
183  if(isSet(PF_9_FLAG))
184  {
185  strcpy(buf, "%.9f");
186  }
187  else
188  {
189  strcpy(buf, "%.16lf");
190  }
191 
192  return(0);
193 }
194 
195 
197 {
198  int i, f = -1;
199 
200  for(i=PF_0_FLAG; i<PF_23_FLAG;i++)
201  {
202  if(isSet(i))
203  {
204  f = i-PF_0_FLAG;
205 
206  return(f);
207  }
208  }
209 
210  return(f);
211 }
212 
213 
214 int getNset()
215 {
216  int i;
217  int ret = 0;
218  struct PARSE_FLAGS *p;
219 
220  p = &args->pflags[PF_0_FLAG];
221  for(i=PF_0_FLAG;i<PF_24_FLAG;i++,p++)
222  {
223  if(p->set)
224  {
225  ret = i-PF_0_FLAG;
226 
227  break;
228  }
229  }
230 
231  return(ret);
232 }
233 
234 
235 /* Keep looking until you find a variable; if it's defined that is */
236 /* If found, populate the gv struct */
238 char *name;
239 struct QUERY_VAR *qv;
240 {
241  int r;
242  char dfmt[80];
243  struct HEAD_PARAMS *p;
244 
245  setDFormat(dfmt);
246 
247  bzero((char *)qv, sizeof(struct QUERY_VAR));
248 
249  strcpy(qv->name, name);
250 
251  /* Look for dynamic variables first */
252  r = findVariable(name);
253  if(r >= 0)
254  {
255  if(dvars[r].type == DYN_TYPE_DOUBLE)
256  {
257  sprintf(qv->buf, dfmt, *dvars[r].dval);
258  qv->dval = *dvars[r].dval;
259  qv->ntype = DOUBLE;
260  }
261  else
262  {
263  st_print(qv->buf, "%d", *dvars[r].ival);
264  qv->ival = *dvars[r].ival;
265  qv->ntype = INTEGER;
266  }
267 
269  qv->indx = r;
270 
272  {
273  st_print("%s(%s:%d): DVAR: %s = %s\n", __func__, __FILE__, __LINE__, name, qv->buf);
274  }
275 
276  return(1);
277  }
278 
279  /* Look for environmental variables next */
280  r = findGSVar(name);
281  if(r >= 0)
282  {
283  if(getset[r].type == INTEGER)
284  {
285  sprintf(qv->buf, "%d", *getset[r].iaddr);
286  qv->ival = *getset[r].iaddr;
287  }
288  else
289  if(getset[r].type == DOUBLE)
290  {
291  sprintf(qv->buf, dfmt, *getset[r].daddr);
292  qv->dval = *getset[r].daddr;
293  }
294  else
295  if(getset[r].type == CHARSTAR)
296  {
297  sprintf(qv->buf, "%s", getset[r].caddr);
298  strcpy(qv->cval, getset[r].caddr);
299  }
300 
301  qv->ntype = getset[r].type;
303  qv->indx = r;
304 
306  {
307  st_print("%s(%s:%d): GSVAR: %s = %s\n", __func__, __FILE__, __LINE__, name, qv->buf);
308  }
309 
310  return(1);
311  }
312 
313  /* Is it a header variable */
314  p = findHeaderVal(name);
315 
316  if(p)
317  {
318  switch(p->type)
319  {
320  case DOUBLE:
321  sprintf(qv->buf, dfmt, *p->dptr);
322  qv->dval = *p->dptr;
323  break;
324 
325  case CHARSTAR:
326  strncpy(qv->buf, p->cptr, p->size);
327  qv->buf[p->size] = '\0';
328  strcpy(qv->cval, qv->buf);
329 
330  break;
331  }
332 
333  qv->ntype = p->type;
335  bcopy((char *)p, (char *)&qv->phead, sizeof(struct HEAD_PARAMS));
336 
338  {
339  st_print("%s(%s:%d): HEADER: %s = %s\n", __func__, __FILE__, __LINE__, name, qv->buf);
340  }
341 
342  return(1);
343  }
344 
345  strcpy(qv->buf, name); /* Just return the token into buf */
346 
347  return(0);
348 }
349 
350 
351 
353 {
354  int i;
355  struct PARSE_STACK *p;
356 
357  p = &parse_stack[0];
358  for(i=0;i<MAX_PARSE_STACK;i++,p++)
359  {
360  p->firstArg = p->tok[0];
361  p->secondArg = p->tok[1];
362  p->thirdArg = p->tok[2];
363  p->fourthArg = p->tok[3];
364  p->fifthArg = p->tok[4];
365  p->sixthArg = p->tok[5];
366  p->seventhArg = p->tok[6];
367  p->eighthArg = p->tok[7];
368  p->ninethArg = p->tok[8];
369  p->tenthArg = p->tok[9];
370 
371  bzero((char *)p->pflags, sizeof(p->pflags));
372 
373  /* Copy only 'defined' parse_flags */
374  bcopy((char *)parse_flags, (char *)p->pflags, sizeof(parse_flags));
375  }
376 
377  args = &parse_stack[0];
378 
379  return(0);
380 }
381 
382 
383 int pushArgs()
384 {
385  recursiveLevel++;
386 
388  {
389  st_fail("recursiveLevel > MAX_PARSE_STACK");
390 
391  recursiveLevel--;
392  }
393 
395 
397  {
398  st_report("pushArgs(): recurse level %d", recursiveLevel);
399  }
400 
401  return(0);
402 }
403 
404 
405 int popArgs()
406 {
407  recursiveLevel--;
408 
409  if(recursiveLevel < 0)
410  {
411  st_fail("recursiveLevel < 0");
412 
413  recursiveLevel = 0;
414  }
415 
417 
419  {
420  st_report("popArgs(): recurse level %d", recursiveLevel);
421  }
422 
423  return(0);
424 }
425 
426 
427 /* Substitute passed string into args->tok[wt-1] */
428 /* Some assembly required */
429 
430 void subArgs(int wt, const char *fmt, ...)
431 {
432  char tbuf[4096];
433  va_list targs;
434 
436  {
437  st_report("Entering subArgs()");
438  }
439 
440 // Create the string first
441  va_start(targs, fmt);
442  vsprintf(tbuf, fmt, targs);
443  va_end(targs);
444 
445  if(strlen(tbuf) < MAX_TOKENS && (wt-1) >= 0 && (wt-1) < MAX_TOKENS)
446  {
447  if(verboseLevel & VERBOSE_PARSE)
448  {
449  st_report("Setting args->tok[%d] to %s", wt-1, tbuf);
450  }
451 
452  strcpy(args->tok[wt-1], tbuf);
453  strcpy(args->currentCmd, args->tok[wt-1]); /* Hope this doesn't screw up something else */
454  }
455 }
456 
457 
458 /* I should compress the token package here; remove the
459  empty args->tok[]'s and decriment the args->ntokens
460  Take tokens and reassemble then and re-tokenize them */
462 {
463  int i;
464  char tbuf[512], scratch[512];
465 
466  tbuf[0] = '\0';
467  for(i=0;i<args->ntokens;i++)
468  {
469  sprintf(scratch, "%s ", args->tok[i]);
470  strcat(tbuf, scratch);
471  }
472 
473  /* Re-tokenize them again */
474  args->ntokens = get_tok(tbuf);
475 
476  return(0);
477 }
478 
479 
481 char *buf;
482 {
483  int i;
484  char scratch[512];
485 
486  buf[0] = '\0';
487  for(i=0;i<args->ntokens;i++)
488  {
489  sprintf(scratch, "%s ", args->tok[i]);
490  strcat(buf, scratch);
491  }
492 
493  return(0);
494 }
495 
496 
498 {
499  int i, k, found = 1;
500  struct PARSE_FLAGS *p;
501 
503  {
504  st_report("parseFlags(): Entering with %d tokens", args->ntokens);
505  }
506 
507  for(i=0;i<args->ntokens;i++)
508  {
509  p = &args->pflags[0];
510  for(k=0;k<MAX_PARSE_FLAGS;k++,p++)
511  {
512  if(!strstr(args->tok[i], "/") && !strstr(args->tok[i], "--")) /* It's not a flag if there is no '/' or '-' char */
513  {
514  continue;
515  }
516 
517  if(verboseLevel & VERBOSE_PARSE)
518  {
519  st_report("parseFlags(): Comparing %s to %s", args->tok[i], p->token);
520  }
521 
522  if(!strcmp(args->tok[i], p->token))
523  {
524  if(verboseLevel & VERBOSE_PARSE)
525  {
526  st_report("parseFlags(): Flag: %s Set", p->token);
527  }
528 
529  p->set++;
530 
531  if(!p->order) /* The order I found them */
532  {
533  p->order = found;
534 
535  found++;
536  }
537 
538  if(p->index == PF_Q_FLAG)
539  {
540  if(verboseLevel & VERBOSE_PARSE)
541  {
542  st_report("parseFlags(): Setting Quiet to True");
543  }
544 
545  old_quiet = stquiet;
546  stquiet = 1;
547  }
548 
549  /* Clear out the parsed flags */
550  strcpy(args->tok[i], " ");
551 
552  break;
553  }
554  }
555  }
556 
558 
559  if(verboseLevel & VERBOSE_PARSE)
560  {
561  st_report("parseFlags(): We now have %d tokens", args->ntokens);
562  }
563 
564  if(verboseLevel & VERBOSE_PARSE)
565  {
566  p = &args->pflags[0];
567  for(k=0;k<MAX_PARSE_FLAGS;k++,p++)
568  {
569  if(p->set)
570  {
571  st_print("%16.16s is Set\n", p->name);
572  }
573  }
574 
575  st_report("parseFlags(): Leaving");
576  }
577 
578  return(0);
579 }
580 
581 
582 
584 {
585  int i;
586  struct PARSE_FLAGS *p;
587 
588  p = &args->pflags[0];
589  for(i=0;i<MAX_PARSE_FLAGS;i++,p++)
590  {
591  p->set = 0;
592  p->order = 0;
593  }
594 
595  stquiet = old_quiet;
596 
597  return(0);
598 }
599 
600 
601 /* Return the first flag set; else NULL */
603 {
604  int i;
605  struct PARSE_FLAGS *p;
606 
607  p = &args->pflags[0];
608  for(i=0;i<MAX_PARSE_FLAGS;i++,p++)
609  {
610  if(p->order == 1)
611  {
612  return(p);
613  }
614  }
615 
616  return((struct PARSE_FLAGS *)NULL);
617 }
618 
619 
620 /* Shift all 'set' flags down one place in their 'order' */
621 /* Decriment 'set' count for the previous first flag */
623 {
624  int i;
625  struct PARSE_FLAGS *p;
626 
627  p = &args->pflags[0];
628  for(i=0;i<MAX_PARSE_FLAGS;i++,p++)
629  {
630  if(p->order)
631  {
632  if(p->order == 1)
633  {
634  p->set--;
635  }
636 
637  p->order--;
638  }
639  }
640 
641  return(0);
642 }
643 
644 
645  /* Is the specified flag set ?? */
646 int isSet(flag)
647 int flag;
648 {
650  {
651  if(flag <= PF_Z_FLAG)
652  {
653  st_report("Checking if /%c flag set", (char)(flag+97));
654  }
655  }
656 
657  return(args->pflags[flag].set);
658 }
659 
660 
661  /* Is the specified flag the first flag ?? */
662 int isFirst(flag)
663 int flag;
664 {
665  return(args->pflags[flag].order == 1);
666 }
667 
668  /* Is the specified flag NOT set ?? */
669 int notSet(flag)
670 int flag;
671 {
673  {
674  if(flag <= PF_Z_FLAG)
675  {
676  st_report("Checking if /%c flag NOT set", (char)(flag+97));
677  }
678  }
679 
680  return(!args->pflags[flag].set);
681 }
682 
683 
684  /* Set the specified flag */
685 int setFlag(flag, state)
686 int flag, state;
687 {
688  if(flag <= PF_Z_FLAG)
689  {
690  args->pflags[flag].set = state;
691  }
692  else
693  {
694  return(1);
695  }
696 
697  return(0);
698 }
699 
700 
702 {
703  int i, ngs;
704  struct GSKEY *pgs;
705  char hbuf[256];
706 
707  ngs = getSizeOfGS();
708 
709  pgs = &getset[0];
710  for(i=0;i<ngs;i++,pgs++)
711  {
712  if(!fetchNGSHelp(pgs->name, hbuf))
713  {
714  st_print("%18.18s: %s \n", pgs->name, hbuf);
715  }
716  else
717  {
718  st_print("%18.18s: \n", pgs->name);
719  }
720  }
721 
722  st_print("\n");
723 
724  return(0);
725 }
726 
727 
729 {
730  int i, j, ngs, key=0;
731  struct DYNAMIC_VARIABLES *p;
732  struct GSKEY *pgs;
733  char hbuf[256], tbuf[512], fbuf[256], kbuf[256];
734 
735  if(isSet(PF_K_FLAG))
736  {
737  if(strlen(args->secondArg))
738  {
739  key = 1;
740  strcpy(kbuf, args->secondArg);
741  }
742  else
743  {
744  printH_FlagHelp("dlist");
745  return(0);
746  }
747  }
748 
749  st_print("Defined Dynamic Variables\n");
750 
751  p = &dvars[0];
752  for(i=0;i<MAX_DYN_VARS;i++,p++)
753  {
754  if(p->type == DYN_TYPE_INT)
755  {
756  if(key)
757  {
758  if(strcasestr(p->name, kbuf))
759  {
760  st_print("D: %18.18s: (int) %10d\n", p->name, *p->ival);
761  }
762  }
763  else
764  {
765  st_print("D: %18.18s: (int) %10d\n", p->name, *p->ival);
766  }
767  }
768  else
769  if(p->type == DYN_TYPE_DOUBLE)
770  {
771  if(fabs(*p->dval) < 0.0001 || fabs(*p->dval) > 9999999.9)
772  {
773  if(key)
774  {
775  if(strcasestr(p->name, kbuf))
776  {
777  st_print("D: %18.18s: (double) %14.4e\n", p->name, *p->dval);
778  }
779  }
780  else
781  {
782  st_print("D: %18.18s: (double) %14.4e\n", p->name, *p->dval);
783  }
784  }
785  else
786  {
787  if(key)
788  {
789  if(strcasestr(p->name, kbuf))
790  {
791  st_print("D: %18.18s: (double) %14.6f\n", p->name, *p->dval);
792  }
793  }
794  else
795  {
796  st_print("D: %18.18s: (double) %14.6f\n", p->name, *p->dval);
797  }
798  }
799  }
800  }
801 
802  ngs = getSizeOfGS();
803 
804  pgs = &getset[0];
805  for(i=0;i<ngs;i++,pgs++)
806  {
807  fetchNGSHelp(pgs->name, hbuf);
808 
809  switch(pgs->type)
810  {
811  case INTEGER:
812  if(pgs->size > 1)
813  {
814  for(j=0;j<pgs->size;j++)
815  {
816  if(!strncmp(pgs->name, "badchans", 8))
817  {
818  if(pgs->iaddr[j] == -1)
819  {
820  continue;
821  }
822 
823  if(pgs->iaddr[j] != 0 || j == 0)
824  {
825  if(key)
826  {
827  if(strcasestr(pgs->name, kbuf))
828  {
829  st_print("E: %18.18s[%03d]: (int) %10d\n", pgs->name, j, pgs->iaddr[j]);
830  }
831  }
832  else
833  {
834  st_print("E: %18.18s[%03d]: (int) %10d\n", pgs->name, j, pgs->iaddr[j]);
835  }
836  }
837  }
838  }
839  }
840  else
841  {
842  if(key)
843  {
844  if(strcasestr(pgs->name, kbuf))
845  {
846  st_print("E: %18.18s: (int) %10d %s\n", pgs->name, *pgs->iaddr, hbuf);
847  }
848  }
849  else
850  {
851  st_print("E: %18.18s: (int) %10d %s\n", pgs->name, *pgs->iaddr, hbuf);
852  }
853  }
854  break;
855 
856  case DOUBLE:
857  if(pgs->size > 1)
858  {
859  for(j=0;j<pgs->size;j++)
860  {
861  if(!strncmp(pgs->name, "ignoredScans", 12))
862  {
863  if(pgs->daddr[j] != 0.0 || j == 0)
864  {
865  if(key)
866  {
867  if(strcasestr(pgs->name, kbuf))
868  {
869  st_print("E: %18.18s[%03d]: (double) %14.2f\n", pgs->name, j, pgs->daddr[j]);
870  }
871  }
872  else
873  {
874  st_print("E: %18.18s[%03d]: (double) %14.2f\n", pgs->name, j, pgs->daddr[j]);
875  }
876  }
877  }
878  }
879  }
880  else
881  {
882  if(fabs(*pgs->daddr) < 0.0001 || fabs(*pgs->daddr) > 9999999.9)
883  {
884  if(key)
885  {
886  if(strcasestr(pgs->name, kbuf))
887  {
888  st_print("E: %18.18s: (double) %14.4e %s\n", pgs->name, *pgs->daddr, hbuf);
889  }
890  }
891  else
892  {
893  st_print("E: %18.18s: (double) %14.4e %s\n", pgs->name, *pgs->daddr, hbuf);
894  }
895  }
896  else
897  {
898  if(key)
899  {
900  if(strcasestr(pgs->name, kbuf))
901  {
902  st_print("E: %18.18s: (double) %14.4f %s\n", pgs->name, *pgs->daddr, hbuf);
903  }
904  }
905  else
906  {
907  st_print("E: %18.18s: (double) %14.4f %s\n", pgs->name, *pgs->daddr, hbuf);
908  }
909  }
910 
911  }
912  break;
913 
914  case CHARSTAR:
915  if(!strcmp(pgs->name, "filename") ||
916  !strcmp(pgs->name, "gfilename") ||
917  !strcmp(pgs->name, "savefile") ||
918  !strcmp(pgs->name, "userfile"))
919  {
920  strcpy(tbuf, pgs->caddr);
921  strcpy(fbuf, basename(tbuf));
922  strcpy(tbuf, fbuf);
923  }
924  else
925  {
926  strcpy(tbuf, pgs->caddr);
927  }
928 
929  if(key)
930  {
931  if(strcasestr(pgs->name, kbuf))
932  {
933  st_print("E: %18.18s (char) %20s %s\n", pgs->name, tbuf, hbuf);
934  }
935  }
936  else
937  {
938  st_print("E: %18.18s (char) %20s %s\n", pgs->name, tbuf, hbuf);
939  }
940  break;
941  }
942  }
943 
944  return(0);
945 }
946 
947 
949 char *name;
950 {
951  int i, ngs;
952 
953  ngs = getSizeOfGS();
954 
955  for(i=0;i<ngs;i++)
956  {
957  if(!strcmp(getset[i].name, name))
958  {
959  return(i);
960  }
961  }
962 
963  return(-1);
964 }
965 
966 
968 {
969  int i;
970  struct DYNAMIC_VARIABLES *p;
971 
972  p = &dvars[0];
973  for(i=0;i<MAX_DYN_VARS;i++,p++)
974  {
975  p->dval = &dvars_holders[i];
976  p->ival = &ivars_holders[i];
977  }
978 
979  return(0);
980 }
981 
982 
983 
984 int findVariable(var)
985 char *var;
986 {
987  int i;
988 
989  for(i=0;i<MAX_DYN_VARS;i++)
990  {
991  if(!strcmp(dvars[i].name, var))
992  {
993  return(i);
994  }
995  }
996 
997  return(-1);
998 }
999 
1000 
1002 {
1003  int i;
1004 
1005  if(strlen(args->secondArg))
1006  {
1007  for(i=CGM->ndvars_used;i<MAX_DYN_VARS;i++)
1008  {
1009  if(!strcasecmp(dvars[i].name, args->secondArg))
1010  {
1011  st_report("Clearing Variable: %s", dvars[i].name);
1012 
1013  strcpy(dvars[i].name, "");
1014  dvars[i].type = 0;
1015 
1016  return(0);
1017  }
1018  }
1019 
1020  st_fail("Variable %s either not found or protected", args->secondArg);
1021  }
1022 
1023  return(1);
1024 }
1025 
1026 
1028 {
1029  int i;
1030 
1031  for(i=CGM->ndvars_used;i<MAX_DYN_VARS;i++)
1032  {
1033  if(dvars[i].type)
1034  {
1035  st_report("Clearing Variable: %s", dvars[i].name);
1036 
1037  strcpy(dvars[i].name, "");
1038  dvars[i].type = 0;
1039 
1040  return(0);
1041  }
1042  }
1043 
1044  return(1);
1045 }
1046 
1047 
1048 /* Not previously defined; make a new one */
1050 char *var;
1051 int type;
1052 {
1053  int i;
1054 
1055  /* Check for already defined variable */
1056  for(i=CGM->ndvars_used;i<MAX_DYN_VARS;i++)
1057  {
1058  if(!strcmp(dvars[i].name, var))
1059  {
1060  // st_warn("Variable %s, already defined", var);
1061  return(i);
1062  }
1063  }
1064 
1065 /* Find the first unused slot */
1066  for(i=CGM->ndvars_used;i<MAX_DYN_VARS;i++)
1067  {
1068  if(!dvars[i].type)
1069  {
1070  strcpy(dvars[i].name, var);
1071 
1072  dvars[i].type = type;
1073 
1074  st_report("New Variable: %s", var);
1075 
1076  return(i);
1077  }
1078  }
1079 
1080  FAIL_INFO("Dynamic Variable slot all Used");
1081 
1082  return(-1);
1083 }
1084 
1085 
1087 {
1088  if(strlen(args->secondArg))
1089  {
1091  }
1092  else
1093  {
1094  FAIL_INFO("Usage: int integer_variable_name");
1095  }
1096 
1097  return(0);
1098 }
1099 
1100 
1102 {
1103  if(strlen(args->secondArg))
1104  {
1106  }
1107  else
1108  {
1109  FAIL_INFO("Usage: double double_variable_name");
1110  }
1111 
1112  return(0);
1113 }
1114 
1115 
1116 
1117 #define FORMAT_NORMAL 0
1118 #define FORMAT_EXP 1
1119 #define FORMAT_HEX 2
1120 
1122 {
1123  int i, n, ret, format = FORMAT_NORMAL, saveqt;
1124  char *cp, tstr[256], pbuf[256], tbuf[1024], bigBuf[512];
1125  struct QUERY_VAR qv;
1126  FILE *fp;
1127 
1128  /* Check for flags */
1129  if(isSet(PF_X_FLAG) || isSet(PF_HEX_FLAG))
1130  {
1131  format = FORMAT_HEX;
1132  }
1133 
1134  if(isSet(PF_EXP_FLAG))
1135  {
1136  format = FORMAT_EXP;
1137  }
1138 
1139  if(strlen(args->secondArg))
1140  {
1141  if(!strcmp(args->secondArg, "header"))
1142  {
1144  return(0);
1145  }
1146 
1147  if(!strcmp(args->secondArg, "weather"))
1148  {
1150  return(0);
1151  }
1152 
1153  if(!strcmp(args->secondArg, "source"))
1154  {
1156  return(0);
1157  }
1158 
1159  if(!strcmp(args->secondArg, "telescope"))
1160  {
1162  return(0);
1163  }
1164 
1165  if(!strcmp(args->secondArg, "map"))
1166  {
1168  return(0);
1169  }
1170 
1171  if(!strcmp(args->secondArg, "status"))
1172  {
1173  printStatus();
1174  return(0);
1175  }
1176 
1177  /* Look for a batch file or procedure */
1178  ret = isBatch(args->secondArg);
1179 
1180  if(ret >= 0)
1181  {
1183  return(0);
1184  }
1185 
1186  /* If the planets have not been loaded yet, this call will
1187  produce a lot of verbose output; Possibly not what the
1188  user is expecting at this time; Set the quiet flag */
1189  saveqt = stquiet;
1190  stquiet = 1;
1191 
1192  ret = isPlanetVar(args->secondArg);
1193 
1194  stquiet = saveqt;
1195 
1196  if(!ret)
1197  {
1199  return(0);
1200  }
1201 
1202  n = args->ntokens;
1203 
1204  bzero(bigBuf, sizeof(bigBuf));
1205 
1206  for(i=1;i<n;i++)
1207  {
1208  strcpy(pbuf, args->tok[i]);
1209 
1210  ret = findAnyVariable(pbuf, &qv);
1211 
1212  if(ret)
1213  {
1214  if(isSet(PF_N_FLAG))
1215  {
1216  tstr[0] = '\0';
1217  }
1218  else
1219  {
1220  switch(qv.stype)
1221  {
1222  case VARIABLE_TYPE_DVAR:
1223  sprintf(tstr, "DVar: ");
1224  break;
1225 
1226  case VARIABLE_TYPE_GSVAR:
1227  sprintf(tstr, "GSVar: ");
1228  break;
1229 
1230  case VARIABLE_TYPE_HEADER:
1231  sprintf(tstr, "HEADER:");
1232  break;
1233 
1234  default:
1235  sprintf(tstr, "NOTDEF:");
1236  break;
1237  }
1238  }
1239 
1240  switch(qv.stype)
1241  {
1242  case VARIABLE_TYPE_DVAR:
1243  case VARIABLE_TYPE_GSVAR:
1244  case VARIABLE_TYPE_HEADER:
1245 
1246  if(qv.ntype == DOUBLE)
1247  {
1248  if(format == FORMAT_EXP)
1249  {
1250  if(isSet(PF_N_FLAG))
1251  {
1252  sprintf(tbuf, "%e ", qv.dval);
1253  strcat(bigBuf, tbuf);
1254  }
1255  else
1256  {
1257  sprintf(tbuf, "%s %s = %e ", tstr, pbuf, qv.dval);
1258  strcat(bigBuf, tbuf);
1259  }
1260  }
1261  else
1262  if(format == FORMAT_HEX)
1263  {
1264  if(isSet(PF_N_FLAG))
1265  {
1266  sprintf(tbuf, "0x%x ", (int)qv.dval);
1267  strcat(bigBuf, tbuf);
1268  }
1269  else
1270  {
1271  sprintf(tbuf, "%s %s = 0x%x ", tstr, pbuf, (int)qv.dval);
1272  strcat(bigBuf, tbuf);
1273  }
1274  }
1275  else
1276  {
1277  if(isSet(PF_N_FLAG))
1278  {
1279  sprintf(tbuf, "%s ", qv.buf);
1280  strcat(bigBuf, tbuf);
1281  }
1282  else
1283  {
1284  sprintf(tbuf, "%s %s = %s ", tstr, pbuf, qv.buf);
1285  strcat(bigBuf, tbuf);
1286  }
1287  }
1288  }
1289  else
1290  if(qv.ntype == INTEGER)
1291  {
1292  if(format == FORMAT_HEX)
1293  {
1294  if(isSet(PF_N_FLAG))
1295  {
1296  sprintf(tbuf, "0x%x ", qv.ival);
1297  strcat(bigBuf, tbuf);
1298  }
1299  else
1300  {
1301  sprintf(tbuf, "%s %s = 0x%x ", tstr, pbuf, qv.ival);
1302  strcat(bigBuf, tbuf);
1303  }
1304  }
1305  else
1306  {
1307  if(isSet(PF_N_FLAG))
1308  {
1309  sprintf(tbuf, "%d ", qv.ival);
1310  strcat(bigBuf, tbuf);
1311  }
1312  else
1313  {
1314  sprintf(tbuf, "%s %s = %d ", tstr, pbuf, qv.ival);
1315  strcat(bigBuf, tbuf);
1316  }
1317  }
1318  }
1319  else
1320  {
1321  if(isSet(PF_N_FLAG))
1322  {
1323  sprintf(tbuf, "%s ", qv.buf);
1324  strcat(bigBuf, tbuf);
1325  }
1326  else
1327  {
1328  sprintf(tbuf, "%s %s = %s ", tstr, pbuf, qv.buf);
1329  strcat(bigBuf, tbuf);
1330  }
1331  }
1332  break;
1333 
1334  default:
1335  break;
1336  }
1337  }
1338  else
1339  {
1340  if(!strcmp(pbuf, "@Space"))
1341  {
1342  strcat(bigBuf, " ");
1343  continue;
1344  }
1345 
1346  if(!strcmp(pbuf, "@Comma"))
1347  {
1348  strcat(bigBuf, ",");
1349  continue;
1350  }
1351 
1352  if(!strcmp(pbuf, "@Quote"))
1353  {
1354  strcat(bigBuf, "\" ");
1355  continue;
1356  }
1357 
1358  if(!strcmp(pbuf, "@Tick"))
1359  {
1360  strcat(bigBuf, "\'");
1361  continue;
1362  }
1363 
1364  sprintf(tbuf, "%s ", pbuf); /* Just print it */
1365  strcat(bigBuf, tbuf);
1366  }
1367  }
1368 
1369  if(isSet(PF_S_FLAG))
1370  {
1371  cp = FilterChars(bigBuf, " ");
1372  }
1373  else
1374  {
1375  cp = bigBuf;
1376  }
1377 
1378  if(isSet(PF_W_FLAG))
1379  {
1380  if(strlen(env->userfile))
1381  {
1382  if((fp = fopen(env->userfile, "a")))
1383  {
1384  fprintf(fp, "%s\n", cp);
1385 
1386  fclose(fp);
1387 
1388  return(0);
1389  }
1390  else
1391  {
1392  st_fail("Unable to open userfile: %s", env->userfile);
1393  return(1);
1394  }
1395  }
1396  else
1397  {
1398  st_fail("Write called without setting userfile");
1399  return(1);
1400  }
1401  }
1402  else
1403  {
1404  st_print("%s", cp);
1405  }
1406  }
1407 
1408  st_print("\n");
1409 
1410  return(0);
1411 }
1412 
1413 
1414 int isSpecial(cp)
1415 char cp;
1416 {
1417  if(isalnum(cp) || isspace(cp) || cp == 46 || cp == 95) /* check for '.' & '_' */
1418  {
1419  return(0);
1420  }
1421 
1422  return(1);
1423 }
1424 
1425 
1426 #define FIX_MATH_REPLACE 1
1427 #define FIX_MATH_COMPRESS 2
1428 
1430  char *find;
1431  char *replace;
1432 };
1433 
1434 struct FIX_OPERATORS fixers[] = {
1435  { "< =", "<= " },
1436  { "> =", ">= " },
1437  { "! =", "!= " },
1438  { "= =", "== " },
1439 };
1440 
1441 int max_fix_operators = (sizeof(fixers) / sizeof(struct FIX_OPERATORS));
1442 
1443 
1444 /* the expandCmd can break things like <=, >=, ==, e-3, e+3 etc; fix them here */
1445 /* FIXED: e-3, e+3 fixed now; get around e+3 error by using 1.0e3, not 1.0e+3 */
1446 
1448 char *buf;
1449 {
1450  int i;
1451  char *cp;
1452 
1454  {
1455  st_print("%s(%s:%d):\n", __func__, __FILE__, __LINE__);
1456  st_print("%s(%s:%d): Starting String: %s\n", __func__, __FILE__, __LINE__, buf);
1457  }
1458 
1459  strcmprs(buf); /* remove any excess spaces */
1460 
1461  if(verboseLevel & VERBOSE_PARSE)
1462  {
1463  st_print("%s(%s:%d):\n", __func__, __FILE__, __LINE__);
1464  st_print("%s(%s:%d): Compressed String: %s\n", __func__, __FILE__, __LINE__, buf);
1465  }
1466 
1467  for(i=0;i<max_fix_operators;i++)
1468  {
1469  if((cp = strstr(buf, fixers[i].find)))
1470  {
1471  strncpy(cp, fixers[i].replace, 3);
1472  }
1473  }
1474 
1475  strcmprs(buf);
1476 
1477  if(verboseLevel & VERBOSE_PARSE)
1478  {
1479  st_print("%s(%s:%d):\n", __func__, __FILE__, __LINE__);
1480  st_print("%s(%s:%d): Ending String: %s\n", __func__, __FILE__, __LINE__, buf);
1481  }
1482 
1483  return(0);
1484 }
1485 
1486 
1487 /* Copy the src string into the dst string placing spaces
1488  * into the command so the parser can identify possible
1489  * variable named tokens
1490  *
1491  * FIXME: Does NOT handle exponets properly; 1.0e-3 turns into 1.0e - 3
1492  *
1493  */
1494 int expandCmd(src, dst)
1495 char *src, *dst;
1496 {
1497  char *cp, *ccp;
1498 
1500  {
1501  st_print("%s(%s:%d): Starting Buf: %s\n", __func__, __FILE__, __LINE__, src);
1502  }
1503 
1504  dst[0] = '\0';
1505 
1506  cp = src;
1507  ccp = dst;
1508 
1509  for(;*cp;cp++)
1510  {
1511  if(isSpecial(*cp)) /* if it's a non alphanumeric char add extra space */
1512  {
1513  *ccp = ' ';
1514  ccp++;
1515  *ccp = *cp;
1516  ccp++;
1517  *ccp = ' ';
1518  ccp++;
1519  }
1520  else /* Else just copy the char to the dst */
1521  {
1522  *ccp = *cp;
1523  ccp++;
1524  }
1525  }
1526 
1527  if(verboseLevel & VERBOSE_PARSE)
1528  {
1529  st_print("%s(%s:%d): Endig Buf: %s\n", __func__, __FILE__, __LINE__, dst);
1530  }
1531 
1532  fixMathOperators(dst);
1533 
1534  return(0);
1535 }
1536 
1537 
1542 int evaluateRightSide(k, n, buf)
1543 int k, n;
1544 char *buf;
1545 {
1546  int i;
1547  struct QUERY_VAR qvw;
1548  char tbuf[512];
1549 
1550  for(i=k;i<n;i++) /* Substitute any variable names here */
1551  {
1553  {
1554  st_print("%s(%s:%d):\n", __func__, __FILE__, __LINE__);
1555  st_print("%s(%s:%d): Working token %s\n", __func__, __FILE__, __LINE__, args->tok[i]);
1556  }
1557 
1558  if(findAnyVariable(args->tok[i], &qvw))
1559  {
1560  if(verboseLevel & VERBOSE_PARSE)
1561  {
1562  st_print("%s(%s:%d): %s is a %d %d variable\n", __func__, __FILE__, __LINE__, args->tok[i], qvw.stype, qvw.ntype);
1563  }
1564 
1565  sprintf(tbuf, "%s ", qvw.buf); /* buf already conatins the ascii representation of the variable */
1566  strcat(buf, tbuf);
1567  }
1568  else
1569  {
1570  if(verboseLevel & VERBOSE_PARSE)
1571  {
1572  st_print("%s(%s:%d): %s not a variable\n", __func__, __FILE__, __LINE__, args->tok[i]);
1573  }
1574 
1575  sprintf(tbuf, "%s ", qvw.buf); /* buf already conatins the ascii representation of the variable */
1576  strcat(buf, tbuf);
1577  }
1578  }
1579 
1581  {
1582  st_print("%s(%s:%d): To doCalculation() %s\n", __func__, __FILE__, __LINE__, buf);
1583  }
1584 
1585  return(0);
1586 }
1587 
1588 
1601 char *buf;
1602 {
1603  int i, n, ret;
1604  char tbuf[512], var[256], evalBuf[1024];
1605  struct GSKEY *pgs;
1606  struct DYNAMIC_VARIABLES *pdv;
1607  struct QUERY_VAR qvl;
1608 
1610  {
1611  st_print("%s(%s:%d): Before Expansion: %s\n", __func__, __FILE__, __LINE__, buf);
1612  }
1613 
1614  bzero(tbuf, sizeof(tbuf));
1615 
1616  expandCmd(buf, tbuf);
1617 
1618  if(verboseLevel & VERBOSE_PARSE)
1619  {
1620  st_print("%s(%s:%d): After Expansion: %s\n", __func__, __FILE__, __LINE__, tbuf);
1621  }
1622 
1623 /* First parse out the variable name */
1624 
1625  n = get_tok(tbuf);
1626 
1627  if(verboseLevel & VERBOSE_PARSE)
1628  {
1629  st_print("%s(%s:%d): N = %d\n", __func__, __FILE__, __LINE__, n);
1630  }
1631 
1632  if(verboseLevel & VERBOSE_PARSE)
1633  {
1634  st_print("Tokens are: ");
1635  for(i=0;i<n;i++)
1636  {
1637  st_print("%s ", args->tok[i]);
1638  }
1639  st_print("\n");
1640  }
1641 
1642  if(n > 1)
1643  {
1644  strcpy(var, args->tok[0]);
1645 
1646  ret = findAnyVariable(var, &qvl);
1647 
1648  if(verboseLevel & VERBOSE_PARSE)
1649  {
1650  st_print("%s(%s:%d): After findVariable(%s): Ret = %d\n", __func__, __FILE__, __LINE__, var, ret);
1651  }
1652 
1653  if(ret) /* We have a defined variable so parse it */
1654  {
1655  /* Now find expression */
1656  if(!strcmp(args->tok[1], "=")) /* Assign the variable */
1657  {
1658  if(n > 2)
1659  {
1660  evalBuf[0] = '\0';
1661 
1662  if(evaluateRightSide(2, n, evalBuf))
1663  {
1664  st_fail("Failure in expression evaluation");
1665  return(1);
1666  }
1667 
1668  if(!doCalculation(evalBuf))
1669  {
1670  if(qvl.stype) /* it was a previously defind variable */
1671  {
1672  if(qvl.stype == VARIABLE_TYPE_DVAR)
1673  {
1674  if(qvl.indx < DYN_VARIBLE_PROTECTED)
1675  {
1676  st_fail("Not allowed to change protected variables");
1677  return(1);
1678  }
1679 
1680  if(verboseLevel & VERBOSE_PARSE)
1681  {
1682  st_print("%s(%s:%d): Setting = DVAR\n", __func__, __FILE__, __LINE__);
1683  }
1684 
1685  pdv = &dvars[qvl.indx];
1686  if(pdv->type == DYN_TYPE_INT)
1687  {
1688  *pdv->ival = (int)env->rst;
1689  st_report("Setting %s to %d", pdv->name, *pdv->ival);
1690  }
1691  else
1692  {
1693  *pdv->dval = env->rst;
1694 
1695  if(fabs(env->rst) < 0.5)
1696  {
1697  st_report("Setting %s to %e", pdv->name, *pdv->dval);
1698  }
1699  else
1700  {
1701  st_report("Setting %s to %lf", pdv->name, *pdv->dval);
1702  }
1703  }
1704  }
1705  else
1706  if(qvl.stype == VARIABLE_TYPE_GSVAR)
1707  {
1708  if(verboseLevel & VERBOSE_PARSE)
1709  {
1710  st_print("%s(%s:%d): Setting = GSVAR\n", __func__, __FILE__, __LINE__);
1711  }
1712 
1713  pgs = &getset[qvl.indx];
1714 
1715  if(pgs->type == INTEGER)
1716  {
1717  *pgs->iaddr = (int)env->rst;
1718  st_report("Setting %s to %d", pgs->name, *pgs->iaddr);
1719  }
1720  else
1721  if(pgs->type == DOUBLE)
1722  {
1723  *pgs->daddr = env->rst;
1724  st_report("Setting %s to %f", pgs->name, *pgs->daddr);
1725  }
1726  }
1727  else
1728  if(qvl.stype == VARIABLE_TYPE_HEADER)
1729  {
1730  st_report("CAUTION: ******* Setting HEADER variable: '%s' ********", qvl.name);
1731 
1732  /* We can only change doubles at this time */
1733  if(qvl.phead.type == DOUBLE)
1734  {
1735  st_report("Setting Header Variable %s to %f", qvl.name, env->rst);
1736 
1737  /* gvl.phead points to a static local header */
1738  *qvl.phead.dptr = env->rst;
1739 
1740  /* Copy the local header to the working header */
1741  updateHeader();
1742  }
1743  }
1744  }
1745  else
1746  {
1747  FAIL_INFO("Assignment variable not found");
1748  }
1749  }
1750  }
1751  }
1752  else
1753  {
1754  FAIL_INFO("No '=' sign found in expression");
1755  }
1756  }
1757  else
1758  {
1759  FAIL_INFO("You must define a variable before assignment");
1760  }
1761  }
1762 
1763  return(0);
1764 }
1765 
1766 
1767 /* Remove all spaces from command before sending it to calculator engine
1768  * This fixed the problem of the expansion function turning 1.0e-3 into 10e - 3
1769  * This function turns 1.0e - 3 back into 1.0e-3; the lcalc function doesn't care
1770  it the passed argument has spaces. */
1771 int removeAllSpaces(in, out)
1772 char *in, *out;
1773 {
1774  char *cpin, *cpout;
1775 
1777  {
1778  st_print("%s(%s:%d): IN: %s\n", __func__, __FILE__, __LINE__, in);
1779  }
1780 
1781  cpin = in;
1782  cpout = out;
1783 
1784  for( ;*cpin;cpin++)
1785  {
1786  if(!isspace(*cpin))
1787  {
1788  *cpout = *cpin;
1789  cpout++;
1790  }
1791  }
1792 
1793  if(verboseLevel & VERBOSE_PARSE)
1794  {
1795  st_print("%s(%s:%d): OUT: %s\n", __func__, __FILE__, __LINE__, out);
1796  }
1797 
1798  return(0);
1799 }
1800 
1801 
1803 char *buf;
1804 {
1805  char line[256], tbuf[256];
1806 
1807  bzero(tbuf, sizeof(tbuf));
1808 
1809  removeAllSpaces(buf, tbuf);
1810 
1811  env->rst = 0.0;
1812 
1814  {
1815  st_print("%s(%s:%d): %s\n", __func__, __FILE__, __LINE__, tbuf);
1816  }
1817 
1826  strcpy(line, lcalc(tbuf));
1827 
1828  strcmprs(line);
1829 
1830  if(strlen(line))
1831  {
1832  if(strstr(line, "undefined"))
1833  {
1834  st_print("%s: %s", tbuf, line);
1835  FAIL_INFO("Failure in doCalculation()");
1836  return(1);
1837  }
1838 
1839  if(strstr(line, "Error"))
1840  {
1841  st_print("%s: %s", tbuf, line);
1842  FAIL_INFO("Failure in doCalculation()");
1843  return(1);
1844  }
1845 
1846  if(strstr(line, "Unable"))
1847  {
1848  st_print("%s: %s", tbuf, line);
1849  FAIL_INFO("Failure in doCalculation()");
1850  return(1);
1851  }
1852 
1853  if(strstr(line, "Missing"))
1854  {
1855  st_print("%s: %s", tbuf, line);
1856  FAIL_INFO("Failure in doCalculation()");
1857  return(1);
1858  }
1859 
1860  if(strstr(line, "Unknown"))
1861  {
1862  st_print("%s: %s", tbuf, line);
1863  FAIL_INFO("Failure in doCalculation()");
1864  return(1);
1865  }
1866 
1867  if(strlen(line))
1868  {
1869  if(verboseLevel & VERBOSE_PARSE)
1870  {
1871  st_print("doCalculation(): Entering Math Parser with: '%s'\n", line);
1872  }
1873 
1874  if(!strncmp(line, "~", 1)) /* Check for 'calc' roundoff flag */
1875  {
1876  env->rst = strtod(line+1, NULL);
1877  }
1878  else
1879  {
1880  env->rst = strtod(line, NULL);
1881  }
1882 
1883  if(verboseLevel & VERBOSE_PARSE)
1884  {
1885  st_print("doCalculation(): Results: '%s', %.6e, len = %d\n", line, env->rst, strlen(line));
1886  }
1887 
1888  return(0);
1889  }
1890  }
1891 
1892  st_print("%s: ", tbuf);
1893  st_print("Failure: doCalc() returned NULL\n");
1894 
1895  return(1);
1896 }
1897 
1898 
1902 int get_tok(edit)
1903 char *edit;
1904 {
1905  int c = 0;
1906  char *token;
1907 
1908  bzero((char *)&args->tok, sizeof(args->tok));
1909 
1910  token = strtok(edit, "\n\t ");
1911  while(token != (char *)NULL)
1912  {
1913  sprintf(args->tok[c++], "%s", token);
1914  token = strtok((char *)NULL, "\n\t ");
1915  }
1916 
1917  return(c);
1918 }
1919 
1920 
1924 int get_cmd_tok(edit)
1925 char *edit;
1926 {
1927  int c = 0;
1928  char *token;
1929 
1930  bzero((char *)&args->cmd_tok, sizeof(args->cmd_tok));
1931 
1932  token = strtok(edit, ";");
1933  while(token != (char *)NULL)
1934  {
1935  sprintf(args->cmd_tok[c++], "%s", token);
1936  token = strtok((char *)NULL, ";");
1937  }
1938 
1939  return(c);
1940 }
1941 
1942 
1943 int bang(buf)
1944 char *buf;
1945 {
1946  int ret;
1947  char *cp;
1948 
1949  ret = history_expand(buf, &cp);
1950 
1951  if(ret == -1) /* Not found */
1952  {
1953  st_report("Command %s, not found in History", buf);
1954  }
1955 
1956  if(ret == 1 || ret == 0) /* Yes, found */
1957  {
1958  st_report("%s", cp);
1959  my_add_history(cp);
1960  parseCmd(cp);
1961 
1962  free(cp);
1963  }
1964 
1965  if(ret == 2) /* :p Just print it */
1966  {
1967  st_print("%s %s\n", prompt, cp);
1968 
1969  my_add_history(cp);
1970 
1971  free(cp);
1972  }
1973 
1974  return(0);
1975 }
1976 
1977 
1982 char *buf;
1983 {
1984  int i;
1985  struct COMMANDS *p;
1986 
1987  p = &commands[0];
1988  for(i=0;p->cmd;i++,p++)
1989  {
1990  if(!strncasecmp(p->cmd, buf, strlen(buf)))
1991  {
1992  return(i);
1993  }
1994  }
1995 
1996  return(-1);
1997 }
1998 
1999 
2001 char *buf;
2002 {
2003  FILE *fp;
2004 
2005  if(strlen(recordingFname))
2006  {
2007  if((fp = fopen(recordingFname, "a")))
2008  {
2009  fprintf(fp, "%s\n", buf);
2010 
2011  fclose(fp);
2012  }
2013  else
2014  {
2015  st_fail("Unable to open %s", recordingFname);
2016 
2017  return(1);
2018  }
2019  }
2020  else
2021  {
2022  st_fail("recordingFname not set");
2023 
2024  return(1);
2025  }
2026 
2027  return(0);
2028 }
2029 
2030 
2031 
2033 char *arg;
2034 struct COMMANDS *cmds;
2035 {
2036  int len, found=0;
2037  char tbuf[512], bbuf[2048], batchBuf[2048];
2038  struct COMMANDS *p;
2039 
2040  if(arg)
2041  {
2042  bzero(tbuf, sizeof(tbuf));
2043  bzero(bbuf, sizeof(bbuf));
2044  bzero(batchBuf, sizeof(batchBuf));
2045 
2046  len = strlen(arg);
2047 
2048  p = &commands[0];
2049 
2050  for(;p->cmd;p++)
2051  {
2052  if(p->ntoken == -1) /* Skip all those help commands */
2053  {
2054  continue;
2055  }
2056 
2057  /* print all commands who's first few letters match */
2058 
2059  if(p->mode == env->mode || p->mode == MODE_ALL)
2060  {
2061  if(!strncasecmp(p->cmd, arg, len))
2062  {
2063  sprintf(tbuf, "%s ", p->cmd);
2064  strcat(bbuf, tbuf);
2065 
2066  found++;
2067  }
2068  }
2069  }
2070 
2071  found += findAmbiguousBatch(arg, batchBuf);
2072 
2073  if(found)
2074  {
2075  st_report("Ambiguous Command: %s", arg);
2076 
2077  strcmprs(bbuf);
2078 
2079  st_report("Possible Command: %s %s", bbuf, batchBuf);
2080  }
2081  }
2082 
2083  return(0);
2084 }
2085 
2086 
2090 int parseCmd(buf)
2091 char *buf;
2092 {
2093  int i, j, n, len, commandOK, ret;
2094  char tbuf[512], ttbuf[512], evalBuf[1024];
2095  char tmp[256];
2096  char saveBuf[256];
2097  struct COMMANDS *p;
2098 
2099  /* First, remove all extra spaces from command; including leading */
2100  strcmprs(buf);
2101 
2102  if(recording)
2103  {
2104  recordThisCommand(buf);
2105  }
2106 
2107  /* Save the stack */
2108  pushArgs();
2109 
2110  /* Clear all the '/' flags */
2111  clearFlags();
2112 
2113  if(!strstr(buf, "alias")) /* Avoid commands with the word alias in them */
2114  {
2115  substituteAliases(buf); /* Go substitute any aliases */
2116  }
2117 
2118  /* Save a copy of the command */
2119  strcpy(tbuf, buf);
2120 
2121  if(errPrint_fp)
2122  {
2123  fprintf(errPrint_fp, "%s %s\n", prompt, buf);
2124  }
2125 
2126  /* Trap the 'eval' command */
2127  if(!strncmp(tbuf, "eval ", 5))
2128  {
2129  bzero(ttbuf, sizeof(ttbuf));
2130  expandCmd(tbuf, ttbuf);
2131 
2132  n = get_tok(ttbuf);
2133 
2134  if(n < 3)
2135  {
2136  st_fail("Too few args for Eval Function");
2137 
2138  /* pop the stack */
2139  popArgs();
2140 
2141  return(1);
2142  }
2143 
2144  evalBuf[0] = '\0';
2145 
2146  if(evaluateRightSide(1, n, evalBuf))
2147  {
2148  st_fail("Failure in expression evaluation");
2149 
2150  /* pop the stack */
2151  popArgs();
2152 
2153  return(1);
2154  }
2155 
2156  if(!doCalculation(evalBuf))
2157  {
2158  if(fabs(env->rst) < 0.01)
2159  {
2160  st_print("Results: %.6e\n", env->rst);
2161  }
2162  else
2163  {
2164  st_print("Results: %f\n", env->rst);
2165  }
2166  }
2167 
2168  /* pop the stack */
2169  popArgs();
2170 
2171  return(0);
2172  }
2173 
2174  if(!strncmp(tbuf, "let ", 4) || !strncmp(tbuf, "set ", 4))
2175  {
2176  doLetCmd(tbuf+4);
2177 
2178  /* pop the stack */
2179  popArgs();
2180 
2181  return(0);
2182  }
2183 
2184 
2185  /* First break the command into individual commands that are
2186  seperated by any ';' characters */
2187  args->ncmds = get_cmd_tok(tbuf);
2188 
2190  {
2191  st_report("parseCmd(): Found %d Sub-Command(s)", args->ncmds);
2192  }
2193 
2194  /* Now parse each seperate command */
2195  for(j=0;j<args->ncmds;j++)
2196  {
2197  if(verboseLevel & VERBOSE_PARSE)
2198  {
2199  st_report("parseCmd(): Parsing sub-command[%d]: %s", j, args->cmd_tok[j]);
2200  }
2201 
2202  strcpy(saveBuf, args->cmd_tok[j]);
2203 
2204  args->ntokens = get_tok(args->cmd_tok[j]);
2205 
2206  parseFlags();
2207 
2208  substituteEnv();
2209 
2210  if(!strcmp(args->firstArg, "#"))
2211  {
2212  if(verboseLevel & VERBOSE_PARSE)
2213  {
2214  REPORT_INFO("parseCmd(): Ignoring # Comment");
2215  }
2216 
2217  continue;
2218  }
2219 
2220  /* Check for a variable assignment; must be done after parseFlags() */
2221  if(strstr(saveBuf, "="))
2222  {
2223  /* reassemble command minus the flags */
2224 
2225  reassembleCommand(saveBuf);
2226 
2227  doLetCmd(saveBuf);
2228 
2229  if(isSet(PF_R_FLAG))
2230  {
2231  xx();
2232  }
2233 
2234  continue;
2235  }
2236 
2237  if(verboseLevel & VERBOSE_PARSE)
2238  {
2239  st_report("parseCmd(): Parsing %d tokens", args->ntokens);
2240  }
2241 
2242  if(verboseLevel & VERBOSE_PARSE)
2243  {
2244  for(i=0;i<args->ntokens;i++)
2245  {
2246  if(isnumber(args->tok[i]))
2247  {
2248  st_report("parseCmd(): Token %d: %s; is number", i, args->tok[i]);
2249  }
2250  else
2251  if(isCommand(args->tok[i]) >= 0)
2252  {
2253  st_report("parseCmd(): Token %d: %s; is Command", i, args->tok[i]);
2254  }
2255  else
2256  {
2257  st_report("parseCmd(): Token %d: %s; is possible arg", i, args->tok[i]);
2258  }
2259  }
2260  }
2261 
2262  /* Check if user is inputting numbers first */
2263  /* Only works for first two args */
2264  if(isnumber(args->firstArg)) /* RPN ?? */
2265  {
2266  if(verboseLevel & VERBOSE_PARSE)
2267  {
2268  REPORT_INFO("parseCmd(): Swapping tokens 0 <-> 1");
2269  }
2270 
2271  strcpy(tmp, args->firstArg); /* Save it */
2272  strcpy(args->tok[0], args->tok[1]);/* Reverse them */
2273  strcpy(args->tok[1], tmp);
2274  }
2275 
2276  p = &commands[0];
2277 
2278  commandOK = 0;
2279 
2280  len = strlen(args->firstArg);
2281 
2282  if(len == 0)
2283  {
2284  st_fail("parseCmd(): Missing or mal-formed command: %s", buf);
2285 
2286  /* pop the stack */
2287  popArgs();
2288 
2289  return(1);
2290  }
2291 
2292  for(;p->cmd;p++)
2293  {
2294  if(!strncasecmp(p->cmd, args->firstArg, len))
2295  {
2296  if(isSet(PF_H_FLAG) && isFirst(PF_H_FLAG))
2297  {
2298  printH_FlagHelp(p->cmd);
2299 
2300  popArgs();
2301 
2302  return(0);
2303  }
2304 
2305  if(p->ntoken == -1)
2306  {
2308 
2309  if(verboseLevel & VERBOSE_PARSE)
2310  {
2311  st_report("parseCmd(): Returning %d", 0);
2312  }
2313 
2314  popArgs();
2315 
2316  return(1);
2317  }
2318 
2319  if(p->ntoken > 0 && args->ntokens < p->ntoken) /* Should have at least ntoken's */
2320  {
2321  st_print("parseCmd(): Missing argument(s) to command: [%s]\n", p->cmd);
2322  st_print("parseCmd(): Usage: %s %s : %s\n", p->cmd, p->args, p->help);
2323 
2324  if(verboseLevel & VERBOSE_PARSE)
2325  {
2326  st_report("parseCmd(): Returning %d", 1);
2327  }
2328 
2329  popArgs();
2330 
2331  return(1);
2332  }
2333 
2334  if(verboseLevel & VERBOSE_PARSE)
2335  {
2336  st_report("parseCmd(): Excuting command %s", p->cmd);
2337  }
2338 
2339  /* Check for proper mode now */
2340  if(p->mode && p->mode != env->mode)
2341  {
2342  if(env->mode == MODE_OTF) /* Check for OTF mode; SPEC commands OK */
2343  {
2344  if(p->mode != MODE_SPEC)
2345  {
2346  st_fail("parseCmd(): %s Command not allowed in %s mode", p->cmd, modeStrs[env->mode]);
2347  popArgs();
2348 
2349  return(1);
2350  }
2351  }
2352  else
2353  {
2354  st_fail("parseCmd(): %s Command not allowed in %s mode", p->cmd, modeStrs[env->mode]);
2355  popArgs();
2356 
2357  return(1);
2358  }
2359  }
2360 
2361  if(verboseLevel & VERBOSE_PARSE)
2362  {
2363  st_report("parseCmd(): Setting Current Command to %s", args->currentCmd);
2364  }
2365 
2366  strcpy(args->currentCmd, p->cmd); /* Some functions are overloaded; remember who called */
2367  strcmprs(args->currentCmd); /* Strip any spaces */
2368 
2369  ret = p->fctn();
2370 
2371  if(!ret && p->post) /* Command executed OK and it has a post command */
2372  {
2373  p->post();
2374  }
2375 
2376  if(ret)
2377  {
2378  st_fail("parseCmd(): Command: %s Failed", p->cmd);
2379 
2380  popArgs();
2381 
2382  return(ret);
2383  }
2384 
2385  commandOK = 1;
2386 
2387  break;
2388  }
2389  }
2390 
2391  /* Keep looking */
2392  if(!commandOK) /* Maybe its a loaded batch procedure */
2393  {
2394  ret = isBatch(args->firstArg);
2395  if(ret >= 0)
2396  {
2398  commandOK = 1;
2399  }
2400  }
2401 
2402  if(!commandOK) /* OK, maybe its' an expression; check for '=' */
2403  {
2404 // if(strstr(saveBuf, "="))
2405 // {
2406 // doLetCmd(saveBuf); /* Time to use that saved buf */
2407 // commandOK = 1;
2408 //
2409 // if(isSet(PF_R_FLAG))
2410 // {
2411 // xx();
2412 // }
2413 // }
2414  }
2415 
2416  /* Check for a variable assignment; must be done after parseFlags() */
2417 // if(!commandOK && strstr(saveBuf, "="))
2418 // {
2419  /* reassemble command minus the flags */
2420 
2421 // reassembleCommand(saveBuf);
2422 
2423 // doLetCmd(saveBuf);
2424 
2425 // if(isSet(PF_R_FLAG))
2426 // {
2427 // xx();
2428 // }
2429 
2430 // popArgs();
2431 
2432  // return(0);
2433  // }
2434 
2435  if(!commandOK)
2436  {
2437  st_fail("parseCmd(): Command(%s); Not Found", args->firstArg);
2438 
2439  popArgs();
2440 
2441  return(2);
2442  }
2443  } /* Looping through ';' seperated commands */
2444 
2445  popArgs();
2446 
2447  return(0);
2448 }
2449 
2450 
2451 
2452 void process_line(char *line)
2453 {
2454  static int nulls=0;
2455  int len, old_stquiet;
2456  char tbuf[512];
2457 
2458  if(line == NULL)
2459  {
2460  nulls++; /* Check for lost stdin */
2461 
2462  if(nulls > 25) /* good numer */
2463  {
2464  st_report("Infinite Loop detected; exiting");
2465  exit(1);
2466  }
2467 
2468  st_print("process_line(): received NULL line\n");
2469  return;
2470  }
2471 
2472  nulls = 0;
2473 
2474  strcpy(myline, line);
2475 
2476  free(line);
2477 
2478  len = strlen(myline);
2479 
2480  /* Look for things like 'c?' or 'map?' */
2481  /* backend command can have wildcards including ? in it */
2482  if(!strncmp(myline+(len-1), "?", 1) && !strstr(myline, "back"))
2483  {
2484  letterHelp(myline);
2485  }
2486  else
2487  if(!strncmp(myline, "@", 1)) /* Catch the special case of the '@' char */
2488  {
2489  sprintf(tbuf, "load %s", myline+1);
2490  parseCmd(tbuf);
2491  }
2492  else
2493  if(!strncmp(myline, "!", 1))
2494  {
2495  bang(myline);
2496  }
2497  else
2498  if(!strncmp(myline, ":", 1))
2499  {
2500  if(strlen(myline) > 2)
2501  {
2502  system(myline+1); /* Ignore the ':' character */
2504  }
2505  }
2506  else
2507  {
2508  /* Make sure all commands see the most recent data file listing */
2509  old_stquiet = stquiet;
2510  stquiet = 1; /* Be quiet about it */
2511 
2512  fileChanged(); /* Check if new data present */
2513 
2514  stquiet = old_stquiet; /* Restore old quiet flag */
2515 
2516  parseCmd(myline);
2517 
2518  if(strlen(myline) > 1)
2519  {
2521  }
2522  }
2523 
2524  fflush(stdin);
2525 }
2526 
2527 /* Shortcut to 'autoscale' */
2528 int ctrl_a_list(int arg1, int arg2)
2529 {
2530  st_print("\n");
2531 
2532  doAutoScale();
2533  xx();
2534 
2535  /* redraw the current line - this is an undocumented function. It invokes the
2536  * redraw-current-line command.
2537  */
2538  rl_refresh_line(0, 0);
2539 
2540  return(0);
2541 }
2542 
2543 
2544 /* Shortcut to 'baseline' */
2545 int ctrl_b_list(int arg1, int arg2)
2546 {
2547  st_print("\n");
2548 
2549  doBaseline();
2550  xx();
2551 
2552  /* redraw the current line - this is an undocumented function. It invokes the
2553  * redraw-current-line command.
2554  */
2555  rl_refresh_line(0, 0);
2556 
2557  return(0);
2558 }
2559 
2560 
2561 /* Shortcut to 'summary' */
2562 int ctrl_t_list(int arg1, int arg2)
2563 {
2564  st_print("\n");
2565 
2566  doSummary();
2567 
2568  /* redraw the current line - this is an undocumented function. It invokes the
2569  * redraw-current-line command.
2570  */
2571  rl_refresh_line(0, 0);
2572 
2573  return(0);
2574 }
2575 
2576 /* Shortcut to 'list' */
2577 int ctrl_l_list(int arg1, int arg2)
2578 {
2579  st_print("\n");
2580 
2581  listScans();
2582 
2583  /* redraw the current line - this is an undocumented function. It invokes the
2584  * redraw-current-line command.
2585  */
2586  rl_refresh_line(0, 0);
2587 
2588  return(0);
2589 }
2590 
2591 
2592 /* Shortcut to 'params' */
2593 int ctrl_p_list(int arg1, int arg2)
2594 {
2595  st_print("\n");
2596 
2597  showParams();
2598 
2599  /* redraw the current line - this is an undocumented function. It invokes the
2600  * redraw-current-line command.
2601  */
2602  rl_refresh_line(0, 0);
2603 
2604  return(0);
2605 }
2606 
2607 /* Shortcut to 'tellEnv' */
2608 int ctrl_e_list(int arg1, int arg2)
2609 {
2610  st_print("\n");
2611 
2612  tellEnv();
2613 
2614  /* redraw the current line - this is an undocumented function. It invokes the
2615  * redraw-current-line command.
2616  */
2617  rl_refresh_line(0, 0);
2618 
2619  return(0);
2620 }
2621 
2622 
2624 {
2626 
2627  return(0);
2628 }
2629 
2630 
2631 int init_readline(void)
2632 {
2633  rl_add_defun("auto", &ctrl_a_list, CTRL('a'));
2634  rl_add_defun("base", &ctrl_b_list, CTRL('b'));
2635  rl_add_defun("list", &ctrl_l_list, CTRL('l'));
2636  rl_add_defun("params", &ctrl_p_list, CTRL('p'));
2637  rl_add_defun("env", &ctrl_e_list, CTRL('e'));
2638  rl_add_defun("summ", &ctrl_t_list, CTRL('t'));
2639 
2640  return(0);
2641 }
2642 
2643 
2644 int setColor(c)
2645 int c;
2646 {
2647  if(c == oldColor)
2648  {
2649  return(0);
2650  }
2651 
2652  switch(c)
2653  {
2654  case BLACK: st_print("%c[0m", 0x1b); break;
2655  case RED: st_print("%c[31m", 0x1b); break;
2656  case GREEN: st_print("%c[32;1m", 0x1b); break;
2657  case YELLOW: st_print("%c[33;1m", 0x1b); break;
2658  case BLUE: st_print("%c[34;1m", 0x1b); break;
2659  case MAGENTA: st_print("%c[35;1m", 0x1b); break;
2660  case CYAN: st_print("%c[36;1m", 0x1b); break;
2661  case WHITE: st_print("%c[37;40m", 0x1b); break;
2662  case RESERVE: st_print("%c[0m", 0x1b); break;
2663  case RESET: st_print("%c[39m", 0x1b); break;
2664  }
2665 
2666  oldColor = c;
2667 
2668  return(0);
2669 }
2670 
2671 
2672 int doColor()
2673 {
2674  int i, color;
2675 
2676  if(isSet(PF_R_FLAG))
2677  {
2678  setColor(BLACK);
2679 
2680  return(0);
2681  }
2682 
2683  if(!strlen(args->secondArg))
2684  {
2685  color = parseColor();
2686  setColor(color);
2687 
2688  return(0);
2689  }
2690 
2691  for(i=0;i<MAX_COLORS;i++)
2692  {
2693  if(!strcasecmp(args->secondArg, colors[i]))
2694  {
2695  setColor(BLACK+i);
2696  return(0);
2697  }
2698  }
2699 
2700  return(1);
2701 }
2702 
2703 
2704 int findInArgs(parg)
2705 char *parg;
2706 {
2707  int i;
2708 
2709  for(i=0;i<MAX_TOKENS;i++)
2710  {
2711  if(strlen(args->tok[i]))
2712  {
2713  if(!strncmp(args->tok[i], parg, strlen(parg)))
2714  {
2715  return(1);
2716  }
2717  }
2718  }
2719 
2720  return(0);
2721 }
2722 
2723 
2725 {
2726  char sysBuf[512];
2727 
2728  if(args->ntokens == 3)
2729  {
2730  if(!strncmp(args->secondArg, "on", 2))
2731  {
2732  if(strlen(args->thirdArg))
2733  {
2734  strcpy(recordingFname, args->thirdArg);
2735  recording = 1;
2736 
2737  st_report("Recording Commands to %s", recordingFname);
2738 
2739  sprintf(sysBuf, "lp_removefile %s", recordingFname);
2740 
2741  system(sysBuf);
2742  }
2743  else
2744  {
2745  printH_FlagHelp("record");
2746  }
2747  }
2748  else
2749  {
2750  printH_FlagHelp("record");
2751  }
2752  }
2753  else
2754  if(args->ntokens == 2)
2755  {
2756  if(!strncmp(args->secondArg, "off", 3))
2757  {
2758  recording = 0;
2759  bzero(recordingFname, sizeof(recordingFname));
2760 
2761  st_report("Turning Command Recording Off");
2762  }
2763  else
2764  {
2765  printH_FlagHelp("record");
2766  }
2767  }
2768  else
2769  {
2770  printH_FlagHelp("record");
2771  }
2772 
2773  return(0);
2774 }
2775 
2776 
2778 {
2779  listDynVars();
2780 
2781  return(0);
2782 }
2783 
2784 
2785 /* Filter out passed charaters from a string; good for stripping out spaces and tabs */
2786 char *FilterChars(String, Filter)
2787 char *String, *Filter;
2788 {
2789  int a=0,i=0;
2790  char *Filtered = (char *)malloc(strlen(String) * sizeof(char));
2791 
2792  for(a=0;String[a];a++)
2793  {
2794  if(!strchr(Filter, String[a]))
2795  {
2796  Filtered[i++]=String[a];
2797  }
2798  }
2799 
2800  Filtered[i]=0;
2801 
2802  return(Filtered);
2803 }
2804 
2805 char sv_tok[40][40];
2806 
2807 
2811 int get_sv_tok(edit)
2812 char *edit;
2813 {
2814  int c = 0;
2815  char *token;
2816 
2817  bzero((char *)&sv_tok, sizeof(sv_tok));
2818 
2819  token = strtok(edit, "\n\t ");
2820  while(token != (char *)NULL)
2821  {
2822  sprintf(sv_tok[c++], "%s", token);
2823  token = strtok((char *)NULL, "\n\t ");
2824  }
2825 
2826  return(c);
2827 }
2828 
2829 
2830 
2831 int substituteVariables(inbuf, outbuf)
2832 char *inbuf, *outbuf;
2833 {
2834  int i, n;
2835  struct QUERY_VAR qvw;
2836  char tbuf[512];
2837 
2838  n = get_sv_tok(inbuf);
2839 
2840  outbuf[0] = '\0';
2841 
2842  for(i=0;i<n;i++) /* Substitute any variable names here */
2843  {
2845  {
2846  st_print("%s(%s:%d):\n", __func__, __FILE__, __LINE__);
2847  st_print("%s(%s:%d): Working token %s\n", __func__, __FILE__, __LINE__, sv_tok[i]);
2848  }
2849 
2850  if(findAnyVariable(sv_tok[i], &qvw))
2851  {
2852  if(verboseLevel & VERBOSE_PARSE)
2853  {
2854  st_print("%s(%s:%d): %s is a %d %d variable\n", __func__, __FILE__, __LINE__, sv_tok[i], qvw.stype, qvw.ntype);
2855  }
2856 
2857  sprintf(tbuf, "%s ", qvw.buf); /* buf already conatins the ascii representation of the variable */
2858  strcat(outbuf, tbuf);
2859  }
2860  else
2861  {
2862  if(verboseLevel & VERBOSE_PARSE)
2863  {
2864  st_print("%s(%s:%d): %s not a variable\n", __func__, __FILE__, __LINE__, sv_tok[i]);
2865  }
2866 
2867  sprintf(tbuf, "%s ", qvw.buf); /* buf already conatins the ascii representation of the variable */
2868  strcat(outbuf, tbuf);
2869  }
2870  }
2871 
2873  {
2874  st_print("%s(%s:%d): Result: %s\n", __func__, __FILE__, __LINE__, outbuf);
2875  }
2876 
2877  return(0);
2878 }
2879 
2880 
2881 /* Entry point to a generic one-line "if, else endif" function; */
2882 int doIf()
2883 {
2884  int i, ret;
2885  char buf[256], tbuf[512], *cp1=NULL, *cp2=NULL;
2886  char ebuf[256], evalBuf[256], trueCmd[256];
2887 
2889  {
2890  st_print("%s(%s:%d): Enter\n", __func__, __FILE__, __LINE__);
2891  }
2892 
2893  bzero( buf, sizeof( buf));
2894  bzero(tbuf, sizeof(tbuf));
2895 
2896  if(strlen(args->secondArg))
2897  {
2898  reassembleCommand(buf); /* Take all the tokens and reassemble into a single command */
2899 
2900  if(verboseLevel & VERBOSE_PARSE)
2901  {
2902  st_print("%s(%s:%d): Parsing: %s\n", __func__, __FILE__, __LINE__, buf);
2903  }
2904 
2905  expandCmd(buf, tbuf); /* Stretch it out so there are spaces between everything */
2906 
2907  if(verboseLevel & VERBOSE_PARSE)
2908  {
2909  st_print("%s(%s:%d): Now Parsing: %s\n", __func__, __FILE__, __LINE__, tbuf);
2910  }
2911 
2912  substituteVariables(tbuf, ebuf);
2913 
2914  /* Find the limits of the if statement */
2915  cp1 = strchr(ebuf, '(');
2916  cp2 = strrchr(ebuf, ')');
2917 
2918  if(cp1)
2919  {
2920  if(verboseLevel & VERBOSE_PARSE)
2921  {
2922  st_print("%s(%s:%d): cp1: %s\n", __func__, __FILE__, __LINE__, cp1);
2923  }
2924  }
2925 
2926  cp2++;
2927  if(cp2)
2928  {
2929  if(verboseLevel & VERBOSE_PARSE)
2930  {
2931  st_print("%s(%s:%d): cp2: %s\n", __func__, __FILE__, __LINE__, cp2);
2932  }
2933  }
2934 
2935  if(!cp1 || !cp2)
2936  {
2938 
2939  return(1);
2940  }
2941 
2942  strcpy(trueCmd, cp2); /* Save the command to do if test evals to true */
2943  if(verboseLevel & VERBOSE_PARSE)
2944  {
2945  st_print("%s(%s:%d): True Cmd: %s\n", __func__, __FILE__, __LINE__, trueCmd);
2946  }
2947 
2948  strncpy(evalBuf, cp1, cp2-cp1);
2949  evalBuf[cp2-cp1] = '\0';
2950 
2951  if(verboseLevel & VERBOSE_PARSE)
2952  {
2953  st_print("%s(%s:%d): Eval Buf: %s\n", __func__, __FILE__, __LINE__, evalBuf);
2954  }
2955 
2956  /* Evaluate for true; 0, 1 or -1 will be left in env->rst */
2957  ret = doCalculation(evalBuf);
2958 
2959  if(ret)
2960  {
2961  st_report("Test evaluations Failed");
2962  return(1);
2963  }
2964 
2965  if(verboseLevel & VERBOSE_PARSE)
2966  {
2967  st_print("%s(%s:%d): Result: %.1f\n", __func__, __FILE__, __LINE__, env->rst);
2968  }
2969 
2970  if(env->rst > 0.0)
2971  {
2973  }
2974  else
2975  if(env->rst < 0.0)
2976  {
2978  }
2979  else
2980  {
2982  }
2983 
2985  {
2986  if(verboseLevel & VERBOSE_PARSE)
2987  {
2988  st_print("%s(%s:%d): Executing True Cmd: %s\n", __func__, __FILE__, __LINE__, trueCmd);
2989  }
2990 
2992  parseCmd(trueCmd);
2993  }
2994  else /* Find and set up 'else' command, if any, for later */
2995  {
2996  for(i=0;i<args->ncmds;i++)
2997  {
2998  if(verboseLevel & VERBOSE_PARSE)
2999  {
3000  st_print("%s(%s:%d): Searching for 'else' command in: %s:\n", __func__, __FILE__, __LINE__, args->cmd_tok[i]);
3001  }
3002 
3003  if(strstr(args->cmd_tok[i], "else "))
3004  {
3005  if(verboseLevel & VERBOSE_PARSE)
3006  {
3007  st_print("%s(%s:%d): Found 'else' command: %s:\n", __func__, __FILE__, __LINE__, args->cmd_tok[i]);
3008  }
3009 
3012 
3013  break;
3014  }
3015  }
3016  }
3017  }
3018  else
3019  {
3021  return(1);
3022  }
3023 
3024  if(verboseLevel & VERBOSE_PARSE)
3025  {
3026  st_print("%s(%s:%d): Leaving\n", __func__, __FILE__, __LINE__);
3027  }
3028 
3029  return(0);
3030 }
3031 
3032 
3033 int doElse()
3034 {
3036  {
3037  st_print("%s(%s:%d): Enter\n", __func__, __FILE__, __LINE__);
3038  }
3039 
3041  {
3042  if(*lp_if_else_buf && strlen(lp_if_else_buf))
3043  {
3045 
3046  if(verboseLevel & VERBOSE_PARSE)
3047  {
3048  st_print("%s(%s:%d): Leaving\n", __func__, __FILE__, __LINE__);
3049  }
3050 
3051  return(0);
3052  }
3053  }
3054 
3055  /* Let endif clear flags */
3056 
3057  if(verboseLevel & VERBOSE_PARSE)
3058  {
3059  st_print("%s(%s:%d): Leaving\n", __func__, __FILE__, __LINE__);
3060  }
3061 
3062  return(0);
3063 }
3064 
3065 
3066 int doEndIf()
3067 {
3069  {
3070  st_print("%s(%s:%d): Enter\n", __func__, __FILE__, __LINE__);
3071  }
3072 
3074 
3075  if(verboseLevel & VERBOSE_PARSE)
3076  {
3077  st_print("%s(%s:%d): Leaving\n", __func__, __FILE__, __LINE__);
3078  }
3079 
3080  return(0);
3081 }
3082 
3083 
3084 int doFor()
3085 {
3086  st_report("doFor(): Enter");
3087 
3088  return(0);
3089 }
3090 
3091 
3093 {
3094  st_report("doEndFor(): Enter");
3095 
3096  return(0);
3097 }
3098 
3099 
3101 {
3102  double d;
3103  char sexStr[256], tbuf[256];
3104 
3105  if(args->ntokens < 2)
3106  {
3108 
3109  return(0);
3110  }
3111 
3112  if(strstr(args->secondArg, ":")) /* it's alread in sex, reverse it */
3113  {
3114 
3115  strcpy(tbuf, args->secondArg);
3116  strcmprs(tbuf);
3117 
3118  d = ctod(tbuf);
3119 
3120  if(isSet(PF_T_FLAG))
3121  {
3122  d *= 15.0;
3123  }
3124 
3125  if(isSet(PF_EXP_FLAG))
3126  {
3127  st_print("Value = %.8e\n", d);
3128  }
3129  else
3130  {
3131  st_print("Value = %.8f\n", d);
3132  }
3133 
3134  env->rst = d;
3135  }
3136  else /* convert to sex */
3137  {
3138  d = ctod(args->secondArg);
3139 
3140  if(isSet(PF_T_FLAG))
3141  {
3142  d /= 15.0;
3143  }
3144 
3145  sexagesimal(d, sexStr, DDDD_MM_SS_SS100);
3146 
3147  st_print("Value = %s\n", sexStr);
3148  }
3149 
3150  return(0);
3151 }
int xx()
Definition: linuxpops.c:605
int ctrl_e_list(int arg1, int arg2)
Definition: parse.c:2608
#define LP_IF_TEST_TRUE
Definition: parse.c:69
#define DOUBLE
Definition: defines.h:56
int shiftFlags()
Definition: parse.c:622
double ctod(char *str)
int findGSVar(char *name)
Definition: parse.c:948
int getSizeOfGS()
int ctrl_l_list(int arg1, int arg2)
Definition: parse.c:2577
int doCalculation(char *buf)
Definition: parse.c:1802
int * iaddr
Definition: gminit.h:9
#define YELLOW
Definition: colors.h:7
char saveBuf[16384]
Definition: dataserv.c:52
char * strcmprs(char *str)
int recordThisCommand(char *buf)
Definition: parse.c:2000
char * prompt
Definition: makemem.c:101
#define MODE_OTF
Definition: defines.h:88
struct HEAD_PARAMS * findHeaderVal(char *name)
Definition: header.c:239
int first
Definition: dbase.c:106
struct PARSE_STACK * args
Definition: parse.h:196
struct COMMANDS commands[]
Definition: commands.c:76
char * caddr
Definition: gminit.h:10
#define MAX_PARSE_FLAGS
Definition: parse.h:80
#define LP_IF_STATE_ELSE
Definition: parse.c:75
char userfile[256]
Definition: env.h:14
struct PARSE_STACK parse_stack[MAX_PARSE_STACK]
Definition: parse.h:194
struct HEAD_PARAMS phead
Definition: parse.h:208
int printVar()
Definition: parse.c:1121
#define FORMAT_EXP
Definition: parse.c:1118
parse_flags
Definition: parse.h:7
char buf[256]
Definition: parse.h:207
int ctrl_a_list(int arg1, int arg2)
Definition: parse.c:2528
#define VARIABLE_TYPE_DVAR
Definition: parse.c:99
char sv_tok[40][40]
Definition: parse.c:2805
#define LP_IF_TEST_NTRUE
Definition: parse.c:70
int substituteEnv()
Definition: env.c:569
char * args
Definition: commands.h:7
int ctrl_b_list(int arg1, int arg2)
Definition: parse.c:2545
int setDFormat(char *buf)
Definition: parse.c:135
char * seventhArg
Definition: parse.h:186
int ivars_holders[MAX_DYN_VARS]
Definition: parse.c:87
char * cmd
Definition: commands.h:5
#define RED
Definition: colors.h:5
#define MAX_DYN_VARS
Definition: commands.h:27
#define MAX_TOKENS
Definition: defines.h:40
int showVariables()
Definition: parse.c:2777
int ival
Definition: parse.h:201
int getNumericFlag()
Definition: parse.c:196
int max_fix_operators
Definition: parse.c:1441
struct DYNAMIC_VARIABLES dvars[MAX_DYN_VARS]
Definition: parse.c:89
#define RESET
Definition: colors.h:13
char recordingFname[256]
Definition: parse.c:92
int doSexCmd()
Definition: parse.c:3100
FILE * fp
Definition: linuxpops.c:70
char * fifthArg
Definition: parse.h:184
int printH_FlagHelp(char *cmd)
Definition: help.c:430
char * find
Definition: parse.c:1430
#define BLUE
Definition: colors.h:8
int printHeader(int type)
Definition: header.c:328
char * FilterChars(char *String, char *Filter)
Definition: parse.c:2786
int ntoken
Definition: commands.h:6
#define WHITE
Definition: colors.h:11
int stquiet
Definition: env.c:90
char name[80]
Definition: commands.h:37
int setFlag(int flag, int state)
Definition: parse.c:685
int isBatch(char *name)
Definition: batch.c:604
struct PARSE_FLAGS pflags[MAX_PARSE_FLAGS]
Definition: parse.h:178
int tellEnv()
Definition: env.c:360
int parseColor(void)
int index
Definition: parse.h:87
#define RESERVE
Definition: colors.h:12
char tok[MAX_TOKENS][MAX_TOKENS]
Definition: parse.h:176
#define DDDD_MM_SS_SS100
Definition: sex.h:13
int printBatch(char *name)
Definition: batch.c:576
double dval
Definition: parse.h:205
#define INTEGER
Definition: defines.h:54
int recordCmds()
Definition: parse.c:2724
#define LP_IF_STATE_TRUE
Definition: parse.c:73
char * replace
Definition: parse.c:1431
int showLetHelp()
Definition: parse.c:701
int isnumber(char *string)
double * dval
Definition: commands.h:38
#define CYAN
Definition: colors.h:10
#define LP_IF_STATE_IDLE
Definition: parse.c:72
#define DYN_TYPE_INT
Definition: commands.h:30
int notSet(int flag)
Definition: parse.c:669
int size
Definition: gminit.h:8
int recursiveLevel
Definition: makemem.c:93
char * help
Definition: commands.h:11
int ncmds
Definition: parse.h:172
int printPlanetVar(char *name)
Definition: planets.c:1614
int ctrl_p_list(int arg1, int arg2)
Definition: parse.c:2593
int isSpecial(char cp)
Definition: parse.c:1414
char name[256]
Definition: parse.h:200
#define CLASS_MAPPING
Definition: head.h:7
int sexagesimal(double dang, char *str, int prec)
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int ndvars_used
Definition: global.h:61
int letterHelp(char *h)
Definition: help.c:108
int clearAllVar()
Definition: parse.c:1027
char * thirdArg
Definition: parse.h:182
double * dptr
Definition: head.h:13
int isPlanetVar(char *name)
Definition: planets.c:1487
struct FIX_OPERATORS fixers[]
Definition: parse.c:1434
int get_sv_tok(char *edit)
Definition: parse.c:2811
#define MAX_PARSE_STACK
Definition: defines.h:150
char * modeStrs[]
Definition: strings.c:90
int isSet(int flag)
Definition: parse.c:646
int parseFlags()
Definition: parse.c:497
int doColor
Definition: xgr.c:13
int popArgs()
Definition: parse.c:405
int fixMathOperators(char *buf)
Definition: parse.c:1447
int substituteVariables(char *inbuf, char *outbuf)
Definition: parse.c:2831
#define FORMAT_HEX
Definition: parse.c:1119
#define DYN_VARIBLE_PROTECTED
Definition: commands.h:34
int removeAllSpaces(char *in, char *out)
Definition: parse.c:1771
int doEndFor()
Definition: parse.c:3092
int get_cmd_tok(char *edit)
Definition: parse.c:1924
int doSummary()
Definition: sdd.c:369
char cval[256]
Definition: parse.h:206
int get_tok(char *edit)
Definition: make_env.c:69
char * name
Definition: parse.h:85
char * lcalc(char *cmdbuf)
Definition: lcalc.c:266
int doEndIf()
Definition: parse.c:3066
int fetchNGSHelp(char *name, char *buf)
Definition: env_help.c:264
void st_fail()
int printStatus()
Definition: header.c:729
struct PARSE_FLAGS * findFirstFlag()
Definition: parse.c:602
int strcmp()
#define DYN_TYPE_DOUBLE
Definition: commands.h:31
int findVariable(char *var)
Definition: parse.c:984
int lp_if_test_rst
Definition: parse.c:77
double * daddr
Definition: gminit.h:11
int showParams()
Definition: params.c:662
char * colors[]
int isCommand(char *buf)
Definition: parse.c:1981
#define VARIABLE_TYPE_HEADER
Definition: parse.c:101
int indx
Definition: parse.h:204
struct GLOBAL_MEMORY * CGM
Definition: global.h:66
int init_readline(void)
Definition: parse.c:2631
#define CLASS_ALL
Definition: head.h:4
#define VERBOSE_FLAGS
Definition: defines.h:78
int ctrl_t_list(int arg1, int arg2)
Definition: parse.c:2562
int stype
Definition: parse.h:203
int expandCmd(char *src, char *dst)
Definition: parse.c:1494
int oldColor
Definition: parse.c:82
int isFirst(int flag)
Definition: parse.c:662
#define CLASS_SOURCE
Definition: head.h:5
int reassembleCommand(char *buf)
Definition: parse.c:480
int mode
Definition: commands.h:10
int listDynVars()
Definition: parse.c:728
int initDvars()
Definition: parse.c:967
int mode
Definition: env.h:75
int createDouble()
Definition: parse.c:1101
#define MODE_SPEC
Definition: defines.h:87
int clearVar()
Definition: parse.c:1001
int setColor(int c)
Definition: parse.c:2644
char myline[4096]
Definition: parse.c:91
int(* fctn)()
Definition: commands.h:8
int clearFlags()
Definition: parse.c:583
#define CHARSTAR
Definition: defines.h:57
#define CLASS_TELESCOPE
Definition: head.h:9
char * sixthArg
Definition: parse.h:185
void st_report()
int old_quiet
Definition: parse.c:84
int execBatch(char *name)
Definition: batch.c:315
int my_add_history(char *buf)
Definition: parse.c:109
struct GSKEY getset[]
char * token
Definition: parse.h:84
#define LP_IF_TEST_FALSE
Definition: parse.c:68
#define FAIL_INFO(msg)
Definition: info.h:4
int type
Definition: head.h:15
#define FORMAT_NORMAL
Definition: parse.c:1117
double rst
Definition: env.h:263
int recording
Definition: parse.c:83
int set
Definition: parse.h:88
int doLetCmd(char *buf)
Definition: parse.c:1600
#define CLASS_WEATHER
Definition: head.h:8
char * eighthArg
Definition: parse.h:187
char * fourthArg
Definition: parse.h:183
int initParseArgs()
Definition: parse.c:352
int doIf()
Definition: parse.c:2882
int findAmbiguousBatch(char *arg, char *buf)
Definition: batch.c:645
int findInArgs(char *parg)
Definition: parse.c:2704
int doElse()
Definition: parse.c:3033
char currentCmd[256]
Definition: parse.h:191
int printAmbiguousCommand(char *arg, struct COMMANDS *cmds)
Definition: parse.c:2032
int order
Definition: parse.h:89
int doAutoScale()
Definition: linuxpops.c:2788
int getNset()
Definition: parse.c:214
void subArgs(int wt, const char *fmt,...)
Definition: parse.c:430
#define BLACK
Definition: colors.h:4
#define REPORT_INFO(msg)
Definition: info.h:5
char * cptr
Definition: head.h:14
char lp_if_else_buf[512]
Definition: parse.c:80
int showShortCuts()
Definition: parse.c:2623
Definition: gminit.h:4
int size
Definition: head.h:16
int(* post)()
Definition: commands.h:12
int findAnyVariable(char *name, struct QUERY_VAR *qv)
Definition: parse.c:237
char * secondArg
Definition: parse.h:181
char * name
Definition: gminit.h:6
void st_print()
char cmd_tok[MAX_TOKENS][MAX_TOKENS]
Definition: parse.h:175
int listScans()
Definition: linuxpops.c:1124
int ntokens
Definition: parse.h:173
int compressReassembleTokens()
Definition: parse.c:461
int substituteAliases(char *buf)
Definition: alias.c:223
int doFor()
Definition: parse.c:3084
int updateHeader()
Definition: header.c:263
char last_history[4096]
Definition: parse.c:105
double dvars_holders[MAX_DYN_VARS]
Definition: parse.c:88
int bang(char *buf)
Definition: parse.c:1943
#define MAX_COLORS
Definition: colors.h:15
int lp_if_test_state
Definition: parse.c:78
char * firstArg
Definition: parse.h:180
int parseCmd(char *buf)
Definition: parse.c:2090
int fileChanged()
Definition: io.c:402
#define VARIABLE_TYPE_GSVAR
Definition: parse.c:100
int ntype
Definition: parse.h:202
#define MODE_ALL
Definition: defines.h:85
void process_line(char *line)
Definition: parse.c:2452
int doBaseline()
Definition: baseline.c:77
char * ninethArg
Definition: parse.h:188
int makeDynVariable(char *var, int type)
Definition: parse.c:1049
#define VERBOSE_PARSE
Definition: makemem.c:90
int evaluateRightSide(int k, int n, char *buf)
Definition: parse.c:1542
int type
Definition: gminit.h:7
#define MAGENTA
Definition: colors.h:9
FILE * errPrint_fp
Definition: linuxpops.c:70
int createInteger()
Definition: parse.c:1086
char * tenthArg
Definition: parse.h:189
int pushArgs()
Definition: parse.c:383
int verboseLevel
Definition: linuxpops.c:80
#define GREEN
Definition: colors.h:6