home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / misc / b186_1 / Source / c / general < prev    next >
Text File  |  1994-02-15  |  6KB  |  249 lines

  1. /*
  2.  
  3.        This file is part of the PDP software package.
  4.          
  5.        Copyright 1987 by James L. McClelland and David E. Rumelhart.
  6.        
  7.        Please refer to licensing information in the file license.txt,
  8.        which is in the same directory with this source file and is
  9.        included here by reference.
  10. */
  11.  
  12.  
  13. /* general.c
  14.  
  15.     Some general functions for PPD-pc package.
  16.     
  17.     First version implemented by Elliot Jaffe.
  18.     
  19.     Date of last revision:  8-12-87/JLM.
  20. */
  21. /*LINTLIBRARY*/
  22.  
  23. #include "general.h"
  24. #include "command.h"
  25. #include "variable.h"
  26. #include <signal.h>
  27.  
  28. #ifdef MSDOS
  29. #include <memory.h>   /* for memcpy() in erealloc in general.c */
  30. #include <process.h>  /* for system() in do_exec in command.c */
  31. #endif
  32.  
  33. FILE * in_stream = stdin;
  34. int     Interrupt_flag = 0;
  35. int    single_flag = 0;
  36. int    step_size;
  37. int    random_seed;
  38. char    step_string[STRINGLENGTH];
  39.  
  40. struct Command_table    *Command;
  41.  
  42. extern int  dump_template ();
  43. extern int  clear_display ();
  44. extern int  update_display ();
  45. extern int  redisplay ();
  46. extern int  do_io ();
  47. extern int  do_network ();
  48. extern int  do_system ();
  49. extern int  do_command ();
  50. extern int  do_comfile ();
  51. extern int  do_exec ();
  52. extern int  set_log ();
  53. extern int  run_fork();
  54.  
  55.  
  56. int_handler() {
  57.     int     int_handler ();
  58.  
  59.     (void) signal(SIGINT, int_handler);
  60.     Interrupt_flag = 1;
  61. }
  62.  
  63. #ifdef MSDOS
  64. char *index(somestring,somechar)
  65. char *somestring;
  66. char somechar;
  67. {
  68.     return strchr(somestring,somechar);
  69. }
  70. #endif
  71.  
  72. char   *emalloc (n)        /* check return from malloc */
  73. unsigned    n;
  74. {
  75.     char   *p,
  76.            *malloc ();
  77.  
  78.     p = malloc(n);
  79.     if (p == 0) {
  80.     put_error("out of memory");
  81.     end_display();
  82.     exit(0);
  83.     }
  84.     return p;
  85. }
  86.  
  87. char   *erealloc (ptr,oldsize,newsize)        /* check return from realloc*/
  88. char *ptr;
  89. unsigned    oldsize;
  90. unsigned    newsize;
  91. {
  92. #ifndef MSDOS
  93.     char   *realloc ();
  94.     char   *p;
  95.  
  96.     p = realloc(ptr,newsize);
  97.     if (p == 0) {
  98.     put_error("out of memory");
  99.     end_display();
  100.     exit(0);
  101.       }
  102.     return p;
  103.     
  104. #else (if MSDOS)
  105.  
  106.     char *malloc();
  107.     char *p;
  108.     
  109.     p = malloc(newsize);
  110.     if (p == 0) {
  111.         put_error("out of memory");
  112.       end_display();
  113.       exit(0);
  114.     }
  115.     if (ptr && p) {
  116.     memcpy(p, ptr, oldsize);
  117.     free(ptr);
  118.     }
  119.     return p;
  120.  
  121. #endif MSDOS
  122. }
  123.  
  124. startsame(s1, s2)        /* does s1 start the same as s2? */
  125. char   *s1,
  126.        *s2; {
  127.     while (*s1 && *s2) {
  128.     if (*s1++ != *s2++)
  129.         return(0);
  130.     }
  131.     if(*s1 && !*s2)  /* if s1 is longer than s2 it should fail */
  132.        return(0);
  133.     return(1);
  134. }
  135.  
  136. char   *strsave (s)
  137. char   *s;
  138. {
  139.     char   *p,
  140.            *emalloc ();
  141.  
  142.     if ((p = emalloc((unsigned)(strlen(s) + 1))) != NULL)
  143.     (void) strcpy(p, s);
  144.     return(p);
  145. }
  146.  
  147.  
  148. randint(low, high)
  149.  int  low,high; {
  150.     int     answer;
  151.     float   randf;
  152.     int     range;
  153.  
  154.     randf = rnd();
  155.     range = high - low + 1;
  156.     answer = randf * range + low;
  157.     return(answer);
  158. }
  159.  
  160. quit() {
  161.    char * str;
  162.    str = get_command("Quit program? -- type y to confirm:  ");
  163.  
  164.    if (str && str[0] == 'y') {
  165.         end_display();
  166.         exit(0);
  167.    }
  168.    else
  169.       return(CONTINUE);
  170. }
  171.  
  172. set_step() {
  173.     char old_step_string[STRINGLENGTH];
  174.     struct Variable *vp, *lookup_var();
  175.     
  176.     strcpy(old_step_string,step_string);
  177.  
  178.     vp = lookup_var("stepsize");
  179.     change_variable("stepsize",vp);
  180.     
  181.     if (startsame(step_string,"nepochs")) strcpy(step_string,"nepochs");
  182.     else if (startsame(step_string,"epoch")) strcpy(step_string,"epoch");
  183.     else if (startsame(step_string,"pattern")) strcpy(step_string,"pattern");
  184.     else if (startsame(step_string,"ncycles")) strcpy(step_string,"ncycles");
  185.     else if (startsame(step_string,"cycle")) strcpy(step_string,"cycle");
  186.     else if (startsame(step_string,"update")) strcpy(step_string,"update");
  187.     else if (startsame(step_string,"default"))
  188.                     strcpy(step_string,Default_step_string);
  189.     else {
  190.     strcpy(step_string,old_step_string);
  191.         return(put_error("urecognized stepsize -- size not changed."));
  192.     }
  193.     set_stepsize();
  194.     return(CONTINUE);
  195. }
  196.  
  197. set_stepsize() {
  198.     if (strcmp(step_string,"update") == 0) step_size = UPDATE;
  199.     else if (strcmp(step_string,"cycle") == 0) step_size = CYCLE;
  200.     else if (strcmp(step_string,"ncycles") == 0) step_size = NCYCLES;
  201.     else if (strcmp(step_string,"pattern") == 0) step_size = PATTERN;
  202.     else if (strcmp(step_string,"epoch") == 0) step_size = EPOCH;
  203.     else if (strcmp(step_string,"nepochs") == 0) step_size = NEPOCHS;
  204. }
  205.  
  206. init_general() {
  207.     extern int     int_handler ();
  208.  
  209.     Interrupt_flag = 0;
  210.     strcpy(step_string,Default_step_string);
  211.     set_stepsize();
  212.     init_commands();
  213.     (void) signal(SIGINT, int_handler);
  214.     (void) install_command("?", do_help, 0, 0);
  215.     (void) install_command("disp/", do_command, BASEMENU, (int *) DISPLAYMENU);
  216.     (void) install_command("opt/",  do_command, DISPLAYMENU, (int *) DISPLAYOPTIONS);
  217.     (void) install_command("exam/", do_command, BASEMENU, (int *) SETMENU);
  218.     (void) install_command("get/",  do_command, BASEMENU, (int *) GETMENU);
  219.     (void) install_command("save/", do_command, BASEMENU, (int *) SAVEMENU);
  220.     (void) install_command("set/",  do_command, BASEMENU, (int *) SETMENU);
  221.     (void) install_command("config/", do_command, SETMENU, (int *) SETCONFMENU);
  222.     (void) install_command("env/",  do_command, SETMENU, (int *) SETENVMENU);
  223.     (void) install_command("mode/", do_command, SETMENU, (int *) SETMODEMENU);
  224.     (void) install_command("param/",do_command, SETMENU, (int *) SETPARAMMENU);
  225.     (void) install_command("state/", do_command, SETMENU, (int *) SETSVMENU);
  226.     (void) install_command("clear", clear_display, BASEMENU, 0);
  227.     (void) install_command("do",  do_comfile, BASEMENU, 0);
  228.     (void) install_command("log",   set_log, BASEMENU, 0);
  229.     (void) install_command("quit",  quit, BASEMENU, 0);
  230.     (void) install_command("run", do_exec, BASEMENU, 0);
  231. /*  (void) install_command("srand", random_seed, BASEMENU, 0); */
  232.     (void) install_command("state", redisplay, DISPLAYMENU, 0);
  233.     (void) install_var("seed", Int, (int *) & random_seed, 0, 0,SETPCMENU);
  234.     (void) install_var("single", Int, (int *) & single_flag, 0, 0,SETPCMENU);
  235.     (void) install_var("stepsize", String, (int *) step_string,0, 0,NOMENU);
  236.     (void) install_command("stepsize",set_step,SETPCMENU,(int *) NULL);
  237. }
  238.  
  239. #ifdef MSDOS
  240. sleep(n_sec)
  241. int n_sec;
  242. {
  243.     int i,j;
  244.     for (i = 0; i < (n_sec); i++)
  245.         for (j = 0; j < 20000; j++);
  246. }
  247. #endif  MSDOS
  248.  
  249.