db_hangman.c (161c1caf) db_hangman.c (56c88695)
1/* $OpenBSD: db_hangman.c,v 1.15 2001/02/08 23:06:32 niklas Exp $ */
1/* $OpenBSD: db_hangman.c,v 1.16 2001/02/10 10:42:35 niklas Exp $ */
2
3/*
4 * Copyright (c) 1996 Theo de Raadt, Michael Shalayeff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 24 unchanged lines hidden (view full) ---

34#include <sys/param.h>
35
36#include <vm/vm.h>
37
38#include <machine/db_machdep.h>
39
40#include <ddb/db_sym.h>
41#include <ddb/db_extern.h>
2
3/*
4 * Copyright (c) 1996 Theo de Raadt, Michael Shalayeff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:

--- 24 unchanged lines hidden (view full) ---

34#include <sys/param.h>
35
36#include <vm/vm.h>
37
38#include <machine/db_machdep.h>
39
40#include <ddb/db_sym.h>
41#include <ddb/db_extern.h>
42#include <ddb/db_output.h>
42
43#include <dev/cons.h>
44#include <dev/rndvar.h>
45
46#define TOLOWER(c) (('A'<=(c)&&(c)<='Z')?(c)-'A'+'a':(c))
47#define ISALPHA(c) (('a'<=(c)&&(c)<='z')||('A'<=(c)&&(c)<='Z'))
48
49/*
50 * if [ `size db_hangman.o | awk 'BEGIN {getline} {print $$1+$$2}'` -gt 1024 ];
51 * then
52 * echo 'hangman is too big!!!'
53 * fi
54 *
55 */
56
57static __inline size_t db_random __P((size_t));
58static __inline char *db_randomsym __P((size_t *));
59void db_hang __P((int, char *, char *));
43
44#include <dev/cons.h>
45#include <dev/rndvar.h>
46
47#define TOLOWER(c) (('A'<=(c)&&(c)<='Z')?(c)-'A'+'a':(c))
48#define ISALPHA(c) (('a'<=(c)&&(c)<='z')||('A'<=(c)&&(c)<='Z'))
49
50/*
51 * if [ `size db_hangman.o | awk 'BEGIN {getline} {print $$1+$$2}'` -gt 1024 ];
52 * then
53 * echo 'hangman is too big!!!'
54 * fi
55 *
56 */
57
58static __inline size_t db_random __P((size_t));
59static __inline char *db_randomsym __P((size_t *));
60void db_hang __P((int, char *, char *));
60int db_hangon __P((void));
61static __inline int db_hangon __P((void));
61
62static int skill;
63
64static __inline size_t
65db_random(mod)
62
63static int skill;
64
65static __inline size_t
66db_random(mod)
66 register size_t mod;
67 size_t mod;
67{
68 return arc4random() % mod;
69}
70
68{
69 return arc4random() % mod;
70}
71
72struct db_hang_forall_arg {
73 int cnt;
74 db_sym_t sym;
75};
76
77/*
78 * Horrible abuse of the forall function, but we're not in a hurry.
79 */
80static void db_hang_forall __P((db_symtab_t *, db_sym_t, char *, char *, int,
81 void *));
82
83static void
84db_hang_forall(stab, sym, name, suff, pre, varg)
85 db_symtab_t *stab;
86 db_sym_t sym;
87 char *name;
88 char *suff;
89 int pre;
90 void *varg;
91{
92 struct db_hang_forall_arg *arg = (struct db_hang_forall_arg *)varg;
93
94 if (--arg->cnt == 0)
95 arg->sym = sym;
96}
97
71static __inline char *
72db_randomsym(lenp)
73 size_t *lenp;
74{
98static __inline char *
99db_randomsym(lenp)
100 size_t *lenp;
101{
75 register char *p, *q;
76 /* choose random symtab */
77 register db_symtab_t stab = db_istab(db_random(db_nsymtabs));
102 extern db_symtab_t db_symtabs[];
103 db_symtab_t *stab;
104 int nsymtabs, nsyms;
105 char *p, *q;
106 struct db_hang_forall_arg dfa;
78
107
79 /* choose random symbol from the table */
80 q = db_qualify(X_db_isym(stab, db_random(X_db_nsyms(stab))),stab->name);
108 for (nsymtabs = 0; db_symtabs[nsymtabs].name != NULL; nsymtabs++)
109 ;
81
110
82 /* don't show symtab name if there are less than 3 of 'em */
83 if (db_nsymtabs < 3)
111 if (nsymtabs == 0)
112 return (NULL);
113
114 stab = &db_symtabs[db_random(nsymtabs)];
115
116 dfa.cnt = 1000000;
117 X_db_forall(stab, db_hang_forall, &dfa);
118 if (dfa.cnt <= 0)
119 return (NULL);
120 nsyms = 1000000 - dfa.cnt;
121
122 if (nsyms == 0)
123 return (NULL);
124
125 dfa.cnt = db_random(nsyms);
126 X_db_forall(stab, db_hang_forall, &dfa);
127
128 q = db_qualify(dfa.sym, stab->name);
129
130 /* don't show symtab name if there are less than 3 of 'em */
131 if (nsymtabs < 3)
84 while(*q++ != ':');
85
132 while(*q++ != ':');
133
86 /* strlen(q) && ignoring underscores and colons */
134 /* strlen(q) && ignoring underscores and colons */
87 for ((*lenp) = 0, p = q; *p; p++)
88 if (ISALPHA(*p))
89 (*lenp)++;
90
135 for ((*lenp) = 0, p = q; *p; p++)
136 if (ISALPHA(*p))
137 (*lenp)++;
138
91 return q;
139 return (q);
92}
93
94static char hangpic[]=
140}
141
142static char hangpic[]=
95 "\n88888 \r\n"
96 "9 7 6 \r\n"
97 "97 5 \r\n"
98 "9 423\r\n"
99 "9 2 \r\n"
100 "9 1 0\r\n"
101 "9\r\n"
102 "9 ";
143 "\n88888 \r\n"
144 "9 7 6 \r\n"
145 "97 5 \r\n"
146 "9 423\r\n"
147 "9 2 \r\n"
148 "9 1 0\r\n"
149 "9\r\n"
150 "9 ";
103static char substchar[]="\\/|\\/O|/-|";
104
105void
106db_hang(tries, word, abc)
107 int tries;
108 register char *word;
109 register char *abc;
110{
111 register char *p;
112
151static char substchar[]="\\/|\\/O|/-|";
152
153void
154db_hang(tries, word, abc)
155 int tries;
156 register char *word;
157 register char *abc;
158{
159 register char *p;
160
113 for(p=hangpic; *p; p++) {
114 if(*p>='0' && *p<='9') {
115 if(tries<=(*p)-'0')
116 cnputc(substchar[(*p)-'0']);
117 else
118 cnputc(' ');
119 } else
120 cnputc(*p);
121 }
161 for(p=hangpic; *p; p++)
162 cnputc((*p>='0' && *p<='9') ? ((tries<=(*p)-'0') ?
163 substchar[(*p)-'0'] : ' ') : *p);
122
123 for (p = word; *p; p++)
164
165 for (p = word; *p; p++)
124 if (ISALPHA(*p) && abc[TOLOWER(*p) - 'a'] == '-')
125 cnputc('-');
126 else
127 cnputc(*p);
166 cnputc(ISALPHA(*p) && abc[TOLOWER(*p) - 'a'] == '-'?'-':*p);
128
167
129 cnputc(' ');
130 cnputc('(');
168 db_printf(" (");
131
132 for (p = abc; *p; p++)
133 if (*p == '_')
134 cnputc('a' + (p - abc));
135
169
170 for (p = abc; *p; p++)
171 if (*p == '_')
172 cnputc('a' + (p - abc));
173
136 cnputc(')');
137 cnputc('\r');
174 db_printf(")\r");
138}
139
140
175}
176
177
141int
178static __inline int
142db_hangon(void)
143{
144 static size_t len;
145 static size_t tries;
146 static char *word = NULL;
147 static char abc[26+1]; /* for '\0' */
148
149 if (word == NULL) {
150 register char *p;
151
152 for (p = abc; p < &abc[sizeof(abc)-1]; p++)
153 *p = '-';
154 *p = '\0';
155
179db_hangon(void)
180{
181 static size_t len;
182 static size_t tries;
183 static char *word = NULL;
184 static char abc[26+1]; /* for '\0' */
185
186 if (word == NULL) {
187 register char *p;
188
189 for (p = abc; p < &abc[sizeof(abc)-1]; p++)
190 *p = '-';
191 *p = '\0';
192
156 tries = 2 * (1 + skill / 3);
193 tries = skill + 1;
157 word = db_randomsym(&len);
194 word = db_randomsym(&len);
195 if (word == NULL)
196 return (0);
158 }
159
160 {
161 register char c;
162
163 db_hang(tries, word, abc);
164 c = cngetc();
165 c = TOLOWER(c);

--- 13 unchanged lines hidden (view full) ---

179 } else {
180 abc[c - 'a'] = '_';
181 tries--;
182 }
183 }
184 }
185
186 if (tries && len)
197 }
198
199 {
200 register char c;
201
202 db_hang(tries, word, abc);
203 c = cngetc();
204 c = TOLOWER(c);

--- 13 unchanged lines hidden (view full) ---

218 } else {
219 abc[c - 'a'] = '_';
220 tries--;
221 }
222 }
223 }
224
225 if (tries && len)
187 return 1;
226 return (1);
188
189 if (!tries && skill > 2) {
190 register char *p = word;
191 for (; *p; p++)
192 if (ISALPHA(*p))
193 abc[TOLOWER(*p) - 'a'] = *p;
194 }
195 db_hang(tries, word, abc);
196 cnputc('\n');
197 word = NULL;
198
227
228 if (!tries && skill > 2) {
229 register char *p = word;
230 for (; *p; p++)
231 if (ISALPHA(*p))
232 abc[TOLOWER(*p) - 'a'] = *p;
233 }
234 db_hang(tries, word, abc);
235 cnputc('\n');
236 word = NULL;
237
199 return !tries;
238 return (!tries);
200}
201
202void
203db_hangman(addr, haddr, count, modif)
204 db_expr_t addr;
205 int haddr;
206 db_expr_t count;
207 char *modif;
208{
209 if (modif[0] == 's' && '0' <= modif[1] && modif[1] <= '9')
210 skill = modif[1] - '0';
211 else
239}
240
241void
242db_hangman(addr, haddr, count, modif)
243 db_expr_t addr;
244 int haddr;
245 db_expr_t count;
246 char *modif;
247{
248 if (modif[0] == 's' && '0' <= modif[1] && modif[1] <= '9')
249 skill = modif[1] - '0';
250 else
212 skill = 5;
251 skill = 3;
213
214 while (db_hangon());
215}
252
253 while (db_hangon());
254}