db_hangman.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* $OpenBSD: db_hangman.c,v 1.32 2015/03/14 03:38:46 jsg Exp $ */
  2. /*
  3. * Copyright (c) 1996 Theo de Raadt, Michael Shalayeff
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #include <sys/param.h>
  28. #include <sys/systm.h>
  29. #include <machine/db_machdep.h>
  30. #include <ddb/db_sym.h>
  31. #include <ddb/db_output.h>
  32. #include <dev/cons.h>
  33. #define ABC_ISCLR(c) sabc->abc[(c)-'a']==0
  34. #define ABC_ISWRONG(c) sabc->abc[(c)-'a']=='_'
  35. #define ABC_SETWRONG(c) (sabc->abc[(c)-'a']='_')
  36. #define ABC_SETRIGHT(c) (sabc->abc[(c)-'a']='+')
  37. #define ABC_CLR() memset(sabc->abc,0,sizeof sabc->abc)
  38. struct _abc {
  39. char abc[26+2]; /* for int32 alignment */
  40. };
  41. #define TOLOWER(c) ((c)|0x20)
  42. #define ISLOWALPHA(c) ('a'<=(c) && (c)<='z')
  43. #define ISALPHA(c) ISLOWALPHA(TOLOWER(c))
  44. void db_hang(int, char *, struct _abc *);
  45. u_long db_plays, db_guesses;
  46. static const char hangpic[]=
  47. "\n88888\r\n"
  48. "9 7 6\r\n"
  49. "97 5\r\n"
  50. "9 423\r\n"
  51. "9 2\r\n"
  52. "9 1 0\r\n"
  53. "9\r\n"
  54. "9 ";
  55. static const char substchar[]="\\/|\\/O|/-|";
  56. struct db_hang_forall_arg {
  57. int cnt;
  58. db_sym_t sym;
  59. };
  60. /*
  61. * Horrible abuse of the forall function, but we're not in a hurry.
  62. */
  63. static void db_hang_forall(db_symtab_t *, db_sym_t, char *, char *, int,
  64. void *);
  65. static void
  66. db_hang_forall(db_symtab_t *stab, db_sym_t sym, char *name, char *suff, int pre,
  67. void *varg)
  68. {
  69. struct db_hang_forall_arg *arg = (struct db_hang_forall_arg *)varg;
  70. if (arg->cnt-- == 0)
  71. arg->sym = sym;
  72. }
  73. static __inline char *
  74. db_randomsym(size_t *lenp)
  75. {
  76. extern db_symtab_t db_symtabs[];
  77. db_symtab_t *stab;
  78. int nsymtabs, nsyms;
  79. char *p, *q;
  80. struct db_hang_forall_arg dfa;
  81. for (nsymtabs = 0; db_symtabs[nsymtabs].name != NULL; nsymtabs++)
  82. ;
  83. if (nsymtabs == 0)
  84. return (NULL);
  85. stab = &db_symtabs[arc4random_uniform(nsymtabs)];
  86. dfa.cnt = 0;
  87. X_db_forall(stab, db_hang_forall, &dfa);
  88. nsyms = -dfa.cnt;
  89. if (nsyms == 0)
  90. return (NULL);
  91. dfa.cnt = arc4random_uniform(nsyms);
  92. X_db_forall(stab, db_hang_forall, &dfa);
  93. q = db_qualify(dfa.sym, stab->name);
  94. /* don't show symtab name if there are less than 3 of 'em */
  95. if (nsymtabs < 3)
  96. while (*q++ != ':');
  97. /* strlen(q) && ignoring underscores and colons */
  98. for ((*lenp) = 0, p = q; *p; p++)
  99. if (ISALPHA(*p))
  100. (*lenp)++;
  101. return (q);
  102. }
  103. void
  104. db_hang(int tries, char *word, struct _abc *sabc)
  105. {
  106. const char *p;
  107. int i;
  108. int c;
  109. #ifdef ABC_BITMASK
  110. int m;
  111. #endif
  112. for (p = hangpic; *p; p++)
  113. cnputc((*p >= '0' && *p <= '9') ? ((tries <= (*p) - '0') ?
  114. substchar[(*p) - '0'] : ' ') : *p);
  115. for (p = word; *p; p++) {
  116. c = TOLOWER(*p);
  117. cnputc(ISLOWALPHA(c) && ABC_ISCLR(c) ? '-' : *p);
  118. }
  119. #ifdef ABC_WRONGSTR
  120. db_printf(" (%s)\r", ABC_WRONGSTR);
  121. #else
  122. db_printf(" (");
  123. #ifdef ABC_BITMASK
  124. m = sabc->wrong;
  125. for (i = 'a'; i <= 'z'; ++i, m >>= 1)
  126. if (m&1)
  127. cnputc(i);
  128. #else
  129. for (i = 'a'; i <= 'z'; ++i)
  130. if (ABC_ISWRONG(i))
  131. cnputc(i);
  132. #endif
  133. db_printf(")\r");
  134. #endif
  135. }
  136. void
  137. db_hangman(db_expr_t addr, int haddr, db_expr_t count, char *modif)
  138. {
  139. char *word;
  140. size_t tries;
  141. size_t len;
  142. struct _abc sabc[1];
  143. int skill;
  144. if (modif[0] != 's' || (skill = modif[1] - '0') > 9U)
  145. skill = 3;
  146. word = NULL;
  147. tries = 0;
  148. for (;;) {
  149. if (word == NULL) {
  150. ABC_CLR();
  151. tries = skill + 1;
  152. word = db_randomsym(&len);
  153. if (word == NULL)
  154. break;
  155. db_plays++;
  156. }
  157. {
  158. int c;
  159. db_hang(tries, word, sabc);
  160. c = cngetc();
  161. c = TOLOWER(c);
  162. if (ISLOWALPHA(c) && ABC_ISCLR(c)) {
  163. char *p;
  164. size_t n;
  165. /* strchr(word,c) */
  166. for (n = 0, p = word; *p ; p++)
  167. if (TOLOWER(*p) == c)
  168. n++;
  169. if (n) {
  170. ABC_SETRIGHT(c);
  171. len -= n;
  172. } else {
  173. ABC_SETWRONG(c);
  174. tries--;
  175. }
  176. }
  177. }
  178. if (tries && len)
  179. continue;
  180. if (!tries && skill > 2) {
  181. char *p = word;
  182. for (; *p; p++)
  183. if (ISALPHA(*p))
  184. ABC_SETRIGHT(TOLOWER(*p));
  185. }
  186. if (tries)
  187. db_guesses++;
  188. db_hang(tries, word, sabc);
  189. db_printf("\nScore: %lu/%lu\n", db_plays, db_guesses);
  190. word = NULL;
  191. if (tries)
  192. break;
  193. }
  194. }