int convert(char* roman_numeral) { Parser parser; parser.value = 0; parser.prev = 0; while (*roman_numeral) { Parser_parse(&parser, *roman_numeral); roman_numeral++; } return parser.value; }
int main(int argc, char** argv) { setlocale(LC_ALL, "en_US.UTF-8"); int i; for (i = 1; i < argc; ++i) { TokenScanner* token_scanner = FileTokenScanner_new(argv[i]); TokenMatcher* token_matcher = TokenMatcher_new(L"en"); Builder* builder = TokenFormatterBuilder_new(); Parser* parser = Parser_new(builder); Parser_parse(parser, token_matcher, token_scanner); Parser_delete(parser); TokenFormatterBuilder_delete(builder); TokenMatcher_delete(token_matcher); TokenScanner_delete(token_scanner); } return 0; }
char *constructPrompt( char *Prompt, int maxLen ) { /* * We will use the current user's name, the current host and the current * directory to construct the prompt */ /* An array of environment variable to search */ char *find[] = { "USER", "HOSTNAME", "PWD", NULL }; char **searcher, *finder; int curLen = 0; Parser *p; int count; /* Restart the prompt */ Prompt[0] = '\0'; /* Create a new parser for the PWD */ p = Parser_new(); Parser_setDelimiters(p, "/"); /* * Find the proper environment variables and construct the prompt */ for( searcher = find; *searcher != NULL; searcher++ ) { finder = getenv( *searcher ); curLen = maxLen - strlen( Prompt ); if( finder != NULL ) { /* We are now messing with the PWD */ if( !strncmp( *searcher, "PWD", 3 ) ) { /* Parse the directory */ Parser_parse(p, finder ); count = Parser_numTokens(p); /* * There are more than two direcories in the Path * so we need to some more work */ if( count > 2 ) { /* * Forget all but the last two tokens */ for( count = Parser_numTokens(p) - 2; count > 0; count-- ) { Parser_next(p); } /* * Add the last two tokens to our prompt */ strncat( Prompt, Parser_next(p), strlen(Prompt) - 1 ); strncat( Prompt, "/", 1 ); strncat( Prompt, Parser_next(p), strlen(Prompt) - 1 ); strncat( Prompt, ":", 1 ); continue; } } strncat( Prompt, finder, --curLen ); } else { strncat( Prompt, "(null)", --curLen ); } strncat( Prompt, ":", curLen - 1 ); } strncat( Prompt, ">", curLen ); return Prompt; }
int main( int argc, char *argv[], char *env[] ) { char Prompt[MAXSIZE]; char Input[MAXSIZE]; int i; int count; char *look; Process *child; Parser *p; Executor *e; /* Point to our name */ PROGNAME = argv[0]; /* * Construct a parser to handle the input * * We delimit on the space and "|" for now, but need to keep the * "|" so we need to it as a Special Delimiter */ p = Parser_new(); Parser_setDelimiters( p, " |" ); Parser_setSpecialDelimiters( p, "|" ); /* * Construct an executor to run the commands * * We delimit on the "|" for now */ e = Executor_new(); Executor_setExecDelimiters( e, "|" ); /* * Setup the signals */ setupSignals(); /* * Starting from the first true argument, scan the argument table */ for( i = 1; i < argc; i++ ) { if( !strncmp( argv[i], "--version", 10 ) ) { Version(); } if( !strncmp( argv[i], "--help", 7 ) ) { Usage(); } } /* Construct the prompt */ constructPrompt( Prompt, MAXSIZE ); do { gotSignal = 0; /* Infinite loop to read in from the user */ for( printf("%s ", Prompt); fgets( Input, MAXSIZE, stdin) != NULL; printf("%s ", Prompt) ) { /* They just pressed Enter */ if( *Input == '\n' ) { continue; } /* Quit */ if( !strncmp( Input, "exit\n", 5 ) ) { gotSignal = 0; break; } /* * Background the process */ if( !strncmp( Input, "bg\n", 3 ) ) { child = ProcControl_get(ProcControl_numProcs()); if( child != NULL ) { printf("Backgrounded %d ", Process_getPID(child)); Process_printCommand( child, stdout ); printf("\n"); Process_resume(child); Parser_clear( p ); gotSignal = 0; } else { fprintf( stderr, "\n!!! There is no child to background.\n" ); } ProcControl_printTable( stdout ); continue; } /* * List current jobs */ if( !strncmp( Input, "jobs\n", 5 ) ) { ProcControl_printTable( stdout ); Parser_clear(p); gotSignal = 0; continue; } /* * Foreground the last process */ if( !strncmp( Input, "fg\n", 3 ) ) { child = ProcControl_get(ProcControl_numProcs()); RUNNING = child; if( child != NULL ) { Process_resume( RUNNING ); printf("Forground process %d ", Process_getPID(RUNNING) ); Process_printCommand( RUNNING, stdout ); printf("\n"); Process_wait( RUNNING, PROCESS_WAIT ); Parser_clear(p); gotSignal = 0; } else { fprintf(stderr, "\n!!! There are no backgrounded children to " "bring to the fore.\n\n"); } continue; } /* Parse the input */ Parser_parse( p, Input ); while( (RUNNING = Executor_execute( e, p ) ) != NULL) { ProcControl_add(RUNNING); Process_start( RUNNING ); if( RUNNING != NULL ) { Process_wait( RUNNING, PROCESS_WAIT); } } /* Clear the parser's token list */ Parser_clear( p ); gotSignal = 0; } }while( gotSignal ); printf("\nProgram exit.\n"); /* Clean up children that haven't finished */ ProcControl_free(); /* Free our parser and executor */ Parser_free( p ); Executor_free( e ); Signal_clear(); return 0; }
int main(int argc, char* argv[]){ char system_call[80]; CryEnv *env; CryTag tag; CryHash hash, extra; CryContext *ctx; CryFunctionLib *library; data_allocated = 0; CryParseContext context; if(argc < 2) { printf("Usage: %s filename\n", argv[0]); return -1; } CrymlSetup(); env = CryEnv_init(); // For debug purposes only! // env->flags = TAG_UNCOMPRESSED_TEXT; env->flags = 0; env->scopes = malloc(sizeof(CryScope)); env->scopes->name = strdup("rb"); env->tags = CryHash_init(3); CryLibCore_include_corelib(env, "cryml", TRUE); // CryHash_push(env->tags, "if", CryLibCore_if()); // CryHash_push(env->tags, "funclib", CryLibCore_funclib()); // library = CryFunctionLib_init("bin/lib_example_functions.so"); // if(library) printf("Library Loaded!\n"); // CryFunctionLib_apply(library, env); // printf("%s\n", CryArray_get(CryStack_top(library->functions)->value.expression->data->value.array, 0)->value.string); // printf("%d\n", CryArray_get(CryStack_top(library->functions)->value.expression->data->value.array, 1)->value.integer); // printf("Syntactic Analisys\n"); Parser_parse(env, argv[1], 0); hash = CryHash_init(3); extra = CryHash_init(3); CryHash_add(hash, "home_url", CryData_string("http://www.home.url")); ctx = CryContext_init(env, hash, extra); CryRender_render(ctx, stdout); CryHash_free(hash); CryHash_free(extra); CryContext_free(ctx); free(env->scopes->name); free(env->scopes); CryEnv_free(env); CrymlCleanup(); printf("\n\nAllocated data: %d\n", data_allocated); if(DEBUG_LEAKS){ sprintf(system_call, "leaks %d > tmp/parsing_leaks.txt\n", getpid()); printf("%s", system_call); system(system_call); } return 0; }