svars.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <string.h>
  7. #include "ack.h"
  8. #ifndef NORCSID
  9. static char rcs_id[] = "$Id$" ;
  10. #endif
  11. /* The processing of string valued variables,
  12. this is an almost self contained module.
  13. Five externally visible routines:
  14. setsvar(name,result)
  15. Associate the name with the result.
  16. name a string pointer
  17. result a string pointer
  18. setpvar(name,routine)
  19. Associate the name with the routine.
  20. name a string pointer
  21. routine a routine id
  22. The parameters name and result are supposed to be pointing to
  23. non-volatile string storage used only for this call.
  24. char *getvar(name)
  25. returns the pointer to a string associated with name,
  26. the pointer is produced by returning result or the
  27. value returned by calling the routine.
  28. name a string pointer
  29. Other routines called
  30. fatal(args*) When something goes wrong
  31. getcore(size) Core allocation
  32. */
  33. struct vars {
  34. char *v_name;
  35. enum { routine, string } v_type;
  36. union {
  37. char *v_string;
  38. char *(*v_routine)();
  39. } v_value ;
  40. struct vars *v_next ;
  41. };
  42. static struct vars *v_first ;
  43. static struct vars *newvar(char *name)
  44. {
  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. void setsvar(char *name, char *str)
  62. {
  63. register struct vars *new ;
  64. new= newvar(name);
  65. #ifdef DEBUG
  66. if ( debug>=2 ) vprint("%s=%s\n", name, str) ;
  67. #endif
  68. new->v_type= string;
  69. new->v_value.v_string= str;
  70. }
  71. void setpvar(char *name,char *(*rout)(void))
  72. {
  73. register struct vars *new ;
  74. new= newvar(name);
  75. #ifdef DEBUG
  76. if ( debug>=2 ) vprint("%s= (*%o)()\n",name,rout) ;
  77. #endif
  78. new->v_type= routine;
  79. new->v_value.v_routine= rout;
  80. }
  81. char *getvar(char *name)
  82. {
  83. register struct vars *scan ;
  84. for ( scan=v_first ; scan ; scan= scan->v_next ) {
  85. if ( strcmp(name,scan->v_name)==0 ) {
  86. switch ( scan->v_type ) {
  87. case string:
  88. return scan->v_value.v_string ;
  89. case routine:
  90. return (*scan->v_value.v_routine)() ;
  91. }
  92. }
  93. }
  94. return (char *)0 ;
  95. }