lookup.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef NORCSID
  2. static char rcsid[]= "$Header$";
  3. #endif
  4. #include "assert.h"
  5. #include "param.h"
  6. #include "lookup.h"
  7. char *myalloc();
  8. char *mystrcpy();
  9. symbol dumsym; /* dummy to return in case of error */
  10. symbol *lookup(name,type,style)
  11. char *name;
  12. symtype type;
  13. lookupstyle style;
  14. {
  15. symbol *sy_p,**sy_pp;
  16. for (sy_pp = &symhash[hashvalue(name)];(sy_p= *sy_pp) != 0;sy_pp= &sy_p->sy_next) {
  17. if (strcmp(sy_p->sy_name,name)!=0)
  18. continue;
  19. switch(style) {
  20. default:
  21. assert(0);
  22. case justlooking:
  23. case mustexist:
  24. case makeexist:
  25. if (type==symany || type==sy_p->sy_type)
  26. return(sy_p);
  27. continue;
  28. case newsymbol:
  29. error("%s already defined",name);
  30. return(&dumsym);
  31. }
  32. }
  33. switch(style) {
  34. default:
  35. assert(0);
  36. case justlooking:
  37. return((symbol *) 0);
  38. case mustexist:
  39. fatal("%s is unknown symbol",name);
  40. /* NOTREACHED */
  41. case newsymbol:
  42. case makeexist:
  43. NEW(sy_p,symbol);
  44. sy_p->sy_next = 0;
  45. sy_p->sy_name = mystrcpy(name);
  46. assert(type!=symany);
  47. sy_p->sy_type = type;
  48. *sy_pp = sy_p;
  49. return(sy_p);
  50. }
  51. }
  52. hashvalue(s) register char *s; {
  53. register unsigned sum=0;
  54. register i;
  55. for(i=0;*s;s++,i=(i+3)&07)
  56. sum += *s<<i;
  57. return(sum%NSYMHASH);
  58. }