svars.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /* The processing of string valued variables,
  9. this is an almost self contained module.
  10. Five externally visible routines:
  11. setsvar(name,result)
  12. Associate the name with the result.
  13. name a string pointer
  14. result a string pointer
  15. setpvar(name,routine)
  16. Associate the name with the routine.
  17. name a string pointer
  18. routine a routine id
  19. The parameters name and result are supposed to be pointing to
  20. non-volatile string storage used only for this call.
  21. char *getvar(name)
  22. returns the pointer to a string associated with name,
  23. the pointer is produced by returning result or the
  24. value returned by calling the routine.
  25. name a string pointer
  26. Other routines called
  27. fatal(args*) When something goes wrong
  28. getcore(size) Core allocation
  29. */
  30. struct vars {
  31. char *v_name;
  32. enum { routine, string } v_type;
  33. union {
  34. char *v_string;
  35. char *(*v_routine)();
  36. } v_value ;
  37. struct vars *v_next ;
  38. };
  39. static struct vars *v_first ;
  40. static struct vars *newvar(char *name)
  41. {
  42. register struct vars *new ;
  43. for ( new=v_first ; new ; new= new->v_next ) {
  44. if ( strcmp(name,new->v_name)==0 ) {
  45. throws(name) ;
  46. if ( new->v_type== string ) {
  47. throws(new->v_value.v_string) ;
  48. }
  49. return new ;
  50. }
  51. }
  52. new= (struct vars *)getcore( (unsigned)sizeof (struct vars));
  53. new->v_name= name ;
  54. new->v_next= v_first ;
  55. v_first= new ;
  56. return new ;
  57. }
  58. void setsvar(char *name, char *str)
  59. {
  60. register struct vars *new ;
  61. new= newvar(name);
  62. #ifdef DEBUG
  63. if ( debug>=2 ) vprint("%s=%s\n", name, str) ;
  64. #endif
  65. new->v_type= string;
  66. new->v_value.v_string= str;
  67. }
  68. void setpvar(char *name,char *(*rout)(void))
  69. {
  70. register struct vars *new ;
  71. new= newvar(name);
  72. #ifdef DEBUG
  73. if ( debug>=2 ) vprint("%s= (*%o)()\n",name,rout) ;
  74. #endif
  75. new->v_type= routine;
  76. new->v_value.v_routine= rout;
  77. }
  78. char *getvar(char *name)
  79. {
  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. }