From d2161ffb208ae9df2e313038769bbf3d57abf9e9 Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Sun, 21 Jan 2018 14:33:54 +0000 Subject: [PATCH] Make CLI errors more consistent Should now output errors and warnings in a consistent and predictable fashion. This is not a neat solution, but functional for now. --- backend/library.c | 21 +++++++++--- frontend/main.c | 83 ++++++++++++++++++++++++----------------------- 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/backend/library.c b/backend/library.c index 8cf85bca..f2d93749 100644 --- a/backend/library.c +++ b/backend/library.c @@ -1062,6 +1062,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int for (i = 0; i < in_length; i++) { if (source[i] == '\0') { strcpy(symbol->errtxt, "219: NULL characters not permitted in GS1 mode"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_DATA); return ZINT_ERROR_INVALID_DATA; } } @@ -1073,6 +1074,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int in_length =(int)ustrlen(local_source); } else { strcpy(symbol->errtxt, "220: Selected symbology does not support GS1 mode"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); return ZINT_ERROR_INVALID_OPTION; } } else { @@ -1083,6 +1085,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int if (symbol->input_mode & ESCAPE_MODE) { error_number = escape_char_process(symbol, local_source, &in_length); if (error_number != 0) { + error_tag(symbol->errtxt, error_number); return error_number; } symbol->input_mode -= ESCAPE_MODE; @@ -1102,6 +1105,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int if ((symbol->dot_size < 0.01) || (symbol->dot_size > 20.0)) { strcpy(symbol->errtxt, "221: Invalid dot size"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); return ZINT_ERROR_INVALID_OPTION; } @@ -1123,10 +1127,6 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int /* Try another ECI mode */ symbol->eci = get_best_eci(local_source, in_length); - error_number = ZINT_WARN_USES_ECI; - strcpy(symbol->errtxt, "222: Encoded data includes ECI codes"); - //printf("Data will encode with ECI %d\n", symbol->eci); - switch (symbol->symbology) { case BARCODE_QRCODE: case BARCODE_MICROQR: @@ -1139,7 +1139,12 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int error_number = reduced_charset(symbol, local_source, in_length); break; } - + + if (error_number == 0) { + error_number = ZINT_WARN_USES_ECI; + strcpy(symbol->errtxt, "222: Encoded data includes ECI"); + if (symbol->debug) printf("Data ECI %d\n", symbol->eci); + } } if (error_number == 0) { @@ -1174,12 +1179,14 @@ int ZBarcode_Print(struct zint_symbol *symbol, int rotate_angle) { break; default: strcpy(symbol->errtxt, "223: Invalid rotation angle"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); return ZINT_ERROR_INVALID_OPTION; } if (symbol->output_options & BARCODE_DOTTY_MODE) { if (!(is_matrix(symbol->symbology))) { strcpy(symbol->errtxt, "224: Selected symbology cannot be rendered as dots"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); return ZINT_ERROR_INVALID_OPTION; } } @@ -1264,6 +1271,7 @@ int ZBarcode_Buffer(struct zint_symbol *symbol, int rotate_angle) { break; default: strcpy(symbol->errtxt, "228: Invalid rotation angle"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); return ZINT_ERROR_INVALID_OPTION; } @@ -1310,6 +1318,7 @@ int ZBarcode_Encode_File(struct zint_symbol *symbol, char *filename) { file = fopen(filename, "rb"); if (!file) { strcpy(symbol->errtxt, "229: Unable to read input file"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); return ZINT_ERROR_INVALID_DATA; } @@ -1321,6 +1330,7 @@ int ZBarcode_Encode_File(struct zint_symbol *symbol, char *filename) { if (fileLen > 7100) { /* The largest amount of data that can be encoded is 7089 numeric digits in QR Code */ strcpy(symbol->errtxt, "230: Input file too long"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_DATA); fclose(file); return ZINT_ERROR_INVALID_DATA; } @@ -1330,6 +1340,7 @@ int ZBarcode_Encode_File(struct zint_symbol *symbol, char *filename) { buffer = (unsigned char *) malloc(fileLen * sizeof (unsigned char)); if (!buffer) { strcpy(symbol->errtxt, "231: Internal memory error"); + error_tag(symbol->errtxt, ZINT_ERROR_MEMORY); if (strcmp(filename, "-")) fclose(file); return ZINT_ERROR_MEMORY; diff --git a/frontend/main.c b/frontend/main.c index c20888cf..a33d8719 100644 --- a/frontend/main.c +++ b/frontend/main.c @@ -344,7 +344,7 @@ int batch_process(struct zint_symbol *symbol, char *filename, int mirror_mode, c posn++; } if (posn > 7090) { - fprintf(stderr, "103 on line %d: Input data too long\n", line_count); + fprintf(stderr, "On line %d: Error 103: Input data too long\n", line_count); fflush(stderr); do { character = fgetc(file); @@ -353,7 +353,7 @@ int batch_process(struct zint_symbol *symbol, char *filename, int mirror_mode, c } while ((!feof(file)) && (line_count < 2000000000)); if (character != '\n') { - fprintf(stderr, "warning 104: No newline at end of file\n"); + fprintf(stderr, "Warning 104: No newline at end of file\n"); fflush(stderr); } @@ -503,7 +503,7 @@ int main(int argc, char **argv) { my_symbol->scale = (float) (atof(optarg)); if (my_symbol->scale < 0.01) { /* Zero and negative values are not permitted */ - fprintf(stderr, "105: Invalid scale value\n"); + fprintf(stderr, "Warning 105: Invalid scale value\n"); fflush(stderr); my_symbol->scale = 1.0; } @@ -512,7 +512,7 @@ int main(int argc, char **argv) { my_symbol->dot_size = (float) (atof(optarg)); if (my_symbol->dot_size < 0.01) { /* Zero and negative values are not permitted */ - fprintf(stderr, "106: Invalid dot radius value\n"); + fprintf(stderr, "Warning 106: Invalid dot radius value\n"); fflush(stderr); my_symbol->dot_size = 4.0F / 5.0F; } @@ -520,26 +520,26 @@ int main(int argc, char **argv) { if (!strcmp(long_options[option_index].name, "border")) { error_number = validator(NESET, optarg); if (error_number == ZINT_ERROR_INVALID_DATA) { - fprintf(stderr, "107: Invalid border width\n"); + fprintf(stderr, "Error 107: Invalid border width\n"); exit(1); } if ((atoi(optarg) >= 0) && (atoi(optarg) <= 1000)) { my_symbol->border_width = atoi(optarg); } else { - fprintf(stderr, "108: Border width out of range\n"); + fprintf(stderr, "Warning 108: Border width out of range\n"); fflush(stderr); } } if (!strcmp(long_options[option_index].name, "height")) { error_number = validator(NESET, optarg); if (error_number == ZINT_ERROR_INVALID_DATA) { - fprintf(stderr, "109: Invalid symbol height\n"); + fprintf(stderr, "Error 109: Invalid symbol height\n"); exit(1); } if ((atoi(optarg) >= 1) && (atoi(optarg) <= 1000)) { my_symbol->height = atoi(optarg); } else { - fprintf(stderr, "110: Symbol height out of range\n"); + fprintf(stderr, "Warning 110: Symbol height out of range\n"); fflush(stderr); } } @@ -548,7 +548,7 @@ int main(int argc, char **argv) { if ((atoi(optarg) >= 1) && (atoi(optarg) <= 66)) { my_symbol->option_2 = atoi(optarg); } else { - fprintf(stderr, "111: Number of columns out of range\n"); + fprintf(stderr, "Warning 111: Number of columns out of range\n"); fflush(stderr); } } @@ -556,7 +556,7 @@ int main(int argc, char **argv) { if ((atoi(optarg) >= 1) && (atoi(optarg) <= 44)) { my_symbol->option_1 = atoi(optarg); } else { - fprintf(stderr, "112: Number of rows out of range\n"); + fprintf(stderr, "Warning 112: Number of rows out of range\n"); fflush(stderr); } } @@ -564,7 +564,7 @@ int main(int argc, char **argv) { if ((atoi(optarg) >= 1) && (atoi(optarg) <= 84)) { my_symbol->option_2 = atoi(optarg); } else { - fprintf(stderr, "113: Invalid Version\n"); + fprintf(stderr, "Warning 113: Invalid Version\n"); fflush(stderr); } } @@ -572,7 +572,7 @@ int main(int argc, char **argv) { if ((atoi(optarg) >= 1) && (atoi(optarg) <= 8)) { my_symbol->option_1 = atoi(optarg); } else { - fprintf(stderr, "114: ECC level out of range\n"); + fprintf(stderr, "Warning 114: ECC level out of range\n"); fflush(stderr); } } @@ -580,7 +580,7 @@ int main(int argc, char **argv) { if (strlen(optarg) <= 90) { strcpy(my_symbol->primary, optarg); } else { - fprintf(stderr, "115: Primary data string too long"); + fprintf(stderr, "Error 115: Primary data string too long"); fflush(stderr); } } @@ -588,7 +588,7 @@ int main(int argc, char **argv) { if ((optarg[0] >= '0') && (optarg[0] <= '6')) { my_symbol->option_1 = optarg[0] - '0'; } else { - fprintf(stderr, "116: Invalid mode\n"); + fprintf(stderr, "Warning 116: Invalid mode\n"); fflush(stderr); } } @@ -596,7 +596,7 @@ int main(int argc, char **argv) { /* Only certain inputs allowed */ error_number = validator(NESET, optarg); if (error_number == ZINT_ERROR_INVALID_DATA) { - fprintf(stderr, "117: Invalid rotation parameter\n"); + fprintf(stderr, "Error 117: Invalid rotation parameter\n"); exit(1); } switch (atoi(optarg)) { @@ -626,7 +626,7 @@ int main(int argc, char **argv) { if ((atoi(optarg) >= 0) && (atoi(optarg) <= 999999)) { my_symbol->eci = atoi(optarg); } else { - fprintf(stderr, "118: Invalid ECI code\n"); + fprintf(stderr, "Warning 118: Invalid ECI code\n"); fflush(stderr); } } @@ -655,7 +655,7 @@ int main(int argc, char **argv) { case 'b': error_number = validator(NESET, optarg); if (error_number == ZINT_ERROR_INVALID_DATA) { - fprintf(stderr, "119: Invalid barcode type\n"); + fprintf(stderr, "Error 119: Invalid barcode type\n"); exit(1); } my_symbol->symbology = atoi(optarg); @@ -664,13 +664,13 @@ int main(int argc, char **argv) { case 'w': error_number = validator(NESET, optarg); if (error_number == ZINT_ERROR_INVALID_DATA) { - fprintf(stderr, "120: Invalid whitespace value\n"); + fprintf(stderr, "Error 120: Invalid whitespace value\n"); exit(1); } if ((atoi(optarg) >= 0) && (atoi(optarg) <= 1000)) { my_symbol->whitespace_width = atoi(optarg); } else { - fprintf(stderr, "121: Whitespace value out of range"); + fprintf(stderr, "Warning 121: Whitespace value out of range"); fflush(stderr); } break; @@ -682,22 +682,23 @@ int main(int argc, char **argv) { strcat(my_symbol->outfile, filetype); } error_number = ZBarcode_Encode(my_symbol, (unsigned char*) optarg, strlen(optarg)); - if (error_number < 5) { + generated = 1; + if (error_number != 0) { + fprintf(stderr, "%s\n", my_symbol->errtxt); + fflush(stderr); + } + if (error_number < 5) { + error_number = ZBarcode_Print(my_symbol, rotate_angle); + if (error_number != 0) { fprintf(stderr, "%s\n", my_symbol->errtxt); fflush(stderr); + ZBarcode_Delete(my_symbol); + return 1; } - error_number = ZBarcode_Print(my_symbol, rotate_angle); - } - generated = 1; - if (error_number != 0) { - fprintf(stderr, "%s\n", my_symbol->errtxt); - fflush(stderr); - ZBarcode_Delete(my_symbol); - return 1; } } else { - fprintf(stderr, "122: Cannot define data in batch mode"); + fprintf(stderr, "Warning 122: Can't define data in batch mode"); fflush(stderr); } break; @@ -705,19 +706,19 @@ int main(int argc, char **argv) { case 'i': /* Take data from file */ if (batch_mode == 0) { error_number = ZBarcode_Encode_File(my_symbol, optarg); - if (error_number < 5) { - if (error_number != 0) { - fprintf(stderr, "%s\n", my_symbol->errtxt); - fflush(stderr); - } - error_number = ZBarcode_Print(my_symbol, rotate_angle); - } generated = 1; if (error_number != 0) { fprintf(stderr, "%s\n", my_symbol->errtxt); fflush(stderr); - ZBarcode_Delete(my_symbol); - return 1; + } + if (error_number < 5) { + error_number = ZBarcode_Print(my_symbol, rotate_angle); + if (error_number != 0) { + fprintf(stderr, "%s\n", my_symbol->errtxt); + fflush(stderr); + ZBarcode_Delete(my_symbol); + return 1; + } } } else { /* Take each line of text as a separate data set */ @@ -748,13 +749,13 @@ int main(int argc, char **argv) { break; default: - fprintf(stderr, "123: ?? getopt error 0%o\n", c); + fprintf(stderr, "Error 123: ?? getopt error 0%o\n", c); fflush(stderr); } } if (optind < argc) { - fprintf(stderr, "125: Invalid option\n"); + fprintf(stderr, "Error 125: Invalid option\n"); while (optind < argc) fprintf(stderr, "%s", argv[optind++]); fprintf(stderr, "\n"); @@ -762,7 +763,7 @@ int main(int argc, char **argv) { } if (generated == 0) { - fprintf(stderr, "124: No data received, no symbol generated\n"); + fprintf(stderr, "Warning 124: No data received, no symbol generated\n"); fflush(stderr); }