2 *******************************************************************************
4 * Copyright (C) 2005-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: icupkg.cpp
10 * tab size: 8 (not used)
13 * created on: 2005jul29
14 * created by: Markus W. Scherer
16 * This tool operates on ICU data (.dat package) files.
17 * It takes one as input, or creates an empty one, and can remove, add, and
18 * extract data pieces according to command-line options.
19 * At the same time, it swaps each piece to a consistent set of platform
20 * properties as desired.
21 * Useful as an install-time tool for shipping only one flavor of ICU data
22 * and preparing data files for the target platform.
23 * Also for customizing ICU data (pruning, augmenting, replacing) and for
25 * Subsumes functionality and implementation code from
26 * gencmn, decmn, and icuswap tools.
27 * Will not work with data DLLs (shared libraries).
30 #include "unicode/utypes.h"
31 #include "unicode/putil.h"
42 // TODO: add --matchmode=regex for using the ICU regex engine for item name pattern matching?
44 // general definitions ----------------------------------------------------- ***
46 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
48 // read a file list -------------------------------------------------------- ***
50 static const char *reservedChars
="\"%&'()*+,-./:;<=>?_";
55 } listFileSuffixes
[]={
61 /* check for multiple text file suffixes to see if this list name is a text file name */
63 isListTextFile(const char *listname
) {
64 const char *listNameEnd
=strchr(listname
, 0);
67 for(i
=0; i
<LENGTHOF(listFileSuffixes
); ++i
) {
68 suffix
=listFileSuffixes
[i
].suffix
;
69 length
=listFileSuffixes
[i
].length
;
70 if((listNameEnd
-listname
)>length
&& 0==memcmp(listNameEnd
-length
, suffix
, length
)) {
79 * If the listname ends with ".txt", then read the list file
80 * (in the system/ invariant charset).
81 * If the listname ends with ".dat", then read the ICU .dat package file.
82 * Otherwise, read the file itself as a single-item list.
85 readList(const char *filesPath
, const char *listname
, UBool readContents
) {
88 const char *listNameEnd
;
90 if(listname
==NULL
|| listname
[0]==0) {
91 fprintf(stderr
, "missing list file\n");
95 listPkg
=new Package();
97 fprintf(stderr
, "icupkg: not enough memory\n");
98 exit(U_MEMORY_ALLOCATION_ERROR
);
101 listNameEnd
=strchr(listname
, 0);
102 if(isListTextFile(listname
)) {
103 // read the list file
108 file
=fopen(listname
, "r");
110 fprintf(stderr
, "icupkg: unable to open list file \"%s\"\n", listname
);
112 exit(U_FILE_ACCESS_ERROR
);
115 while(fgets(line
, sizeof(line
), file
)) {
117 end
=strchr(line
, '#');
121 // remove trailing CR LF
123 while(line
<end
&& (*(end
-1)=='\r' || *(end
-1)=='\n')) {
128 // check first non-whitespace character and
129 // skip empty lines and
130 // skip lines starting with reserved characters
131 start
=u_skipWhitespace(line
);
132 if(*start
==0 || NULL
!=strchr(reservedChars
, *start
)) {
136 // take whitespace-separated items from the line
138 // find whitespace after the item or the end of the line
139 for(end
=(char *)start
; *end
!=0 && *end
!=' ' && *end
!='\t'; ++end
) {}
141 // this item is the last one on the line
144 // the item is terminated by whitespace, terminate it with NUL
148 listPkg
->addFile(filesPath
, start
);
150 listPkg
->addItem(start
);
153 // find the start of the next item or exit the loop
154 if(end
==NULL
|| *(start
=u_skipWhitespace(end
+1))==0) {
160 } else if((listNameEnd
-listname
)>4 && 0==memcmp(listNameEnd
-4, ".dat", 4)) {
161 // read the ICU .dat package
162 listPkg
->readPackage(listname
);
164 // list the single file itself
166 listPkg
->addFile(filesPath
, listname
);
168 listPkg
->addItem(listname
);
175 // main() ------------------------------------------------------------------ ***
178 printUsage(const char *pname
, UBool isHelp
) {
179 FILE *where
=isHelp
? stdout
: stderr
;
182 "%csage: %s [-h|-?|--help ] [-tl|-tb|-te] [-c] [-C comment]\n"
183 "\t[-a list] [-r list] [-x list] [-l]\n"
184 "\t[-s path] [-d path] [-w] [-m mode]\n"
185 "\tinfilename [outfilename]\n",
186 isHelp
? 'U' : 'u', pname
);
190 "Read the input ICU .dat package file, modify it according to the options,\n"
191 "swap it to the desired platform properties (charset & endianness),\n"
192 "and optionally write the resulting ICU .dat package to the output file.\n"
193 "Items are removed, then added, then extracted and listed.\n"
194 "An ICU .dat package is written if items are removed or added,\n"
195 "or if the input and output filenames differ,\n"
196 "or if the --writepkg (-w) option is set.\n");
199 "If the input filename is \"new\" then an empty package is created.\n"
200 "If the output filename is missing, then it is automatically generated\n"
201 "from the input filename: If the input filename ends with an l, b, or e\n"
202 "matching its platform properties, then the output filename will\n"
203 "contain the letter from the -t (--type) option.\n");
206 "This tool can also be used to just swap a single ICU data file, replacing the\n"
207 "former icuswap tool. For this mode, provide the infilename (and optional\n"
208 "outfilename) for a non-package ICU data file.\n"
209 "Allowed options include -t, -w, -s and -d.\n"
210 "The filenames can be absolute, or relative to the source/dest dir paths.\n"
211 "Other options are not allowed in this mode.\n");
215 "\t(Only the last occurrence of an option is used.)\n"
217 "\t-h or -? or --help print this message and exit\n");
220 "\t-tl or --type l output for little-endian/ASCII charset family\n"
221 "\t-tb or --type b output for big-endian/ASCII charset family\n"
222 "\t-te or --type e output for big-endian/EBCDIC charset family\n"
223 "\t The output type defaults to the input type.\n"
225 "\t-c or --copyright include the ICU copyright notice\n"
226 "\t-C comment or --comment comment include a comment string\n");
229 "\t-a list or --add list add items to the package\n"
230 "\t-r list or --remove list remove items from the package\n"
231 "\t-x list or --extract list extract items from the package\n"
232 "\tThe list can be a single item's filename,\n"
233 "\tor a .txt filename with a list of item filenames,\n"
234 "\tor an ICU .dat package filename.\n");
237 "\t-w or --writepkg write the output package even if no items are removed\n"
238 "\t or added (e.g., for only swapping the data)\n");
241 "\t-m mode or --matchmode mode set the matching mode for item names with\n"
243 "\t noslash: the '*' wildcard does not match the '/' tree separator\n");
245 * Usage text columns, starting after the initial TAB.
247 * 901234567890123456789012345678901234567890123456789012345678901234567890
251 "\tList file syntax: Items are listed on one or more lines and separated\n"
252 "\tby whitespace (space+tab).\n"
253 "\tComments begin with # and are ignored. Empty lines are ignored.\n"
254 "\tLines where the first non-whitespace character is one of %s\n"
255 "\tare also ignored, to reserve for future syntax.\n",
258 "\tItems for removal or extraction may contain a single '*' wildcard\n"
259 "\tcharacter. The '*' matches zero or more characters.\n"
260 "\tIf --matchmode noslash (-m noslash) is set, then the '*'\n"
261 "\tdoes not match '/'.\n");
264 "\tItems must be listed relative to the package, and the --sourcedir or\n"
265 "\tthe --destdir path will be prepended.\n"
266 "\tThe paths are only prepended to item filenames while adding or\n"
267 "\textracting items, not to ICU .dat package or list filenames.\n"
269 "\tPaths may contain '/' instead of the platform's\n"
270 "\tfile separator character, and are converted as appropriate.\n");
273 "\t-s path or --sourcedir path directory for the --add items\n"
274 "\t-d path or --destdir path directory for the --extract items\n"
276 "\t-l or --list list the package items to stdout\n"
277 "\t (after modifying the package)\n");
281 static UOption options
[]={
283 UOPTION_HELP_QUESTION_MARK
,
284 UOPTION_DEF("type", 't', UOPT_REQUIRES_ARG
),
287 UOPTION_DEF("comment", 'C', UOPT_REQUIRES_ARG
),
292 UOPTION_DEF("writepkg", 'w', UOPT_NO_ARG
),
294 UOPTION_DEF("matchmode", 'm', UOPT_REQUIRES_ARG
),
296 UOPTION_DEF("add", 'a', UOPT_REQUIRES_ARG
),
297 UOPTION_DEF("remove", 'r', UOPT_REQUIRES_ARG
),
298 UOPTION_DEF("extract", 'x', UOPT_REQUIRES_ARG
),
300 UOPTION_DEF("list", 'l', UOPT_NO_ARG
)
305 OPT_HELP_QUESTION_MARK
,
328 isPackageName(const char *filename
) {
331 len
=(int32_t)strlen(filename
)-4; /* -4: subtract the length of ".dat" */
332 return (UBool
)(len
>0 && 0==strcmp(filename
+len
, ".dat"));
336 main(int argc
, char *argv
[]) {
337 const char *pname
, *sourcePath
, *destPath
, *inFilename
, *outFilename
, *outComment
;
339 UBool isHelp
, isModified
, isPackage
;
341 Package
*pkg
, *listPkg
;
343 U_MAIN_INIT_ARGS(argc
, argv
);
345 /* get the program basename */
346 pname
=findBasename(argv
[0]);
348 argc
=u_parseArgs(argc
, argv
, LENGTHOF(options
), options
);
349 isHelp
=options
[OPT_HELP_H
].doesOccur
|| options
[OPT_HELP_QUESTION_MARK
].doesOccur
;
351 printUsage(pname
, TRUE
);
354 if(argc
<2 || 3<argc
) {
355 printUsage(pname
, FALSE
);
356 return U_ILLEGAL_ARGUMENT_ERROR
;
361 fprintf(stderr
, "icupkg: not enough memory\n");
362 return U_MEMORY_ALLOCATION_ERROR
;
366 if(options
[OPT_SOURCEDIR
].doesOccur
) {
367 sourcePath
=options
[OPT_SOURCEDIR
].value
;
369 // work relative to the current working directory
372 if(options
[OPT_DESTDIR
].doesOccur
) {
373 destPath
=options
[OPT_DESTDIR
].value
;
375 // work relative to the current working directory
379 if(0==strcmp(argv
[1], "new")) {
384 if(isPackageName(inFilename
)) {
385 pkg
->readPackage(inFilename
);
388 /* swap a single file (icuswap replacement) rather than work on a package */
389 pkg
->addFile(sourcePath
, inFilename
);
396 if(0!=strcmp(argv
[1], argv
[2])) {
399 } else if(isPackage
) {
401 } else /* !isPackage */ {
402 outFilename
=inFilename
;
403 isModified
=(UBool
)(sourcePath
!=destPath
);
406 /* parse the output type option */
407 if(options
[OPT_OUT_TYPE
].doesOccur
) {
408 const char *type
=options
[OPT_OUT_TYPE
].value
;
409 if(type
[0]==0 || type
[1]!=0) {
410 /* the type must be exactly one letter */
411 printUsage(pname
, FALSE
);
412 return U_ILLEGAL_ARGUMENT_ERROR
;
421 printUsage(pname
, FALSE
);
422 return U_ILLEGAL_ARGUMENT_ERROR
;
426 * Set the isModified flag if the output type differs from the
427 * input package type.
428 * If we swap a single file, just assume that we are modifying it.
429 * The Package class does not give us access to the item and its type.
431 isModified
=(UBool
)(!isPackage
|| outType
!=pkg
->getInType());
432 } else if(isPackage
) {
433 outType
=pkg
->getInType(); // default to input type
434 } else /* !isPackage: swap single file */ {
435 outType
=0; /* tells extractItem() to not swap */
438 if(options
[OPT_WRITEPKG
].doesOccur
) {
444 * icuswap tool replacement: Only swap a single file.
445 * Check that irrelevant options are not set.
447 if( options
[OPT_COMMENT
].doesOccur
||
448 options
[OPT_COPYRIGHT
].doesOccur
||
449 options
[OPT_MATCHMODE
].doesOccur
||
450 options
[OPT_REMOVE_LIST
].doesOccur
||
451 options
[OPT_ADD_LIST
].doesOccur
||
452 options
[OPT_EXTRACT_LIST
].doesOccur
||
453 options
[OPT_LIST_ITEMS
].doesOccur
455 printUsage(pname
, FALSE
);
456 return U_ILLEGAL_ARGUMENT_ERROR
;
459 pkg
->extractItem(destPath
, outFilename
, 0, outType
);
466 /* Work with a package. */
468 if(options
[OPT_COMMENT
].doesOccur
) {
469 outComment
=options
[OPT_COMMENT
].value
;
470 } else if(options
[OPT_COPYRIGHT
].doesOccur
) {
471 outComment
=U_COPYRIGHT_STRING
;
476 if(options
[OPT_MATCHMODE
].doesOccur
) {
477 if(0==strcmp(options
[OPT_MATCHMODE
].value
, "noslash")) {
478 pkg
->setMatchMode(Package::MATCH_NOSLASH
);
480 printUsage(pname
, FALSE
);
481 return U_ILLEGAL_ARGUMENT_ERROR
;
486 if(options
[OPT_REMOVE_LIST
].doesOccur
) {
487 listPkg
=readList(NULL
, options
[OPT_REMOVE_LIST
].value
, FALSE
);
489 pkg
->removeItems(*listPkg
);
493 printUsage(pname
, FALSE
);
494 return U_ILLEGAL_ARGUMENT_ERROR
;
500 * use a separate Package so that its memory and items stay around
501 * as long as the main Package
503 if(options
[OPT_ADD_LIST
].doesOccur
) {
504 listPkg
=readList(sourcePath
, options
[OPT_ADD_LIST
].value
, TRUE
);
506 pkg
->addItems(*listPkg
);
510 printUsage(pname
, FALSE
);
511 return U_ILLEGAL_ARGUMENT_ERROR
;
516 if(options
[OPT_EXTRACT_LIST
].doesOccur
) {
517 listPkg
=readList(NULL
, options
[OPT_EXTRACT_LIST
].value
, FALSE
);
519 pkg
->extractItems(destPath
, *listPkg
, outType
);
522 printUsage(pname
, FALSE
);
523 return U_ILLEGAL_ARGUMENT_ERROR
;
528 if(options
[OPT_LIST_ITEMS
].doesOccur
) {
529 pkg
->listItems(stdout
);
532 /* check dependencies between items */
533 if(!pkg
->checkDependencies()) {
534 /* some dependencies are not fulfilled */
535 return U_MISSING_RESOURCE_ERROR
;
538 /* write the output .dat package if there are any modifications */
540 char outFilenameBuffer
[1024]; // for auto-generated output filename, if necessary
542 if(outFilename
==NULL
|| outFilename
[0]==0) {
543 if(inFilename
==NULL
|| inFilename
[0]==0) {
544 fprintf(stderr
, "icupkg: unable to auto-generate an output filename if there is no input filename\n");
545 exit(U_ILLEGAL_ARGUMENT_ERROR
);
549 * auto-generate a filename:
550 * copy the inFilename,
551 * and if the last basename character matches the input file's type,
552 * then replace it with the output file's type
554 char suffix
[6]="?.dat";
557 suffix
[0]=pkg
->getInType();
558 strcpy(outFilenameBuffer
, inFilename
);
559 s
=strchr(outFilenameBuffer
, 0);
560 if((s
-outFilenameBuffer
)>5 && 0==memcmp(s
-5, suffix
, 5)) {
563 outFilename
=outFilenameBuffer
;
565 pkg
->writePackage(outFilename
, outType
, outComment
);
573 * Hey, Emacs, please set the following:
576 * indent-tabs-mode: nil