static int decode_sick_model_number(const char *model) { if (string_begins_with(model, "LMS291;S05")) return SICK_MODEL_LMS291_S05; if (string_begins_with(model, "LMS291;S14")) return SICK_MODEL_LMS291_S14; return SICK_MODEL_UNKNOWN; }
static int find_wlan_rfill (const char *ifname, uint32_t *out_index) { char ieee_path[100]; snprintf(ieee_path, sizeof(ieee_path), "/sys/class/net/%s/../../ieee80211", ifname); int res = 0; DIR *d = opendir(ieee_path); if (!d) { goto fail0; } struct dirent *e; while (e = readdir(d)) { if (!string_begins_with(e->d_name, "phy")) { continue; } char phy_path[150]; snprintf(phy_path, sizeof(phy_path), "%s/%s", ieee_path, e->d_name); DIR *d2 = opendir(phy_path); if (!d2) { continue; } struct dirent *e2; while (e2 = readdir(d2)) { int index_pos; if (!(index_pos = string_begins_with(e2->d_name, "rfkill"))) { continue; } uint32_t index; if (sscanf(e2->d_name + index_pos, "%"SCNu32, &index) != 1) { continue; } res = 1; *out_index = index; } closedir(d2); } closedir(d); fail0: return res; }
static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out) { struct instance *o = vo; if (!strcmp(name, "succeeded")) { *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index); return 1; } size_t pos; uintmax_t n; if ((pos = string_begins_with(name, "match")) && parse_unsigned_integer(MemRef_MakeCstr(name + pos), &n)) { if (o->succeeded && n < MAX_MATCHES && o->matches[n].rm_so >= 0) { regmatch_t *m = &o->matches[n]; ASSERT(m->rm_so <= o->input.len) ASSERT(m->rm_eo >= m->rm_so) ASSERT(m->rm_eo <= o->input.len) size_t len = m->rm_eo - m->rm_so; *out = NCDVal_NewStringBinMr(mem, MemRef_Sub(o->input, m->rm_so, len)); return 1; } } return 0; }
static int make_connect_addr (const char *str, struct BConnection_addr *out_addr) { size_t i; if (i = string_begins_with(str, "unix:")) { *out_addr = BConnection_addr_unix(str + i, strlen(str + i)); } else if (i = string_begins_with(str, "tcp:")) { BAddr baddr; if (!BAddr_Parse2(&baddr, (char *)str + i, NULL, 0, 1)) { BLog(BLOG_ERROR, "failed to parse tcp address"); return 0; } *out_addr = BConnection_addr_baddr(baddr); } else { BLog(BLOG_ERROR, "address must start with unix: or tcp:"); return 0; } return 1; }
bool app_arguments (int argc, char **argv, AppArgument *arguments) { int argi = 1; int order = 1; if (argc < 0) { error_code (InvalidArgument, 1); return false; } if (!argv) { error_code (InvalidArgument, 2); return false; } if (!arguments) { error_code (InvalidArgument, 3); return false; } if (!arguments_validate (arguments)) { error_code (FunctionCall, 1); return false; } set_ordinal (arguments); set_default (arguments); app_arguments_default_reset (); while (argi < argc) { if (string_begins_with (argv[argi], "-")) { if (!argument_named (argc, argv, &argi, arguments)) { error_code (FunctionCall, 2); return false; } } else { if (!argument_ordinal (argc, argv, &argi, arguments, &order)) { error_code (FunctionCall, 3); return false; } } } if (!app_arguments_default (argc, argv, arguments)) { error_code (FunctionCall, 4); return false; } if (!check_required (arguments)) { error_code (FunctionCall, 5); return false; } return true; }
CompileProject *compile_project_create (const char *path) { CompileProject *project = NULL; if (!file_path_is_valid (path)) { compile_print ("The path is not valid: %s\n", path); error_code (FunctionCall, 1); return NULL; } if (!(project = memory_create (sizeof (CompileProject)))) { error_code (FunctionCall, 2); return NULL; } if (!(project->topological = topological_create ())) { compile_project_destroy (project); error_code (FunctionCall, 3); return NULL; } if (!(project->nodes = list_create ())) { compile_project_destroy (project); error_code (FunctionCall, 4); return NULL; } if (!(project->directory_to_compile = tree_create ())) { compile_project_destroy (project); error_code (FunctionCall, 5); return NULL; } if (!(project->directory = directory_open (path))) { compile_print ("Failed to open the directory: %s\n", path); compile_project_destroy (project); error_code (FunctionCall, 6); return NULL; } if (!string_begins_with (project->directory->name, "project.")) { compile_print ("The directory name must begin with 'project.'.\n"); compile_project_destroy (project); error_code (FunctionCall, 7); return NULL; } project->sorted = NULL; return project; }
int parse_arguments (int argc, char *argv[]) { if (argc <= 0) { return 0; } options.help = 0; options.version = 0; options.logger = LOGGER_STDERR; #ifdef BADVPN_USE_SYSLOG options.logger_syslog_facility = "daemon"; options.logger_syslog_ident = argv[0]; #endif options.loglevel = -1; for (int i = 0; i < BLOG_NUM_CHANNELS; i++) { options.loglevels[i] = -1; } options.config_file = NULL; options.syntax_only = 0; options.retry_time = DEFAULT_RETRY_TIME; options.signal_exit_code = DEFAULT_SIGNAL_EXIT_CODE; options.no_udev = 0; options.extra_args = NULL; options.num_extra_args = 0; for (int i = 1; i < argc; i++) { char *arg = argv[i]; if (!strcmp(arg, "--help")) { options.help = 1; } else if (!strcmp(arg, "--version")) { options.version = 1; } else if (!strcmp(arg, "--logger")) { if (1 >= argc - i) { fprintf(stderr, "%s: requires an argument\n", arg); return 0; } char *arg2 = argv[i + 1]; if (!strcmp(arg2, "stdout")) { options.logger = LOGGER_STDOUT; } else if (!strcmp(arg2, "stderr")) { options.logger = LOGGER_STDERR; } #ifdef BADVPN_USE_SYSLOG else if (!strcmp(arg2, "syslog")) { options.logger = LOGGER_SYSLOG; } #endif else { fprintf(stderr, "%s: wrong argument\n", arg); return 0; } i++; } #ifdef BADVPN_USE_SYSLOG else if (!strcmp(arg, "--syslog-facility")) { if (1 >= argc - i) { fprintf(stderr, "%s: requires an argument\n", arg); return 0; } options.logger_syslog_facility = argv[i + 1]; i++; } else if (!strcmp(arg, "--syslog-ident")) { if (1 >= argc - i) { fprintf(stderr, "%s: requires an argument\n", arg); return 0; } options.logger_syslog_ident = argv[i + 1]; i++; } #endif else if (!strcmp(arg, "--loglevel")) { if (1 >= argc - i) { fprintf(stderr, "%s: requires an argument\n", arg); return 0; } if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) { fprintf(stderr, "%s: wrong argument\n", arg); return 0; } i++; } else if (!strcmp(arg, "--channel-loglevel")) { if (2 >= argc - i) { fprintf(stderr, "%s: requires two arguments\n", arg); return 0; } int channel = BLogGlobal_GetChannelByName(argv[i + 1]); if (channel < 0) { fprintf(stderr, "%s: wrong channel argument\n", arg); return 0; } int loglevel = parse_loglevel(argv[i + 2]); if (loglevel < 0) { fprintf(stderr, "%s: wrong loglevel argument\n", arg); return 0; } options.loglevels[channel] = loglevel; i += 2; } else if (!strcmp(arg, "--config-file")) { if (1 >= argc - i) { fprintf(stderr, "%s: requires an argument\n", arg); return 0; } options.config_file = argv[i + 1]; i++; } else if (!strcmp(arg, "--syntax-only")) { options.syntax_only = 1; } else if (!strcmp(arg, "--retry-time")) { if (1 >= argc - i) { fprintf(stderr, "%s: requires an argument\n", arg); return 0; } if ((options.retry_time = atoi(argv[i + 1])) < 0) { fprintf(stderr, "%s: wrong argument\n", arg); return 0; } i++; } else if (!strcmp(arg, "--signal-exit-code")) { if (1 >= argc - i) { fprintf(stderr, "%s: requires an argument\n", arg); return 0; } if ((options.signal_exit_code = atoi(argv[i + 1])) < 0) { fprintf(stderr, "%s: wrong argument\n", arg); return 0; } i++; } else if (!strcmp(arg, "--no-udev")) { options.no_udev = 1; } else if (!strcmp(arg, "--")) { options.extra_args = &argv[i + 1]; options.num_extra_args = argc - i - 1; i += options.num_extra_args; } else if (!string_begins_with(arg, "--")) { if (options.config_file) { fprintf(stderr, "%s: program is already specified (did you mean to use -- ?)\n", arg); return 0; } options.config_file = argv[i]; options.extra_args = &argv[i + 1]; options.num_extra_args = argc - i - 1; i += options.num_extra_args; } else { fprintf(stderr, "unknown option: %s\n", arg); return 0; } } if (options.help || options.version) { return 1; } if (!options.config_file) { fprintf(stderr, "No program is specified.\n"); return 0; } return 1; }
int main (int argc, char **argv) { CompileProject *project = NULL; char *path = NULL; char *name = NULL; char *sub_path = NULL; char *sub_name = NULL; bool result; result = try (argc, argv, &project, &path, &name, &sub_path, &sub_name); if (project) { compile_project_destroy (project); } if (path) { string_destroy (path); } if (name) { string_destroy (name); } if (sub_path) { string_destroy (sub_path); } if (sub_name) { string_destroy (sub_name); } return result ? EXIT_SUCCESS : EXIT_FAILURE; } static bool try (int argc, char **argv, CompileProject **project, char **path, char **name, char **sub_path, char **sub_name) { bool bootstrap; AppArgument arguments[] = { ARGUMENT_NAMED_BOOLEAN ("-b", "--bootstrap", false, false, &bootstrap, "Only print commands, don't execute them."), ARGUMENT_DEFAULT, ARGUMENT_END }; if (!app_arguments (argc, argv, arguments)) { app_arguments_usage (argc, argv, arguments); return EXIT_FAILURE; } if (!(*path = directory_current_path ())) { compile_print ("Failed to get current path.\n"); return false; } if (!(*name = file_name_from_path (*path))) { compile_print ("Failed to get name from path.\n"); return false; } if (string_begins_with (*name, "project.")) { if (!(*project = compile_project_create (*path)) || !compile_project_prepare (*project) || !compile_project_execute (*project, bootstrap)) { return false; } else { return true; } } else if (string_begins_with (*name, "lib.") || string_begins_with (*name, "app.") || string_begins_with (*name, "plugin.")) { if (!(*sub_path = directory_sub_path (*path))) { return false; } if (!(*sub_name = file_name_from_path (*sub_path))) { return false; } if (!string_begins_with (*sub_name, "project.")) { return false; } if (!(*project = compile_project_create (*sub_path)) || !compile_project_prepare (*project) || !compile_project_execute_with_directory_name (*project, *name, bootstrap)) { return false; } else { return true; } } else { compile_print ("Directory name must begin with either 'project.', 'app.', 'lib.' or 'plugin'.\n"); } return false; }
bool compile_project_prepare (CompileProject *project) { ListNode *node; Directory *sub_directory; Compile *compile; Compile *sub_compile; TreeIterator *iterator; if (!project) { error (InvalidArgument); return false; } if (!directory_read (project->directory)) { compile_print ("Failed to read directory: %s\n", project->directory->name); error_code (FunctionCall, 1); return false; } for (node = list_first (project->directory->directories); node; node = list_next (node)) { if (!(sub_directory = node->data)) { error_code (InvalidOperation, 1); return false; } if (!string_begins_with (sub_directory->name, "lib.") && !string_begins_with (sub_directory->name, "app.") && !string_begins_with (sub_directory->name, "plugin.")) { continue; } if (!(compile = compile_create (project->directory, sub_directory))) { error_code (FunctionCall, 2); return false; } if (!list_append (project->nodes, compile)) { compile_destroy (compile); error_code (FunctionCall, 3); return false; } if (!topological_add_vertex (project->topological, (Object *)compile)) { error_code (FunctionCall, 4); return false; } if (!tree_insert (project->directory_to_compile, (Object *)sub_directory, compile)) { error_code (FunctionCall, 5); return false; } } for (node = list_first (project->directory->directories); node; node = list_next (node)) { if (!(sub_directory = node->data)) { error_code (InvalidOperation, 3); return false; } if (!string_begins_with (sub_directory->name, "lib.") && !string_begins_with (sub_directory->name, "app.") && !string_begins_with (sub_directory->name, "plugin.")) { continue; } if (!(compile = tree_search (project->directory_to_compile, (Object *)sub_directory))) { error_code (InvalidOperation, 5); return false; } if (!compile_prepare (compile)) { error_code (FunctionCall, 6); return false; } if (!(iterator = tree_iterator_create (compile->libraries))) { error_code (FunctionCall, 7); return false; } while (tree_iterator_next (iterator)) { if (string_equals (sub_directory->name, ((Directory *)iterator->key)->name)) { continue; } if (!(sub_compile = tree_search (project->directory_to_compile, iterator->key))) { tree_iterator_destroy (iterator); error_code (InvalidOperation, 6); return false; } if (!topological_set_edge (project->topological, (Object *)compile, (Object *)sub_compile)) { tree_iterator_destroy (iterator); error_code (FunctionCall, 8); return false; } } tree_iterator_destroy (iterator); } if (!(project->sorted = topological_sort (project->topological))) { compile_print ("Topological sort of project directories failed.\n"); error_code (InvalidOperation, 7); return false; } for (node = list_first (project->sorted); node; node = list_next (node)) { if (!recursively_flatten_libraries (project, node->data)) { return false; } if (!sort_libraries (project, node->data)) { return false; } } for (node = list_last (project->sorted); node; node = list_previous (node)) { if (!compile_actions (node->data, project->directory->path)) { error_code (FunctionCall, 9); return false; } } return true; }