home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / mdl / src / mdl.c next >
Encoding:
C/C++ Source or Header  |  1994-02-16  |  26.5 KB  |  916 lines

  1. /* {(rm mdl ; cc -DUNIX -I. -s -n -O -Wc,-Nd4000 -Wc,-Ns4000 -Wc,-Nt50000 -DSYSV -DNLS16 -Dhp9000s300 -o mdl mdl.c mdllib.o  -L /usr/lib/X11R4 -L /usr/lib/Motif1.1 -lMrm -lXm -lXt -lX11 -lPW ; mdl test.mdl)& } */
  2. /**---------------------------------------------------------------------
  3. ***    
  4. ***   file:      mdl.c
  5. ***
  6. ***   project:   Menu Description Language
  7. ***                     General purpose Menu generator.
  8. ***
  9. ***         (c) 1990 Rainer Kowallik
  10. ***
  11. ***-------------------------------------------------------------------*/
  12.  
  13. #include <stdio.h>
  14. /* #include <sys/stat.h> */
  15. #define TMPDIRFILE "/tmp/mdltmpdir.tmp"
  16.  
  17. #ifdef AMIGA
  18. #include <exec/types.h>
  19. #include <exec/types.h>
  20. #include <libraries/dos.h>
  21. #include <libraries/dosextens.h>
  22. #include <intuition/intuition.h>
  23. #include <workbench/workbench.h>
  24. #include <workbench/startup.h>
  25. #include <workbench/icon.h>
  26.  
  27. extern struct WBStartup *WBenchMsg;
  28. extern struct IconBase *IconBase;
  29. #endif
  30. #ifdef UNIX
  31. #include <unistd.h>
  32. #endif
  33.  
  34. #include <mdllib.h>
  35.  
  36. int    tmpnum=0;
  37. char   resource[80];
  38. char   *fn_command_str[160];
  39. char   *var_name_str[160];
  40. char   *var_value_str[160];
  41. int    var_value_int[160];
  42. char   language[80];
  43. char   myname[80];
  44. char   execfile[80];
  45. int    wl = -1;
  46. int    wt = -1;
  47.  
  48. void tmpnam_(s)      /* due to a bug in the Lattice C 5.0 library ! */
  49. char *s;
  50. {
  51. long int t;
  52.  
  53.    time(&t); t = t + tmpnum++;
  54.    sprintf(s,"tmp%x",t);
  55. }
  56.  
  57. /* --------------------------------------------------------
  58.    Special callback function for mdl, executes string in
  59.    fn_command_str[last_gadgetid]
  60.    -------------------------------------------------------- */
  61. void CallBack_mdl()
  62. {
  63. parse(fn_command_str[last_gadgetid]);
  64. }
  65.  
  66.  
  67. void main(argc,argv)
  68. int argc;
  69. char *argv[];
  70. {
  71. int n;
  72. FILE *stream;
  73. char mdlfile[256];
  74. char c,s[256];
  75. char cmdline[256];
  76. FILE *tmp1;
  77.  
  78. #ifdef AMIGA
  79. struct WBArg *Arg;
  80. #endif
  81.  
  82.  
  83.    strcpy(language,"ksh ");
  84.    strcpy(mdlfile,argv[1]);
  85.    if(argc > 0) strcpy(myname,argv[0]);
  86.  
  87.  
  88.  
  89. #ifdef AMIGA
  90.    if(argc <= 1) {
  91.       IconBase = (struct IconBase *) OpenLibrary("icon.library", 0L);
  92.       Arg = WBenchMsg->sm_ArgList;
  93.       strcpy(myname,Arg->wa_Name);
  94.       Arg++;
  95.       CurrentDir(Arg->wa_Lock);
  96.       strcpy(mdlfile,Arg->wa_Name);
  97.    }
  98. #endif
  99.  
  100.    stream = fopen(mdlfile,"r");
  101.    if(stream == NULL) {
  102.       fprintf(stderr,"mdl: could not open command file>%s<\n",mdlfile);
  103.       exit(0);
  104.    }
  105.  
  106. /* the following is for two purposes: 
  107.    1. Making a self detaching programm on an Amiga
  108.    2. passing standard commandstrings from the mdl file
  109. */
  110.  
  111.    strcpy(cmdline,"");
  112.    c = fgetc(stream);         /* an '-' in the first column of the first line */
  113.    ungetc(c,stream);
  114.    if(c == '-') {             /* signals a default command string */
  115.       fgets(cmdline,80,stream); /* read default command string */
  116.    }
  117.    if((!checkopt(argc,argv,"-fork",s)) && (argc > 0)) {
  118.       strcpy(s,cmdline);
  119.       strcpy(cmdline," ");
  120.       strcat(cmdline,myname); strcat(cmdline," ");
  121.       for(n = 1; n < argc; n++) {
  122.          strcat(cmdline,argv[n]);
  123.          strcat(cmdline," ");
  124.       }
  125.       strcat(cmdline,"-fork ");
  126.       strcat(cmdline,s);            /* add default options from mdl file */
  127. #ifdef UNIX
  128.       cmdline[0] = '(';
  129.       strcat(cmdline,")&");
  130.       system(cmdline);
  131. #endif
  132. #ifdef AMIGA
  133.       amifork(cmdline);
  134. #endif
  135.       fclose(stream);
  136.       exit(0);
  137.    }
  138.  
  139. #ifdef UNIX
  140.    strcpy(resource,mdlfile);  /* default resource file name is the mdl prog name */
  141.    c = fgetc(stream);
  142.    if(c == '{') { /* } create resource file: copy text enclosed in {} to TMPFILE */
  143.       strcpy(resource,"mdl.tmp");
  144.       sprintf(s,"/usr/lib/X11/app-defaults/%s",resource);
  145.       tmp1 = fopen(s,"w");
  146.       if(tmp1 == NULL) {
  147.          fprintf(stderr,"mdl: could not open %s for write !\n",s);
  148.       } else {
  149.          chmod(s,0666);
  150.       }
  151.       n=1;
  152.       while(!feof(stream)) {
  153.          c = fgetc(stream);
  154.          if(c == '{') n++;
  155.          if(c == '}') n--;
  156.          if(n == 0) break;
  157.          if(tmp1 != NULL) fputc(c,tmp1);
  158.       }
  159.       if(tmp1 != NULL) fclose(tmp1);
  160.    } else {
  161.       ungetc(c,stream);
  162.       sprintf(s,"/usr/lib/X11/app-defaults/%s",resource);
  163.       tmp1 = fopen(s,"r");          /* check if resource file exists */
  164.       if(tmp1 == NULL) {
  165.          strcpy(resource,"mdl");    /* take mdl default resource file instead */
  166.       } else {
  167.          fclose(tmp1);
  168.       }
  169.    }
  170.  
  171. /* open display and read resource file (if any) */
  172.  
  173.    XtToolkitInitialize();
  174.    app_context = XtCreateApplicationContext();
  175.    if(!app_context) {
  176.       printf("could not create application context\n");
  177.       exit(0);
  178.    }
  179.    display = XtOpenDisplay (app_context, NULL, myname, resource, NULL, 0,
  180.                  &argc, argv);
  181.                  
  182.     if (!display) {
  183.         XtWarning ("mdl: Can't open display, exiting...");
  184.         exit (0);
  185.     }
  186. #endif
  187.  
  188.   menu_init(argv,stream);
  189. /* !!!! Debug !!!!
  190.    sprintf(s,"fn_number = %d\n",fn_number);
  191.    Help(s);
  192.    !!!! Debug !!!! */
  193.   fclose(stream);
  194.  
  195. #ifdef AMIGA
  196.   my_window = (struct Window *) OpenWindow( my_new_window );
  197.   SetMenuStrip( my_window, menu_bar );
  198.   if(my_window == NULL)
  199.   {
  200.     fprintf(stderr,"could not open Window\n");
  201.     close_gfx(my_window);
  202.     exit(0);  
  203.   }
  204.   fin_flg = FALSE;
  205.   MainLoop(my_window);
  206. #endif
  207.  
  208. #ifdef UNIX
  209. /* dont forget to activate the last menu entry */
  210.    add_item(-1,-1,NEW_MENU,"empty",0,0);
  211.     XtRealizeWidget (app_shell);
  212.     XtAppMainLoop (app_context);
  213. #endif
  214.  
  215.   exit(0);
  216. }
  217.  
  218.  
  219.  
  220. /* ********************************************************************
  221.  
  222.    S T R I N G H A N D L I N G
  223.  
  224.    ******************************************************************** */
  225.  
  226. /* --------------------------------------------------
  227.    reurns in s1 s2 from nth occurence of c up to c
  228.    -------------------------------------------------- */
  229.  
  230. ExtractStr(s1,s2,n,c)
  231. char s1[],s2[],c;
  232. int n;
  233. {
  234. int m,i,l;
  235. char c1;
  236.  
  237.    m=0; l=strlen(s2);
  238.    i = 0;
  239.    if(n > 0) {
  240.       for(i=0;i<l;i++) {
  241.           c1=s2[i];
  242.           if(c1 == c) m++;
  243.           if(m == n) break;
  244.       }
  245.       s1[0] = 0;
  246.       if(c1 != c) return(-1);
  247.       i++;
  248.    }
  249.    m=0;
  250.    while(i<l) {
  251.       c1 = s2[i++];
  252.       if(c1 == c) break;
  253.       s1[m++] = c1;
  254.    }
  255.    s1[m] = 0;
  256.    if(strlen(s1)==0) return(-1);
  257.    return(1);
  258. }
  259.  
  260. /* --------------------------------------------------
  261.    check if character is a bra '([{' or a ket ')]}'
  262.    -------------------------------------------------- */
  263. tstbra(c)
  264. char c;
  265. {
  266.    if((c == '(') || (c == '[') || (c == '{')) return(1);
  267.    if((c == ')') || (c == ']') || (c == '}')) return(-1);
  268.    return(0);
  269. }
  270.  
  271.  
  272.  
  273.  
  274. /* --------------------------------------------------
  275.     get parameter enclosed in brackets from file.
  276.     any New line character will be substituted by the
  277.     supplied character.
  278.    -------------------------------------------------- */
  279. fgetpar(stream,s,nl)
  280. FILE *stream;
  281. char nl,s[];
  282. {
  283. int n,cnt;
  284. char c,bra,ket;
  285.  
  286.    n=0;
  287.    while(n != 1) {
  288.       if(feof(stream)) return(0);
  289.       c = fgetc(stream);
  290.       n = tstbra(c);
  291.    }
  292.    bra = c;
  293.    if(bra == '(') {
  294.       ket = ')';
  295.    } else {
  296.       ket = bra + 2;
  297.    }
  298. /* skip SPC and NL at start of line */
  299.    c = 0;
  300.    while(c < 33) c = fgetc(stream);
  301.    ungetc(c,stream);
  302.    n = 0; cnt = 1;
  303.    while(!feof(stream)) {
  304.       c = fgetc(stream);
  305.       if(c == bra) cnt = cnt + 1;
  306.       if(c == ket) cnt = cnt - 1;
  307.       if(c == 10) {s[n++] = ' ' ; c = nl; }
  308.       if(cnt == 0) break;
  309.       s[n++] = c;
  310.    }
  311.    s[n] = 0; 
  312. }
  313.  
  314. /* --------------------------------------------------
  315.     get parameter enclosed in brackets from string.
  316.    -------------------------------------------------- */
  317. sgetpar(str,s,n,j)
  318. char str[],s[];
  319. int n,*j;
  320. {
  321. int m,i,l,cnt;
  322. unsigned char c,bra,ket;
  323.  
  324.    l = strlen(str);
  325.    i = *j;
  326.    i = n; c=str[i];
  327.    bra = 0; ket=' ';
  328.    if(tstbra(c) == 1) {   
  329.       bra = c;
  330.       if(bra == '(') {
  331.         ket = ')';
  332.       } else {
  333.         ket = bra + 2;
  334.       }
  335.       i = i + 1;
  336.    }
  337.    m = 0; cnt = 1;
  338.    while(m < l) {
  339.      c = str[i++];
  340.      if(c == bra) cnt = cnt + 1;
  341.      if(c == ket) cnt = cnt - 1;
  342.      if((ket == ' ') && (tstbra(c) != 0)) cnt = cnt -1;
  343.      if((ket == ' ') && (c < '0')) cnt = cnt -1;
  344.      if(cnt <= 0) break;
  345.      s[m++] = c;
  346.    }
  347.    if(tstbra(c)==-1) i++;
  348.    s[m] = 0;
  349.    *j = i-1;
  350.    return(0);
  351. }
  352.  
  353.  
  354. /* ------------------------------------------------------
  355.    reading file and start initializing menus
  356.    ------------------------------------------------------ */
  357. menu_init(argv,stream)
  358. char *argv[];
  359. FILE *stream;
  360. {
  361. int n,i;
  362. int p1,p2;
  363. int x = 200,
  364.     y = 200,
  365.     set_flg = 0;
  366. float fx, fy;
  367. char c,s[256],s1[128],s2[128],*cmd,title[80];
  368. #ifdef UNIX
  369. Arg args[10];
  370. #endif
  371.    cmd = (char *) malloc(8192);
  372.    fx = 1.0; fy = 1.0;
  373.  
  374.    strcpy(title,"UNTITLED SHOULD CHANGE IT !");
  375.    while(!feof(stream)) {         /* go through file */
  376.       c=0;
  377.       while(c < 33) {            /* skip NL CR SPC TAB ... */
  378.          c = fgetc(stream);
  379.          if(feof(stream)) {free(cmd); return(0);}
  380.       }
  381.       ungetc(c,stream);
  382.       c = 33; s[0]=0; n=0;
  383.       while((c > 32) && (c < 123)) {      /* read menu command */
  384.          c = toupper(fgetc(stream));
  385.          if((c == '(') || (c == '[') || (c == ' ') || (c == '{')) break;
  386.          s[n++] = c;
  387.       }
  388.       ungetc(c,stream);
  389.       s[n] = 0;
  390. /* ----------- parse command and read following parameters ---------------- */
  391.       if(strcmp(s,"SIZE") == 0) {      /* size of main window */
  392.          fgetpar(stream,s,' ');
  393.          ExtractStr(s1,s,0,',');      /* get x size */
  394.          x = (int) (((float) atoi(s1)) * fx);
  395.          ExtractStr(s1,s,1,',');      /* get y size */
  396.          y = (int) (((float) atoi(s1)) * fy);
  397.          n = ExtractStr(s1,s,2,',');      /* get y increment */
  398.          if(n == 1) bas_y = atoi(s1);
  399.          n = ExtractStr(s1,s,3,',');      /* get top edge */
  400.          if(n == 1) wt = atoi(s1);
  401.          n = ExtractStr(s1,s,4,',');      /* get left edge */
  402.          if(n == 1) wl = atoi(s1);
  403.          continue;
  404.       }
  405.       if(strcmp(s,"NAME") == 0) {      /* name of application */
  406.          fgetpar(stream,s,' ');
  407.          strcpy(title,s);
  408.          continue;
  409.       }
  410.       if(strcmp(s,"FACTOR") == 0) {   /* scaling factor */
  411.          fgetpar(stream,s,' ');
  412.          ExtractStr(s1,s,0,',');      /* get x size */
  413.          sscanf(s1,"%f",&fx);
  414.          ExtractStr(s1,s,1,',');      /* get y size */
  415.          sscanf(s1,"%f",&fy);
  416.          continue;
  417.       }
  418.       if(strcmp(s,"LANGUAGE") == 0) {  /* command language for application */
  419.          fgetpar(stream,s,' ');
  420.          strcpy(language,s);
  421.          continue;
  422.       }
  423.       if(set_flg == 0) {         /* open window */
  424.          set_flg = 1;            /* should be done only once */
  425.    /*   Create application shell.
  426.    */
  427.  
  428. #ifdef AMIGA
  429.          my_new_window = init_window(x,y,title,0,1);
  430.          if(wl > 0)my_new_window->LeftEdge = wl;
  431.          if(wt > 0)my_new_window->TopEdge = wt;
  432. #endif
  433. #ifdef UNIX
  434.     n = 0;
  435.     XtSetArg (args[n], XmNwidth, x);  n++;
  436.     XtSetArg (args[n], XmNheight, y);  n++;
  437.     app_shell = XtAppCreateShell (myname,resource,
  438.                 applicationShellWidgetClass, display, args, n);
  439.     main_window = (Widget) CreateApplication (app_shell);
  440. #endif
  441.       }
  442.       if(strcmp(s,"STARTUP") == 0) {  /* do initializing procedure */
  443.          fgetpar(stream,cmd,10);
  444.          parse(cmd);
  445.          continue;
  446.       }
  447.       if(strcmp(s,"NEW_MENU") == 0) {      /* new entry on menu bar */
  448.          fgetpar(stream,s,' ');         /* get name */
  449.          add_item(-1,-1,NEW_MENU,s,0,0);
  450.          fn_number++;
  451.          continue;
  452.       }
  453.       if(strcmp(s,"MENU") == 0) {      /* new menu item */
  454.          fgetpar(stream,s,' ');         /* get name */
  455.          fgetpar(stream,cmd,10);      /* get action */
  456.          add_item(-1,-1,MENU,s,0,0);
  457.          fn_command[fn_number] = (void *)CallBack_mdl;
  458.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  459.          strcpy(fn_command_str[fn_number],cmd);
  460.          fn_number++;
  461.          continue;
  462.       }
  463.       if(strcmp(s,"RADIO") == 0) {      /* start radio box */
  464.          add_item(-1,-1,RADIO,"radio_box",0,0);
  465.          fn_number++;
  466.          continue;
  467.       }
  468.       if(strcmp(s,"TOGGLE") == 0) {      /* toggle button */
  469.          fgetpar(stream,s,' ');         /* get name and position */
  470.          fgetpar(stream,cmd,10);      /* get variable name */
  471.          ExtractStr(s1,s,0,',');
  472.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  473.          ExtractStr(s1,s,1,',');
  474.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  475.          ExtractStr(s1,s,2,',');       /* get name */
  476.          add_item(x,y,TOGGLE,s1,0,0);
  477.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  478.          strcpy(var_name_str[fn_number],cmd);
  479.          var_value[fn_number] = &var_value_int[fn_number];
  480.          fn_number++;
  481.          continue;
  482.       }
  483.       if(strcmp(s,"PUSH") == 0) {      /* push button */
  484.          fgetpar(stream,s,' ');         /* get name and position */
  485.          fgetpar(stream,cmd,10);      /* get command string */
  486.          ExtractStr(s1,s,0,',');
  487.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  488.          ExtractStr(s1,s,1,',');
  489.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  490.          ExtractStr(s1,s,2,',');       /* get name */
  491.          add_item(x,y,PUSH,s1,0,0);
  492.          fn_command[fn_number] = (void *)CallBack_mdl;
  493.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  494.          strcpy(fn_command_str[fn_number],cmd);
  495.          fn_number++;
  496.          continue;
  497.       }
  498.       if(strcmp(s,"SCALEX") == 0) {      /* scale */
  499.          fgetpar(stream,s,' ');         /* get name and position */
  500.          fgetpar(stream,cmd,10);      /* get variable name */
  501.          ExtractStr(s1,s,0,',');
  502.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  503.          ExtractStr(s1,s,1,',');
  504.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  505.          ExtractStr(s2,s,2,',');       /* get name */
  506.          ExtractStr(s1,s,3,',');
  507.          p1 = (int) (((float) atoi(s1)) * fx); /* get x position */
  508.          ExtractStr(s1,s,1,',');
  509.          p2 = (int) (((float) atoi(s1)) * fy); /* get y position */
  510.          add_item(x,y,SCALEX,s2,p1,p2);
  511.          var_value[fn_number] = &var_value_int[fn_number];
  512.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  513.          strcpy(var_name_str[fn_number],cmd);
  514.          fn_number++;
  515.          continue;
  516.       }
  517.       if(strcmp(s,"SCALEY") == 0) {      /* scale */
  518.          fgetpar(stream,s,' ');         /* get name and position */
  519.          fgetpar(stream,cmd,10);      /* get variable name */
  520.          ExtractStr(s1,s,0,',');
  521.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  522.          ExtractStr(s1,s,1,',');
  523.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  524.          ExtractStr(s2,s,2,',');       /* get name */
  525.          ExtractStr(s1,s,3,',');
  526.          p1 = (int) (((float) atoi(s1)) * fx); /* get x position */
  527.          ExtractStr(s1,s,1,',');
  528.          p2 = (int) (((float) atoi(s1)) * fy); /* get y position */
  529.          add_item(x,y,SCALEY,s2,p1,p2);
  530.          var_value[fn_number] = &var_value_int[fn_number];
  531.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  532.          strcpy(var_name_str[fn_number],cmd);
  533.          fn_number++;
  534.          continue;
  535.       }
  536.       if(strcmp(s,"SCALEX11") == 0) {      /* scale */
  537.          fgetpar(stream,s,' ');         /* get name and position */
  538.          fgetpar(stream,cmd,10);      /* get variable name */
  539.          ExtractStr(s1,s,0,',');
  540.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  541.          ExtractStr(s1,s,1,',');
  542.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  543.          ExtractStr(s2,s,2,',');       /* get name */
  544.          ExtractStr(s1,s,3,',');
  545.          p1 = (int) (((float) atoi(s1)) * fx); /* get x position */
  546.          ExtractStr(s1,s,1,',');
  547.          p2 = (int) (((float) atoi(s1)) * fy); /* get y position */
  548.          add_item(x,y,SCALEX11,s2,p1,p2);
  549.          var_value[fn_number] = &var_value_int[fn_number];
  550.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  551.          strcpy(var_name_str[fn_number],cmd);
  552.          fn_number++;
  553.          continue;
  554.       }
  555.       if(strcmp(s,"SCALEY11") == 0) {      /* scale */
  556.          fgetpar(stream,s,' ');         /* get name and position */
  557.          fgetpar(stream,cmd,10);      /* get variable name */
  558.          ExtractStr(s1,s,0,',');
  559.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  560.          ExtractStr(s1,s,1,',');
  561.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  562.          ExtractStr(s2,s,2,',');       /* get name */
  563.          ExtractStr(s1,s,3,',');
  564.          p1 = (int) (((float) atoi(s1)) * fx); /* get x position */
  565.          ExtractStr(s1,s,1,',');
  566.          p2 = (int) (((float) atoi(s1)) * fy); /* get y position */
  567.          add_item(x,y,SCALEY11,s2,p1,p2);
  568.          var_value[fn_number] = &var_value_int[fn_number];
  569.          var_name_str[fn_number] = (char *)malloc(strlen(cmd)+1);
  570.          strcpy(var_name_str[fn_number],cmd);
  571.          fn_number++;
  572.          continue;
  573.       }
  574.       if(strcmp(s,"SELECTION") == 0) {      /* selection box */
  575.          fgetpar(stream,s,' ');         /* get name and position */
  576.          ExtractStr(s1,s,0,',');
  577.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  578.          ExtractStr(s1,s,1,',');
  579.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  580.          n = 2; i = 0; s1[0] = 0;
  581.          while(i != -1) {
  582.             i = ExtractStr(s2,s,n++,',');    /* get name */
  583.             if(i == 1) {
  584.                if(s1[0] != 0) strcat(s1,",");
  585.                strcat(s1,s2);
  586.             }
  587.          }
  588.          add_item(x,y,SELECTION,s1,0,0);
  589.          var_value_str[fn_number] = (char *)malloc(80);
  590.          var_value[fn_number] = (int *)var_value_str[fn_number];
  591.          fn_command[fn_number] = (void *)CallBack_mdl;
  592.          fgetpar(stream,s2,' ');      /* get variable name */
  593.          fgetpar(stream,cmd,10);      /* get command string */
  594.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  595.          strcpy(fn_command_str[fn_number],cmd);
  596.          var_name_str[fn_number] = (char *)malloc(strlen(s2)+1);
  597.          strcpy(var_name_str[fn_number],s2);
  598.          fn_number++;
  599.          continue;
  600.       }
  601.       if(strcmp(s,"FILE_SELECT") == 0) {   /* selection box */
  602.          fgetpar(stream,s,' ');         /* get name and position */
  603.          fgetpar(stream,s2,' ');      /* get variable name */
  604.          fgetpar(stream,cmd,10);      /* get command string */
  605.          ExtractStr(s1,s,0,',');
  606.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  607.          ExtractStr(s1,s,1,',');
  608.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  609.          ExtractStr(s1,s,2,',');       /* get name */
  610.          add_item(x,y,FILE_SELECT,s1,0,0);
  611.          var_value_str[fn_number] = (char *)malloc(80);
  612.          var_value[fn_number] = (int *)var_value_str[fn_number];
  613.          fn_command[fn_number] = (void *)CallBack_mdl;
  614.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  615.          strcpy(fn_command_str[fn_number],cmd);
  616.          var_name_str[fn_number] = (char *)malloc(strlen(s2)+1);
  617.          strcpy(var_name_str[fn_number],s2);
  618.          fn_number++;
  619.          continue;
  620.       }
  621.       if(strcmp(s,"STRING_BOX") == 0) {      /* selection box */
  622.          fgetpar(stream,s,' ');         /* get name and position */
  623.          fgetpar(stream,s2,' ');      /* get variable name */
  624.          fgetpar(stream,cmd,10);      /* get command string */
  625.          ExtractStr(s1,s,0,',');
  626.          x = (int) (((float) atoi(s1)) * fx); /* get x position */
  627.          ExtractStr(s1,s,1,',');
  628.          y = (int) (((float) atoi(s1)) * fy); /* get y position */
  629.          ExtractStr(s1,s,2,',');       /* get name */
  630.          add_item(x,y,STRING,s1,0,0);
  631.          var_value_str[fn_number] = (char *)malloc(80); var_value_str[fn_number][0] = 0;
  632.          var_value[fn_number] = (int *)var_value_str[fn_number];
  633.          fn_command[fn_number] = (void *)CallBack_mdl;
  634.          fn_command_str[fn_number] = (char *) malloc(strlen(cmd)+1);
  635.          strcpy(fn_command_str[fn_number],cmd);
  636.          var_name_str[fn_number] = (char *)malloc(strlen(s2)+1);
  637.          strcpy(var_name_str[fn_number],s2);
  638.          fn_number++;
  639.          continue;
  640.       }
  641.       fprintf(stderr,"mdl: unknown command >%s<\n",s);
  642.       c = 0;
  643.       while(c != 10) {            /* try to synchronize */
  644.          if(feof(stream)) {free(cmd); return(0);}
  645.          c = fgetc(stream);
  646.       }
  647.    }
  648.    free(cmd);
  649. }
  650.  
  651. /* -------------------------------------------------------------
  652.    parse a string:
  653.    search for any occurence of $, and try to substitute
  654.    the variable.
  655.    Pass the string to the ksh and read the result
  656.    ------------------------------------------------------------- */
  657. parse(cstr)
  658. char cstr[];
  659. {
  660. int i,n,m,l;
  661. int flg;
  662. char c,s[80],*str,*str2;
  663. FILE *stream;
  664.  
  665.    str = (char *) malloc(8192); str2 = (char *) malloc(8192);
  666.    tmpnam_(s);
  667.  
  668. #ifdef AMIGA
  669.    strcpy(execfile,"ram:"); strcat(execfile,s);
  670. #endif
  671.  
  672. #ifdef UNIX
  673.    strcpy(execfile,s);
  674. #endif
  675.    strcpy(str,cstr);
  676.    n = 0; l = strlen(str);
  677.    while(n < l) {
  678.       if(str[n] == '$') {      /* search for variable substitution */
  679.          sgetpar(str,s,n+1,&i);
  680.          flg = 0;
  681.          for(m = 0; m < fn_number; m++) {      /* try to find menu variable */
  682.             if(var_name_str[m] == NULL) continue;
  683.             if(strcmp(s,var_name_str[m]) == 0 ) {   /* variable name found ! */
  684.                flg = 1;
  685.                if((var_funct[m] == STRING) || 
  686.                   (var_funct[m] == SELECTION) ||
  687.                   (var_funct[m] == FILE_SELECT)) {
  688.                   strcpy(s,var_value_str[m]);      /* get replacement value */
  689.                } else {
  690.                   sprintf(s,"%d",var_value_int[m]);
  691.                }
  692.                break;
  693.             }
  694.          }
  695.          if(flg == 0) {       /* try to find internal function */
  696.             if(strcmp(s,"HELP") == 0) {
  697.                flg = 1;
  698.                sgetpar(str,s,i,&i);
  699.                Help(s);
  700.                strcpy(s," ");
  701.             }
  702.             if(strcmp(s,"EXIT") == 0) {
  703. #ifdef AMIGA
  704.                close_gfx(my_window);
  705. #endif
  706.                exit(0);
  707.             }
  708.             if(strcmp(s,"STRINGBOX") == 0) {
  709.                flg = 1;
  710.                sgetpar(str,s,i,&i);
  711.                StringBox(s,s);
  712.             }
  713.             if(strcmp(s,"FILESELECT") == 0) {
  714.                flg = 1;
  715.                sgetpar(str,s,i,&i);
  716.                FileSelect(s,s);
  717.             }
  718.          }
  719.          if(flg == 1) {         /* replace variable name by string */
  720.             m = 0;
  721.             while(i < l) str2[m++] = str[i++];
  722.             str2[m]=0;
  723.             c = 32; m = 0;
  724.             while(s[m] != 0) str[n++] = s[m++];
  725.             str[n]=0;
  726.             strcat(str,str2); l=strlen(str);
  727.          }
  728.       }
  729.       n = n + 1;
  730.    }
  731. /* going to execute command. Output will be directed to a help window */
  732.    if(str[0] == '!') {
  733.       n = 0;
  734.       while(str[n] != 0) {str[n] = str[n+1]; n++;} ; 
  735.       ExecIO(str);
  736.    } else {
  737.       stream = fopen(execfile,"w");
  738.       fprintf(stream,"%s\n",str);
  739.       fclose(stream);
  740.  
  741.       strcpy(str,language);
  742.       strcat(str," ");
  743.       strcat(str,execfile);
  744.       ExecIO(str);
  745.       unlink(execfile);
  746.    }
  747.  
  748.    free(str); free(str2);
  749.    return(0);
  750. }
  751.  
  752. /* add I/O redirection to command, execute command and display output */
  753.  
  754. ExecIO(cmd)
  755. char cmd[];
  756. {
  757. int n,m,l;
  758. char *s;
  759. char *str;
  760. char *resultfile;
  761. FILE *stream;
  762.  
  763. #ifdef AMIGA
  764. struct FileHandle *input, *output;
  765. #endif
  766.  
  767.    str = (char *) malloc(8192);
  768.    resultfile = (char *) malloc(80);
  769.    s = (char *) malloc(80);
  770.  
  771. #ifdef AMIGA
  772.    tmpnam_(s); strcpy(resultfile,"ram:"); strcat(resultfile,s);
  773.  
  774.    n = 0;
  775.    while(cmd[n] != ' ') {str[n] = cmd[n]; n++;}
  776.    str[n] = 0;
  777.    m = 0; l = strlen(cmd);
  778.    while(n <= l) s[m++] = cmd[n++];
  779.    strcat(str," >"); strcat(str,resultfile);
  780.    strcat(str," <CON:30/30/30/30/mdl-hilf");
  781.    strcat(str,s);
  782. input = (struct FileHandle *) Open("nil:",MODE_OLDFILE);
  783. output = (struct FileHandle *) Open("nil:",MODE_NEWFILE);
  784.    Execute(str,input,output);
  785. Close(input);
  786. Close(output);
  787.  
  788.    stream = fopen(resultfile,"r");
  789.    n=fread(str,1,4094,stream); str[n]=0;
  790.    fclose(stream);
  791. #endif
  792.  
  793. #ifdef UNIX
  794.    tmpnam_(resultfile);
  795.    freopen(resultfile,"w",stdout);
  796.    system(cmd);
  797.    freopen(resultfile,"r",stdout);
  798.    n=fread(str,1,8190,stdout); str[n]=0;
  799.    freopen("/dev/syscon","w",stdout);
  800. #endif
  801.  
  802.    unlink(resultfile);
  803.    postparse(str);
  804.    free(s); free(resultfile); free(str);
  805.    return(0);
  806. }
  807.  
  808.  
  809. postparse(str)
  810. char *str;
  811. {
  812. char *s1, *s2, *s3, *s4, *cmd;
  813. int i,m,p;
  814.  
  815.    s1 = (char *) malloc(128);
  816.    s2 = (char *) malloc(128);
  817.    s3 = (char *) malloc(256);
  818.    s4 = (char *) malloc(8192);
  819.    cmd = (char *) malloc(128);
  820.  
  821.    p = 0; s4[0] = 0;
  822.    while(str[p] != 0) {
  823.       i = 0; while(str[p] != '\n') s3[i++] = str[p++];
  824.       s3[i] = 0; p = p + 1;
  825.  
  826.       sscanf(s3,"%s %s %s",cmd,s1,s2);
  827.  
  828.       if(strcmp(cmd,"SET") == 0) { /* SET variable */
  829.          for(m = 0; m < fn_number; m++) {      /* try to find menu variable */
  830.             if(var_name_str[m] == NULL) continue;
  831.             if(strcmp(s1,var_name_str[m]) == 0 ) {   /* variable name found ! */
  832.                if(var_funct[m] == STRING) strcpy(var_value_str[m],s2);
  833.                if(var_funct[m] == TOGGLE) var_value_int[m] = atoi(s2);
  834.                if(var_funct[m] == SCALEX) var_value_int[m] = atoi(s2);
  835.                if(var_funct[m] == SCALEY) var_value_int[m] = atoi(s2);
  836.                if(var_funct[m] == SCALEX11) var_value_int[m] = atoi(s2);
  837.                if(var_funct[m] == SCALEY11) var_value_int[m] = atoi(s2);
  838.                s3[0] = 0;
  839.                break;
  840.             }
  841.          }
  842.       }
  843.       if(strcmp(cmd,"EXIT") == 0) { /* EXIT menu */
  844. #ifdef AMIGA
  845.          close_gfx(my_window);
  846. #endif
  847.          free(cmd); free(s4); free(s3); free(s2); free(s1);
  848.          unlink(execfile);
  849.          exit(0);
  850.       }
  851.       if(strcmp(cmd,"CD") == 0) { /* CD */
  852.          changedir(s1);
  853.          free(cmd); free(s4); free(s3); free(s2); free(s1);
  854.          return(0);
  855.       }
  856.       if(s3[0] != 0) {strcat(s4,s3); strcat(s4,"\n");}
  857.    }
  858.    if(s4[0] != 0) Help(s4);
  859.    free(cmd); free(s4); free(s3); free(s2); free(s1);
  860. }
  861.  
  862.  
  863. changedir(str)
  864. char *str;
  865. {
  866. #ifdef AMIGA
  867. struct FileLock *pwd;
  868.  
  869.       pwd = (struct FileLock *) Lock(str,-2);
  870.       if(pwd != NULL) {
  871.          pwd = (struct FileLock *) CurrentDir(pwd);
  872.          UnLock(pwd);
  873.       }
  874. #endif
  875. #ifdef UNIX
  876.       chdir(str);
  877. #endif
  878. return(0);
  879. }
  880.  
  881.  
  882. checkopt(argc,argv,s,sv)
  883. int argc;
  884. char *argv[],s[],sv[];
  885. {
  886. int   n,erg;
  887.  
  888.    erg=FALSE;
  889.    for(n=1;n<argc;n++) {
  890.       if(strcmp(argv[n],s)==0) {
  891.          erg=TRUE;
  892.          if(argc > (n + 1)) strcpy(sv,argv[n+1]);
  893.       }
  894.    }
  895.    return(erg);
  896. }
  897.  
  898. #ifdef AMIGA
  899. amifork(str)
  900. char *str;
  901. {
  902. struct FileHandle *input, *output;
  903. char *s;
  904.  
  905. s = (char *) malloc(256);
  906. strcpy(s,"run >nil: <nil: "); strcat(s,str);
  907. input = (struct FileHandle *) Open("nil:",MODE_OLDFILE);
  908. output = (struct FileHandle *) Open("nil:",MODE_NEWFILE);
  909.    Execute(s,input,output);
  910. Close(input);
  911. Close(output);
  912. free(s);
  913. return(0);
  914. }
  915. #endif
  916.