Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
formula.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 #include "extern.h"
59 
61 
62 
64 {
65  int n;
66  char cmdBuf[256];
67  struct FORMULA *f;
68 
69  n = getNumericFlag();
70 
71  if(n<0)
72  {
73  st_fail("Missing numeric flag for formula /x");
74 
76 
77  return(1);
78  }
79 
80  f = &formulas.formulas[n];
81 
82  if(f->used)
83  {
84  if(isSet(PF_V_FLAG))
85  {
86  st_report("Executing formula %d: %s", n, f->formula);
87  }
88  else
89  {
90  st_report("Executing formula %d", n);
91  }
92 
93 
94  sprintf(cmdBuf, "eval %s", f->formula);
95  parseCmd(cmdBuf);
96  }
97  else
98  {
99  st_fail("Formula: %d, not defined", n);
100  }
101 
102  return(0);
103 }
104 
105 
107 {
108  int n;
109  struct FORMULA *f;
110 
111  n = getNumericFlag();
112 
113  if(n<0)
114  {
115  st_fail("Missing numeric flag for formula /c");
116 
118 
119  return(1);
120  }
121 
122  st_report("Clearing formula %d", n);
123 
124  f = &formulas.formulas[n];
125 
126  strcpy(f->formula, "");
127  f->used = 0;
128 
129  return(0);
130 }
131 
132 
134 {
135  int i, n;
136  char tbuf[512], scratch[512];
137  struct FORMULA *f;
138 
139  n = getNumericFlag();
140 
141  if(n<0)
142  {
143  st_fail("Missing numeric flag for formula /d");
144 
146 
147  return(1);
148  }
149 
150  f = &formulas.formulas[n];
151 
152  tbuf[0] = '\0';
153  for(i=1;i<args->ntokens;i++)
154  {
155  sprintf(scratch, "%s ", args->tok[i]);
156  strcat(tbuf, scratch);
157  }
158 
159  strcmprs(tbuf);
160 
161  if(strlen(tbuf) < MAX_FORMULA_LEN)
162  {
163  strcpy(f->formula, tbuf);
164  f->used = 1;
165  st_report("Defined Formula: %d as %s", n, f->formula);
166  }
167  else
168  {
169  st_fail("Formula to long for buffers");
170  return(1);
171  }
172 
173  return(0);
174 }
175 
176 
178 {
179  FILE *fp;
180  char *string, line[256];
181 
182  if(strlen(args->secondArg))
183  {
184  if((fp = fopen(args->secondArg, "r")) == NULL)
185  {
186  if(isSet(PF_I_FLAG)) /* Ignore any errors */
187  {
188  st_report("No %s formula to load");
189  return(0);
190  }
191 
192  st_fail("Unable to open %s", args->secondArg);
193  return(1);
194  }
195 
196  st_report("Reading Formulas from %s", args->secondArg);
197  }
198  else
199  {
200  st_fail("Missing filename args for 'formula /r'");
201  return(1);
202  }
203 
204  while((string = fgets(line, sizeof(line), fp)) != NULL)
205  {
206  if(line[0] == '#')
207  {
208  continue;
209  }
210 
211  parseCmd(line);
212  }
213 
214  fclose(fp);
215 
216 
217  return(0);
218 }
219 
221 {
222  int i, j = 0;
223  FILE *fp;
224  struct FORMULA *f;
225 
226  if(strlen(args->secondArg))
227  {
228  if((fp = fopen(args->secondArg, "w")) == NULL)
229  {
230  st_fail("Unable to open %s", args->secondArg);
231  return(1);
232  }
233 
234  st_report("Saving Formulas to %s", args->secondArg);
235  }
236  else
237  {
238  st_fail("Missing filename args for 'formula /w'");
239  return(1);
240  }
241 
242  f = &formulas.formulas[0];
243 
244  for(i=0;i<MAX_FORMULAS;i++,f++)
245  {
246  if(f->used)
247  {
248  fprintf(fp, "formula /q /d /%d %s\n", i, f->formula);
249  j++;
250  }
251  }
252 
253  fclose(fp);
254 
255  st_report("Wrote %d formulas to %s", j, args->secondArg);
256 
257  return(0);
258 }
259 
260 
262 {
263  int i, first = 1;
264  struct FORMULA *f;
265 
266  f = &formulas.formulas[0];
267 
268  for(i=0;i<MAX_FORMULAS;i++,f++)
269  {
270  if(f->used)
271  {
272 
273  if(first)
274  {
275  st_print("\n");
276  st_print(" # Formula\n");
277  st_print("\n");
278 
279  first = 0;
280  }
281 
282  st_print("%2d: %s\n", i, f->formula);
283  }
284  }
285 
286  if(first)
287  {
288  st_report("No Formulas defined");
289  }
290  else
291  {
292  st_print("\n");
293  }
294 
295  return(0);
296 }
297 
298 
300 {
301  st_report("Erasing all formulas");
302 
303  bzero((char *)&formulas, sizeof(formulas));
304 
305  return(0);
306 }
307 
308 
309 
311 {
312  if(isSet(PF_E_FLAG))
313  {
314  return(eraseFormulas());
315  }
316 
317  if(isSet(PF_C_FLAG))
318  {
319  return(clearFormula());
320  }
321 
322  if(isSet(PF_D_FLAG))
323  {
324  return(defineFormula());
325  }
326 
327  if(isSet(PF_R_FLAG))
328  {
329  return(readFormulas());
330  }
331 
332  if(isSet(PF_W_FLAG))
333  {
334  return(writeFormulas());
335  }
336 
337  if(isSet(PF_L_FLAG))
338  {
339  return(listFormulas());
340  }
341 
342  if(isSet(PF_X_FLAG))
343  {
344  return(executeFormula());
345  }
346 
347  return(0);
348 }
int executeFormula()
Definition: formula.c:63
int eraseFormulas()
Definition: formula.c:299
char * strcmprs(char *str)
int first
Definition: dbase.c:106
struct PARSE_STACK * args
Definition: parse.h:196
int writeFormulas()
Definition: formula.c:220
Definition: formula.h:7
int doFormula()
Definition: formula.c:310
int getNumericFlag()
Definition: parse.c:196
FILE * fp
Definition: linuxpops.c:70
int printH_FlagHelp(char *cmd)
Definition: help.c:430
int listFormulas()
Definition: formula.c:261
int used
Definition: formula.h:8
char tok[MAX_TOKENS][MAX_TOKENS]
Definition: parse.h:176
#define MAX_FORMULAS
Definition: formula.h:5
#define MAX_FORMULA_LEN
Definition: formula.h:4
int isSet(int flag)
Definition: parse.c:646
int n
Definition: formula.h:15
int defineFormula()
Definition: formula.c:133
void st_fail()
void st_report()
int readFormulas()
Definition: formula.c:177
struct FORMULA formulas[MAX_FORMULAS]
Definition: formula.h:18
struct FORMULAS formulas
Definition: formula.c:60
char formula[MAX_FORMULA_LEN]
Definition: formula.h:11
char * secondArg
Definition: parse.h:181
void st_print()
int ntokens
Definition: parse.h:173
char * firstArg
Definition: parse.h:180
int parseCmd(char *buf)
Definition: parse.c:2090
int clearFormula()
Definition: formula.c:106