source: PSPA/madxPSPA/src/mad_cmd.c @ 447

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

import madx-5.01.00

File size: 27.4 KB
Line 
1#include "madx.h"
2
3static void
4store_threader(struct in_cmd* cmd)
5{
6  threader_par = cmd->clone;
7  cmd->clone_flag = 1;
8  dump_command(threader_par);
9}
10
11static int
12cmd_match(int cnt, char** toks, int* cmd_pos, int* decl_start)
13  /* matches input (user) command with the symbolic description
14     from the list in madxl.h and returns is type */
15{
16  int i, i2, j, k, lp;
17  for (i = 0; i < n_match; i++)
18  {
19    k = 0; lp = -1;
20    i2 = t_match[i];
21    for (j = s_match[i2]; j < s_match[i2+1]; j++)
22    {
23      if (k == cnt)  break;
24      if (*cmd_match_base[j] == '@')
25      {
26        if (strcmp(cmd_match_base[j], "@cmd") == 0)
27        {
28          if ((lp = name_list_pos(toks[k],
29                                  defined_commands->list)) < 0) break;
30        }
31        else if (isalpha(*toks[k]) == 0) break;
32      }
33      else if (strcmp(cmd_match_base[j], toks[k]) != 0)  break;
34      k++;
35    }
36
37    if (j == s_match[i2+1]) goto found;
38  }
39  return -3;
40  found:
41  *cmd_pos = lp; *decl_start = s_match[i2+1] - s_match[i2];
42
43
44  return i2;
45}
46
47static void
48grow_command_list_list(struct command_list_list* p)
49{
50  char rout_name[] = "grow_command_list_list";
51  struct command_list** c_loc = p->command_lists;
52  int new = 2*p->max;
53
54  p->max = new;
55  p->command_lists = mycalloc(rout_name,new, sizeof(struct command_list*));
56  for (int j = 0; j < p->curr; j++) p->command_lists[j] = c_loc[j];
57  myfree(rout_name, c_loc);
58}
59
60static void
61control(struct in_cmd* cmd)
62  /* executes so-called "control" commands */
63{
64  char** toks = cmd->tok_list->p;
65  int k = cmd->decl_start - 1;
66
67  // known control command in the same order as in mad_dict.c
68  if      (strcmp(toks[k], "assign")      == 0) exec_assign(cmd);
69  else if (strcmp(toks[k], "beam")        == 0) exec_beam(cmd, 0);
70  else if (strcmp(toks[k], "beta0")       == 0) store_beta0(cmd);
71  else if (strcmp(toks[k], "call")        == 0) exec_call(cmd);
72  else if (strcmp(toks[k], "coguess")     == 0) exec_store_coguess(cmd);
73  else if (strcmp(toks[k], "create")      == 0) exec_create_table(cmd);
74  else if (strcmp(toks[k], "delete")      == 0) exec_cmd_delete(cmd);
75  else if (strcmp(toks[k], "deselect")    == 0) store_deselect(cmd);
76  else if (strcmp(toks[k], "dumpsequ")    == 0) exec_dumpsequ(cmd);
77  else if (strcmp(toks[k], "exec")        == 0) ;
78  else if (strcmp(toks[k], "exit")        == 0) ;
79  else if (strcmp(toks[k], "extract")     == 0) exec_extract(cmd);
80  else if (strcmp(toks[k], "fill")        == 0) exec_fill_table(cmd);
81  else if (strcmp(toks[k], "setvars")     == 0) exec_setvars_table(cmd);
82  else if (strcmp(toks[k], "setvars_lin") == 0) exec_setvars_lin_table(cmd);
83  else if (strcmp(toks[k], "help")        == 0) ;
84  else if (strcmp(toks[k], "option")      == 0) exec_option();
85  else if (strcmp(toks[k], "plot")        == 0) exec_plot(cmd);
86  else if (strcmp(toks[k], "print")       == 0) exec_print(cmd);
87  else if (strcmp(toks[k], "quit")        == 0) ;
88  else if (strcmp(toks[k], "readtable")   == 0) read_table(cmd);
89  else if (strcmp(toks[k], "removefile")  == 0) exec_removefile(cmd);
90  else if (strcmp(toks[k], "renamefile")  == 0) exec_renamefile(cmd);
91  else if (strcmp(toks[k], "resbeam")     == 0) exec_beam(cmd, 1);
92  else if (strcmp(toks[k], "resplot")     == 0) ;
93  else if (strcmp(toks[k], "return")      == 0) ;
94  else if (strcmp(toks[k], "save")        == 0) exec_save(cmd);
95  else if (strcmp(toks[k], "savebeta")    == 0) store_savebeta(cmd);
96  else if (strcmp(toks[k], "select")      == 0) store_select(cmd);
97  else if (strcmp(toks[k], "set")         == 0) store_set(cmd->clone, 1);
98  else if (strcmp(toks[k], "setplot")     == 0) ;
99  else if (strcmp(toks[k], "sodd")        == 0) exec_sodd(cmd);
100  else if (strcmp(toks[k], "show")        == 0) ;
101  else if (strcmp(toks[k], "stop")        == 0) ;
102  else if (strcmp(toks[k], "system")      == 0) ;
103  else if (strcmp(toks[k], "title")       == 0) ;
104  else if (strcmp(toks[k], "threader")    == 0) store_threader(cmd);
105  else if (strcmp(toks[k], "use")         == 0) use_sequ(cmd);
106  else if (strcmp(toks[k], "value")       == 0) ;
107  else if (strcmp(toks[k], "write")       == 0) exec_dump(cmd);
108  /* insert your proper command action here */
109  else fprintf(stderr, "++++++++++++++ unknown command '%s', skipped in parser version\n", toks[k]);
110}
111
112// public interface
113
114void
115exec_command(void)
116  /* executes one command */
117{
118  char** toks;
119  char* cmd_name;
120  struct in_cmd* p = this_cmd;
121  struct in_cmd* pp;
122  int izero = 0, pos; // ret, not used
123
124  if (p->cmd_def != NULL)
125  {
126    while (strcmp(p->cmd_def->name, "exec") == 0)
127    {
128      if ((pos = name_list_pos(p->tok_list->p[p->decl_start], macro_list->list)) > -1)
129      {
130        exec_macro(p, pos);
131        return;
132      }
133      pp = p;
134      if ((p = buffered_cmd(pp)) == pp) break;
135    }
136    this_cmd = p;
137    toks = p->tok_list->p;
138    cmd_name = p->cmd_def->name;
139    if (strcmp(cmd_name, "stop") == 0 || strcmp(cmd_name, "quit") == 0 || strcmp(cmd_name, "exit") == 0)
140    {
141      madx_finish(); stop_flag = 1; return;
142    }
143    else if (strcmp(cmd_name, "help") == 0) exec_help(p);
144    else if (strcmp(cmd_name, "show") == 0) exec_show(p);
145    else if (strcmp(cmd_name, "return") == 0)  return_flag = 1;
146    else if (strcmp(cmd_name, "value") == 0)
147    {
148      print_value(p);
149    }
150    else if (strcmp(cmd_name, "system") == 0)
151      system(noquote(toks[p->decl_start])); // ret =, not used
152    else if (strcmp(cmd_name, "title") == 0)
153      title = permbuff(noquote(toks[p->decl_start]));
154    else if (strcmp(cmd_name, "resplot") == 0)
155    {
156      plot_options = delete_command(plot_options);
157      set_defaults("setplot");
158    }
159    else
160    {
161      if (get_option("trace")) time_stamp(cmd_name);
162      /* clones with defaults for most commands */
163      if (strcmp(cmd_name, "option") == 0 && options != NULL)
164      {
165        set_option("tell", &izero); /* reset every time */
166        p->clone = options; p->clone_flag = 1;
167      }
168      else if (strcmp(cmd_name, "setplot") == 0 && plot_options != NULL)
169      {
170        p->clone = plot_options; p->clone_flag = 1;
171      }
172      else p->clone = clone_command(p->cmd_def);
173
174      // parser of parameters (mad_cmdin.c)
175      scan_in_cmd(p); /* match input command with clone + fill */
176      current_command = p->clone;
177
178      if (strcmp(p->cmd_def->module, "control") == 0) control(p);
179      else if (strcmp(p->cmd_def->module, "c6t") == 0) conv_sixtrack(p);
180      else if (strcmp(p->cmd_def->module, "edit") == 0) seq_edit_main(p);
181      else if (strcmp(p->cmd_def->module, "ibs") == 0)
182      {
183        current_ibs = p->clone;
184        pro_ibs(p);
185      }
186      else if (strcmp(p->cmd_def->module, "aperture") == 0)
187      {
188        pro_aperture(p);
189      }
190      else if (strcmp(p->cmd_def->module, "touschek") == 0)
191      {
192        current_touschek = p->clone;
193        pro_touschek(p);
194      }
195      else if (strcmp(p->cmd_def->module, "makethin") == 0) makethin(p);
196      else if (strcmp(p->cmd_def->module, "match") == 0)
197      {
198        current_match = p->clone; /* OB 23.1.2002 */
199        pro_match(p);
200      }
201      else if (strcmp(p->cmd_def->module, "correct") == 0)
202      {
203        pro_correct(p);
204      }
205      else if (strcmp(p->cmd_def->module, "emit") == 0)
206      {
207        pro_emit(p);
208      }
209      else if (strcmp(p->cmd_def->module, "sdds") == 0)
210      {
211#ifdef _ONLINE
212        pro_sdds(p);
213#else
214        warning("ignored, only available in ONLINE model:", "SDDS conversion");
215#endif
216      }
217      else if (strcmp(p->cmd_def->module, "error") == 0)
218      {
219        current_error = p->clone;
220        pro_error(p);
221      }
222      else if (strcmp(p->cmd_def->module, "ptc_create_universe") == 0)
223      {
224        if (match_is_on == kMatch_PTCknobs)
225        {
226          madx_mpk_setcreateuniverse(p);
227        }
228        else
229        {
230          w_ptc_create_universe_();
231          curr_obs_points = 1;  /* default: always observe at machine end */
232        }
233      }
234      /* export XML */
235      else if (strcmp(p->cmd_def->module, "ptc_export_xml") == 0)
236      {
237        /* command with parameters, decoded by dedicated function in madxn.c */
238        pro_ptc_export_xml(p);
239      }     
240      else if (strcmp(p->cmd_def->module, "ptc_create_layout") == 0)
241      {
242        if (match_is_on == kMatch_PTCknobs)
243        {
244          madx_mpk_setcreatelayout(p);
245        }
246        else
247        {
248          pro_ptc_create_layout();
249          reset_count("normal_results");
250          reset_count("ptc_twiss");
251/*          exec_delete_table("normal_results");
252            exec_delete_table("ptc_twiss");*/
253        }
254      }
255      else if (strcmp(p->cmd_def->module, "ptc_move_to_layout") == 0)
256      {
257        w_ptc_move_to_layout_();
258      }
259      else if (strcmp(p->cmd_def->module, "ptc_read_errors") == 0)
260      {
261        pro_ptc_read_errors();
262      }
263      else if (strcmp(p->cmd_def->module, "ptc_refresh_k") == 0)
264      {
265        pro_ptc_refresh_k();
266      }
267      else if (strcmp(p->cmd_def->module, "ptc_align") == 0)
268      {
269        w_ptc_align_();
270      }
271      else if (strcmp(p->cmd_def->module, "ptc_twiss") == 0)
272      {
273
274        if (match_is_on == kMatch_PTCknobs)
275        {
276          madx_mpk_setcalc(p);
277        }
278        else
279        {
280          current_twiss = p->clone;
281          pro_ptc_twiss();
282        }
283      }
284      else if (strcmp(p->cmd_def->module, "ptc_normal") == 0)
285      {
286        if (match_is_on == kMatch_PTCknobs)
287        {
288          madx_mpk_setcalc(p);
289        }
290        else
291        {
292          w_ptc_normal_();
293        }
294      }
295      else if (strcmp(p->cmd_def->module, "select_ptc_normal") == 0)
296      {
297        select_ptc_normal(p);
298      }
299      else if (strcmp(p->cmd_def->module, "ptc_trackline") == 0)
300      {
301        pro_ptc_trackline(p);
302      }
303      else if (strcmp(p->cmd_def->module, "ptc_dumpmaps") == 0)
304      {
305        ptc_dumpmaps(p);
306      }
307      else if (strcmp(p->cmd_def->module, "ptc_oneturnmap") == 0)
308      {
309        ptc_oneturnmap(p);
310      }
311      else if (strcmp(p->cmd_def->module, "ptc_track") == 0)
312      {
313        pro_ptc_track(p);
314      }
315      else if (strcmp(p->cmd_def->module, "ptc_setswitch") == 0)
316      {
317        pro_ptc_setswitch(p);
318      }
319      else if (strcmp(p->cmd_def->module, "ptc_select") == 0)
320      {
321        pro_ptc_select(p);
322      }
323      else if (strcmp(p->cmd_def->module, "ptc_moments") == 0)
324      {
325        pro_ptc_moments(p);
326      }
327      else if (strcmp(p->cmd_def->module, "ptc_select_moment") == 0)
328      {
329        pro_ptc_select_moment(p);
330      }
331      else if (strcmp(p->cmd_def->module, "ptc_printparametric") == 0)
332      {
333        pro_ptc_printparametric(p);
334      }
335      else if (strcmp(p->cmd_def->module, "ptc_knob") == 0)
336      {
337        pro_ptc_knob(p);
338      }
339      else if (strcmp(p->cmd_def->module, "ptc_varyknob") == 0)
340      {
341        pro_ptc_varyknob(p);
342      }
343      else if (strcmp(p->cmd_def->module, "ptc_setknobvalue") == 0)
344      {
345        pro_ptc_setknobvalue(p);
346      }
347      else if (strcmp(p->cmd_def->module, "ptc_refreshpartables") == 0)
348      {
349        w_ptc_refreshtables_();
350      }
351      else if (strcmp(p->cmd_def->module, "ptc_setfieldcomp") == 0)
352      {
353        pro_ptc_setfieldcomp(p);
354      }
355      else if (strcmp(p->cmd_def->module, "rviewer") == 0)
356      {
357        w_ptc_rviewer_();
358      }
359      else if (strcmp(p->cmd_def->module, "ptc_printframes") == 0)
360      {
361        pro_ptc_printframes(p);
362      }
363      else if (strcmp(p->cmd_def->module, "ptc_eplacement") == 0)
364      {
365        pro_ptc_eplacement(p);
366      }
367      else if (strcmp(p->cmd_def->module, "ptc_script") == 0)
368      {
369        pro_ptc_script(p);
370      }
371      else if (strcmp(p->cmd_def->module, "ptc_open_gino") == 0)
372      {
373        pro_ptc_open_gino(p);
374      }
375      else if (strcmp(p->cmd_def->module, "ptc_enforce6d") == 0)
376      {
377        pro_ptc_enforce6d(p);
378      }
379      else if (strcmp(p->cmd_def->module, "ptc_observe") == 0)
380      {
381        ptc_track_observe(p);
382      }
383      else if (strcmp(p->cmd_def->module, "ptc_putbeambeam") == 0)
384      {
385        ptc_putbeambeam(p);
386      }
387      else if (strcmp(p->cmd_def->module, "ptc_start") == 0)
388      {
389        track_is_on = 1;
390        track_start(p->clone);
391        p->clone_flag = 1;
392        /* w_ptc_start_(); */
393      }
394      else if (strcmp(p->cmd_def->module, "ptc_track_end") == 0)
395      {
396        ptc_track_end();
397      }
398      else if (strcmp(p->cmd_def->module, "ptc_end") == 0)
399      {
400        if(track_is_on) ptc_track_end();
401        w_ptc_end_();
402      }
403      else if (strcmp(p->cmd_def->module, "sxf") == 0)
404      {
405        pro_sxf(p);
406      }
407      else if (strcmp(p->cmd_def->module, "survey") == 0)
408      {
409        current_survey = p->clone;
410        pro_survey(p);
411      }
412      else if (strcmp(p->cmd_def->module, "track") == 0)
413      {
414        pro_track(p);
415      }
416      else if (strcmp(p->cmd_def->module, "twiss") == 0)
417      {
418        current_twiss = p->clone;
419        pro_twiss();
420      }
421    }
422  }
423}
424
425struct command*
426find_command(char* name, struct command_list* cl)
427{
428  int pos;
429  if ((pos = name_list_pos(name, cl->list)) < 0)
430    return NULL;
431  return cl->commands[pos];
432}
433
434struct command_list*
435find_command_list(char* name, struct command_list_list* sl)
436{
437  int pos;
438  if ((pos = name_list_pos(name, sl->list)) < 0)
439    return NULL;
440  return sl->command_lists[pos];
441}
442
443void
444remove_from_command_list(char* label, struct command_list* list)
445{
446  int i;
447  if ((i = remove_from_name_list(label, list->list)) > -1)
448  {
449    if (i < --list->curr)
450    {
451      delete_command(list->commands[i]);
452      list->commands[i] = list->commands[list->curr];
453    }
454  }
455}
456
457void
458get_defined_commands(void)
459  /* reads + stores the commands defined in madxdict.h */
460{
461  char rout_name[] = "get_defined_commands";
462  int i;
463  char** p;
464  int n = char_cnt(';', command_def);
465  p = mymalloc(rout_name,n * sizeof(char*));
466  p[0] = strtok(command_def, ";");
467  for (i = 1; i < n; i++) /* make temporary list - strtok is called again */
468    p[i] = strtok(NULL, ";");
469  for (i = 0; i < n; i++) store_command_def(p[i]);
470  myfree(rout_name, p);
471}
472
473int
474make_line(char* statement)
475  /* makes a new line from input command, stores name in name list */
476{
477  struct macro* m;
478  char** toks = tmp_l_array->p;
479  char *prs, *psem;
480  int i, n, rs, re;
481  int len = strlen(statement);
482  while(len >= aux_buff->max) grow_char_array(aux_buff);
483  strcpy(aux_buff->c, statement);
484  if ((prs = strchr(aux_buff->c, '=')) == NULL) return -3;
485  *prs = '\0'; prs++;
486  pre_split(aux_buff->c, l_wrk, 0);
487  mysplit(l_wrk->c, tmp_l_array);
488  get_bracket_t_range(toks, '(', ')', 0, tmp_l_array->curr-1, &rs, &re);
489  if ((n = re - rs - 1) < 0) n = 0; /* number of formal arguments if any */
490  m = new_macro(n, 2*strlen(prs), 50);
491  strcpy(m->name, toks[0]); rs++;
492  for (i = 0; i < n; i++) m->formal->p[i] = permbuff(toks[rs+i]);
493  if (n > 0) m->formal->curr = n;
494  if ((psem = strchr(prs, ';')) != NULL) *psem = '\0';
495  mystrcpy(l_wrk, prs);
496  pre_split(l_wrk->c, m->body, 0);
497  m->body->curr = strlen(m->body->c);
498  mysplit(m->body->c, m->tokens);
499  n = 0;
500  for (i = 0; i < m->tokens->curr; i++)
501  {
502    if (*m->tokens->p[i] == '(')  n++;
503    else if (*m->tokens->p[i] == ')')  n--;
504    else if(i > 0 && *m->tokens->p[i] == '*'
505            && isdigit(*m->tokens->p[i-1]) == 0) return -3;
506  }
507  if (n) return -3; /* bracket closing error */
508  add_to_macro_list(m, line_list);
509  return 0;
510}
511
512int
513get_stmt(FILE* file, int supp_flag)
514  /* Returns complete command(s) in in->buffers[inbuf_level] */
515  /* return value:  0   no more input */
516  /*                >0  OK */
517
518{
519  struct char_array* ca;
520  char *c_st, *c_cc, *c_ex;
521  char end = ';';
522  int in_comment = 0, spec_test = 0, curl_level = 0, go_cond;
523  if (in->buffers[in->curr] == NULL)
524    in->buffers[in->curr] = new_in_buffer(IN_BUFF_SIZE);
525  ca = in->buffers[in->curr]->c_a;
526  ca->curr = 0;
527  do /* read lines until complete statement(s) */
528  {
529    next:
530    if (ca->max - ca->curr < MAX_LINE) grow_char_array(ca);
531    if (!fgets(&ca->c[ca->curr], MAX_LINE, file)) return 0;
532    currentline[in->curr]++;
533    if (get_option("echo")) puts(&ca->c[ca->curr]);
534    c_cc = mystrstr(&ca->c[ca->curr], "//");
535    c_ex = mystrchr(&ca->c[ca->curr], '!');
536    c_st = mystrstr(&ca->c[ca->curr], "/*");
537    if (c_cc != NULL && c_ex != NULL)
538    {
539      c_cc = c_cc < c_ex ? c_cc : c_ex; *c_cc = '\0';
540    }
541    else if(c_cc != NULL 
542            &&(c_st == NULL || c_cc < c_st))
543    {
544      if (c_cc == &ca->c[ca->curr]) goto next;
545      else *c_cc = '\0';
546    }
547    else if(c_ex != NULL
548            &&(c_st == NULL || c_ex < c_st))
549    {
550      if (c_ex == &ca->c[ca->curr]) goto next;
551      else *c_ex = '\0';
552    }
553    if (in_comment)
554    {
555      if (mystrstr(&ca->c[ca->curr], "*/") == NULL)  goto next;
556      else
557      {
558        remove_upto(&ca->c[ca->curr], "*/"); in_comment = 0;
559      }
560    }
561    else if((c_st = mystrstr(&ca->c[ca->curr], "/*")) != NULL)
562    {
563      if (mystrstr(&ca->c[ca->curr], "*/") == NULL)
564      {
565        *c_st = '\0'; ca->curr += supp_lt(&ca->c[ca->curr], supp_flag);
566        in_comment = 1; goto next;
567      }
568      else remove_range(&ca->c[ca->curr], "/*", "*/");
569    }
570    if (spec_test == 0 && mystrchr(&ca->c[ca->curr], '{') != NULL)
571    {
572      spec_test = 1; if (in_spec_list(ca->c)) end = '}';
573    }
574    curl_level += char_cnt('{', &ca->c[ca->curr])
575      - char_cnt('}', &ca->c[ca->curr]);
576    if ((ca->curr += supp_lt(&ca->c[ca->curr], supp_flag)) == 0) go_cond = 1;
577    else if ((go_cond = curl_level) == 0)
578      go_cond = ca->c[ca->curr-1] == ';' || ca->c[ca->curr-1] == end ? 0 : 1;
579  }
580  while (go_cond);
581  return 1;
582}
583
584int 
585decode_command(void)  /* compares command with templates, fills this_cmd
586                         return: 0 command from list (except below);
587                         1 element definition (as well inside sequence);
588                         2 variable definition;
589                         3 sequence or endsequence;
590                         4 element reference inside sequence (no def.);
591                         -1 illegal in this context or form;
592                         -2 label is protected keyword;
593                         -3 statement not recognised */
594{
595
596
597  int i, aux_pos, cmd_pos, decl_start, type;
598  int n = this_cmd->tok_list->curr;
599  char** toks = this_cmd->tok_list->p;
600  this_cmd->type = -3;
601  if ((i = cmd_match(n, toks, &cmd_pos, &decl_start)) < 0)  return i;
602
603
604  this_cmd->sub_type = i;
605  this_cmd->decl_start = decl_start;
606  switch (i)
607  {
608    case 0:
609      if (n > 1 && *toks[1] == ':') /* label is (protected) key */ return -2;
610      this_cmd->cmd_def = defined_commands->commands[cmd_pos];
611      if (strcmp(toks[0], "endsequence") == 0)
612      {
613        if (sequ_is_on == 0) return -1;
614        this_cmd->type = 3;
615        sequ_is_on = 0;
616      }
617      else if (strcmp(this_cmd->cmd_def->module, "element") == 0
618               || strcmp(this_cmd->cmd_def->module, "sequence") == 0) return -1;
619      else this_cmd->type = 0;
620      break;
621    case 1:
622    case 16:
623      if (i == 1) aux_pos = 0;
624      else          aux_pos = 1;
625      this_cmd->cmd_def = defined_commands->commands[cmd_pos];
626      this_cmd->type = 0;
627      this_cmd->label = permbuff(toks[aux_pos]);
628      if (strcmp(this_cmd->cmd_def->module, "element") == 0)
629        this_cmd->type = 1; /* element definition */
630      else if (strcmp(this_cmd->cmd_def->module, "sequence") == 0)
631      {
632        if (strcmp(toks[aux_pos+2], "sequence") == 0)
633        {
634          this_cmd->type = 3;
635          sequ_is_on = 1;
636        }
637        else return -1;
638      }
639      type = this_cmd->cmd_def->link_type;
640      if (type == 1)
641      {
642        if (group_is_on || sequ_is_on) return -1; /* group in group illegal */
643        group_is_on = 1;
644        current_link_group = this_cmd->cmd_def->group;
645      }
646      else if (group_is_on)
647      {
648        if (strcmp(none, this_cmd->cmd_def->group) != 0
649            && strcmp(current_link_group, this_cmd->cmd_def->group) != 0)
650          return -1; /* command does not belong to this group */
651        if (type == 2)
652        {
653          current_link_group = none; /* end of group */
654          group_is_on = 0;
655        }
656      }
657      break;
658    case 14:
659      this_cmd->type = 1;
660      this_cmd->decl_start++; /* skip (as yet unknown) element class pos. */
661      break;
662    case 15:
663      this_cmd->type = 4; /* element or sequence reference inside sequence
664                             or element parameter definition */
665      this_cmd->decl_start--; /* second name is already declaration start */
666      break;
667    default:
668      type = 0;
669      this_cmd->type = 2;
670  }
671  return this_cmd->type;
672}
673
674struct command*
675clone_command(struct command* p)
676{
677  int i;
678  struct command* clone = new_command(p->name, 0, p->par->curr,
679                                      p->module, p->group, p->link_type,
680                                      p->mad8_type);
681  copy_name_list(clone->par_names, p->par_names);
682  clone->par->curr = p->par->curr;
683  for (i = 0; i < p->par->curr; i++)
684    clone->par->parameters[i] =
685      clone_command_parameter(p->par->parameters[i]);
686  return clone;
687}
688
689struct command*
690new_command(char* name, int nl_length, int pl_length, char* module, char* group, int link, int mad_8)
691{
692  char rout_name[] = "new_command";
693  char loc_name[2*NAME_L];
694  struct command* new = mycalloc(rout_name,1, sizeof(struct command));
695  strcpy(loc_name, name); strcat(loc_name, "_param");
696  new->stamp = 123456;
697  strcpy(new->name, name);
698  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", loc_name);
699  strcpy(new->module, module);
700  strcpy(new->group, group);
701  new->link_type = link;
702  new->mad8_type = mad_8;
703  if (nl_length == 0) nl_length = 1;
704  new->par_names = new_name_list(loc_name, nl_length);
705  new->par = new_command_parameter_list(pl_length);
706  return new;
707}
708
709struct command_list*
710new_command_list(char* l_name, int length)
711{
712  char rout_name[] = "new_command_list";
713  struct command_list* il = mycalloc(rout_name,1, sizeof(struct command_list));
714  strcpy(il->name, l_name);
715  il->stamp = 123456;
716  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", il->name);
717  il->curr = 0;
718  il->max = length;
719  il->list = new_name_list(il->name, length);
720  il->commands
721    = (struct command**) mycalloc(rout_name,length, sizeof(struct command*));
722  return il;
723}
724
725struct command_list_list*
726new_command_list_list(int length)
727{
728  char rout_name[] = "new_command_list_list";
729  struct command_list_list* il = mycalloc(rout_name,1, sizeof(struct command_list_list));
730  strcpy(il->name, "command_list_list");
731  il->stamp = 123456;
732  if (watch_flag) fprintf(debug_file, "creating ++> %s\n", il->name);
733  il->curr = 0;
734  il->max = length;
735  il->list = new_name_list(il->name, length);
736  il->command_lists = mycalloc(rout_name,length, sizeof(struct command_list*));
737  return il;
738}
739
740#if 0 // not used...
741struct command_list_list*
742delete_command_list_list( struct command_list_list* ll)
743{
744  char rout_name[] = "delete_command_list_list";
745 
746  int i;
747 
748  if (ll == 0x0) return 0x0;
749 
750  for (i = 0; i < ll->curr; i++)
751  {
752    delete_command_list(ll->command_lists[i]);
753  }
754
755  myfree(rout_name, ll->command_lists );
756 
757  delete_name_list(ll->list);
758
759  myfree(rout_name, ll );
760
761  return 0x0;
762}
763#endif
764
765struct command*
766delete_command(struct command* cmd)
767{
768  char rout_name[] = "delete_command";
769  if (cmd == NULL) return NULL;
770  if (stamp_flag && cmd->stamp != 123456)
771    fprintf(stamp_file, "d_c double delete --> %s\n", cmd->name);
772  if (watch_flag) fprintf(debug_file, "deleting --> %s\n", cmd->name);
773  if (cmd->par != NULL)  delete_command_parameter_list(cmd->par);
774  if (cmd->par_names != NULL) delete_name_list(cmd->par_names);
775  myfree(rout_name, cmd);
776  return NULL;
777}
778
779struct command_list*
780delete_command_list(struct command_list* cl)
781{
782  char rout_name[] = "delete_command_list";
783  int i;
784  if (cl == NULL) return NULL;
785  if (stamp_flag && cl->stamp != 123456)
786    fprintf(stamp_file, "d_c_l double delete --> %s\n", cl->name);
787  if (watch_flag) fprintf(debug_file, "deleting --> %s\n", cl->name);
788  if (cl->list != NULL) delete_name_list(cl->list);
789  for (i = 0; i < cl->curr; i++) delete_command(cl->commands[i]);
790  if (cl->commands) myfree(rout_name, cl->commands);
791  myfree(rout_name, cl);
792  return NULL;
793}
794
795void
796grow_command_list(struct command_list* p)
797{
798  char rout_name[] = "grow_command_list";
799  struct command** c_loc = p->commands;
800  int j, new = 2*p->max;
801
802  p->max = new;
803  p->commands
804    = (struct command**) mycalloc(rout_name,new, sizeof(struct command*));
805  for (j = 0; j < p->curr; j++) p->commands[j] = c_loc[j];
806  myfree(rout_name, c_loc);
807}
808
809void
810store_command_def(char* cmd_string)  /* processes command definition */
811{
812  int i, n, b_s = 0, r_start, r_end, b_cnt; // j, not used
813//  struct element* el; // not used
814  struct command* cmd;
815  struct command_parameter* p;
816  struct in_cmd* tmp_cmd = new_in_cmd(1000);
817  struct char_p_array* toks = tmp_cmd->tok_list;
818
819  pre_split(cmd_string, work, 0);
820  n = mysplit(work->c, toks);
821  if (n < 6 || *toks->p[1] != ':') fatal_error("illegal command:", cmd_string);
822  if (defined_commands->curr == defined_commands->max)
823    grow_command_list(defined_commands);
824  cmd = defined_commands->commands[defined_commands->curr++] =
825    new_command(toks->p[0], 40, 40, toks->p[2], toks->p[3],
826                atoi(toks->p[4]), atoi(toks->p[5]));
827
828/*
829  printf("skowrondebug: store_command_def.c command name %s\n",cmd->name);
830  if (strcmp(cmd->name,"twcavity") == 0)
831  {
832  printf("skowrondebug: store_command_def.c I have got TWCAVITY\n");
833  }
834*/
835  i = add_to_name_list(cmd->name, 0, defined_commands->list);
836  if (n > 6)
837  {
838    b_cnt = string_cnt('[', n, toks->p);
839    for (i = 0; i < b_cnt; i++)  /* loop over parameter definitions */
840    {
841      get_bracket_t_range(toks->p, '[', ']', b_s, n, &r_start, &r_end);
842      if (r_start < b_s || r_end - r_start < 2)
843        fatal_error("empty or illegal cmd parameter definition:",
844                    cmd->name);
845      if (cmd->par->curr == cmd->par->max)
846        grow_command_parameter_list(cmd->par);
847      p = store_comm_par_def(toks->p, r_start+1, r_end-1);
848      if (p == NULL) fatal_error("illegal cmd parameter definition:",
849                                 cmd->name);
850      cmd->par->parameters[cmd->par->curr++] = p;
851      add_to_name_list(p->name, 1, cmd->par_names); // j = not used
852      b_s = r_end + 1;
853    }
854  }
855  if (strcmp(toks->p[2], "element") == 0)
856    make_element(toks->p[0], toks->p[0], cmd, 0); // el = not used
857  delete_in_cmd(tmp_cmd);
858}
859
860void
861add_to_command_list(char* label, struct command* comm, struct command_list* cl, int flag)
862  /* adds command comm to the command list cl */
863  /* flag for printing a warning */
864{
865  int pos; // , j; not used
866  if ((pos = name_list_pos(label, cl->list)) > -1)
867  {
868    if (flag) put_info(label, "redefined");
869    if (cl != defined_commands && cl != stored_commands)
870      delete_command(cl->commands[pos]);
871    cl->commands[pos] = comm;
872  }
873  else
874  {
875    if (cl->curr == cl->max) grow_command_list(cl);
876    add_to_name_list(permbuff(label), 0, cl->list);
877    cl->commands[cl->curr++] = comm;
878  }
879}
880
881void
882add_to_command_list_list(char* label, struct command_list* cl, struct command_list_list* sl)
883  /* adds command list cl to command-list list sl */
884{
885  int pos; // , j; not used
886  if ((pos = name_list_pos(label, sl->list)) > -1)
887  {
888    delete_command_list(sl->command_lists[pos]);
889    sl->command_lists[pos] = cl;
890  }
891  else
892  {
893    if (sl->curr == sl->max) grow_command_list_list(sl);
894    add_to_name_list(permbuff(label), 0, sl->list);
895    sl->command_lists[sl->curr++] = cl;
896  }
897}
898
899void
900dump_command(struct command* cmd)
901{
902  int i;
903  fprintf(prt_file, "command: %s  module: %s\n",
904          cmd->name, cmd->module);
905  for (i = 0; i < cmd->par->curr; i++)
906    dump_command_parameter(cmd->par->parameters[i]);
907}
908
909void
910print_command(struct command* cmd)
911{
912  int i;
913  fprintf(prt_file, "command: %s\n", cmd->name);
914  for (i = 0; i < cmd->par->curr; i++)
915  {
916    print_command_parameter(cmd->par->parameters[i]);
917    if ((i+1)%3 == 0) fprintf(prt_file, "\n");
918  }
919  if (i%3 != 0) fprintf(prt_file, "\n");
920}
921
922
Note: See TracBrowser for help on using the repository browser.