Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
batch.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 #include "extern.h"
60 
62 
63 
65 
66 int batch_get_tok(edit)
67 char *edit;
68 {
69  int c = 0;
70  char *token;
71 
72  bzero((char *)&batch_tok, sizeof(batch_tok));
73 
74  token = strtok(edit, " \r\t\n");
75  while(token != (char *)NULL)
76  {
77  sprintf(batch_tok[c++], "%s", token);
78  token = strtok((char *)NULL, " \r\t\n");
79  }
80 
81  return(c);
82 }
83 
84 
85 int doBatch()
86 {
87  int ret;
88  char name[256], fname[256], sysBuf[512];
89 
90  if(isSet(PF_L_FLAG))
91  {
92  listBatch();
93 
94  return(0);
95  }
96 
97  if(!strlen(args->secondArg))
98  {
99  st_fail("Usage: batch batch_file_name");
100  return(1);
101  }
102 
103  if(!strcmp(args->secondArg, "clear"))
104  {
105  if(strlen(args->thirdArg))
106  {
107  strcpy(name, args->thirdArg);
108  clearBatch(name);
109 
110  return(0);
111  }
112  else
113  {
114  st_fail("Missing arg for 'batch clear' command");
115  return(1);
116  }
117  }
118 
119  if(!strcmp(args->secondArg, "edit"))
120  {
121  if(strlen(args->thirdArg))
122  {
123  strcpy(name, args->thirdArg);
124  ret = isBatch(name);
125 
126  if(ret >= 0)
127  {
128  strcpy(fname, bcommands[ret].fname);
129  sprintf(sysBuf, "vi %s", fname);
130 
131  system(sysBuf);
132 
133  return(0);
134  }
135  else
136  {
137  st_fail("Unable to locate an internal procedure named: %s", name);
138  return(0);
139  }
140  }
141  else
142  {
143  st_fail("Missing arg for 'batch edit' command");
144  return(1);
145  }
146  }
147 
148  /* default action */
150 
151  return(0);
152 }
153 
154 
156 char *name;
157 {
158  int n;
159  struct BATCH_COMMAND *p;
160 
161  n = isBatch(name);
162 
163  if(n >= 0)
164  {
165  p = &bcommands[n];
166 
167  bzero((char *)p, sizeof(struct BATCH_COMMAND));
168 
169  return(0);
170  }
171  else
172  {
173  st_fail("Unable to locate batch file %s", name);
174  }
175 
176  return(1);
177 }
178 
179 
180 int loadBatch(bname)
181 char *bname;
182 {
183  int i;
184  char *cp, line[256], *string;
185  char *home, *init, *proc;
186  char pname1[256], pname2[256], pname3[256];
187  FILE *fp;
188  struct BATCH_COMMAND *p;
189 
190  /* The batch file may be in any number of places; try them all */
191 
192  home = getenv("HOME");
193  init = getenv("OBSINIT");
194  proc = getenv("LINUXPOPS_HOME");
195 
196  st_print("loadBatch(): Looking for batch file: %s\n", bname);
197 
198  if(home)
199  {
200  sprintf(pname1, "%s/%s", home, bname);
201  }
202 
203  if(init && home)
204  {
205  sprintf(pname2, "%s/%s/%s", home, init, bname);
206  }
207 
208  if(proc)
209  {
210  sprintf(pname3, "%s/procedures/%s", proc, bname);
211  }
212 
213  st_print("loadBatch(): %s\n", pname1);
214  st_print("loadBatch(): %s\n", pname2);
215  st_print("loadBatch(): %s\n", pname3);
216 
217  /* First try the bare name */
218  if((fp = fopen(bname, "r") ) > (FILE *)0)
219  {
220  st_print("Found batch file ./here\n");
221  }
222  else /* Try home/batchfile */
223  if((fp = fopen(pname1, "r") ) > (FILE *)0)
224  {
225  st_print("Found batch file in HOME \n");
226  }
227  else /* Try home/initials/batchfile */
228  if((fp = fopen(pname2, "r") ) > (FILE *)0)
229  {
230  st_print("Found batch file in HOME/INIT \n");
231  }
232  else /* Try $LINUXPOPS_HOME/procedures/batchfile */
233  if((fp = fopen(pname3, "r") ) > (FILE *)0)
234  {
235  st_print("Found batch file in $LINUXPOPS_HOME/procedures\n");
236  }
237 
238  if(!fp)
239  {
240  st_fail("Unable to open file %s", bname);
241  return(0);
242  }
243 
244  st_report("loadBatch(): Loading: %s", bname);
245 
246  p = getFreeBatch(bname);
247 
248  if(!p)
249  {
250  st_fail("Unable to allocate free Batch memory");
251  return(0);
252  }
253 
254  /* Clear it out just in case it's reused */
255  bzero((char *)p, sizeof(struct BATCH_COMMAND));
256 
257  i = 0;
258 
259  p->used = 1;
260  strcpy(p->fname, args->secondArg);
261 
262  while((string = fgets(line, sizeof(line), fp)) != NULL)
263  {
264  deNewLineIt(line);
265  strcmprs(line);
266  st_print("%s\n", line);
267 
268  if(strlen(line) < MAX_BATCH_LWIDTH)
269  {
270  strcmprs(line); /* strip excess spaces */
271 
272  if((!strncmp(line, "procedure", 9)))
273  {
274  strcpy(p->name, line+10);
275  }
276  else
277  if((cp = strstr(line, "ntoken? ")))
278  {
279  p->ntokens = atoi(cp+8);
280  }
281  else /* Copy into batch */
282  {
283  strcpy(p->lines[i++], line);
284 
285  /* Look for keywords */
286  if(!strncmp(line, "#", 1) && (cp = strstr(line, "Usage")))
287  {
288  strcpy(p->usage, cp);
289  }
290  }
291  }
292  else
293  {
294  st_fail("Batch file line too long");
295  }
296 
297  if(i >= MAX_BATCH_LINES)
298  {
299  st_fail("Too many lines, %d, in batch file %s", i, bname);
300  fclose(fp);
301  p->nlines = i;
302 
303  return(1);
304  }
305  }
306 
307  fclose(fp);
308 
309  p->nlines = i;
310 
311  return(0);
312 }
313 
314 
316 char *name;
317 {
318  int i, j, n, ret, lptokens, pause=0, loophead = 0;
319  char *cp, tbuf[256], scanBuf[256],
320  arg1[256], arg2[256], arg3[256], arg4[256], arg5[256], arg6[256], arg7[256], arg8[256];
321  static struct BATCH_COMMAND *p = NULL;
322 
323  lptokens = args->ntokens; /* Remember number of tokens passed to batch file */
324 
325  ret = isBatch(name);
326 
327  pause = isSet(PF_P_FLAG);
328 
329  if(ret >= 0)
330  {
331  /* remember args; because they are going to get stomped on */
332  /* by all the commands we are about to execute. */
333  strcpy(arg1, args->firstArg);
334  strcpy(arg2, args->secondArg);
335  strcpy(arg3, args->thirdArg);
336  strcpy(arg4, args->fourthArg);
337  strcpy(arg5, args->fifthArg);
338  strcpy(arg6, args->sixthArg);
339  strcpy(arg7, args->seventhArg);
340  strcpy(arg8, args->eighthArg);
341 
343  {
344  st_print("execBatch(): Arg1 = %s\n", arg1);
345  st_print("execBatch(): Arg2 = %s\n", arg2);
346  st_print("execBatch(): Arg3 = %s\n", arg3);
347  st_print("execBatch(): Arg4 = %s\n", arg4);
348  st_print("execBatch(): Arg5 = %s\n", arg5);
349  st_print("execBatch(): Arg6 = %s\n", arg6);
350  st_print("execBatch(): Arg7 = %s\n", arg7);
351  st_print("execBatch(): Arg8 = %s\n", arg8);
352  st_print("execBatch(): pToken = %d\n", lptokens);
353  }
354 
355  p = &bcommands[ret];
356 
357  for(i=0;i<MAX_BATCH_LINES;i++)
358  {
359  if(strlen(p->lines[i]))
360  {
361  /* Use a local copy of the commands because we will
362  be modifing the command substituting values in
363  place of args */
364  strcpy(tbuf, p->lines[i]);
365 
366  if(!strncmp(tbuf, "#", 1)) /* Skip comments */
367  {
368  continue;
369  }
370 
371  if(verboseLevel & VERBOSE_PARSE)
372  {
373 // st_report("execBatch(): Found ntoken: %s", cp);
374  st_report("execBatch(): ptokens: %d", lptokens);
375  }
376 
377  env->btokens = lptokens;
378 
379  if(lptokens < p->ntokens+1)
380  {
381  st_fail("%s: Wrong number of args: Needs at least: %d", name, p->ntokens);
382 
383  st_print("%s\n", p->usage);
384 
385  return(1);
386  }
387 
389  {
390  st_report("execBatch(): Before Parsing: %s", tbuf);
391  }
392 
393  n = batch_get_tok(tbuf);
394 
395  if(verboseLevel & VERBOSE_BATCH)
396  {
397  st_report("execBatch(): Found %d tokens in this line", n);
398  }
399 
400  bzero(tbuf, sizeof(tbuf));
401 
402  for(j=0;j<n;j++)
403  {
404  /* Substitue args here */
405  if(verboseLevel & VERBOSE_BATCH)
406  {
407  st_report("execBatch(): Comparing: %s ", batch_tok[j]);
408  }
409 
410  if(!strcasecmp(batch_tok[j], "secondArg"))
411  {
412  strcat(tbuf, arg2);
413  strcat(tbuf, " ");
414 
415  if(verboseLevel & VERBOSE_BATCH)
416  {
417  st_report("execBatch(): #2 %s", tbuf);
418  }
419  }
420  else
421  if(!strcasecmp(batch_tok[j], "thirdArg"))
422  {
423  strcat(tbuf, arg3);
424  strcat(tbuf, " ");
425 
426  if(verboseLevel & VERBOSE_BATCH)
427  {
428  st_report("execBatch(): #3 %s", tbuf);
429  }
430  }
431  else
432  if(!strcasecmp(batch_tok[j], "fourthArg"))
433  {
434  strcat(tbuf, arg4);
435  strcat(tbuf, " ");
436 
437  if(verboseLevel & VERBOSE_BATCH)
438  {
439  st_report("execBatch(): #4 %s", tbuf);
440  }
441  }
442  else
443  if(!strcasecmp(batch_tok[j], "fifthArg"))
444  {
445  strcat(tbuf, arg5);
446  strcat(tbuf, " ");
447 
448  if(verboseLevel & VERBOSE_BATCH)
449  {
450  st_report("execBatch(): #5 %s", tbuf);
451  }
452  }
453  else
454  if(!strcasecmp(batch_tok[j], "sixthArg"))
455  {
456  strcat(tbuf, arg6);
457  strcat(tbuf, " ");
458 
459  if(verboseLevel & VERBOSE_BATCH)
460  {
461  st_report("execBatch(): #6 %s", tbuf);
462  }
463  }
464  else
465  if(!strcasecmp(batch_tok[j], "seventhArg"))
466  {
467  strcat(tbuf, arg7);
468  strcat(tbuf, " ");
469 
470  if(verboseLevel & VERBOSE_BATCH)
471  {
472  st_report("execBatch(): #7 %s", tbuf);
473  }
474  }
475  else
476  if(!strcasecmp(batch_tok[j], "eightArg"))
477  {
478  strcat(tbuf, arg8);
479  strcat(tbuf, " ");
480 
481  if(verboseLevel & VERBOSE_BATCH)
482  {
483  st_report("execBatch(): #8 %s", tbuf);
484  }
485  }
486  else
487  {
488  strcat(tbuf, batch_tok[j]);
489  strcat(tbuf, " ");
490  }
491  }
492 
493  if(verboseLevel & VERBOSE_BATCH)
494  {
495  st_report("execBatch(): After reassemble: %s", tbuf);
496  }
497 
498  if((cp = strcasestr(tbuf, "stackscan")))
499  {
500  sprintf(scanBuf, "%.2f", env->stackscan);
501 
502  strcpy(cp, scanBuf);
503 
504  if(verboseLevel & VERBOSE_BATCH)
505  {
506  st_report("execBatch(): %s", tbuf);
507  }
508  }
509 
510  if(strstr(tbuf, "startstack"))
511  {
512  loophead = i;
513 
514  if(verboseLevel & VERBOSE_BATCH)
515  {
516  st_report("execBatch(): Found Top of Stack Loop: indx %d", loophead);
517  }
518 
519  continue;
520  }
521 
522  if(strstr(tbuf, "endstack"))
523  {
524  if(verboseLevel & VERBOSE_BATCH)
525  {
526  st_report("execBatch(): Found End of Stack Loop");
527  }
528 
529  if(env->stackdone) /* Test for end of stack */
530  {
531  strcpy(tbuf, "#"); /* Make it a empty command */
532  }
533  else
534  {
535  i = loophead;
536  continue;
537  }
538  }
539 
540  if(strstr(tbuf, "pause"))
541  {
542  if(doPause() == 2)
543  {
544  return(0);
545  }
546 
547  continue;
548  }
549 
550  if(verboseLevel & VERBOSE_BATCH)
551  {
552  st_report("Parsing: %s", tbuf);
553  }
554 
555  parseCmd(tbuf);
556 
557  if(pause)
558  {
559  if(doPause() == 2)
560  {
561  return(0);
562  }
563  }
564  }
565  }
566  }
567  else
568  {
569  st_fail("execBatch(): Batch procedure: %s, not found", name);
570  }
571 
572  return(0);
573 }
574 
575 
577 char *name;
578 {
579  int i, ret;
580  static struct BATCH_COMMAND *p = NULL;
581 
582  ret = isBatch(name);
583 
584  if(ret >= 0)
585  {
586  p = &bcommands[ret];
587  for(i=0;i<MAX_BATCH_LINES;i++)
588  {
589  if(strlen(p->lines[i]))
590  {
591  st_report("%s", p->lines[i]);
592  }
593  }
594  }
595  else
596  {
597  st_fail("printBatch(): Batch procedure: %s, not found", name);
598  }
599 
600  return(0);
601 }
602 
603 
605 char *name;
606 {
607  int i;
608  static struct BATCH_COMMAND *p = NULL;
609 
610  p = &bcommands[0];
611  for(i=0;i<MAX_BATCH_COMMANDS;i++,p++)
612  {
613  if(p->used && (!strcmp(p->name, name) || !strcmp(p->fname, name)))
614  {
615  return(i);
616  }
617  }
618 
619  return(-1);
620 }
621 
622 
623 
625 {
626  int i;
627  static struct BATCH_COMMAND *p = NULL;
628 
629  st_print(" Slot: Filename Procedure n-Lines nTokens Usage\n\n");
630 
631  p = &bcommands[0];
632  for(i=0;i<MAX_BATCH_COMMANDS;i++,p++)
633  {
634  if(p->used)
635  {
636  st_print("%3d:%16.16s%16.16s %4d %d '%s'\n",
637  i, p->fname, p->name, p->nlines, p->ntokens, p->usage);
638  }
639  }
640 
641  return(0);
642 }
643 
644 
645 int findAmbiguousBatch(arg, buf)
646 char *arg;
647 char *buf;
648 {
649  int i, len, found=0;
650  char tbuf[256];
651  static struct BATCH_COMMAND *p = NULL;
652 
653  p = &bcommands[0];
654 
655  len = strlen(arg);
656 
657  for(i=0;i<MAX_BATCH_COMMANDS;i++,p++)
658  {
659  if(p->used)
660  {
661  if(!strncasecmp(p->name, arg, len))
662  {
663  sprintf(tbuf, "%s ", p->name);
664  strcat(buf, tbuf);
665 
666  found++;
667  }
668  }
669  }
670 
671  return(found);
672 }
673 
674 
675 
677 char *name;
678 {
679  int i;
680  static struct BATCH_COMMAND *p = NULL;
681 
682  /* First make sure this procedure isn't already named */
683  p = &bcommands[0];
684  for(i=0;i<MAX_BATCH_COMMANDS;i++,p++)
685  {
686  if(p->used && (!strcmp(p->name, name) || !strcmp(p->fname, name)))
687  {
688  st_report("Re-Loading Procedure %s into Slot %d", name, i);
689  return(p);
690  }
691  }
692 
693  /* OK, its a new one */
694  p = &bcommands[0];
695  for(i=0;i<MAX_BATCH_COMMANDS;i++,p++)
696  {
697  if(!p->used)
698  {
699  st_report("Loading New Procedure %s into Slot %d", name, i);
700  return(p);
701  }
702  }
703 
704  p = (struct BATCH_COMMAND *)NULL;
705 
706  return(p);
707 }
708 
709 
710 
711 int doEnd()
712 {
713  st_report("End of procedure reached");
714 
715  return(0);
716 }
#define VERBOSE_BATCH
Definition: defines.h:74
char * strcmprs(char *str)
double stackscan
Definition: env.h:268
struct PARSE_STACK * args
Definition: parse.h:196
int deNewLineIt(char *buf)
int doPause()
Definition: linuxpops.c:3093
char fname[128]
Definition: batch.h:9
char * seventhArg
Definition: parse.h:186
#define MAX_BATCH_LWIDTH
Definition: batch.h:5
#define MAX_TOKENS
Definition: defines.h:40
FILE * fp
Definition: linuxpops.c:70
struct BATCH_COMMAND * getFreeBatch(char *name)
Definition: batch.c:676
char * fifthArg
Definition: parse.h:184
int isBatch(char *name)
Definition: batch.c:604
struct BATCH_COMMAND bcommands[MAX_BATCH_COMMANDS]
Definition: batch.c:61
int ntokens
Definition: batch.h:13
int printBatch(char *name)
Definition: batch.c:576
struct ENVIRONMENT * env
Definition: linuxpops.c:71
int doEnd()
Definition: batch.c:711
char * thirdArg
Definition: parse.h:182
int doBatch()
Definition: batch.c:85
int isSet(int flag)
Definition: parse.c:646
#define MAX_BATCH_COMMANDS
Definition: batch.h:6
int stackdone
Definition: env.h:137
int loadBatch(char *bname)
Definition: batch.c:180
char batch_tok[MAX_TOKENS][MAX_TOKENS]
Definition: batch.c:64
void st_fail()
int strcmp()
int btokens
Definition: env.h:153
int used
Definition: batch.h:11
char * sixthArg
Definition: parse.h:185
void st_report()
int execBatch(char *name)
Definition: batch.c:315
char name[32]
Definition: batch.h:10
char * eighthArg
Definition: parse.h:187
char * fourthArg
Definition: parse.h:183
#define MAX_BATCH_LINES
Definition: batch.h:4
int findAmbiguousBatch(char *arg, char *buf)
Definition: batch.c:645
int clearBatch(char *name)
Definition: batch.c:155
char * secondArg
Definition: parse.h:181
void st_print()
int ntokens
Definition: parse.h:173
char usage[256]
Definition: batch.h:14
int batch_get_tok(char *edit)
Definition: batch.c:66
char * firstArg
Definition: parse.h:180
int parseCmd(char *buf)
Definition: parse.c:2090
int listBatch()
Definition: batch.c:624
int nlines
Definition: batch.h:12
char lines[MAX_BATCH_LINES][MAX_BATCH_LWIDTH]
Definition: batch.h:15
#define VERBOSE_PARSE
Definition: makemem.c:90
int verboseLevel
Definition: linuxpops.c:80