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 638 639 640 641 642 643 644 645
|
/* file rdPrtDesc.c
*
* read printer description file and fill the
* printer knowledge data base
*
* Copyrights: Jean-Jacques Sarton j.sarton@t-online.de
*/
/*
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "model.c"
#ifndef BLD_UTIL
#define BLD_UTIL 1
#endif
/* a few define for the type of description entry */
/* and the description for the diferent entries */
#define D_BEGIN 1
#define A_STR 2
#define A_NB 3
#define A_PROT 4
#define A_FLAG 5
#define A_FNAME 6
#define D_END 7
#define OFFSET(a) ((int)(&((configData_t*)0)->a))
typedef struct attr_s
{
char *attrName;
int attrType;
int attrOffset;
} attr_t;
static attr_t attr[] =
{
{ ".PRINTER", D_BEGIN, -1 },
{ ".name:", A_STR, OFFSET(name) },
{ ".colorsNb:", A_NB, OFFSET(colors) },
{ ".mainProt:", A_PROT, OFFSET(prot) },
{ ".stateFlg:", A_FLAG, OFFSET(state) },
{ ".exchangeFlg:", A_FLAG, OFFSET(exchange) },
{ ".exchangeSeparateFlg:", A_FLAG, OFFSET(exchangeSeparate) },
{ ".cleanSeparateFlg:", A_FLAG, OFFSET(cleanSeparate) },
{ ".resetProt:", A_PROT, OFFSET(reset) },
{ ".alignProt:", A_PROT, OFFSET(align) },
{ ".idFlg:", A_FLAG, OFFSET(id) },
{ ".passesNb:", A_NB, OFFSET(passes) },
{ ".choicesNb:", A_NB, OFFSET(choices) },
{ ".colorPassesNb:", A_NB, OFFSET(color_passes) },
{ ".colorChoicesNb:", A_NB, OFFSET(color_choices) },
{ ".checkNozzleNeedReset:",A_FLAG, OFFSET(checkNeedReset) },
{ ".alignFunctionName:", A_FNAME, OFFSET(alignFunction) },
{ ".END", D_END, -1 },
{ NULL, }
};
/* help structures for accessing the members of the original structur */
/* via the structur base address and an offset for the menbers */
typedef struct s_s
{
char *s;
} s_t;
typedef struct i_s
{
int i;
} i_t;
#define ATTR_NO (sizeof(attr)/sizeof(attr_t) - 3)
configData_t *configData = NULL;
int configEntries = 0;
/*******************************************************************/
/* Function skipSpace */
/* */
/* return the a pointer to the firdst non space character */
/* */
/*******************************************************************/
static char *skipSpace(char *s)
{
while ( *s && (*s==' '||*s=='\t'||*s=='\r'|| *s=='\n') )
s++;
return s;
}
/*******************************************************************/
/* Function endFName */
/* */
/* skip over non space characters and set the first space char */
/* to '\0' */
/* */
/*******************************************************************/
static void endFName(char *s)
{
while(*s && !(*s==' '||*s=='\t'||*s=='\r'|| *s=='\n'))
s++;
*s = '\0';
}
/*******************************************************************/
/* Function endStr */
/* */
/* eliminate trailing space characters from the end of the line */
/* */
/*******************************************************************/
static void endStr(char *s)
{
while(s[1])
s++;
while (*s==' '||*s=='\t'||*s=='\r'|| *s=='\n')
*s-- = '\0';
}
/*******************************************************************/
/* Function getAttr */
/* */
/* retriewe the attribute description for the given key */
/* */
/*******************************************************************/
static attr_t *getAttr(char **s)
{
attr_t *a = attr;
int len;
while(a->attrName)
{
if ( strncmp(a->attrName, *s, (len = strlen(a->attrName))) == 0 )
{
*s += len;
if ( **s )
*s = skipSpace(*s);
return a;
}
a++;
}
return NULL;
}
/*******************************************************************/
/* Function readDataFile */
/* */
/* read the description file and build an array of printer desc. */
/* */
/*******************************************************************/
static int readDataFile(FILE *fp)
{
char buf[2048];
int attrNb = 0;
char *s;
s_t *ss;
i_t *is;
attr_t *a;
configData_t *c = NULL; /* make gcc happy */
int line = 0;
while (fgets(buf,sizeof(buf), fp))
{
line++;
s = skipSpace(buf);
if ( *s != '.' )
{
/* this may be an empty or comment line */
continue;
}
else
{
a = getAttr(&s);
if ( a )
{
if ( *s == '\0' && a->attrOffset != -1 )
{
fprintf(stderr,"error at line %d\n",line++);
return 0;
}
else
{
if ( a->attrType == D_END && attrNb != ATTR_NO )
{
fprintf(stderr,
"incomplete printer description at line %d\n",line);
return 0;
}
else if ( a->attrType == D_BEGIN && attrNb != 0 )
{
fprintf(stderr,
"Printer description not closed at line %d\n",line);
return 0;
}
else if ( a->attrType == D_BEGIN )
{
/* alloc storage for this */
if ( configData == NULL )
{
configData = (configData_t*)calloc(sizeof(configData_t), 1);
}
else
{
configData = (configData_t*)realloc(configData, sizeof(configData_t)*(configEntries+1));
if ( configData )
memset(&configData[configEntries], 0, sizeof(configData_t));
}
if ( configData == NULL )
{
fprintf(stderr,"no enough memory\n");
return 0;
}
c = &configData[configEntries];
}
else if ( a->attrType == D_END && attrNb == ATTR_NO )
{
configEntries++;
attrNb = 0;
}
else
{
is = (void*)((char*)c + a->attrOffset);
ss = (void*)((char*)c + a->attrOffset);
is->i = 0;
ss->s = NULL;
switch(a->attrType)
{
case A_FLAG:
if ( *s == '0' || strncasecmp("false",s,5) == 0 )
is->i = 0;
else if ( *s == '1' || strncasecmp("true",s,4) == 0 )
is->i = 1;
else
{
fprintf(stderr,"Wrong value at line %d\n",line);
return 0;
}
break;
case A_NB:
if ( (*s == '-' && s[1] >= '0' && s[1] <= '9') ||
(*s >= '0' && *s <= '9') )
{
is->i = atoi(s);
}
else
{
fprintf(stderr,"Wrong value at line %d\n",line);
return 0;
}
break;
case A_PROT:
while(*s)
{
if ( strncmp(s, "D4", 2) == 0 )
{
is->i |= PROT_D4;
s += 2;
}
else if ( strncmp(s, "EXD4", 4) == 0 )
{
is->i |= PROT_NEW;
s += 4;
}
else if ( strncmp(s, "OLD", 3) == 0 )
{
is->i |= PROT_OLD;
s += 3;
}
s = skipSpace(s);
}
break;
case A_FNAME:
endFName(s);
if ( *s != '-' )
ss->s = strdup(s);
break;
case A_STR:
endStr(s);
ss->s = strdup(s);
break;
}
attrNb++;
}
}
}
}
}
return 1;
}
/*******************************************************************/
/* Function mStrCmp() */
/* */
/* Compare string regardless of case, consider number as numvber */
/* and not a single character. */
/* */
/*******************************************************************/
int _mStrCmp(char *s, char *t)
{
int ret = 0;
while ( !ret && *s )
{
if ( isdigit(*s) && isdigit(*t) )
{
ret = atoi(s) - atoi(t);
while ( isdigit(*s) ) s++;
while ( isdigit(*t) ) t++;
}
else
{
ret = tolower(*s) - tolower(*t);
s++;
t++;
}
}
return ret;
}
/*******************************************************************/
/* Function cmpName() */
/* */
/* help function for sorting of printer descriptions */
/* */
/*******************************************************************/
static int cmpName(const void *P1, const void *P2)
{
configData_t *p1 = (configData_t *)P1;
configData_t *p2 = (configData_t *)P2;
if ( p1->name[0] == '?' )
{
return 1;
}
if ( p2->name[0] == '?' )
{
return -1;
}
return _mStrCmp(p1->name, p2->name);
}
/*******************************************************************/
/* Function mergeDescs() */
/* */
/* merge the buildin and the external datas */
/* */
/*******************************************************************/
static int mergeDescs()
{
int i, j;
for ( i = 0; i < defaultConfigDataSize; i++ )
{
/* if the printer is allready externaly described */
/* don't add this */
for ( j = 0; j < configEntries; j++ )
{
if ( strcmp(configData[j].name, defaultConfigData[i].name) == 0 )
break;
}
if ( j == configEntries )
{
/* printer not found, add an array element */
if ( configData == NULL )
{
configData = (configData_t*)malloc(sizeof(configData_t));
}
else
{
configData = (configData_t*)realloc(configData,
sizeof(configData_t)*(configEntries+1));
}
if ( configData == NULL )
{
fprintf(stderr,"no enough memory\n");
configEntries = 0;
return 0;
}
else
{
memcpy(&configData[configEntries],
&defaultConfigData[i],
sizeof(configData_t));
configEntries++;
}
}
}
if ( configEntries )
{
qsort(configData, configEntries, sizeof(configData_t), cmpName);
}
return 1;
}
/*******************************************************************/
/* Function readPrinterData */
/* */
/* read the description file, main entry */
/* */
/*******************************************************************/
int readPrinterData()
{
int i;
FILE *fp = NULL;
char *path[] =
{
#if ! BLD_UTIL
"/usr/lib/mtink/printer.desc",
"/usr/local/lib/mtink/printer.desc",
"/opt/mtink/printer.desc",
#else
"./printer.desc"
#endif
};
for ( i = 0; i < sizeof(path)/sizeof(char*); i++ )
{
if ( path[i] && (fp = fopen(path[i],"r")) )
{
/* read the file */
readDataFile(fp);
fclose(fp);
mergeDescs();
return 1;
}
}
mergeDescs();
return 0;
}
#if BLD_UTIL
/*******************************************************************/
/* Function printData */
/* */
/* print the data as described into the file model.c */
/* */
/*******************************************************************/
void printData(configData_t *conf, int nb)
{
int i;
attr_t *a;
s_t *ss;
i_t *is;
configData_t *c;
for (i=0; i < nb; i++)
{
c = &conf[i];
a = attr;
while(a->attrName)
{
ss = (void*)((char*)c+a->attrOffset);
is = (void*)((char*)c+a->attrOffset);
switch(a->attrType)
{
case D_BEGIN:
printf("%s\n",a->attrName);
break;
case D_END:
printf("%s\n\n",a->attrName);
break;
case A_STR:
printf(" %-21s %s\n",a->attrName, ss->s);
break;
case A_NB:
printf(" %-21s %d\n",a->attrName, is->i);
break;
case A_FNAME:
printf(" %-21s %s\n",a->attrName, ss->s ? ss->s : "-");
break;
case A_FLAG:
printf(" %-21s %s\n",a->attrName, is->i ? "True" : "False");
break;
case A_PROT:
printf(" %-21s",a->attrName);
if ( is->i & PROT_OLD )
printf(" OLD");
if ( is->i & PROT_EXIT )
printf(" EXD4");
if ( is->i & PROT_D4 )
printf(" D4");
printf("\n");
}
a++;
}
}
}
static void genCFile(configData_t *conf, int nb)
{
int i;
attr_t *a;
s_t *ss;
i_t *is;
configData_t *c;
printf("/* file model.c\n"
" *\n"
" *\n"
" List of know printers and flags\n"
" * device and printer choice\n"
" *\n"
" */\n"
"\n"
"#include \"model.h\"\n"
"\n"
"configData_t defaultConfigData[] = {\n" );
for (i=0; i < nb; i++)
{
c = &conf[i];
a = attr;
while(a->attrName)
{
ss = (void*)((char*)c+a->attrOffset);
is = (void*)((char*)c+a->attrOffset);
switch(a->attrType)
{
case D_BEGIN:
printf(" { ");
break;
case D_END:
printf("},\n");
break;
case A_STR:
printf("\"%s\",%*s ", ss->s,20-strlen(ss->s),"");
break;
case A_NB:
printf("%2d, ", is->i);
break;
case A_FNAME:
if ( ss->s )
printf("\"%s\" ", ss->s);
else
printf("NULL ");
break;
case A_FLAG:
if ( is->i ) printf("True, ");
else printf("False, ");
break;
case A_PROT:
switch(is->i)
{
case 1: printf("PROT_OLD, "); break;
case 2: printf("PROT_EXIT, "); break;
case 3: printf("PROT_NEW, "); break;
case 4: printf("PROT_D4, "); break;
case 5: printf("PROT_OLD|PROT_D4, "); break;
}
break;
}
a++;
}
}
printf("};\n"
"\n"
"configData_t defaultConfigData[];\n"
"int defaultConfigDataSize = sizeof(defaultConfigData) / sizeof(configData_t);\n"
"\n"
);
}
/*******************************************************************/
/* Function main */
/* */
/* This is for producing a printer description file for ttink/mtink*/
/* or generationg the c description file from an allready provided */
/* printer description file */
/* */
/*******************************************************************/
int main(int argc, char **argv)
{
int i;
attr_t *a;
char *t;
configData_t *c;
int mode = 0;
char *prgName;
prgName = strrchr(argv[0], '/');
if ( prgName )
prgName++;
else
prgName = argv[0];
argv++;
argc--;
if ( argc == 0 || argc > 1 || argv[0][0] != '-' ||
!(argv[0][1] == 'c' || argv[0][1] == 'p'))
{
fprintf(stderr, "Syntax: %s -p|-c\n\n",prgName);
fprintf(stderr, " with the -p option you will print a description file\n");
fprintf(stderr, " The -c option produce a c file from the desc. file\n");
fprintf(stderr, " printer.desc which must be in the working directory\n");
fprintf(stderr, " All output are on the console\n");
exit(1);
}
if ( argv[0][1] == 'p' )
{
printData(defaultConfigData,defaultConfigDataSize);
}
else
{
readPrinterData();
fprintf(stderr,"Read %d descriptions\n",configEntries);
if ( configData )
genCFile(configData,configEntries);
}
return 0;
}
#endif
|