util.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <thread.h>
  12. #include <keyboard.h>
  13. #include <fcall.h>
  14. #include "dat.h"
  15. #include "fns.h"
  16. void
  17. cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
  18. {
  19. print_func_entry();
  20. uint8_t *q;
  21. Rune *s;
  22. int j, w;
  23. /*
  24. * Always guaranteed that n bytes may be interpreted
  25. * without worrying about partial runes. This may mean
  26. * reading up to UTFmax-1 more bytes than n; the caller
  27. * knows this. If n is a firm limit, the caller should
  28. * set p[n] = 0.
  29. */
  30. q = (uint8_t*)p;
  31. s = r;
  32. for(j=0; j<n; j+=w){
  33. if(*q < Runeself){
  34. w = 1;
  35. *s = *q++;
  36. }else{
  37. w = chartorune(s, (char*)q);
  38. q += w;
  39. }
  40. if(*s)
  41. s++;
  42. else if(nulls)
  43. *nulls = TRUE;
  44. }
  45. *nb = (char*)q-p;
  46. *nr = s-r;
  47. print_func_exit();
  48. }
  49. void
  50. error(char *s)
  51. {
  52. print_func_entry();
  53. fprint(2, "rio: %s: %r\n", s);
  54. if(errorshouldabort)
  55. abort();
  56. threadexitsall("error");
  57. print_func_exit();
  58. }
  59. void*
  60. erealloc(void *p, uint n)
  61. {
  62. print_func_entry();
  63. p = realloc(p, n);
  64. if(p == nil)
  65. error("realloc failed");
  66. print_func_exit();
  67. return p;
  68. }
  69. void*
  70. emalloc(uint n)
  71. {
  72. print_func_entry();
  73. void *p;
  74. p = malloc(n);
  75. if(p == nil)
  76. error("malloc failed");
  77. memset(p, 0, n);
  78. print_func_exit();
  79. return p;
  80. }
  81. char*
  82. estrdup(char *s)
  83. {
  84. print_func_entry();
  85. char *p;
  86. p = malloc(strlen(s)+1);
  87. if(p == nil)
  88. error("strdup failed");
  89. strcpy(p, s);
  90. print_func_exit();
  91. return p;
  92. }
  93. int
  94. isalnum(Rune c)
  95. {
  96. print_func_entry();
  97. /*
  98. * Hard to get absolutely right. Use what we know about ASCII
  99. * and assume anything above the Latin control characters is
  100. * potentially an alphanumeric.
  101. */
  102. if(c <= ' ') {
  103. print_func_exit();
  104. return FALSE;
  105. }
  106. if(0x7F<=c && c<=0xA0) {
  107. print_func_exit();
  108. return FALSE;
  109. }
  110. if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c)) {
  111. print_func_exit();
  112. return FALSE;
  113. }
  114. print_func_exit();
  115. return TRUE;
  116. }
  117. Rune*
  118. strrune(Rune *s, Rune c)
  119. {
  120. print_func_entry();
  121. Rune c1;
  122. if(c == 0) {
  123. while(*s++)
  124. ;
  125. print_func_exit();
  126. return s-1;
  127. }
  128. while(c1 = *s++)
  129. if(c1 == c) {
  130. print_func_exit();
  131. return s-1;
  132. }
  133. print_func_exit();
  134. return nil;
  135. }
  136. int
  137. min(int a, int b)
  138. {
  139. print_func_entry();
  140. if(a < b) {
  141. print_func_exit();
  142. return a;
  143. }
  144. print_func_exit();
  145. return b;
  146. }
  147. int
  148. max(int a, int b)
  149. {
  150. print_func_entry();
  151. if(a > b) {
  152. print_func_exit();
  153. return a;
  154. }
  155. print_func_exit();
  156. return b;
  157. }
  158. char*
  159. runetobyte(Rune *r, int n, int *ip)
  160. {
  161. print_func_entry();
  162. char *s;
  163. int m;
  164. s = emalloc(n*UTFmax+1);
  165. m = snprint(s, n*UTFmax+1, "%.*S", n, r);
  166. *ip = m;
  167. print_func_exit();
  168. return s;
  169. }