diff options
Diffstat (limited to 'src/escputil/escputil.c')
-rw-r--r-- | src/escputil/escputil.c | 178 |
1 files changed, 100 insertions, 78 deletions
diff --git a/src/escputil/escputil.c b/src/escputil/escputil.c index ffe4d19..2abec6e 100644 --- a/src/escputil/escputil.c +++ b/src/escputil/escputil.c @@ -1,5 +1,5 @@ /* - * "$Id: escputil.c,v 1.104 2010/12/11 22:04:07 rlk Exp $" + * "$Id: escputil.c,v 1.108 2012/01/19 13:26:57 m0m Exp $" * * Printer maintenance utility for EPSON Stylus (R) printers * @@ -264,7 +264,7 @@ exit_packet_mode_old(int do_init) { static char hdr[] = "\000\000\000\033\001@EJL 1284.4\n@EJL \n\033@"; memcpy(printer_cmd + bufpos, hdr, sizeof(hdr) - 1); /* DON'T include null! */ - STP_DEBUG(printf("Exit packet mode (%d)\n", do_init)); + STP_DEBUG(printf("***Exit packet mode (%d)\n", do_init)); bufpos += sizeof(hdr) - 1; if (!do_init) bufpos -= 2; @@ -274,7 +274,7 @@ static void initialize_print_cmd(int do_init) { bufpos = 0; - STP_DEBUG(printf("Initialize print command\n")); + STP_DEBUG(printf("***Initialize print command\n")); if (isnew) exit_packet_mode_old(do_init); } @@ -283,7 +283,7 @@ static void initialize_print_cmd_new(int do_init) { bufpos = 0; - STP_DEBUG(printf("Initialize print command (force new)\n")); + STP_DEBUG(printf("***Initialize print command (force new)\n")); exit_packet_mode_old(do_init); } @@ -445,7 +445,7 @@ print_debug_data(const char *buf, size_t count) for (i = 0; i < count; i++) { if (i % 16 == 0) - printf("\n%4d: ", i); + printf("\n***%4d: ", i); else if (i % 4 == 0) printf(" "); if (isgraph(buf[i])) @@ -498,7 +498,7 @@ do_print_cmd(void) return 1; } } - STP_DEBUG(printf("Sending print command to %s:", + STP_DEBUG(printf("***Sending print command to %s:", raw_device ? raw_device : command)); STP_DEBUG(print_debug_data(printer_cmd, bufpos)); while (bytes < bufpos) @@ -573,7 +573,7 @@ read_from_printer(int fd, char *buf, int bufsize, int quiet) if (status > 0) { - STP_DEBUG(printf("read_from_printer returns %d\n", status)); + STP_DEBUG(printf("***read_from_printer returns %d\n", status)); STP_DEBUG(print_debug_data(buf, status)); } else if (status == 0 && retry == 0) @@ -596,7 +596,7 @@ start_remote_sequence(void) static char remote_hdr[] = "\033@\033(R\010\000\000REMOTE1"; memcpy(printer_cmd + bufpos, remote_hdr, sizeof(remote_hdr) - 1); bufpos += sizeof(remote_hdr) - 1; - STP_DEBUG(printf("Start remote sequence\n")); + STP_DEBUG(printf("***Start remote sequence\n")); } static void @@ -605,7 +605,7 @@ end_remote_sequence(void) static char remote_trailer[] = "\033\000\000\000\033\000"; memcpy(printer_cmd + bufpos, remote_trailer, sizeof(remote_trailer) - 1); bufpos += sizeof(remote_trailer) - 1; - STP_DEBUG(printf("End remote sequence\n")); + STP_DEBUG(printf("***End remote sequence\n")); } static void @@ -617,7 +617,7 @@ do_remote_cmd(const char *cmd, int nargs, ...) start_remote_sequence(); memcpy(printer_cmd + bufpos, cmd, 2); - STP_DEBUG(printf("Remote command: %s", cmd)); + STP_DEBUG(printf("***Remote command: %s", cmd)); bufpos += 2; printer_cmd[bufpos] = nargs % 256; printer_cmd[bufpos + 1] = (nargs >> 8) % 256; @@ -634,6 +634,7 @@ do_remote_cmd(const char *cmd, int nargs, ...) STP_DEBUG(printf("\n")); bufpos += 2 + nargs; end_remote_sequence(); + va_end(args); } static void @@ -644,7 +645,7 @@ do_remote_cmd_only(const char *cmd, int nargs, ...) va_start(args, nargs); memcpy(printer_cmd + bufpos, cmd, 2); - STP_DEBUG(printf("Remote command: %s", cmd)); + STP_DEBUG(printf("***Remote command: %s", cmd)); bufpos += 2; printer_cmd[bufpos] = nargs % 256; printer_cmd[bufpos + 1] = (nargs >> 8) % 256; @@ -660,13 +661,14 @@ do_remote_cmd_only(const char *cmd, int nargs, ...) } STP_DEBUG(printf("\n")); bufpos += 2 + nargs; + va_end(args); } static void add_resets(int count) { int i; - STP_DEBUG(printf("Add %d resets\n", count)); + STP_DEBUG(printf("***Add %d resets\n", count)); for (i = 0; i < count; i++) { printer_cmd[bufpos++] = '\033'; @@ -674,7 +676,7 @@ add_resets(int count) } } -static int +static void send_nulls(int fd) { char buf[16384]; @@ -687,35 +689,35 @@ init_packet(int fd, int force) { int status; int tries = 0; - STP_DEBUG(printf("Init packet mode %d\n", force)); + STP_DEBUG(printf("***Init packet mode %d\n", force)); Loop: if (!force) { - STP_DEBUG(printf("Flushing early data...\n")); + STP_DEBUG(printf("***Flushing early data...\n")); flushData(fd, (unsigned char) -1); } - STP_DEBUG(printf("EnterIEEE...\n")); + STP_DEBUG(printf("***EnterIEEE...\n")); if (!EnterIEEE(fd)) { - STP_DEBUG(printf("EnterIEEE failed!\n")); + STP_DEBUG(printf("***EnterIEEE failed!\n")); if (tries++ < 5) { - STP_DEBUG(printf("Retrying\n")); + STP_DEBUG(printf("***Retrying\n")); send_nulls(fd); flushData(fd, (unsigned char) -1); goto Loop; } return 1; } - STP_DEBUG(printf("Init...\n")); + STP_DEBUG(printf("***Init...\n")); if (!Init(fd)) { - STP_DEBUG(printf("Init failed!\n")); + STP_DEBUG(printf("***Init failed!\n")); if (tries++ < 5) { - STP_DEBUG(printf("Retrying\n")); + STP_DEBUG(printf("***Retrying\n")); send_nulls(fd); flushData(fd, (unsigned char) -1); goto Loop; @@ -723,28 +725,28 @@ init_packet(int fd, int force) return 1; } - STP_DEBUG(printf("GetSocket...\n")); + STP_DEBUG(printf("***GetSocket...\n")); socket_id = GetSocketID(fd, "EPSON-CTRL"); if (!socket_id) { - STP_DEBUG(printf("GetSocket failed!\n")); + STP_DEBUG(printf("***GetSocket failed!\n")); return 1; } - STP_DEBUG(printf("OpenChannel...\n")); + STP_DEBUG(printf("***OpenChannel...\n")); switch ( OpenChannel(fd, socket_id, &send_size, &receive_size) ) { case -1: - STP_DEBUG(printf("Fatal Error return 1\n")); + STP_DEBUG(printf("***Fatal Error return 1\n")); return 1; /* unrecoverable error */ break; case 0: - STP_DEBUG(printf("Error\n")); /* recoverable error ? */ + STP_DEBUG(printf("***Error\n")); /* recoverable error ? */ return 1; break; } status = 1; - STP_DEBUG(printf("Flushing data...\n")); + STP_DEBUG(printf("***Flushing data...\n")); flushData(fd, socket_id); return 0; } @@ -813,7 +815,7 @@ set_printer_model(void) if (desc.p_type == STP_PARAMETER_TYPE_BOOLEAN) interchangeable_inks = desc.deflt.boolean; stp_parameter_description_destroy(&desc); - STP_DEBUG(printf("Found it! %s\n", printer_model)); + STP_DEBUG(printf("***Found it! %s\n", printer_model)); return; } } @@ -870,14 +872,14 @@ initialize_printer(int quiet, int fail_if_not_found) status = SafeWrite(fd, init_str, sizeof(init_str) - 1); alarm(0); signal(SIGALRM, SIG_DFL); - STP_DEBUG(printf("status %d alarm %d\n", status, alarm_interrupt)); + STP_DEBUG(printf("***status %d alarm %d\n", status, alarm_interrupt)); if (status != sizeof(init_str) - 1 && (status != -1 || !alarm_interrupt)) { fprintf(stderr, _("Cannot write to %s: %s\n"), raw_device, strerror(errno)); exit(1); } - STP_DEBUG(printf("Try old command %d alarm %d\n", + STP_DEBUG(printf("***Try old command %d alarm %d\n", tries, alarm_interrupt)); status = read_from_printer(fd, (char*)buf, 1024, 1); if (status <= 0 && tries > 0) @@ -888,7 +890,7 @@ initialize_printer(int quiet, int fail_if_not_found) if (!forced_packet_mode && status > 0 && !strstr((char *) buf, "@EJL ID") && tries < 3) { - STP_DEBUG(printf("Found bad data: %s\n", buf)); + STP_DEBUG(printf("***Found bad data: %s\n", buf)); /* * We know the printer's not dead. Try to turn off status * and try again. @@ -914,7 +916,7 @@ initialize_printer(int quiet, int fail_if_not_found) (void) SafeWrite(fd, printer_cmd, bufpos); flushData(fd, (unsigned char) -1); forced_packet_mode = !init_packet(fd, 1); - STP_DEBUG(printf("Printer in packet mode....\n")); + STP_DEBUG(printf("***Printer in packet mode....\n")); packet_initialized = 1; isnew = 1; /* request status command */ @@ -930,24 +932,38 @@ initialize_printer(int quiet, int fail_if_not_found) return NULL; } } - STP_DEBUG(printf("status: %i\n", status)); - STP_DEBUG(printf("Buf: %s\n", buf)); + STP_DEBUG(printf("***status: %i\n", status)); + STP_DEBUG(printf("***Buf: %s\n", buf)); if (status > 0) { + char *xpos; pos = strstr((char*)buf, "@EJL ID"); - STP_DEBUG(printf("pos: %s\n", pos ? pos : "(null)")); - if (pos) - pos = strchr(pos, (int) ';'); - STP_DEBUG(printf("pos: %s\n", pos ? pos : "(null)")); - if (pos) - pos = strchr(pos + 1, (int) ';'); - STP_DEBUG(printf("pos: %s\n", pos ? pos : "(null)")); - if (pos) - pos = strchr(pos, (int) ':'); - STP_DEBUG(printf("pos: %s\n", pos ? pos : "(null)")); + STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); if (pos) { - spos = strchr(pos, (int) ';'); + if (((xpos = strstr((char *)pos, "MDL:")) != NULL) || + ((xpos = strstr((char *)pos, "DES:")) != NULL)) + { + STP_DEBUG(printf("***xpos: %s\n", xpos ? xpos : "(null)")); + pos = xpos + 4; + STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); + } + else + { + if (pos) + pos = strchr(pos, (int) ';'); + STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); + if (pos) + pos = strchr(pos + 1, (int) ';'); + STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); + if (pos) + pos = strchr(pos, (int) ':'); + STP_DEBUG(printf("***pos: %s\n", pos ? pos : "(null)")); + if (pos) + pos++; + } + if (pos) + spos = strchr(pos, (int) ';'); } if (!pos) { @@ -972,12 +988,12 @@ initialize_printer(int quiet, int fail_if_not_found) * However, make it clear that this is a dummy, * so we don't actually try to print it out. */ - STP_DEBUG(printf("Can't find printer name, assuming Stylus Photo\n")); + STP_DEBUG(printf("***Can't find printer name, assuming Stylus Photo\n")); printer_model = c_strdup("escp2-photo"); } else { - STP_DEBUG(printf("Can't get response to @EJL ID\n")); + STP_DEBUG(printf("***Can't get response to @EJL ID\n")); close(fd); return NULL; } @@ -986,8 +1002,8 @@ initialize_printer(int quiet, int fail_if_not_found) { if (spos) *spos = '\000'; - printer_model = pos + 1; - STP_DEBUG(printf("printer model: %s\n", printer_model)); + printer_model = pos; + STP_DEBUG(printf("***printer model: %s\n", printer_model)); } } } @@ -1000,7 +1016,7 @@ initialize_printer(int quiet, int fail_if_not_found) } close(fd); - STP_DEBUG(printf("new? %s\n", isnew ? "yes" : "no")); + STP_DEBUG(printf("***new? %s\n", isnew ? "yes" : "no")); return the_printer_t; } @@ -1013,7 +1029,7 @@ get_printer(int quiet, int fail_if_not_found) { const stp_printer_t *printer = initialize_printer(quiet, fail_if_not_found); - STP_DEBUG(printf("init done, printer found? %s...\n", + STP_DEBUG(printf("***init done, printer found? %s...\n", printer ? "yes" : "no")); return printer; } @@ -1303,7 +1319,7 @@ do_old_status(status_cmd_t cmd, const char *buf, const stp_printer_t *printer) if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST) { color_list = stp_string_list_create_copy(desc.bounds.str); - STP_DEBUG(printf("Using color list from driver (%ld %ld)\n", + STP_DEBUG(printf("***Using color list from driver (%ld %ld)\n", (long)stp_string_list_count(desc.bounds.str), (long)stp_string_list_count(color_list))); stp_parameter_description_destroy(&desc); @@ -1327,7 +1343,7 @@ do_old_status(status_cmd_t cmd, const char *buf, const stp_printer_t *printer) if (cmd == CMD_STATUS) printf("\n"); } - STP_DEBUG(printf("looking at %s\n", buf)); + STP_DEBUG(printf("***looking at %s\n", buf)); } while ((buf = find_group(buf)) != NULL); } @@ -1340,11 +1356,16 @@ do_new_status(status_cmd_t cmd, char *buf, int bytes, const char *ind; const stp_string_list_t *color_list = NULL; stp_parameter_t desc; - const stp_vars_t *printvars = stp_printer_get_defaults(printer); - stp_describe_parameter(printvars, "ChannelNames", &desc); - if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST) - color_list = desc.bounds.str; - STP_DEBUG(printf("New format bytes: %d bytes\n", bytes)); + + const stp_vars_t *printvars = NULL; + if (printer) + { + printvars = stp_printer_get_defaults(printer); + stp_describe_parameter(printvars, "ChannelNames", &desc); + if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST) + color_list = desc.bounds.str; + } + STP_DEBUG(printf("***New format bytes: %d bytes\n", bytes)); if (cmd == CMD_STATUS) printf(_("Printer Name: %s\n"), printer ? stp_printer_get_long_name(printer) : _("Unknown")); @@ -1353,7 +1374,7 @@ do_new_status(status_cmd_t cmd, char *buf, int bytes, unsigned hdr = buf[i]; unsigned total_param_count = buf[i + 1]; unsigned param = buf[i + 2]; - STP_DEBUG(printf("Header: %x param count: %d\n", hdr, total_param_count)); + STP_DEBUG(printf("***Header: %x param count: %d\n", hdr, total_param_count)); if (hdr == 0x0f) /* Always report ink */ { size_t count = (total_param_count - 1) / param; @@ -1363,26 +1384,26 @@ do_new_status(status_cmd_t cmd, char *buf, int bytes, printf("%20s %20s\n", _("Ink color"), _("Percent remaining")); for (j = 0; j < count; j++) { - STP_DEBUG(printf(" Ink %d: ind[0] %d ind[1] %d ind[2] %d interchangeable %d param %d count %d aux %d\n", + STP_DEBUG(printf("*** Ink %d: ind[0] %d ind[1] %d ind[2] %d interchangeable %d param %d count %d aux %d\n", j, ind[0], ind[1], ind[2], interchangeable_inks, param, color_count, aux_color_count)); if (ind[0] < color_count && param == 3 /* && (interchangeable_inks || ind[1] >= aux_color_count || ! aux_colors[(int) ind[1]]) */) { - STP_DEBUG(printf("Case 0\n")); + STP_DEBUG(printf("***Case 0\n")); printf("%20s %20d\n", gettext(colors_new[(int) ind[0]]), ind[2]); } else if (ind[1] < aux_color_count && aux_colors[(int) ind[1]]) { - STP_DEBUG(printf("Case 1\n")); + STP_DEBUG(printf("***Case 1\n")); printf("%20s %20d\n", gettext(aux_colors[(int) ind[1]]), ind[2]); } else { - STP_DEBUG(printf("Case 2\n")); + STP_DEBUG(printf("***Case 2\n")); printf("%8s 0x%02x 0x%02x %20d\n", _("Unknown"), (unsigned char) ind[0], (unsigned char) ind[1], ind[2]); @@ -1428,7 +1449,8 @@ do_new_status(status_cmd_t cmd, char *buf, int bytes, } i += total_param_count + 2; } - stp_parameter_description_destroy(&desc); + if (printer) + stp_parameter_description_destroy(&desc); exit(0); } @@ -1457,10 +1479,10 @@ do_status_command_internal(status_cmd_t cmd) exit(1); } - STP_DEBUG(printf("%s...\n", cmd_name)); + STP_DEBUG(printf("***%s...\n", cmd_name)); printer = get_printer(1, 0); if (!found_unknown_old_printer) - STP_DEBUG(printf("%s found %s%s\n", gettext(cmd_name), + STP_DEBUG(printf("***%s found %s%s\n", gettext(cmd_name), printer ? stp_printer_get_long_name(printer) : printer_model ? printer_model : "(null)", printer ? "" : "(Unknown model)")); @@ -1561,7 +1583,7 @@ do_extended_ink_info(int extended_output) if (printer && desc.p_type == STP_PARAMETER_TYPE_STRING_LIST) { - STP_DEBUG(printf("Using color list from driver (%ld %ld)\n", + STP_DEBUG(printf("***Using color list from driver (%ld %ld)\n", (long)stp_string_list_count(desc.bounds.str), (long)stp_string_list_count(color_list))); color_list = stp_string_list_create_copy(desc.bounds.str); @@ -1590,7 +1612,7 @@ do_extended_ink_info(int extended_output) buf[status] = '\0'; if ( buf[7] == '2' ) { - STP_DEBUG(printf("New format ink!\n")); + STP_DEBUG(printf("***New format ink!\n")); /* new binary format ! */ i = 10; while (buf[i] != 0x0f && i < status) @@ -1601,7 +1623,7 @@ do_extended_ink_info(int extended_output) { if (ind[i] < color_count) { - STP_DEBUG(printf(" Case 0: Ink %d %d (%s)\n", + STP_DEBUG(printf("*** Case 0: Ink %d %d (%s)\n", i, ind[i], colors_new[(int) ind[i]])); stp_string_list_add_string(color_list, colors_new[(int) ind[i]], @@ -1609,7 +1631,7 @@ do_extended_ink_info(int extended_output) } else if (ind[i] == 0x40 && ind[i + 1] < aux_color_count) { - STP_DEBUG(printf(" Case 1: Ink %d %d (%s)\n", + STP_DEBUG(printf("*** Case 1: Ink %d %d (%s)\n", i, ind[i+1], aux_colors[(int) ind[i+1]])); stp_string_list_add_string(color_list, aux_colors[(int) ind[i + 1]], @@ -1617,14 +1639,14 @@ do_extended_ink_info(int extended_output) } else { - STP_DEBUG(printf(" Case 2: Unknown\n")); + STP_DEBUG(printf("*** Case 2: Unknown\n")); stp_string_list_add_string(color_list, "Unknown", "Unknown"); } i+=3; } } - STP_DEBUG(printf("Using color list from status message\n")); + STP_DEBUG(printf("***Using color list from status message\n")); } for (i = 0; i < stp_string_list_count(color_list); i++) @@ -1643,7 +1665,7 @@ do_extended_ink_info(int extended_output) ind = strchr(buf, 'I'); if (!ind) { - STP_DEBUG(printf("Case 0: failure %i (%s)\n", i, buf)); + STP_DEBUG(printf("***Case 0: failure %i (%s)\n", i, buf)); printf("Cannot identify cartridge in slot %d\n", i); } else if (sscanf(ind, @@ -1658,7 +1680,7 @@ do_extended_ink_info(int extended_output) &year2, &month2, &id2) == 12) { int j; - STP_DEBUG(printf("Case 1: i %i iv %ud %ud %ud %ud %ud %ud year %ud %ud mo %ud %ud id %ud %ud\n", + STP_DEBUG(printf("***Case 1: i %i iv %ud %ud %ud %ud %ud %ud year %ud %ud mo %ud %ud id %ud %ud\n", i, iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], year, year2, month, month2, id, id2)); printf("%20s %20s %12s %7s\n", @@ -1685,7 +1707,7 @@ do_extended_ink_info(int extended_output) &year2, &month2, &id2) == 10) { int j; - STP_DEBUG(printf("Case 2: i %i iv %ud %ud %ud %ud year %ud %ud mo %ud %ud id %ud %ud\n", + STP_DEBUG(printf("***Case 2: i %i iv %ud %ud %ud %ud year %ud %ud mo %ud %ud id %ud %ud\n", i, iv[0], iv[1], iv[2], iv[3], year, year2, month, month2, id, id2)); printf("%20s %20s %12s %7s\n", @@ -1707,7 +1729,7 @@ do_extended_ink_info(int extended_output) "II:01;IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;", &val, &year, &month, &id ) == 4) { - STP_DEBUG(printf("Case 3: i %i val %ud year %ud mo %ud id %ud\n", + STP_DEBUG(printf("***Case 3: i %i val %ud year %ud mo %ud id %ud\n", i, val, year, month, id)); if (i == 0) printf("%20s %20s %12s %7s\n", @@ -1724,7 +1746,7 @@ do_extended_ink_info(int extended_output) "IQT:%x;TSH:%*4s;PDY:%x;PDM:%x;IC1:%x;IC2:%*x;IK1:%*x;IK2:%*x;TOV:%*x;TVU:%*x;LOG:INKbyEPSON;", &val, &year, &month, &id ) == 4) { - STP_DEBUG(printf("Case 4: i %i val %ud year %ud mo %ud id %ud\n", + STP_DEBUG(printf("***Case 4: i %i val %ud year %ud mo %ud id %ud\n", i, val, year, month, id)); if (i == 0) printf("%20s %20s %12s %7s\n", @@ -1736,7 +1758,7 @@ do_extended_ink_info(int extended_output) } else { - STP_DEBUG(printf("Case 5: failure %i (%s)\n", i, ind)); + STP_DEBUG(printf("***Case 5: failure %i (%s)\n", i, ind)); printf("Cannot identify cartridge in slot %d\n", i); } } |