ascii.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <bio.h>
  12. #define MAXBASE 36
  13. void usage(void);
  14. void put(int);
  15. void putn(int, int);
  16. void puttext(char *);
  17. void putnum(char *);
  18. int btoi(char *);
  19. int value(int, int);
  20. int isnum(char *);
  21. char *str[256]={
  22. "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
  23. "bs ", "ht ", "nl ", "vt ", "np ", "cr ", "so ", "si ",
  24. "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
  25. "can", "em ", "sub", "esc", "fs ", "gs ", "rs ", "us ",
  26. "sp ", " ! ", " \" ", " # ", " $ ", " % ", " & ", " ' ",
  27. " ( ", " ) ", " * ", " + ", " , ", " - ", " . ", " / ",
  28. " 0 ", " 1 ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ",
  29. " 8 ", " 9 ", " : ", " ; ", " < ", " = ", " > ", " ? ",
  30. " @ ", " A ", " B ", " C ", " D ", " E ", " F ", " G ",
  31. " H ", " I ", " J ", " K ", " L ", " M ", " N ", " O ",
  32. " P ", " Q ", " R ", " S ", " T ", " U ", " V ", " W ",
  33. " X ", " Y ", " Z ", " [ ", " \\ ", " ] ", " ^ ", " _ ",
  34. " ` ", " a ", " b ", " c ", " d ", " e ", " f ", " g ",
  35. " h ", " i ", " j ", " k ", " l ", " m ", " n ", " o ",
  36. " p ", " q ", " r ", " s ", " t ", " u ", " v ", " w ",
  37. " x ", " y ", " z ", " { ", " | ", " } ", " ~ ", "del",
  38. "x80", "x81", "x82", "x83", "x84", "x85", "x86", "x87",
  39. "x88", "x89", "x8a", "x8b", "x8c", "x8d", "x8e", "x8f",
  40. "x90", "x91", "x92", "x93", "x94", "x95", "x96", "x97",
  41. "x98", "x99", "x9a", "x9b", "x9c", "x9d", "x9e", "x9f",
  42. "xa0", " ¡ ", " ¢ ", " £ ", " ¤ ", " ¥ ", " ¦ ", " § ",
  43. " ¨ ", " © ", " ª ", " « ", " ¬ ", " ­ ", " ® ", " ¯ ",
  44. " ° ", " ± ", " ² ", " ³ ", " ´ ", " µ ", " ¶ ", " · ",
  45. " ¸ ", " ¹ ", " º ", " » ", " ¼ ", " ½ ", " ¾ ", " ¿ ",
  46. " À ", " Á ", " Â ", " Ã ", " Ä ", " Å ", " Æ ", " Ç ",
  47. " È ", " É ", " Ê ", " Ë ", " Ì ", " Í ", " Î ", " Ï ",
  48. " Ð ", " Ñ ", " Ò ", " Ó ", " Ô ", " Õ ", " Ö ", " × ",
  49. " Ø ", " Ù ", " Ú ", " Û ", " Ü ", " Ý ", " Þ ", " ß ",
  50. " à ", " á ", " â ", " ã ", " ä ", " å ", " æ ", " ç ",
  51. " è ", " é ", " ê ", " ë ", " ì ", " í ", " î ", " ï ",
  52. " ð ", " ñ ", " ò ", " ó ", " ô ", " õ ", " ö ", " ÷ ",
  53. " ø ", " ù ", " ú ", " û ", " ü ", " ý ", " þ ", " ÿ "
  54. };
  55. char Ncol[]={
  56. 0,0,7,5,4,4,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  57. };
  58. int nchars=128;
  59. int base=16;
  60. int ncol;
  61. int text=1;
  62. int strip=0;
  63. Biobuf bin;
  64. void
  65. main(int argc, char **argv)
  66. {
  67. int i;
  68. Binit(&bin, 1, OWRITE);
  69. ARGBEGIN{
  70. case '8':
  71. nchars=256; break;
  72. case 'x':
  73. base=16; break;
  74. case 'o':
  75. base=8; break;
  76. case 'd':
  77. base=10; break;
  78. case 'b':
  79. base=strtoul(EARGF(usage()), 0, 0);
  80. if(base<2||base>MAXBASE)
  81. usage();
  82. break;
  83. case 'n':
  84. text=0; break;
  85. case 't':
  86. strip=1;
  87. /* fall through */
  88. case 'c':
  89. text=2; break;
  90. default:
  91. usage();
  92. }ARGEND
  93. ncol=Ncol[base];
  94. if(argc==0){
  95. for(i=0;i<nchars;i++){
  96. put(i);
  97. if((i&7)==7)
  98. Bprint(&bin, "|\n");
  99. }
  100. }else{
  101. if(text==1)
  102. text=isnum(argv[0]);
  103. while(argc--)
  104. if(text)
  105. puttext(*argv++);
  106. else
  107. putnum(*argv++);
  108. }
  109. Bputc(&bin, '\n');
  110. exits(0);
  111. }
  112. void
  113. usage(void)
  114. {
  115. fprint(2, "usage: %s [-8cnt] [-dox | -b n] [text]\n", argv0);
  116. exits("usage");
  117. }
  118. void
  119. put(int i)
  120. {
  121. Bputc(&bin, '|');
  122. putn(i, ncol);
  123. Bprint(&bin, " %s", str[i]);
  124. }
  125. char dig[]="0123456789abcdefghijklmnopqrstuvwxyz";
  126. void
  127. putn(int n, int ndig)
  128. {
  129. if(ndig==0)
  130. return;
  131. putn(n/base, ndig-1);
  132. Bputc(&bin, dig[n%base]);
  133. }
  134. void
  135. puttext(char *s)
  136. {
  137. int n;
  138. n=btoi(s)&0377;
  139. if(strip)
  140. Bputc(&bin, n);
  141. else
  142. Bprint(&bin, "%s\n", str[n]);
  143. }
  144. void
  145. putnum(char *s)
  146. {
  147. while(*s){
  148. putn(*s++&0377, ncol);
  149. Bputc(&bin, '\n');
  150. }
  151. }
  152. int
  153. btoi(char *s)
  154. {
  155. int n;
  156. n=0;
  157. while(*s)
  158. n=n*base+value(*s++, 0);
  159. return(n);
  160. }
  161. int
  162. value(int c, int f)
  163. {
  164. char *s;
  165. for(s=dig; s<dig+base; s++)
  166. if(*s==c)
  167. return(s-dig);
  168. if(f)
  169. return(-1);
  170. fprint(2, "%s: bad input char %c\n", argv0, c);
  171. exits("bad");
  172. return 0; /* to keep ken happy */
  173. }
  174. int
  175. isnum(char *s)
  176. {
  177. while(*s)
  178. if(value(*s++, 1)==-1)
  179. return(0);
  180. return(1);
  181. }