svars.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. */
  6. #include "ack.h"
  7. #ifndef NORCSID
  8. static char rcs_id[] = "$Id$" ;
  9. #endif
  10. /* The processing of string valued variables,
  11. this is an almost self contained module.
  12. Five externally visible routines:
  13. setsvar(name,result)
  14. Associate the name with the result.
  15. name a string pointer
  16. result a string pointer
  17. setpvar(name,routine)
  18. Associate the name with the routine.
  19. name a string pointer
  20. routine a routine id
  21. The parameters name and result are supposed to be pointing to
  22. non-volatile string storage used only for this call.
  23. char *getvar(name)
  24. returns the pointer to a string associated with name,
  25. the pointer is produced by returning result or the
  26. value returned by calling the routine.
  27. name a string pointer
  28. Other routines called
  29. fatal(args*) When something goes wrong
  30. getcore(size) Core allocation
  31. */
  32. extern char *getcore();
  33. extern fatal();
  34. struct vars {
  35. char *v_name;
  36. enum { routine, string } v_type;
  37. union {
  38. char *v_string;
  39. char *(*v_routine)();
  40. } v_value ;
  41. struct vars *v_next ;
  42. };
  43. static struct vars *v_first ;
  44. static struct vars *newvar(name) char *name; {
  45. register struct vars *new ;
  46. for ( new=v_first ; new ; new= new->v_next ) {
  47. if ( strcmp(name,new->v_name)==0 ) {
  48. throws(name) ;
  49. if ( new->v_type== string ) {
  50. throws(new->v_value.v_string) ;
  51. }
  52. return new ;
  53. }
  54. }
  55. new= (struct vars *)getcore( (unsigned)sizeof (struct vars));
  56. new->v_name= name ;
  57. new->v_next= v_first ;
  58. v_first= new ;
  59. return new ;
  60. }
  61. setsvar(name,str) char *name, *str ; {
  62. register struct vars *new ;
  63. new= newvar(name);
  64. #ifdef DEBUG
  65. if ( debug>=2 ) vprint("%s=%s\n", name, str) ;
  66. #endif
  67. new->v_type= string;
  68. new->v_value.v_string= str;
  69. }
  70. setpvar(name,rout) char *name, *(*rout)() ; {
  71. register struct vars *new ;
  72. new= newvar(name);
  73. #ifdef DEBUG
  74. if ( debug>=2 ) vprint("%s= (*%o)()\n",name,rout) ;
  75. #endif
  76. new->v_type= routine;
  77. new->v_value.v_routine= rout;
  78. }
  79. char *getvar(name) char *name ; {
  80. register struct vars *scan ;
  81. for ( scan=v_first ; scan ; scan= scan->v_next ) {
  82. if ( strcmp(name,scan->v_name)==0 ) {
  83. switch ( scan->v_type ) {
  84. case string:
  85. return scan->v_value.v_string ;
  86. case routine:
  87. return (*scan->v_value.v_routine)() ;
  88. }
  89. }
  90. }
  91. return (char *)0 ;
  92. }