1
0
mirror of https://git.familie-radermacher.ch/linux/ptouch-print.git synced 2025-06-28 03:57:02 +00:00

fix segfault for last commit

This commit is contained in:
Dominic Radermacher 2025-05-19 13:33:55 +02:00
parent 140bf0a6cc
commit 2f18522602
2 changed files with 58 additions and 21 deletions

View File

@ -163,6 +163,9 @@ int ptouch_open(ptouch_dev *ptdev)
int ptouch_close(ptouch_dev ptdev) int ptouch_close(ptouch_dev ptdev)
{ {
if (!ptdev) {
return -1;
}
libusb_release_interface(ptdev->h, 0); libusb_release_interface(ptdev->h, 0);
libusb_close(ptdev->h); libusb_close(ptdev->h);
return 0; return 0;
@ -172,7 +175,11 @@ int ptouch_send(ptouch_dev ptdev, uint8_t *data, size_t len)
{ {
int r, tx; int r, tx;
if ((ptdev == NULL) || (len > 128)) { if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_send() with NULL ptdev\n"));
return -1;
}
if (len > 128) {
return -1; return -1;
} }
if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, (int)len, &tx, 0)) != 0) { if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, (int)len, &tx, 0)) != 0) {
@ -228,6 +235,11 @@ int ptouch_enable_packbits(ptouch_dev ptdev)
/* print information command */ /* print information command */
int ptouch_info_cmd(ptouch_dev ptdev, int size_x) int ptouch_info_cmd(ptouch_dev ptdev, int size_x)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_info_cmd() with NULL ptdev\n"));
return -1;
}
/* 1B 69 7A {n1} {n2} {n3} {n4} {n5} {n6} {n7} {n8} {n9} {n10} */ /* 1B 69 7A {n1} {n2} {n3} {n4} {n5} {n6} {n7} {n8} {n9} {n10} */
uint8_t cmd[] = "\x1b\x69\x7a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; uint8_t cmd[] = "\x1b\x69\x7a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
@ -265,6 +277,10 @@ int ptouch_send_precut_cmd(ptouch_dev ptdev, int precut)
int ptouch_rasterstart(ptouch_dev ptdev) int ptouch_rasterstart(ptouch_dev ptdev)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_rasterstart() with NULL ptdev\n"));
return -1;
}
/* 1B 69 52 01 = Select graphics transfer mode = Raster */ /* 1B 69 52 01 = Select graphics transfer mode = Raster */
char cmd[] = "\x1b\x69\x52\x01"; char cmd[] = "\x1b\x69\x52\x01";
/* 1B 69 61 01 = switch mode (0=esc/p, 1=raster mode) */ /* 1B 69 61 01 = switch mode (0=esc/p, 1=raster mode) */
@ -292,6 +308,11 @@ int ptouch_ff(ptouch_dev ptdev)
/* finish print and either cut or leave tape in machine */ /* finish print and either cut or leave tape in machine */
int ptouch_finalize(ptouch_dev ptdev, int chain) int ptouch_finalize(ptouch_dev ptdev, int chain)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_finalize() with NULL ptdev\n"));
return -1;
}
char cmd_eject[]="\x1a"; /* Print command with feeding */ char cmd_eject[]="\x1a"; /* Print command with feeding */
char cmd_chain[]="\x0c"; /* Print command (no cut) */ char cmd_chain[]="\x0c"; /* Print command (no cut) */
@ -321,6 +342,11 @@ int ptouch_getstatus(ptouch_dev ptdev)
int i, r, tx=0, tries=0; int i, r, tx=0, tries=0;
struct timespec w; struct timespec w;
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_getstatus() with NULL ptdev\n"));
return -1;
}
ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd)); ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
while (tx == 0) { while (tx == 0) {
w.tv_sec=0; w.tv_sec=0;
@ -372,11 +398,19 @@ int ptouch_getstatus(ptouch_dev ptdev)
size_t ptouch_get_tape_width(ptouch_dev ptdev) size_t ptouch_get_tape_width(ptouch_dev ptdev)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_get_tape_width() with NULL ptdev\n"));
return 0;
}
return ptdev->tape_width_px; return ptdev->tape_width_px;
} }
size_t ptouch_get_max_width(ptouch_dev ptdev) size_t ptouch_get_max_width(ptouch_dev ptdev)
{ {
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_get_max_width() with NULL ptdev\n"));
return 0;
}
return ptdev->devinfo->max_px; return ptdev->devinfo->max_px;
} }
@ -385,6 +419,10 @@ int ptouch_sendraster(ptouch_dev ptdev, uint8_t *data, size_t len)
uint8_t buf[64]; uint8_t buf[64];
int rc; int rc;
if (!ptdev) {
fprintf(stderr, _("debug: called ptouch_sendraster() with NULL ptdev\n"));
return -1;
}
if (len > (size_t)(ptdev->devinfo->max_px / 8)) { if (len > (size_t)(ptdev->devinfo->max_px / 8)) {
return -1; return -1;
} }

View File

@ -58,7 +58,7 @@ int verbose = 0;
int fontsize = 0; int fontsize = 0;
bool debug = false; bool debug = false;
bool chain = false; bool chain = false;
int forced_print_width = 0; int forced_tape_width = 0;
/* -------------------------------------------------------------------- /* --------------------------------------------------------------------
-------------------------------------------------------------------- */ -------------------------------------------------------------------- */
@ -473,7 +473,7 @@ int parse_args(int argc, char **argv)
} }
} else if (strcmp(&argv[i][1], "-force-tape-width") == 0) { } else if (strcmp(&argv[i][1], "-force-tape-width") == 0) {
if (i+1 < argc) { if (i+1 < argc) {
forced_print_width = strtol(argv[++i], NULL, 10); forced_tape_width = strtol(argv[++i], NULL, 10);
} else { } else {
usage(argv[0]); usage(argv[0]);
} }
@ -520,15 +520,15 @@ int parse_args(int argc, char **argv)
usage(argv[0]); usage(argv[0]);
} }
} }
if (forced_print_width && !save_png) { if (forced_tape_width && !save_png) {
forced_print_width = 0; forced_tape_width = 0;
} }
return i; return i;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i, lines = 0, copies = 1, print_width = 0; int lines = 0, copies = 1, print_width = 0;
char *line[MAX_LINES]; char *line[MAX_LINES];
gdImage *im = NULL; gdImage *im = NULL;
gdImage *out = NULL; gdImage *out = NULL;
@ -537,13 +537,11 @@ int main(int argc, char *argv[])
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
bindtextdomain("ptouch-print", "/usr/share/locale/"); bindtextdomain("ptouch-print", "/usr/share/locale/");
textdomain("ptouch-print"); textdomain("ptouch-print");
i = parse_args(argc, argv); int i = parse_args(argc, argv);
if (i != argc) { if (i != argc) {
usage(argv[0]); usage(argv[0]);
} }
int tape_width = ptouch_get_tape_width(ptdev); if (!forced_tape_width) {
int max_print_width = ptouch_get_max_width(ptdev);
if (!forced_print_width) {
if ((ptouch_open(&ptdev)) < 0) { if ((ptouch_open(&ptdev)) < 0) {
return 5; return 5;
} }
@ -554,14 +552,15 @@ int main(int argc, char *argv[])
printf(_("ptouch_getstatus() failed\n")); printf(_("ptouch_getstatus() failed\n"));
return 1; return 1;
} }
print_width = tape_width; print_width = ptouch_get_tape_width(ptdev);
} else { int max_print_width = ptouch_get_max_width(ptdev);
print_width = forced_print_width;
}
// do not try to print more pixels than printhead has // do not try to print more pixels than printhead has
if (print_width > max_print_width) { if (print_width > max_print_width) {
print_width = max_print_width; print_width = max_print_width;
} }
} else { // --forced_tape_width together with --writepng
print_width = forced_tape_width;
}
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
if (*argv[i] != '-') { if (*argv[i] != '-') {
break; break;
@ -579,7 +578,7 @@ int main(int argc, char *argv[])
usage(argv[0]); usage(argv[0]);
} }
} else if (strcmp(&argv[i][1], "-force-tape-width") == 0) { } else if (strcmp(&argv[i][1], "-force-tape-width") == 0) {
if (forced_print_width && save_png) { if (forced_tape_width && save_png) {
++i; ++i;
continue; continue;
} else { } else {
@ -589,8 +588,8 @@ int main(int argc, char *argv[])
i++; i++;
continue; continue;
} else if (strcmp(&argv[i][1], "-info") == 0) { } else if (strcmp(&argv[i][1], "-info") == 0) {
printf(_("maximum printing width for this printer is %ipx\n"), max_print_width); printf(_("maximum printing width for this printer is %ldpx\n"), ptouch_get_max_width(ptdev));
printf(_("maximum printing width for this tape is %ipx\n"), tape_width); printf(_("maximum printing width for this tape is %ldpx\n"), ptouch_get_tape_width(ptdev));
printf("media type = %02x (%s)\n", ptdev->status->media_type, pt_mediatype(ptdev->status->media_type)); printf("media type = %02x (%s)\n", ptdev->status->media_type, pt_mediatype(ptdev->status->media_type));
printf("media width = %d mm\n", ptdev->status->media_width); printf("media width = %d mm\n", ptdev->status->media_width);
printf("tape color = %02x (%s)\n", ptdev->status->tape_color, pt_tapecolor(ptdev->status->tape_color)); printf("tape color = %02x (%s)\n", ptdev->status->tape_color, pt_tapecolor(ptdev->status->tape_color));
@ -663,7 +662,7 @@ int main(int argc, char *argv[])
if (im != NULL) { if (im != NULL) {
gdImageDestroy(im); gdImageDestroy(im);
} }
if (!forced_print_width) { if (!forced_tape_width) {
ptouch_close(ptdev); ptouch_close(ptdev);
} }
libusb_exit(NULL); libusb_exit(NULL);