1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/*
* main.c - a commandline frontend for the barcode library
*
* Copyright (c) 1999 Michele Comitini (mcm@glisco.it)
* Copyright (c) 1999 Alessandro Rubini (rubini@gnu.org)
* Copyright (c) 1999 Prosa Srl. (prosa@prosa.it)
* Copyright (c) 2010, 2011 Giuseppe Scrivano (gscrivano@gnu.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "barcode.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "cmdline.h"
#ifndef NO_LIBPAPER
#include <paper.h>
#endif
/*
* Most of this file deals with command line options, by exploiting
* the cmdline.[ch] engine to offer defaults via environment variables
* and handling functions for complex options.
*
* In order to offer a friendly interface (for those who feel the
* cmdline *is* friendly, like me), we have to convert names to enums...
*/
struct {
char *name;
int type;
} encode_tab[] = {
{"ean", BARCODE_EAN},
{"ean13", BARCODE_EAN},
{"ean-13", BARCODE_EAN},
{"ean8", BARCODE_EAN},
{"ean-8", BARCODE_EAN},
{"upc", BARCODE_UPC},
{"upc-a", BARCODE_UPC},
{"upc-e", BARCODE_UPC},
{"isbn", BARCODE_ISBN},
{"39", BARCODE_39},
{"code39", BARCODE_39},
{"39ext", BARCODE_39EXT},
{"code39ext",BARCODE_39EXT},
{"128c", BARCODE_128C},
{"code128c", BARCODE_128C},
{"128b", BARCODE_128B},
{"code128b", BARCODE_128B},
{"128", BARCODE_128},
{"code128", BARCODE_128},
{"128raw", BARCODE_128RAW},
{"i25", BARCODE_I25},
{"interleaved 2 of 5", BARCODE_I25},
{"cbr", BARCODE_CBR},
{"codabar", BARCODE_CBR},
{"msi", BARCODE_MSI},
{"pls", BARCODE_PLS},
{"plessey", BARCODE_PLS},
{"code93", BARCODE_93},
{"93", BARCODE_93},
{"code11", BARCODE_11},
{"11", BARCODE_11},
{NULL, 0}
};
/*
* Get encoding type from string rapresentation.
* Returns -1 on error.
*/
#ifndef HAVE_STRCASECMP /* some libs (windows, for example) have stricmp */
# define strcasecmp stricmp
#endif
int encode_id(char *encode_name)
{
int i;
for (i = 0; encode_tab[i].name; i++)
if (!strcasecmp(encode_tab[i].name, encode_name))
return encode_tab[i].type;
return -1;
}
int list_encodes(FILE *f) /* used in the help message */
{
int prev = -1;
int i;
fprintf(f, _("Known encodings are (synonyms appear on the same line):"));
for (i = 0; encode_tab[i].name; i++) {
if (encode_tab[i].type != prev)
fprintf(f, "\n\t");
else
fprintf(f, ", ");
fprintf(f, "\"%s\"", encode_tab[i].name);
prev = encode_tab[i].type;
}
fprintf(f, "\n");
return 0;
}
/*
* Variables to hold cmdline arguments (or defaults)
*/
char *ifilename, *ofilename;
int encoding_type; /* filled by get_encoding() */
int code_width, code_height; /* "-g" for standalone codes */
int lines, columns; /* "-t" for tables */
int xmargin0, ymargin0; /* both for "-g" and "-t" */
int xmargin1, ymargin1; /* same, but right and top */
int ximargin, yimargin; /* "-m": internal margins */
int eps, svg, pcl, ps, noascii, nochecksum; /* boolean flags */
int page_wid, page_hei; /* page size in points */
char *page_name; /* name of the media */
double unit = 1.0; /* unit specification */
char *prgname; /* used to print error msgs, initialized to argv[0] by main */
/*
* Functions to handle command line arguments
*/
struct encode_item {
char *string;
struct encode_item *next;
} *list_head, *list_tail;
/* each "-b" option adds a string to the input pool allocating its space */
int get_input_string(void *arg)
{
struct encode_item *item = malloc(sizeof(*item));
if (!item) {
fprintf(stderr, "%s: malloc: %s\n", prgname, strerror(errno));
return -2;
}
item->string = strdup(arg);
if (!list_head) {
list_head = list_tail = item;
} else {
list_tail->next = item;
list_tail = item;
}
item->next = NULL;
return 0;
}
/* and this function extracts strings from the pool */
char *retrieve_input_string(FILE *ifile)
{
char *string;
static char fileline[128];
struct encode_item *item = list_head;
if (list_tail) { /* this means at least one "-b" was specified */
if (!item)
return NULL; /* the list is empty */
string = item->string;
list_head = item->next;
free(item);
return string;
}
/* else, read from the file */
if (!fgets(fileline, 128, ifile))
return NULL;
if (fileline[strlen(fileline)-1]=='\n')
fileline[strlen(fileline)-1]= '\0';
if (fileline[strlen(fileline)-1]=='\r')
fileline[strlen(fileline)-1]= '\0';
return strdup(fileline);
}
/* accept a unit specification */
int get_unit(void *arg)
{
static struct {
char *str;
double unit;
} *ptr, unittab[] = {
{"pt", 1.0},
{"in", 72.0},
{"cm", 72.0/2.54},
{"mm", 72.0/25.4},
{NULL, 0.0}
};
for (ptr = unittab; ptr->str && strcmp((char *)arg, ptr->str); ptr++)
;
unit = ptr->unit;
if (ptr->str) return 0;
fprintf(stderr, "%s: incorrect unit \"%s\" (use one of",
prgname, (char *)arg);
for (ptr = unittab; ptr->str; ptr++)
fprintf(stderr, " \"%s\"", ptr->str);
fprintf(stderr, ")\n");
return -2;
}
/* convert an encoding name to an encoding integer code */
int get_encoding(void *arg)
{
encoding_type = encode_id((char *)arg);
if (encoding_type >=0) return 0;
fprintf(stderr, "%s: wrong encoding \"%s\"\n", prgname,
(char *)arg);
return -2; /* error, no help */
}
/* convert a geometry specification */
int get_geometry(void *arg)
{
double w = 0.0, h = 0.0;
double x = 0.0, y = 0.0;
int n;
if (((char *)arg)[0]=='+' || ((char *)arg)[0]=='-') {
n = sscanf((char *)arg, "%lf%lf%s", &x, &y, (char *)arg);
} else {
n = sscanf((char *)arg, "%lfx%lf%lf%lf%s", &w, &h, &x, &y,
(char *)arg);
}
if (n != 4 && n != 2) {
fprintf(stderr, _("%s: wrong geometry \"%s\"\n"), prgname, (char *)arg);
return -2;
}
/* convert to points */
code_width = w * unit;
code_height = h * unit;
xmargin0 = x * unit;
ymargin0 = y * unit;
return 0;
}
/* convert a geometry specification */
int get_table(void *arg)
{
double x0 = 0.0, y0 = 0.0, x1 = 0.0, y1 = 0.0;
int n;
n = sscanf((char *)arg, "%dx%d+%lf+%lf-%lf-%lf",
&columns, &lines, &x0, &y0, &x1, &y1);
if (n==1 || n==3) { /* error: 2, 4, 5, 6 are fine */
fprintf(stderr, _("%s: wrong table specification \"%s\"\n"), prgname,
(char *)arg);
return -2;
}
if (n < 6) y1 = y0; /* symmetric by default */
if (n < 5) x1 = x0;
/* convert and return */
xmargin0 = x0 * unit;
ymargin0 = y0 * unit;
xmargin1 = x1 * unit;
ymargin1 = y1 * unit;
return 0;
}
/* convert an internal margin specification */
int get_margin(void *arg)
{
char separator;
double x,y;
int n;
/* accept one number or two, separated by any char */
n = sscanf((char *)arg, "%lf%c%lf", &x, &separator, &y);
if (n==1) {
n=3; y = x;
}
if (n==3) {
ximargin = x * unit;
yimargin = y * unit;
return 0;
}
fprintf(stderr, _("%s: wrong margin specification \"%s\"\n"), prgname,
(char *)arg);
return -2;
return 0;
}
/* convert a page geometry specification */
int get_page_geometry(void *arg)
{
int n;
double dpw, dph; /* page width, height in mm or inches */
static char tmpstr[20];
page_name = arg; /* if undecipherable, we won't run the program :) */
/*
* try to decode a "mm" string (eg. "210mmx297mm" or "210x297mm")
*/
n = sscanf((char *)arg, "%lfmmx%lf", &dpw, &dph);
if (n != 2 && strlen(arg)<20) {
n = sscanf((char *)arg, "%lfx%lf%s", &dpw, &dph, tmpstr);
if (n == 3 && !strcmp(tmpstr, "mm")) {
/* Ok, convert to points: 1in is 25.4mm, 1in is also 72p */
page_wid = (int)(dpw / 25.4 * 72.0 + 0.5);
page_hei = (int)(dph / 25.4 * 72.0 + 0.5);
return 0;
}
}
/*
* try to decode an "in" string (eg. "8.5inx11in" or "8.5x11in")
*/
n = sscanf((char *)arg, "%lfinx%lf", &dpw, &dph);
if (n != 2 && strlen(arg)<20) {
n = sscanf((char *)arg, "%lfx%lf%s", &dpw, &dph, tmpstr);
if (n == 3 && !strcmp(tmpstr, "in")) {
page_wid = (int)(dpw * 72.0 + 0.5); /* round to points */
page_hei = (int)(dph * 72.0 + 0.5);
return 0;
}
}
/*
* try to decode a numeric specification
*/
n = sscanf((char *)arg, "%lfx%lf", &dpw, &dph);
if (n == 2) {
page_wid = dpw * unit;
page_hei = dph * unit;
if (unit != 1.0) { /* rebuild the page name */
page_name = malloc(32); /* big, to avoid snprintf, missing on HP */
if (page_name)
sprintf(page_name, "%dx%d\n", page_wid, page_hei);
}
return 0;
}
#ifndef NO_LIBPAPER
/*
* try to use libpaper, since it is available
*/
{
const struct paper* paptr;
paperinit();
paptr = paperinfo(arg);
if (!paptr) { /* unknown name */
paperdone();
return -1;
}
page_wid = (int)(paperpswidth(paptr) + 0.5);
page_hei = (int)(paperpsheight(paptr) + 0.5);
paperdone();
return 0;
}
#endif
/* If we got here, the argument is undecipherable: fail */
fprintf(stderr, _("%s: wrong page size specification \"%s\"\n"), prgname,
(char *)arg);
return -2;
}
/*
* The table of possible arguments
*/
struct commandline option_table[] = {
{'i', CMDLINE_S, &ifilename, NULL, NULL, NULL,
_N ("input file (strings to encode), default is stdin")},
{'o', CMDLINE_S, &ofilename, NULL, NULL, NULL,
_N ("output file, default is stdout")},
{'b', CMDLINE_S, NULL, get_input_string, NULL, NULL,
_N ("string to encode (use input file if missing)")},
{'e', CMDLINE_S, NULL, get_encoding, "BARCODE_ENCODING", NULL,
_N ("encoding type (default is best fit for first string)")},
{'u', CMDLINE_S, NULL, get_unit, "BARCODE_UNIT", NULL,
_N ("unit (\"mm\", \"in\", ...) used to decode -g, -t, -p")},
{'g', CMDLINE_S, NULL, get_geometry, "BARCODE_GEOMETRY", NULL,
_N ("geometry on the page: [<wid>x<hei>][+<margin>+<margin>]")},
{'t', CMDLINE_S, NULL, get_table, "BARCODE_TABLE", NULL,
_N ("table geometry: <cols>x<lines>[+<margin>+<margin>]")},
{'m', CMDLINE_S, NULL, get_margin, "BARCODE_MARGIN", "10",
_N ("internal margin for each item in a table: <xm>[,<ym>]")},
{'n', CMDLINE_NONE, &noascii, NULL, NULL, NULL,
_N ("\"numeric\": avoid printing text along with the bars")},
{'c', CMDLINE_NONE, &nochecksum, NULL, NULL, NULL,
_N ("no Checksum character, if the chosen encoding allows it")},
{'E', CMDLINE_NONE, &eps, NULL, NULL, NULL,
_N ("print one code as eps file (default: multi-page ps)")},
{'S', CMDLINE_NONE, &svg, NULL, NULL, NULL,
_N ("print one code as svg file (default: multi-page ps)")},
{'P', CMDLINE_NONE, &pcl, NULL, NULL, NULL,
_N ("create PCL output instead of postscript")},
{'p', CMDLINE_S, NULL, get_page_geometry, NULL, NULL,
_N ("page size (refer to the man page)")},
{'s', CMDLINE_NONE, &streaming, NULL, NULL, 0,
_N ("streaming mode (refer to the man page)")},
{0,}
};
#ifdef NO_STRERROR
/*
* A strerror replacement (thanks to Thad Floryan <thad@thadlabs.com>)
*/
char *strerror(int error)
{
static char msg[16];
if (error >= 0 && error < sys_nerr)
return sys_errlist[error];
sprintf(msg, _("Error %d"), error);
return msg;
}
#endif
/*
* The main function
*/
int main(int argc, char **argv)
{
struct Barcode_Item * bc;
FILE *ifile = stdin;
FILE *ofile = stdout;
char *line;
int flags=0; /* for the library */
int page, retval;
int errors = 0;
prgname = argv[0];
#define SHOW_VERSION() \
printf (_("GNU barcode %s is a tool to convert text strings to printed bars.\n"), PACKAGE_VERSION); \
printf("\n");
#define SHOW_BUG_REPORT_ADDR() \
printf (_("Mail bug reports and suggestions to <%s>.\n"), PACKAGE_BUGREPORT);
/* First of all, accept "--help" and "-h" as a special case */
if (argc == 2 && (!strcmp(argv[1],"--help") || !strcmp(argv[1],"-h"))) {
SHOW_VERSION ();
commandline_errormsg(stdout, option_table, argv[0], _("Options:\n"));
printf("\n");
list_encodes(stdout);
SHOW_BUG_REPORT_ADDR ();
exit(0);
}
/* Also, accept "--version" as a special case */
if (argc == 2 && (!strcmp(argv[1],"--version"))) {
SHOW_VERSION ();
printf ("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
printf ("This is free software: you are free to change and redistribute it.\n");
printf ("There is NO WARRANTY, to the extent permitted by law.\n");
printf("\n");
SHOW_BUG_REPORT_ADDR ();
exit(0);
}
/* Otherwise, parse the commandline */
retval = commandline(option_table, argc, argv, _("Use: %s [options]\n"));
if (retval) {
if (retval == -1) /* help printed, complete it */
list_encodes(stderr);
else /* no help printed, suggest it */
fprintf(stderr, _("%s: try \"%s --help\"\n"), prgname, prgname);
exit(1);
}
/* If no paper size has been specified, use the default, if any */
if (!page_name) {
page_wid = 595; page_hei = 842;
page_name = "A4"; /* I live in Europe :) */
#ifndef NO_LIBPAPER
get_page_geometry(systempapername()); /* or the system default */
#endif
}
if (!pcl && streaming) {
fprintf(stderr, _("Streaming is only implemented for PCL mode\n"));
exit(1);
}
if (streaming && (lines || columns)) {
fprintf(stderr, _("Streaming is not intended for table mode\n"));
exit(1);
}
/* FIXME: print warnings for incompatible options */
/* open the input stream if specified */
if (ifilename)
ifile = fopen(ifilename,"r");
if (!ifile) {
fprintf(stderr, "%s: %s: %s\n", argv[0], ifilename,
strerror(errno));
exit(1);
}
/* open the output stream if specified */
if (ofilename)
ofile = fopen(ofilename,"w");
if (!ofile) {
fprintf(stderr, "%s: %s: %s\n", argv[0], ofilename,
strerror(errno));
exit(1);
}
if (encoding_type < 0) { /* unknown type specified */
fprintf(stderr, _("%s: Unknown encoding. Try \"%s --help\"\n"),
argv[0], argv[0]);
exit(1);
}
flags |= encoding_type;
if (pcl) {
flags |= BARCODE_OUT_PCL;
} else {
ps = !(eps || svg); /* a shortcut */
if (eps)
flags |= BARCODE_OUT_EPS; /* print headers too */
else if (svg)
flags |= BARCODE_OUT_SVG;
else
flags |= BARCODE_OUT_PS | BARCODE_OUT_NOHEADERS;
}
if (noascii)
flags |= BARCODE_NO_ASCII;
if (nochecksum)
flags |= BARCODE_NO_CHECKSUM;
/* the table is not available in eps mode */
if ((eps || svg) && (lines>1 || columns>1)) {
fprintf(stderr, _("%s: can't print tables in EPS format\n"),argv[0]);
exit(1);
}
if (ps) { /* The header is independent of single/table mode */
/* Headers. Don't let the library do it, we may need multi-page */
fprintf(ofile, "%%!PS-Adobe-2.0\n");
/* It would be nice to know the bounding box. Leave it alone */
fprintf(ofile, "%%%%Creator: \"barcode\", "
"libbarcode sample frontend\n");
if (page_name)
fprintf(ofile, "%%%%DocumentPaperSizes: %s\n", page_name);
fprintf(ofile, "%%%%EndComments\n");
fprintf(ofile, "%%%%EndProlog\n\n");
}
/*
* Here we are, ready to work. Handle the one-per-page case first,
* as it is shorter.
*/
if (!lines && !columns) {
page = 0;
while ( (line = retrieve_input_string(ifile)) ) {
page++;
if (ps) {
fprintf(ofile, "%%%%Page: %i %i\n\n",page,page);
}
if (Barcode_Encode_and_Print(line, ofile, code_width, code_height,
xmargin0, ymargin0, flags) < 0) {
fprintf(stderr, _("%s: can't encode \"%s\"\n"), argv[0], line);
errors++;
}
if (eps || svg) break; /* if output is eps, do it once only */
if (ps) fprintf(ofile, "showpage\n");
if (pcl && !streaming) fprintf(ofile, "\f");
}
/* no more lines, print footers */
if (ps) {
fprintf(ofile, "%%%%Trailer\n\n");
}
} else {
/* table mode, the header has been already printed */
int xstep = (page_wid - xmargin0 - xmargin1)/columns;
int ystep = (page_hei - ymargin0 - ymargin1)/lines;
int x = columns, y = -1; /* position in the table, start off-page */
if (!ximargin) ximargin = BARCODE_DEFAULT_MARGIN;
if (!yimargin) yimargin = BARCODE_DEFAULT_MARGIN;
/* Assign default size unless -g did it (Joachim Reichelt) */
if ( !code_width && !code_height) {
code_width = xstep - 2*ximargin;
code_height = ystep - 2*yimargin;
}
page=0;
while ( (line = retrieve_input_string(ifile)) ) {
x++; /* fit x and y */
if (x >= columns) {
x=0; y--;
if (y<0) {
y = lines-1; page++;
/* flush page */
if (ps && page > 1) fprintf(ofile, "showpage\n");
if (pcl && page > 1) fprintf(ofile, "\f");
/* new page */
if (ps) fprintf(ofile, "%%%%Page: %i %i\n\n",page,page);
}
}
/*
* Create a barcode item. This allows to set the margin to 0, as
* we have [xy]imargin to use. But don't use Encode_and_Print(),
* unroll it here instead
*/
bc = Barcode_Create(line);
if (!bc) {
fprintf(stderr, _("%s: Barcode_Create(): %s\n"), argv[0],
strerror(errno));
exit(1);
}
bc->margin = 0;
if ( (Barcode_Position(bc, code_width, code_height,
xmargin0 + ximargin + x * xstep,
ymargin0 + yimargin + y * ystep, 0.0) < 0)
|| (Barcode_Encode(bc, flags) < 0)
|| (Barcode_Print(bc, ofile, flags) < 0) ) {
fprintf(stderr, _("%s: can't encode \"%s\": %s\n"), argv[0],
line, strerror(bc->error));
}
Barcode_Delete(bc);
}
if (ps) fprintf(ofile, "showpage\n\n%%%%Trailer\n\n");
if (pcl) fprintf(ofile, "\f");
}
return errors ? 1 : 0;
}
|