int Disassembler::print_one(Instruction* seq) { printf("%8s | ", InstructionNames[seq[0]]); InstructionTypes op = (InstructionTypes)seq[0]; switch(op) { case MOVR: printf("R(%d) := R(%d)\n", seq[1], seq[2]); return 3; case MOVN: printf("R(%d) := nil\n", seq[1]); return 2; case MOVT: printf("R(%d) := true\n", seq[1]); return 2; case MOVF: printf("R(%d) := false\n", seq[1]); return 2; case MOVI8: printf("R(%d) := %d\n", seq[1], seq[2]); return 3; case LOADC: printf("R(%d) := CODE[%d]\n", seq[1], seq[2]); return 3; case MOVI32: printf("R(%d) := %d\n", seq[1], (*(int*)(&seq[2]))); return 6; case SENDI: switch(seq[4]) { case 0: printf("R(%d) := R(%d).$R(%d)\n", seq[1], seq[3], seq[2]); break; case 1: printf("R(%d) := R(%d).$R(%d) R(%d)\n", seq[1], seq[3], seq[2], seq[3] + 1); break; default: printf("R(%d) := R(%d).$R(%d) R(%d)..R(%d)\n", seq[1], seq[3], seq[2], seq[3] + 1, seq[3] + seq[4]); } return 5; case SENDI_KW: printf("R(%d) := R(%d).$R(%d) ", seq[1], seq[3], seq[2]); print_keywords(code_->keywords(seq[5]), seq[3]+1); printf("\n"); return 6; case SEND: switch(seq[4]) { case 0: printf("R(%d) := R(%d).%s\n", seq[1], seq[3], code_->string(seq[2])->c_str()); break; case 1: printf("R(%d) := R(%d).%s R(%d)\n", seq[1], seq[3], code_->string(seq[2])->c_str(), seq[3] + 1); break; default: printf("R(%d) := R(%d).%s R(%d)..R(%d)\n", seq[1], seq[3], code_->string(seq[2])->c_str(), seq[3] + 1, seq[3] + seq[4]); } return 5; case SEND_KW: printf("R(%d) := R(%d).%s ", seq[1], seq[3], code_->string(seq[2])->c_str()); print_keywords(code_->keywords(seq[5]), seq[3]+1); printf("\n"); return 6; case IVA: printf("@%s = R(%d)\n", code_->string(seq[1])->c_str(), seq[2]); return 3; case IVR: printf("R(%d) := @%s\n", seq[1], code_->string(seq[2])->c_str()); return 3; case LATTR: printf("R(%d) := R(%d)::%s\n", seq[1], seq[3], code_->string(seq[2])->c_str()); return 4; case LOADS: printf("R(%d) := \"%s\"\n", seq[1], code_->string(seq[2])->c_str()); return 3; case RET: printf("<- R(%d)\n", seq[1]); return 2; case LRET: printf("<@ R(%d)\n", seq[1]); return 2; case SELF: printf("R(%d) := R(-1)\n", seq[1]); return 2; case JMPF: printf("IP += %d\n", seq[1]); return 2; case JMPB: printf("IP -= %d\n", seq[1]); return 2; case JMPIT: printf("IP += %d if R(%d)\n", seq[2], seq[1]); return 3; case JMPIF: printf("IP += %d if !R(%d)\n", seq[2], seq[1]); return 3; case JMPHA: printf("IP += %d if R(%d).set?\n", seq[2], seq[1]); return 3; case REGE: printf("R(E).push <reg:%d, ip:%d>\n", seq[1], seq[2]); return 3; case POPE: printf("R(E).pop\n"); return 1; case LVAR: printf("R(%d) = R(vars)[%d][%d]\n", seq[1], seq[2], seq[3]); return 4; case SVAR: printf("R(vars)[%d][%d] = R(%d)\n", seq[1], seq[2], (char)seq[3]); return 4; case RAISE: printf("raise R(%d)\n", seq[1]); return 2; case NOT: printf("R(%d) := !R(%d)\n", seq[1], seq[2]); return 3; case TUPLE: printf("R(%d) := (R(%d)..R(%d))\n", seq[1], seq[2], seq[2] + (seq[3] - 1)); return 4; case LIST: printf("R(%d) := [R(%d)..R(%d)]\n", seq[1], seq[2], seq[2] + (seq[3] - 1)); return 4; case TotalInstructions: assert(0); } return -1; }
/* * process command-line parameters */ int process_options(int argc, char *argv[]) { int i; int opt_ihavename = 0; int opt_nomore = 0; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { if (opt_nomore) { if (strlen(opt_command) + strlen(argv[i]) + 2 < OPT_CMD_SZ) { // +1 for space +1 for the trailing zero strcat(opt_command, " "); strcat(opt_command, argv[i]); } else { fprintf(stderr, "Too long command line! (%s)\n", argv[i]); return 1; } } else { switch (argv[i][1]) { case '-': // the following parameters are going to script // (COMMAND$) opt_nomore = 1; break; case 's': // decompile opt_decomp++; break; case 'c': // syntax check opt_syntaxcheck++; break; case 'u': dev_setenv("UNITPATH", &argv[i][2]); break; case 'v': // verbose check if (!opt_quiet) { // -v -v opt_verbose = 1; } opt_quiet = 0; break; case 'i': opt_ide = IDE_EXTERNAL; break; case 'x': opt_nosave = 0; break; case 'p': if (strcmp(argv[i] + 1, "pkw") == 0) { print_keywords(); } break; case 'm': // load run-time modules opt_loadmod = 1; if (i + 1 < argc) { strcpy(opt_modlist, argv[++i]); } break; case 'q': // shutup opt_quiet = 1; break; case 'h': // print command-line parameters fprintf(stdout, "SmallBASIC version %s - kw:%d, pc:%d, fc:%d, ae:%d\n", SB_STR_VER, kwNULL, (kwNULLPROC - kwCLS) + 1, (kwNULLFUNC - kwASC) + 1, (int)(65536 / sizeof(var_t))); fprintf(stdout, "http://smallbasic.sourceforge.net\n\n"); if (argv[i][2] == '-' || argv[i][2] == 'x') { /* * search for command, or print all doc */ if (argv[i][2] == '-') { #ifdef HELP_SUBSYS char *command = argv[i] + 3; help_printinfo(command); #endif } else if (argv[i][2] == 'x') { // print all // printf("%s\n", help_text); ; } } else { show_help(); } return 1; default: fprintf(stderr, "unknown option: %s\n", argv[i]); return 1; }; } } else { // no - switch // this is the filename or script-parameters if (opt_ihavename == 0) { strcpy(g_file, argv[i]); if (access(g_file, F_OK)) { strcat(g_file, ".bas"); if (access(g_file, F_OK)) { fprintf(stderr, "file not accessible - %s\n", g_file); return 1; } } if (access(g_file, R_OK)) { fprintf(stderr, "file not readable - %s\n", g_file); return 1; } opt_ihavename = 1; } else { if (strlen(opt_command) + strlen(argv[i]) + 2 < OPT_CMD_SZ) { // +1 for space +1 for the trailing zero strcat(opt_command, " "); strcat(opt_command, argv[i]); } else { fprintf(stderr, "Too long command line! (%s)\n", argv[i]); return 1; } } } } // initialization if (strlen(g_file) == 0) { // stdin if (isatty(STDIN_FILENO)) { // check if it is a terminal. opt_interactive = 1; } #if defined(_Win32) || defined(_DOS) char *slash = strchr(argv[0], OS_DIRSEP); if (slash) { strcpy(g_file, argv[0]); slash = strrchr(g_file, OS_DIRSEP); *slash = OS_DIRSEP; *(slash + 1) = '\0'; strcat(g_file, "sbasic.tmp"); } else { sprintf(g_file, "sbasic.tmp"); } #elif defined(_UnixOS) sprintf(g_file, "%ctmp%csb%d.bas", OS_DIRSEP, OS_DIRSEP, getpid()); #else sprintf(g_file, "sb%d.bas", getpid()); // for minimal GNU systems like // MINGW #endif // its a temporary and it must be deleted atexit(remove_temp_file); if (opt_interactive) { // get it from console #ifdef INTERACTIVE_CONSOLE interactive_mode(g_file); #endif } else { // get it from stdin FILE *fp = fopen(g_file, "wb"); int c; if (fp) { while ((c = fgetc(stdin)) != EOF) { fputc(c, fp); } fclose(fp); } else { fprintf(stderr, "file not writeable - %s\n", g_file); return 1; } } } return 0; }
int main(int argc, char **argv) { char *dictionary_file = "en"; /* if not told otherwise, assume we're using english */ const char *input_file = NULL; char *output_file = NULL; FILE *input_stream = stdin; /*by default read from stdin */ FILE *output_stream = stdout; /*by default read from stdout */ OtsArticle *Art; int sumPercent = 20; /* if not told otherwise highlight 20% of the document */ int c,n_args=0; int html = FALSE; int keywords = FALSE; int about = FALSE; int version = FALSE; const char *const *args=NULL; GOptionContext *context = NULL; GError *error = NULL; const GOptionEntry options[] = { {"ratio" , 'r', 0, G_OPTION_ARG_INT , &sumPercent, "summarization % [default = 20%]", "<int>"}, {"dic" , 'd', 0, G_OPTION_ARG_STRING, &dictionary_file, "dictionary to use", "<string>"}, {"out" , 'o', 0, G_OPTION_ARG_STRING, &output_file, "output file [default = stdout]", "<string>"}, {"html" , 'h', 0, G_OPTION_ARG_NONE , &html, "output as html", NULL}, {"keywords", 'k', 0, G_OPTION_ARG_NONE , &keywords, "only output keywords", NULL}, {"about" , 'a', 0, G_OPTION_ARG_NONE , &about, "only output the summary", NULL}, {"version" , 'v', 0, G_OPTION_ARG_NONE , &version, "show version information", NULL}, { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, "[file.txt | stdin]" }, {NULL} }; context = g_option_context_new(" - Open Text Summarizer"); g_option_context_add_main_entries(context, options, NULL); /* Parse command line */ if (!g_option_context_parse (context, &argc, &argv, &error)) { g_option_context_free (context); g_print ("%s\n", error->message); g_error_free (error); exit (1); } /* print version number */ if (version) { printf("%s\n", PACKAGE_STRING); g_option_context_free(context); exit (0); } if(args==NULL) { printf("\nInvalid number of arguments. Use --help to see options\n"); exit(1); } if (args) while (args[n_args] != NULL) n_args++; if (n_args > 1) { g_option_context_free(context); return 1; } if (n_args == 1 && g_file_test (args[0], G_FILE_TEST_EXISTS) == TRUE) input_file = args[0]; if (input_file) { input_stream = fopen (input_file, "r"); if (!input_stream) { g_option_context_free(context); perror ("Couldn't load input file"); return 1; } } if (output_file) { output_stream = fopen (output_file, "w"); if (!output_stream) { if (input_file) fclose (input_stream); g_option_context_free(context); perror ("Couldn't load output file"); return 1; } } Art = ots_new_article (); if (!ots_load_xml_dictionary (Art, dictionary_file)) { ots_free_article (Art); if (input_file) fclose(input_stream); if (output_file) fclose(output_stream); g_option_context_free(context); perror ("Couldn't load dictionary"); return 1; } ots_parse_file (input_stream, Art); /* read article from stdin , put it in struct Article */ ots_grade_doc (Art); /* grade each sentence (how relevent is it to the text) */ /* int i; for (i=0;i<1000000;i++) { printf("\n word = %s ", ots_word_in_list(Art->ImpWords,i)); } */ ots_highlight_doc (Art, sumPercent); /* highlight what we are going to print 0% - 100% of the words */ if (html) ots_print_HTML(output_stream, Art); /* print article in html form */ else if (keywords) print_keywords(Art, input_file); else if (about) print_about(output_stream, Art); else ots_print_doc(output_stream, Art); /* print article in text */ ots_free_article (Art); if (input_file) fclose (input_stream); if (output_file) fclose (output_stream); return 0; }