The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include "ast.h" |
| 21 | #include "lexer.h" |
| 22 | #include "parser.h" |
| 23 | #include "register.h" |
| 24 | #include "execute.h" |
| 25 | |
| 26 | void |
| 27 | lexTest() |
| 28 | { |
| 29 | int token; |
| 30 | do { |
| 31 | token = yylex(); |
| 32 | if (token == 0) { |
| 33 | printf(" EOF"); |
| 34 | fflush(stdout); |
| 35 | break; |
| 36 | } else { |
| 37 | printf(" %s", tokenToString(token)); |
| 38 | fflush(stdout); |
| 39 | if (token == TOK_IDENTIFIER) { |
| 40 | if (strcmp(yylval.literalString, "assert") == 0) { |
| 41 | setLexerArgumentType(AM_BOOLEAN_ARGS); |
| 42 | } else { |
| 43 | setLexerArgumentType(AM_WORD_ARGS); |
| 44 | } |
| 45 | do { |
| 46 | token = yylex(); |
| 47 | printf(" %s", tokenToString(token)); |
| 48 | fflush(stdout); |
| 49 | } while (token != TOK_EOL && token != TOK_EOF && token != 0); |
| 50 | } else if (token != TOK_EOL) { |
| 51 | fprintf(stderr, "syntax error: expected identifier\n"); |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | } while (token != 0); |
| 56 | printf("\n"); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | usage() |
| 61 | { |
| 62 | printf("usage: amend [--debug-lex|--debug-ast] [<filename>]\n"); |
| 63 | exit(1); |
| 64 | } |
| 65 | |
| 66 | extern const AmCommandList *gCommands; |
| 67 | int |
| 68 | main(int argc, char *argv[]) |
| 69 | { |
| 70 | FILE *inputFile = NULL; |
| 71 | bool debugLex = false; |
| 72 | bool debugAst = false; |
| 73 | const char *fileName = NULL; |
| 74 | int err; |
| 75 | |
| 76 | #if 1 |
| 77 | extern int test_symtab(void); |
| 78 | int ret = test_symtab(); |
| 79 | if (ret != 0) { |
| 80 | fprintf(stderr, "test_symtab() failed: %d\n", ret); |
| 81 | exit(ret); |
| 82 | } |
| 83 | extern int test_cmd_fn(void); |
| 84 | ret = test_cmd_fn(); |
| 85 | if (ret != 0) { |
| 86 | fprintf(stderr, "test_cmd_fn() failed: %d\n", ret); |
| 87 | exit(ret); |
| 88 | } |
| 89 | extern int test_permissions(void); |
| 90 | ret = test_permissions(); |
| 91 | if (ret != 0) { |
| 92 | fprintf(stderr, "test_permissions() failed: %d\n", ret); |
| 93 | exit(ret); |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | argc--; |
| 98 | argv++; |
| 99 | while (argc > 0) { |
| 100 | if (strcmp("--debug-lex", argv[0]) == 0) { |
| 101 | debugLex = true; |
| 102 | } else if (strcmp("--debug-ast", argv[0]) == 0) { |
| 103 | debugAst = true; |
| 104 | } else if (argv[0][0] == '-') { |
| 105 | fprintf(stderr, "amend: Unknown option \"%s\"\n", argv[0]); |
| 106 | usage(); |
| 107 | } else { |
| 108 | fileName = argv[0]; |
| 109 | } |
| 110 | argc--; |
| 111 | argv++; |
| 112 | } |
| 113 | |
| 114 | if (fileName != NULL) { |
| 115 | inputFile = fopen(fileName, "r"); |
| 116 | if (inputFile == NULL) { |
| 117 | fprintf(stderr, "amend: Can't open input file '%s'\n", fileName); |
| 118 | usage(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | commandInit(); |
| 123 | //xxx clean up |
| 124 | |
| 125 | err = registerUpdateCommands(); |
| 126 | if (err < 0) { |
| 127 | fprintf(stderr, "amend: Error registering commands: %d\n", err); |
| 128 | exit(-err); |
| 129 | } |
| 130 | err = registerUpdateFunctions(); |
| 131 | if (err < 0) { |
| 132 | fprintf(stderr, "amend: Error registering functions: %d\n", err); |
| 133 | exit(-err); |
| 134 | } |
| 135 | |
| 136 | #if AMEND_LEXER_BUFFER_INPUT |
| 137 | if (inputFile == NULL) { |
| 138 | fprintf(stderr, "amend: No input file\n"); |
| 139 | usage(); |
| 140 | } |
| 141 | char *fileData; |
| 142 | int fileDataLen; |
| 143 | fseek(inputFile, 0, SEEK_END); |
| 144 | fileDataLen = ftell(inputFile); |
| 145 | rewind(inputFile); |
| 146 | if (fileDataLen < 0) { |
| 147 | fprintf(stderr, "amend: Can't get file length\n"); |
| 148 | exit(2); |
| 149 | } else if (fileDataLen == 0) { |
| 150 | printf("amend: Empty input file\n"); |
| 151 | exit(0); |
| 152 | } |
| 153 | fileData = (char *)malloc(fileDataLen + 1); |
| 154 | if (fileData == NULL) { |
| 155 | fprintf(stderr, "amend: Can't allocate %d bytes\n", fileDataLen + 1); |
| 156 | exit(2); |
| 157 | } |
| 158 | size_t nread = fread(fileData, 1, fileDataLen, inputFile); |
| 159 | if (nread != (size_t)fileDataLen) { |
| 160 | fprintf(stderr, "amend: Didn't read %d bytes, only %zd\n", fileDataLen, |
| 161 | nread); |
| 162 | exit(2); |
| 163 | } |
| 164 | fileData[fileDataLen] = '\0'; |
| 165 | setLexerInputBuffer(fileData, fileDataLen); |
| 166 | #else |
| 167 | if (inputFile == NULL) { |
| 168 | inputFile = stdin; |
| 169 | } |
| 170 | yyset_in(inputFile); |
| 171 | #endif |
| 172 | |
| 173 | if (debugLex) { |
| 174 | lexTest(); |
| 175 | } else { |
| 176 | int ret = yyparse(); |
| 177 | if (ret != 0) { |
| 178 | fprintf(stderr, "amend: Parse failed (%d)\n", ret); |
| 179 | exit(2); |
| 180 | } else { |
| 181 | if (debugAst) { |
| 182 | dumpCommandList(gCommands); |
| 183 | } |
| 184 | printf("amend: Parse successful.\n"); |
| 185 | ret = execCommandList((ExecContext *)1, gCommands); |
| 186 | if (ret != 0) { |
| 187 | fprintf(stderr, "amend: Execution failed (%d)\n", ret); |
| 188 | exit(3); |
| 189 | } |
| 190 | printf("amend: Execution successful.\n"); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |