📄 dmalloc_argv.c
字号:
} len = strlen(buf); break; case ARGV_INCR: (void)loc_snprintf(buf, buf_size, "%d", *(int *)var); len = strlen(buf); break; case ARGV_SIZE: { long morf, val = *(long *)var; if (val == 0) { (void)strcpy(buf, "0"); } else if (val % (1024 * 1024 * 1024) == 0) { morf = val / (1024 * 1024 * 1024); (void)loc_snprintf(buf, buf_size, "%ldg (%ld)", morf, val); } else if (val % (1024 * 1024) == 0) { morf = val / (1024 * 1024); (void)loc_snprintf(buf, buf_size, "%ldm (%ld)", morf, val); } else if (val % 1024 == 0) { morf = val / 1024; (void)loc_snprintf(buf, buf_size, "%ldk (%ld)", morf, val); } else { (void)loc_snprintf(buf, buf_size, "%ld", val); } len = strlen(buf); } break; case ARGV_U_SIZE: { unsigned long morf, val = *(unsigned long *)var; if (val == 0) { (void)strcpy(buf, "0"); } else if (val % (1024 * 1024 * 1024) == 0) { morf = val / (1024 * 1024 * 1024); (void)loc_snprintf(buf, buf_size, "%ldg (%ld)", morf, val); } else if (val % (1024 * 1024) == 0) { morf = val / (1024 * 1024); (void)loc_snprintf(buf, buf_size, "%ldm (%ld)", morf, val); } else if (val % 1024 == 0) { morf = val / 1024; (void)loc_snprintf(buf, buf_size, "%ldk (%ld)", morf, val); } else { (void)loc_snprintf(buf, buf_size, "%ld", val); } len = strlen(buf); } break; case ARGV_BOOL_INT: case ARGV_BOOL_INT_NEG: case ARGV_BOOL_INT_ARG: if (*(int *)var) { strncpy(buf, "true (! 0)", buf_size); } else { strncpy(buf, "false (0)", buf_size); } buf[buf_size - 1] = '\0'; len = strlen(buf); break; default: strncpy(buf, "(unknown)", buf_size); buf[buf_size - 1] = '\0'; len = strlen(buf); break; } return len;}/* * static void display_variables * * DESCRIPTION: * * Display all of the variable values from our array. * * RETURNS: * * None. * * ARGUMENTS: * * args - Array of argv_t structures whose variables we are * displaying. */static void display_variables(const argv_t *args){ const argv_t *arg_p; argv_type_t *type_p; char buf[256]; int len, col_c; unsigned int val_type; /* run through the argument structure */ for (arg_p = args; arg_p->ar_short_arg != ARGV_LAST; arg_p++) { val_type = ARGV_TYPE(arg_p->ar_type); /* skip or specifiers */ if (arg_p->ar_short_arg == ARGV_OR || arg_p->ar_short_arg == ARGV_XOR) { continue; } col_c = 0; if (arg_p->ar_short_arg == '\0') { if (arg_p->ar_long_arg != NULL) { len = COMMENT_COLUMN - col_c - (LONG_PREFIX_LENGTH + 1); if (arg_p->ar_short_arg != '\0') { (void)fprintf(argv_error_stream, "%s", LONG_LABEL); col_c += LONG_LABEL_LENGTH; len -= LONG_LABEL_LENGTH; } (void)fprintf(argv_error_stream, "%s%-.*s", LONG_PREFIX, len, arg_p->ar_long_arg); col_c += LONG_PREFIX_LENGTH + MIN(len, (int)strlen(arg_p->ar_long_arg)); } } else if (arg_p->ar_short_arg == ARGV_MAND) { display_arg(argv_error_stream, arg_p, COMMENT_COLUMN, &col_c); } else { /* ARGV_MAYBE handled here */ display_option(argv_error_stream, arg_p, &col_c); } /* put the type in the correct column */ if (col_c < LONG_COLUMN) { (void)fprintf(argv_error_stream, "%*.*s", LONG_COLUMN - col_c, LONG_COLUMN - col_c, ""); col_c = LONG_COLUMN; } /* find the type */ type_p = NULL; for (type_p = argv_types; type_p->at_value != 0; type_p++) { if (type_p->at_value == ARGV_TYPE(arg_p->ar_type)) { int tlen; len = COMMENT_COLUMN - col_c - 1; tlen = strlen(type_p->at_name); (void)fprintf(argv_error_stream, " %-.*s", len, type_p->at_name); col_c += MIN(len, tlen); if (arg_p->ar_type & ARGV_FLAG_ARRAY) { (void)fprintf(argv_error_stream, "%s", ARRAY_LABEL); col_c += sizeof(ARRAY_LABEL) - 1; } break; } } if (col_c < COMMENT_COLUMN) { (void)fprintf(argv_error_stream, "%*.*s", COMMENT_COLUMN - col_c, COMMENT_COLUMN - col_c, ""); col_c = COMMENT_COLUMN; } if (arg_p->ar_type & ARGV_FLAG_ARRAY) { argv_array_t *arr_p; int entry_c, size = 0; /* find the type and the size for array */ if (type_p == NULL) { (void)fprintf(argv_error_stream, "%s: illegal variable type %d\n", __FILE__, val_type); continue; } size = type_p->at_size; arr_p = (argv_array_t *)arg_p->ar_variable; if (arr_p->aa_entry_n == 0) { (void)fprintf(argv_error_stream, "no entries"); } else { for (entry_c = 0; entry_c < arr_p->aa_entry_n; entry_c++) { ARGV_PNT var; if (entry_c > 0) { (void)fputc(',', argv_error_stream); } var = (char *)(arr_p->aa_entries) + entry_c * size; len = value_to_string(var, val_type, buf, sizeof(buf)); (void)fwrite(buf, sizeof(char), len, argv_error_stream); } } } else { len = value_to_string(arg_p->ar_variable, val_type, buf, sizeof(buf)); (void)fwrite(buf, sizeof(char), len, argv_error_stream); } (void)fputc('\n', argv_error_stream); }}/************************** checking used arguments **************************//* * static int check_or * * DESCRIPTION: * * Check out if an argument has an ARGV_OR attached to it and both * variables have not been set. * * RETURNS: * * Success - 0 * * Faulure - -1 * * ARGUMENTS: * * args - Array of argv_t structures that we are checking. * * which_p - Pointer to the specific argument that we are checking for * the ARGV_OR. */static int check_or(const argv_t *args, const argv_t *which_p){ const argv_t *arg_p, *match_p = NULL; /* check ORs below */ for (arg_p = which_p - 2; arg_p >= args; arg_p -= 2) { if ((arg_p + 1)->ar_short_arg != ARGV_OR && (arg_p + 1)->ar_short_arg != ARGV_XOR) { break; } if (arg_p->ar_type & ARGV_FLAG_USED) { match_p = arg_p; break; } } /* check ORs above */ if (match_p == NULL) { /* NOTE: we assume that which_p is not pointing now to ARGV_LAST */ for (arg_p = which_p + 2; arg_p->ar_short_arg != ARGV_LAST && (arg_p - 1)->ar_short_arg != ARGV_LAST; arg_p += 2) { if ((arg_p - 1)->ar_short_arg != ARGV_OR && (arg_p - 1)->ar_short_arg != ARGV_XOR) { break; } if (arg_p->ar_type & ARGV_FLAG_USED) { match_p = arg_p; break; } } } /* did we not find a problem? */ if (match_p == NULL) { return NOERROR; } if (argv_error_stream == NULL) { return ERROR; } (void)fprintf(argv_error_stream, "%s: %s, specify only one of the following:\n", argv_program, USAGE_ERROR_NAME); /* little hack to print the one that matched and the one we were checking */ for (;;) { if (match_p->ar_long_arg == NULL) { (void)fprintf(argv_error_stream, "%*.*s%s%c\n", (int)USAGE_LABEL_LENGTH, (int)USAGE_LABEL_LENGTH, "", SHORT_PREFIX, match_p->ar_short_arg); } else { (void)fprintf(argv_error_stream, "%*.*s%s%c (%s%s)\n", (int)USAGE_LABEL_LENGTH, (int)USAGE_LABEL_LENGTH, "", SHORT_PREFIX, match_p->ar_short_arg, LONG_PREFIX, match_p->ar_long_arg); } if (match_p == which_p) { break; } match_p = which_p; } return ERROR;}/* * static int check_xor * * DESCRIPTION: * * Check out if an argument has an ARGV_XOR attached to it and that at * least one but not both variables have been set. * * RETURNS: * * Success - 0 * * Faulure - -1 * * ARGUMENTS: * * args - Array of argv_t structures that we are checking. * * which_p - Pointer to the specific argument that we are checking for * the ARGV_XOR. */static int check_xor(const argv_t *args){ const argv_t *start_p = NULL, *arg_p; /* run through the list of arguments */ for (arg_p = args; arg_p->ar_short_arg != ARGV_LAST; arg_p++) { /* only check the XORs */ if (arg_p->ar_short_arg != ARGV_XOR) { continue; } start_p = arg_p; /* * NOTE: we are guaranteed that we are on a XOR so there is * something below and above... */ if ((arg_p - 1)->ar_type & ARGV_FLAG_USED) { start_p = NULL; } /* run through all XORs */ for (;;) { arg_p++; if (arg_p->ar_type & ARGV_FLAG_USED) { start_p = NULL; } if ((arg_p + 1)->ar_short_arg != ARGV_XOR) { break; } arg_p++; } /* were none of the xor's filled? */ if (start_p != NULL) { break; } } /* did we not find a problem? */ if (start_p == NULL) { return NOERROR; } /* arg_p points to the first XOR which failed */ if (argv_error_stream == NULL) { return ERROR; } (void)fprintf(argv_error_stream, "%s: %s, must specify one of:\n", argv_program, USAGE_ERROR_NAME); for (arg_p = start_p;; arg_p += 2) { /* * NOTE: we are guaranteed that we are on a XOR so there is * something below and above... */ (void)fprintf(argv_error_stream, "%*.*s%s%c", (int)USAGE_LABEL_LENGTH, (int)USAGE_LABEL_LENGTH, "", SHORT_PREFIX, (arg_p - 1)->ar_short_arg); if ((arg_p - 1)->ar_long_arg != NULL) { (void)fprintf(argv_error_stream, " (%s%s)", LONG_PREFIX, (arg_p - 1)->ar_long_arg); } (void)fprintf(argv_error_stream, "\n"); if (arg_p->ar_short_arg != ARGV_XOR) { break; } } return ERROR;}/* * static int check_mand * * DESCRIPTION: * * Verify that all of the mandatory arguments in our array have been * specified. * * RETURNS: * * Success - 0 * * Faulure - -1 * * ARGUMENTS: * * args - Array of argv_t structures that we are checking. */static int check_mand(const argv_t *args){ const argv_t *arg_p; int mand_c = 0, flag_c = 0; /* see if there are any mandatory args left */ for (arg_p = args; arg_p->ar_short_arg != ARGV_LAST; arg_p++) { if (arg_p->ar_short_arg == ARGV_MAND && (! (arg_p->ar_type & ARGV_FLAG_USED))) { mand_c++; } if (arg_p->ar_type & ARGV_FLAG_MAND && (! (arg_p->ar_type & ARGV_FLAG_USED))) { flag_c++; if (argv_error_stream != NULL) { if (flag_c == 1) { (void)fprintf(argv_error_stream, "%s: %s, these mandatory flags must be specified:\n", argv_program, USAGE_ERROR_NAME); } (void)fprintf(argv_error_stream, "%*.*s%s%c", (int)USAGE_LABEL_LENGTH, (int)USAGE_LABEL_LENGTH, "", SHORT_PREFIX, arg_p->ar_short_arg); if (arg_p->ar_long_arg != NULL) { (void)fprintf(argv_error_stream, " (%s%s)", LONG_PREFIX, arg_p->ar_long_arg); } (void)fprintf(argv_error_stream, "\n"); } } } if (mand_c > 0 && argv_error_stream != NULL) { (void)fprintf(argv_error_stream, "%s: %s, %d more mandatory argument%s must be specified\n", argv_program, USAGE_ERROR_NAME, mand_c, (mand_c == 1 ? "" : "s")); } if (mand_c > 0 || flag_c > 0) { return ERROR; } else { return NOERROR; }}/* * static int check_opt * * DESCRIPTION: * * Check for any missing argument options. * * RETURNS: * * Success - 0 * * Faulure - -1 * * ARGUMENTS: * * queue_head - Head of the option queue. * * queue_tail - Tail of the option queue. */static int check_opt(const int queue_head, const int queue_tail){ int queue_c; queue_c = queue_head - queue_tail; if (queue_c > 0) { if (argv_error_stream != NULL) { (void)fprintf(argv_error_stream, "%s: %s, %d more option-argument%s must be specified\n", argv_program, USAGE_ERROR_NAME, queue_c, (queue_c == 1 ? "" : "s")); } return ERROR; } return NOERROR;}/**************************** argument processing ****************************//* * static void file_args * * DESCRIPTION: * * Read in arguments from a file and process them like they were * specified on the command line. * * RETURNS: * * Success - 0 * * Faulure - -1 * * ARGUMENTS: * * path -> File of the arguments we are reading in. * * grid -> Array of argv_t structures we are using. * * queue_list <-> Our option queue for storing options to arguments. * * queue_head_p <-> Pointer to integer which will be updated with the * head position in our option queue. * * queue_tail_p <-> Pointer to integer which will be updated with the * tail position in our option queue. * * okay_bp <- Pointer to an integer which is set with 0 if the * arguments specified in the env variable are somehow invalid. */static void file_args(const char *path, argv_t *grid, argv_t **queue_list, int *queue_head_p, int *queue_tail_p, int *okay_bp){ char **argv, **argv_p; int arg_c, max; FILE *infile; char line[FILE_LINE_SIZE + 1], *line_p; /* open the input file */ infile = fopen(path, "r");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -