📄 spu.c
字号:
/* spu -- A program to make lots of random C/C++ code. Copyright (C) 1993, 1994, 2000 Free Software Foundation, Inc. Contributed by Cygnus Support. Written by Stan Shebs.This file is part of SPU.SPU is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; see the file COPYING. If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. *//* This is a random program generator. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <time.h>char *version_string = "0.5";/* These values are the builtin defaults, mainly useful for testing purposes, or if the user is uninterested in setting a value. */#define DEFAULT_NUM_FILES 5#define DEFAULT_NUM_HEADER_FILES 1#define DEFAULT_NUM_MACROS 10#define DEFAULT_NUM_LIB_MACROS 30#define DEFAULT_MAX_MACRO_ARGS 5#define DEFAULT_NUM_ENUMS 10#define DEFAULT_NUM_LIB_ENUMS 30#define DEFAULT_NUM_ENUMERATORS 10#define DEFAULT_NUM_STRUCTS 10#define DEFAULT_NUM_LIB_STRUCTS 30#define DEFAULT_NUM_FIELDS 20#define DEFAULT_NUM_CLASSES 10#define DEFAULT_NUM_LIB_CLASSES 30#define DEFAULT_NUM_METHODS 20#define DEFAULT_NUM_FUNCTIONS 100#define DEFAULT_NUM_LIB_FUNCTIONS 300#define DEFAULT_MAX_FUNCTION_ARGS 8#define DEFAULT_FUNCTION_LENGTH 20#define DEFAULT_FUNCTION_DEPTH 3#define DEFAULT_LIB_PERCENT 10/* Generic hash table. */struct hash_entry{ char *val; struct hash_entry *next;};struct hash_table{ struct hash_entry *entries[253]; int numadds;};enum decl_types { d_nothing, d_macro, d_enum, d_struct, d_class, d_function};enum { t_nothing = 0, t_void = 1, t_int = 2, t_short = 3, t_char = 4, t_first_user = 100, t_char_ptr = 1000004};char *typenames[] = { "?", "void", "int", "short", "char" };struct macro_desc{ int id; char *name; int numargs; char **args; int use;};struct enumerator_desc{ char *name;};struct enum_desc{ int id; char *name; int num_enumerators; struct enumerator_desc *enumerators; int use;};struct field_desc{ int type; char *name;};struct struct_desc{ int id; char *name; int numfields; struct field_desc *fields; int use;};/* (should add unions as type of struct) */struct class_desc{ int id; char *name; int numfields; struct field_desc *fields; int nummethods; struct function_desc *methods; int use;};struct type_desc{ char *name;};struct arg_desc{ int type; char *name;};struct function_desc{ int id; char *name; int return_type; int numargs; struct arg_desc *args; struct class_desc *class; int use;};struct file_desc{ char *name;};struct decl_entry { enum decl_types type; union { struct macro_desc *macro_d; struct enum_desc *enum_d; struct struct_desc *struct_d; struct class_desc *class_d; struct function_desc *function_d; } decl; int seq; int order;};struct decl_table { int size; int nextentry; struct decl_entry *entries;};/* Function declarations. */void display_usage (void);int hash_string (char *str);struct hash_entry *get_hash_entry (void);char *add_to_hash_table (char *buf, struct hash_table *table);char *get_from_hash_table (char *buf, struct hash_table *table);void init_xrandom (int seed);int xrandom (int n);int probability (int prob);char *copy_string (char *str);char *xmalloc (int n);char *gen_unique_global_name (char *root, int upcase);void gen_random_global_name (char *root, char *namebuf);char *gen_random_local_name (int n, char **others);void create_macros (void);void create_macro (struct macro_desc *macrodesc);char *gen_new_macro_name (void);void create_enums (void);void create_enum (struct enum_desc *enumdesc);char *gen_random_enumerator_name (void);void create_structs (void);void create_struct (struct struct_desc *structdesc, int lib);char *gen_random_field_name (int n);void create_classes (void);void create_class (struct class_desc *classdesc, int lib);void create_functions (void);void create_function (struct function_desc *fndesc, int lib);void write_header_file (int n);void write_lib_header_file (void);void write_source_file (int n);void write_lib_source_file (void);void write_macro (FILE *fp, struct macro_desc *macrodesc);void write_enum (FILE *fp, struct enum_desc *enumdesc);void write_struct (FILE *fp, struct struct_desc *structdesc);void write_class (FILE *fp, struct class_desc *classdesc);void write_function_decl (FILE *fp, struct function_desc *fndesc);void write_function (FILE *fp, struct function_desc *fndesc);void write_lib_function (FILE *fp, int n);void write_statement (FILE *fp, int depth, int max_depth);void write_expression (FILE *fp, int rslttype, int depth, int max_depth, int exclude_id);void write_description_block (FILE *fp);void write_makefile (void);/* Global variables. *//* The possible languages. */enum languages { knr, c, cpp, objc };enum languages language = c;/* Filename extensions to use with each language type. */char *extensions[] = { "c", "c", "cc", "m" };/* Names for each language. */char *lang_names[] = { "K&R C", "standard C", "standard C++", "Objective-C" };int num_files = DEFAULT_NUM_FILES;int num_header_files = DEFAULT_NUM_HEADER_FILES;char *file_base_name = "file";int num_macros = DEFAULT_NUM_MACROS;int num_lib_macros = DEFAULT_NUM_LIB_MACROS;int num_enums = DEFAULT_NUM_ENUMS;int num_lib_enums = DEFAULT_NUM_LIB_ENUMS;int num_enumerators = DEFAULT_NUM_ENUMERATORS;int num_structs = DEFAULT_NUM_STRUCTS;int num_lib_structs = DEFAULT_NUM_LIB_STRUCTS;int num_fields = DEFAULT_NUM_FIELDS;int num_classes = DEFAULT_NUM_CLASSES;int num_lib_classes = DEFAULT_NUM_LIB_CLASSES;int num_methods = DEFAULT_NUM_METHODS;int num_functions = DEFAULT_NUM_FUNCTIONS;int num_lib_functions = DEFAULT_NUM_LIB_FUNCTIONS;int max_function_args = DEFAULT_MAX_FUNCTION_ARGS;int function_length = DEFAULT_FUNCTION_LENGTH;int function_depth = DEFAULT_FUNCTION_DEPTH;/* Percentage of library constructs that will be referenced. */int lib_percent = DEFAULT_LIB_PERCENT;int randomize_order = 1;int num_functions_per_file;/* The amount of commenting in the source. */int commenting = 0;/* Hash table for globally visible symbols. */struct hash_table *global_hash_table;/* The seed for the random number generator. */int seed = -1;int next_id = 1;/* Space to record info about generated constructs. */struct macro_desc *macros;struct macro_desc *lib_macros;struct enum_desc *enums;struct enum_desc *lib_enums;struct struct_desc *structs;struct struct_desc *lib_structs;struct class_desc *classes;struct class_desc *lib_classes;struct function_desc *functions;struct function_desc *lib_functions;struct decl_table order;struct decl_table lib_order;int num_computer_terms;/* Likely words to appear in names of things. These must never be valid C/C++ keywords, since they may appear by themselves in some contexts. */char *computerese[] = { "add", "all", "alloc", "allocate", "area", "array", "at", "bogus", "buf", "buff", "buffer", "by", "btree", "ch", "chr", "clean", "cleanup", "count", "create", "cull", "data", "del", "delete_", "depth", "desc", "dest", "discard", "dismiss", "dma", "done", "dst", "fill", "find", "fn", "for_", "gc", "go", "goto_", "grok", "gronk", "group", "grovel", "hack", "hacked", "have", "heap", "in", "ind", "index", "ini", "init", "initial", "inside", "lab", "label", "last", "len", "length", "line", "lis", "list", "lose", "make", "mark", "mod", "modify", "more", "name", "nest", "nesting", "new_", "next", "node", "null", "num", "number", "part", "partial", "query", "queue", "ob", "obj", "object", "of", "pc", "pnt", "point", "pop", "pos", "position", "push", "raw", "recalc", "rect", "rectangle", "rel", "relative", "ret", "rslt", "remove", "reset", "rmv", "see", "set", "shape", "stack", "str", "string", "tab", "table", "tbl", "tag", "tree", "undel", "undo", "unmark", "use", "vary", "vec", "vect", "vector", "virt", "virtual_", "win", "wind", "window", "word", "zbuf", NULL};/* Return a word that commonly appears in programs. */char *random_computer_word (void){ if (num_computer_terms == 0) { int i; for (i = 0; computerese[i] != NULL; ++i) ; num_computer_terms = i; } return computerese[xrandom (num_computer_terms)];}intmain (int argc, char **argv){ int i, num; char *arg; /* Parse all the arguments. */ /* (should check on numeric values) */ for (i = 1; i < argc; ++i) { arg = argv[i]; if (strcmp(arg, "--basename") == 0) { file_base_name = copy_string(argv[++i]); } else if (strcmp(arg, "--classes") == 0) { num = strtol (argv[++i], NULL, 10); num_classes = num; } else if (strcmp(arg, "--comments") == 0) { num = strtol (argv[++i], NULL, 10); commenting = num; } else if (strcmp(arg, "--enums") == 0) { num = strtol (argv[++i], NULL, 10); num_enums = num; } else if (strcmp(arg, "--enumerators") == 0) { num = strtol (argv[++i], NULL, 10); num_enumerators = num; } else if (strcmp(arg, "--fields") == 0) { num = strtol (argv[++i], NULL, 10); num_fields = num; } else if (strcmp(arg, "--files") == 0) { num = strtol (argv[++i], NULL, 10); num_files = num; } else if (strcmp(arg, "--functions") == 0) { num = strtol (argv[++i], NULL, 10); num_functions = num; } else if (strcmp(arg, "--function-length") == 0) { num = strtol (argv[++i], NULL, 10); function_length = num; } else if (strcmp(arg, "--function-depth") == 0) { num = strtol (argv[++i], NULL, 10); function_depth = num; } else if (strcmp(arg, "--header-files") == 0) { num = strtol (argv[++i], NULL, 10); num_header_files = num; } else if (strcmp(arg, "--help") == 0) { display_usage (); exit (0); } else if (strcmp(arg, "--language") == 0) { if (strcmp (argv[i+1], "c") == 0) language = c; else if (strcmp (argv[i+1], "c++") == 0) language = cpp; else if (strcmp (argv[i+1], "knr") == 0) language = knr; else if (strcmp (argv[i+1], "objc") == 0) language = objc; ++i; } else if (strcmp(arg, "--lib-classes") == 0) { num = strtol (argv[++i], NULL, 10); num_lib_classes = num; } else if (strcmp(arg, "--lib-enums") == 0) { num = strtol (argv[++i], NULL, 10); num_lib_enums = num; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -