/******************************************************************* * parse_option */ static void parse_option( const char *opt ) { switch(opt[1]) { case 'I': if (opt[2]) add_include_path( opt + 2 ); break; case 'C': if (opt[2]) SrcDir = opt + 2; else SrcDir = NULL; break; case 'f': if (opt[2]) OutputFileName = opt + 2; break; case 's': if (opt[2]) Separator = opt + 2; else Separator = NULL; break; default: fprintf( stderr, "Unknown option '%s'\n", opt ); fprintf( stderr, Usage, ProgramName ); exit(1); } }
bool add_include_path(char const *path_, bool is_system = false) { return add_include_path(path_, (is_system || was_sys_include_path) ? system_include_paths : user_include_paths); }
int main(int argc, char **argv) { int i; int res; int one = 1; int option_index = 0; int c; char *main_script = NULL; // adding default include paths add_include_path("."); // TODO: once ordinary installation is available, add /usr/share/kbd-mangler/js or something while (1) { c = getopt_long(argc, argv, "r:w:I:", long_options, &option_index); if (c == -1) break; switch (c) { case 'r': { // preparing input device int input_fd = open(optarg, O_RDONLY); if (input_fd < 0) { fprintf(stderr, "could not open input device %s: %s\n", optarg, strerror(errno)); exit(2); } if (ioctl(input_fd, EVIOCGRAB, (void *)1) < 0) { fprintf (stderr, "unable to grab device '%s' : %s\n", optarg, strerror(errno)); } if (ioctl(input_fd, FIONBIO, (void *)&one) < 0) { fprintf (stderr, "unable to set device '%s' to non-blocking mode : %s\n", optarg, strerror(errno)); } if (input_count < MAX_INPUTS) { input_fds[input_count++] = input_fd; } else { fprintf (stderr, "Maximum number of input devices exceeded (%d)\n", MAX_INPUTS); exit(1); } break; } case 'w': if (uinput_fd >= 0) { fprintf(stderr, "multiple -w options are not allowed\n"); usage(); exit(1); } init_uinput(optarg); break; case 'I': add_include_path(optarg); break; case '?': break; default: fprintf(stderr, "getopt returned unexpectd char code: 0%o\n", c); exit(1); } } if (uinput_fd == -1) { usage(); } // script name left if (argc - optind != 1) usage(); res = InitScripting(argv[optind]); // custom main script if (res) { main_loop(); } else { fprintf(stderr, "Error initializing scripting\n"); } DestroyScripting(); for (i = 0; i < input_count; i++) close(input_fds[i]); close(uinput_fd); }
int main(int argc, const char *argv[]) { const char *p; char *t; int i; int ret; char buf[128]; for(i = 1; i < argc; i++) { p = argv[i]; if (*p != '-') add_source(p); else { switch(p[1]) { case 'V': verbose = 1; break; /* What are we doing */ case 'c': mode = MODE_OBJ; break; case 'E': mode = MODE_CPP; break; case 'S': mode = MODE_ASM; break; case 'v': printf("fcc: front end to sdcc\n"); add_argument("sdcc"); add_argument("-v"); do_command(); exit(0); case 'D': add_macro(p); break; case 'l': add_library(p+2); break; case 'L': if (p[2] == 0) add_library_path(argv[++i]); else add_library_path(p+2); break; case 'I': if (p[2] == 0) add_include_path(argv[++i]); else add_include_path(p+2); break; case 'o': if (p[2] == 0) set_target(argv[++i]); else set_target(p + 2); break; case 'O': set_optimize(p + 2); break; case 'm': if (p[2] == 0) set_cpu(argv[++i]); else set_cpu(p + 2); break; case 'M': if (p[2] == 0) set_map(argv[++i]); else set_map(p + 2); break; case 't': if (p[2] == 0) set_platform(argv[++i]); else set_platform(p + 2); break; case 'g': debug = 1; break; default: if (strcmp(p, "-Werror") == 0) werror = 1; else if (strcmp(p, "-funsigned-char") == 0) unschar = 1; else if (strcmp(p, "--pedantic") == 0) pedantic = 1; else if (strcmp(p, "--nostdio") == 0) nostdio = 1; else { fprintf(stderr, "fcc: Unknown option '%s'.\n", p); exit(1); } } } } add_include_path(FCC_DIR "/include/"); add_library_path(FCC_DIR "/lib/"); snprintf(buf, sizeof(buf), "c%s", platform); add_library(buf); if (mode == MODE_OBJ) { while (srchead) { build_command(); ret = do_command(); if (ret) break; if (mode == MODE_OBJ && target) { char *orel = filebasename(rebuildname("", srchead->p, "rel")); if (rename(orel, target) == -1) { fprintf(stderr, "Unable to rename %s to %s.\n", orel, target); perror(srchead->p); exit(1); } } srchead = srchead->next; argp = 0; } } else { build_command(); ret = do_command(); } if (mode != MODE_LINK || ret) exit(ret); argp = 0; add_argument("makebin"); add_argument("-p"); add_argument("-s"); add_argument("65535"); add_argument(target); add_argument(t = rebuildname("", target, "bin")); ret = do_command(); if (ret) exit(ret); argp = 0; add_argument(FCC_DIR "/bin/binman"); snprintf(buf, sizeof(buf), "%x", progbase); add_argument(buf); add_argument(t); add_argument(rebuildname("", target, "map")); add_argument(target); ret = do_command(); exit(ret); }
int main(int argc, char **argv) { #define FIND_LONG_OPTION(ARRAY) \ { \ for(i = 0; i < sizeof (ARRAY) / sizeof(struct option); i++) { \ struct option *o = (ARRAY) + i; \ if(o->arg < 2) { \ if(strcmp(arg, o->opt) == 0) { \ const char *a = o->arg ? *++v : argv[0]; \ if(o->arg && !a) { \ fprintf(stderr, "%s: option '%s' need an argument\n", argv[0], *v); \ return 1; \ } \ if(o->act) o->act(a); \ goto first_loop; \ } \ } else { \ size_t len = strlen(o->opt); \ if(strncmp(arg, o->opt, len) == 0) { \ if(arg[len] && arg[len] != '=') continue; \ if(!arg[len] || !arg[len + 1]) { \ fprintf(stderr, "%s: option '%s' need an argument\n", argv[0], *v); \ return 1; \ } \ const char *a = arg + len + 1; \ if(o->act) o->act(a); \ goto first_loop; \ } \ } \ } \ } #define UNRECOGNIZED_OPTION(O) \ do { \ fprintf(stderr, "%s: error: unrecognized option '%s'\n", \ argv[0], (O)); \ return 1; \ } while(0) int verbose = 0; int no_link = 0; int preprocess_only = -1; int no_warning = -1; int end_of_options = 0; const char *output_file = NULL; char **v = argv; init_argv(); const char *vs_path = getenv("VS_PATH"); if(!vs_path) vs_path = getenv("VSINSTALLDIR"); if(!getenv("INCLUDE")) { if(vs_path) { size_t len = strlen(vs_path); if(vs_path[len - 1] == '/' || vs_path[len - 1] == '\\') len--; char buffer[len + 12 + len + 24 + 1]; memcpy(buffer, vs_path, len); memcpy(buffer + len, "/VC/include;", 12); memcpy(buffer + len + 12, vs_path, len); strcpy(buffer + len + 12 + len, "/VC/PlatformSDK/include;"); setenv("INCLUDE", buffer, 0); } else { no_warning = find_argv(argv, "-w"); if(!no_warning) fprintf(stderr, "%s: warning: no system include path set\n", argv[0]); } } if(!getenv("LIB")) { if(vs_path) { size_t len = strlen(vs_path); if(vs_path[len - 1] == '/' || vs_path[len - 1] == '\\') len--; char buffer[len + 8 + len + 20 + 1]; memcpy(buffer, vs_path, len); memcpy(buffer + len, "/VC/lib;", 8); memcpy(buffer + len + 12, vs_path, len); strcpy(buffer + len + 12 + len, "/VC/PlatformSDK/lib;"); setenv("LIB", buffer, 0); } else { if(no_warning == -1) no_warning = find_argv(argv, "-w"); if(!no_warning) fprintf(stderr, "%s: warning: no system library path set\n", argv[0]); } } first_loop: while(*++v) { if(!end_of_options && **v == '-') { int i; if((*v)[1] == '-') { const char *arg = *v + 2; if(!*arg) { end_of_options = 1; continue; } FIND_LONG_OPTION(double_dash_long_options); if(strcmp(arg, "verbose") == 0) { verbose = 1; } else UNRECOGNIZED_OPTION(*v); } else { const char *arg = *v + 1; FIND_LONG_OPTION(singal_dash_long_options); switch(*arg) { case 0: goto not_an_option; case 'c': if(arg[1]) UNRECOGNIZED_OPTION(*v); add_to_argv("-c"); no_link = 1; break; case 'D': if(arg[1]) add_to_argv(*v); else { const char *d = *++v; if(!d) { fprintf(stderr, "%s: error: macro name missing after '-D'\n", argv[0]); return 1; } define(d); } break; case 'E': if(arg[1]) UNRECOGNIZED_OPTION(*v); add_to_argv("-E"); preprocess_only = 1; break; case 'f': if(arg[1]) set_feature(arg + 1); else { const char *feature = *++v; if(!feature) { fprintf(stderr, "%s: error: option '-f' need an argument\n", argv[0]); return -1; } set_feature(feature); } break; case 'g': // -g[coff][<level>] (level: 0~3) if(arg[1]) { const char *level = arg + 1; if(strncmp(level, "coff", 4) == 0) level += 4; if(*level && *level != '-') { int i = 0; do { if(!isdigit(level[i])) { fprintf(stderr, "%s: error: unrecognized debug output level \"%s\"\n", argv[0], level); return 1; } } while(level[++i]); /* if(i > 1 || *level > '3') { fprintf(stderr, "%s: error: debug output level %s is too high\n", argv[0], level); return 1; } if(*level == '0') break; */ int l = atoi(level); if(!l) break; if(l > 3) { fprintf(stderr, "%s: error: debug output level %s is too high\n", argv[0], level); return 1; } } } add_to_argv("-Zi"); break; case 'I': if(no_warning == -1) no_warning = find_argv(argv, "-w"); //if(arg[1]) add_to_argv(*v); if(arg[1]) add_include_path(arg + 1, no_warning); else { const char *path = *++v; if(!path) { fprintf(stderr, "%s: error: option '-I' need an argument\n", argv[0]); return 1; } add_include_path(path, no_warning); } break; case 'L': if(no_warning == -1) no_warning = find_argv(argv, "-w"); if(arg[1]) add_library_path(arg + 1, no_warning); else { const char *path = *++v; if(!path) { fprintf(stderr, "%s: error: option '-L' need an argument\n", argv[0]); return 1; } add_library_path(path, no_warning); } break; case 'l': if(arg[1]) add_library(arg + 1); else { const char *path = *++v; if(!path) { fprintf(stderr, "%s: error: option '-l' need an argument\n", argv[0]); return 1; } add_library(path); } break; case 'M': if(arg[1]) UNRECOGNIZED_OPTION(*v); add_to_argv("-showIncludes"); break; case 'm': if(arg[1]) set_machine(arg + 1); else { const char *machine = *++v; if(!machine) { fprintf(stderr, "%s: argument to `-m' is missing\n", argv[0]); return 1; } set_machine(machine); } break; case 'O': if(arg[1]) { const char *o = arg + 1; if(strcmp(o, "0") == 0) add_to_argv("-Od"); else if(strcmp(o, "1") == 0) add_to_argv("-O2"); else if(strcmp(o, "3") == 0) add_to_argv("-Ox"); else if(strcmp(o, "s") == 0) add_to_argv("-O1"); else if(strcmp(o, "fast") == 0) add_to_argv("-O2"); else add_to_argv(*v); } else add_to_argv("-O2"); break; case 'o': if(arg[1]) output_file = arg + 1; else { output_file = *++v; if(!output_file) { fprintf(stderr, "%s: error: option '-o' need an argument\n", argv[0]); return 1; } } break; case 'P': if(preprocess_only == -1) preprocess_only = find_argv(argv, "-E"); if(preprocess_only) add_to_argv("-EP"); break; case 's': if(arg[1]) UNRECOGNIZED_OPTION(*v); break; case 'U': if(arg[1]) add_to_argv(*v); else { const char *u = *++v; if(!u) { fprintf(stderr, "%s: error: macro name missing after '-U'\n", argv[0]); return 1; } undefine(u); } break; case 'v': if(arg[1]) UNRECOGNIZED_OPTION(*v); verbose = 1; break; case 'W': if(!arg[1]) { if(no_warning == -1) no_warning = find_argv(argv, "-w"); if(!no_warning) { fprintf(stderr, "%s: warning: option '-W' is deprecated; use '-Wextra' instead\n", argv[0]); } add_to_argv("-Wall"); break; } if(strncmp(arg, "Wa,", 3) == 0 || strncmp(arg, "Wp,", 3) == 0 || strncmp(arg, "Wl,", 3) == 0) { (*v)[3] = 0; // XXX fprintf(stderr, "%s: warning: option '%s' is not supported\n", argv[0], *v); break; } if(set_warning(arg + 1)) break; add_to_argv(*v); break; case 'w': if(arg[1]) UNRECOGNIZED_OPTION(*v); add_to_argv("-w"); no_warning = 1; break; case 'x': if(arg[1]) set_language(arg + 1); else { const char *lang = *++v; if(!lang) { fprintf(stderr, "%s: error: missing argument to ‘-x’", argv[0]); return 4; } set_language(lang); } break; default: fprintf(stderr, "%s: error: unrecognized option '%s'\n", argv[0], *v); return 1; } } } else { not_an_option: #if defined __INTERIX && !defined _NO_CONV_PATH if(**v == '/') { char buffer[PATH_MAX + 1]; if(unixpath2win(*v, 0, buffer, sizeof buffer) == 0) { add_input_file(buffer); } else { if(no_warning == -1) no_warning = find_argv(argv, "-w"); if(!no_warning) { fprintf(stderr, "%s: warning: cannot convert '%s' to Windows path name, %s\n", argv[0], *v, strerror(errno)); } add_input_file(*v); } } else #endif add_input_file(*v); } } setvbuf(stdout, NULL, _IOLBF, 0); if(preprocess_only == -1) preprocess_only = 0; if(no_warning == -1) no_warning = 0; if(last_language && last_language_unused && !no_warning) { fprintf(stderr, "%s: warning: '-x %s' after last input file has no effect\n", argv[0], last_language); } if(!first_input_file) { if(verbose) { if(!no_link) add_to_argv("-c"); start_cl(); return 0; } fprintf(stderr, "%s: no input files\n", argv[0]); return 1; } if(multiple_input_files && (preprocess_only || no_link)) { if(output_file) { fprintf(stderr, "%s: error: cannot specify -o with -c or -E with multiple files\n", argv[0]); return 4; } else if(no_link) { fprintf(stderr, "%s: error: '-c' with multiple files is currently not supported\n", argv[0]); return -1; } } if(!output_file && !preprocess_only) { if(no_link) { size_t len = strlen(first_input_file); int n = get_last_dot(first_input_file, len); if(n >= 0) len = n; char *p = malloc(len + 3); if(!p) { perror(argv[0]); return 1; } memcpy(p, first_input_file, len); strcpy(p + len, ".o"); output_file = p; } else output_file = DEFAULT_OUTPUT_FILENAME; } if(!verbose) add_to_argv("-nologo"); if(preprocess_only) { if(output_file) target.name = output_file; target.type = PREPROCESSED_SOURCE; } else set_output_file(output_file, no_link, no_warning); //if(no_static_link) add_to_argv("-MD"); add_to_argv(no_static_link ? "-MD" : "-MT"); add_libraries_to_argv(); if(verbose) print_argv(); return start_cl(); }