source: PSPA/madxPSPA/src/mad_exec.c @ 457

Last change on this file since 457 was 430, checked in by touze, 11 years ago

import madx-5.01.00

File size: 12.8 KB
Line 
1#include "madx.h"
2
3static void
4exec_delete_sequ(char* name)
5{
6  struct sequence* keep = current_sequ;
7  int spos;
8  if ((spos = name_list_pos(name, sequences->list)) >= 0)
9  {
10    current_sequ = sequences->sequs[spos];
11    if (current_sequ->ex_start != NULL) /* delete expanded */
12    {
13      current_sequ->ex_nodes = delete_node_list(current_sequ->ex_nodes);
14      current_sequ->ex_start = delete_node_ring(current_sequ->ex_start);
15      current_sequ->orbits = delete_vector_list(current_sequ->orbits);
16    }
17    sequences->sequs[spos] = delete_sequence(current_sequ);
18    remove_from_sequ_list(current_sequ, sequences);
19    current_sequ = keep; 
20  }
21  else warning("sequence to be deleted does not exist:", name);
22}
23
24void
25exec_delete_table(char* name)
26{
27  struct table_list* tl;
28  int j, k, pos;
29  for (j = 0; j < all_table_lists->curr; j++)
30  {
31    tl = all_table_lists->table_lists[j];
32    if ((pos = name_list_pos(name, tl->names)) >= 0)
33    {
34      tl->tables[pos] = delete_table(tl->tables[pos]);
35      k = remove_from_name_list(name, tl->names);
36      tl->tables[k] = tl->tables[--tl->curr];
37      return;
38    }
39  }
40}
41
42// public interface
43
44void
45exec_option(void)
46{
47  if (get_option("reset")) set_defaults("option");
48  if (get_option("tell")) print_command(options);
49
50}
51
52void
53exec_help(struct in_cmd* cmd)
54  /* prints list of commands */
55{
56  char** toks = cmd->tok_list->p;
57  int i, k = 0, pos, n = cmd->tok_list->curr;
58  if (n == 1)
59  {
60    while (special_comm_cnt[k] > 0) k++;
61    puts("special commands - no further help:");
62    puts(" ");
63    for (i = 0; i < k-1; i++)
64    {
65      if (strchr(special_comm_desc[i], '(') != NULL)
66        fprintf(prt_file, "%s<condition>){<statements(s)>}\n",
67                &special_comm_desc[i][0]);
68      else if (strchr(special_comm_desc[i], '{') != NULL)
69        fprintf(prt_file, "%s<statements(s)>}\n",
70                &special_comm_desc[i][0]);
71      else fprintf(prt_file, "%s{<statements(s)>}\n",
72                   &special_comm_desc[i][0]);
73    }
74    fprintf(prt_file, "<name>:line(...);\n");
75    puts(" ");
76    puts("normal commands or predefined particles:");
77    dump_name_list(defined_commands->list);
78  }
79  else
80  {
81    for (i = 1; i < n; i++)
82    {
83      if ((pos = name_list_pos(toks[i], defined_commands->list)) > -1)
84        dump_command(defined_commands->commands[pos]);
85      else puts("no help for this command - try help; (no arguments)");
86    }
87  }
88}
89
90void
91exec_assign(struct in_cmd* cmd)
92  /* executes output unit assignment */
93{
94  char* p;
95  char tmp[FNAME_L];
96  struct name_list* nl = cmd->clone->par_names;
97  struct command_parameter_list* pl = cmd->clone->par;
98  int pos = name_list_pos("echo", nl);
99  int cut = name_list_pos("truncate", nl);
100
101  if (prt_file != stdout)  fclose(prt_file);
102  if (nl->inform[pos]) {
103    p = pl->parameters[pos]->string; strcpy(tmp, p);
104    if (strcmp(stolower(tmp), "terminal") == 0)
105      prt_file = stdout;
106    else {
107      if (assign_start == 0) {
108        assign_start = 1;
109        prt_file = fopen(p, "w");
110      }
111      else if (!nl->inform[cut] || !pl->parameters[cut]->double_value)
112        prt_file = fopen(p, "a");
113      else
114        prt_file = fopen(p, "w");
115    }
116  }
117  else prt_file = stdout;
118}
119
120void
121exec_removefile(struct in_cmd* cmd)
122{
123  struct name_list* nl = cmd->clone->par_names;
124  struct command_parameter_list* pl = cmd->clone->par;
125  int pos = name_list_pos("file", nl);
126
127  if (nl->inform[pos]) {
128    if (remove(pl->parameters[pos]->string))
129      warning("unable to remove file: ", pl->parameters[pos]->string);
130  }
131}
132
133void
134exec_renamefile(struct in_cmd* cmd)
135{
136  struct name_list* nl = cmd->clone->par_names;
137  struct command_parameter_list* pl = cmd->clone->par;
138  int pos = name_list_pos("file", nl);
139  int new = name_list_pos("name", nl);
140
141  if (nl->inform[pos] && nl->inform[new]) {
142    if (rename(pl->parameters[pos]->string, pl->parameters[new]->string))
143      warning("unable to rename file: ", pl->parameters[pos]->string);
144  }
145}
146
147void
148exec_call(struct in_cmd* cmd)
149  /* handles calling external files */
150{
151  struct command_parameter_list* pl = cmd->clone->par;
152  struct name_list* nl = cmd->clone->par_names;
153  int pos = name_list_pos("file", nl);
154  int top = in->curr;
155  if (nl->inform[pos])
156  {
157    if (down_unit(pl->parameters[pos]->string)) madx_input(top);
158  }
159  else warning("call without filename:", "ignored");
160}
161
162void
163exec_cmd_delete(struct in_cmd* cmd)
164/* handles all delete request through "delete" command */
165{
166  struct name_list* nl = cmd->clone->par_names;
167  struct command_parameter_list* pl = cmd->clone->par;
168  int pos;
169  char* name;
170  pos = name_list_pos("sequence", nl);
171  if (nl->inform[pos])
172  {
173    name = pl->parameters[pos]->string;
174    exec_delete_sequ(name);
175  }
176  pos = name_list_pos("table", nl);
177  if (nl->inform[pos])
178  {
179    name = pl->parameters[pos]->string;
180    exec_delete_table(name);
181  }
182}
183
184void
185exec_show(struct in_cmd* cmd)
186  /* executes "show" command */
187{
188  struct element* el;
189  struct variable* var;
190  char** toks = cmd->tok_list->p;
191  int i, pos, n = cmd->tok_list->curr;
192  for (i = 1; i < n; i++)
193  {
194    if (strcmp(toks[i],","))
195    {
196      if (strncmp(toks[i], "beam", 4) == 0) show_beam(toks[i]);
197      else if ((pos = name_list_pos(toks[i], defined_commands->list)) > -1)
198      {
199        if (strcmp(toks[i], "option") == 0) dump_command(options);
200        else if (strcmp(toks[i], "eoption") == 0 && current_eopt != NULL)
201          dump_command(current_eopt);
202        else dump_command(defined_commands->commands[pos]);
203      }
204      else if ((pos = name_list_pos(toks[i], beta0_list->list)) > -1)
205        dump_command(beta0_list->commands[pos]);
206      else if ((el = find_element(toks[i], element_list)) != NULL)
207        dump_element(el);
208      else if ((var = find_variable(toks[i], variable_list)))
209      {
210        if (var->expr)  fprintf(prt_file, "%s := %s ;\n", toks[i], var->expr->string);
211        else fprintf(prt_file, v_format("%s = %F ;\n"), toks[i], var->value);
212      }
213      else fprintf(prt_file, "%s not found\n;", toks[i]);
214    }
215  }
216}
217
218void
219exec_create_table(struct in_cmd* cmd)
220  /* makes a user defined table */
221{
222  char rout_name[] = "exec_create_table";
223  struct table* t;
224  int* t_types;
225  struct name_list* nl = cmd->clone->par_names;
226  struct command_parameter_list* pl = cmd->clone->par;
227  struct char_p_array* m;
228  char** t_c;
229  int j, pos = name_list_pos("table", nl);
230  char* name = NULL;
231  int  ncols = 0;  /*number of columns*/
232
233  if (nl->inform[pos] == 0)
234  {
235    warning("no table name:", "ignored");
236    return;
237  }
238  if ((name = pl->parameters[pos]->string) == NULL)
239  {
240    warning("no table name: ", "ignored");
241    return;
242  }
243  if ((pos = name_list_pos(name, table_register->names)) > -1)
244  {
245    warning("table already exists: ", "ignored");
246    return;
247  }
248
249  pos = name_list_pos("column", nl);
250  if (nl->inform[pos] == 0)
251  {
252    warning("table without columns: ", "ignored");
253    return;
254  }
255  m = pl->parameters[pos]->m_string;
256  ncols = m->curr;
257  /* now make table */
258  t_types = mymalloc(rout_name, ncols*sizeof(int));
259  t_c = mymalloc(rout_name, (ncols+1)*sizeof(char*));
260
261  for (j = 0; j < m->curr; j++)
262  {
263    if (*m->p[j] == '_')
264    {
265      t_types[j] = 3; /* type string */
266      t_c[j] = permbuff(&m->p[j][1]);
267    }
268    else
269    {
270      t_types[j] = 2; /* type double */
271      t_c[j] = permbuff(m->p[j]);
272    }
273  }
274  t_c[ncols] = blank;
275  t = make_table(name, "user", t_c, t_types, USER_TABLE_LENGTH);
276  t->org_cols = 0;  /* all entries are "added" */
277  add_to_table_list(t, table_register);
278  myfree(rout_name, t_c); myfree(rout_name, t_types);
279  t->dynamic = 1;
280}
281
282void
283exec_store_coguess(struct in_cmd* cmd)
284  /* stores the initial orbit guess of the user */
285{
286  struct name_list* nl = cmd->clone->par_names;
287  int pos = name_list_pos("tolerance", nl);
288  double tol;
289  if (nl->inform[pos])
290  {
291    tol = command_par_value("tolerance", cmd->clone);
292    set_variable("twiss_tol", &tol);
293  }
294  store_orbit(cmd->clone, guess_orbit);
295  guess_flag = 1;
296}
297
298void
299exec_dump(struct in_cmd* cmd)
300  /* write a table out */
301{
302  struct table* t;
303  struct name_list* nl = cmd->clone->par_names;
304  struct command_parameter_list* pl = cmd->clone->par;
305  int pos = name_list_pos("table", nl);
306  char* name = NULL;
307  char *f, filename[FNAME_L];
308  if (nl->inform[pos] == 0)
309  {
310    warning("dump without table name:", "ignored");
311    return;
312  }
313  if ((name = pl->parameters[pos]->string) == NULL)
314  {
315    warning("dump without table name:", "ignored");
316    return;
317  }
318  pos = name_list_pos("file", nl);
319  if (nl->inform[pos] == 0) strcpy(filename, "terminal");
320  else if ((f = pl->parameters[pos]->string) == NULL
321           || *f == '\0') strcpy(filename, name);
322  else strcpy(filename,f);
323  if ((pos = name_list_pos(name, table_register->names)) > -1)
324  {
325    t = table_register->tables[pos];
326    out_table(name, t, filename);
327  }
328  else
329  {
330    warning("table name not found:", "ignored");
331  }
332}
333
334void
335exec_fill_table(struct in_cmd* cmd)
336  /* adds variables to a table */
337{
338  struct table* t;
339  struct name_list* nl = cmd->clone->par_names;
340  struct command_parameter_list* pl = cmd->clone->par;
341  int pos = name_list_pos("table", nl);
342  char* name = NULL;
343  int row,curr;
344  if (nl->inform[pos] == 0)
345  {
346    warning("no table name:", "ignored");
347    return;
348  }
349  if ((name = pl->parameters[pos]->string) == NULL)
350  {
351    warning("no table name: ", "ignored");
352    return;
353  }
354  pos=name_list_pos("row", nl);
355  row=(int) pl->parameters[pos]->double_value;
356  if ((pos = name_list_pos(name, table_register->names)) > -1)
357  {
358    t = table_register->tables[pos];
359    if (row<0) {
360      add_vars_to_table(t);
361      if (++t->curr == t->max) grow_table(t);
362    } else {
363      row--;
364      curr=t->curr;
365      if (row < t->curr) {
366        t->curr=row;}
367      else {
368        t->curr--;
369      }
370      add_vars_to_table(t);
371      t->curr=curr;
372    }
373  }
374  else warning("table not found: ", "ignored");
375  return;
376}
377
378void
379exec_setvars_table(struct in_cmd* cmd)
380  /* set variables from a table */
381{
382  struct table* t;
383  struct name_list* nl = cmd->clone->par_names;
384  struct command_parameter_list* pl = cmd->clone->par;
385  int pos = name_list_pos("table", nl);
386  char* name = NULL;
387  int row,curr;
388  if (nl->inform[pos] == 0)
389  {
390    warning("no table name:", "ignored");
391    return;
392  }
393  if ((name = pl->parameters[pos]->string) == NULL)
394  {
395    warning("no table name: ", "ignored");
396    return;
397  }
398  current_node = NULL; /* to distinguish from other table fills */
399  pos=name_list_pos("row", nl);
400  row=(int) pl->parameters[pos]->double_value;
401  if ((pos = name_list_pos(name, table_register->names)) > -1)
402  {
403    t = table_register->tables[pos];
404    row--;
405    curr=t->curr;
406    if ((row < t->curr) && (row >-1)) {
407      t->curr=row;}
408    else {
409      t->curr--;
410    }
411    set_vars_from_table(t);
412    t->curr=curr;
413  }
414  else warning("table not found: ", "ignored");
415  return;
416}
417
418
419void
420exec_setvars_lin_table(struct in_cmd* cmd)
421  /* set variables from a table */
422{
423  struct table* t;
424  struct name_list* nl = cmd->clone->par_names;
425  struct command_parameter_list* pl = cmd->clone->par;
426  int pos,row1,row2,i;
427  char* name = NULL;
428  char* param = NULL;
429  char* colname = NULL;
430  double val1,val2;
431  char expr[10*NAME_L];
432  i=0;
433
434  pos = name_list_pos("table", nl);
435  if (nl->inform[pos] == 0)
436  {
437    warning("no table name:", "ignored");
438    return;
439  }
440  if ((name = pl->parameters[pos]->string) == NULL)
441  {
442    warning("no table name: ", "ignored");
443    return;
444  }
445  /*current_node = NULL;  to distinguish from other table fills ????*/
446  pos=name_list_pos("row1", nl);
447  row1=(int) pl->parameters[pos]->double_value-1;
448  pos=name_list_pos("row2", nl);
449  row2=(int) pl->parameters[pos]->double_value-1;
450  pos = name_list_pos("param", nl);
451  param = pl->parameters[pos]->string;
452  if ((pos = name_list_pos(name, table_register->names)) > -1)
453  {
454    t = table_register->tables[pos];
455    if (row1<0){ row1=t->curr+row1;}
456    if (row2<0){ row2=t->curr+row2;}
457    /*printf("Using row1=%d, row2=%d\n",row1,row2); */
458    if (row1<0 || row1>=t->curr){
459      warning("row1 index out of bounds:", " ignored");
460      return;
461    }
462    if (row2<0 || row2>=t->curr){
463      warning("row2 index out of bounds:", " ignored");
464      return;
465    }
466    /* printf("Using row1=%d, row2=%d\n",row1,row2); */
467    for (i = 0; i < t->num_cols; i++) {
468      if (t->columns->inform[i] <3){
469        colname=t->columns->names[i];
470        val1=t->d_cols[i][row1];
471        val2=t->d_cols[i][row2];
472        sprintf(expr,"%s:=%10.16g*(%s)%+10.16g*(1-(%s));",
473            colname,val1,param,val2,param);
474        /* printf("%s\n",expr); */
475        pro_input(expr);
476      }
477    }
478  }
479  else warning("table not found: ", "ignored");
480  return;
481}
482
483
484void
485exec_print(struct in_cmd* cmd)
486  /* prints text from "print" command to current output unit */
487{
488  struct command_parameter_list* pl = cmd->clone->par;
489  struct name_list* nl = cmd->clone->par_names;
490  int pos = name_list_pos("text", nl);
491  if (nl->inform[pos]) fprintf(prt_file,"%s\n", pl->parameters[pos]->string);
492}
493
Note: See TracBrowser for help on using the repository browser.