PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/sxpm/sxpm.c

#
C | 750 lines | 583 code | 75 blank | 92 comment | 109 complexity | 399a8d29bc2d05aba001b0ec83cf7897 MD5 | raw file
  1. /*
  2. * Copyright (C) 1989-95 GROUPE BULL
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * Except as contained in this notice, the name of GROUPE BULL shall not be
  22. * used in advertising or otherwise to promote the sale, use or other dealings
  23. * in this Software without prior written authorization from GROUPE BULL.
  24. */
  25. /*****************************************************************************\
  26. * sxpm.c: *
  27. * *
  28. * Show XPM File program *
  29. * *
  30. * Developed by Arnaud Le Hors *
  31. \*****************************************************************************/
  32. #ifdef HAVE_CONFIG_H
  33. #include <config.h>
  34. #endif
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <X11/StringDefs.h>
  38. #include <X11/Intrinsic.h>
  39. #include <X11/IntrinsicP.h>
  40. #include <X11/Shell.h>
  41. #ifdef VMS
  42. #include <X11/shape.h>
  43. #else
  44. #include <X11/extensions/shape.h>
  45. #endif
  46. #include <X11/xpm.h>
  47. #ifdef USE_GETTEXT
  48. #include <locale.h>
  49. #include <libintl.h>
  50. #else
  51. #define gettext(a) (a)
  52. #endif
  53. /* XPM */
  54. /* plaid pixmap */
  55. static char *plaid[] = {
  56. /* width height ncolors chars_per_pixel */
  57. "22 22 4 2 XPMEXT",
  58. /* colors */
  59. " c red m white s light_color",
  60. "Y c green m black s lines_in_mix",
  61. "+ c yellow m white s lines_in_dark",
  62. "x m black s dark_color",
  63. /* pixels */
  64. "x x x x x x x x x x x x + x x x x x ",
  65. " x x x x x x x x x x x x x x x x ",
  66. "x x x x x x x x x x x x + x x x x x ",
  67. " x x x x x x x x x x x x x x x x ",
  68. "x x x x x x x x x x x x + x x x x x ",
  69. "Y Y Y Y Y x Y Y Y Y Y + x + x + x + x + x + ",
  70. "x x x x x x x x x x x x + x x x x x ",
  71. " x x x x x x x x x x x x x x x x ",
  72. "x x x x x x x x x x x x + x x x x x ",
  73. " x x x x x x x x x x x x x x x x ",
  74. "x x x x x x x x x x x x + x x x x x ",
  75. " x x x x Y x x x ",
  76. " x x x Y x x ",
  77. " x x x x Y x x x ",
  78. " x x x Y x x ",
  79. " x x x x Y x x x ",
  80. "x x x x x x x x x x x x x x x x x x x x x x ",
  81. " x x x x Y x x x ",
  82. " x x x Y x x ",
  83. " x x x x Y x x x ",
  84. " x x x Y x x ",
  85. " x x x x Y x x x ",
  86. "bullshit",
  87. "XPMEXT ext1 data1",
  88. "XPMEXT ext2",
  89. "data2_1",
  90. "data2_2",
  91. "XPMEXT",
  92. "foo",
  93. "",
  94. "XPMEXT ext3",
  95. "data3",
  96. "XPMENDEXT"
  97. };
  98. #define win XtWindow(topw)
  99. #define dpy XtDisplay(topw)
  100. #define root XRootWindowOfScreen(XtScreen(topw))
  101. #define xrdb XtDatabase(dpy)
  102. static Colormap colormap;
  103. void Usage(void);
  104. void ErrorMessage(int ErrorStatus, const char *tag);
  105. void Punt(int i);
  106. void VersionInfo(void);
  107. void kinput(Widget widget, char *tag, XEvent *xe, Boolean *b);
  108. void GetNumbers(int num, int *format_return,
  109. int *libmajor_return,
  110. char *libminor_return);
  111. #define IWIDTH 50
  112. #define IHEIGHT 50
  113. typedef struct _XpmIcon {
  114. Pixmap pixmap;
  115. Pixmap mask;
  116. XpmAttributes attributes;
  117. } XpmIcon;
  118. static char **command;
  119. static Widget topw;
  120. static XpmIcon view, icon;
  121. static XrmOptionDescRec options[] = {
  122. {"-hints", ".hints", XrmoptionNoArg, (XtPointer) "True"},
  123. {"-icon", ".icon", XrmoptionSepArg, (XtPointer) NULL},
  124. };
  125. int
  126. main(
  127. int argc,
  128. char **argv)
  129. {
  130. int ErrorStatus;
  131. unsigned int verbose = 0; /* performs verbose output */
  132. unsigned int stdinf = 1; /* read from stdin */
  133. unsigned int stdoutf = 0; /* write to stdout */
  134. unsigned int nod = 0; /* no display */
  135. unsigned int nom = 0; /* no mask display */
  136. unsigned int incResize = 0;
  137. unsigned int resize = 0;
  138. unsigned int w_rtn;
  139. unsigned int h_rtn;
  140. char *input = NULL;
  141. char *output = NULL;
  142. char *iconFile = NULL;
  143. unsigned int numsymbols = 0;
  144. XpmColorSymbol symbols[10];
  145. char *stype;
  146. XrmValue val;
  147. unsigned long valuemask = 0;
  148. int n;
  149. Arg args[4];
  150. #ifdef Debug
  151. char **data;
  152. char *buffer;
  153. #endif
  154. #ifdef USE_GETTEXT
  155. XtSetLanguageProc(NULL,NULL,NULL);
  156. bindtextdomain("sxpm",LOCALEDIR);
  157. textdomain("sxpm");
  158. #endif
  159. topw = XtInitialize(argv[0], "Sxpm",
  160. options, XtNumber(options), &argc, argv);
  161. if (!topw) {
  162. /* L10N_Comments : Error if no $DISPLAY or $DISPLAY can't be opened.
  163. Not normally reached as Xt exits before we get here. */
  164. fprintf(stderr, gettext("Sxpm Error... [ Undefined DISPLAY ]\n"));
  165. exit(1);
  166. }
  167. colormap = XDefaultColormapOfScreen(XtScreen(topw));
  168. /*
  169. * geometry management
  170. */
  171. if (XrmGetResource(xrdb, NULL, "sxpm.geometry", &stype, &val)
  172. || XrmGetResource(xrdb, NULL, "Sxpm.geometry", &stype, &val)) {
  173. int flags;
  174. int x_rtn;
  175. int y_rtn;
  176. char *geo = NULL;
  177. geo = (char *) val.addr;
  178. flags = XParseGeometry(geo, &x_rtn, &y_rtn,
  179. (unsigned int *) &w_rtn,
  180. (unsigned int *) &h_rtn);
  181. if (!((WidthValue & flags) && (HeightValue & flags)))
  182. resize = 1;
  183. } else
  184. resize = 1;
  185. n = 0;
  186. if (resize) {
  187. w_rtn = 0;
  188. h_rtn = 0;
  189. XtSetArg(args[n], XtNwidth, 1);
  190. n++;
  191. XtSetArg(args[n], XtNheight, 1);
  192. n++;
  193. }
  194. XtSetArg(args[n], XtNmappedWhenManaged, False);
  195. n++;
  196. XtSetArg(args[n], XtNinput, True);
  197. n++;
  198. XtSetValues(topw, args, n);
  199. if ((XrmGetResource(xrdb, "sxpm.hints", "", &stype, &val)
  200. || XrmGetResource(xrdb, "Sxpm.hints", "", &stype, &val))
  201. && !strcmp((char *) val.addr, "True")) {
  202. /* gotcha */
  203. incResize = 1;
  204. resize = 1;
  205. }
  206. /*
  207. * icon management
  208. */
  209. if (XrmGetResource(xrdb, "sxpm.icon", "", &stype, &val)
  210. || XrmGetResource(xrdb, "Sxpm.icon", "", &stype, &val)) {
  211. iconFile = (char *) val.addr;
  212. }
  213. if (iconFile) {
  214. XColor color, junk;
  215. Pixel bpix;
  216. Window iconW;
  217. if (XAllocNamedColor(dpy, colormap, "black", &color, &junk))
  218. bpix = color.pixel;
  219. else
  220. bpix = XBlackPixelOfScreen(XtScreen(topw));
  221. iconW = XCreateSimpleWindow(dpy, root, 0, 0,
  222. IWIDTH, IHEIGHT, 1, bpix, bpix);
  223. icon.attributes.valuemask = XpmReturnAllocPixels;
  224. ErrorStatus = XpmReadFileToPixmap(dpy, root, iconFile, &icon.pixmap,
  225. &icon.mask, &icon.attributes);
  226. ErrorMessage(ErrorStatus, "Icon");
  227. XSetWindowBackgroundPixmap(dpy, iconW, icon.pixmap);
  228. n = 0;
  229. XtSetArg(args[n], XtNbackground, bpix);
  230. n++;
  231. XtSetArg(args[n], XtNiconWindow, iconW);
  232. n++;
  233. XtSetValues(topw, args, n);
  234. }
  235. /*
  236. * arguments parsing
  237. */
  238. command = argv;
  239. for (n = 1; n < argc; n++) {
  240. if (strcmp(argv[n], "-plaid") == 0) {
  241. stdinf = 0;
  242. continue;
  243. }
  244. if (argv[n][0] != '-') {
  245. stdinf = 0;
  246. input = argv[n];
  247. continue;
  248. }
  249. if ((strlen(argv[n]) == 1) && (argv[n][0] == '-'))
  250. /* stdin */
  251. continue;
  252. if (strcmp(argv[n], "-o") == 0) {
  253. if (n < argc - 1) {
  254. if ((strlen(argv[n + 1]) == 1) && (argv[n + 1][0] == '-'))
  255. stdoutf = 1;
  256. else
  257. output = argv[n + 1];
  258. n++;
  259. continue;
  260. } else
  261. Usage();
  262. }
  263. if (strcmp(argv[n], "-nod") == 0) {
  264. nod = 1;
  265. continue;
  266. }
  267. if (strcmp(argv[n], "-nom") == 0) {
  268. nom = 1;
  269. continue;
  270. }
  271. if (strcmp(argv[n], "-sc") == 0) {
  272. if (n < argc - 2) {
  273. valuemask |= XpmColorSymbols;
  274. symbols[numsymbols].name = argv[++n];
  275. symbols[numsymbols++].value = argv[++n];
  276. continue;
  277. } else
  278. Usage();
  279. }
  280. if (strcmp(argv[n], "-sp") == 0) {
  281. if (n < argc - 2) {
  282. valuemask |= XpmColorSymbols;
  283. symbols[numsymbols].name = argv[++n];
  284. symbols[numsymbols].value = NULL;
  285. symbols[numsymbols++].pixel = atol(argv[++n]);
  286. continue;
  287. }
  288. }
  289. if (strcmp(argv[n], "-cp") == 0) {
  290. if (n < argc - 2) {
  291. valuemask |= XpmColorSymbols;
  292. symbols[numsymbols].name = NULL;
  293. symbols[numsymbols].value = argv[++n];
  294. symbols[numsymbols++].pixel = atol(argv[++n]);
  295. continue;
  296. }
  297. }
  298. if (strcmp(argv[n], "-mono") == 0) {
  299. valuemask |= XpmColorKey;
  300. view.attributes.color_key = XPM_MONO;
  301. continue;
  302. }
  303. if (strcmp(argv[n], "-gray4") == 0 || strcmp(argv[n], "-grey4") == 0) {
  304. valuemask |= XpmColorKey;
  305. view.attributes.color_key = XPM_GRAY4;
  306. continue;
  307. }
  308. if (strcmp(argv[n], "-gray") == 0 || strcmp(argv[n], "-grey") == 0) {
  309. valuemask |= XpmColorKey;
  310. view.attributes.color_key = XPM_GRAY;
  311. continue;
  312. }
  313. if (strcmp(argv[n], "-color") == 0) {
  314. valuemask |= XpmColorKey;
  315. view.attributes.color_key = XPM_COLOR;
  316. continue;
  317. }
  318. if (strncmp(argv[n], "-closecolors", 6) == 0) {
  319. valuemask |= XpmCloseness;
  320. view.attributes.closeness = 40000;
  321. continue;
  322. }
  323. if (strcmp(argv[n], "-rgb") == 0) {
  324. if (n < argc - 1) {
  325. valuemask |= XpmRgbFilename;
  326. view.attributes.rgb_fname = argv[++n];
  327. continue;
  328. } else
  329. Usage();
  330. }
  331. if (strncmp(argv[n], "-version", 4) == 0) {
  332. VersionInfo();
  333. exit(0);
  334. }
  335. if (strcmp(argv[n], "-v") == 0) {
  336. verbose = 1;
  337. continue;
  338. }
  339. if (strcmp(argv[n], "-pcmap") == 0) {
  340. valuemask |= XpmColormap;
  341. continue;
  342. }
  343. Usage();
  344. }
  345. XtRealizeWidget(topw);
  346. if (valuemask & XpmColormap) {
  347. colormap = XCreateColormap(dpy, win,
  348. DefaultVisual(dpy, DefaultScreen(dpy)),
  349. AllocNone);
  350. view.attributes.colormap = colormap;
  351. XSetWindowColormap(dpy, win, colormap);
  352. }
  353. view.attributes.colorsymbols = symbols;
  354. view.attributes.numsymbols = numsymbols;
  355. view.attributes.valuemask = valuemask;
  356. #ifdef Debug
  357. /* this is just to test the XpmCreateDataFromPixmap function */
  358. view.attributes.valuemask |= XpmReturnAllocPixels;
  359. view.attributes.valuemask |= XpmReturnExtensions;
  360. ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
  361. &view.pixmap, &view.mask,
  362. &view.attributes);
  363. ErrorMessage(ErrorStatus, "Plaid");
  364. ErrorStatus = XpmCreateDataFromPixmap(dpy, &data, view.pixmap, view.mask,
  365. &view.attributes);
  366. ErrorMessage(ErrorStatus, "Data");
  367. if (verbose && view.attributes.nextensions) {
  368. unsigned int i, j;
  369. for (i = 0; i < view.attributes.nextensions; i++) {
  370. fprintf(stderr, "Xpm extension : %s\n",
  371. view.attributes.extensions[i].name);
  372. for (j = 0; j < view.attributes.extensions[i].nlines; j++)
  373. fprintf(stderr, "\t\t%s\n",
  374. view.attributes.extensions[i].lines[j]);
  375. }
  376. }
  377. XFreePixmap(dpy, view.pixmap);
  378. if (view.mask)
  379. XFreePixmap(dpy, view.mask);
  380. XFreeColors(dpy, colormap,
  381. view.attributes.alloc_pixels,
  382. view.attributes.nalloc_pixels, 0);
  383. XpmFreeAttributes(&view.attributes);
  384. view.attributes.valuemask = valuemask;
  385. #endif
  386. if (input || stdinf) {
  387. view.attributes.valuemask |= XpmReturnInfos;
  388. view.attributes.valuemask |= XpmReturnAllocPixels;
  389. view.attributes.valuemask |= XpmReturnExtensions;
  390. #ifdef Debug
  391. XpmFree(data);
  392. /*
  393. * this is just to test the XpmCreatePixmapFromBuffer and
  394. * XpmCreateBufferFromPixmap functions
  395. */
  396. ErrorStatus = XpmReadFileToBuffer(input, &buffer);
  397. ErrorMessage(ErrorStatus, "CreateBufferFromFile");
  398. ErrorStatus = XpmCreatePixmapFromBuffer(dpy, win, buffer,
  399. &view.pixmap, &view.mask,
  400. &view.attributes);
  401. ErrorMessage(ErrorStatus, "CreatePixmapFromBuffer");
  402. XpmFree(buffer);
  403. ErrorStatus = XpmCreateBufferFromPixmap(dpy, &buffer,
  404. view.pixmap, view.mask,
  405. &view.attributes);
  406. ErrorMessage(ErrorStatus, "CreateBufferFromPixmap");
  407. ErrorStatus = XpmWriteFileFromBuffer("buffer_output", buffer);
  408. ErrorMessage(ErrorStatus, "WriteFileFromBuffer");
  409. XpmFree(buffer);
  410. if (view.pixmap) {
  411. XFreePixmap(dpy, view.pixmap);
  412. if (view.mask)
  413. XFreePixmap(dpy, view.mask);
  414. XFreeColors(dpy, colormap, view.attributes.alloc_pixels,
  415. view.attributes.nalloc_pixels, 0);
  416. XpmFreeAttributes(&view.attributes);
  417. }
  418. ErrorStatus = XpmReadFileToData(input, &data);
  419. ErrorMessage(ErrorStatus, "ReadFileToData");
  420. ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
  421. &view.pixmap, &view.mask,
  422. &view.attributes);
  423. ErrorMessage(ErrorStatus, "CreatePixmapFromData");
  424. ErrorStatus = XpmWriteFileFromData("sxpmout.xpm", data);
  425. ErrorMessage(ErrorStatus, "WriteFileFromData");
  426. XpmFree(data);
  427. XpmFreeAttributes(&view.attributes);
  428. #endif
  429. ErrorStatus = XpmReadFileToPixmap(dpy, win, input,
  430. &view.pixmap, &view.mask,
  431. &view.attributes);
  432. ErrorMessage(ErrorStatus, "Read");
  433. if (verbose && view.attributes.nextensions) {
  434. unsigned int i, j;
  435. for (i = 0; i < view.attributes.nextensions; i++) {
  436. /* L10N_Comments : Output when -v & file has extensions
  437. %s is replaced by extension name */
  438. fprintf(stderr, gettext("Xpm extension : %s\n"),
  439. view.attributes.extensions[i].name);
  440. for (j = 0; j < view.attributes.extensions[i].nlines; j++)
  441. fprintf(stderr, "\t\t%s\n",
  442. view.attributes.extensions[i].lines[j]);
  443. }
  444. }
  445. } else {
  446. #ifdef Debug
  447. ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
  448. &view.pixmap, &view.mask,
  449. &view.attributes);
  450. XpmFree(data);
  451. #else
  452. ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
  453. &view.pixmap, &view.mask,
  454. &view.attributes);
  455. #endif
  456. ErrorMessage(ErrorStatus, "Plaid");
  457. }
  458. if (output || stdoutf) {
  459. ErrorStatus = XpmWriteFileFromPixmap(dpy, output, view.pixmap,
  460. view.mask, &view.attributes);
  461. ErrorMessage(ErrorStatus, "Write");
  462. }
  463. if (!nod) {
  464. /*
  465. * manage display if requested
  466. */
  467. XSizeHints size_hints;
  468. char *xString = NULL;
  469. if (w_rtn && h_rtn
  470. && ((w_rtn < view.attributes.width)
  471. || h_rtn < view.attributes.height)) {
  472. resize = 1;
  473. }
  474. if (resize) {
  475. XtResizeWidget(topw,
  476. view.attributes.width, view.attributes.height, 1);
  477. }
  478. if (incResize) {
  479. size_hints.flags = USSize | PMinSize | PResizeInc;
  480. size_hints.height = view.attributes.height;
  481. size_hints.width = view.attributes.width;
  482. size_hints.height_inc = view.attributes.height;
  483. size_hints.width_inc = view.attributes.width;
  484. } else
  485. size_hints.flags = PMinSize;
  486. size_hints.min_height = view.attributes.height;
  487. size_hints.min_width = view.attributes.width;
  488. XSetWMNormalHints(dpy, win, &size_hints);
  489. if (input) {
  490. xString = (char *) XtMalloc((sizeof(char) * strlen(input)) + 20);
  491. sprintf(xString, "Sxpm: %s", input);
  492. XStoreName(dpy, win, xString);
  493. XSetIconName(dpy, win, xString);
  494. } else if (stdinf) {
  495. XStoreName(dpy, win, "Sxpm: stdin");
  496. XSetIconName(dpy, win, "Sxpm: stdin");
  497. } else {
  498. XStoreName(dpy, win, "Sxpm");
  499. XSetIconName(dpy, win, "Sxpm");
  500. }
  501. XtAddEventHandler(topw, KeyPressMask, False,
  502. (XtEventHandler) kinput, NULL);
  503. XSetWindowBackgroundPixmap(dpy, win, view.pixmap);
  504. if (view.mask && !nom)
  505. XShapeCombineMask(dpy, win, ShapeBounding, 0, 0,
  506. view.mask, ShapeSet);
  507. XClearWindow(dpy, win);
  508. XtMapWidget(topw);
  509. if (xString)
  510. XtFree(xString);
  511. XtMainLoop();
  512. }
  513. Punt(0);
  514. /* Muffle gcc */
  515. return 0;
  516. }
  517. void
  518. Usage(void)
  519. {
  520. /* L10N_Comments : Usage message (sxpm -h) in two parts.
  521. In the first part %s is replaced by the command name. */
  522. fprintf(stderr, gettext("\nUsage: %s [options...]\n"), command[0]);
  523. fprintf(stderr, gettext("Where options are:\n\
  524. \n\
  525. [-d host:display] Display to connect to.\n\
  526. [-g geom] Geometry of window.\n\
  527. [-hints] Set ResizeInc for window.\n\
  528. [-icon filename] Set pixmap for iconWindow.\n\
  529. [-plaid] Read the included plaid pixmap.\n\
  530. [filename] Read from file 'filename', and from standard\n\
  531. input if 'filename' is '-'.\n\
  532. [-o filename] Write to file 'filename', and to standard\n\
  533. output if 'filename' is '-'.\n\
  534. [-pcmap] Use a private colormap.\n\
  535. [-closecolors] Try to use `close' colors.\n\
  536. [-nod] Don't display in window.\n\
  537. [-nom] Don't use clip mask if any.\n\
  538. [-mono] Use the colors specified for a monochrome visual.\n\
  539. [-grey4] Use the colors specified for a 4 greyscale visual.\n\
  540. [-grey] Use the colors specified for a greyscale visual.\n\
  541. [-color] Use the colors specified for a color visual.\n\
  542. [-sc symbol color] Override color defaults.\n\
  543. [-sp symbol pixel] Override color defaults.\n\
  544. [-cp color pixel] Override color defaults.\n\
  545. [-rgb filename] Search color names in the rgb text file 'filename'.\n\
  546. [-v] Verbose - print out extensions.\n\
  547. [-version] Print out program's version number\n\
  548. and library's version number if different.\n\
  549. if no input is specified sxpm reads from standard input.\n\
  550. \n"));
  551. exit(0);
  552. }
  553. void
  554. ErrorMessage(
  555. int ErrorStatus,
  556. const char *tag)
  557. {
  558. char *error = NULL;
  559. char *warning = NULL;
  560. switch (ErrorStatus) {
  561. case XpmSuccess:
  562. return;
  563. case XpmColorError:
  564. /* L10N_Comments : The following set of messages are classified as
  565. either errors or warnings. Based on the class of message, different
  566. wrappers are selected at the end to state the message source & class.
  567. L10N_Comments : WARNING produced when filename can be read, but
  568. contains an invalid color specification (need to create test case)*/
  569. warning = gettext("Could not parse or alloc requested color");
  570. break;
  571. case XpmOpenFailed:
  572. /* L10N_Comments : ERROR produced when filename does not exist
  573. or insufficient permissions to open (i.e. sxpm /no/such/file ) */
  574. error = gettext("Cannot open file");
  575. break;
  576. case XpmFileInvalid:
  577. /* L10N_Comments : ERROR produced when filename can be read, but
  578. is not an XPM file (i.e. sxpm /dev/null ) */
  579. error = gettext("Invalid XPM file");
  580. break;
  581. case XpmNoMemory:
  582. /* L10N_Comments : ERROR produced when filename can be read, but
  583. is too big for memory
  584. (i.e. limit datasize 32 ; sxpm /usr/dt/backdrops/Crochet.pm ) */
  585. error = gettext("Not enough memory");
  586. break;
  587. case XpmColorFailed:
  588. /* L10N_Comments : ERROR produced when filename can be read, but
  589. contains an invalid color specification (need to create test case)*/
  590. error = gettext("Failed to parse or alloc some color");
  591. break;
  592. }
  593. if (warning)
  594. /* L10N_Comments : Wrapper around above WARNING messages.
  595. First %s is the tag for the operation that produced the warning.
  596. Second %s is the message selected from the above set. */
  597. fprintf(stderr, gettext("%s Xpm Warning: %s.\n"), tag, warning);
  598. if (error) {
  599. /* L10N_Comments : Wrapper around above ERROR messages.
  600. First %s is the tag for the operation that produced the error.
  601. Second %s is the message selected from the above set */
  602. fprintf(stderr, gettext("%s Xpm Error: %s.\n"), tag, error);
  603. Punt(1);
  604. }
  605. }
  606. void
  607. Punt(int i)
  608. {
  609. if (icon.pixmap) {
  610. XFreePixmap(dpy, icon.pixmap);
  611. if (icon.mask)
  612. XFreePixmap(dpy, icon.mask);
  613. XFreeColors(dpy, colormap,
  614. icon.attributes.alloc_pixels,
  615. icon.attributes.nalloc_pixels, 0);
  616. XpmFreeAttributes(&icon.attributes);
  617. }
  618. if (view.pixmap) {
  619. XFreePixmap(dpy, view.pixmap);
  620. if (view.mask)
  621. XFreePixmap(dpy, view.mask);
  622. XFreeColors(dpy, colormap,
  623. view.attributes.alloc_pixels,
  624. view.attributes.nalloc_pixels, 0);
  625. XpmFreeAttributes(&view.attributes);
  626. }
  627. exit(i);
  628. }
  629. void
  630. kinput(
  631. Widget widget,
  632. char *tag,
  633. XEvent *xe,
  634. Boolean *b)
  635. {
  636. char c = '\0';
  637. XLookupString(&(xe->xkey), &c, 1, NULL, NULL);
  638. if (c == 'q' || c == 'Q')
  639. Punt(0);
  640. }
  641. /*
  642. * small function to extract various version numbers from the given global
  643. * number (following the rule described in xpm.h).
  644. */
  645. void
  646. GetNumbers(
  647. int num,
  648. int *format_return,
  649. int *libmajor_return,
  650. char *libminor_return)
  651. {
  652. *format_return = num / 10000;
  653. *libmajor_return = (num % 10000) / 100;
  654. *libminor_return = 'a' + (num % 10000) % 100 - 1;
  655. }
  656. void
  657. VersionInfo(void)
  658. {
  659. int format, libmajor;
  660. char libminor;
  661. GetNumbers(XpmIncludeVersion, &format, &libmajor, &libminor);
  662. /* L10N_Comments : sxpm -version output */
  663. fprintf(stderr, gettext("sxpm version: %d.%d%c\n"),
  664. format, libmajor, libminor);
  665. /* L10N_Comments :
  666. * if we are linked to an XPM library different from the one we've been
  667. * compiled with, print its own number too when sxpm -version is called.
  668. */
  669. if (XpmIncludeVersion != XpmLibraryVersion()) {
  670. GetNumbers(XpmLibraryVersion(), &format, &libmajor, &libminor);
  671. fprintf(stderr, gettext("using the XPM library version: %d.%d%c\n"),
  672. format, libmajor, libminor);
  673. }
  674. }