svars.c 2.8 KB

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