gen_collate.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979
  1. /*
  2. * Usage:
  3. * gen_collate <INPUTDIR> [-o OUTPUTFILE] LOCALE ...
  4. *
  5. * Generate collation data from locales LOCALE.
  6. * Reads all LOCALE from INPUTDIR and writes collation data to OUTPUTFILE.
  7. *
  8. * The output file defaults to "locales_collate.h".
  9. */
  10. /* TODO:
  11. *
  12. * add UNDEFINED at end if not specified
  13. * convert POSITION -> FORWARD,POSITION
  14. *
  15. *
  16. * deal with lowercase in <Uhhhh>
  17. *
  18. * what about reorders that keep the same rule?
  19. *
  20. * remove "unused" collation elements? (probably doesn't save much)
  21. *
  22. * add_rule function ... returns index into rule table after possibly adding custom-indexed rule
  23. * but don't forget about multichar weights... replace with strings of indexes
  24. *
  25. */
  26. #include <stddef.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <stdarg.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <assert.h>
  35. #include <errno.h>
  36. #include <search.h>
  37. typedef struct {
  38. char *name; /* */
  39. int num_weights; /* */
  40. int ii_shift; /* */
  41. int ti_shift; /* */
  42. int ii_len; /* */
  43. int ti_len; /* */
  44. int max_weight; /* */
  45. int num_col_base; /* */
  46. int max_col_index; /* */
  47. int undefined_idx; /* */
  48. int range_low; /* */
  49. int range_count; /* high - low */
  50. int range_base_weight; /* */
  51. int num_starters; /* */
  52. int range_rule_offset; /* */
  53. int wcs2colidt_offset; /* */
  54. int index2weight_offset; /* */
  55. int index2ruleidx_offset; /* */
  56. int multistart_offset; /* */
  57. } base_locale_t;
  58. #define BASE_LOCALE_LEN 20
  59. static base_locale_t base_locale_array[BASE_LOCALE_LEN];
  60. static size_t base_locale_len;
  61. typedef struct {
  62. char *name; /* */
  63. int base_idx; /* */
  64. int undefined_idx; /* */
  65. int overrides_offset; /* */
  66. int multistart_offset; /* */
  67. } der_locale_t;
  68. #define DER_LOCALE_LEN 300
  69. static der_locale_t der_locale_array[DER_LOCALE_LEN];
  70. static size_t der_locale_len;
  71. #define OVERRIDE_LEN 50000
  72. static uint16_t override_buffer[OVERRIDE_LEN];
  73. static size_t override_len;
  74. #define MULTISTART_LEN 10000
  75. static uint16_t multistart_buffer[MULTISTART_LEN];
  76. static size_t multistart_len;
  77. #define WCS2COLIDT_LEN 200000
  78. static uint16_t wcs2colidt_buffer[WCS2COLIDT_LEN];
  79. static size_t wcs2colidt_len;
  80. #define INDEX2WEIGHT_LEN 200000
  81. static uint16_t index2weight_buffer[INDEX2WEIGHT_LEN];
  82. static size_t index2weight_len;
  83. static uint16_t index2ruleidx_buffer[INDEX2WEIGHT_LEN];
  84. static size_t index2ruleidx_len;
  85. #define WEIGHTSTR_LEN 10000
  86. static uint16_t weightstr_buffer[WEIGHTSTR_LEN];
  87. static size_t weightstr_len;
  88. #define RULETABLE_LEN (1L<<16)
  89. static uint16_t ruletable_buffer[RULETABLE_LEN];
  90. static size_t ruletable_len;
  91. #define RANGE (0x10000UL)
  92. typedef uint16_t tbl_item;
  93. static uint16_t u16_buf[10000];
  94. static int u16_buf_len;
  95. static int u16_starter;
  96. typedef struct {
  97. uint16_t ii_len;
  98. uint16_t ti_len;
  99. uint16_t ut_len;
  100. unsigned char ii_shift;
  101. unsigned char ti_shift;
  102. tbl_item *ii;
  103. tbl_item *ti;
  104. tbl_item *ut;
  105. } table_data;
  106. static size_t newopt(tbl_item *ut, size_t usize, int shift, table_data *tbl);
  107. #define MAX_COLLATION_WEIGHTS 4
  108. #define MAX_FNO 1
  109. #define MAX_FILES (MAX_FNO + 1)
  110. static FILE *fstack[MAX_FILES];
  111. static char *fname[MAX_FILES];
  112. static int lineno[MAX_FILES];
  113. static int fno = -1;
  114. static tbl_item wcs2index[RANGE];
  115. static char linebuf[1024];
  116. static char *pos;
  117. static char *pos_e = NULL;
  118. static char end_of_token = 0; /* slot to save */
  119. #define IN_ORDER 0x01
  120. #define IN_REORDER 0x02
  121. #define IN_REORDER_SECTIONS 0x04
  122. static int order_state;
  123. static int cur_num_weights; /* number of weights in current use */
  124. static char cur_rule[MAX_COLLATION_WEIGHTS];
  125. static int anonsection = 0;
  126. typedef struct ll_item_struct ll_item_t;
  127. struct ll_item_struct {
  128. ll_item_t *next;
  129. ll_item_t *prev;
  130. void *data;
  131. int data_type;
  132. int idx;
  133. };
  134. static ll_item_t *reorder_section_ptr = NULL;
  135. static int superset;
  136. static int superset_order_start_cnt; /* only support one order for now */
  137. static int superset_in_sync;
  138. static ll_item_t *comm_cur_ptr;
  139. static ll_item_t *comm_prev_ptr;
  140. enum {
  141. R_FORWARD = 0x01,
  142. R_POSITION = 0x02,
  143. R_BACKWARD = 0x04 /* must be largest in value */
  144. };
  145. typedef struct {
  146. size_t num_weights;
  147. char rule[MAX_COLLATION_WEIGHTS];
  148. const char *colitem[MAX_COLLATION_WEIGHTS];
  149. } weight_t;
  150. static void *root_weight = NULL;
  151. size_t unique_weights = 0;
  152. typedef struct {
  153. const char *symbol;
  154. weight_t *weight;
  155. } weighted_item_t;
  156. typedef struct {
  157. const char *symbol1;
  158. const char *symbol2;
  159. int length;
  160. weight_t *weight;
  161. } range_item_t;
  162. typedef struct {
  163. const char *name;
  164. ll_item_t *itm_list; /* weighted_item_t list .. circular!!! */
  165. size_t num_items;
  166. size_t num_rules;
  167. char rules[MAX_COLLATION_WEIGHTS];
  168. } section_t;
  169. static section_t *cur_section = NULL;
  170. typedef struct {
  171. const char *symbol;
  172. ll_item_t *node;
  173. } wi_index_t;
  174. typedef struct col_locale_struct col_locale_t;
  175. struct col_locale_struct {
  176. char *name;
  177. void *root_colitem; /* all base and derived, or just derived */
  178. void *root_element;
  179. void *root_scripts;
  180. void *root_wi_index;
  181. void *root_wi_index_reordered;
  182. ll_item_t *section_list;
  183. col_locale_t *base_locale; /* null if this is a base */
  184. void *root_derived_wi;
  185. ll_item_t *derived_list;
  186. void *root_starter_char;
  187. void *root_starter_all;
  188. ll_item_t *undefined_idx;
  189. };
  190. typedef struct {
  191. const char *symbol;
  192. int idx;
  193. } col_index_t;
  194. static void *root_col_locale = NULL;
  195. typedef struct {
  196. const char *keyword;
  197. void (*handler)(void);
  198. } keyword_table_t;
  199. typedef struct {
  200. const char *string;
  201. const char *element; /* NULL if collating symbol */
  202. } colitem_t;
  203. static col_locale_t *cur_base = NULL;
  204. static col_locale_t *cur_derived = NULL;
  205. static col_locale_t *cur_col = NULL;
  206. static void *root_sym = NULL;
  207. static size_t num_sym = 0;
  208. static size_t mem_sym = 0;
  209. static const char *inputdir;
  210. static size_t inputdir_len;
  211. static unsigned verbose = 0;
  212. enum {
  213. VINFO = (1<<0),
  214. VDETAIL = (1<<1),
  215. };
  216. static void error_msg(const char *fmt, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
  217. static void *xmalloc(size_t n);
  218. static char *xsymdup(const char *s); /* only allocate once... store in a tree */
  219. static void pushfile(char *filename);
  220. static void popfile(void);
  221. static void processfile(void);
  222. static int iscommentchar(int);
  223. static void eatwhitespace(void);
  224. static int next_line(void);
  225. static char *next_token(void);
  226. static void do_unrecognized(void);
  227. static col_locale_t *new_col_locale(char *name);
  228. static ll_item_t *new_ll_item(int data_type, void *data);
  229. static weight_t *register_weight(weight_t *w);
  230. static size_t ll_len(ll_item_t *l);
  231. static size_t ll_count(ll_item_t *l, int mask);
  232. static void add_wi_index(ll_item_t *l);
  233. static size_t tnumnodes(const void *root);
  234. static ll_item_t *find_wi_index(const char *sym, col_locale_t *cl);
  235. static void mark_reordered(const char *sym);
  236. static ll_item_t *find_wi_index_reordered(const char *sym);
  237. static ll_item_t *next_comm_ptr(void);
  238. static ll_item_t *init_comm_ptr(void);
  239. static ll_item_t *find_ll_last(ll_item_t *p);
  240. static void dump_weights(const char *name);
  241. static void finalize_base(void);
  242. static int is_ucode(const char *s);
  243. static int sym_cmp(const void *n1, const void *n2);
  244. static void do_starter_lists(col_locale_t *cl);
  245. static void dump_base_locale(int n);
  246. static void dump_der_locale(int n);
  247. static void dump_collate(FILE *fp);
  248. enum {
  249. DT_SECTION = 0x01,
  250. DT_WEIGHTED = 0x02,
  251. DT_REORDER = 0x04, /* a section to support reorder_after */
  252. DT_COL_LOCALE = 0x08,
  253. DT_RANGE = 0x10,
  254. };
  255. static int verbose_msg(const unsigned lvl, const char *fmt, ...)
  256. {
  257. va_list arg;
  258. int ret = 0;
  259. if (verbose & lvl) {
  260. va_start(arg, fmt);
  261. ret = vfprintf(stderr, fmt, arg);
  262. va_end(arg);
  263. }
  264. return ret;
  265. }
  266. static section_t *new_section(const char *name)
  267. {
  268. section_t *p;
  269. char buf[128];
  270. p = xmalloc(sizeof(section_t));
  271. if (!name) { /* anonymous section */
  272. name = buf;
  273. snprintf(buf, sizeof(buf), "anon%05d", anonsection);
  274. ++anonsection;
  275. } else if (*name != '<') { /* reorder */
  276. name = buf;
  277. snprintf(buf, sizeof(buf), "%s %05d", cur_col->name, anonsection);
  278. ++anonsection;
  279. }
  280. /* verbose_msg(VDETAIL, "section %s\n", name); */
  281. p->name = xsymdup(name);
  282. p->itm_list = NULL;
  283. p->num_items = 0;
  284. p->num_rules = 0;
  285. memset(p->rules, 0, MAX_COLLATION_WEIGHTS);
  286. /* cur_num_weights = p->num_rules = 0; */
  287. /* memset(p->rules, 0, MAX_COLLATION_WEIGHTS); */
  288. /* memset(cur_rule, R_FORWARD, 4); */
  289. if (*p->name == 'a') {
  290. cur_num_weights = p->num_rules = 4;
  291. memset(p->rules, R_FORWARD, 4);
  292. memset(cur_rule, R_FORWARD, 4);
  293. p->rules[3] |= R_POSITION;
  294. cur_rule[3] |= R_POSITION;
  295. }
  296. /* verbose_msg(VDETAIL, "new section %s -- cur_num_weights = %d\n", p->name, cur_num_weights); */
  297. return p;
  298. }
  299. static void do_order_start(void);
  300. static void do_order_end(void);
  301. static void do_reorder_after(void);
  302. static void do_reorder_end(void);
  303. static void do_reorder_sections_after(void);
  304. static void do_reorder_sections_end(void);
  305. static void do_copy(void);
  306. static void do_colsym(void);
  307. static void do_colele(void);
  308. static void do_script(void);
  309. static void do_range(void);
  310. static col_locale_t *new_col_locale(char *name);
  311. static int colitem_cmp(const void *n1, const void *n2);
  312. static int colelement_cmp(const void *n1, const void *n2);
  313. static void del_colitem(colitem_t *p);
  314. static colitem_t *new_colitem(char *item, char *def);
  315. static void add_colitem(char *item, char *def);
  316. static void add_script(const char *s);
  317. static unsigned int add_rule(weighted_item_t *wi);
  318. static unsigned int add_range_rule(range_item_t *ri);
  319. static const keyword_table_t keyword_table[] = {
  320. { "collating-symbol", do_colsym },
  321. { "collating-element", do_colele },
  322. { "script", do_script },
  323. { "copy", do_copy },
  324. { "order_start", do_order_start },
  325. { "order_end", do_order_end },
  326. { "order-end", do_order_end },
  327. { "reorder-after", do_reorder_after },
  328. { "reorder-end", do_reorder_end },
  329. { "reorder-sections-after", do_reorder_sections_after },
  330. { "reorder-sections-end", do_reorder_sections_end },
  331. { "UCLIBC_RANGE", do_range },
  332. { NULL, do_unrecognized }
  333. };
  334. static void do_unrecognized(void)
  335. {
  336. #if 1
  337. error_msg("warning: unrecognized: %s", pos);
  338. #else
  339. /* verbose_msg(VDETAIL, "warning: unrecognized initial keyword \"%s\"\n", pos); */
  340. fprintf(stderr, "warning: unrecognized: %s", pos);
  341. if (end_of_token) {
  342. fprintf(stderr, "%c%s", end_of_token, pos_e+1);
  343. }
  344. fprintf(stderr, "\n");
  345. #endif
  346. }
  347. /* typedef struct { */
  348. /* const char *symbol1; */
  349. /* const char *symbol2; */
  350. /* int length; */
  351. /* weight_t *weight; */
  352. /* } range_item_t; */
  353. static void do_range(void)
  354. {
  355. range_item_t *ri;
  356. weight_t w;
  357. int i;
  358. char *s;
  359. char *s1;
  360. char *s2;
  361. const char **ci;
  362. ll_item_t *lli;
  363. assert(!superset);
  364. assert(order_state == IN_ORDER);
  365. s1 = next_token();
  366. if (!s1) {
  367. error_msg("missing start of range");
  368. }
  369. if (!is_ucode(s1)) {
  370. error_msg("start of range is not a ucode: %s", s1);
  371. }
  372. s1 = xsymdup(s1);
  373. s2 = next_token();
  374. if (!s2) {
  375. error_msg("missing end of range");
  376. }
  377. if (!is_ucode(s2)) {
  378. error_msg("end of range is not a ucode: %s", s2);
  379. }
  380. s2 = xsymdup(s2);
  381. ri = (range_item_t *) xmalloc(sizeof(range_item_t));
  382. ri->symbol1 = s1;
  383. ri->symbol2 = s2;
  384. ri->length = strtoul(s2+2, NULL, 16) - strtoul(s1+2, NULL, 16);
  385. if (ri->length <= 0) {
  386. error_msg("illegal range length %d", ri->length);
  387. }
  388. s = next_token();
  389. w.num_weights = cur_num_weights;
  390. for (i=0 ; i < cur_num_weights ; i++) {
  391. w.rule[i] = cur_rule[i];
  392. }
  393. ci = w.colitem + (i-1);
  394. /* now i == cur_num_weights */
  395. #define STR_DITTO "."
  396. while (s && *s && i) {
  397. --i;
  398. if (*s == ';') {
  399. ci[-i] = xsymdup(STR_DITTO);
  400. if (*++s) {
  401. continue;
  402. }
  403. }
  404. if (*s) {
  405. ci[-i] = xsymdup(s);
  406. }
  407. s = next_token();
  408. if (s) {
  409. if (*s == ';') {
  410. ++s;
  411. } else if (i) {
  412. error_msg("missing seperator");
  413. }
  414. }
  415. }
  416. if (s) {
  417. error_msg("too many weights: %d %d |%s| %d", cur_num_weights, i, s, (int)*s);
  418. }
  419. while (i) { /* missing weights are not an error */
  420. --i;
  421. ci[-i] = xsymdup(STR_DITTO);
  422. }
  423. ri->weight = register_weight(&w);
  424. /* if ((i = is_ucode(t)) != 0) { */
  425. /* assert(!t[i]); */
  426. /* add_colitem(t, NULL); */
  427. /* } */
  428. lli = new_ll_item(DT_RANGE, ri);
  429. if (!cur_section->itm_list) {
  430. /* printf("creating new item list: %s\n", wi->symbol); */
  431. cur_section->itm_list = lli;
  432. lli->prev = lli->next = lli;
  433. ++cur_section->num_items;
  434. } else {
  435. insque(lli, cur_section->itm_list->prev);
  436. /* printf("adding item to list: %d - %s\n", ll_len(cur_section->itm_list), wi->symbol); */
  437. ++cur_section->num_items;
  438. }
  439. /* add_wi_index(lli); */
  440. }
  441. static weighted_item_t *add_weight(char *t)
  442. {
  443. weighted_item_t *wi;
  444. weight_t w;
  445. int i;
  446. char *s;
  447. const char **ci;
  448. t = xsymdup(t);
  449. s = next_token();
  450. w.num_weights = cur_num_weights;
  451. for (i=0 ; i < cur_num_weights ; i++) {
  452. w.rule[i] = cur_rule[i];
  453. }
  454. ci = w.colitem + (i-1);
  455. /* now i == cur_num_weights */
  456. while (s && *s && i) {
  457. --i;
  458. if (*s == ';') {
  459. ci[-i] = xsymdup(STR_DITTO);
  460. if (*++s) {
  461. continue;
  462. }
  463. }
  464. if (*s) {
  465. if (!strcmp(s,t)) {
  466. s = STR_DITTO;
  467. }
  468. ci[-i] = xsymdup(s);
  469. }
  470. s = next_token();
  471. if (s) {
  472. if (*s == ';') {
  473. ++s;
  474. } else if (i) {
  475. error_msg("missing seperator");
  476. }
  477. }
  478. }
  479. if (s) {
  480. error_msg("too many weights: %d %d |%s| %d", cur_num_weights, i, s, (int)*s);
  481. }
  482. while (i) { /* missing weights are not an error */
  483. --i;
  484. ci[-i] = xsymdup(STR_DITTO);
  485. }
  486. wi = xmalloc(sizeof(weighted_item_t));
  487. wi->symbol = t;
  488. wi->weight = register_weight(&w);
  489. if ((i = is_ucode(t)) != 0) {
  490. assert(!t[i]);
  491. add_colitem(t, NULL);
  492. }
  493. return wi;
  494. }
  495. static void add_superset_weight(char *t)
  496. {
  497. ll_item_t *lli;
  498. weighted_item_t *wi;
  499. if (!comm_cur_ptr
  500. || (strcmp(t, ((weighted_item_t *)(comm_cur_ptr->data))->symbol) != 0)
  501. ) { /* now out of sync */
  502. if (superset_in_sync) { /* need a new section */
  503. superset_in_sync = 0;
  504. cur_section = new_section("R");
  505. cur_num_weights = cur_section->num_rules
  506. = ((section_t *)(cur_base->section_list->data))->num_rules;
  507. memcpy(cur_rule,
  508. ((section_t *)(cur_base->section_list->data))->rules,
  509. MAX_COLLATION_WEIGHTS);
  510. memcpy(cur_section->rules,
  511. ((section_t *)(cur_base->section_list->data))->rules,
  512. MAX_COLLATION_WEIGHTS);
  513. insque(new_ll_item(DT_REORDER, cur_section), find_ll_last(cur_col->section_list));
  514. assert(comm_prev_ptr);
  515. lli = new_ll_item(DT_REORDER, cur_section);
  516. lli->prev = lli->next = lli;
  517. insque(lli, comm_prev_ptr);
  518. /* verbose_msg(VDETAIL, " subsection -----------------------\n"); */
  519. }
  520. /* verbose_msg(VDETAIL, " %s %s\n", t, ((weighted_item_t *)(comm_cur_ptr->data))->symbol); */
  521. wi = add_weight(t);
  522. lli = new_ll_item(DT_WEIGHTED, wi);
  523. mark_reordered(wi->symbol);
  524. /* printf("reorder: %s\n", t); */
  525. if (!cur_section->itm_list) {
  526. cur_section->itm_list = lli;
  527. lli->prev = lli->next = lli;
  528. ++cur_section->num_items;
  529. } else {
  530. insque(lli, cur_section->itm_list->prev);
  531. ++cur_section->num_items;
  532. }
  533. add_wi_index(lli);
  534. } else { /* in sync */
  535. superset_in_sync = 1;
  536. next_comm_ptr();
  537. }
  538. }
  539. static void do_weight(char *t)
  540. {
  541. weighted_item_t *wi;
  542. ll_item_t *lli;
  543. if (superset) {
  544. add_superset_weight(t);
  545. return;
  546. }
  547. switch(order_state) {
  548. case 0:
  549. /* fprintf(stdout, "no-order weight: %s\n", t); */
  550. /* break; */
  551. case IN_ORDER:
  552. /* in a section */
  553. /* fprintf(stdout, "weight: %s\n", t); */
  554. wi = add_weight(t);
  555. lli = new_ll_item(DT_WEIGHTED, wi);
  556. if (!cur_section->itm_list) {
  557. /* fprintf(stdout, "creating new item list: %s %s %p\n", wi->symbol, cur_section->name, lli); */
  558. cur_section->itm_list = lli;
  559. lli->prev = lli->next = lli;
  560. ++cur_section->num_items;
  561. } else {
  562. insque(lli, cur_section->itm_list->prev);
  563. /* fprintf(stdout, "adding item to list: %d - %s %p\n", ll_len(cur_section->itm_list), wi->symbol, lli); */
  564. ++cur_section->num_items;
  565. }
  566. add_wi_index(lli);
  567. break;
  568. case IN_REORDER:
  569. /* std rule - but in a block with an insert-after pt */
  570. wi = add_weight(t);
  571. lli = new_ll_item(DT_WEIGHTED, wi);
  572. mark_reordered(wi->symbol);
  573. /* fprintf(stdout, "reorder: %s %s %p\n", t, cur_section->name, lli); */
  574. if (!cur_section->itm_list) {
  575. cur_section->itm_list = lli;
  576. lli->prev = lli->next = lli;
  577. ++cur_section->num_items;
  578. } else {
  579. insque(lli, cur_section->itm_list->prev);
  580. ++cur_section->num_items;
  581. }
  582. add_wi_index(lli);
  583. break;
  584. case IN_REORDER_SECTIONS:
  585. t = xsymdup(t);
  586. if (next_token() != NULL) {
  587. error_msg("trailing text in reorder section item: %s", pos);
  588. }
  589. lli = cur_col->section_list;
  590. do {
  591. if (lli->data_type & DT_SECTION) {
  592. if (!strcmp(((section_t *)(lli->data))->name, t)) {
  593. lli->data_type = DT_REORDER;
  594. lli = new_ll_item(DT_REORDER, (section_t *)(lli->data));
  595. insque(lli, reorder_section_ptr);
  596. reorder_section_ptr = lli;
  597. return;
  598. }
  599. }
  600. lli = lli->next;
  601. } while (lli);
  602. error_msg("reorder_sections_after for non-base item currently not supported: %s", t);
  603. /* fprintf(stdout, "reorder_secitons: %s\n", t); */
  604. break;
  605. default:
  606. error_msg("invalid order_state %d", order_state);
  607. }
  608. }
  609. static int col_locale_cmp(const void *n1, const void *n2)
  610. {
  611. return strcmp(((const col_locale_t *) n1)->name, ((const col_locale_t *) n2)->name);
  612. }
  613. static void processfile(void)
  614. {
  615. char *t;
  616. const keyword_table_t *k;
  617. order_state = 0;
  618. /* cur_num_weights = 0; */
  619. /* cur_num_weights = 4; */
  620. /* memset(cur_rule, R_FORWARD, 4); */
  621. if (cur_col != cur_base) {
  622. cur_col->base_locale = cur_base;
  623. cur_col->undefined_idx = cur_base->undefined_idx;
  624. if (!cur_base->derived_list) {
  625. cur_base->derived_list = new_ll_item(DT_COL_LOCALE, cur_col);
  626. } else {
  627. insque(new_ll_item(DT_COL_LOCALE, cur_col), find_ll_last(cur_base->derived_list));
  628. }
  629. }
  630. if (tfind(cur_col, &root_col_locale, col_locale_cmp)) {
  631. error_msg("attempt to read locale: %s", cur_col->name);
  632. }
  633. if (!tsearch(cur_col, &root_col_locale, col_locale_cmp)) {
  634. error_msg("OUT OF MEMORY!");
  635. }
  636. if (superset) {
  637. superset_order_start_cnt = 0;
  638. superset_in_sync = 0;
  639. init_comm_ptr();
  640. }
  641. while (next_line()) {
  642. /* printf("%5d:", lineno[fno]); */
  643. /* while ((t = next_token()) != NULL) { */
  644. /* printf(" |%s|", t); */
  645. /* printf("\n"); */
  646. /* } */
  647. t = next_token();
  648. assert(t);
  649. assert(t == pos);
  650. if ((*t == '<') || (!strcmp(t, "UNDEFINED"))) {
  651. do_weight(t);
  652. } else {
  653. for (k = keyword_table ; k->keyword ; k++) {
  654. if (!strcmp(k->keyword, t)) {
  655. break;
  656. }
  657. }
  658. k->handler();
  659. }
  660. }
  661. if (cur_base == cur_col) {
  662. verbose_msg(VDETAIL, "Base: %15s", cur_col->name);
  663. } else {
  664. #if 1
  665. if (!cur_col->undefined_idx) {
  666. #if 0
  667. if (superset) {
  668. if (superset_order_start_cnt == 1) {
  669. --superset_order_start_cnt; /* ugh.. hack this */
  670. }
  671. }
  672. #endif
  673. /* This is an awful hack to get around the problem of unspecified UNDEFINED
  674. * definitions in the supported locales derived from iso14651_t1. */
  675. if (!strcmp(cur_base->name, "iso14651_t1")) {
  676. fprintf(stderr, "Warning: adding UNDEFINED entry for %s\n", cur_col->name);
  677. strcpy(linebuf, "script <UNDEFINED_SECTION>\n");
  678. pos_e = NULL;
  679. pos = linebuf;
  680. t = next_token();
  681. assert(t);
  682. assert(t == pos);
  683. do_script();
  684. strcpy(linebuf, "order_start <UNDEFINED_SECTION>;forward;backward;forward;forward,position\n");
  685. pos_e = NULL;
  686. pos = linebuf;
  687. t = next_token();
  688. assert(t);
  689. assert(t == pos);
  690. do_order_start();
  691. strcpy(linebuf, "UNDEFINED IGNORE;IGNORE;IGNORE\n");
  692. pos_e = NULL;
  693. pos = linebuf;
  694. t = next_token();
  695. assert(t);
  696. assert(t == pos);
  697. do_weight(t);
  698. strcpy(linebuf, "order_end\n");
  699. pos_e = NULL;
  700. pos = linebuf;
  701. t = next_token();
  702. assert(t);
  703. assert(t == pos);
  704. do_order_end();
  705. } else {
  706. error_msg("no definition of UNDEFINED for %s", cur_col->name);
  707. }
  708. }
  709. #endif
  710. verbose_msg(VDETAIL, " Der: %15s", cur_col->name);
  711. }
  712. {
  713. #if 0
  714. ll_item_t *p = cur_col->section_list;
  715. #endif
  716. verbose_msg(VDETAIL, "%6u weights", tnumnodes(cur_col->root_wi_index));
  717. if (cur_base) {
  718. verbose_msg(VDETAIL, " %6u der %6u reor %6u starter - %u new stubs",
  719. tnumnodes(cur_base->root_derived_wi),
  720. tnumnodes(cur_base->root_wi_index_reordered),
  721. tnumnodes(cur_base->root_starter_char),
  722. ll_count(cur_col->section_list, DT_REORDER));
  723. }
  724. verbose_msg(VDETAIL, "\n");
  725. #if 0
  726. while (p) {
  727. assert(((section_t *)(p->data))->num_items ==
  728. ll_len(((section_t *)(p->data))->itm_list));
  729. if (!p->next &&
  730. ((*((section_t *)(p->data))->name == 'a')
  731. && (((section_t *)(p->data))->num_items == 0))
  732. ) {
  733. break;
  734. }
  735. if (!(p->data_type & DT_REORDER)) {
  736. if ((*((section_t *)(p->data))->name != 'a')
  737. || (((section_t *)(p->data))->num_items > 0)
  738. ) {
  739. verbose_msg(VDETAIL,
  740. /* "\t%-15s %zu\n", */
  741. "\t%-15s %6u\n",
  742. ((section_t *)(p->data))->name,
  743. ((section_t *)(p->data))->num_items);
  744. }
  745. }
  746. p = p->next;
  747. }
  748. #endif
  749. }
  750. }
  751. static void print_colnode(const void *ptr, VISIT order, int level)
  752. {
  753. const colitem_t *p = *(const colitem_t **) ptr;
  754. if (order == postorder || order == leaf) {
  755. printf("collating item = \"%s\"", p->string);
  756. if (p->element) {
  757. printf(" is %s", p->element);
  758. }
  759. printf("\n");
  760. }
  761. }
  762. static void print_weight_node(const void *ptr, VISIT order, int level)
  763. {
  764. const weight_t *p = *(const weight_t **) ptr;
  765. int i;
  766. if (order == postorder || order == leaf) {
  767. printf("weight: (%d) ", p->num_weights);
  768. for (i = 0 ; i < p->num_weights ; i++) {
  769. if (p->rule[i] & R_FORWARD) {
  770. printf("F");
  771. }
  772. if (p->rule[i] & R_BACKWARD) {
  773. printf("B");
  774. }
  775. if (p->rule[i] & R_POSITION) {
  776. printf("P");
  777. }
  778. printf(",");
  779. }
  780. for (i = 0 ; i < p->num_weights ; i++) {
  781. printf(" %s", p->colitem[i]);
  782. }
  783. printf("\n");
  784. }
  785. }
  786. typedef struct {
  787. const char *der_name;
  788. int base_locale;
  789. } deps_t;
  790. enum {
  791. BASE_iso14651_t1,
  792. BASE_comm,
  793. BASE_cs_CZ,
  794. BASE_ar_SA,
  795. BASE_th_TH,
  796. BASE_ja_JP,
  797. BASE_ko_KR,
  798. BASE_MAX
  799. };
  800. static const char *base_name[] = {
  801. "iso14651_t1",
  802. "comm",
  803. "cs_CZ",
  804. "ar_SA",
  805. "th_TH",
  806. "ja_JP",
  807. "ko_KR"
  808. };
  809. static ll_item_t *locale_list[BASE_MAX];
  810. static void init_locale_list(void)
  811. {
  812. int i;
  813. for (i=0 ; i < BASE_MAX ; i++) {
  814. locale_list[i] = (ll_item_t *) xmalloc(sizeof(ll_item_t));
  815. locale_list[i]->prev = locale_list[i]->next = locale_list[i];
  816. locale_list[i]->data = (void *) base_name[i];
  817. }
  818. }
  819. deps_t deps[] = {
  820. { "af_ZA", BASE_iso14651_t1 },
  821. { "am_ET", BASE_iso14651_t1 },
  822. { "ar_AE", BASE_iso14651_t1 },
  823. { "ar_BH", BASE_iso14651_t1 },
  824. { "ar_DZ", BASE_iso14651_t1 },
  825. { "ar_EG", BASE_iso14651_t1 },
  826. { "ar_IN", BASE_iso14651_t1 },
  827. { "ar_IQ", BASE_iso14651_t1 },
  828. { "ar_JO", BASE_iso14651_t1 },
  829. { "ar_KW", BASE_iso14651_t1 },
  830. { "ar_LB", BASE_iso14651_t1 },
  831. { "ar_LY", BASE_iso14651_t1 },
  832. { "ar_MA", BASE_iso14651_t1 },
  833. { "ar_OM", BASE_iso14651_t1 },
  834. { "ar_QA", BASE_iso14651_t1 },
  835. { "ar_SA", BASE_ar_SA },
  836. { "ar_SD", BASE_iso14651_t1 },
  837. { "ar_SY", BASE_iso14651_t1 },
  838. { "ar_TN", BASE_iso14651_t1 },
  839. { "ar_YE", BASE_iso14651_t1 },
  840. { "az_AZ", BASE_iso14651_t1 },
  841. { "be_BY", BASE_iso14651_t1 },
  842. { "bg_BG", BASE_iso14651_t1 },
  843. { "bn_BD", BASE_iso14651_t1 },
  844. { "bn_IN", BASE_iso14651_t1 },
  845. { "br_FR", BASE_iso14651_t1 },
  846. { "bs_BA", BASE_iso14651_t1 },
  847. { "ca_ES", BASE_comm },
  848. { "cs_CZ", BASE_cs_CZ },
  849. { "cy_GB", BASE_iso14651_t1 },
  850. { "da_DK", BASE_comm },
  851. { "de_AT", BASE_iso14651_t1 },
  852. { "de_BE", BASE_iso14651_t1 },
  853. { "de_CH", BASE_iso14651_t1 },
  854. { "de_DE", BASE_iso14651_t1 },
  855. { "de_LU", BASE_iso14651_t1 },
  856. { "el_GR", BASE_iso14651_t1 },
  857. { "en_AU", BASE_iso14651_t1 },
  858. { "en_BW", BASE_iso14651_t1 },
  859. { "en_CA", BASE_comm },
  860. { "en_DK", BASE_iso14651_t1 },
  861. { "en_GB", BASE_iso14651_t1 },
  862. { "en_HK", BASE_iso14651_t1 },
  863. { "en_IE", BASE_iso14651_t1 },
  864. { "en_IN", BASE_iso14651_t1 },
  865. { "en_NZ", BASE_iso14651_t1 },
  866. { "en_PH", BASE_iso14651_t1 },
  867. { "en_SG", BASE_iso14651_t1 },
  868. { "en_US", BASE_iso14651_t1 },
  869. { "en_ZA", BASE_iso14651_t1 },
  870. { "en_ZW", BASE_iso14651_t1 },
  871. { "eo_EO", BASE_iso14651_t1 },
  872. { "es_AR", BASE_comm },
  873. { "es_BO", BASE_comm },
  874. { "es_CL", BASE_comm },
  875. { "es_CO", BASE_comm },
  876. { "es_CR", BASE_comm },
  877. { "es_DO", BASE_comm },
  878. { "es_EC", BASE_comm },
  879. { "es_ES", BASE_comm },
  880. { "es_GT", BASE_comm },
  881. { "es_HN", BASE_comm },
  882. { "es_MX", BASE_comm },
  883. { "es_NI", BASE_comm },
  884. { "es_PA", BASE_comm },
  885. { "es_PE", BASE_comm },
  886. { "es_PR", BASE_comm },
  887. { "es_PY", BASE_comm },
  888. { "es_SV", BASE_comm },
  889. { "es_US", BASE_comm },
  890. { "es_UY", BASE_comm },
  891. { "es_VE", BASE_comm },
  892. { "et_EE", BASE_comm },
  893. { "eu_ES", BASE_iso14651_t1 },
  894. { "fa_IR", BASE_iso14651_t1 },
  895. { "fi_FI", BASE_comm },
  896. { "fo_FO", BASE_comm },
  897. { "fr_BE", BASE_iso14651_t1 },
  898. { "fr_CA", BASE_comm },
  899. { "fr_CH", BASE_iso14651_t1 },
  900. { "fr_FR", BASE_iso14651_t1 },
  901. { "fr_LU", BASE_iso14651_t1 },
  902. { "ga_IE", BASE_iso14651_t1 },
  903. { "gd_GB", BASE_iso14651_t1 },
  904. { "gl_ES", BASE_comm },
  905. { "gv_GB", BASE_iso14651_t1 },
  906. { "he_IL", BASE_iso14651_t1 },
  907. { "hi_IN", BASE_iso14651_t1 },
  908. { "hr_HR", BASE_comm },
  909. { "hu_HU", BASE_iso14651_t1 },
  910. { "hy_AM", BASE_iso14651_t1 },
  911. { "id_ID", BASE_iso14651_t1 },
  912. { "is_IS", BASE_comm },
  913. { "it_CH", BASE_iso14651_t1 },
  914. { "it_IT", BASE_iso14651_t1 },
  915. { "iw_IL", BASE_iso14651_t1 },
  916. { "ja_JP", BASE_ja_JP },
  917. { "ka_GE", BASE_iso14651_t1 },
  918. { "kl_GL", BASE_comm },
  919. { "ko_KR", BASE_ko_KR },
  920. { "kw_GB", BASE_iso14651_t1 },
  921. { "lt_LT", BASE_comm },
  922. { "lv_LV", BASE_comm },
  923. { "mi_NZ", BASE_iso14651_t1 },
  924. { "mk_MK", BASE_iso14651_t1 },
  925. { "mr_IN", BASE_iso14651_t1 },
  926. { "ms_MY", BASE_iso14651_t1 },
  927. { "mt_MT", BASE_iso14651_t1 },
  928. { "nl_BE", BASE_iso14651_t1 },
  929. { "nl_NL", BASE_iso14651_t1 },
  930. { "nn_NO", BASE_iso14651_t1 },
  931. { "no_NO", BASE_comm },
  932. { "oc_FR", BASE_iso14651_t1 },
  933. { "pl_PL", BASE_comm },
  934. { "pt_BR", BASE_iso14651_t1 },
  935. { "pt_PT", BASE_iso14651_t1 },
  936. { "ro_RO", BASE_iso14651_t1 },
  937. { "ru_RU", BASE_iso14651_t1 },
  938. { "ru_UA", BASE_iso14651_t1 },
  939. { "se_NO", BASE_iso14651_t1 },
  940. { "sk_SK", BASE_cs_CZ },
  941. { "sl_SI", BASE_comm },
  942. { "sq_AL", BASE_iso14651_t1 },
  943. { "sr_YU", BASE_iso14651_t1 },
  944. { "sv_FI", BASE_comm },
  945. { "sv_SE", BASE_iso14651_t1 },
  946. { "ta_IN", BASE_iso14651_t1 },
  947. { "te_IN", BASE_iso14651_t1 },
  948. { "tg_TJ", BASE_iso14651_t1 },
  949. { "th_TH", BASE_th_TH },
  950. { "ti_ER", BASE_iso14651_t1 },
  951. { "ti_ET", BASE_iso14651_t1 },
  952. { "tl_PH", BASE_iso14651_t1 },
  953. { "tr_TR", BASE_comm },
  954. { "tt_RU", BASE_iso14651_t1 },
  955. { "uk_UA", BASE_iso14651_t1 },
  956. { "ur_PK", BASE_iso14651_t1 },
  957. { "uz_UZ", BASE_iso14651_t1 },
  958. { "vi_VN", BASE_iso14651_t1 },
  959. { "wa_BE", BASE_iso14651_t1 },
  960. { "yi_US", BASE_iso14651_t1 },
  961. { "zh_CN", BASE_iso14651_t1 },
  962. { "zh_HK", BASE_iso14651_t1 },
  963. { "zh_SG", BASE_iso14651_t1 },
  964. { "zh_TW", BASE_iso14651_t1 },
  965. };
  966. static int der_count[BASE_MAX];
  967. static const char *new_args[500];
  968. static int new_arg_count;
  969. static int dep_cmp(const void *s1, const void *s2)
  970. {
  971. return strcmp( (const char *) s1, ((const deps_t *) s2)->der_name);
  972. }
  973. static int old_main(int argc, char **argv);
  974. int main(int argc, char **argv)
  975. {
  976. const deps_t *p;
  977. ll_item_t *lli;
  978. int i;
  979. int total;
  980. char *output_file = "locale_collate.h";
  981. unsigned verbosity = 0;
  982. if (argc < 3) {
  983. return EXIT_FAILURE;
  984. }
  985. --argc;
  986. inputdir = strdup(*++argv);
  987. inputdir_len = strlen(inputdir);
  988. init_locale_list();
  989. while (--argc) {
  990. ++argv;
  991. if (!strcmp(*argv, "-o")) {
  992. --argc;
  993. if (*++argv == NULL) {
  994. printf("-o <outfile> requires an argument\n");
  995. return EXIT_FAILURE;
  996. }
  997. output_file = strdup(*argv);
  998. continue;
  999. } else if (!strcmp(*argv, "-v")) {
  1000. verbosity++;
  1001. continue;
  1002. }
  1003. p = (const deps_t *) bsearch(*argv, deps, sizeof(deps)/sizeof(deps[0]), sizeof(deps[0]), dep_cmp);
  1004. if (!p) {
  1005. if (!strcmp("C", *argv)) {
  1006. printf("ignoring %s locale\n", *argv);
  1007. continue;
  1008. } else {
  1009. printf("%s not found\n", *argv);
  1010. return EXIT_FAILURE;
  1011. }
  1012. }
  1013. i = p->base_locale;
  1014. ++der_count[i];
  1015. if (!strcmp(base_name[i], *argv)) {
  1016. /* same name as base, so skip after count incremented */
  1017. continue;
  1018. }
  1019. /* add it to the list. the main body will catch duplicates */
  1020. lli = (ll_item_t *) xmalloc(sizeof(ll_item_t));
  1021. lli->prev = lli->next = NULL;
  1022. lli->data = (void *) *argv;
  1023. insque(lli, locale_list[i]);
  1024. }
  1025. total = 0;
  1026. for (i=0 ; i < BASE_MAX ; i++) {
  1027. /* printf("der_count[%2d] = %3d\n", i, der_count[i]); */
  1028. total += der_count[i];
  1029. }
  1030. /* printf("total = %d\n", total); */
  1031. new_args[new_arg_count++] = "dummyprogramname";
  1032. for (i=0 ; i < BASE_MAX ; i++) {
  1033. if (!der_count[i]) {
  1034. continue;
  1035. }
  1036. new_args[new_arg_count++] = (i == BASE_comm) ? "-c" : "-b";
  1037. lli = locale_list[i];
  1038. do {
  1039. new_args[new_arg_count++] = (const char *) (lli->data);
  1040. lli = lli->next;
  1041. } while (lli != locale_list[i]);
  1042. new_args[new_arg_count++] = "-f";
  1043. }
  1044. for (i=0; i < verbosity; i++)
  1045. new_args[new_arg_count++] = "-v";
  1046. new_args[new_arg_count++] = "-o";
  1047. new_args[new_arg_count++] = output_file;
  1048. /*
  1049. for (i=0 ; i < new_arg_count ; i++) {
  1050. printf("%3d: %s\n", i, new_args[i]);
  1051. }
  1052. */
  1053. return old_main(new_arg_count, (char **) new_args);
  1054. }
  1055. /* usage... prog -b basefile derived {derived} -s single {single} */
  1056. static int old_main(int argc, char **argv)
  1057. {
  1058. int next_is_base = 0;
  1059. int next_is_subset = 0;
  1060. char *output_file = NULL;
  1061. superset = 0;
  1062. while (--argc) {
  1063. ++argv;
  1064. if (**argv == '-') {
  1065. if ((*argv)[1] == 'd') {
  1066. dump_weights((*argv) + 2);
  1067. } else if ((*argv)[1] == 'f') { /* dump all weight rules */
  1068. finalize_base();
  1069. } else if ((*argv)[1] == 'R') { /* dump all weight rules */
  1070. twalk(root_weight, print_weight_node);
  1071. } else if (((*argv)[1] == 'c') && !(*argv)[2]) { /* new common subset */
  1072. cur_base = cur_derived = NULL;
  1073. next_is_subset = 1;
  1074. next_is_base = 1;
  1075. superset = 0;
  1076. } else if (((*argv)[1] == 'b') && !(*argv)[2]) { /* new base locale */
  1077. cur_base = cur_derived = NULL;
  1078. next_is_subset = 0;
  1079. next_is_base = 1;
  1080. superset = 0;
  1081. } else if (((*argv)[1] == 's') && !(*argv)[2]) { /* single locales follow */
  1082. cur_base = cur_derived = NULL;
  1083. next_is_subset = 0;
  1084. next_is_base = 2;
  1085. superset = 0;
  1086. } else if (((*argv)[1] == 'o') && !(*argv)[2]) { /* output file */
  1087. --argc;
  1088. output_file = *++argv;
  1089. } else if (((*argv)[1] == 'v') && !(*argv)[2]) { /* verbose */
  1090. ++verbose;
  1091. } else {
  1092. error_msg("unrecognized option %s", *argv);
  1093. }
  1094. continue;
  1095. }
  1096. /* new file */
  1097. new_col_locale(*argv); /* automaticly sets cur_col */
  1098. if (next_is_base) {
  1099. cur_base = cur_col;
  1100. } else {
  1101. cur_derived = cur_col;
  1102. }
  1103. pushfile(*argv);
  1104. /* verbose_msg(VDETAIL, "processing file %s\n", *argv); */
  1105. processfile(); /* this does a popfile */
  1106. /* twalk(cur_col->root_colitem, print_colnode); */
  1107. if (next_is_base == 1) {
  1108. next_is_base = 0;
  1109. }
  1110. if (next_is_subset) {
  1111. next_is_subset = 0;
  1112. superset = 1;
  1113. }
  1114. }
  1115. verbose_msg(VINFO, "success!\n");
  1116. verbose_msg(VINFO,
  1117. /* "num_sym=%zu mem_sym=%zu unique_weights=%zu\n", */
  1118. "num_sym=%u mem_sym=%u unique_weights=%u\n",
  1119. num_sym, mem_sym, unique_weights);
  1120. /* twalk(root_weight, print_weight_node); */
  1121. verbose_msg(VINFO, "num base locales = %d num derived locales = %d\n",
  1122. base_locale_len, der_locale_len);
  1123. verbose_msg(VINFO,
  1124. "override_len = %d multistart_len = %d weightstr_len = %d\n"
  1125. "wcs2colidt_len = %d index2weight_len = %d index2ruleidx_len = %d\n"
  1126. "ruletable_len = %d\n"
  1127. "total size is %d bytes or %d kB\n",
  1128. override_len, multistart_len, weightstr_len,
  1129. wcs2colidt_len, index2weight_len, index2ruleidx_len,
  1130. ruletable_len,
  1131. (override_len + multistart_len + weightstr_len
  1132. + wcs2colidt_len + index2weight_len + index2ruleidx_len + ruletable_len) * 2,
  1133. (override_len + multistart_len + weightstr_len
  1134. + wcs2colidt_len + index2weight_len + index2ruleidx_len + ruletable_len + 511) / 512);
  1135. #if 0
  1136. {
  1137. int i;
  1138. for (i=0 ; i < base_locale_len ; i++) {
  1139. dump_base_locale(i);
  1140. }
  1141. for (i=0 ; i < der_locale_len ; i++) {
  1142. dump_der_locale(i);
  1143. }
  1144. }
  1145. #endif
  1146. {
  1147. FILE *fp = fopen(output_file, "w");
  1148. if (!fp) {
  1149. error_msg("cannot open output file '%s'!", output_file);
  1150. }
  1151. dump_collate(fp);
  1152. if (ferror(fp) || fclose(fp)) {
  1153. error_msg("write error or close error for output file!\n");
  1154. }
  1155. }
  1156. return EXIT_SUCCESS;
  1157. }
  1158. static void error_msg(const char *fmt, ...)
  1159. {
  1160. va_list arg;
  1161. fprintf(stderr, "Error: ");
  1162. if (fno >= 0) {
  1163. fprintf(stderr, "file %s (%d): ", fname[fno], lineno[fno]);
  1164. }
  1165. va_start(arg, fmt);
  1166. vfprintf(stderr, fmt, arg);
  1167. va_end(arg);
  1168. fprintf(stderr, "\n");
  1169. exit(EXIT_FAILURE);
  1170. }
  1171. static void pushfile(char *filename)
  1172. {
  1173. char *inputfile;
  1174. size_t inputfile_len;
  1175. if (fno >= MAX_FNO) {
  1176. error_msg("file stack size exceeded");
  1177. }
  1178. inputfile_len = inputdir_len + strlen(filename) + 2;
  1179. inputfile = xmalloc(inputfile_len);
  1180. memset(inputfile, 0, inputfile_len);
  1181. sprintf(inputfile, "%s/%s", inputdir, filename);
  1182. if (!(fstack[++fno] = fopen(inputfile, "r"))) {
  1183. --fno; /* oops */
  1184. error_msg("cannot open file %s: %s", inputfile, strerror(errno));
  1185. }
  1186. fname[fno] = xsymdup(inputfile);
  1187. lineno[fno] = 0;
  1188. }
  1189. static void popfile(void)
  1190. {
  1191. if (fno < 0) {
  1192. error_msg("pop on empty file stack");
  1193. }
  1194. /* free(fname[fno]); */
  1195. fclose(fstack[fno]);
  1196. --fno;
  1197. }
  1198. static void eatwhitespace(void)
  1199. {
  1200. while (isspace(*pos)) {
  1201. ++pos;
  1202. }
  1203. }
  1204. static int iscommentchar(int c)
  1205. {
  1206. return ((c == '#') || (c == '%'));
  1207. }
  1208. static int next_line(void)
  1209. {
  1210. size_t n;
  1211. char *s = linebuf;
  1212. assert(fno >= 0);
  1213. pos_e = NULL;
  1214. do {
  1215. if (fgets(s, sizeof(linebuf), fstack[fno]) != NULL) {
  1216. ++lineno[fno];
  1217. n = strlen(linebuf);
  1218. if ((n == sizeof(linebuf) - 1) && (linebuf[n-1] != '\n')) {
  1219. /* Either line is too long or last line is very long with
  1220. * no trailing newline. But we'll always treat it as an
  1221. * errro. */
  1222. error_msg("line too long?");
  1223. }
  1224. --n;
  1225. /* Be careful... last line doesn't need a newline. */
  1226. if (linebuf[n] == '\n') {
  1227. linebuf[n--] = 0; /* trim trailing newline */
  1228. }
  1229. pos = linebuf;
  1230. eatwhitespace();
  1231. if (*pos && !iscommentchar(*pos)) { /* not empty or comment line */
  1232. return 1; /* got a line */
  1233. }
  1234. } else { /* eof */
  1235. popfile();
  1236. }
  1237. } while (fno >= 0);
  1238. return 0;
  1239. }
  1240. static char *next_token(void)
  1241. {
  1242. char *p;
  1243. #if 0
  1244. if (pos_e == NULL) {
  1245. return NULL
  1246. pos = pos_e;
  1247. *pos = end_of_token;
  1248. end_of_token = 0;
  1249. }
  1250. #else
  1251. if (pos_e != NULL) {
  1252. pos = pos_e;
  1253. *pos = end_of_token;
  1254. end_of_token = 0;
  1255. }
  1256. #endif
  1257. eatwhitespace();
  1258. p = pos;
  1259. if (!*p || iscommentchar(*p)) { /* end of line or start of comment */
  1260. pos = pos_e = NULL;
  1261. *p = 0; /* treat comment as end of line */
  1262. /* fprintf(stdout, "returning NUL token |%s|\n", pos); */
  1263. return NULL;
  1264. #if 1
  1265. } else if (*p == '<') { /* collating symbol, element, or value */
  1266. while (*++p) {
  1267. if ((*p == '/') && p[1]) {
  1268. ++p;
  1269. continue;
  1270. }
  1271. if (*p == '>') {
  1272. pos_e = ++p;
  1273. end_of_token = *p;
  1274. *p = 0;
  1275. /* fprintf(stdout, "returning col token |%s|\n", pos); */
  1276. return pos;
  1277. }
  1278. }
  1279. } else if (*p == '"') { /* collating element value? */
  1280. while (*++p) {
  1281. if (*p == '"') { /* found the end of the quoted string */
  1282. pos_e = ++p;
  1283. end_of_token = *p;
  1284. *p = 0;
  1285. /* fprintf(stdout, "returning quote token |%s|\n", pos); */
  1286. return pos;
  1287. }
  1288. }
  1289. #endif
  1290. } else { /* some kind of keyword */
  1291. while (*++p) {
  1292. if (isspace(*p) || (*p == ';')) {
  1293. break;
  1294. }
  1295. }
  1296. pos_e = p;
  1297. end_of_token = *p;
  1298. *p = 0;
  1299. /* fprintf(stdout, "returning key token |%s|\n", pos); */
  1300. return pos;
  1301. }
  1302. error_msg("illegal token |%s|", pos);
  1303. }
  1304. static void *xmalloc(size_t n)
  1305. {
  1306. void *p;
  1307. if (!(p = malloc(n))) {
  1308. error_msg("OUT OF MEMORY");
  1309. }
  1310. return p;
  1311. }
  1312. static void do_copy(void)
  1313. {
  1314. char *s;
  1315. char *e;
  1316. if ((s = next_token()) != NULL) {
  1317. e = strchr(s + 1, '"');
  1318. if ((*s == '"') && e && (*e == '"') && !e[1]) {
  1319. if (next_token() != NULL) {
  1320. error_msg("illegal trailing text: %s", pos);
  1321. }
  1322. *e = 0;
  1323. ++s;
  1324. if (cur_base && !strcmp(cur_base->name,s)) {
  1325. /* verbose_msg(VDETAIL, "skipping copy of base file %s\n", s); */
  1326. return;
  1327. }
  1328. /* verbose_msg(VDETAIL, "full copy of %s\n", s); */
  1329. pushfile(s);
  1330. return;
  1331. }
  1332. }
  1333. error_msg("illegal or missing arg for copy: %s", s);
  1334. }
  1335. static void do_colsym(void)
  1336. {
  1337. char *s;
  1338. char *e;
  1339. if ((s = next_token()) != NULL) {
  1340. e = strrchr(s,'>');
  1341. if ((*s == '<') && e && (*e == '>') && !e[1]) {
  1342. if (next_token() != NULL) {
  1343. error_msg("illegal trailing text: %s", pos);
  1344. }
  1345. e[1] = 0; /* cleanup in case next_token stored something */
  1346. add_colitem(s,NULL);
  1347. return;
  1348. }
  1349. }
  1350. error_msg("illegal or missing arg for collating-symbol: %s", s);
  1351. }
  1352. static void do_colele(void)
  1353. {
  1354. char *s;
  1355. char *e;
  1356. char *s1;
  1357. char *e1;
  1358. int n;
  1359. if ((s = next_token()) != NULL) {
  1360. e = strrchr(s,'>');
  1361. if ((*s == '<') && e && (*e == '>') && !e[1]) {
  1362. if (((s1 = next_token()) == NULL)
  1363. || (strcmp(s1,"from") != 0)
  1364. || ((s1 = next_token()) == NULL)
  1365. || (*s1 != '\"')
  1366. ) {
  1367. error_msg("illegal format for collating-element spec");
  1368. }
  1369. e1 = strchr(s1 + 1, '"');
  1370. if ((*s1 != '"') || !e1 || (*e1 != '"') || (e1[1] != 0)) {
  1371. error_msg("illegal definition for collating-element: %s", s1);
  1372. }
  1373. if (next_token() != NULL) {
  1374. error_msg("illegal trailing text: %s", pos);
  1375. }
  1376. e[1] = 0; /* cleanup in case next_token stored something */
  1377. e1[1] = 0;
  1378. add_colitem(s,s1);
  1379. ++s1;
  1380. if (!(n = is_ucode(s1))) {
  1381. error_msg("starting char must be a <U####> code: %s", s1);
  1382. }
  1383. assert(s1[n] == '<');
  1384. s1[n] = 0;
  1385. s = xsymdup(s1);
  1386. if (!(tsearch(s, &cur_base->root_starter_char, sym_cmp))) {
  1387. error_msg("OUT OF MEMORY");
  1388. }
  1389. return;
  1390. }
  1391. }
  1392. error_msg("illegal or missing arg for collating-element: %s", s);
  1393. }
  1394. static ll_item_t *find_section_list_item(const char *name, col_locale_t *loc)
  1395. {
  1396. ll_item_t *p;
  1397. if (!loc) {
  1398. return NULL;
  1399. }
  1400. p = loc->section_list;
  1401. while (p) {
  1402. /* if (!((p->data_type == DT_SECTION) || (p->data_type == DT_REORDER))) { */
  1403. /* verbose_msg(VDETAIL, "fsli = %d\n", p->data_type); */
  1404. /* } */
  1405. assert((p->data_type == DT_SECTION) || (p->data_type == DT_REORDER));
  1406. if (!strcmp(name, ((section_t *)(p->data))->name)) {
  1407. break;
  1408. }
  1409. p = p->next;
  1410. }
  1411. return p;
  1412. }
  1413. static ll_item_t *find_ll_last(ll_item_t *p)
  1414. {
  1415. assert(p);
  1416. while (p->next) {
  1417. p = p->next;
  1418. }
  1419. return p;
  1420. }
  1421. static void do_script(void)
  1422. {
  1423. char *s;
  1424. char *e;
  1425. if ((s = next_token()) != NULL) {
  1426. e = strrchr(s,'>');
  1427. if ((*s == '<') && e && (*e == '>') && !e[1]) {
  1428. if (next_token() != NULL) {
  1429. error_msg("illegal trailing text: %s", pos);
  1430. }
  1431. e[1] = 0; /* cleanup in case next_token stored something */
  1432. add_script(s);
  1433. return;
  1434. }
  1435. }
  1436. error_msg("illegal or missing arg for script: %s", s);
  1437. }
  1438. static col_locale_t *new_col_locale(char *name)
  1439. {
  1440. ll_item_t *lli;
  1441. ll_item_t *lli2;
  1442. cur_col = (col_locale_t *) xmalloc(sizeof(col_locale_t));
  1443. cur_col->name = name;
  1444. cur_col->root_colitem = NULL;
  1445. cur_col->root_element = NULL;
  1446. cur_col->root_scripts = NULL;
  1447. cur_col->base_locale = NULL;
  1448. if (!superset) {
  1449. /* start with an anonymous section */
  1450. cur_section = new_section(NULL);
  1451. cur_col->section_list = new_ll_item(DT_SECTION, cur_section);
  1452. } else {
  1453. /* start with a reorder section */
  1454. cur_section = new_section("R");
  1455. cur_num_weights = cur_section->num_rules
  1456. = ((section_t *)(cur_base->section_list->data))->num_rules;
  1457. memcpy(cur_rule,
  1458. ((section_t *)(cur_base->section_list->data))->rules,
  1459. MAX_COLLATION_WEIGHTS);
  1460. memcpy(cur_section->rules,
  1461. ((section_t *)(cur_base->section_list->data))->rules,
  1462. MAX_COLLATION_WEIGHTS);
  1463. cur_col->section_list = new_ll_item(DT_REORDER, cur_section);
  1464. assert(cur_base->section_list->next == NULL); /* currently only one section allowed */
  1465. lli = ((section_t *)(cur_base->section_list->data))->itm_list;
  1466. assert(lli);
  1467. lli2 = new_ll_item(DT_REORDER, cur_section);
  1468. lli2->prev = lli2->next = lli2;
  1469. insque(lli2, lli->prev);
  1470. ((section_t *)(cur_base->section_list->data))->itm_list = lli2;
  1471. }
  1472. /* cur_col->section_list = NULL; */
  1473. /* add_script(((section_t *)(cur_col->section_list->data))->name); */
  1474. cur_col->root_wi_index = NULL;
  1475. cur_col->root_wi_index_reordered = NULL;
  1476. cur_col->root_derived_wi = NULL;
  1477. cur_col->derived_list = NULL;
  1478. cur_col->root_starter_char = NULL;
  1479. cur_col->root_starter_all = NULL;
  1480. cur_col->undefined_idx = NULL;
  1481. return cur_col;
  1482. }
  1483. static int colitem_cmp(const void *n1, const void *n2)
  1484. {
  1485. return strcmp(((colitem_t *)n1)->string, ((colitem_t *)n2)->string);
  1486. }
  1487. static int colelement_cmp(const void *n1, const void *n2)
  1488. {
  1489. int r;
  1490. r = strcmp(((colitem_t *)n1)->string, ((colitem_t *)n2)->string);
  1491. if (!r) {
  1492. if (((colitem_t *)n1)->element && ((colitem_t *)n2)->element) {
  1493. r = strcmp(((colitem_t *)n1)->element, ((colitem_t *)n2)->element);
  1494. } else if (((colitem_t *)n1)->element == ((colitem_t *)n2)->element) {
  1495. r = 0; /* both null */
  1496. } else {
  1497. r = (((colitem_t *)n1)->element == NULL) ? -1 : 1;
  1498. }
  1499. }
  1500. return r;
  1501. }
  1502. static void del_colitem(colitem_t *p)
  1503. {
  1504. /* free((void *) p->element); */
  1505. /* free((void *) p->string); */
  1506. free(p);
  1507. }
  1508. static colitem_t *new_colitem(char *item, char *def)
  1509. {
  1510. colitem_t *p;
  1511. p = xmalloc(sizeof(colitem_t));
  1512. p->string = xsymdup(item);
  1513. p->element = (!def) ? def : xsymdup(def);
  1514. return p;
  1515. }
  1516. static void add_colitem(char *item, char *def)
  1517. {
  1518. colitem_t *p;
  1519. #if 0
  1520. printf("adding collation item %s", item);
  1521. if (def) {
  1522. printf(" with definition %s", def);
  1523. }
  1524. printf("\n");
  1525. #endif
  1526. p = new_colitem(item, def);
  1527. if (superset) {
  1528. if (tfind(p, &cur_base->root_colitem, colitem_cmp)) {
  1529. /* verbose_msg(VDETAIL, "skipping superset duplicate collating item \"%s\"\n", p->string); */
  1530. del_colitem(p);
  1531. return;
  1532. /* } else { */
  1533. /* verbose_msg(VDETAIL, "superset: new collating item \"%s\" = %s\n", p->string, p->element); */
  1534. }
  1535. }
  1536. if (cur_col == cur_derived) {
  1537. if (!tfind(p, &cur_base->root_colitem, colitem_cmp)) {
  1538. /* not in current but could be in base */
  1539. if (!tsearch(p, &cur_base->root_colitem, colitem_cmp)) {
  1540. error_msg("OUT OF MEMORY!");
  1541. }
  1542. } else if (!tfind(p, &cur_base->root_colitem, colelement_cmp)) {
  1543. error_msg("collating element/symbol mismatch: item=%s def=%s", item, def);
  1544. }
  1545. }
  1546. if (!tfind(p, &cur_col->root_colitem, colitem_cmp)) {
  1547. /* not in current but could be in base */
  1548. if (!tsearch(p, &cur_col->root_colitem, colitem_cmp)) {
  1549. error_msg("OUT OF MEMORY!");
  1550. }
  1551. } else if (!tfind(p, &cur_col->root_colitem, colelement_cmp)) {
  1552. error_msg("collating element/symbol mismatch");
  1553. } else { /* already there */
  1554. fprintf(stderr, "duplicate collating item \"%s\"\n", p->string);
  1555. del_colitem(p);
  1556. }
  1557. }
  1558. /* add a script (section) to the current locale */
  1559. static void add_script(const char *s)
  1560. {
  1561. ll_item_t *l;
  1562. /* make sure it isn't in base if working with derived */
  1563. if (cur_base != cur_col) {
  1564. if (find_section_list_item(s, cur_base)) {
  1565. error_msg("attempt to add script %s for derived when already in base", s);
  1566. }
  1567. }
  1568. if (find_section_list_item(s, cur_col)) {
  1569. error_msg("attempt to readd script %s", s);
  1570. }
  1571. l = find_ll_last(cur_col->section_list);
  1572. insque(new_ll_item(DT_SECTION, new_section(s)), l);
  1573. }
  1574. static const char str_forward[] = "forward";
  1575. static const char str_backward[] = "backward";
  1576. static const char str_position[] = "position";
  1577. static void do_order_start(void)
  1578. {
  1579. const char *s;
  1580. char *e;
  1581. ll_item_t *l;
  1582. section_t *sect;
  1583. int rule;
  1584. if (order_state & ~IN_ORDER) {
  1585. error_msg("order_start following reorder{_sections}_after");
  1586. }
  1587. order_state |= IN_ORDER;
  1588. if (superset) {
  1589. if (++superset_order_start_cnt > 1) {
  1590. error_msg("currently only a common order_start is supported in superset");
  1591. }
  1592. return;
  1593. }
  1594. if (!(s = next_token())) {
  1595. s = str_forward; /* if no args */
  1596. }
  1597. if (*s == '<') { /* section (script) */
  1598. e = strrchr(s,'>');
  1599. if ((*s == '<') && e && (*e == '>') && !e[1]) {
  1600. e[1] = 0; /* cleanup in case next_token stored something */
  1601. if (!(l = find_section_list_item(s, cur_col))) {
  1602. error_msg("ref of undefined sections: %s", s);
  1603. }
  1604. sect = (section_t *)(l->data);
  1605. if (sect->num_rules) {
  1606. error_msg("sections already defined: %s", s);
  1607. }
  1608. } else {
  1609. error_msg("illegal section ref: %s", s);
  1610. }
  1611. if (!(s = next_token())) {
  1612. s = str_forward; /* if no args */
  1613. } else if (*s != ';') {
  1614. error_msg("missing seperator!");
  1615. }
  1616. } else { /* need an anonymous section */
  1617. if ((*cur_section->name != '<') && (cur_section->num_items == 0)) { /* already in an empty anonymous section */
  1618. sect = cur_section;
  1619. /* fprintf(stdout, "using empty anon section %s\n", sect->name); */
  1620. } else {
  1621. sect = new_section(NULL);
  1622. l = find_ll_last(cur_col->section_list);
  1623. insque(new_ll_item(DT_SECTION, sect), l);
  1624. /* fprintf(stdout, "adding order section after section %s\n", ((section_t *)(l->data))->name); */
  1625. /* fprintf(stdout, " last section is %s\n", ((section_t *)(l->next->data))->name); */
  1626. }
  1627. sect->num_rules = 0; /* setting this below so nix default */
  1628. }
  1629. cur_section = sect;
  1630. /* fprintf(stdout, "cur_section now %s\n", cur_section->name); */
  1631. /* now do rules */
  1632. do {
  1633. rule = 0;
  1634. if (*s == ';') {
  1635. ++s;
  1636. }
  1637. while (*s) {
  1638. if (!strncmp(str_forward, s, 7)) {
  1639. rule |= R_FORWARD;
  1640. s += 7;
  1641. } else if (!strncmp(str_backward, s, 8)) {
  1642. rule |= R_BACKWARD;
  1643. s += 8;
  1644. } else if (!strncmp(str_position, s, 8)) {
  1645. rule |= R_POSITION;
  1646. s += 8;
  1647. }
  1648. if (*s == ',') {
  1649. ++s;
  1650. continue;
  1651. }
  1652. if (!*s || (*s == ';')) {
  1653. if (sect->num_rules >= MAX_COLLATION_WEIGHTS) {
  1654. error_msg("more than %d weight rules!", MAX_COLLATION_WEIGHTS);
  1655. }
  1656. if (!rule) {
  1657. error_msg("missing weight rule!");
  1658. }
  1659. if ((rule & (R_FORWARD|R_BACKWARD|R_POSITION)) > R_BACKWARD) {
  1660. error_msg("backward paired with forward and/or position!");
  1661. }
  1662. sect->rules[sect->num_rules++] = rule;
  1663. rule = 0;
  1664. continue;
  1665. }
  1666. error_msg("illegal weight rule: %s", s);
  1667. }
  1668. } while ((s = next_token()) != NULL);
  1669. cur_section = sect;
  1670. /* verbose_msg(VDETAIL, "setting cur_num_weights to %d for %s\n", sect->num_rules, sect->name); */
  1671. cur_num_weights = sect->num_rules;
  1672. memcpy(cur_rule, sect->rules, MAX_COLLATION_WEIGHTS);
  1673. }
  1674. static void do_order_end(void)
  1675. {
  1676. if (!(order_state & IN_ORDER)) {
  1677. error_msg("order_end with no matching order_start");
  1678. }
  1679. order_state &= ~IN_ORDER;
  1680. cur_section = new_section(NULL);
  1681. }
  1682. static void do_reorder_after(void)
  1683. {
  1684. char *t;
  1685. ll_item_t *lli;
  1686. const weight_t *w;
  1687. int save_cur_num_weights;
  1688. char save_cur_rule[MAX_COLLATION_WEIGHTS];
  1689. if (order_state & ~IN_REORDER) {
  1690. error_msg("reorder_after following order_start or reorder_sections_after");
  1691. }
  1692. order_state |= IN_REORDER;
  1693. if (superset) {
  1694. error_msg("currently reorder_after is not supported in supersets");
  1695. }
  1696. if (!(t = next_token())) {
  1697. error_msg("missing arg for reorder_after");
  1698. }
  1699. t = xsymdup(t);
  1700. if (next_token() != NULL) {
  1701. error_msg("trailing text reorder_after: %s", pos);
  1702. }
  1703. if (cur_col == cur_base) {
  1704. error_msg("sorry.. reorder_after in base locale is not currently supported");
  1705. }
  1706. if (!(lli = find_wi_index(t, cur_base))) {
  1707. error_msg("reorder_after for non-base item currently not supported: %s", t);
  1708. }
  1709. w = ((weighted_item_t *)(lli->data))->weight;
  1710. save_cur_num_weights = cur_num_weights;
  1711. memcpy(save_cur_rule, cur_rule, MAX_COLLATION_WEIGHTS);
  1712. cur_section = new_section("R");
  1713. insque(new_ll_item(DT_REORDER, cur_section), lli);
  1714. #if 0
  1715. {
  1716. ll_item_t *l1;
  1717. ll_item_t *l2;
  1718. ll_item_t *l3;
  1719. l1 = new_ll_item(DT_REORDER, cur_section);
  1720. l2 = find_ll_last(cur_col->section_list);
  1721. insque(l1, l2);
  1722. l3 = find_ll_last(cur_col->section_list);
  1723. verbose_msg(VDETAIL, "reorder_after %p %p %p %s\n", l1, l2, l3, cur_section->name);
  1724. }
  1725. #else
  1726. insque(new_ll_item(DT_REORDER, cur_section), find_ll_last(cur_col->section_list));
  1727. #endif
  1728. cur_num_weights = cur_section->num_rules = save_cur_num_weights;
  1729. memcpy(cur_rule, save_cur_rule, MAX_COLLATION_WEIGHTS);
  1730. memcpy(cur_section->rules, save_cur_rule, MAX_COLLATION_WEIGHTS);
  1731. /* verbose_msg(VDETAIL, "reorder -- %s %d\n", ((weighted_item_t *)(lli->data))->symbol, w->num_weights); */
  1732. /* if (!w->num_weights) { */
  1733. /* } else { */
  1734. /* cur_num_weights = w->num_weights; */
  1735. /* memcpy(cur_rule, w->rule, MAX_COLLATION_WEIGHTS); */
  1736. /* } */
  1737. /* verbose_msg(VDETAIL, "reorder_after succeeded for %s\n", t); */
  1738. }
  1739. static void do_reorder_end(void)
  1740. {
  1741. if (!(order_state & IN_REORDER)) {
  1742. error_msg("reorder_end with no matching reorder_after");
  1743. }
  1744. order_state &= ~IN_REORDER;
  1745. }
  1746. static void do_reorder_sections_after(void)
  1747. {
  1748. const char *t;
  1749. ll_item_t *lli;
  1750. if (order_state & ~IN_REORDER_SECTIONS) {
  1751. error_msg("reorder_sections_after following order_start or reorder_after");
  1752. }
  1753. order_state |= IN_REORDER_SECTIONS;
  1754. if (superset) {
  1755. error_msg("currently reorder_sections_after is not supported in supersets");
  1756. }
  1757. if (!(t = next_token())) {
  1758. error_msg("missing arg for reorder_sections_after");
  1759. }
  1760. t = xsymdup(t);
  1761. if (next_token() != NULL) {
  1762. error_msg("trailing text reorder_sections_after: %s", pos);
  1763. }
  1764. if (cur_col == cur_base) {
  1765. error_msg("sorry.. reorder_sections_after in base locale is not currently supported");
  1766. }
  1767. lli = cur_base->section_list;
  1768. do {
  1769. /* verbose_msg(VDETAIL, "hmm -- |%s|%d|\n", ((section_t *)(lli->data))->name, lli->data_type); */
  1770. if (lli->data_type & DT_SECTION) {
  1771. /* verbose_msg(VDETAIL, "checking |%s|%s|\n", ((section_t *)(lli->data))->name, t); */
  1772. if (!strcmp(((section_t *)(lli->data))->name, t)) {
  1773. reorder_section_ptr = lli;
  1774. return;
  1775. }
  1776. }
  1777. lli = lli->next;
  1778. } while (lli);
  1779. error_msg("reorder_sections_after for non-base item currently not supported: %s", t);
  1780. }
  1781. static void do_reorder_sections_end(void)
  1782. {
  1783. if (!(order_state & IN_REORDER_SECTIONS)) {
  1784. error_msg("reorder_sections_end with no matching reorder_sections_after");
  1785. }
  1786. order_state &= ~IN_REORDER_SECTIONS;
  1787. reorder_section_ptr = NULL;
  1788. }
  1789. static ll_item_t *new_ll_item(int data_type, void *data)
  1790. {
  1791. ll_item_t *p;
  1792. p = xmalloc(sizeof(ll_item_t));
  1793. p->next = p->prev = NULL;
  1794. p->data_type = data_type;
  1795. p->data = data;
  1796. p->idx = INT_MIN;
  1797. return p;
  1798. }
  1799. static int sym_cmp(const void *n1, const void *n2)
  1800. {
  1801. /* verbose_msg(VDETAIL, "sym_cmp: |%s| |%s|\n", (const char *)n1, (const char *)n2); */
  1802. return strcmp((const char *) n1, (const char *) n2);
  1803. }
  1804. static char *xsymdup(const char *s)
  1805. {
  1806. void *p;
  1807. if (!(p = tfind(s, &root_sym, sym_cmp))) { /* not a currently known symbol */
  1808. if (!(s = strdup(s)) || !(p = tsearch(s, &root_sym, sym_cmp))) {
  1809. error_msg("OUT OF MEMORY!");
  1810. }
  1811. ++num_sym;
  1812. mem_sym += strlen(s) + 1;
  1813. /* verbose_msg(VDETAIL, "xsymdup: alloc |%s| %p |%s| %p\n", *(char **)p, p, s, s); */
  1814. /* } else { */
  1815. /* verbose_msg(VDETAIL, "xsymdup: found |%s| %p\n", *(char **)p, p); */
  1816. }
  1817. return *(char **) p;
  1818. }
  1819. static int weight_cmp(const void *n1, const void *n2)
  1820. {
  1821. const weight_t *w1 = (const weight_t *) n1;
  1822. const weight_t *w2 = (const weight_t *) n2;
  1823. int i, r;
  1824. if (w1->num_weights != w2->num_weights) {
  1825. return w1->num_weights - w2->num_weights;
  1826. }
  1827. for (i=0 ; i < w1->num_weights ; i++) {
  1828. if (w1->rule[i] != w2->rule[i]) {
  1829. return w1->rule[i] - w2->rule[i];
  1830. }
  1831. if ((r = strcmp(w1->colitem[i], w2->colitem[i])) != 0) {
  1832. return r;
  1833. }
  1834. }
  1835. return 0;
  1836. }
  1837. static weight_t *register_weight(weight_t *w)
  1838. {
  1839. void *p;
  1840. if (!(p = tfind(w, &root_weight, weight_cmp))) { /* new weight */
  1841. p = xmalloc(sizeof(weight_t));
  1842. memcpy(p, w, sizeof(weight_t));
  1843. if (!(p = tsearch(p, &root_weight, weight_cmp))) {
  1844. error_msg("OUT OF MEMORY!");
  1845. }
  1846. ++unique_weights;
  1847. /* } else { */
  1848. /* verbose_msg(VDETAIL, "rw: found\n"); */
  1849. }
  1850. return *(weight_t **)p;
  1851. }
  1852. static size_t ll_len(ll_item_t *l)
  1853. {
  1854. size_t n = 0;
  1855. ll_item_t *p = l;
  1856. while (p) {
  1857. ++n;
  1858. p = p->next;
  1859. if (p == l) { /* work for circular too */
  1860. break;
  1861. }
  1862. }
  1863. return n;
  1864. }
  1865. static size_t ll_count(ll_item_t *l, int mask)
  1866. {
  1867. size_t n = 0;
  1868. ll_item_t *p = l;
  1869. while (p) {
  1870. if (p->data_type & mask) {
  1871. ++n;
  1872. }
  1873. p = p->next;
  1874. if (p == l) { /* work for circular too */
  1875. break;
  1876. }
  1877. }
  1878. return n;
  1879. }
  1880. static int wi_index_cmp(const void *n1, const void *n2)
  1881. {
  1882. const char *s1 = ((weighted_item_t *)(((ll_item_t *) n1)->data))->symbol;
  1883. const char *s2 = ((weighted_item_t *)(((ll_item_t *) n2)->data))->symbol;
  1884. return strcmp(s1, s2);
  1885. }
  1886. static void add_wi_index(ll_item_t *l)
  1887. {
  1888. assert(l->data_type == DT_WEIGHTED);
  1889. if (!strcmp(((weighted_item_t *)(l->data))->symbol, "UNDEFINED")) {
  1890. cur_col->undefined_idx = l;
  1891. }
  1892. if (!tfind(l, &cur_col->root_wi_index, wi_index_cmp)) { /* new wi_index */
  1893. if (!tsearch(l, &cur_col->root_wi_index, wi_index_cmp)) {
  1894. error_msg("OUT OF MEMORY!");
  1895. }
  1896. }
  1897. if (cur_base != cur_col) {
  1898. if (!tfind(l, &cur_base->root_wi_index, wi_index_cmp)) {/* not a base val */
  1899. /* printf("derived: %s\n", ((weighted_item_t *)(l->data))->symbol); */
  1900. if (!tfind(l, &cur_base->root_derived_wi, wi_index_cmp)) { /* new derived */
  1901. if (!tsearch(l, &cur_base->root_derived_wi, wi_index_cmp)) {
  1902. error_msg("OUT OF MEMORY!");
  1903. }
  1904. }
  1905. }
  1906. }
  1907. }
  1908. static int final_index;
  1909. static int is_ucode(const char *s)
  1910. {
  1911. if ((s[0] == '<')
  1912. && (s[1] == 'U')
  1913. && isxdigit(s[2])
  1914. && isxdigit(s[3])
  1915. && isxdigit(s[4])
  1916. && isxdigit(s[5])
  1917. && (s[6] == '>')
  1918. ) {
  1919. return 7;
  1920. } else {
  1921. return 0;
  1922. }
  1923. }
  1924. static void add_final_col_index(const char *s)
  1925. {
  1926. ENTRY e;
  1927. e.key = (char *) s;
  1928. e.data = (void *)(final_index);
  1929. if (!hsearch(e, FIND)) { /* not in the table */
  1930. if (!hsearch(e, ENTER)) {
  1931. error_msg("OUT OF MEMORY! (hsearch)");
  1932. }
  1933. #if 0
  1934. {
  1935. int n;
  1936. void *v;
  1937. colitem_t ci;
  1938. colitem_t *p;
  1939. const char *t;
  1940. if (!strcmp(s, "UNDEFINED")) {
  1941. printf("%6d: %s\n", final_index, s);
  1942. } else {
  1943. assert(*s == '<');
  1944. if ((n = is_ucode(s)) != 0) {
  1945. assert(!s[n]);
  1946. printf("%6d: %s\n", final_index, s);
  1947. } else {
  1948. ci.string = (char *) s;
  1949. ci.element = NULL; /* don't care */
  1950. v = tfind(&ci, &cur_base->root_colitem, colitem_cmp);
  1951. if (!v) {
  1952. verbose_msg(VDETAIL, "%s NOT DEFINED!!!\n", s);
  1953. } else {
  1954. p = *((colitem_t **) v);
  1955. if (p->element != NULL) {
  1956. t = p->element;
  1957. assert(*t == '"');
  1958. ++t;
  1959. n = is_ucode(t);
  1960. assert(n);
  1961. printf("%6d: %.*s | ", final_index, n, t);
  1962. do {
  1963. t += n;
  1964. assert(*t);
  1965. if (*t == '"') {
  1966. assert(!t[1]);
  1967. break;
  1968. }
  1969. n = is_ucode(t);
  1970. assert(n);
  1971. printf("%.*s", n, t);
  1972. } while (1);
  1973. printf(" collating-element %s\n", s);
  1974. } else {
  1975. printf("%6d: %s (collating-symbol)\n", final_index, s);
  1976. }
  1977. }
  1978. }
  1979. }
  1980. }
  1981. #endif
  1982. ++final_index;
  1983. }
  1984. }
  1985. static int final_index_val0(const char *s)
  1986. {
  1987. ENTRY *p;
  1988. ENTRY e;
  1989. e.key = (char *) s;
  1990. if (!(p = hsearch(e, FIND))) { /* not in the table */
  1991. return 0;
  1992. }
  1993. return (int)(p->data);
  1994. }
  1995. static int final_index_val(const char *s)
  1996. {
  1997. ENTRY *p;
  1998. ENTRY e;
  1999. e.key = (char *) s;
  2000. if (!(p = hsearch(e, FIND))) { /* not in the table */
  2001. error_msg("can't find final index: %s", s);
  2002. }
  2003. return (int)(p->data);
  2004. }
  2005. static size_t num_tree_nodes;
  2006. static void count_nodes(const void *ptr, VISIT order, int level)
  2007. {
  2008. if ((order == postorder) || (order == leaf)) {
  2009. ++num_tree_nodes;
  2010. }
  2011. }
  2012. static size_t tnumnodes(const void *root)
  2013. {
  2014. num_tree_nodes = 0;
  2015. twalk(root, count_nodes);
  2016. return num_tree_nodes;
  2017. }
  2018. static ll_item_t *find_wi_index(const char *sym, col_locale_t *cl)
  2019. {
  2020. weighted_item_t w;
  2021. ll_item_t l;
  2022. void *p;
  2023. w.symbol = sym;
  2024. l.data = &w;
  2025. l.data_type = DT_WEIGHTED;
  2026. p = tfind(&l, &cl->root_wi_index, wi_index_cmp);
  2027. if (p) {
  2028. p = *(ll_item_t **)p;
  2029. }
  2030. return (ll_item_t *) p;
  2031. }
  2032. static void mark_reordered(const char *sym)
  2033. {
  2034. ll_item_t *lli;
  2035. lli = find_wi_index(sym, cur_base);
  2036. if (lli) {
  2037. if (!tsearch(lli, &cur_base->root_wi_index_reordered, wi_index_cmp)) {
  2038. error_msg("OUT OF MEMORY!");
  2039. }
  2040. }
  2041. }
  2042. static ll_item_t *find_wi_index_reordered(const char *sym)
  2043. {
  2044. weighted_item_t w;
  2045. ll_item_t l;
  2046. void *p;
  2047. w.symbol = sym;
  2048. l.data = &w;
  2049. l.data_type = DT_WEIGHTED;
  2050. p = tfind(&l, &cur_base->root_wi_index_reordered, wi_index_cmp);
  2051. if (p) {
  2052. p = *(ll_item_t **)p;
  2053. }
  2054. return (ll_item_t *) p;
  2055. }
  2056. static ll_item_t *init_comm_ptr(void)
  2057. {
  2058. assert(cur_base);
  2059. assert(cur_base->section_list);
  2060. /* at the moment, only support one section in comm */
  2061. assert(cur_base->section_list->next == NULL);
  2062. comm_cur_ptr = ((section_t *)(cur_base->section_list->data))->itm_list;
  2063. while (comm_cur_ptr && (comm_cur_ptr->data_type & DT_REORDER)) {
  2064. comm_cur_ptr = comm_cur_ptr->next;
  2065. }
  2066. /* { */
  2067. /* ll_item_t *p = comm_cur_ptr; */
  2068. /* verbose_msg(VDETAIL, "init_comm_ptr\n"); */
  2069. /* while (p != comm_cur_ptr) { */
  2070. /* if (p->data_type & DT_WEIGHTED) { */
  2071. /* verbose_msg(VDETAIL, "%s", ((weighted_item_t *)p)->symbol); */
  2072. /* } */
  2073. /* p = p->next; */
  2074. /* } */
  2075. /* } */
  2076. assert(comm_cur_ptr);
  2077. /* verbose_msg(VDETAIL, "init_comm_ptr -- %s %p %p %p %d\n", */
  2078. /* ((weighted_item_t *)(comm_cur_ptr->data))->symbol, */
  2079. /* comm_cur_ptr, comm_cur_ptr->prev, comm_cur_ptr->next, */
  2080. /* ll_len(comm_cur_ptr)); */
  2081. comm_prev_ptr = NULL;
  2082. return comm_cur_ptr;
  2083. }
  2084. static ll_item_t *next_comm_ptr(void)
  2085. {
  2086. /* at the moment, only support one section in comm */
  2087. assert(cur_base->section_list->next == NULL);
  2088. comm_prev_ptr = comm_cur_ptr;
  2089. while (comm_cur_ptr && ((comm_cur_ptr = comm_cur_ptr->next) != NULL)) {
  2090. if (!(comm_cur_ptr->data_type & DT_REORDER)) {
  2091. break;
  2092. }
  2093. }
  2094. return comm_cur_ptr;
  2095. }
  2096. static int dump_count;
  2097. #if 0
  2098. static void dump_section(section_t *s, int mask, col_locale_t *der)
  2099. {
  2100. ll_item_t *lli;
  2101. ll_item_t *lli0;
  2102. weighted_item_t *w;
  2103. weight_t *p;
  2104. int i;
  2105. lli0 = lli = s->itm_list;
  2106. if (!lli0) {
  2107. return;
  2108. }
  2109. do {
  2110. if (!(lli->data_type & mask)) {
  2111. lli = lli->next;
  2112. continue;
  2113. }
  2114. if (lli->data_type & DT_WEIGHTED) {
  2115. ++dump_count;
  2116. w = (weighted_item_t *)(lli->data);
  2117. p = w->weight;
  2118. printf("%6d: %s (%d) ", dump_count, w->symbol, p->num_weights);
  2119. for (i = 0 ; i < p->num_weights ; i++) {
  2120. if (p->rule[i] & R_FORWARD) {
  2121. printf("F");
  2122. }
  2123. if (p->rule[i] & R_BACKWARD) {
  2124. printf("B");
  2125. }
  2126. if (p->rule[i] & R_POSITION) {
  2127. printf("P");
  2128. }
  2129. printf(",");
  2130. }
  2131. for (i = 0 ; i < p->num_weights ; i++) {
  2132. printf(" %s", p->colitem[i]);
  2133. }
  2134. printf("\n");
  2135. } else if (lli->data_type & (DT_SECTION|DT_REORDER)) {
  2136. if (lli->data_type == DT_REORDER) {
  2137. assert(der);
  2138. if (strncmp(((section_t *)(lli->data))->name, der->name, strlen(der->name))) {
  2139. lli = lli->next;
  2140. continue;
  2141. }
  2142. }
  2143. if (lli->data_type & DT_SECTION) {
  2144. printf("SECTION -----------------\n");
  2145. } else {
  2146. printf("REORDER -----------------\n");
  2147. }
  2148. dump_section((section_t *)(lli->data), mask, der);
  2149. printf("DONE --------------------\n");
  2150. }
  2151. lli = lli->next;
  2152. } while (lli != lli0);
  2153. }
  2154. #else
  2155. static int in_reorder_section = 0;
  2156. static void dump_section(section_t *s, int mask, col_locale_t *der)
  2157. {
  2158. ll_item_t *lli;
  2159. ll_item_t *lli0;
  2160. weighted_item_t *w;
  2161. weight_t *p;
  2162. int i;
  2163. lli0 = lli = s->itm_list;
  2164. if (!lli0) {
  2165. return;
  2166. }
  2167. do {
  2168. if (!(lli->data_type & mask)) {
  2169. lli = lli->next;
  2170. continue;
  2171. }
  2172. if (lli->data_type & DT_WEIGHTED) {
  2173. ++dump_count;
  2174. w = (weighted_item_t *)(lli->data);
  2175. p = w->weight;
  2176. #if 1
  2177. if (in_reorder_section) {
  2178. printf(" %p", w);
  2179. }
  2180. #else
  2181. printf("%6d: %s (%d) ", dump_count, w->symbol, p->num_weights);
  2182. for (i = 0 ; i < p->num_weights ; i++) {
  2183. if (p->rule[i] & R_FORWARD) {
  2184. printf("F");
  2185. }
  2186. if (p->rule[i] & R_BACKWARD) {
  2187. printf("B");
  2188. }
  2189. if (p->rule[i] & R_POSITION) {
  2190. printf("P");
  2191. }
  2192. printf(",");
  2193. }
  2194. for (i = 0 ; i < p->num_weights ; i++) {
  2195. printf(" %s", p->colitem[i]);
  2196. }
  2197. printf("\n");
  2198. #endif
  2199. } else if (lli->data_type & (DT_SECTION|DT_REORDER)) {
  2200. if (lli->data_type == DT_REORDER) {
  2201. assert(der);
  2202. if (strncmp(((section_t *)(lli->data))->name, der->name, strlen(der->name))) {
  2203. lli = lli->next;
  2204. continue;
  2205. }
  2206. }
  2207. if (lli->data_type & DT_SECTION) {
  2208. /* printf("SECTION -----------------\n"); */
  2209. assert(0);
  2210. } else {
  2211. /* printf("REORDER -----------------\n"); */
  2212. in_reorder_section = 1;
  2213. }
  2214. dump_section((section_t *)(lli->data), mask, der);
  2215. /* printf("DONE --------------------\n"); */
  2216. printf("\n");
  2217. in_reorder_section = 0;
  2218. }
  2219. lli = lli->next;
  2220. } while (lli != lli0);
  2221. }
  2222. #endif
  2223. static void dump_weights(const char *name)
  2224. {
  2225. ll_item_t *lli;
  2226. col_locale_t *base;
  2227. col_locale_t *der;
  2228. col_locale_t cl;
  2229. void *p;
  2230. assert(name);
  2231. if (!*name) { /* use last */
  2232. base = cur_base;
  2233. der = cur_derived;
  2234. } else {
  2235. cl.name = (char *) name;
  2236. if (!(p = tfind(&cl, &root_col_locale, col_locale_cmp))) {
  2237. error_msg("unknown locale: %s", name);
  2238. }
  2239. base = *((col_locale_t **) p);
  2240. der = NULL;
  2241. if (base->base_locale) { /* oops... really derived */
  2242. der = base;
  2243. base = der->base_locale;
  2244. }
  2245. }
  2246. dump_count = 0;
  2247. if (base) {
  2248. /* printf("BASE - %s\n", base->name); */
  2249. for (lli = base->section_list ; lli ; lli = lli->next) {
  2250. /* printf("SECTION %s\n", ((section_t *)(lli->data))->name); */
  2251. dump_section((section_t *)(lli->data), ~0, der);
  2252. }
  2253. }
  2254. assert(der != base);
  2255. if (der) {
  2256. /* printf("DERIVED - %s\n", der->name); */
  2257. for (lli = der->section_list ; lli ; lli = lli->next) {
  2258. if (lli->data_type == DT_SECTION) {
  2259. dump_section((section_t *)(lli->data), DT_WEIGHTED, der);
  2260. }
  2261. }
  2262. }
  2263. /* printf("DONE\n"); */
  2264. }
  2265. static void print_starter_node(const void *ptr, VISIT order, int level)
  2266. {
  2267. if (order == postorder || order == leaf) {
  2268. fprintf(stderr, " %s\n", *(const char **) ptr);
  2269. }
  2270. }
  2271. static void finalize_base(void)
  2272. {
  2273. ll_item_t *s;
  2274. ll_item_t *h;
  2275. ll_item_t *lli;
  2276. ll_item_t *h2;
  2277. ll_item_t *l2;
  2278. ll_item_t *cli;
  2279. ll_item_t *rli = NULL;
  2280. weighted_item_t *w;
  2281. weight_t *p;
  2282. int i, n, mr, r, mi;
  2283. col_locale_t *cl;
  2284. void *mm;
  2285. int num_invariant = 0;
  2286. int num_varying = 0;
  2287. int max_weight;
  2288. int index2weight_len_inc = 1;
  2289. assert(cur_base);
  2290. assert(base_locale_len+1 < BASE_LOCALE_LEN);
  2291. base_locale_array[base_locale_len].name = cur_base->name;
  2292. base_locale_array[base_locale_len].num_weights = 1;
  2293. base_locale_array[base_locale_len].index2weight_offset = index2weight_len;
  2294. base_locale_array[base_locale_len].index2ruleidx_offset = index2ruleidx_len;
  2295. if (!strcmp(cur_base->name,"ja_JP") || !strcmp(cur_base->name,"ko_KR")) {
  2296. index2weight_len_inc = 0;
  2297. }
  2298. /* printf("%s -- index2weight_len = %d\n", cur_base->name, index2weight_len); */
  2299. if (!hcreate(30000)) {
  2300. error_msg("OUT OF MEMORY!");
  2301. }
  2302. /* first pass ... set the fixed indexes */
  2303. final_index = i = 1;
  2304. mr = 0;
  2305. for (s = cur_base->section_list ; s ; s = s->next) {
  2306. #if 1
  2307. if (s->data_type & DT_REORDER) { /* a reordered section */
  2308. verbose_msg(VDETAIL, "pass1: reordered section %s - xxx\n", ((section_t *)(s->data))->name);
  2309. lli = ((section_t *)(s->data))->itm_list;
  2310. r = 0;
  2311. if (lli) {
  2312. /* r = ll_len( ((section_t *)(lli->data))->itm_list ); */
  2313. r = ll_len(lli) + 1;
  2314. }
  2315. if (r > mr) {
  2316. mr = r;
  2317. }
  2318. verbose_msg(VDETAIL, "pass1: reordered section %s - %d\n", ((section_t *)(s->data))->name, r);
  2319. continue;
  2320. }
  2321. #endif
  2322. h = lli = ((section_t *)(s->data))->itm_list;
  2323. if (!lli) {
  2324. continue;
  2325. }
  2326. do {
  2327. if (lli->data_type & DT_RANGE) {
  2328. i += mr;
  2329. mr = 0;
  2330. /* ++i; */
  2331. lli->idx = i;
  2332. assert(!rli);
  2333. rli = lli;
  2334. verbose_msg(VDETAIL, "range pre = %d after = ", i);
  2335. i += ((range_item_t *)(lli->data))->length + 1;
  2336. /* ++i; */
  2337. verbose_msg(VDETAIL, "%d\n", i);
  2338. if (!index2weight_len_inc) { /* ko_KR hack */
  2339. final_index += ((range_item_t *)(lli->data))->length + 1;
  2340. }
  2341. /* add_final_col_index("RANGE"); */
  2342. } else if (lli->data_type & DT_WEIGHTED) {
  2343. i += mr;
  2344. mr = 0;
  2345. w = (weighted_item_t *)(lli->data);
  2346. if (find_wi_index_reordered(w->symbol)) { /* reordered symbol so skip on first pass */
  2347. ++num_varying;
  2348. ++i;
  2349. continue;
  2350. }
  2351. ++num_invariant;
  2352. index2weight_buffer[index2weight_len] = lli->idx = i++;
  2353. index2weight_len += index2weight_len_inc;
  2354. add_final_col_index(w->symbol);
  2355. } else {
  2356. assert(lli->data_type & DT_REORDER);
  2357. r = ll_len( ((section_t *)(lli->data))->itm_list );
  2358. if (r > mr) {
  2359. mr = r;
  2360. }
  2361. /* r = 0; */
  2362. }
  2363. } while ((lli = lli->next) != h);
  2364. }
  2365. /* second pass ... set the reordered indexes */
  2366. mi = i + mr;
  2367. mr = i = 0;
  2368. for (s = cur_base->section_list ; s ; s = s->next) {
  2369. h = lli = ((section_t *)(s->data))->itm_list;
  2370. if (!lli) {
  2371. continue;
  2372. }
  2373. do {
  2374. if (lli->data_type & DT_RANGE) {
  2375. i += mr;
  2376. mr = 0;
  2377. i = lli->idx + ((range_item_t *)(lli->data))->length + 1;
  2378. } else if ((lli->data_type & DT_WEIGHTED) && !(s->data_type & DT_REORDER)) {
  2379. i += mr;
  2380. mr = 0;
  2381. w = (weighted_item_t *)(lli->data);
  2382. if (find_wi_index_reordered(w->symbol) /* reordered symbol skipped on first pass */
  2383. #if 0
  2384. || (s->data_type & DT_REORDER) /* or in a reordered section */
  2385. #endif
  2386. ) {
  2387. assert(!(s->data_type & DT_REORDER));
  2388. index2weight_buffer[index2weight_len] = lli->idx = ++i;
  2389. index2weight_len += index2weight_len_inc;
  2390. add_final_col_index(w->symbol);
  2391. /* fprintf(stdout, "%11s: r %6d %6d %s\n", */
  2392. /* cur_base->name, lli->idx, final_index_val(w->symbol), w->symbol); */
  2393. continue;
  2394. }
  2395. i = lli->idx;
  2396. /* fprintf(stdout, "%11s: w %6d %6d %s\n", */
  2397. /* cur_base->name, lli->idx, final_index_val(w->symbol), w->symbol); */
  2398. } else {
  2399. /* verbose_msg(VDETAIL, "section: %s %d %d\n", ((section_t *)(s->data))->name, */
  2400. /* s->data_type, lli->data_type); */
  2401. /* assert(!(s->data_type & DT_REORDER)); */
  2402. /* assert(lli->data_type & DT_REORDER); */
  2403. #if 1
  2404. if (s->data_type & DT_REORDER) {
  2405. h2 = l2 = lli;
  2406. if (!h2) {
  2407. continue;
  2408. }
  2409. } else {
  2410. assert(s->data_type & DT_SECTION);
  2411. h2 = l2 = ((section_t *)(lli->data))->itm_list;
  2412. if (!h2) {
  2413. continue;
  2414. }
  2415. }
  2416. #else
  2417. h2 = l2 = ((section_t *)(lli->data))->itm_list;
  2418. if (!h2) {
  2419. continue;
  2420. }
  2421. #endif
  2422. r = 0;
  2423. do {
  2424. assert(l2->data_type & DT_WEIGHTED);
  2425. ++r;
  2426. l2->idx = i + r;
  2427. /* fprintf(stdout, "%s: R %6d %s\n", */
  2428. /* ((section_t *)(lli->data))->name, l2->idx, ((weighted_item_t *)(l2->data))->symbol); */
  2429. } while ((l2 = l2->next) != h2);
  2430. if (r > mr) {
  2431. mr = r;
  2432. }
  2433. }
  2434. } while ((lli = lli->next) != h);
  2435. }
  2436. /* finally, walk through all derived locales and set non-reordered section items */
  2437. mr = mi;
  2438. for (cli = cur_base->derived_list ; cli ; cli = cli->next) {
  2439. cl = (col_locale_t *)(cli->data);
  2440. /* verbose_msg(VDETAIL, "pass3: %d %s\n", cli->data_type, cl->name); */
  2441. /* fprintf(stdout, "pass3: %d %s\n", cli->data_type, cl->name); */
  2442. assert(cli->data_type == DT_COL_LOCALE);
  2443. i = mi;
  2444. for (s = cl->section_list ; s ; s = s->next) {
  2445. /* if (s->data_type & DT_REORDER) { */
  2446. /* continue; */
  2447. /* } */
  2448. h = lli = ((section_t *)(s->data))->itm_list;
  2449. if (!lli) {
  2450. continue;
  2451. }
  2452. do {
  2453. assert(!(lli->data_type & DT_RANGE));
  2454. if (lli->data_type & DT_WEIGHTED) {
  2455. /* verbose_msg(VDETAIL, " %d %d %s\n", lli->data_type, lli->idx, ((weighted_item_t *)(lli->data))->symbol); */
  2456. add_final_col_index(((weighted_item_t *)(lli->data))->symbol);
  2457. if (s->data_type & DT_REORDER) {
  2458. continue;
  2459. }
  2460. assert(lli->idx == INT_MIN);
  2461. lli->idx = ++i;
  2462. /* fprintf(stdout, "%11s: S %6d %6d %s\n", */
  2463. /* cl->name, lli->idx, */
  2464. /* final_index_val(((weighted_item_t *)(lli->data))->symbol), */
  2465. /* ((weighted_item_t *)(lli->data))->symbol); */
  2466. } else {
  2467. assert(0);
  2468. assert(lli->data_type & DT_SECTION);
  2469. h2 = l2 = ((section_t *)(lli->data))->itm_list;
  2470. if (!h2) {
  2471. continue;
  2472. }
  2473. do {
  2474. assert(l2->data_type & DT_WEIGHTED);
  2475. assert(l2->idx == INT_MIN);
  2476. l2->idx = ++i;
  2477. add_final_col_index(((weighted_item_t *)(l2->data))->symbol);
  2478. } while ((l2 = l2->next) != h2);
  2479. }
  2480. } while ((lli = lli->next) != h);
  2481. }
  2482. if (i > mr) {
  2483. mr = i;
  2484. }
  2485. }
  2486. max_weight = mr;
  2487. assert(num_varying == tnumnodes(cur_base->root_wi_index_reordered));
  2488. /* we can now initialize the wcs2index array */
  2489. {
  2490. ENTRY *p;
  2491. ENTRY e;
  2492. char buf[8];
  2493. static const char xd[] = "0123456789ABCDEF";
  2494. int starter_index = final_index;
  2495. int wcs2index_count = 0;
  2496. strcpy(buf, "<U....>");
  2497. memset(wcs2index, 0, sizeof(wcs2index));
  2498. e.key = (char *) buf;
  2499. for (i=1 ; i <= 0xffff ; i++) {
  2500. buf[5] = xd[ i & 0xf ];
  2501. buf[4] = xd[ (i >> 4) & 0xf ];
  2502. buf[3] = xd[ (i >> 8) & 0xf ];
  2503. buf[2] = xd[ (i >> 12) & 0xf ];
  2504. if ((p = hsearch(e, FIND)) != NULL) {
  2505. ++wcs2index_count;
  2506. if ((tfind(buf, &cur_base->root_starter_char, sym_cmp)) != NULL) {
  2507. wcs2index[i] = ++starter_index;
  2508. /* verbose_msg(VDETAIL, "wcs2index[ %#06x ] = %d (starter)\n", i, wcs2index[i]); */
  2509. } else {
  2510. wcs2index[i] = (int)(p->data);
  2511. /* verbose_msg(VDETAIL, "wcs2index[ %#06x ] = %d\n", i, wcs2index[i]); */
  2512. }
  2513. } else {
  2514. if ((tfind(buf, &cur_base->root_starter_char, sym_cmp)) != NULL) {
  2515. error_msg("marked starter but not in hash: %s", buf);
  2516. }
  2517. }
  2518. }
  2519. /* ---------------------------------------------------------------------- */
  2520. {
  2521. int i, n;
  2522. table_data table;
  2523. size_t t, smallest;
  2524. n = 0;
  2525. smallest = SIZE_MAX;
  2526. table.ii = NULL;
  2527. for (i=0 ; i < 14 ; i++) {
  2528. if ((RANGE >> i) < 4) {
  2529. break;
  2530. }
  2531. t = newopt(wcs2index, RANGE, i, &table);
  2532. if (smallest >= t) {
  2533. n = i;
  2534. smallest = t;
  2535. /* } else { */
  2536. /* break; */
  2537. }
  2538. }
  2539. /* printf("smallest = %u for range %#x (%u)\n", smallest, RANGE, RANGE); */
  2540. assert(smallest != SIZE_MAX);
  2541. if (smallest + wcs2colidt_len >= WCS2COLIDT_LEN) {
  2542. error_msg("WCS2COLIDT_LEN too small");
  2543. }
  2544. base_locale_array[base_locale_len].wcs2colidt_offset = wcs2colidt_len;
  2545. table.ii = wcs2colidt_buffer + wcs2colidt_len;
  2546. t = smallest;
  2547. smallest = SIZE_MAX;
  2548. smallest = newopt(wcs2index, RANGE, n, &table);
  2549. assert(t == smallest);
  2550. wcs2colidt_len += smallest;
  2551. /* verbose_msg(VDETAIL, "smallest = %d wcs2colidt_len = %d\n", smallest, wcs2colidt_len); */
  2552. #if 0
  2553. {
  2554. unsigned int sc, n, i0, i1;
  2555. unsigned int u = 0xe40;
  2556. table_data *tbl = &table;
  2557. #define __LOCALE_DATA_WCctype_TI_MASK ((1 << tbl->ti_shift)-1)
  2558. #define __LOCALE_DATA_WCctype_TI_SHIFT (tbl->ti_shift)
  2559. #define __LOCALE_DATA_WCctype_TI_LEN (tbl->ti_len)
  2560. #define __LOCALE_DATA_WCctype_II_MASK ((1 << tbl->ii_shift)-1)
  2561. #define __LOCALE_DATA_WCctype_II_SHIFT (tbl->ii_shift)
  2562. #define __LOCALE_DATA_WCctype_II_LEN (tbl->ii_len)
  2563. sc = u & __LOCALE_DATA_WCctype_TI_MASK;
  2564. u >>= __LOCALE_DATA_WCctype_TI_SHIFT;
  2565. n = u & __LOCALE_DATA_WCctype_II_MASK;
  2566. u >>= __LOCALE_DATA_WCctype_II_SHIFT;
  2567. i0 = tbl->ii[u];
  2568. verbose_msg(VDETAIL, "i0 = %d\n", i0);
  2569. i0 <<= __LOCALE_DATA_WCctype_II_SHIFT;
  2570. i1 = tbl->ii[__LOCALE_DATA_WCctype_II_LEN + i0 + n];
  2571. /* i1 = tbl->ti[i0 + n]; */
  2572. verbose_msg(VDETAIL, "i1 = %d\n", i1);
  2573. i1 <<= __LOCALE_DATA_WCctype_TI_SHIFT;
  2574. /* return *(uint16_t *)(&(tbl->ii[__LOCALE_DATA_WCctype_II_LEN + __LOCALE_DATA_WCctype_TI_LEN + i1 + sc])); */
  2575. verbose_msg(VDETAIL, "i2 = %d\n", __LOCALE_DATA_WCctype_II_LEN + __LOCALE_DATA_WCctype_TI_LEN + i1 + sc);
  2576. verbose_msg(VDETAIL, "val = %d\n", tbl->ii[__LOCALE_DATA_WCctype_II_LEN + __LOCALE_DATA_WCctype_TI_LEN + i1 + sc]);
  2577. /* return tbl->ut[i1 + sc]; */
  2578. }
  2579. #endif
  2580. base_locale_array[base_locale_len].ii_shift = table.ii_shift;
  2581. base_locale_array[base_locale_len].ti_shift = table.ti_shift;
  2582. base_locale_array[base_locale_len].ii_len = table.ii_len;
  2583. base_locale_array[base_locale_len].ti_len = table.ti_len;
  2584. }
  2585. /* ---------------------------------------------------------------------- */
  2586. base_locale_array[base_locale_len].num_col_base = num_invariant + num_varying;
  2587. base_locale_array[base_locale_len].max_col_index = final_index;
  2588. base_locale_array[base_locale_len].max_weight = max_weight;
  2589. verbose_msg(VDETAIL, "%s: %6u invariant %6u varying %6u derived %6u total %6u max weight %6u wcs2\n",
  2590. cur_base->name, num_invariant, num_varying,
  2591. tnumnodes(cur_base->root_derived_wi), final_index, max_weight,
  2592. wcs2index_count);
  2593. }
  2594. #if 1
  2595. /* ok, now we need to dump out the base and derived tables... */
  2596. /* don't forget to break up collating elements!!! */
  2597. /* fprintf(stdout, "**************************************************\n"); */
  2598. /* first pass ... set the invariants */
  2599. for (s = cur_base->section_list ; s ; s = s->next) {
  2600. #if 1
  2601. if (s->data_type & DT_REORDER) {
  2602. verbose_msg(VDETAIL, "1: skipping reordered section %s\n", ((section_t *)(s->data))->name);
  2603. continue;
  2604. }
  2605. #endif
  2606. h = lli = ((section_t *)(s->data))->itm_list;
  2607. if (!lli) {
  2608. continue;
  2609. }
  2610. do {
  2611. if (lli->data_type & DT_WEIGHTED) {
  2612. w = (weighted_item_t *)(lli->data);
  2613. if (find_wi_index_reordered(w->symbol)) { /* reordered symbol so skip on first pass */
  2614. continue;
  2615. }
  2616. if (index2weight_len_inc) {
  2617. index2ruleidx_buffer[index2ruleidx_len++] =
  2618. add_rule((weighted_item_t *)(lli->data));
  2619. }
  2620. /* fprintf(stdout, "%11s: w %6d %6d %s\n", */
  2621. /* cur_base->name, lli->idx, final_index_val(w->symbol), w->symbol); */
  2622. }
  2623. } while ((lli = lli->next) != h);
  2624. }
  2625. /* second pass ... set varying */
  2626. for (s = cur_base->section_list ; s ; s = s->next) {
  2627. #if 1
  2628. if (s->data_type & DT_REORDER) {
  2629. verbose_msg(VDETAIL, "2: skipping reordered section %s\n", ((section_t *)(s->data))->name);
  2630. continue;
  2631. }
  2632. #endif
  2633. h = lli = ((section_t *)(s->data))->itm_list;
  2634. if (!lli) {
  2635. continue;
  2636. }
  2637. do {
  2638. if (lli->data_type & DT_WEIGHTED) {
  2639. w = (weighted_item_t *)(lli->data);
  2640. if (find_wi_index_reordered(w->symbol)) { /* reordered symbol so skip on first pass */
  2641. if (index2weight_len_inc) {
  2642. index2ruleidx_buffer[index2ruleidx_len++] =
  2643. add_rule((weighted_item_t *)(lli->data));
  2644. }
  2645. /* fprintf(stdout, "%11s: r %6d %6d %s\n", */
  2646. /* cur_base->name, lli->idx, final_index_val(w->symbol), w->symbol); */
  2647. continue;
  2648. }
  2649. }
  2650. } while ((lli = lli->next) != h);
  2651. }
  2652. do_starter_lists(cur_base);
  2653. /* verbose_msg(VDETAIL,"updated final_index = %d\n", final_index); */
  2654. if (rli) {
  2655. base_locale_array[base_locale_len].range_low
  2656. = strtoul(((range_item_t *)(rli->data))->symbol1 + 2, NULL, 16);
  2657. base_locale_array[base_locale_len].range_count
  2658. = ((range_item_t *)(rli->data))->length;
  2659. base_locale_array[base_locale_len].range_base_weight = rli->idx;
  2660. base_locale_array[base_locale_len].range_rule_offset = add_range_rule((range_item_t *)(rli->data));
  2661. /* fprintf(stdout, "%11s: %6d %6d %s %s (%d)\n", */
  2662. /* "RANGE", rli->idx, -1, */
  2663. /* ((range_item_t *)(rli->data))->symbol1, */
  2664. /* ((range_item_t *)(rli->data))->symbol2, */
  2665. /* ((range_item_t *)(rli->data))->length); */
  2666. }
  2667. /* fprintf(stdout,"\nDerived\n\n"); */
  2668. /* first, if base name is of the form ll_CC, add a derived locale for it */
  2669. if ((strlen(cur_base->name) == 5)
  2670. && islower(cur_base->name[0])
  2671. && islower(cur_base->name[1])
  2672. && (cur_base->name[2] == '_')
  2673. && isupper(cur_base->name[3])
  2674. && isupper(cur_base->name[4])
  2675. ) {
  2676. verbose_msg(VDETAIL, "adding special derived for %s\n", cur_base->name);
  2677. /* verbose_msg(VDETAIL,"updated final_index = %d\n", final_index); */
  2678. assert(der_locale_len+1 < DER_LOCALE_LEN);
  2679. der_locale_array[der_locale_len].name = cur_base->name;
  2680. der_locale_array[der_locale_len].base_idx = base_locale_len;
  2681. u16_buf[0] = 1;
  2682. u16_buf[1] = 0;
  2683. u16_buf_len = 2;
  2684. mm = NULL;
  2685. if ((u16_buf_len > override_len) ||
  2686. !(mm = memmem(override_buffer, override_len*sizeof(override_buffer[0]),
  2687. u16_buf, u16_buf_len*sizeof(u16_buf[0])))
  2688. ) {
  2689. assert(override_len + u16_buf_len < OVERRIDE_LEN);
  2690. memcpy(override_buffer + override_len, u16_buf, u16_buf_len*sizeof(u16_buf[0]));
  2691. der_locale_array[der_locale_len].overrides_offset = override_len;
  2692. override_len += u16_buf_len;
  2693. /* printf("%s: override_len = %d u16_buf_len = %d\n", cl->name, override_len, u16_buf_len); */
  2694. } else if (!(u16_buf_len > override_len)) {
  2695. assert(mm);
  2696. der_locale_array[der_locale_len].overrides_offset = ((uint16_t *)(mm)) - override_buffer;
  2697. /* printf("%s: memmem found a match with u16_buf_len = %d\n", cl->name, u16_buf_len); */
  2698. }
  2699. der_locale_array[der_locale_len].multistart_offset
  2700. = base_locale_array[base_locale_len].multistart_offset;
  2701. der_locale_array[der_locale_len].undefined_idx = final_index_val0("UNDEFINED");
  2702. if (!der_locale_array[der_locale_len].undefined_idx) {
  2703. error_msg("no UNDEFINED definition for %s", cur_base->name);
  2704. }
  2705. ++der_locale_len;
  2706. } else {
  2707. verbose_msg(VDETAIL, "NOT adding special derived for %s\n", cur_base->name);
  2708. }
  2709. /* now all the derived... */
  2710. for (cli = cur_base->derived_list ; cli ; cli = cli->next) {
  2711. cl = (col_locale_t *)(cli->data);
  2712. assert(cli->data_type == DT_COL_LOCALE);
  2713. assert(der_locale_len+1 < DER_LOCALE_LEN);
  2714. der_locale_array[der_locale_len].name = cl->name;
  2715. der_locale_array[der_locale_len].base_idx = base_locale_len;
  2716. u16_buf_len = 0;
  2717. for (i = 0 ; i < 2 ; i++) {
  2718. if (i) {
  2719. /* fprintf(stdout, " section --- (singles)\n"); */
  2720. u16_buf[u16_buf_len++] = 1; /* single */
  2721. }
  2722. /* we do this in two passes... first all sequences, then all single reorders */
  2723. for (s = cl->section_list ; s ; s = s->next) {
  2724. /* verbose_msg(VDETAIL, "doing section %s\n", ((section_t *)(s->data))->name); */
  2725. h = lli = ((section_t *)(s->data))->itm_list;
  2726. if (!lli) {
  2727. /* fprintf(stdout, "EMPTY ITEM LIST IN SECTION %s\n", ((section_t *)(s->data))->name ); */
  2728. continue;
  2729. }
  2730. assert(u16_buf_len +4 < sizeof(u16_buf)/sizeof(u16_buf[0]));
  2731. if ((!i && (ll_len(h) > 1) ) || (ll_len(h) == i)) {
  2732. if (!i) {
  2733. /* fprintf(stdout, " section ----------------- %d %d\n", i, ll_len(h)); */
  2734. u16_buf[u16_buf_len++] = ll_len(h); /* multi */
  2735. assert(lli->data_type & DT_WEIGHTED);
  2736. #if 0
  2737. u16_buf[u16_buf_len++] = final_index_val(((weighted_item_t *)(lli->data))->symbol); /* start index */
  2738. #endif
  2739. u16_buf[u16_buf_len++] = lli->idx; /* start weight */
  2740. }
  2741. do {
  2742. assert(lli->data_type & DT_WEIGHTED);
  2743. if (lli->data_type & DT_WEIGHTED) {
  2744. /* fprintf(stdout, "%11s: S %6d %6d %s\n", */
  2745. /* cl->name, lli->idx, */
  2746. /* final_index_val(((weighted_item_t *)(lli->data))->symbol), */
  2747. /* ((weighted_item_t *)(lli->data))->symbol); */
  2748. #if 0
  2749. if (i) {
  2750. assert(u16_buf_len +4 < sizeof(u16_buf)/sizeof(u16_buf[0]));
  2751. u16_buf[u16_buf_len++] = final_index_val(((weighted_item_t *)(lli->data))->symbol);
  2752. assert(u16_buf[u16_buf_len-1]);
  2753. u16_buf[u16_buf_len++] = lli->idx; /* weight */
  2754. }
  2755. #else
  2756. assert(u16_buf_len +4 < sizeof(u16_buf)/sizeof(u16_buf[0]));
  2757. u16_buf[u16_buf_len++] = final_index_val(((weighted_item_t *)(lli->data))->symbol);
  2758. assert(u16_buf[u16_buf_len-1]);
  2759. if (i) {
  2760. u16_buf[u16_buf_len++] = lli->idx; /* weight */
  2761. }
  2762. #endif
  2763. u16_buf[u16_buf_len++] = add_rule((weighted_item_t *)(lli->data));
  2764. }
  2765. } while ((lli = lli->next) != h);
  2766. }
  2767. }
  2768. }
  2769. u16_buf[u16_buf_len++] = 0;
  2770. mm = NULL;
  2771. if ((u16_buf_len > override_len) ||
  2772. !(mm = memmem(override_buffer, override_len*sizeof(override_buffer[0]),
  2773. u16_buf, u16_buf_len*sizeof(u16_buf[0])))
  2774. ) {
  2775. assert(override_len + u16_buf_len < OVERRIDE_LEN);
  2776. memcpy(override_buffer + override_len, u16_buf, u16_buf_len*sizeof(u16_buf[0]));
  2777. der_locale_array[der_locale_len].overrides_offset = override_len;
  2778. override_len += u16_buf_len;
  2779. /* printf("%s: override_len = %d u16_buf_len = %d\n", cl->name, override_len, u16_buf_len); */
  2780. } else if (!(u16_buf_len > override_len)) {
  2781. assert(mm);
  2782. der_locale_array[der_locale_len].overrides_offset = ((uint16_t *)(mm)) - override_buffer;
  2783. /* printf("%s: memmem found a match with u16_buf_len = %d\n", cl->name, u16_buf_len); */
  2784. }
  2785. do_starter_lists(cl);
  2786. der_locale_array[der_locale_len].undefined_idx = final_index_val0("UNDEFINED");
  2787. #if 0
  2788. assert(der_locale_array[der_locale_len].undefined_idx);
  2789. if (!der_locale_array[der_locale_len].undefined_idx) {
  2790. der_locale_array[der_locale_len].undefined_idx = base_locale_array[base_locale_len].undefined_idx;
  2791. }
  2792. #endif
  2793. if (!der_locale_array[der_locale_len].undefined_idx) {
  2794. error_msg("no UNDEFINED definition for %s", cl->name);
  2795. }
  2796. ++der_locale_len;
  2797. }
  2798. #endif
  2799. /* base_locale_array[base_locale_len].undefined_idx = final_index_val0("UNDEFINED"); */
  2800. base_locale_array[base_locale_len].undefined_idx = 0;
  2801. hdestroy();
  2802. ++base_locale_len;
  2803. /* if (tnumnodes(cur_base->root_starter_char)) { */
  2804. /* verbose_msg(VDETAIL, "starter nodes\n"); */
  2805. /* twalk(cur_base->root_starter_char, print_starter_node); */
  2806. /* } */
  2807. }
  2808. static int starter_all_cmp(const void *n1, const void *n2)
  2809. {
  2810. const char *s1 = ((weighted_item_t *) n1)->symbol;
  2811. const char *s2 = ((weighted_item_t *) n2)->symbol;
  2812. colitem_t x;
  2813. colitem_t *p;
  2814. int n;
  2815. /* sort by 1st char ... then inverse for string */
  2816. x.element = NULL;
  2817. if (!is_ucode(s1)) {
  2818. x.string = s1;
  2819. p = tfind(&x, &cur_base->root_colitem, colitem_cmp);
  2820. s1 = (*((colitem_t **) p))->element + 1;
  2821. }
  2822. if (!is_ucode(s2)) {
  2823. x.string = s2;
  2824. p = tfind(&x, &cur_base->root_colitem, colitem_cmp);
  2825. s2 = (*((colitem_t **) p))->element + 1;
  2826. }
  2827. /* <U####>< */
  2828. /* 01234567 */
  2829. assert(is_ucode(s1));
  2830. assert(is_ucode(s2));
  2831. n = strncmp(s1+2, s2+2, 4);
  2832. if (n) {
  2833. return n;
  2834. }
  2835. s1 += 7;
  2836. s2 += 7;
  2837. return strcmp(s2, s1);
  2838. }
  2839. static void print_starter_all_node(const void *ptr, VISIT order, int level)
  2840. {
  2841. const weighted_item_t *w = *(const weighted_item_t **) ptr;
  2842. colitem_t *ci;
  2843. void *p;
  2844. int n;
  2845. colitem_t x;
  2846. if (order == postorder || order == leaf) {
  2847. #if 0
  2848. if ((n = is_ucode(w->symbol)) != 0) {
  2849. printf(" %s\n", w->symbol);
  2850. } else {
  2851. x.string = w->symbol;
  2852. x.element = NULL;
  2853. p = tfind(&x, &cur_base->root_colitem, colitem_cmp);
  2854. assert(p);
  2855. ci = *((colitem_t **) p);
  2856. printf("%s = %s\n", ci->element, w->symbol);
  2857. }
  2858. #else
  2859. printf("%s|", w->symbol);
  2860. /* if ((n = is_ucode(w->symbol)) != 0) { */
  2861. /* printf("\n"); */
  2862. /* } */
  2863. #endif
  2864. }
  2865. }
  2866. static void process_starter_node(const void *ptr, VISIT order, int level)
  2867. {
  2868. const weighted_item_t *w = *(const weighted_item_t **) ptr;
  2869. colitem_t *ci;
  2870. void *p;
  2871. int n;
  2872. colitem_t x;
  2873. const char *s;
  2874. char buf[32];
  2875. /* store index of collation item followed by (unprefixed) nul-terminated string */
  2876. if (order == postorder || order == leaf) {
  2877. if ((n = is_ucode(w->symbol)) != 0) {
  2878. u16_buf[u16_buf_len++] = final_index_val(w->symbol);
  2879. assert(u16_buf[u16_buf_len-1]);
  2880. u16_buf[u16_buf_len++] = 0;
  2881. if (++u16_starter < base_locale_array[base_locale_len].num_starters) {
  2882. u16_buf[u16_starter] = u16_buf_len;
  2883. }
  2884. /* verbose_msg(VDETAIL, "ucode - %d %d\n", u16_buf[u16_starter-1], u16_buf_len); */
  2885. } else {
  2886. x.string = w->symbol;
  2887. x.element = NULL;
  2888. p = tfind(&x, &cur_base->root_colitem, colitem_cmp);
  2889. assert(p);
  2890. ci = *((colitem_t **) p);
  2891. s = ci->element;
  2892. u16_buf[u16_buf_len++] = final_index_val(w->symbol);
  2893. assert(u16_buf[u16_buf_len-1]);
  2894. assert(*s == '"');
  2895. n = is_ucode(++s);
  2896. /* verbose_msg(VDETAIL, "s is |%s| with len %d (%d)\n", s, strlen(s), n); */
  2897. assert(n);
  2898. s += n;
  2899. while (*s != '"') {
  2900. n = is_ucode(s);
  2901. assert(n);
  2902. strncpy(buf, s, n+1);
  2903. buf[n] = 0;
  2904. /* verbose_msg(VDETAIL, "buf is |%s| with len %d (%d)\n", buf, strlen(buf), n); */
  2905. u16_buf[u16_buf_len++] = final_index_val(buf);
  2906. assert(u16_buf[u16_buf_len-1]);
  2907. s += n;
  2908. }
  2909. u16_buf[u16_buf_len++] = 0;
  2910. }
  2911. }
  2912. }
  2913. static void **p_cl_root_starter_all;
  2914. static void complete_starter_node(const void *ptr, VISIT order, int level)
  2915. {
  2916. weighted_item_t w;
  2917. weighted_item_t *p;
  2918. if (order == postorder || order == leaf) {
  2919. w.symbol = *(const char **) ptr;
  2920. w.weight = NULL;
  2921. if (!tfind(&w, p_cl_root_starter_all, starter_all_cmp)) {
  2922. p = xmalloc(sizeof(weighted_item_t));
  2923. p->symbol = w.symbol;
  2924. p->weight = NULL;
  2925. /* verbose_msg(VDETAIL, "complete_starter_node: %s\n", *(const char **) ptr); */
  2926. if (!tsearch(p, p_cl_root_starter_all, starter_all_cmp)) {
  2927. error_msg("OUT OF MEMORY");
  2928. }
  2929. }
  2930. }
  2931. }
  2932. static void do_starter_lists(col_locale_t *cl)
  2933. {
  2934. ll_item_t *s;
  2935. ll_item_t *h;
  2936. ll_item_t *lli;
  2937. col_locale_t *c;
  2938. colitem_t *ci;
  2939. weighted_item_t *w;
  2940. void *p;
  2941. char buf[32];
  2942. int n;
  2943. colitem_t x;
  2944. void *mm;
  2945. c = cl;
  2946. if (c != cur_base) {
  2947. c = cur_base;
  2948. }
  2949. /* printf("STARTERS %s --------------------\n", cl->name); */
  2950. LOOP:
  2951. for (s = c->section_list ; s ; s = s->next) {
  2952. h = lli = ((section_t *)(s->data))->itm_list;
  2953. if (!lli) {
  2954. continue;
  2955. }
  2956. do {
  2957. if (lli->data_type & DT_WEIGHTED) {
  2958. w = (weighted_item_t *)(lli->data);
  2959. ci = NULL;
  2960. if ((n = is_ucode(w->symbol)) != 0) {
  2961. strcpy(buf, w->symbol);
  2962. } else {
  2963. /* fprintf(stdout, "looking for |%s|\n", w->symbol); */
  2964. x.string = w->symbol;
  2965. x.element = NULL;
  2966. p = tfind(&x, &cur_base->root_colitem, colitem_cmp);
  2967. if (!p) {
  2968. /* verbose_msg(VDETAIL, "Whoa... processing starters for %s and couldn't find %s\n", */
  2969. /* cl->name, w->symbol); */
  2970. continue;
  2971. }
  2972. ci = *((colitem_t **) p);
  2973. if (!ci->element) { /* just a collating symbol */
  2974. continue;
  2975. }
  2976. assert(ci->element[0] == '"');
  2977. n = is_ucode(ci->element + 1);
  2978. assert(n);
  2979. strncpy(buf, ci->element + 1, n);
  2980. }
  2981. if ((tfind(buf, &cur_base->root_starter_char, sym_cmp)) != NULL) {
  2982. /* fprintf(stdout, "adding from %s: %s", c->name, w->symbol); */
  2983. /* if (ci) { */
  2984. /* fprintf(stdout, " = %s", ci->element); */
  2985. /* } */
  2986. /* fprintf(stdout, "\n"); */
  2987. if (!tsearch(w, &cl->root_starter_all, starter_all_cmp)) {
  2988. error_msg("OUT OF MEMORY");
  2989. }
  2990. }
  2991. }
  2992. } while ((lli = lli->next) != h);
  2993. }
  2994. if (c != cl) {
  2995. c = cl;
  2996. goto LOOP;
  2997. }
  2998. p_cl_root_starter_all = &cl->root_starter_all;
  2999. twalk(cur_base->root_starter_char, complete_starter_node);
  3000. if (cl == cur_base) {
  3001. base_locale_array[base_locale_len].num_starters = tnumnodes(cur_base->root_starter_char);
  3002. }
  3003. #if 0
  3004. printf("\nNow walking tree...\n\n");
  3005. twalk(cl->root_starter_all, print_starter_all_node);
  3006. printf("\n\n");
  3007. #endif
  3008. u16_starter = 0;
  3009. u16_buf[0] = u16_buf_len = base_locale_array[base_locale_len].num_starters;
  3010. twalk(cl->root_starter_all, process_starter_node);
  3011. /* verbose_msg(VDETAIL, "s=%d n=%d\n", u16_starter, base_locale_array[base_locale_len].num_starters); */
  3012. assert(u16_starter == base_locale_array[base_locale_len].num_starters);
  3013. #if 0
  3014. { int i;
  3015. for (i=0 ; i < u16_buf_len ; i++) {
  3016. verbose_msg(VDETAIL, "starter %2d: %d - %#06x\n", i, u16_buf[i], u16_buf[i]);
  3017. }}
  3018. #endif
  3019. mm = NULL;
  3020. if (u16_buf_len) {
  3021. /* assert(base_locale_array[base_locale_len].num_starters); */
  3022. if ((u16_buf_len > multistart_len) ||
  3023. !(mm = memmem(multistart_buffer, multistart_len*sizeof(multistart_buffer[0]),
  3024. u16_buf, u16_buf_len*sizeof(u16_buf[0])))
  3025. ) {
  3026. assert(multistart_len + u16_buf_len < MULTISTART_LEN);
  3027. memcpy(multistart_buffer + multistart_len, u16_buf, u16_buf_len*sizeof(u16_buf[0]));
  3028. if (cl == cur_base) {
  3029. base_locale_array[base_locale_len].multistart_offset = multistart_len;
  3030. } else {
  3031. der_locale_array[der_locale_len].multistart_offset = multistart_len;
  3032. }
  3033. multistart_len += u16_buf_len;
  3034. /* verbose_msg(VDETAIL, "%s: multistart_len = %d u16_buf_len = %d\n", cl->name, multistart_len, u16_buf_len); */
  3035. } else if (!(u16_buf_len > multistart_len)) {
  3036. assert(mm);
  3037. if (cl == cur_base) {
  3038. base_locale_array[base_locale_len].multistart_offset = ((uint16_t *)(mm)) - multistart_buffer;
  3039. } else {
  3040. der_locale_array[der_locale_len].multistart_offset = ((uint16_t *)(mm)) - multistart_buffer;
  3041. }
  3042. /* verbose_msg(VDETAIL, "%s: memmem found a match with u16_buf_len = %d\n", cl->name, u16_buf_len); */
  3043. }
  3044. } else {
  3045. assert(!base_locale_array[base_locale_len].num_starters);
  3046. }
  3047. /* printf("u16_buf_len = %d\n", u16_buf_len); */
  3048. /* printf("STARTERS %s DONE ---------------\n", cl->name); */
  3049. }
  3050. /* For sorting the blocks of unsigned chars. */
  3051. static size_t nu_val;
  3052. int nu_memcmp(const void *a, const void *b)
  3053. {
  3054. return memcmp(*(unsigned char**)a, *(unsigned char**)b, nu_val * sizeof(tbl_item));
  3055. }
  3056. size_t newopt(tbl_item *ut, size_t usize, int shift, table_data *tbl)
  3057. {
  3058. static int recurse;
  3059. tbl_item *ti[RANGE]; /* table index */
  3060. size_t numblocks;
  3061. size_t blocksize;
  3062. size_t uniq;
  3063. size_t i, j;
  3064. size_t smallest, t;
  3065. tbl_item *ii_save;
  3066. int uniqblock[1 << (8*sizeof(tbl_item) - 1)];
  3067. tbl_item uit[RANGE];
  3068. int shift2;
  3069. if (shift > 15) {
  3070. return SIZE_MAX;
  3071. }
  3072. ii_save = NULL;
  3073. blocksize = 1 << shift;
  3074. numblocks = usize >> shift;
  3075. /* init table index */
  3076. for (i=j=0 ; i < numblocks ; i++) {
  3077. ti[i] = ut + j;
  3078. j += blocksize;
  3079. }
  3080. /* sort */
  3081. nu_val = blocksize;
  3082. qsort(ti, numblocks, sizeof(unsigned char *), nu_memcmp);
  3083. uniq = 1;
  3084. uit[(ti[0]-ut)/blocksize] = 0;
  3085. for (i=1 ; i < numblocks ; i++) {
  3086. if (memcmp(ti[i-1], ti[i], blocksize*sizeof(tbl_item)) < 0) {
  3087. if (++uniq > (1 << (8*sizeof(tbl_item) - 1))) {
  3088. break;
  3089. }
  3090. uniqblock[uniq - 1] = i;
  3091. }
  3092. #if 1
  3093. else if (memcmp(ti[i-1], ti[i], blocksize*sizeof(tbl_item)) > 0) {
  3094. printf("bad sort %i!\n", i);
  3095. abort();
  3096. }
  3097. #endif
  3098. uit[(ti[i]-ut)/blocksize] = uniq - 1;
  3099. }
  3100. smallest = SIZE_MAX;
  3101. shift2 = -1;
  3102. if (uniq <= (1 << (8*sizeof(tbl_item) - 1))) {
  3103. smallest = numblocks + uniq * blocksize;
  3104. if (!recurse) {
  3105. ++recurse;
  3106. for (j=1 ; j < 14 ; j++) {
  3107. if ((numblocks >> j) < 2) break;
  3108. if (tbl) {
  3109. ii_save = tbl->ii;
  3110. tbl->ii = NULL;
  3111. }
  3112. if ((t = newopt(uit, numblocks, j, tbl)) < SIZE_MAX) {
  3113. t += uniq * blocksize;
  3114. }
  3115. if (tbl) {
  3116. tbl->ii = ii_save;
  3117. }
  3118. if (smallest >= t) {
  3119. shift2 = j;
  3120. smallest = t;
  3121. /* if (!tbl->ii) { */
  3122. /* printf("ishift %u tshift %u size %u\n", */
  3123. /* shift2, shift, t); */
  3124. /* } */
  3125. /* } else { */
  3126. /* break; */
  3127. }
  3128. }
  3129. --recurse;
  3130. }
  3131. } else {
  3132. return SIZE_MAX;
  3133. }
  3134. if (tbl->ii) {
  3135. if (recurse) {
  3136. tbl->ii_shift = shift;
  3137. tbl->ii_len = numblocks;
  3138. memcpy(tbl->ii, uit, numblocks*sizeof(tbl_item));
  3139. tbl->ti = tbl->ii + tbl->ii_len;
  3140. tbl->ti_len = uniq * blocksize;
  3141. for (i=0 ; i < uniq ; i++) {
  3142. memcpy(tbl->ti + i * blocksize, ti[uniqblock[i]], blocksize*sizeof(tbl_item));
  3143. }
  3144. } else {
  3145. ++recurse;
  3146. /* printf("setting ishift %u tshift %u\n", shift2, shift); */
  3147. newopt(uit, numblocks, shift2, tbl);
  3148. --recurse;
  3149. tbl->ti_shift = shift;
  3150. tbl->ut_len = uniq * blocksize;
  3151. tbl->ut = tbl->ti + tbl->ti_len;
  3152. for (i=0 ; i < uniq ; i++) {
  3153. memcpy(tbl->ut + i * blocksize, ti[uniqblock[i]], blocksize*sizeof(tbl_item));
  3154. }
  3155. }
  3156. }
  3157. return smallest;
  3158. }
  3159. static const int rule2val[8] = {
  3160. -1,
  3161. (1 << 14), /* forward */
  3162. (2 << 14), /* position */
  3163. (3 << 14), /* forward,position */
  3164. 0, /* backward */
  3165. -1,
  3166. -1,
  3167. -1,
  3168. };
  3169. static int final_index_val_x(const char *s, const char *sym)
  3170. {
  3171. int r;
  3172. if (!(r = final_index_val0(s))) {
  3173. if (!strcmp(s, "IGNORE")) {
  3174. r = 0;
  3175. } else if (!strcmp(s, "..") || !strcmp(sym, "RANGE")) {
  3176. if (*sym == '.') {
  3177. final_index_val(sym); /* make sure it's known */
  3178. }
  3179. r = 0x3fff;
  3180. } else if (!strcmp(s, ".")) {
  3181. r = 0x3ffe;
  3182. } else {
  3183. error_msg("can't find final index: %s", s);
  3184. }
  3185. }
  3186. return r;
  3187. }
  3188. /* store rule2val in 2 high bits and collation index in lower.
  3189. * for sort strings, store (offset from base) + max colindex as index.
  3190. */
  3191. static unsigned int add_rule(weighted_item_t *wi)
  3192. {
  3193. weight_t *w = wi->weight;
  3194. int i, j, r, n;
  3195. uint16_t rbuf[MAX_COLLATION_WEIGHTS];
  3196. uint16_t ws_buf[32];
  3197. void *mm;
  3198. char buf[32];
  3199. const char *s;
  3200. const char *e;
  3201. for (i=0 ; i < MAX_COLLATION_WEIGHTS ; i++) {
  3202. rbuf[i] = rule2val[R_FORWARD]; /* set a default to forward-ignore */
  3203. }
  3204. if (base_locale_array[base_locale_len].num_weights < w->num_weights) {
  3205. base_locale_array[base_locale_len].num_weights = w->num_weights;
  3206. }
  3207. for (i=0 ; i < w->num_weights ; i++) {
  3208. assert(rule2val[(int)(w->rule[i])] >= 0);
  3209. assert(w->colitem[i] && *w->colitem[i]);
  3210. if (*w->colitem[i] == '"') { /* string... */
  3211. s = w->colitem[i] + 1;
  3212. assert(*s == '<');
  3213. n = 0;
  3214. do {
  3215. e = s;
  3216. do {
  3217. if (*e == '/') {
  3218. e += 2;
  3219. continue;
  3220. }
  3221. } while (*e++ != '>');
  3222. assert(((size_t)(e-s) < sizeof(buf)));
  3223. memcpy(buf, s, (size_t)(e-s));
  3224. buf[(size_t)(e-s)] = 0;
  3225. r = final_index_val_x(buf, wi->symbol);
  3226. assert(n + 1 < sizeof(ws_buf)/sizeof(ws_buf[0]));
  3227. ws_buf[n++] = r | rule2val[(int)(w->rule[i])];
  3228. s = e;
  3229. } while (*s != '"');
  3230. ws_buf[n++] = 0; /* terminator */
  3231. mm = memmem(weightstr_buffer, weightstr_len*sizeof(weightstr_buffer[0]),
  3232. ws_buf, n*sizeof(ws_buf[0]));
  3233. if (!mm) {
  3234. assert(weightstr_len + n < WEIGHTSTR_LEN);
  3235. memcpy(weightstr_buffer + weightstr_len, ws_buf, n*sizeof(ws_buf[0]));
  3236. mm = weightstr_buffer + weightstr_len;
  3237. weightstr_len += n;
  3238. }
  3239. r = (((uint16_t *)(mm)) - weightstr_buffer)
  3240. + base_locale_array[base_locale_len].max_col_index + 2;
  3241. assert(r < (1 << 14));
  3242. rbuf[i] = r | rule2val[(int)(w->rule[i])];
  3243. } else { /* item */
  3244. r = final_index_val_x(w->colitem[i], wi->symbol);
  3245. rbuf[i] = r | rule2val[(int)(w->rule[i])];
  3246. }
  3247. }
  3248. for (i=0 ; i < ruletable_len ; i += MAX_COLLATION_WEIGHTS) {
  3249. if (!memcmp(ruletable_buffer + i, rbuf, MAX_COLLATION_WEIGHTS*sizeof(ruletable_buffer[0]))) {
  3250. return i/MAX_COLLATION_WEIGHTS;
  3251. }
  3252. }
  3253. memcpy(ruletable_buffer + ruletable_len, rbuf, MAX_COLLATION_WEIGHTS*sizeof(ruletable_buffer[0]));
  3254. ruletable_len += MAX_COLLATION_WEIGHTS;
  3255. return (ruletable_len / MAX_COLLATION_WEIGHTS)-1;
  3256. }
  3257. static unsigned int add_range_rule(range_item_t *ri)
  3258. {
  3259. weight_t *w = ri->weight;
  3260. int i, j, r, n;
  3261. uint16_t rbuf[MAX_COLLATION_WEIGHTS];
  3262. uint16_t ws_buf[32];
  3263. void *mm;
  3264. char buf[32];
  3265. const char *s;
  3266. const char *e;
  3267. for (i=0 ; i < MAX_COLLATION_WEIGHTS ; i++) {
  3268. rbuf[i] = rule2val[R_FORWARD]; /* set a default to forward-ignore */
  3269. }
  3270. if (base_locale_array[base_locale_len].num_weights < w->num_weights) {
  3271. base_locale_array[base_locale_len].num_weights = w->num_weights;
  3272. }
  3273. for (i=0 ; i < w->num_weights ; i++) {
  3274. assert(rule2val[(int)(w->rule[i])] >= 0);
  3275. assert(w->colitem[i] && *w->colitem[i]);
  3276. if (*w->colitem[i] == '"') { /* string... */
  3277. s = w->colitem[i] + 1;
  3278. assert(*s == '<');
  3279. n = 0;
  3280. do {
  3281. e = s;
  3282. do {
  3283. if (*e == '/') {
  3284. e += 2;
  3285. continue;
  3286. }
  3287. } while (*e++ != '>');
  3288. assert(((size_t)(e-s) < sizeof(buf)));
  3289. memcpy(buf, s, (size_t)(e-s));
  3290. buf[(size_t)(e-s)] = 0;
  3291. r = final_index_val_x(buf, "RANGE");
  3292. assert(n + 1 < sizeof(ws_buf)/sizeof(ws_buf[0]));
  3293. ws_buf[n++] = r | rule2val[(int)(w->rule[i])];
  3294. s = e;
  3295. } while (*s != '"');
  3296. ws_buf[n++] = 0; /* terminator */
  3297. mm = memmem(weightstr_buffer, weightstr_len*sizeof(weightstr_buffer[0]),
  3298. ws_buf, n*sizeof(ws_buf[0]));
  3299. if (!mm) {
  3300. assert(weightstr_len + n < WEIGHTSTR_LEN);
  3301. memcpy(weightstr_buffer + weightstr_len, ws_buf, n*sizeof(ws_buf[0]));
  3302. mm = weightstr_buffer + weightstr_len;
  3303. weightstr_len += n;
  3304. }
  3305. r = (((uint16_t *)(mm)) - weightstr_buffer)
  3306. + base_locale_array[base_locale_len].max_col_index + 2;
  3307. assert(r < (1 << 14));
  3308. rbuf[i] = r | rule2val[(int)(w->rule[i])];
  3309. } else { /* item */
  3310. r = final_index_val_x(w->colitem[i], "RANGE");
  3311. rbuf[i] = r | rule2val[(int)(w->rule[i])];
  3312. }
  3313. }
  3314. for (i=0 ; i < ruletable_len ; i += MAX_COLLATION_WEIGHTS) {
  3315. if (!memcmp(ruletable_buffer + i, rbuf, MAX_COLLATION_WEIGHTS*sizeof(ruletable_buffer[0]))) {
  3316. return i/MAX_COLLATION_WEIGHTS;
  3317. }
  3318. }
  3319. memcpy(ruletable_buffer + ruletable_len, rbuf, MAX_COLLATION_WEIGHTS*sizeof(ruletable_buffer[0]));
  3320. ruletable_len += MAX_COLLATION_WEIGHTS;
  3321. return (ruletable_len / MAX_COLLATION_WEIGHTS)-1;
  3322. }
  3323. #define DUMPn(X) fprintf(stderr, "%10d-%-.20s", base_locale_array[n]. X, #X);
  3324. static void dump_base_locale(int n)
  3325. {
  3326. assert(n < base_locale_len);
  3327. fprintf(stderr, "Base Locale: %s\n", base_locale_array[n].name);
  3328. DUMPn(num_weights);
  3329. DUMPn(ii_shift);
  3330. DUMPn(ti_shift);
  3331. DUMPn(ii_len);
  3332. DUMPn(ti_len);
  3333. DUMPn(max_weight);
  3334. fprintf(stderr, "\n");
  3335. DUMPn(num_col_base);
  3336. DUMPn(max_col_index);
  3337. DUMPn(undefined_idx);
  3338. DUMPn(range_low);
  3339. DUMPn(range_count);
  3340. fprintf(stderr, "\n");
  3341. DUMPn(range_base_weight);
  3342. DUMPn(num_starters);
  3343. fprintf(stderr, "\n");
  3344. DUMPn(range_rule_offset);
  3345. DUMPn(wcs2colidt_offset);
  3346. DUMPn(index2weight_offset);
  3347. fprintf(stderr, "\n");
  3348. DUMPn(index2ruleidx_offset);
  3349. DUMPn(multistart_offset);
  3350. fprintf(stderr, "\n");
  3351. }
  3352. #undef DUMPn
  3353. #define DUMPn(X) fprintf(stderr, "%10d-%s", der_locale_array[n]. X, #X);
  3354. static void dump_der_locale(int n)
  3355. {
  3356. assert(n < der_locale_len);
  3357. fprintf(stderr, "Derived Locale: %s (%.12s)",
  3358. der_locale_array[n].name,
  3359. base_locale_array[der_locale_array[n].base_idx].name);
  3360. DUMPn(base_idx);
  3361. DUMPn(undefined_idx);
  3362. DUMPn(overrides_offset);
  3363. DUMPn(multistart_offset);
  3364. fprintf(stderr, "\n");
  3365. }
  3366. static unsigned long collate_pos;
  3367. static void dump_u16_array(FILE *fp, uint16_t *u, int len, const char *name)
  3368. {
  3369. int i;
  3370. fprintf(fp, "\t/* %8lu %s */\n", collate_pos, name);
  3371. for (i=0 ; i < len ; i++) {
  3372. if (!(i & 7)) {
  3373. fprintf(fp, "\n\t");
  3374. }
  3375. fprintf(fp," %#06x,", (unsigned int)(u[i]));
  3376. }
  3377. fprintf(fp,"\n");
  3378. collate_pos += len;
  3379. }
  3380. #define OUT_U16C(X,N) fprintf(fp,"\t%10d, /* %8lu %s */\n", X, collate_pos++, N);
  3381. static void dump_collate(FILE *fp)
  3382. {
  3383. int n;
  3384. fprintf(fp, "const uint16_t __locale_collate_tbl[] = {\n");
  3385. OUT_U16C(base_locale_len, "numbef of base locales");
  3386. OUT_U16C(der_locale_len, "number of derived locales");
  3387. OUT_U16C(MAX_COLLATION_WEIGHTS, "max collation weights");
  3388. OUT_U16C(index2weight_len, "number of index2{weight|ruleidx} elements");
  3389. OUT_U16C(weightstr_len, "number of weightstr elements");
  3390. OUT_U16C(multistart_len, "number of multistart elements");
  3391. OUT_U16C(override_len, "number of override elements");
  3392. OUT_U16C(ruletable_len, "number of ruletable elements");
  3393. #undef DUMPn
  3394. #define DUMPn(X) fprintf(fp, "\t%10d, /* %8lu %s */\n", base_locale_array[n]. X, collate_pos++, #X);
  3395. for (n=0 ; n < base_locale_len ; n++) {
  3396. unsigned wcs2colidt_offset_low = base_locale_array[n].wcs2colidt_offset & 0xffffU;
  3397. unsigned wcs2colidt_offset_hi = base_locale_array[n].wcs2colidt_offset >> 16;
  3398. fprintf(fp, "\t/* Base Locale %2d: %s */\n", n, base_locale_array[n].name);
  3399. DUMPn(num_weights);
  3400. DUMPn(num_starters);
  3401. DUMPn(ii_shift);
  3402. DUMPn(ti_shift);
  3403. DUMPn(ii_len);
  3404. DUMPn(ti_len);
  3405. DUMPn(max_weight);
  3406. DUMPn(num_col_base);
  3407. DUMPn(max_col_index);
  3408. DUMPn(undefined_idx);
  3409. DUMPn(range_low);
  3410. DUMPn(range_count);
  3411. DUMPn(range_base_weight);
  3412. DUMPn(range_rule_offset);
  3413. DUMPn(index2weight_offset);
  3414. DUMPn(index2ruleidx_offset);
  3415. DUMPn(multistart_offset);
  3416. #undef DUMPn
  3417. #define DUMPn(X) fprintf(fp, "\t%10d, /* %8lu %s */\n", X, collate_pos++, #X);
  3418. DUMPn(wcs2colidt_offset_low);
  3419. DUMPn(wcs2colidt_offset_hi);
  3420. }
  3421. #undef DUMPn
  3422. fprintf(fp, "#define COL_IDX_C %5d\n", 0);
  3423. #define DUMPn(X) fprintf(fp, "\t%10d, /* %8lu %s */\n", der_locale_array[n]. X, collate_pos++, #X);
  3424. for (n=0 ; n < der_locale_len ; n++) {
  3425. fprintf(fp, "#define COL_IDX_%s %5d\n", der_locale_array[n].name, n+1);
  3426. fprintf(fp, "\t/* Derived Locale %4d: %s (%.12s) */\n",
  3427. n, der_locale_array[n].name,
  3428. base_locale_array[der_locale_array[n].base_idx].name);
  3429. DUMPn(base_idx);
  3430. DUMPn(undefined_idx);
  3431. DUMPn(overrides_offset);
  3432. DUMPn(multistart_offset);
  3433. }
  3434. #undef DUMPn
  3435. fprintf(fp, "\n");
  3436. dump_u16_array(fp, index2weight_buffer, index2weight_len, "index2weight");
  3437. dump_u16_array(fp, index2ruleidx_buffer, index2ruleidx_len, "index2ruleidx");
  3438. dump_u16_array(fp, multistart_buffer, multistart_len, "multistart");
  3439. dump_u16_array(fp, override_buffer, override_len, "override");
  3440. dump_u16_array(fp, ruletable_buffer, ruletable_len, "ruletable");
  3441. dump_u16_array(fp, weightstr_buffer, weightstr_len, "weightstr");
  3442. dump_u16_array(fp, wcs2colidt_buffer, wcs2colidt_len, "wcs2colidt");
  3443. fprintf(fp,"}; /* %8lu */\n", collate_pos);
  3444. fprintf(fp,"#define __lc_collate_data_LEN %lu\n\n", collate_pos);
  3445. }