infocmp.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. /****************************************************************************
  2. * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /****************************************************************************
  29. * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
  30. * and: Eric S. Raymond <esr@snark.thyrsus.com> *
  31. * and: Thomas E. Dickey 1996-on *
  32. ****************************************************************************/
  33. /*
  34. * infocmp.c -- decompile an entry, or compare two entries
  35. * written by Eric S. Raymond
  36. * and Thomas E Dickey
  37. */
  38. #include <progs.priv.h>
  39. #include <dump_entry.h>
  40. MODULE_ID("$Id: infocmp.c,v 1.103 2008/08/16 22:04:56 tom Exp $")
  41. #define L_CURL "{"
  42. #define R_CURL "}"
  43. #define MAX_STRING 1024 /* maximum formatted string */
  44. const char *_nc_progname = "infocmp";
  45. typedef char path[PATH_MAX];
  46. /***************************************************************************
  47. *
  48. * The following control variables, together with the contents of the
  49. * terminfo entries, completely determine the actions of the program.
  50. *
  51. ***************************************************************************/
  52. static ENTRY *entries; /* terminfo entries */
  53. static int termcount; /* count of terminal entries */
  54. static bool limited = TRUE; /* "-r" option is not set */
  55. static bool quiet = FALSE;
  56. static bool literal = FALSE;
  57. static const char *bool_sep = ":";
  58. static const char *s_absent = "NULL";
  59. static const char *s_cancel = "NULL";
  60. static const char *tversion; /* terminfo version selected */
  61. static int itrace; /* trace flag for debugging */
  62. static int mwidth = 60;
  63. static int numbers = 0; /* format "%'char'" to/from "%{number}" */
  64. static int outform = F_TERMINFO; /* output format */
  65. static int sortmode; /* sort_mode */
  66. /* main comparison mode */
  67. static int compare;
  68. #define C_DEFAULT 0 /* don't force comparison mode */
  69. #define C_DIFFERENCE 1 /* list differences between two terminals */
  70. #define C_COMMON 2 /* list common capabilities */
  71. #define C_NAND 3 /* list capabilities in neither terminal */
  72. #define C_USEALL 4 /* generate relative use-form entry */
  73. static bool ignorepads; /* ignore pad prefixes when diffing */
  74. #if NO_LEAKS
  75. #undef ExitProgram
  76. static void ExitProgram(int code) GCC_NORETURN;
  77. /* prototype is to get gcc to accept the noreturn attribute */
  78. static void
  79. ExitProgram(int code)
  80. {
  81. while (termcount-- > 0)
  82. _nc_free_termtype(&entries[termcount].tterm);
  83. _nc_leaks_dump_entry();
  84. free(entries);
  85. _nc_free_tic(code);
  86. }
  87. #endif
  88. static char *
  89. canonical_name(char *ptr, char *buf)
  90. /* extract the terminal type's primary name */
  91. {
  92. char *bp;
  93. (void) strcpy(buf, ptr);
  94. if ((bp = strchr(buf, '|')) != 0)
  95. *bp = '\0';
  96. return (buf);
  97. }
  98. /***************************************************************************
  99. *
  100. * Predicates for dump function
  101. *
  102. ***************************************************************************/
  103. static int
  104. capcmp(PredIdx idx, const char *s, const char *t)
  105. /* capability comparison function */
  106. {
  107. if (!VALID_STRING(s) && !VALID_STRING(t))
  108. return (s != t);
  109. else if (!VALID_STRING(s) || !VALID_STRING(t))
  110. return (1);
  111. if ((idx == acs_chars_index) || !ignorepads)
  112. return (strcmp(s, t));
  113. else
  114. return (_nc_capcmp(s, t));
  115. }
  116. static int
  117. use_predicate(unsigned type, PredIdx idx)
  118. /* predicate function to use for use decompilation */
  119. {
  120. ENTRY *ep;
  121. switch (type) {
  122. case BOOLEAN:
  123. {
  124. int is_set = FALSE;
  125. /*
  126. * This assumes that multiple use entries are supposed
  127. * to contribute the logical or of their boolean capabilities.
  128. * This is true if we take the semantics of multiple uses to
  129. * be 'each capability gets the first non-default value found
  130. * in the sequence of use entries'.
  131. *
  132. * Note that cancelled or absent booleans are stored as FALSE,
  133. * unlike numbers and strings, whose cancelled/absent state is
  134. * recorded in the terminfo database.
  135. */
  136. for (ep = &entries[1]; ep < entries + termcount; ep++)
  137. if (ep->tterm.Booleans[idx] == TRUE) {
  138. is_set = entries[0].tterm.Booleans[idx];
  139. break;
  140. }
  141. if (is_set != entries[0].tterm.Booleans[idx])
  142. return (!is_set);
  143. else
  144. return (FAIL);
  145. }
  146. case NUMBER:
  147. {
  148. int value = ABSENT_NUMERIC;
  149. /*
  150. * We take the semantics of multiple uses to be 'each
  151. * capability gets the first non-default value found
  152. * in the sequence of use entries'.
  153. */
  154. for (ep = &entries[1]; ep < entries + termcount; ep++)
  155. if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
  156. value = ep->tterm.Numbers[idx];
  157. break;
  158. }
  159. if (value != entries[0].tterm.Numbers[idx])
  160. return (value != ABSENT_NUMERIC);
  161. else
  162. return (FAIL);
  163. }
  164. case STRING:
  165. {
  166. char *termstr, *usestr = ABSENT_STRING;
  167. termstr = entries[0].tterm.Strings[idx];
  168. /*
  169. * We take the semantics of multiple uses to be 'each
  170. * capability gets the first non-default value found
  171. * in the sequence of use entries'.
  172. */
  173. for (ep = &entries[1]; ep < entries + termcount; ep++)
  174. if (ep->tterm.Strings[idx]) {
  175. usestr = ep->tterm.Strings[idx];
  176. break;
  177. }
  178. if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
  179. return (FAIL);
  180. else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
  181. return (TRUE);
  182. else
  183. return (FAIL);
  184. }
  185. }
  186. return (FALSE); /* pacify compiler */
  187. }
  188. static bool
  189. useeq(ENTRY * e1, ENTRY * e2)
  190. /* are the use references in two entries equivalent? */
  191. {
  192. unsigned i, j;
  193. if (e1->nuses != e2->nuses)
  194. return (FALSE);
  195. /* Ugh...this is quadratic again */
  196. for (i = 0; i < e1->nuses; i++) {
  197. bool foundmatch = FALSE;
  198. /* search second entry for given use reference */
  199. for (j = 0; j < e2->nuses; j++)
  200. if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
  201. foundmatch = TRUE;
  202. break;
  203. }
  204. if (!foundmatch)
  205. return (FALSE);
  206. }
  207. return (TRUE);
  208. }
  209. static bool
  210. entryeq(TERMTYPE *t1, TERMTYPE *t2)
  211. /* are two entries equivalent? */
  212. {
  213. unsigned i;
  214. for (i = 0; i < NUM_BOOLEANS(t1); i++)
  215. if (t1->Booleans[i] != t2->Booleans[i])
  216. return (FALSE);
  217. for (i = 0; i < NUM_NUMBERS(t1); i++)
  218. if (t1->Numbers[i] != t2->Numbers[i])
  219. return (FALSE);
  220. for (i = 0; i < NUM_STRINGS(t1); i++)
  221. if (capcmp((PredIdx) i, t1->Strings[i], t2->Strings[i]))
  222. return (FALSE);
  223. return (TRUE);
  224. }
  225. #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
  226. static void
  227. print_uses(ENTRY * ep, FILE *fp)
  228. /* print an entry's use references */
  229. {
  230. unsigned i;
  231. if (!ep->nuses)
  232. fputs("NULL", fp);
  233. else
  234. for (i = 0; i < ep->nuses; i++) {
  235. fputs(ep->uses[i].name, fp);
  236. if (i < ep->nuses - 1)
  237. fputs(" ", fp);
  238. }
  239. }
  240. static const char *
  241. dump_boolean(int val)
  242. /* display the value of a boolean capability */
  243. {
  244. switch (val) {
  245. case ABSENT_BOOLEAN:
  246. return (s_absent);
  247. case CANCELLED_BOOLEAN:
  248. return (s_cancel);
  249. case FALSE:
  250. return ("F");
  251. case TRUE:
  252. return ("T");
  253. default:
  254. return ("?");
  255. }
  256. }
  257. static void
  258. dump_numeric(int val, char *buf)
  259. /* display the value of a boolean capability */
  260. {
  261. switch (val) {
  262. case ABSENT_NUMERIC:
  263. strcpy(buf, s_absent);
  264. break;
  265. case CANCELLED_NUMERIC:
  266. strcpy(buf, s_cancel);
  267. break;
  268. default:
  269. sprintf(buf, "%d", val);
  270. break;
  271. }
  272. }
  273. static void
  274. dump_string(char *val, char *buf)
  275. /* display the value of a string capability */
  276. {
  277. if (val == ABSENT_STRING)
  278. strcpy(buf, s_absent);
  279. else if (val == CANCELLED_STRING)
  280. strcpy(buf, s_cancel);
  281. else {
  282. sprintf(buf, "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
  283. }
  284. }
  285. static void
  286. compare_predicate(PredType type, PredIdx idx, const char *name)
  287. /* predicate function to use for entry difference reports */
  288. {
  289. register ENTRY *e1 = &entries[0];
  290. register ENTRY *e2 = &entries[1];
  291. char buf1[MAX_STRING], buf2[MAX_STRING];
  292. int b1, b2;
  293. int n1, n2;
  294. char *s1, *s2;
  295. switch (type) {
  296. case CMP_BOOLEAN:
  297. b1 = e1->tterm.Booleans[idx];
  298. b2 = e2->tterm.Booleans[idx];
  299. switch (compare) {
  300. case C_DIFFERENCE:
  301. if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
  302. (void) printf("\t%s: %s%s%s.\n",
  303. name,
  304. dump_boolean(b1),
  305. bool_sep,
  306. dump_boolean(b2));
  307. break;
  308. case C_COMMON:
  309. if (b1 == b2 && b1 != ABSENT_BOOLEAN)
  310. (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
  311. break;
  312. case C_NAND:
  313. if (b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN)
  314. (void) printf("\t!%s.\n", name);
  315. break;
  316. }
  317. break;
  318. case CMP_NUMBER:
  319. n1 = e1->tterm.Numbers[idx];
  320. n2 = e2->tterm.Numbers[idx];
  321. dump_numeric(n1, buf1);
  322. dump_numeric(n2, buf2);
  323. switch (compare) {
  324. case C_DIFFERENCE:
  325. if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2)
  326. (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
  327. break;
  328. case C_COMMON:
  329. if (n1 != ABSENT_NUMERIC && n2 != ABSENT_NUMERIC && n1 == n2)
  330. (void) printf("\t%s= %s.\n", name, buf1);
  331. break;
  332. case C_NAND:
  333. if (n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)
  334. (void) printf("\t!%s.\n", name);
  335. break;
  336. }
  337. break;
  338. case CMP_STRING:
  339. s1 = e1->tterm.Strings[idx];
  340. s2 = e2->tterm.Strings[idx];
  341. switch (compare) {
  342. case C_DIFFERENCE:
  343. if (capcmp(idx, s1, s2)) {
  344. dump_string(s1, buf1);
  345. dump_string(s2, buf2);
  346. if (strcmp(buf1, buf2))
  347. (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
  348. }
  349. break;
  350. case C_COMMON:
  351. if (s1 && s2 && !capcmp(idx, s1, s2))
  352. (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
  353. break;
  354. case C_NAND:
  355. if (!s1 && !s2)
  356. (void) printf("\t!%s.\n", name);
  357. break;
  358. }
  359. break;
  360. case CMP_USE:
  361. /* unlike the other modes, this compares *all* use entries */
  362. switch (compare) {
  363. case C_DIFFERENCE:
  364. if (!useeq(e1, e2)) {
  365. (void) fputs("\tuse: ", stdout);
  366. print_uses(e1, stdout);
  367. fputs(", ", stdout);
  368. print_uses(e2, stdout);
  369. fputs(".\n", stdout);
  370. }
  371. break;
  372. case C_COMMON:
  373. if (e1->nuses && e2->nuses && useeq(e1, e2)) {
  374. (void) fputs("\tuse: ", stdout);
  375. print_uses(e1, stdout);
  376. fputs(".\n", stdout);
  377. }
  378. break;
  379. case C_NAND:
  380. if (!e1->nuses && !e2->nuses)
  381. (void) printf("\t!use.\n");
  382. break;
  383. }
  384. }
  385. }
  386. /***************************************************************************
  387. *
  388. * Init string analysis
  389. *
  390. ***************************************************************************/
  391. typedef struct {
  392. const char *from;
  393. const char *to;
  394. } assoc;
  395. static const assoc std_caps[] =
  396. {
  397. /* these are specified by X.364 and iBCS2 */
  398. {"\033c", "RIS"}, /* full reset */
  399. {"\0337", "SC"}, /* save cursor */
  400. {"\0338", "RC"}, /* restore cursor */
  401. {"\033[r", "RSR"}, /* not an X.364 mnemonic */
  402. {"\033[m", "SGR0"}, /* not an X.364 mnemonic */
  403. {"\033[2J", "ED2"}, /* clear page */
  404. /* this group is specified by ISO 2022 */
  405. {"\033(0", "ISO DEC G0"}, /* enable DEC graphics for G0 */
  406. {"\033(A", "ISO UK G0"}, /* enable UK chars for G0 */
  407. {"\033(B", "ISO US G0"}, /* enable US chars for G0 */
  408. {"\033)0", "ISO DEC G1"}, /* enable DEC graphics for G1 */
  409. {"\033)A", "ISO UK G1"}, /* enable UK chars for G1 */
  410. {"\033)B", "ISO US G1"}, /* enable US chars for G1 */
  411. /* these are DEC private controls widely supported by emulators */
  412. {"\033=", "DECPAM"}, /* application keypad mode */
  413. {"\033>", "DECPNM"}, /* normal keypad mode */
  414. {"\033<", "DECANSI"}, /* enter ANSI mode */
  415. {"\033[!p", "DECSTR"}, /* soft reset */
  416. {"\033 F", "S7C1T"}, /* 7-bit controls */
  417. {(char *) 0, (char *) 0}
  418. };
  419. static const assoc std_modes[] =
  420. /* ECMA \E[ ... [hl] modes recognized by many emulators */
  421. {
  422. {"2", "AM"}, /* keyboard action mode */
  423. {"4", "IRM"}, /* insert/replace mode */
  424. {"12", "SRM"}, /* send/receive mode */
  425. {"20", "LNM"}, /* linefeed mode */
  426. {(char *) 0, (char *) 0}
  427. };
  428. static const assoc private_modes[] =
  429. /* DEC \E[ ... [hl] modes recognized by many emulators */
  430. {
  431. {"1", "CKM"}, /* application cursor keys */
  432. {"2", "ANM"}, /* set VT52 mode */
  433. {"3", "COLM"}, /* 132-column mode */
  434. {"4", "SCLM"}, /* smooth scroll */
  435. {"5", "SCNM"}, /* reverse video mode */
  436. {"6", "OM"}, /* origin mode */
  437. {"7", "AWM"}, /* wraparound mode */
  438. {"8", "ARM"}, /* auto-repeat mode */
  439. {(char *) 0, (char *) 0}
  440. };
  441. static const assoc ecma_highlights[] =
  442. /* recognize ECMA attribute sequences */
  443. {
  444. {"0", "NORMAL"}, /* normal */
  445. {"1", "+BOLD"}, /* bold on */
  446. {"2", "+DIM"}, /* dim on */
  447. {"3", "+ITALIC"}, /* italic on */
  448. {"4", "+UNDERLINE"}, /* underline on */
  449. {"5", "+BLINK"}, /* blink on */
  450. {"6", "+FASTBLINK"}, /* fastblink on */
  451. {"7", "+REVERSE"}, /* reverse on */
  452. {"8", "+INVISIBLE"}, /* invisible on */
  453. {"9", "+DELETED"}, /* deleted on */
  454. {"10", "MAIN-FONT"}, /* select primary font */
  455. {"11", "ALT-FONT-1"}, /* select alternate font 1 */
  456. {"12", "ALT-FONT-2"}, /* select alternate font 2 */
  457. {"13", "ALT-FONT-3"}, /* select alternate font 3 */
  458. {"14", "ALT-FONT-4"}, /* select alternate font 4 */
  459. {"15", "ALT-FONT-5"}, /* select alternate font 5 */
  460. {"16", "ALT-FONT-6"}, /* select alternate font 6 */
  461. {"17", "ALT-FONT-7"}, /* select alternate font 7 */
  462. {"18", "ALT-FONT-1"}, /* select alternate font 1 */
  463. {"19", "ALT-FONT-1"}, /* select alternate font 1 */
  464. {"20", "FRAKTUR"}, /* Fraktur font */
  465. {"21", "DOUBLEUNDER"}, /* double underline */
  466. {"22", "-DIM"}, /* dim off */
  467. {"23", "-ITALIC"}, /* italic off */
  468. {"24", "-UNDERLINE"}, /* underline off */
  469. {"25", "-BLINK"}, /* blink off */
  470. {"26", "-FASTBLINK"}, /* fastblink off */
  471. {"27", "-REVERSE"}, /* reverse off */
  472. {"28", "-INVISIBLE"}, /* invisible off */
  473. {"29", "-DELETED"}, /* deleted off */
  474. {(char *) 0, (char *) 0}
  475. };
  476. static int
  477. skip_csi(const char *cap)
  478. {
  479. int result = 0;
  480. if (cap[0] == '\033' && cap[1] == '[')
  481. result = 2;
  482. else if (UChar(cap[0]) == 0233)
  483. result = 1;
  484. return result;
  485. }
  486. static bool
  487. same_param(const char *table, const char *param, unsigned length)
  488. {
  489. bool result = FALSE;
  490. if (strncmp(table, param, length) == 0) {
  491. result = !isdigit(UChar(param[length]));
  492. }
  493. return result;
  494. }
  495. static char *
  496. lookup_params(const assoc * table, char *dst, char *src)
  497. {
  498. char *result = 0;
  499. const char *ep = strtok(src, ";");
  500. if (ep != 0) {
  501. const assoc *ap;
  502. do {
  503. bool found = FALSE;
  504. for (ap = table; ap->from; ap++) {
  505. size_t tlen = strlen(ap->from);
  506. if (same_param(ap->from, ep, tlen)) {
  507. (void) strcat(dst, ap->to);
  508. found = TRUE;
  509. break;
  510. }
  511. }
  512. if (!found)
  513. (void) strcat(dst, ep);
  514. (void) strcat(dst, ";");
  515. } while
  516. ((ep = strtok((char *) 0, ";")));
  517. dst[strlen(dst) - 1] = '\0';
  518. result = dst;
  519. }
  520. return result;
  521. }
  522. static void
  523. analyze_string(const char *name, const char *cap, TERMTYPE *tp)
  524. {
  525. char buf2[MAX_TERMINFO_LENGTH];
  526. const char *sp;
  527. const assoc *ap;
  528. int tp_lines = tp->Numbers[2];
  529. if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
  530. return;
  531. (void) printf("%s: ", name);
  532. for (sp = cap; *sp; sp++) {
  533. int i;
  534. int csi;
  535. size_t len = 0;
  536. size_t next;
  537. const char *expansion = 0;
  538. char buf3[MAX_TERMINFO_LENGTH];
  539. /* first, check other capabilities in this entry */
  540. for (i = 0; i < STRCOUNT; i++) {
  541. char *cp = tp->Strings[i];
  542. /* don't use soft-key capabilities */
  543. if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
  544. continue;
  545. if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
  546. != cap) {
  547. len = strlen(cp);
  548. (void) strncpy(buf2, sp, len);
  549. buf2[len] = '\0';
  550. if (_nc_capcmp(cp, buf2))
  551. continue;
  552. #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
  553. /*
  554. * Theoretically we just passed the test for translation
  555. * (equality once the padding is stripped). However, there
  556. * are a few more hoops that need to be jumped so that
  557. * identical pairs of initialization and reset strings
  558. * don't just refer to each other.
  559. */
  560. if (ISRS(name) || ISRS(strnames[i]))
  561. if (cap < cp)
  562. continue;
  563. #undef ISRS
  564. expansion = strnames[i];
  565. break;
  566. }
  567. }
  568. /* now check the standard capabilities */
  569. if (!expansion) {
  570. csi = skip_csi(sp);
  571. for (ap = std_caps; ap->from; ap++) {
  572. size_t adj = (size_t) (csi ? 2 : 0);
  573. len = strlen(ap->from);
  574. if (csi && skip_csi(ap->from) != csi)
  575. continue;
  576. if (len > adj
  577. && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
  578. expansion = ap->to;
  579. len -= adj;
  580. len += (size_t) csi;
  581. break;
  582. }
  583. }
  584. }
  585. /* now check for standard-mode sequences */
  586. if (!expansion
  587. && (csi = skip_csi(sp)) != 0
  588. && (len = strspn(sp + csi, "0123456789;"))
  589. && (len < sizeof(buf3))
  590. && (next = (size_t) csi + len)
  591. && ((sp[next] == 'h') || (sp[next] == 'l'))) {
  592. (void) strcpy(buf2, (sp[next] == 'h') ? "ECMA+" : "ECMA-");
  593. (void) strncpy(buf3, sp + csi, len);
  594. buf3[len] = '\0';
  595. len += (size_t) csi + 1;
  596. expansion = lookup_params(std_modes, buf2, buf3);
  597. }
  598. /* now check for private-mode sequences */
  599. if (!expansion
  600. && (csi = skip_csi(sp)) != 0
  601. && sp[csi] == '?'
  602. && (len = strspn(sp + csi + 1, "0123456789;"))
  603. && (len < sizeof(buf3))
  604. && (next = (size_t) csi + 1 + len)
  605. && ((sp[next] == 'h') || (sp[next] == 'l'))) {
  606. (void) strcpy(buf2, (sp[next] == 'h') ? "DEC+" : "DEC-");
  607. (void) strncpy(buf3, sp + csi + 1, len);
  608. buf3[len] = '\0';
  609. len += (size_t) csi + 2;
  610. expansion = lookup_params(private_modes, buf2, buf3);
  611. }
  612. /* now check for ECMA highlight sequences */
  613. if (!expansion
  614. && (csi = skip_csi(sp)) != 0
  615. && (len = strspn(sp + csi, "0123456789;")) != 0
  616. && (len < sizeof(buf3))
  617. && (next = (size_t) csi + len)
  618. && sp[next] == 'm') {
  619. (void) strcpy(buf2, "SGR:");
  620. (void) strncpy(buf3, sp + csi, len);
  621. buf3[len] = '\0';
  622. len += (size_t) csi + 1;
  623. expansion = lookup_params(ecma_highlights, buf2, buf3);
  624. }
  625. if (!expansion
  626. && (csi = skip_csi(sp)) != 0
  627. && sp[csi] == 'm') {
  628. len = (size_t) csi + 1;
  629. (void) strcpy(buf2, "SGR:");
  630. strcat(buf2, ecma_highlights[0].to);
  631. expansion = buf2;
  632. }
  633. /* now check for scroll region reset */
  634. if (!expansion
  635. && (csi = skip_csi(sp)) != 0) {
  636. if (sp[csi] == 'r') {
  637. expansion = "RSR";
  638. len = 1;
  639. } else {
  640. (void) sprintf(buf2, "1;%dr", tp_lines);
  641. len = strlen(buf2);
  642. if (strncmp(buf2, sp + csi, len) == 0)
  643. expansion = "RSR";
  644. }
  645. len += (size_t) csi;
  646. }
  647. /* now check for home-down */
  648. if (!expansion
  649. && (csi = skip_csi(sp)) != 0) {
  650. (void) sprintf(buf2, "%d;1H", tp_lines);
  651. len = strlen(buf2);
  652. if (strncmp(buf2, sp + csi, len) == 0) {
  653. expansion = "LL";
  654. } else {
  655. (void) sprintf(buf2, "%dH", tp_lines);
  656. len = strlen(buf2);
  657. if (strncmp(buf2, sp + csi, len) == 0) {
  658. expansion = "LL";
  659. }
  660. }
  661. len += (size_t) csi;
  662. }
  663. /* now look at the expansion we got, if any */
  664. if (expansion) {
  665. printf("{%s}", expansion);
  666. sp += len - 1;
  667. } else {
  668. /* couldn't match anything */
  669. buf2[0] = *sp;
  670. buf2[1] = '\0';
  671. fputs(TIC_EXPAND(buf2), stdout);
  672. }
  673. }
  674. putchar('\n');
  675. }
  676. /***************************************************************************
  677. *
  678. * File comparison
  679. *
  680. ***************************************************************************/
  681. static void
  682. file_comparison(int argc, char *argv[])
  683. {
  684. #define MAXCOMPARE 2
  685. /* someday we may allow comparisons on more files */
  686. int filecount = 0;
  687. ENTRY *heads[MAXCOMPARE];
  688. ENTRY *qp, *rp;
  689. int i, n;
  690. memset(heads, 0, sizeof(heads));
  691. dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, itrace, FALSE);
  692. for (n = 0; n < argc && n < MAXCOMPARE; n++) {
  693. if (freopen(argv[n], "r", stdin) == 0)
  694. _nc_err_abort("Can't open %s", argv[n]);
  695. _nc_head = _nc_tail = 0;
  696. /* parse entries out of the source file */
  697. _nc_set_source(argv[n]);
  698. _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
  699. if (itrace)
  700. (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
  701. /* maybe do use resolution */
  702. if (!_nc_resolve_uses2(!limited, literal)) {
  703. (void) fprintf(stderr,
  704. "There are unresolved use entries in %s:\n",
  705. argv[n]);
  706. for_entry_list(qp) {
  707. if (qp->nuses) {
  708. (void) fputs(qp->tterm.term_names, stderr);
  709. (void) fputc('\n', stderr);
  710. }
  711. }
  712. ExitProgram(EXIT_FAILURE);
  713. }
  714. heads[filecount] = _nc_head;
  715. filecount++;
  716. }
  717. /* OK, all entries are in core. Ready to do the comparison */
  718. if (itrace)
  719. (void) fprintf(stderr, "Entries are now in core...\n");
  720. /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
  721. for (qp = heads[0]; qp; qp = qp->next) {
  722. for (rp = heads[1]; rp; rp = rp->next)
  723. if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
  724. if (qp->ncrosslinks < MAX_CROSSLINKS)
  725. qp->crosslinks[qp->ncrosslinks] = rp;
  726. qp->ncrosslinks++;
  727. if (rp->ncrosslinks < MAX_CROSSLINKS)
  728. rp->crosslinks[rp->ncrosslinks] = qp;
  729. rp->ncrosslinks++;
  730. }
  731. }
  732. /* now we have two circular lists with crosslinks */
  733. if (itrace)
  734. (void) fprintf(stderr, "Name matches are done...\n");
  735. for (qp = heads[0]; qp; qp = qp->next) {
  736. if (qp->ncrosslinks > 1) {
  737. (void) fprintf(stderr,
  738. "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
  739. _nc_first_name(qp->tterm.term_names),
  740. argv[0],
  741. qp->ncrosslinks,
  742. argv[1]);
  743. for (i = 0; i < qp->ncrosslinks; i++)
  744. (void) fprintf(stderr,
  745. "\t%s\n",
  746. _nc_first_name((qp->crosslinks[i])->tterm.term_names));
  747. }
  748. }
  749. for (rp = heads[1]; rp; rp = rp->next) {
  750. if (rp->ncrosslinks > 1) {
  751. (void) fprintf(stderr,
  752. "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
  753. _nc_first_name(rp->tterm.term_names),
  754. argv[1],
  755. rp->ncrosslinks,
  756. argv[0]);
  757. for (i = 0; i < rp->ncrosslinks; i++)
  758. (void) fprintf(stderr,
  759. "\t%s\n",
  760. _nc_first_name((rp->crosslinks[i])->tterm.term_names));
  761. }
  762. }
  763. (void) printf("In file 1 (%s) only:\n", argv[0]);
  764. for (qp = heads[0]; qp; qp = qp->next)
  765. if (qp->ncrosslinks == 0)
  766. (void) printf("\t%s\n",
  767. _nc_first_name(qp->tterm.term_names));
  768. (void) printf("In file 2 (%s) only:\n", argv[1]);
  769. for (rp = heads[1]; rp; rp = rp->next)
  770. if (rp->ncrosslinks == 0)
  771. (void) printf("\t%s\n",
  772. _nc_first_name(rp->tterm.term_names));
  773. (void) printf("The following entries are equivalent:\n");
  774. for (qp = heads[0]; qp; qp = qp->next) {
  775. rp = qp->crosslinks[0];
  776. if (qp->ncrosslinks == 1) {
  777. rp = qp->crosslinks[0];
  778. repair_acsc(&qp->tterm);
  779. repair_acsc(&rp->tterm);
  780. #if NCURSES_XNAMES
  781. _nc_align_termtype(&qp->tterm, &rp->tterm);
  782. #endif
  783. if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
  784. char name1[NAMESIZE], name2[NAMESIZE];
  785. (void) canonical_name(qp->tterm.term_names, name1);
  786. (void) canonical_name(rp->tterm.term_names, name2);
  787. (void) printf("%s = %s\n", name1, name2);
  788. }
  789. }
  790. }
  791. (void) printf("Differing entries:\n");
  792. termcount = 2;
  793. for (qp = heads[0]; qp; qp = qp->next) {
  794. if (qp->ncrosslinks == 1) {
  795. rp = qp->crosslinks[0];
  796. #if NCURSES_XNAMES
  797. /* sorry - we have to do this on each pass */
  798. _nc_align_termtype(&qp->tterm, &rp->tterm);
  799. #endif
  800. if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
  801. char name1[NAMESIZE], name2[NAMESIZE];
  802. entries[0] = *qp;
  803. entries[1] = *rp;
  804. (void) canonical_name(qp->tterm.term_names, name1);
  805. (void) canonical_name(rp->tterm.term_names, name2);
  806. switch (compare) {
  807. case C_DIFFERENCE:
  808. if (itrace)
  809. (void) fprintf(stderr,
  810. "%s: dumping differences\n",
  811. _nc_progname);
  812. (void) printf("comparing %s to %s.\n", name1, name2);
  813. compare_entry(compare_predicate, &entries->tterm, quiet);
  814. break;
  815. case C_COMMON:
  816. if (itrace)
  817. (void) fprintf(stderr,
  818. "%s: dumping common capabilities\n",
  819. _nc_progname);
  820. (void) printf("comparing %s to %s.\n", name1, name2);
  821. compare_entry(compare_predicate, &entries->tterm, quiet);
  822. break;
  823. case C_NAND:
  824. if (itrace)
  825. (void) fprintf(stderr,
  826. "%s: dumping differences\n",
  827. _nc_progname);
  828. (void) printf("comparing %s to %s.\n", name1, name2);
  829. compare_entry(compare_predicate, &entries->tterm, quiet);
  830. break;
  831. }
  832. }
  833. }
  834. }
  835. }
  836. static void
  837. usage(void)
  838. {
  839. static const char *tbl[] =
  840. {
  841. "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
  842. ,""
  843. ,"Options:"
  844. ," -1 print single-column"
  845. ," -C use termcap-names"
  846. ," -F compare terminfo-files"
  847. ," -I use terminfo-names"
  848. ," -L use long names"
  849. ," -R subset (see manpage)"
  850. ," -T eliminate size limits (test)"
  851. ," -U eliminate post-processing of entries"
  852. ," -V print version"
  853. #if NCURSES_XNAMES
  854. ," -a with -F, list commented-out caps"
  855. #endif
  856. ," -c list common capabilities"
  857. ," -d list different capabilities"
  858. ," -e format output for C initializer"
  859. ," -E format output as C tables"
  860. ," -f with -1, format complex strings"
  861. ," -G format %{number} to %'char'"
  862. ," -g format %'char' to %{number}"
  863. ," -i analyze initialization/reset"
  864. ," -l output terminfo names"
  865. ," -n list capabilities in neither"
  866. ," -p ignore padding specifiers"
  867. ," -q brief listing, removes headers"
  868. ," -r with -C, output in termcap form"
  869. ," -r with -F, resolve use-references"
  870. ," -s [d|i|l|c] sort fields"
  871. #if NCURSES_XNAMES
  872. ," -t suppress commented-out capabilities"
  873. #endif
  874. ," -u produce source with 'use='"
  875. ," -v number (verbose)"
  876. ," -w number (width)"
  877. #if NCURSES_XNAMES
  878. ," -x treat unknown capabilities as user-defined"
  879. #endif
  880. };
  881. const size_t first = 3;
  882. const size_t last = SIZEOF(tbl);
  883. const size_t left = (last - first + 1) / 2 + first;
  884. size_t n;
  885. for (n = 0; n < left; n++) {
  886. size_t m = (n < first) ? last : n + left - first;
  887. if (m < last)
  888. fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
  889. else
  890. fprintf(stderr, "%s\n", tbl[n]);
  891. }
  892. ExitProgram(EXIT_FAILURE);
  893. }
  894. static char *
  895. any_initializer(const char *fmt, const char *type)
  896. {
  897. static char *initializer;
  898. char *s;
  899. if (initializer == 0)
  900. initializer = (char *) malloc(strlen(entries->tterm.term_names) +
  901. strlen(type) + strlen(fmt));
  902. (void) strcpy(initializer, entries->tterm.term_names);
  903. for (s = initializer; *s != 0 && *s != '|'; s++) {
  904. if (!isalnum(UChar(*s)))
  905. *s = '_';
  906. }
  907. *s = 0;
  908. (void) sprintf(s, fmt, type);
  909. return initializer;
  910. }
  911. static char *
  912. name_initializer(const char *type)
  913. {
  914. return any_initializer("_%s_data", type);
  915. }
  916. static char *
  917. string_variable(const char *type)
  918. {
  919. return any_initializer("_s_%s", type);
  920. }
  921. /* dump C initializers for the terminal type */
  922. static void
  923. dump_initializers(TERMTYPE *term)
  924. {
  925. unsigned n;
  926. const char *str = 0;
  927. printf("\nstatic char %s[] = \"%s\";\n\n",
  928. name_initializer("alias"), entries->tterm.term_names);
  929. for_each_string(n, term) {
  930. char buf[MAX_STRING], *sp, *tp;
  931. if (VALID_STRING(term->Strings[n])) {
  932. tp = buf;
  933. *tp++ = '"';
  934. for (sp = term->Strings[n];
  935. *sp != 0 && (tp - buf) < MAX_STRING - 6;
  936. sp++) {
  937. if (isascii(UChar(*sp))
  938. && isprint(UChar(*sp))
  939. && *sp != '\\'
  940. && *sp != '"')
  941. *tp++ = *sp;
  942. else {
  943. (void) sprintf(tp, "\\%03o", UChar(*sp));
  944. tp += 4;
  945. }
  946. }
  947. *tp++ = '"';
  948. *tp = '\0';
  949. (void) printf("static char %-20s[] = %s;\n",
  950. string_variable(ExtStrname(term, n, strnames)), buf);
  951. }
  952. }
  953. printf("\n");
  954. (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
  955. for_each_boolean(n, term) {
  956. switch ((int) (term->Booleans[n])) {
  957. case TRUE:
  958. str = "TRUE";
  959. break;
  960. case FALSE:
  961. str = "FALSE";
  962. break;
  963. case ABSENT_BOOLEAN:
  964. str = "ABSENT_BOOLEAN";
  965. break;
  966. case CANCELLED_BOOLEAN:
  967. str = "CANCELLED_BOOLEAN";
  968. break;
  969. }
  970. (void) printf("\t/* %3u: %-8s */\t%s,\n",
  971. n, ExtBoolname(term, n, boolnames), str);
  972. }
  973. (void) printf("%s;\n", R_CURL);
  974. (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
  975. for_each_number(n, term) {
  976. char buf[BUFSIZ];
  977. switch (term->Numbers[n]) {
  978. case ABSENT_NUMERIC:
  979. str = "ABSENT_NUMERIC";
  980. break;
  981. case CANCELLED_NUMERIC:
  982. str = "CANCELLED_NUMERIC";
  983. break;
  984. default:
  985. sprintf(buf, "%d", term->Numbers[n]);
  986. str = buf;
  987. break;
  988. }
  989. (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
  990. ExtNumname(term, n, numnames), str);
  991. }
  992. (void) printf("%s;\n", R_CURL);
  993. (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
  994. for_each_string(n, term) {
  995. if (term->Strings[n] == ABSENT_STRING)
  996. str = "ABSENT_STRING";
  997. else if (term->Strings[n] == CANCELLED_STRING)
  998. str = "CANCELLED_STRING";
  999. else {
  1000. str = string_variable(ExtStrname(term, n, strnames));
  1001. }
  1002. (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
  1003. ExtStrname(term, n, strnames), str);
  1004. }
  1005. (void) printf("%s;\n", R_CURL);
  1006. #if NCURSES_XNAMES
  1007. if ((NUM_BOOLEANS(term) != BOOLCOUNT)
  1008. || (NUM_NUMBERS(term) != NUMCOUNT)
  1009. || (NUM_STRINGS(term) != STRCOUNT)) {
  1010. (void) printf("static char * %s[] = %s\n",
  1011. name_initializer("string_ext"), L_CURL);
  1012. for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
  1013. (void) printf("\t/* %3u: bool */\t\"%s\",\n",
  1014. n, ExtBoolname(term, n, boolnames));
  1015. }
  1016. for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
  1017. (void) printf("\t/* %3u: num */\t\"%s\",\n",
  1018. n, ExtNumname(term, n, numnames));
  1019. }
  1020. for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
  1021. (void) printf("\t/* %3u: str */\t\"%s\",\n",
  1022. n, ExtStrname(term, n, strnames));
  1023. }
  1024. (void) printf("%s;\n", R_CURL);
  1025. }
  1026. #endif
  1027. }
  1028. /* dump C initializers for the terminal type */
  1029. static void
  1030. dump_termtype(TERMTYPE *term)
  1031. {
  1032. (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
  1033. (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
  1034. (void) printf("\t\t%s,\n", name_initializer("bool"));
  1035. (void) printf("\t\t%s,\n", name_initializer("number"));
  1036. (void) printf("\t\t%s,\n", name_initializer("string"));
  1037. #if NCURSES_XNAMES
  1038. (void) printf("#if NCURSES_XNAMES\n");
  1039. (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
  1040. (void) printf("\t\t%s,\t/* ...corresponding names */\n",
  1041. ((NUM_BOOLEANS(term) != BOOLCOUNT)
  1042. || (NUM_NUMBERS(term) != NUMCOUNT)
  1043. || (NUM_STRINGS(term) != STRCOUNT))
  1044. ? name_initializer("string_ext")
  1045. : "(char **)0");
  1046. (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
  1047. (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
  1048. (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
  1049. (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
  1050. NUM_BOOLEANS(term) - BOOLCOUNT);
  1051. (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
  1052. NUM_NUMBERS(term) - NUMCOUNT);
  1053. (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
  1054. NUM_STRINGS(term) - STRCOUNT);
  1055. (void) printf("#endif /* NCURSES_XNAMES */\n");
  1056. #else
  1057. (void) term;
  1058. #endif /* NCURSES_XNAMES */
  1059. (void) printf("\t%s\n", R_CURL);
  1060. }
  1061. static int
  1062. optarg_to_number(void)
  1063. {
  1064. char *temp = 0;
  1065. long value = strtol(optarg, &temp, 0);
  1066. if (temp == 0 || temp == optarg || *temp != 0) {
  1067. fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
  1068. ExitProgram(EXIT_FAILURE);
  1069. }
  1070. return (int) value;
  1071. }
  1072. static char *
  1073. terminal_env(void)
  1074. {
  1075. char *terminal;
  1076. if ((terminal = getenv("TERM")) == 0) {
  1077. (void) fprintf(stderr,
  1078. "%s: environment variable TERM not set\n",
  1079. _nc_progname);
  1080. exit(EXIT_FAILURE);
  1081. }
  1082. return terminal;
  1083. }
  1084. /***************************************************************************
  1085. *
  1086. * Main sequence
  1087. *
  1088. ***************************************************************************/
  1089. int
  1090. main(int argc, char *argv[])
  1091. {
  1092. /* Avoid "local data >32k" error with mwcc */
  1093. /* Also avoid overflowing smaller stacks on systems like AmigaOS */
  1094. path *tfile = 0;
  1095. char **tname = 0;
  1096. int maxterms;
  1097. char **myargv;
  1098. char *firstdir, *restdir;
  1099. int c, i, len;
  1100. bool formatted = FALSE;
  1101. bool filecompare = FALSE;
  1102. int initdump = 0;
  1103. bool init_analyze = FALSE;
  1104. bool suppress_untranslatable = FALSE;
  1105. /* where is the terminfo database location going to default to? */
  1106. restdir = firstdir = 0;
  1107. #if NCURSES_XNAMES
  1108. use_extended_names(FALSE);
  1109. #endif
  1110. _nc_progname = _nc_rootname(argv[0]);
  1111. /* make sure we have enough space to add two terminal entries */
  1112. myargv = typeCalloc(char *, (size_t) (argc + 3));
  1113. memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
  1114. argv = myargv;
  1115. while ((c = getopt(argc,
  1116. argv,
  1117. "1A:aB:CcdEeFfGgIiLlnpqR:rs:TtUuVv:w:x")) != -1) {
  1118. switch (c) {
  1119. case '1':
  1120. mwidth = 0;
  1121. break;
  1122. case 'A':
  1123. firstdir = optarg;
  1124. break;
  1125. #if NCURSES_XNAMES
  1126. case 'a':
  1127. _nc_disable_period = TRUE;
  1128. use_extended_names(TRUE);
  1129. break;
  1130. #endif
  1131. case 'B':
  1132. restdir = optarg;
  1133. break;
  1134. case 'C':
  1135. outform = F_TERMCAP;
  1136. tversion = "BSD";
  1137. if (sortmode == S_DEFAULT)
  1138. sortmode = S_TERMCAP;
  1139. break;
  1140. case 'c':
  1141. compare = C_COMMON;
  1142. break;
  1143. case 'd':
  1144. compare = C_DIFFERENCE;
  1145. break;
  1146. case 'E':
  1147. initdump |= 2;
  1148. break;
  1149. case 'e':
  1150. initdump |= 1;
  1151. break;
  1152. case 'F':
  1153. filecompare = TRUE;
  1154. break;
  1155. case 'f':
  1156. formatted = TRUE;
  1157. break;
  1158. case 'G':
  1159. numbers = 1;
  1160. break;
  1161. case 'g':
  1162. numbers = -1;
  1163. break;
  1164. case 'I':
  1165. outform = F_TERMINFO;
  1166. if (sortmode == S_DEFAULT)
  1167. sortmode = S_VARIABLE;
  1168. tversion = 0;
  1169. break;
  1170. case 'i':
  1171. init_analyze = TRUE;
  1172. break;
  1173. case 'L':
  1174. outform = F_VARIABLE;
  1175. if (sortmode == S_DEFAULT)
  1176. sortmode = S_VARIABLE;
  1177. break;
  1178. case 'l':
  1179. outform = F_TERMINFO;
  1180. break;
  1181. case 'n':
  1182. compare = C_NAND;
  1183. break;
  1184. case 'p':
  1185. ignorepads = TRUE;
  1186. break;
  1187. case 'q':
  1188. quiet = TRUE;
  1189. s_absent = "-";
  1190. s_cancel = "@";
  1191. bool_sep = ", ";
  1192. break;
  1193. case 'R':
  1194. tversion = optarg;
  1195. break;
  1196. case 'r':
  1197. tversion = 0;
  1198. break;
  1199. case 's':
  1200. if (*optarg == 'd')
  1201. sortmode = S_NOSORT;
  1202. else if (*optarg == 'i')
  1203. sortmode = S_TERMINFO;
  1204. else if (*optarg == 'l')
  1205. sortmode = S_VARIABLE;
  1206. else if (*optarg == 'c')
  1207. sortmode = S_TERMCAP;
  1208. else {
  1209. (void) fprintf(stderr,
  1210. "%s: unknown sort mode\n",
  1211. _nc_progname);
  1212. ExitProgram(EXIT_FAILURE);
  1213. }
  1214. break;
  1215. case 'T':
  1216. limited = FALSE;
  1217. break;
  1218. #if NCURSES_XNAMES
  1219. case 't':
  1220. _nc_disable_period = FALSE;
  1221. suppress_untranslatable = TRUE;
  1222. break;
  1223. #endif
  1224. case 'U':
  1225. literal = TRUE;
  1226. break;
  1227. case 'u':
  1228. compare = C_USEALL;
  1229. break;
  1230. case 'V':
  1231. puts(curses_version());
  1232. ExitProgram(EXIT_SUCCESS);
  1233. case 'v':
  1234. itrace = optarg_to_number();
  1235. set_trace_level(itrace);
  1236. break;
  1237. case 'w':
  1238. mwidth = optarg_to_number();
  1239. break;
  1240. #if NCURSES_XNAMES
  1241. case 'x':
  1242. use_extended_names(TRUE);
  1243. break;
  1244. #endif
  1245. default:
  1246. usage();
  1247. }
  1248. }
  1249. maxterms = (argc + 2 - optind);
  1250. tfile = typeMalloc(path, maxterms);
  1251. tname = typeCalloc(char *, maxterms);
  1252. entries = typeCalloc(ENTRY, maxterms);
  1253. if (tfile == 0
  1254. || tname == 0
  1255. || entries == 0) {
  1256. fprintf(stderr, "%s: not enough memory\n", _nc_progname);
  1257. ExitProgram(EXIT_FAILURE);
  1258. }
  1259. /* by default, sort by terminfo name */
  1260. if (sortmode == S_DEFAULT)
  1261. sortmode = S_TERMINFO;
  1262. /* set up for display */
  1263. dump_init(tversion, outform, sortmode, mwidth, itrace, formatted);
  1264. /* make sure we have at least one terminal name to work with */
  1265. if (optind >= argc)
  1266. argv[argc++] = terminal_env();
  1267. /* if user is after a comparison, make sure we have two entries */
  1268. if (compare != C_DEFAULT && optind >= argc - 1)
  1269. argv[argc++] = terminal_env();
  1270. /* exactly two terminal names with no options means do -d */
  1271. if (argc - optind == 2 && compare == C_DEFAULT)
  1272. compare = C_DIFFERENCE;
  1273. if (!filecompare) {
  1274. /* grab the entries */
  1275. termcount = 0;
  1276. for (; optind < argc; optind++) {
  1277. const char *directory = termcount ? restdir : firstdir;
  1278. int status;
  1279. tname[termcount] = argv[optind];
  1280. if (directory) {
  1281. #if USE_DATABASE
  1282. #if MIXEDCASE_FILENAMES
  1283. #define LEAF_FMT "%c"
  1284. #else
  1285. #define LEAF_FMT "%02x"
  1286. #endif
  1287. (void) sprintf(tfile[termcount], "%s/" LEAF_FMT "/%s",
  1288. directory,
  1289. UChar(*argv[optind]), argv[optind]);
  1290. if (itrace)
  1291. (void) fprintf(stderr,
  1292. "%s: reading entry %s from file %s\n",
  1293. _nc_progname,
  1294. argv[optind], tfile[termcount]);
  1295. status = _nc_read_file_entry(tfile[termcount],
  1296. &entries[termcount].tterm);
  1297. #else
  1298. (void) fprintf(stderr, "%s: terminfo files not supported\n",
  1299. _nc_progname);
  1300. ExitProgram(EXIT_FAILURE);
  1301. #endif
  1302. } else {
  1303. if (itrace)
  1304. (void) fprintf(stderr,
  1305. "%s: reading entry %s from database\n",
  1306. _nc_progname,
  1307. tname[termcount]);
  1308. status = _nc_read_entry(tname[termcount],
  1309. tfile[termcount],
  1310. &entries[termcount].tterm);
  1311. directory = TERMINFO; /* for error message */
  1312. }
  1313. if (status <= 0) {
  1314. (void) fprintf(stderr,
  1315. "%s: couldn't open terminfo file %s.\n",
  1316. _nc_progname,
  1317. tfile[termcount]);
  1318. ExitProgram(EXIT_FAILURE);
  1319. }
  1320. repair_acsc(&entries[termcount].tterm);
  1321. termcount++;
  1322. }
  1323. #if NCURSES_XNAMES
  1324. if (termcount > 1)
  1325. _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
  1326. #endif
  1327. /* dump as C initializer for the terminal type */
  1328. if (initdump) {
  1329. if (initdump & 1)
  1330. dump_termtype(&entries[0].tterm);
  1331. if (initdump & 2)
  1332. dump_initializers(&entries[0].tterm);
  1333. }
  1334. /* analyze the init strings */
  1335. else if (init_analyze) {
  1336. #undef CUR
  1337. #define CUR entries[0].tterm.
  1338. analyze_string("is1", init_1string, &entries[0].tterm);
  1339. analyze_string("is2", init_2string, &entries[0].tterm);
  1340. analyze_string("is3", init_3string, &entries[0].tterm);
  1341. analyze_string("rs1", reset_1string, &entries[0].tterm);
  1342. analyze_string("rs2", reset_2string, &entries[0].tterm);
  1343. analyze_string("rs3", reset_3string, &entries[0].tterm);
  1344. analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
  1345. analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
  1346. #undef CUR
  1347. } else {
  1348. /*
  1349. * Here's where the real work gets done
  1350. */
  1351. switch (compare) {
  1352. case C_DEFAULT:
  1353. if (itrace)
  1354. (void) fprintf(stderr,
  1355. "%s: about to dump %s\n",
  1356. _nc_progname,
  1357. tname[0]);
  1358. (void) printf("#\tReconstructed via infocmp from file: %s\n",
  1359. tfile[0]);
  1360. dump_entry(&entries[0].tterm,
  1361. suppress_untranslatable,
  1362. limited,
  1363. numbers,
  1364. NULL);
  1365. len = show_entry();
  1366. if (itrace)
  1367. (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
  1368. break;
  1369. case C_DIFFERENCE:
  1370. if (itrace)
  1371. (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
  1372. (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
  1373. compare_entry(compare_predicate, &entries->tterm, quiet);
  1374. break;
  1375. case C_COMMON:
  1376. if (itrace)
  1377. (void) fprintf(stderr,
  1378. "%s: dumping common capabilities\n",
  1379. _nc_progname);
  1380. (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
  1381. compare_entry(compare_predicate, &entries->tterm, quiet);
  1382. break;
  1383. case C_NAND:
  1384. if (itrace)
  1385. (void) fprintf(stderr,
  1386. "%s: dumping differences\n",
  1387. _nc_progname);
  1388. (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
  1389. compare_entry(compare_predicate, &entries->tterm, quiet);
  1390. break;
  1391. case C_USEALL:
  1392. if (itrace)
  1393. (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
  1394. dump_entry(&entries[0].tterm,
  1395. suppress_untranslatable,
  1396. limited,
  1397. numbers,
  1398. use_predicate);
  1399. for (i = 1; i < termcount; i++)
  1400. dump_uses(tname[i], !(outform == F_TERMCAP
  1401. || outform == F_TCONVERR));
  1402. len = show_entry();
  1403. if (itrace)
  1404. (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
  1405. break;
  1406. }
  1407. }
  1408. } else if (compare == C_USEALL)
  1409. (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
  1410. else if (compare == C_DEFAULT)
  1411. (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
  1412. else if (argc - optind != 2)
  1413. (void) fprintf(stderr,
  1414. "File comparison needs exactly two file arguments.\n");
  1415. else
  1416. file_comparison(argc - optind, argv + optind);
  1417. #if NO_LEAKS
  1418. free(myargv);
  1419. free(tfile);
  1420. free(tname);
  1421. #endif
  1422. ExitProgram(EXIT_SUCCESS);
  1423. }
  1424. /* infocmp.c ends here */