Fix typo.
[beanstalkd.git] / cutgen.c
bloba25e62171b6c0b6bc0bc2c090636dfe27b55d61d
1 /*
2 * This file has been modified from the original CUT distribution.
4 * CUT 2.3-kr1
5 * Copyright (c) 2001-2002 Samuel A. Falvo II, William D. Tanksley
6 * See CUT-LICENSE.TXT for details.
8 * $Log: cutgen.c,v $
9 * Revision 1.4 2003/03/18 05:53:50 sfalvo
10 * ADD: cutgen.c: cut_exit() -- common exit point; returns proper error code
11 * at all times.
13 * FIX: cutgen.c: Factored all instances of exit() to invoke cut_exit()
14 * instead. This fixes the bug #703793.
16 * Revision 1.3 2003/03/13 04:27:54 sfalvo
17 * ADD: LICENSE.TXT -- zlib license
19 * ADD: README cut.h cutgen.c -- Changelog token for CVS
21 * FIX: test/bringup-failure -- reflects new usage for bringups and
22 * teardowns in CUT 2.2.
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
31 #include "util.h"
33 #define DO_NOT_PROCESS "..."
35 #define SEARCH_TOKEN_TEST "__CUT__"
36 #define SEARCH_TOKEN_BRINGUP "__CUT_BRINGUP__"
37 #define SEARCH_TOKEN_TAKEDOWN "__CUT_TAKEDOWN__"
39 #define MAX_SYMBOL_LENGTH 256 /* arbitrary */
40 #define MAX_LINE_LENGTH 1024 /* arbitrary */
42 #define SEARCH_TOKEN_TEST_LENGTH sizeof( SEARCH_TOKEN_TEST )-1
43 #define SEARCH_TOKEN_BRINGUP_LENGTH sizeof( SEARCH_TOKEN_BRINGUP )-1
45 #define terr(fmt, args...) do { fprintf(stderr, "\n"); twarn(fmt, ##args); exit(1); } while (0)
46 #define terrx(fmt, args...) do { fprintf(stderr, "\n"); twarnx(fmt, ##args); exit(1); } while (0)
48 typedef enum TestType {
49 TYPE_TEST = 0,
50 TYPE_BRINGUP = 1,
51 } TestType;
53 typedef struct TestItem {
54 char name[MAX_SYMBOL_LENGTH];
55 struct TestItem *next;
56 } TestItem;
58 typedef struct TestGroup {
59 char name[MAX_SYMBOL_LENGTH];
60 struct TestItem *tests;
61 struct TestGroup *next;
62 } TestGroup;
64 /* globals */
66 TestGroup *test_groups = 0;
67 FILE *outfile;
69 static int g_count, g_ready, g_index; /* Used by filename globbing support for windows */
70 static char **g_wildcards, g_fileName[MAX_LINE_LENGTH];
72 int NameAndTypeInTestList( char *name, TestType type )
74 TestItem *item;
75 TestGroup *group;
77 for (group = test_groups; group; group = group->next) {
78 if (!strcmp(group->name, name) && type == TYPE_BRINGUP) return 1;
79 for (item = group->tests; item; item = item->next) {
80 if (!strcmp(item->name, name)) return 1;
84 return 0;
87 void AppendToTestList( char *name, TestType type )
89 TestGroup *new, *cur;
90 TestItem *newt;
92 if (type == TYPE_BRINGUP) {
93 struct TestGroup *new;
95 new = malloc(sizeof(struct TestGroup));
96 if (!new) terr("malloc");
98 new->next = test_groups;
99 strcpy(new->name, name);
100 test_groups = new;
101 } else {
102 cur = test_groups;
103 if (!cur) terrx("no current test group");
105 newt = malloc(sizeof(struct TestItem));
106 if (!newt) terr("malloc");
108 newt->next = cur->tests;
109 strcpy(newt->name, name);
110 cur->tests = newt;
114 void InsertNameAndTypeIntoTestList( char *name, TestType type )
116 if ( !NameAndTypeInTestList( name, type ) )
117 AppendToTestList( name, type );
120 int CharacterIsDigit(char ch)
122 return ( ( ch >= '0') && ( ch <= '9' ) );
125 int CharacterIsUppercase(char ch)
127 return ( ( ch >= 'A' ) && ( ch <= 'Z' ) );
130 int CharacterIsLowercase(char ch)
132 return ( ( ch >= 'a' ) && ( ch <= 'z' ) );
135 int CharacterIsAlphabetic(char ch)
137 return CharacterIsUppercase(ch) || CharacterIsLowercase(ch) || ( ch == '_' );
140 int CharacterIsAlphanumeric( char ch )
142 return CharacterIsDigit(ch) || CharacterIsAlphabetic(ch);
145 void ProcessGenericFunction( char *line, int position,
146 TestType type, int tokenDisplacement )
148 char name[MAX_SYMBOL_LENGTH] = "";
149 int maxLength = strlen( line ) - 1, offset=0;
150 position = position + tokenDisplacement;
152 while ( CharacterIsAlphanumeric(line[position])
153 && (position<maxLength) && (offset<MAX_SYMBOL_LENGTH) )
155 name[offset++] = line[position++];
156 name[offset] = 0;
159 InsertNameAndTypeIntoTestList( name, type );
162 void ProcessBringupFunction( char *line, int position )
164 ProcessGenericFunction( line, position, TYPE_BRINGUP, SEARCH_TOKEN_BRINGUP_LENGTH );
167 void ProcessTestFunction( char *line, int position )
169 ProcessGenericFunction( line, position, TYPE_TEST, SEARCH_TOKEN_TEST_LENGTH );
173 int OffsetOfSubstring( char *line, char *token )
175 char *inset = strstr(line,token);
177 if ( !inset ) return -1;
178 else return inset - line;
181 void CallIfSubstringFound( char *line, char *token, void (*function)(char*,int) )
183 int index = OffsetOfSubstring( line, token );
184 if ( index != -1 )
185 function( line, index );
188 void ProcessSourceFile( char *filename )
190 FILE *source;
191 char line[MAX_LINE_LENGTH];
193 if( strcmp( filename, DO_NOT_PROCESS ) != 0 )
196 source = fopen(filename,"r");
198 while ( fgets(line,MAX_LINE_LENGTH,source) )
200 CallIfSubstringFound( line, SEARCH_TOKEN_BRINGUP, ProcessBringupFunction );
201 CallIfSubstringFound( line, SEARCH_TOKEN_TEST, ProcessTestFunction );
204 fclose(source);
208 void EmitExternDeclarationFor( char *name, char *prefix )
210 fprintf( outfile, "extern void %s%s( void );\n", prefix, name );
213 void Emit(char *text)
215 fprintf(outfile, "%s\n", text);
218 void BlankLine()
220 Emit( "" );
223 void ListExternalFunctions()
225 TestGroup *cur;
226 TestItem *item;
228 for (cur = test_groups; cur; cur = cur->next) {
229 EmitExternDeclarationFor(cur->name, SEARCH_TOKEN_BRINGUP);
230 EmitExternDeclarationFor(cur->name, SEARCH_TOKEN_TAKEDOWN);
231 for (item = cur->tests; item; item = item->next) {
232 EmitExternDeclarationFor(item->name, SEARCH_TOKEN_TEST);
235 BlankLine();
238 void ListHeaderFiles(void)
240 Emit(
241 "#include <string.h>\n"
242 "#include <stdlib.h>\n"
243 "#include <stdio.h>\n"
244 "#include <stdarg.h>\n"
245 "#include \"cut.h\"\n"
247 BlankLine();
248 BlankLine();
251 void EmitIndented(int indent,char *format, ...)
253 va_list v;
254 /* Print two spaces per level of indentation. */
255 fprintf( outfile, "%*s", indent*2, "" );
257 va_start(v,format);
258 vfprintf( outfile, format, v );
259 va_end(v);
261 fprintf( outfile, "\n" );
264 void EmitUnitTesterBody()
266 TestItem *test;
267 TestGroup *group;
269 Emit( "int main( int argc, char *argv[] )\n{" );
270 Emit( " if ( argc == 1 )" );
271 Emit( " cut_init(argv[0], -1 );" );
272 Emit( " else cut_init(argv[0], atoi( argv[1] ) );" );
273 BlankLine();
275 for (group = test_groups; group; group = group->next) {
276 for (test = group->tests; test; test = test->next) {
277 EmitIndented(1, "cut_run(%s, %s);", group->name, test->name);
281 BlankLine();
282 Emit( " cut_exit();\n" );
283 Emit( " return 0;\n}\n" );
286 void EmitCutCheck()
288 Emit( "/* Automatically generated: DO NOT MODIFY. */" );
289 ListHeaderFiles();
290 BlankLine();
291 ListExternalFunctions();
292 BlankLine();
293 EmitUnitTesterBody();
296 void FileName( char *base )
298 strncpy( g_fileName, base, MAX_LINE_LENGTH );
299 g_fileName[ MAX_LINE_LENGTH - 1 ] = 0;
302 int LoadArgument( void )
304 if ( g_index >= g_count )
305 return 0;
307 FileName( g_wildcards[g_index] );
308 g_index++; /* MUST come after FileName() call; bad code smell */
309 return 1;
312 void StartArguments( int starting, int count, char *array[] )
314 g_index = starting;
315 g_count = count;
316 g_wildcards = array;
318 g_ready = LoadArgument();
321 int NextArgument( void )
323 if( g_ready )
324 g_ready = LoadArgument();
326 return g_ready;
329 char *GetArgument( void )
331 if( g_ready )
332 return g_fileName;
334 return NULL;
337 void EstablishOutputFile( int argc, char *argv[] )
339 int i;
341 i = 0;
342 while( i < argc )
344 if( ( argv[i+1] != NULL ) && ( strcmp( argv[i], "-o" ) == 0 ) )
346 outfile = fopen( argv[i+1], "wb+" );
347 if( outfile == NULL )
348 fprintf( stderr, "ERROR: Can't open %s for writing.\n", argv[i+1] );
350 argv[i] = argv[i+1] = DO_NOT_PROCESS;
352 return;
355 i++;
358 outfile = stdout;
361 int main( int argc,char *argv[] )
363 char *filename;
365 if ( argc < 2 )
367 fprintf(
368 stderr,
369 "USAGE:\n"
370 " %s [options] <input file> [<input file> [...]]\n"
371 "\n"
372 "OPTIONS:\n"
373 " -o filename Specifies output file.\n"
374 "\n"
375 "NOTES:\n"
376 " If -o is left unspecified, output defaults to stdout.\n",
377 argv[0]
379 return 3;
382 EstablishOutputFile( argc, argv );
384 /* Skip the executable's name and the output filename. */
385 StartArguments(0,argc,argv);
387 /* Consume the rest of the arguments, one at a time. */
388 while ( NextArgument() )
390 filename = GetArgument();
392 if( strcmp( filename, DO_NOT_PROCESS ) )
394 fprintf( stderr, " - parsing '%s'... ", filename);
395 ProcessSourceFile( filename );
396 fprintf( stderr, "done.\n");
400 EmitCutCheck();
401 fflush(outfile);
402 fclose(outfile);
403 return 0;
407 * vim: tabstop=3 shiftwidth=3 expandtab