/* * Main entry point of the program. * * @param argc The amount of arguments given. * @param argv The array with arguments. */ int main(int argc, char *argv[]) { int c; int i = 1; int option_index = 0; while (1) { option_index = 0; c = getopt_long (argc, argv, "he:", long_options, &option_index); if (c == -1) break; switch (c) { case 0: if (long_options[option_index].flag != 0) break; break; case 'h': print_usage(); return EXIT_SUCCESS; case 'e': return run_string((char *) optarg); case '?': print_usage(); return EXIT_FAILURE; default: abort(); } } if (argc > 1) { while (i < argc) if (run_file(fopen(argv[i++], "r")) == EXIT_FAILURE) fprintf(stderr, "error: failed to read file %s\n", argv[i - 1]); else printf("\n"); } else { // checks if someone is piping code or just calling it the normal way. if (isatty(fileno(stdin))) { run_interactive_console(); printf("\n"); } else { if (run_file(stdin) == EXIT_FAILURE) fprintf(stderr, "error: failed to read from stdin\n"); } } return EXIT_SUCCESS; }
int main(int argc, char **argv) { int core = 0; // EASYNMC_CORE_ANY; /* Default - use first available core */ int ret; char* self = "nmrun"; if (argc < 2) usage(argv[0]), exit(1); while (1) { int c; int option_index = 0; c = getopt_long (argc, argv, "c:h", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 'c': if (strcmp(optarg, "all") == 0) core = -1; else core = atoi(optarg); break; case 'h': usage(argv[0]); exit(1); break; case '?': default: break; } } char* absfile = argv[optind++]; int num_args = argc - optind; char **args = &argv[optind]; uint32_t flags = ABSLOAD_FLAG_DEFAULT; if (g_nostdio) flags &= ~(ABSLOAD_FLAG_STDIO); struct easynmc_handle *h = easynmc_open(core); g_handle = h; if (!h) { fprintf(stderr, "Failed to open core %d\n", core); exit(1); } int state; if ((state = easynmc_core_state(h)) != EASYNMC_CORE_IDLE) { fprintf(stderr, "Core is %s, expecting core to be idle\n", easynmc_state_name(state)); exit(1); } ret = easynmc_load_abs(h, absfile, &entrypoint, flags); if (0!=ret) { fprintf(stderr, "Failed to upload abs file\n"); exit(1); } ret = easynmc_set_args(h, self, num_args, args); if (ret != 0) { fprintf(stderr, "WARN: Failed to set arguments. Not supported by app?\n"); } ret = easynmc_pollmark(h); if (ret != 0) { fprintf(stderr, "Failed to reset polling counter (\n"); exit(1); }; ret = easynmc_start_app(h, entrypoint); if (ret != 0) { fprintf(stderr, "Failed to start app (\n"); exit(1); } ret = 0; if (!g_nosigint) signal(SIGINT, handle_sigint); easynmc_persist_set(h, g_nosigint ? EASYNMC_PERSIST_ENABLE : EASYNMC_PERSIST_DISABLE); if (!g_detach) { fprintf(stderr, "Application now started, hit CTRL+C to %s it\n", g_nosigint ? "detach" : "stop"); ret = run_interactive_console(h); } else { fprintf(stderr, "Application started, detaching\n"); } if (isatty(STDIN_FILENO)) nonblock(STDIN_FILENO, 0); return ret; }