PKUOS - Pintos
Pintos source browser for PKU Operating System course
insult.c
Go to the documentation of this file.
1/** Insult.c
2
3 This is a version of the famous CS 107 random sentence
4 generator. I wrote a program that reads a grammar definition
5 file and writes a C file containing that grammar as hard code
6 static C strings. Thus the majority of the code below in
7 machine generated and totally unreadable. The arrays created
8 are specially designed to make generating the sentences as
9 easy as possible.
10
11 Originally by Greg Hutchins, March 1998.
12 Modified by Ben Pfaff for Pintos, Sept 2004. */
13char *start[] =
14 { "You", "1", "5", ".", "May", "13", ".", "With", "the", "19", "of", "18",
15",", "may", "13", "."
16};
17char startLoc[] = { 3, 0, 4, 7, 16 };
18char *adj[] = { "3", "4", "2", ",", "1" };
19char adjLoc[] = { 3, 0, 1, 2, 5 };
20char *adj3[] = { "3", "4" };
21char adj3Loc[] = { 2, 0, 1, 2 };
22char *adj1[] =
23 { "lame", "dried", "up", "par-broiled", "bloated", "half-baked", "spiteful",
24"egotistical", "ungrateful", "stupid", "moronic", "fat", "ugly", "puny", "pitiful",
25"insignificant", "blithering", "repulsive", "worthless", "blundering", "retarded",
26"useless", "obnoxious", "low-budget", "assinine", "neurotic", "subhuman", "crochety",
27"indescribable", "contemptible", "unspeakable", "sick", "lazy", "good-for-nothing",
28"slutty", "mentally-deficient", "creepy", "sloppy", "dismal", "pompous", "pathetic",
29"friendless", "revolting", "slovenly", "cantankerous", "uncultured", "insufferable",
30"gross", "unkempt", "defective", "crumby"
31};
32char adj1Loc[] =
33 { 50, 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
3421, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
3543, 44, 45, 46, 47, 48, 49, 50, 51 };
36char *adj2[] =
37 { "putrefied", "festering", "funky", "moldy", "leprous", "curdled", "fetid",
38"slimy", "crusty", "sweaty", "damp", "deranged", "smelly", "stenchy", "malignant",
39"noxious", "grimy", "reeky", "nasty", "mutilated", "sloppy", "gruesome", "grisly",
40"sloshy", "wormy", "mealy", "spoiled", "contaminated", "rancid", "musty",
41"fly-covered", "moth-eaten", "decaying", "decomposed", "freeze-dried", "defective",
42"petrified", "rotting", "scabrous", "hirsute"
43};
44char adj2Loc[] =
45 { 40, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
4620, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
47char *name[] =
48 { "10", ",", "bad", "excuse", "for", "6", ",", "6", "for", "brains", ",",
49"4", "11", "8", "for", "brains", "offspring", "of", "a", "motherless", "10", "7", "6",
50"7", "4", "11", "8"
51};
52char nameLoc[] = { 7, 0, 1, 6, 10, 16, 21, 23, 27 };
53char *stuff[] =
54 { "shit", "toe", "jam", "filth", "puss", "earwax", "leaf", "clippings",
55"bat", "guano", "mucus", "fungus", "mung", "refuse", "earwax", "spittoon", "spittle",
56"phlegm"
57};
58char stuffLoc[] = { 14, 0, 1, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 17, 18 };
60 { "bit", "of", "piece", "of", "vat", "of", "lump", "of", "crock", "of",
61"ball", "of", "tub", "of", "load", "of", "bucket", "of", "mound", "of", "glob", "of", "bag",
62"of", "heap", "of", "mountain", "of", "load", "of", "barrel", "of", "sack", "of", "blob", "of",
63"pile", "of", "truckload", "of", "vat", "of"
64};
66 { 21, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36,
6738, 40, 42 };
68char *organics[] =
69 { "droppings", "mung", "zits", "puckies", "tumors", "cysts", "tumors",
70"livers", "froth", "parts", "scabs", "guts", "entrails", "blubber", "carcuses", "gizards",
71"9"
72};
74 { 17, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };
75char *body_parts[] =
76 { "kidneys", "genitals", "buttocks", "earlobes", "innards", "feet"
77};
78char body_partsLoc[] = { 6, 0, 1, 2, 3, 4, 5, 6 };
79char *noun[] =
80 { "pop", "tart", "warthog", "twinkie", "barnacle", "fondue", "pot",
81"cretin", "fuckwad", "moron", "ass", "neanderthal", "nincompoop", "simpleton", "11"
82};
83char nounLoc[] = { 13, 0, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
84char *animal[] =
85 { "donkey", "llama", "dingo", "lizard", "gekko", "lemur", "moose", "camel",
86"goat", "eel"
87};
88char animalLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
89char *good_verb[] =
90 { "love", "cuddle", "fondle", "adore", "smooch", "hug", "caress", "worship",
91"look", "at", "touch"
92};
93char good_verbLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11 };
94char *curse[] =
95 { "14", "20", "23", "14", "17", "20", "23", "14", "find", "your", "9",
96"suddenly", "delectable", "14", "and", "14", "seek", "a", "battleground", "23"
97};
98char curseLoc[] = { 4, 0, 3, 7, 13, 20 };
99char *afflictors[] =
100 { "15", "21", "15", "21", "15", "21", "15", "21", "a", "22", "Rush",
101"Limbaugh", "the", "hosts", "of", "Hades"
102};
103char afflictorsLoc[] = { 6, 0, 2, 4, 6, 8, 12, 16 };
104char *quantity[] =
105 { "a", "4", "hoard", "of", "a", "4", "pack", "of", "a", "truckload", "of",
106"a", "swarm", "of", "many", "an", "army", "of", "a", "4", "heard", "of", "a", "4",
107"platoon", "of", "a", "4", "and", "4", "group", "of", "16"
108};
109char quantityLoc[] = { 10, 0, 4, 8, 11, 14, 15, 18, 22, 26, 32, 33 };
110char *numbers[] =
111 { "a", "thousand", "three", "million", "ninty-nine", "nine-hundred,",
112"ninty-nine", "forty-two", "a", "gazillion", "sixty-eight", "times", "thirty-three"
113};
114char numbersLoc[] = { 7, 0, 2, 4, 5, 7, 8, 10, 13 };
115char *adv[] =
116 { "viciously", "manicly", "merrily", "happily", ",", "with", "the", "19",
117"of", "18", ",", "gleefully", ",", "with", "much", "ritualistic", "celebration", ",",
118"franticly"
119};
120char advLoc[] = { 8, 0, 1, 2, 3, 4, 11, 12, 18, 19 };
121char *metaphor[] =
122 { "an", "irate", "manticore", "Thor's", "belch", "Alah's", "fist", "16",
123"titans", "a", "particularly", "vicious", "she-bear", "in", "the", "midst", "of", "her",
124"menstrual", "cycle", "a", "pissed-off", "Jabberwock"
125};
126char metaphorLoc[] = { 6, 0, 3, 5, 7, 9, 20, 23 };
127char *force[] = { "force", "fury", "power", "rage" };
128char forceLoc[] = { 4, 0, 1, 2, 3, 4 };
129char *bad_action[] =
130 { "spit", "shimmy", "slobber", "find", "refuge", "find", "shelter", "dance",
131"retch", "vomit", "defecate", "erect", "a", "strip", "mall", "build", "a", "26", "have", "a",
132"religious", "experience", "discharge", "bodily", "waste", "fart", "dance", "drool",
133"lambada", "spill", "16", "rusty", "tacks", "bite", "you", "sneeze", "sing", "16",
134"campfire", "songs", "smite", "you", "16", "times", "construct", "a", "new", "home", "throw",
135"a", "party", "procreate"
136};
138 { 25, 0, 1, 2, 3, 5, 7, 8, 9, 10, 11, 15, 18, 22, 25, 26, 27, 28, 29, 33,
13935, 36, 40, 44, 48, 51, 52 };
140char *beasties[] =
141 { "yaks", "22", "maggots", "22", "cockroaches", "stinging", "scorpions",
142"fleas", "22", "weasels", "22", "gnats", "South", "American", "killer", "bees", "spiders",
143"4", "monkeys", "22", "wiener-dogs", "22", "rats", "22", "wolverines", "4", ",", "22",
144"pit-fiends"
145};
147 { 14, 0, 1, 3, 5, 7, 8, 10, 12, 16, 17, 19, 21, 23, 25, 29 };
148char *condition[] =
149 { "frothing", "manic", "crazed", "plague-ridden", "disease-carrying",
150"biting", "rabid", "blood-thirsty", "ravaging", "slavering"
151};
152char conditionLoc[] = { 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
153char *place[] =
154 { "in", "24", "25", "upon", "your", "mother's", "grave", "on", "24", "best",
155"rug", "in", "the", "26", "you", "call", "home", "upon", "your", "heinie"
156};
157char placeLoc[] = { 5, 0, 3, 7, 11, 17, 20 };
158char *relation[] =
159 { "your", "your", "your", "your", "father's", "your", "mother's", "your",
160"grandma's"
161};
162char relationLoc[] = { 6, 0, 1, 2, 3, 5, 7, 9 };
164 { "entrails", "anal", "cavity", "shoes", "house", "pantry", "general",
165"direction", "pants", "bed"
166};
167char in_somethingLoc[] = { 8, 0, 1, 3, 4, 5, 6, 8, 9, 10 };
168char *bad_place[] =
169 { "rat", "hole", "sewer", "toxic", "dump", "oil", "refinery", "landfill",
170"porto-pottie"
171};
172char bad_placeLoc[] = { 6, 0, 2, 3, 5, 7, 8, 9 };
173char **daGrammar[27];
174char *daGLoc[27];
175
176static void
178{
179 daGrammar[0] = start;
180 daGLoc[0] = startLoc;
181 daGrammar[1] = adj;
182 daGLoc[1] = adjLoc;
183 daGrammar[2] = adj3;
184 daGLoc[2] = adj3Loc;
185 daGrammar[3] = adj1;
186 daGLoc[3] = adj1Loc;
187 daGrammar[4] = adj2;
188 daGLoc[4] = adj2Loc;
189 daGrammar[5] = name;
190 daGLoc[5] = nameLoc;
191 daGrammar[6] = stuff;
192 daGLoc[6] = stuffLoc;
195 daGrammar[8] = organics;
196 daGLoc[8] = organicsLoc;
199 daGrammar[10] = noun;
200 daGLoc[10] = nounLoc;
201 daGrammar[11] = animal;
202 daGLoc[11] = animalLoc;
203 daGrammar[12] = good_verb;
204 daGLoc[12] = good_verbLoc;
205 daGrammar[13] = curse;
206 daGLoc[13] = curseLoc;
207 daGrammar[14] = afflictors;
208 daGLoc[14] = afflictorsLoc;
209 daGrammar[15] = quantity;
210 daGLoc[15] = quantityLoc;
211 daGrammar[16] = numbers;
212 daGLoc[16] = numbersLoc;
213 daGrammar[17] = adv;
214 daGLoc[17] = advLoc;
215 daGrammar[18] = metaphor;
216 daGLoc[18] = metaphorLoc;
217 daGrammar[19] = force;
218 daGLoc[19] = forceLoc;
219 daGrammar[20] = bad_action;
220 daGLoc[20] = bad_actionLoc;
221 daGrammar[21] = beasties;
222 daGLoc[21] = beastiesLoc;
223 daGrammar[22] = condition;
224 daGLoc[22] = conditionLoc;
225 daGrammar[23] = place;
226 daGLoc[23] = placeLoc;
227 daGrammar[24] = relation;
228 daGLoc[24] = relationLoc;
231 daGrammar[26] = bad_place;
232 daGLoc[26] = bad_placeLoc;
233}
234
235#include <ctype.h>
236#include <debug.h>
237#include <random.h>
238#include <stdio.h>
239#include <stdlib.h>
240#include <string.h>
241#include <syscall.h>
242
243void expand (int num, char **grammar[], char *location[], int handle);
244
245static void
246usage (int ret_code, const char *message, ...) PRINTF_FORMAT (2, 3);
247
248static void
249usage (int ret_code, const char *message, ...)
250{
251 va_list args;
252
253 if (message != NULL)
254 {
255 va_start (args, message);
256 vprintf (message, args);
257 va_end (args);
258 }
259
260 printf ("\n"
261 "Usage: insult [OPTION]...\n"
262 "Prints random insults to screen.\n\n"
263 " -h: this help message\n"
264 " -s <integer>: set the random seed (default 4951)\n"
265 " -n <integer>: choose number of insults (default 4)\n"
266 " -f <file>: redirect output to <file>\n");
267
268 exit (ret_code);
269}
270
271int
272main (int argc, char *argv[])
273{
274 int sentence_cnt, new_seed, i, file_flag, sent_flag, seed_flag;
275 int handle;
276
277 new_seed = 4951;
278 sentence_cnt = 4;
279 file_flag = 0;
280 seed_flag = 0;
281 sent_flag = 0;
282 handle = STDOUT_FILENO;
283
284 for (i = 1; i < argc; i++)
285 {
286 if (strcmp (argv[1], "-h") == 0)
287 usage (0, NULL);
288 else if (strcmp (argv[i], "-s") == 0)
289 {
290 if (seed_flag++)
291 usage (-1, "Can't have more than one seed");
292 if (++i >= argc)
293 usage (-1, "Missing value for -s");
294 new_seed = atoi (argv[i]);
295 }
296 else if (strcmp (argv[i], "-n") == 0)
297 {
298 if (sent_flag++)
299 usage (-1, "Can't have more than one sentence option");
300 if (++i >= argc)
301 usage (-1, "Missing value for -n");
302 sentence_cnt = atoi (argv[i]);
303 if (sentence_cnt < 1)
304 usage (-1, "Must have at least one sentence");
305 }
306 else if (strcmp (argv[i], "-f") == 0)
307 {
308 if (file_flag++)
309 usage (-1, "Can't have more than one output file");
310 if (++i >= argc)
311 usage (-1, "Missing value for -f");
312
313 /* Because files have fixed length in the basic Pintos
314 file system, the 0 argument means that this option
315 will not be useful until project 4 is
316 implemented. */
317 create (argv[i], 0);
318 handle = open (argv[i]);
319 if (handle < 0)
320 {
321 printf ("%s: open failed\n", argv[i]);
322 return EXIT_FAILURE;
323 }
324 }
325 else
326 usage (-1, "Unrecognized flag");
327 }
328
329 init_grammar ();
330
331 random_init (new_seed);
332 hprintf (handle, "\n");
333
334 for (i = 0; i < sentence_cnt; i++)
335 {
336 hprintf (handle, "\n");
337 expand (0, daGrammar, daGLoc, handle);
338 hprintf (handle, "\n\n");
339 }
340
341 if (file_flag)
342 close (handle);
343
344 return EXIT_SUCCESS;
345}
346
347void
348expand (int num, char **grammar[], char *location[], int handle)
349{
350 char *word;
351 int i, which, listStart, listEnd;
352
353 which = random_ulong () % location[num][0] + 1;
354 listStart = location[num][which];
355 listEnd = location[num][which + 1];
356 for (i = listStart; i < listEnd; i++)
357 {
358 word = grammar[num][i];
359 if (!isdigit (*word))
360 {
361 if (!ispunct (*word))
362 hprintf (handle, " ");
363 hprintf (handle, "%s", word);
364 }
365 else
366 expand (atoi (word), grammar, location, handle);
367 }
368
369}
static int ispunct(int c)
Definition: ctype.h:21
static int isdigit(int c)
Definition: ctype.h:7
#define PRINTF_FORMAT(FMT, FIRST)
Definition: debug.h:10
char ** daGrammar[27]
Definition: insult.c:173
char * adj[]
Definition: insult.c:18
char conditionLoc[]
Definition: insult.c:152
char * noun_and_prep[]
Definition: insult.c:59
int main(int argc, char *argv[])
Definition: insult.c:272
char * animal[]
Definition: insult.c:84
char noun_and_prepLoc[]
Definition: insult.c:65
char adj1Loc[]
Definition: insult.c:32
char stuffLoc[]
Definition: insult.c:58
char good_verbLoc[]
Definition: insult.c:93
char startLoc[]
Definition: insult.c:17
char * organics[]
Definition: insult.c:68
char * start[]
Insult.c.
Definition: insult.c:13
char advLoc[]
Definition: insult.c:120
char * adj2[]
Definition: insult.c:36
char bad_actionLoc[]
Definition: insult.c:137
char * numbers[]
Definition: insult.c:110
char * name[]
Definition: insult.c:47
char * good_verb[]
Definition: insult.c:89
char animalLoc[]
Definition: insult.c:88
void expand(int num, char **grammar[], char *location[], int handle)
Definition: insult.c:348
char forceLoc[]
Definition: insult.c:128
char * in_something[]
Definition: insult.c:163
static void usage(int ret_code, const char *message,...) PRINTF_FORMAT(2
Definition: insult.c:249
char * place[]
Definition: insult.c:153
char in_somethingLoc[]
Definition: insult.c:167
char * beasties[]
Definition: insult.c:140
char * afflictors[]
Definition: insult.c:99
char numbersLoc[]
Definition: insult.c:114
char * metaphor[]
Definition: insult.c:121
char relationLoc[]
Definition: insult.c:162
char metaphorLoc[]
Definition: insult.c:126
char nameLoc[]
Definition: insult.c:52
char bad_placeLoc[]
Definition: insult.c:172
char * stuff[]
Definition: insult.c:53
static void init_grammar(void)
Definition: insult.c:177
char placeLoc[]
Definition: insult.c:157
char * condition[]
Definition: insult.c:148
char adj2Loc[]
Definition: insult.c:44
char quantityLoc[]
Definition: insult.c:109
char * adj1[]
Definition: insult.c:22
char beastiesLoc[]
Definition: insult.c:146
char adj3Loc[]
Definition: insult.c:21
char organicsLoc[]
Definition: insult.c:73
char curseLoc[]
Definition: insult.c:98
char body_partsLoc[]
Definition: insult.c:78
char * noun[]
Definition: insult.c:79
char nounLoc[]
Definition: insult.c:83
char * adv[]
Definition: insult.c:115
char * curse[]
Definition: insult.c:94
char * bad_action[]
Definition: insult.c:129
char * adj3[]
Definition: insult.c:20
char * relation[]
Definition: insult.c:158
char * force[]
Definition: insult.c:127
char * daGLoc[27]
Definition: insult.c:174
char * quantity[]
Definition: insult.c:104
char * body_parts[]
Definition: insult.c:75
char afflictorsLoc[]
Definition: insult.c:103
char * bad_place[]
Definition: insult.c:168
char adjLoc[]
Definition: insult.c:19
int vprintf(const char *format, va_list args)
The standard vprintf() function, which is like printf() but uses a va_list.
Definition: console.c:126
int printf(const char *format,...)
Writes formatted output to the console.
Definition: stdio.c:79
int atoi(const char *s)
Converts a string representation of a signed decimal integer in S into an ‘int’, which is returned.
Definition: stdlib.c:10
bool create(const char *file, unsigned initial_size)
Definition: syscall.c:91
void exit(int status)
Definition: syscall.c:72
void close(int fd)
Definition: syscall.c:139
int open(const char *file)
Definition: syscall.c:103
#define EXIT_SUCCESS
Typical return values from main() and arguments to exit().
Definition: syscall.h:19
#define EXIT_FAILURE
Unsuccessful execution.
Definition: syscall.h:20
unsigned long random_ulong(void)
Returns a pseudo-random unsigned long.
Definition: random.c:78
void random_init(unsigned seed)
Initializes or reinitializes the PRNG with the given SEED.
Definition: random.c:34
#define va_end(LIST)
Definition: stdarg.h:10
#define va_start(LIST, ARG)
Definition: stdarg.h:9
__builtin_va_list va_list
GCC has <stdarg.h> functionality as built-ins, so all we need is to use it.
Definition: stdarg.h:7
#define NULL
Definition: stddef.h:4
#define STDOUT_FILENO
Definition: stdio.h:16
int strcmp(const char *a_, const char *b_)
Finds the first differing characters in strings A and B.
Definition: string.c:73
Condition variable.
Definition: synch.h:35
int hprintf(int handle, const char *format,...)
Like printf(), but writes output to the given HANDLE.
Definition: console.c:16