lac : diff 214ca407 3642355a

Branch: master

Commit: 214ca407357fd1f95de2e668d577d3eee919c6d9

Author: Gianluca Guida <glguida@tlbflush.org>
Date: Tue Mar 7 18:59:24 UTC 2017
Parent: 3642355a08e524d20ef0ccb62d66c1f1840edaaf
Log message:

    lac: move lac_error_print in lib, simplify init

    1: diff --git a/src/lib/lac.c b/src/lib/lac.c
    2: index ab9fddc..ba7875a 100644
    3: --- a/src/lib/lac.c
    4: +++ b/src/lib/lac.c
    5: @@ -733,12 +733,14 @@ static void machine_init(lenv_t *env)
    6:    lac_extproc_register(env, "GC-COLLECT", proc_collect);
    7:  }
    8:  
    9: -void map_init(lenv_t *env);
   10: -void int_init(lenv_t *env);
   11: -void string_init(lenv_t *env);
   12: +
   13:  static void
   14:  modules_init(lenv_t *env)
   15:  {
   16: +  void map_init(lenv_t *env);
   17: +  void int_init(lenv_t *env);
   18: +  void string_init(lenv_t *env);
   19: +
   20:    int_init(env);
   21:    string_init(env);
   22:    map_init(env);
   23: @@ -759,21 +761,41 @@ library_init(lenv_t *env)
   24:    fclose(f);
   25:  }
   26:  
   27: +
   28: +void
   29: +lac_error_print (FILE * f)
   30: +{
   31: +  fprintf (f, "(*LAC-ERROR* \"%s", lac_errmsg ());
   32: +  if (lac_errlreg () != NIL)
   33: +    {
   34: +      fprintf (f, ": ");
   35: +      sexpr_fprint (f, lac_errlreg ());
   36: +    }
   37: +  fprintf (f, "\")\n");
   38: +}
   39: +
   40: +
   41:  lenv_t *
   42:  lac_init(void)
   43:  {
   44:    sigset_t emptyset;
   45:    lenv_t *env;
   46:    GC_init();
   47: - 
   48: +
   49:    sigemptyset(&emptyset); 
   50:    sigprocmask(SIG_BLOCK, &emptyset, &mainsigset);
   51:  
   52: +  lac_on_error ({
   53: +      lac_error_print (stderr);
   54: +      return NULL;
   55: +    });
   56: +
   57:    stackoverflow_install_handler(stackovf_handler, extra_stack, 16384);
   58:    env = lac_envalloc();
   59:    machine_init(env);
   60:    modules_init(env);
   61:    library_init(env);
   62: +  lac_off_error();
   63:  
   64:    return env;
   65:  }
   66: diff --git a/src/lib/lac.h b/src/lib/lac.h
   67: index 012c293..5973ac0 100644
   68: --- a/src/lib/lac.h
   69: +++ b/src/lib/lac.h
   70: @@ -283,6 +283,8 @@ extern __thread lreg_t _lac_xcpt_reg;
   71:      free(p);					\
   72:    } while(0)
   73:  
   74: +void lac_error_print (FILE * f);
   75: +
   76:  
   77:  /*
   78:   * Representations
   79: diff --git a/src/repl/lac.c b/src/repl/lac.c
   80: index 8b2fb62..666408b 100644
   81: --- a/src/repl/lac.c
   82: +++ b/src/repl/lac.c
   83: @@ -11,18 +11,6 @@
   84:  lenv_t *null_env;
   85:  
   86:  void
   87: -lac_error_print (FILE * f)
   88: -{
   89: -  fprintf (f, "(*LAC-ERROR* \"%s", lac_errmsg ());
   90: -  if (lac_errlreg () != NIL)
   91: -    {
   92: -      fprintf (f, ": ");
   93: -      sexpr_fprint (f, lac_errlreg ());
   94: -    }
   95: -  fprintf (f, "\")\n");
   96: -}
   97: -
   98: -void
   99:  sigint (int sig)
  100:  {
  101:    raise_exception ("Interrupted", NIL);
  102: @@ -38,47 +26,49 @@ sigint (int sig)
  103:       ? (_b)->tv_usec - (_a)->tv_usec					\
  104:       : (_b)->tv_usec + 1000000L - (_a)->tv_usec)
  105:  
  106: -lreg_t timed_eval(lreg_t lr, void *opq)
  107: +lreg_t
  108: +timed_eval (lreg_t lr, void *opq)
  109:  {
  110: -	lreg_t ret;
  111: -	struct timeval t1, t2;
  112: -	lenv_t *env = (lenv_t *)opq;
  113: +  lreg_t ret;
  114: +  struct timeval t1, t2;
  115: +  lenv_t *env = (lenv_t *) opq;
  116:  
  117: -	gettimeofday(&t1, NULL);
  118: -	ret = eval(lr, env);
  119: -	gettimeofday(&t2, NULL);
  120: +  gettimeofday (&t1, NULL);
  121: +  ret = eval (lr, env);
  122: +  gettimeofday (&t2, NULL);
  123:  
  124: -	fprintf(stderr, "Evaluation took %ld seconds and %ld microseconds.\n",
  125: -		TDIFF_SEC(&t1, &t2), TDIFF_USEC(&t1, &t2));
  126: +  fprintf (stderr, "Evaluation took %ld seconds and %ld microseconds.\n",
  127: +	   TDIFF_SEC (&t1, &t2), TDIFF_USEC (&t1, &t2));
  128:  
  129: -	return ret;
  130: +  return ret;
  131:  }
  132:  
  133:  int
  134:  repl (FILE * infd, FILE * outfd, FILE * errfd)
  135:  {
  136: -  int r;
  137: +  int r = 0;
  138:    char *buf = NULL;
  139:    lreg_t res = NIL;
  140: -  struct timeval t1, t2;
  141:  
  142:  restart:
  143:    lac_on_error ({
  144: -      lac_error_print (errfd); res = NIL; goto restart;
  145: +		 lac_error_print (errfd);
  146: +		 res = NIL;
  147: +		 goto restart;
  148:      });
  149:  
  150: -
  151:    do
  152:      {
  153:        buf = readline ("LAC>");
  154:        if (buf == NULL)
  155: -	return -1;
  156: +	{
  157: +	  r = -1;
  158: +	  break;
  159: +	}
  160:        if (*buf != '\0')
  161:  	add_history (buf);
  162:  
  163: -      res = sexpr_parse_string (buf,
  164: -				timed_eval,
  165: -				(void *)null_env);
  166: +      res = sexpr_parse_string (buf, timed_eval, (void *) null_env);
  167:        if (isatty (fileno (outfd)))
  168:  	{
  169:  	  fprintf (outfd, "=> ");
  170: @@ -93,16 +83,17 @@ restart:
  171:  }
  172:  
  173:  int
  174: -main ()
  175: +main (int argc, char *argv[])
  176:  {
  177:  
  178:    signal (SIGINT, sigint);
  179:  
  180: -  lac_on_error ({
  181: -      lac_error_print (stderr); exit (-1);
  182: -    });
  183: -
  184:    null_env = lac_init ();
  185: +  if (null_env == NULL)
  186: +    {
  187: +      fprintf (stderr, "Initialization failed.");
  188: +      return -1;
  189: +    }
  190:    repl (stdin, stdout, stderr);
  191:    fprintf (stdout, "\ngoodbye!\n");
  192:    return 0;

Generated by git2html.