📄 cmdline.c
字号:
goto failure; } if (args_info->hosts_deny_sip_given && ! override) continue; local_args_info.hosts_deny_sip_given = 1; args_info->hosts_deny_sip_given = 1; if (args_info->hosts_deny_sip_arg) free (args_info->hosts_deny_sip_arg); /* free previous string */ args_info->hosts_deny_sip_arg = gengetopt_strdup (optarg); if (args_info->hosts_deny_sip_orig) free (args_info->hosts_deny_sip_orig); /* free previous string */ args_info->hosts_deny_sip_orig = gengetopt_strdup (optarg); } /* the sip address which we accept SIP traffic. */ else if (strcmp (long_options[option_index].name, "hosts_allow_sip") == 0) { if (local_args_info.hosts_allow_sip_given) { fprintf (stderr, "%s: `--hosts_allow_sip' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->hosts_allow_sip_given && ! override) continue; local_args_info.hosts_allow_sip_given = 1; args_info->hosts_allow_sip_given = 1; if (args_info->hosts_allow_sip_arg) free (args_info->hosts_allow_sip_arg); /* free previous string */ args_info->hosts_allow_sip_arg = gengetopt_strdup (optarg); if (args_info->hosts_allow_sip_orig) free (args_info->hosts_allow_sip_orig); /* free previous string */ args_info->hosts_allow_sip_orig = gengetopt_strdup (optarg); } /* nat function option. */ else if (strcmp (long_options[option_index].name, "nat") == 0) { if (local_args_info.nat_given) { fprintf (stderr, "%s: `--nat' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->nat_given && ! override) continue; local_args_info.nat_given = 1; args_info->nat_given = 1; args_info->nat_flag = !(args_info->nat_flag); } /* security function option. */ else if (strcmp (long_options[option_index].name, "security") == 0) { if (local_args_info.security_given) { fprintf (stderr, "%s: `--security' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->security_given && ! override) continue; local_args_info.security_given = 1; args_info->security_given = 1; args_info->security_flag = !(args_info->security_flag); } /* bandctrl function option. */ else if (strcmp (long_options[option_index].name, "bandctrl") == 0) { if (local_args_info.bandctrl_given) { fprintf (stderr, "%s: `--bandctrl' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->bandctrl_given && ! override) continue; local_args_info.bandctrl_given = 1; args_info->bandctrl_given = 1; args_info->bandctrl_flag = !(args_info->bandctrl_flag); } /* interworking function option. */ else if (strcmp (long_options[option_index].name, "interworking") == 0) { if (local_args_info.interworking_given) { fprintf (stderr, "%s: `--interworking' option given more than once%s\n", argv[0], (additional_error ? additional_error : "")); goto failure; } if (args_info->interworking_given && ! override) continue; local_args_info.interworking_given = 1; args_info->interworking_given = 1; args_info->interworking_flag = !(args_info->interworking_flag); } break; case '?': /* Invalid option. */ /* `getopt_long' already printed an error message. */ goto failure; default: /* bug: option not considered. */ fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); abort (); } /* switch */ } /* while */ cmdline_parser_release (&local_args_info); if ( error ) return (EXIT_FAILURE); return 0;failure: cmdline_parser_release (&local_args_info); return (EXIT_FAILURE);}#ifndef CONFIG_FILE_LINE_SIZE#define CONFIG_FILE_LINE_SIZE 2048#endif#define ADDITIONAL_ERROR " in configuration file "#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)/* 3 is for "--" and "=" */static int_cmdline_parser_configfile (char * const filename, int *my_argc){ FILE* file; char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1]; char linebuf[CONFIG_FILE_LINE_SIZE]; int line_num = 0; int result = 0, equal; char *fopt, *farg; char *str_index; size_t len, next_token; char delimiter; if ((file = fopen(filename, "r")) == NULL) { fprintf (stderr, "%s: Error opening configuration file '%s'\n", CMDLINE_PARSER_PACKAGE, filename); return EXIT_FAILURE; } while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != NULL) { ++line_num; my_argv[0] = '\0'; len = strlen(linebuf); if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1)) { fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n", CMDLINE_PARSER_PACKAGE, filename, line_num); result = EXIT_FAILURE; break; } /* find first non-whitespace character in the line */ next_token = strspn (linebuf, " \t\r\n"); str_index = linebuf + next_token; if ( str_index[0] == '\0' || str_index[0] == '#') continue; /* empty line or comment line is skipped */ fopt = str_index; /* truncate fopt at the end of the first non-valid character */ next_token = strcspn (fopt, " \t\r\n="); if (fopt[next_token] == '\0') /* the line is over */ { farg = NULL; equal = 0; goto noarg; } /* remember if equal sign is present */ equal = (fopt[next_token] == '='); fopt[next_token++] = '\0'; /* advance pointers to the next token after the end of fopt */ next_token += strspn (fopt + next_token, " \t\r\n"); /* check for the presence of equal sign, and if so, skip it */ if ( !equal ) if ((equal = (fopt[next_token] == '='))) { next_token++; next_token += strspn (fopt + next_token, " \t\r\n"); } str_index += next_token; /* find argument */ farg = str_index; if ( farg[0] == '\"' || farg[0] == '\'' ) { /* quoted argument */ str_index = strchr (++farg, str_index[0] ); /* skip opening quote */ if (! str_index) { fprintf (stderr, "%s:%s:%d: unterminated string in configuration file\n", CMDLINE_PARSER_PACKAGE, filename, line_num); result = EXIT_FAILURE; break; } } else { /* read up the remaining part up to a delimiter */ next_token = strcspn (farg, " \t\r\n#\'\""); str_index += next_token; } /* truncate farg at the delimiter and store it for further check */ delimiter = *str_index, *str_index++ = '\0'; /* everything but comment is illegal at the end of line */ if (delimiter != '\0' && delimiter != '#') { str_index += strspn(str_index, " \t\r\n"); if (*str_index != '\0' && *str_index != '#') { fprintf (stderr, "%s:%s:%d: malformed string in configuration file\n", CMDLINE_PARSER_PACKAGE, filename, line_num); result = EXIT_FAILURE; break; } } noarg: if (!strcmp(fopt,"include")) { if (farg && *farg) { result = _cmdline_parser_configfile(farg, my_argc); } else { fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n", CMDLINE_PARSER_PACKAGE, filename, line_num); } continue; } len = strlen(fopt); strcat (my_argv, len > 1 ? "--" : "-"); strcat (my_argv, fopt); if (len > 1 && ((farg && *farg) || equal)) strcat (my_argv, "="); if (farg && *farg) strcat (my_argv, farg); ++(*my_argc); cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list)); cmd_line_list_tmp->next = cmd_line_list; cmd_line_list = cmd_line_list_tmp; cmd_line_list->string_arg = gengetopt_strdup(my_argv); } /* while */ if (file) fclose(file); return result;}intcmdline_parser_configfile (char * const filename, struct gengetopt_args_info *args_info, int override, int initialize, int check_required){ int i, result; int my_argc = 1; char **my_argv_arg; char *additional_error; /* store the program name */ cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list)); cmd_line_list_tmp->next = cmd_line_list; cmd_line_list = cmd_line_list_tmp; cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE); result = _cmdline_parser_configfile(filename, &my_argc); if (result != EXIT_FAILURE) { my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *)); cmd_line_list_tmp = cmd_line_list; for (i = my_argc - 1; i >= 0; --i) { my_argv_arg[i] = cmd_line_list_tmp->string_arg; cmd_line_list_tmp = cmd_line_list_tmp->next; } my_argv_arg[my_argc] = 0; additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1); strcpy (additional_error, ADDITIONAL_ERROR); strcat (additional_error, filename); result = cmdline_parser_internal (my_argc, my_argv_arg, args_info, override, initialize, check_required, additional_error); free (additional_error); free (my_argv_arg); } free_cmd_list(); if (result == EXIT_FAILURE) { cmdline_parser_free (args_info); exit (EXIT_FAILURE); } return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -