attr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <auth.h>
  12. int
  13. _attrfmt(Fmt *fmt)
  14. {
  15. char *b, buf[1024], *ebuf;
  16. Attr *a;
  17. ebuf = buf+sizeof buf;
  18. b = buf;
  19. strcpy(buf, " ");
  20. for(a=va_arg(fmt->args, Attr*); a; a=a->next){
  21. if(a->name == nil)
  22. continue;
  23. switch(a->type){
  24. case AttrQuery:
  25. b = seprint(b, ebuf, " %q?", a->name);
  26. break;
  27. case AttrNameval:
  28. b = seprint(b, ebuf, " %q=%q", a->name, a->val);
  29. break;
  30. case AttrDefault:
  31. b = seprint(b, ebuf, " %q:=%q", a->name, a->val);
  32. break;
  33. }
  34. }
  35. return fmtstrcpy(fmt, buf+1);
  36. }
  37. Attr*
  38. _copyattr(Attr *a)
  39. {
  40. Attr **la, *na;
  41. na = nil;
  42. la = &na;
  43. for(; a; a=a->next){
  44. *la = _mkattr(a->type, a->name, a->val, nil);
  45. setmalloctag(*la, getcallerpc());
  46. la = &(*la)->next;
  47. }
  48. *la = nil;
  49. return na;
  50. }
  51. Attr*
  52. _delattr(Attr *a, char *name)
  53. {
  54. Attr *fa;
  55. Attr **la;
  56. for(la=&a; *la; ){
  57. if(strcmp((*la)->name, name) == 0){
  58. fa = *la;
  59. *la = (*la)->next;
  60. fa->next = nil;
  61. _freeattr(fa);
  62. }else
  63. la=&(*la)->next;
  64. }
  65. return a;
  66. }
  67. Attr*
  68. _findattr(Attr *a, char *n)
  69. {
  70. for(; a; a=a->next)
  71. if(strcmp(a->name, n) == 0 && a->type != AttrQuery)
  72. return a;
  73. return nil;
  74. }
  75. void
  76. _freeattr(Attr *a)
  77. {
  78. Attr *anext;
  79. for(; a; a=anext){
  80. anext = a->next;
  81. free(a->name);
  82. free(a->val);
  83. a->name = (void*)~0;
  84. a->val = (void*)~0;
  85. a->next = (void*)~0;
  86. free(a);
  87. }
  88. }
  89. Attr*
  90. _mkattr(int type, char *name, char *val, Attr *next)
  91. {
  92. Attr *a;
  93. a = malloc(sizeof(*a));
  94. if(a==nil)
  95. sysfatal("_mkattr malloc: %r");
  96. a->type = type;
  97. a->name = strdup(name);
  98. a->val = strdup(val);
  99. if(a->name==nil || a->val==nil)
  100. sysfatal("_mkattr malloc: %r");
  101. a->next = next;
  102. setmalloctag(a, getcallerpc());
  103. return a;
  104. }
  105. static Attr*
  106. cleanattr(Attr *a)
  107. {
  108. Attr *fa;
  109. Attr **la;
  110. for(la=&a; *la; ){
  111. if((*la)->type==AttrQuery && _findattr(a, (*la)->name)){
  112. fa = *la;
  113. *la = (*la)->next;
  114. fa->next = nil;
  115. _freeattr(fa);
  116. }else
  117. la=&(*la)->next;
  118. }
  119. return a;
  120. }
  121. Attr*
  122. _parseattr(char *s)
  123. {
  124. char *p, *t, *tok[256];
  125. int i, ntok, type;
  126. Attr *a;
  127. s = strdup(s);
  128. if(s == nil)
  129. sysfatal("_parseattr strdup: %r");
  130. ntok = tokenize(s, tok, nelem(tok));
  131. a = nil;
  132. for(i=ntok-1; i>=0; i--){
  133. t = tok[i];
  134. if((p = strchr(t, '='))){
  135. *p++ = '\0';
  136. // if(p-2 >= t && p[-2] == ':'){
  137. // p[-2] = '\0';
  138. // type = AttrDefault;
  139. // }else
  140. type = AttrNameval;
  141. a = _mkattr(type, t, p, a);
  142. setmalloctag(a, getcallerpc());
  143. }
  144. else if(t[strlen(t)-1] == '?'){
  145. t[strlen(t)-1] = '\0';
  146. a = _mkattr(AttrQuery, t, "", a);
  147. setmalloctag(a, getcallerpc());
  148. }else{
  149. /* really a syntax error, but better to provide some indication */
  150. a = _mkattr(AttrNameval, t, "", a);
  151. setmalloctag(a, getcallerpc());
  152. }
  153. }
  154. free(s);
  155. return cleanattr(a);
  156. }
  157. char*
  158. _strfindattr(Attr *a, char *n)
  159. {
  160. a = _findattr(a, n);
  161. if(a == nil)
  162. return nil;
  163. return a->val;
  164. }