home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / x / volume20 / imagemgc / part05 < prev    next >
Encoding:
Text File  |  1993-07-13  |  50.3 KB  |  1,473 lines

  1. Newsgroups: comp.sources.x
  2. From: cristy@eplrx7.es.duPont.com (Cristy)
  3. Subject: v20i061:  imagemagic - X11 image processing and display, Part05/38
  4. Message-ID: <1993Jul14.175334.852@sparky.sterling.com>
  5. X-Md4-Signature: e6eb6c345341cf17e3845291820e9536
  6. Sender: chris@sparky.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 17:53:34 GMT
  9. Approved: chris@sterling.com
  10.  
  11. Submitted-by: cristy@eplrx7.es.duPont.com (Cristy)
  12. Posting-number: Volume 20, Issue 61
  13. Archive-name: imagemagic/part05
  14. Environment: X11
  15. Supersedes: imagemagic: Volume 13, Issue 17-37
  16.  
  17. #!/bin/sh
  18. # this is magick.05 (part 5 of ImageMagick)
  19. # do not concatenate these parts, unpack them in order with /bin/sh
  20. # file ImageMagick/utilities/segment.c continued
  21. #
  22. if test ! -r _shar_seq_.tmp; then
  23.     echo 'Please unpack part 1 first!'
  24.     exit 1
  25. fi
  26. (read Scheck
  27.  if test "$Scheck" != 5; then
  28.     echo Please unpack part "$Scheck" next!
  29.     exit 1
  30.  else
  31.     exit 0
  32.  fi
  33. ) < _shar_seq_.tmp || exit 1
  34. if test ! -f _shar_wnt_.tmp; then
  35.     echo 'x - still skipping ImageMagick/utilities/segment.c'
  36. else
  37. echo 'x - continuing file ImageMagick/utilities/segment.c'
  38. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/utilities/segment.c' &&
  39. X          Error("unrecognized option",option);
  40. X          break;
  41. X        }
  42. X        case 'f':
  43. X        {
  44. X          font=(char *) NULL;
  45. X          if (*option == '-')
  46. X            {
  47. X              i++;
  48. X              if (i == argc)
  49. X                Error("missing font name on -font",(char *) NULL);
  50. X              font=argv[i];
  51. X            }
  52. X          break;
  53. X        }
  54. X        case 'g':
  55. X        {
  56. X          image_geometry=(char *) NULL;
  57. X          if (*option == '-')
  58. X            {
  59. X              i++;
  60. X              if (i == argc)
  61. X                Error("missing geometry on -geometry",(char *) NULL);
  62. X              image_geometry=argv[i];
  63. X            }
  64. X          break;
  65. X        }
  66. X        case 'h':
  67. X        {
  68. X          Usage();
  69. X          break;
  70. X        }
  71. X        case 'i':
  72. X        {
  73. X          if (strncmp("interlace",option+1,3) == 0)
  74. X            {
  75. X              interlace=NoneInterlace;
  76. X              if (*option == '-')
  77. X                {
  78. X                  i++;
  79. X                  if (i == argc)
  80. X                    Error("missing type on -interlace",(char *) NULL);
  81. X                  option=argv[i];
  82. X                  interlace=UndefinedInterlace;
  83. X                  if (Latin1Compare("none",option) == 0)
  84. X                    interlace=NoneInterlace;
  85. X                  if (Latin1Compare("line",option) == 0)
  86. X                    interlace=LineInterlace;
  87. X                  if (Latin1Compare("plane",option) == 0)
  88. X                    interlace=PlaneInterlace;
  89. X                  if (interlace == UndefinedInterlace)
  90. X                    Error("invalid interlace type on -interlace",option);
  91. X                }
  92. X              break;
  93. X            }
  94. X          Error("unrecognized option",option);
  95. X        }
  96. X        case 'p':
  97. X        {
  98. X          if (strncmp("page",option+1,2) == 0)
  99. X            {
  100. X              page_geometry=(char *) NULL;
  101. X              if (*option == '-')
  102. X                {
  103. X                  i++;
  104. X                  if (i == argc)
  105. X                    Error("missing page geometry on -page",(char *) NULL);
  106. X                  page_geometry=argv[i];
  107. X                }
  108. X              break;
  109. X            }
  110. X          Error("unrecognized option",option);
  111. X          break;
  112. X        }
  113. X        case 'q':
  114. X        {
  115. X          i++;
  116. X          if ((i == argc) || !sscanf(argv[i],"%d",&x))
  117. X            Error("missing quality on -quality",(char *) NULL);
  118. X          quality=atoi(argv[i]);
  119. X          break;
  120. X        }
  121. X        case 's':
  122. X        {
  123. X          scene=0;
  124. X          if (*option == '-')
  125. X            {
  126. X              i++;
  127. X              if ((i == argc) || !sscanf(argv[i],"%d",&x))
  128. X                Error("missing scene number on -scene",(char *) NULL);
  129. X              scene=atoi(argv[i]);
  130. X            }
  131. X          break;
  132. X        }
  133. X        case 'v':
  134. X        {
  135. X          verbose=(*option == '-');
  136. X          break;
  137. X        }
  138. X        default:
  139. X        {
  140. X          Error("unrecognized option",option);
  141. X          break;
  142. X        }
  143. X      }
  144. X  }
  145. X  if (image == (Image *) NULL)
  146. X    Error("missing an image file name",(char *) NULL);
  147. X  /*
  148. X    Write images.
  149. X  */
  150. X  do
  151. X  {
  152. X    total_colors=0;
  153. X    if (alpha != NotInitialized)
  154. X      image->alpha=alpha;
  155. X    if (compression != UndefinedCompression)
  156. X      image->compression=compression;
  157. X    if (scene != 0)
  158. X      image->scene=scene;
  159. X    (void) strcpy(image->filename,argv[i]);
  160. X    if (image->previous != (Image *) NULL)
  161. X      (void) sprintf(image->filename,"%s.%u",argv[i],image->scene);
  162. X    if (colorspace == GRAYColorspace)
  163. X      QuantizeImage(image,256,8,False,GRAYColorspace,True);
  164. X    /*
  165. X      Reduce the number of colors in the image.
  166. X    */
  167. X    SegmentImage(image,colorspace,verbose);
  168. X    if (verbose)
  169. X      {
  170. X        /*
  171. X          Measure quantization error.
  172. X        */
  173. X        QuantizationError(image,&mean_error_per_pixel,&normalized_mean_error,
  174. X          &normalized_maximum_error);
  175. X        total_colors=NumberColors(image,(FILE *) NULL);
  176. X      }
  177. X    SyncImage(image);
  178. X    status=WriteImage(&image_info,image);
  179. X    if (verbose)
  180. X      {
  181. X        /*
  182. X          Display detailed info about the image.
  183. X        */
  184. X        (void) fprintf(stderr,"[%u] %s=>%s %ux%u",image->scene,filename,
  185. X          image->filename,image->columns,image->rows);
  186. X        if (image->class == DirectClass)
  187. X          (void) fprintf(stderr," DirectClass");
  188. X        else
  189. X          if (total_colors == 0)
  190. X            (void) fprintf(stderr," PseudoClass %uc",image->colors);
  191. X          else
  192. X            {
  193. X              (void) fprintf(stderr," PseudoClass %lu=>%uc",total_colors,
  194. X                image->colors);
  195. X              (void) fprintf(stderr," %u/%.6f/%.6fe",mean_error_per_pixel,
  196. X                normalized_mean_error,normalized_maximum_error);
  197. X            }
  198. X        (void) fprintf(stderr," %s %lds\n",image->magick,
  199. X          time((time_t *) NULL)-start_time+1);
  200. X      }
  201. X    next_image=image->next;
  202. X    DestroyImage(image);
  203. X    image=next_image;
  204. X  } while (image != (Image *) NULL);
  205. X  return(!status);
  206. }
  207. SHAR_EOF
  208. echo 'File ImageMagick/utilities/segment.c is complete' &&
  209. chmod 0644 ImageMagick/utilities/segment.c ||
  210. echo 'restore of ImageMagick/utilities/segment.c failed'
  211. Wc_c="`wc -c < 'ImageMagick/utilities/segment.c'`"
  212. test 59822 -eq "$Wc_c" ||
  213.     echo 'ImageMagick/utilities/segment.c: original size 59822, current size' "$Wc_c"
  214. rm -f _shar_wnt_.tmp
  215. fi
  216. # ============= ImageMagick/utilities/segment.man ==============
  217. if test -f 'ImageMagick/utilities/segment.man' -a X"$1" != X"-c"; then
  218.     echo 'x - skipping ImageMagick/utilities/segment.man (File already exists)'
  219.     rm -f _shar_wnt_.tmp
  220. else
  221. > _shar_wnt_.tmp
  222. echo 'x - extracting ImageMagick/utilities/segment.man (Text)'
  223. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/utilities/segment.man' &&
  224. .ad l
  225. .nh
  226. .TH segment 1 "10 April 1993" "ImageMagick"
  227. .SH NAME
  228. segment - segments an image with thresholding and the fuzzy c-Means
  229. technique.
  230. .SH SYNOPSIS
  231. .B "segment"
  232. [ \fIoptions\fP ... ] \fIinput_file\fP \fIoutput_file\fP
  233. .SH DESCRIPTION
  234. \fBsegment\fP segments an image by analyzing the histograms of the color
  235. components and identifying units that are homogeneous with the fuzzy c-means
  236. technique.  The scale-space filter analyzes the histograms of the three
  237. color components of the image and identifies a set of classes.  The
  238. extents of each class is used to coarsely segment the image with
  239. thresholding.  The color associated with each class is determined by
  240. the mean color of all pixels within the extents of a particular class.
  241. Finally, any unclassified pixels are assigned to the closest class with
  242. the fuzzy c-means technique.
  243. .PP
  244. The fuzzy c-Means algorithm can be summarized as follows:
  245. .RS
  246. .LP
  247. o Build a histogram, one for each color component of the image.
  248. .LP
  249. o For each histogram, successively apply the scale-space filter and
  250. build an interval tree of zero crossings in the second derivative at
  251. each scale.  Analyze this scale-space ``fingerprint'' to determine
  252. which peaks or valleys in the histogram are most predominant.
  253. .LP
  254. o The fingerprint defines intervals on the axis of the histogram.  Each
  255. interval contains either a minima or a maxima in the original signal.
  256. If each color component lies within the maxima interval, that pixel is
  257. considered ``classified'' and is assigned an unique class number.
  258. .LP
  259. o Any pixel that fails to be classified in the above thresholding pass is
  260. classified using the fuzzy c-Means technique.  It is assigned to one
  261. of the classes discovered in the histogram analysis phase.
  262. .RE
  263. .PP
  264. The fuzzy c-Means technique attempts to cluster a pixel by finding the local
  265. minima of the generalized within group sum of squared error objective
  266. function.  A pixel is assigned to the closest class of which the fuzzy
  267. membership has a maximum value.
  268. X
  269. For additional information see
  270. .IP
  271. Young Won Lim, Sang Uk Lee, "On The Color Image Segmentation Algorithm Based
  272. on the Thresholding and the Fuzzy c-Means Techniques", Pattern Recognition,
  273. Volume 23, Number 9, pages 935-952, 1990.
  274. .SH EXAMPLES
  275. .PP
  276. To segment an image of a cockatoo, use:
  277. .PP
  278. .B
  279. X     segment cockatoo.miff cockatoo.segmented
  280. .PP
  281. To segment a raw grayscale image, use:
  282. .PP
  283. .B
  284. X     segment -geometry 768x512 gray:raw image.miff
  285. .SH OPTIONS
  286. .TP 5
  287. .B "-alpha"
  288. store alpha channel if the image has one.
  289. .TP 5
  290. .B "-colorspace \fIvalue\fP"
  291. the type of colorspace: \fBGRAY\fP, \fBRGB\fP, \fBXYZ\fP, \fBYCbCr\fP,
  292. \fBYIQ\fP, or \fBYUV\fP.
  293. X
  294. Color reduction, by default, takes place in the RGB color space.
  295. Empirical evidence suggests that distances in color spaces such as YUV
  296. or YIQ correspond to perceptual color differences more closely
  297. than do distances in RGB space.  These color spaces may give better
  298. results when color reducing an image.  Refer to \fBquantize(9)\fP for
  299. more details.
  300. X
  301. The \fB-colors\fP or \fB-monochrome\fP option is required for this option
  302. to take effect.
  303. .TP 5
  304. .B "-compress \fItype\fP"
  305. the type of image compression: \fIQEncoded\fP or \fIRunlengthEncoded\fP.
  306. X
  307. Specify \fB\+compress\fP to store the binary image in an uncompressed format.
  308. The default is the compression type of the specified image file.
  309. .TP 5
  310. .B "-density \fI<width>x<height>
  311. vertical and horizonal density of the image.
  312. X
  313. This option specifies an image density whose interpretation changes
  314. with the type of image.  The default is 72 dots per inch in the
  315. horizonal and vertical direction for Postscript.  Text files default to
  316. 80 characters in width and 60 lines in height.  Use this option to
  317. alter the default density.
  318. .TP 5
  319. .B "-display \fIhost:display[.screen]\fP"
  320. specifies the X server to contact; see \fBX(1)\fP.
  321. .TP 5
  322. .B "-font \fIname\fP"
  323. This option specifies the font to be used  for displaying normal text.
  324. The default is \fBfixed\fP.
  325. .TP 5
  326. .B "-geometry \fI<width>x<height>\fP"
  327. the width and height of the image.  
  328. X
  329. Use this option to specified the width and height of raw images whose
  330. dimensions are unknown such as \fBGRAY\fP, \fBRGB\fP, and \fBCMYK\fP.
  331. This option can also change the default 8.5 by 11 width and height of
  332. the Postscript canvas.
  333. .TP 5
  334. .B "-interlace \fItype\fP"
  335. the type of interlacing scheme: \fBNONE\fP, \fBLINE\fP, or \fBPLANE\fP.
  336. X
  337. This option is used to specify the type of interlacing scheme for raw
  338. image formats such as \fBRGB\fP or \fBYUV\fP.  \fBNONE\fP means do not
  339. interlace (RGBRGBRGBRGBRGBRGB...), \fBLINE\fP uses scanline
  340. interlacing (RRR...GGG...BBB...RRR...GGG...BBB...), and \fBPLANE\fP uses
  341. plane interlacing (RRRRRR...GGGGGG...BBBBBB...).
  342. .TP 5
  343. .B "-page \fI<width>x<height>{\+-}<x offset>{\+-}<y offset>\fP"
  344. preferred size and location of the Postscript page.
  345. X
  346. Use this option to specify the dimensions of the Postscript page in picas.  The
  347. default is to center the image on a letter page, 612 by 792 picas.
  348. Other common sizes are:
  349. X
  350. X    540x720   Note
  351. X    612x1008  Legal
  352. X    842x1190  A3
  353. X    595x842   A4
  354. X    421x595   A5
  355. X    297x421   A6
  356. X    709x1002  B4
  357. X    612x936   U.S. Foolscap
  358. X    612x936   European Foolscap
  359. X    396x612   Half Letter
  360. X    792x1224  11x17
  361. X    1224x792  Ledger
  362. X
  363. The page geometry is relative to the vertical and horizonal density of the
  364. Postscript page.  See \fB-density\fP for details.
  365. .TP 5
  366. .B "-quality \fIvalue\fP"
  367. JPEG quality setting.
  368. X
  369. Quality is 0 (worst) to 100 (best). The default is 75.
  370. .TP 5
  371. .B "-scene \fIvalue\fP"
  372. image scene number.
  373. .TP 5
  374. .B -verbose
  375. print detailed information about the image.
  376. X
  377. This information is printed: image scene number;  image name;  converted
  378. image name;  image size;  the image class (\fIDirectClass\fP or 
  379. \fIPseudoClass\fP);  the total number of unique colors;  and the number
  380. of seconds to read and convert the image.
  381. .PP
  382. Change '-' to '+' in any option above to reverse its effect.  For
  383. example, specify +alpha to store the image without its alpha channel.
  384. .PP
  385. By default, the image format is determined by its magic number. To
  386. specify a particular image format, precede the filename with an image
  387. format name and a colon (i.e. ps:image) or specify the image type as
  388. the filename suffix (i.e. image.ps).  See \fBconvert(1)\fP for a list
  389. of valid image formats.
  390. .PP
  391. When you specify \fBX\fP as your image type, the filename has special
  392. meaning.  It specifies an X window by id, name, or \fBroot\fP.  If no
  393. filename is specified, the window is selected by clicking the mouse in
  394. the desired window.
  395. .PP
  396. Specify \fIinput_file\fP as \fI-\fP for standard input,
  397. \fIoutput_file\fP as \fI-\fP for standard output.  If \fIinput_file\fP
  398. has the extension \fB.Z\fP or \fB.gz\fP, the file is uncompressed with
  399. \fBuncompress\fP or \fBgunzip\fP respectively.  If \fIoutput_file\fP
  400. has the extension \fB.Z\fP or \fB.gz\fP, the file size is compressed
  401. using with \fBcompress\fP or \fBgzip\fP respectively.  If
  402. \fIoutput_file\fP already exists, you will be prompted as to whether it
  403. should be overwritten.
  404. .SH ENVIRONMENT
  405. .PP
  406. .TP 5
  407. .B display
  408. To get the default host, display number, and screen.
  409. .SH SEE ALSO
  410. .B
  411. display(1), animate(1), mogrify(1), quantize(9), X(1), miff(5)
  412. .SH COPYRIGHT
  413. Copyright 1993 E. I. du Pont de Nemours & Company
  414. .PP
  415. Permission to use, copy, modify, distribute, and sell this software and
  416. its documentation for any purpose is hereby granted without fee,
  417. provided that the above copyright notice appear in all copies and that
  418. both that copyright notice and this permission notice appear in
  419. supporting documentation, and that the name of E. I. du Pont de Nemours
  420. & Company not be used in advertising or publicity pertaining to
  421. distribution of the software without specific, written prior
  422. permission.  E. I. du Pont de Nemours & Company makes no representations
  423. about the suitability of this software for any purpose.  It is provided
  424. "as is" without express or implied warranty.
  425. .PP
  426. E. I. du Pont de Nemours & Company disclaims all warranties with regard
  427. to this software, including all implied warranties of merchantability
  428. and fitness, in no event shall E. I. du Pont de Nemours & Company be
  429. liable for any special, indirect or consequential damages or any
  430. damages whatsoever resulting from loss of use, data or profits, whether
  431. in an action of contract, negligence or other tortious action, arising
  432. out of or in connection with the use or performance of this software.
  433. .SH ACKNOWLEDGEMENTS
  434. Andy Gallo, University of Delaware.  This software is strongly based on
  435. software Andy wrote.
  436. .SH AUTHORS
  437. John Cristy, E.I. du Pont De Nemours & Company Incorporated
  438. SHAR_EOF
  439. chmod 0644 ImageMagick/utilities/segment.man ||
  440. echo 'restore of ImageMagick/utilities/segment.man failed'
  441. Wc_c="`wc -c < 'ImageMagick/utilities/segment.man'`"
  442. test 8572 -eq "$Wc_c" ||
  443.     echo 'ImageMagick/utilities/segment.man: original size 8572, current size' "$Wc_c"
  444. rm -f _shar_wnt_.tmp
  445. fi
  446. # ============= ImageMagick/xtp/Imakefile ==============
  447. if test ! -d 'ImageMagick/xtp'; then
  448.     echo 'x - creating directory ImageMagick/xtp'
  449.     mkdir 'ImageMagick/xtp'
  450. fi
  451. if test -f 'ImageMagick/xtp/Imakefile' -a X"$1" != X"-c"; then
  452.     echo 'x - skipping ImageMagick/xtp/Imakefile (File already exists)'
  453.     rm -f _shar_wnt_.tmp
  454. else
  455. > _shar_wnt_.tmp
  456. echo 'x - extracting ImageMagick/xtp/Imakefile (Text)'
  457. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/xtp/Imakefile' &&
  458. #
  459. #  Imakefile for xtp.
  460. #
  461. #  Copyright 1992 E. I. du Pont de Nemours & Company
  462. #
  463. #  Permission to use, copy, modify, distribute, and sell this software and
  464. #  its documentation for any purpose is hereby granted without fee,
  465. #  provided that the above Copyright notice appear in all copies and that
  466. #  both that Copyright notice and this permission notice appear in
  467. #  supporting documentation, and that the name of E. I. du Pont de Nemours
  468. #  & Company not be used in advertising or publicity pertaining to
  469. #  distribution of the software without specific, written prior
  470. #  permission.  E. I. du Pont de Nemours & Company makes no representations
  471. #  about the suitability of this software for any purpose.  It is provided
  472. #  "as is" without express or implied warranty.
  473. #
  474. #  E. I. du Pont de Nemours & Company disclaims all warranties with regard
  475. #  to this software, including all implied warranties of merchantability
  476. #  and fitness, in no event shall E. I. du Pont de Nemours & Company be
  477. #  liable for any special, indirect or consequential damages or any
  478. #  damages whatsoever resulting from loss of use, data or profits, whether
  479. #  in an action of contract, negligence or other tortious action, arising
  480. #  out of or in connection with the use or performance of this software.
  481. #
  482. X
  483. #include "../Magick.tmpl"
  484. X
  485. XXTPObjects= xtp.o network.o regular.o
  486. X
  487. PROGRAMS= xtp
  488. X
  489. AllTarget($(PROGRAMS))
  490. X
  491. NormalProgramTarget(xtp,$(XTPObjects), , , )
  492. InstallProgram(xtp,$(LOCALDIR))
  493. InstallManPage(xtp,$(MANDIR))
  494. DependTarget()
  495. SHAR_EOF
  496. chmod 0644 ImageMagick/xtp/Imakefile ||
  497. echo 'restore of ImageMagick/xtp/Imakefile failed'
  498. Wc_c="`wc -c < 'ImageMagick/xtp/Imakefile'`"
  499. test 1512 -eq "$Wc_c" ||
  500.     echo 'ImageMagick/xtp/Imakefile: original size 1512, current size' "$Wc_c"
  501. rm -f _shar_wnt_.tmp
  502. fi
  503. # ============= ImageMagick/xtp/Makefile ==============
  504. if test -f 'ImageMagick/xtp/Makefile' -a X"$1" != X"-c"; then
  505.     echo 'x - skipping ImageMagick/xtp/Makefile (File already exists)'
  506.     rm -f _shar_wnt_.tmp
  507. else
  508. > _shar_wnt_.tmp
  509. echo 'x - extracting ImageMagick/xtp/Makefile (Text)'
  510. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/xtp/Makefile' &&
  511. #
  512. #  Generic makefile for xtp for computers that do not have xmkmf.
  513. #
  514. #  Copyright 1992 E. I. du Pont de Nemours & Company
  515. #
  516. #  Permission to use, copy, modify, distribute, and sell this software and
  517. #  its documentation for any purpose is hereby granted without fee,
  518. #  provided that the above Copyright notice appear in all copies and that
  519. #  both that Copyright notice and this permission notice appear in
  520. #  supporting documentation, and that the name of E. I. du Pont de Nemours
  521. #  & Company not be used in advertising or publicity pertaining to
  522. #  distribution of the software without specific, written prior
  523. #  permission.  E. I. du Pont de Nemours & Company makes no representations
  524. #  about the suitability of this software for any purpose.  It is provided
  525. #  "as is" without express or implied warranty.
  526. #
  527. #  E. I. du Pont de Nemours & Company disclaims all warranties with regard
  528. #  to this software, including all implied warranties of merchantability
  529. #  and fitness, in no event shall E. I. du Pont de Nemours & Company be
  530. #  liable for any special, indirect or consequential damages or any
  531. #  damages whatsoever resulting from loss of use, data or profits, whether
  532. #  in an action of contract, negligence or other tortious action, arising
  533. #  out of or in connection with the use or performance of this software.
  534. #
  535. X
  536. CC= cc -O
  537. DESTDIR= /usr/local/bin
  538. INSTALL = install -c
  539. RM= /bin/rm -f
  540. #SYS_LIBRARIES= -lnsl
  541. X
  542. XXTPObjects= xtp.o network.o regular.o
  543. X
  544. PROGRAMS= xtp 
  545. X
  546. all: $(PROGRAMS)
  547. X
  548. xtp:     $(XTPObjects)
  549. X    $(RM) $@
  550. X    $(CC) -o $@ $(XTPObjects) $(SYS_LIBRARIES)
  551. X
  552. clean::
  553. X    $(RM) xtp
  554. X
  555. install:: xtp
  556. X    $(INSTALL) xtp $(DESTDIR)
  557. X
  558. clean::
  559. X    $(RM) *.ln *.bak *.o core errs ,* *~ *.a .emacs_* make.log MakeOut
  560. X
  561. SHAR_EOF
  562. chmod 0644 ImageMagick/xtp/Makefile ||
  563. echo 'restore of ImageMagick/xtp/Makefile failed'
  564. Wc_c="`wc -c < 'ImageMagick/xtp/Makefile'`"
  565. test 1709 -eq "$Wc_c" ||
  566.     echo 'ImageMagick/xtp/Makefile: original size 1709, current size' "$Wc_c"
  567. rm -f _shar_wnt_.tmp
  568. fi
  569. # ============= ImageMagick/xtp/README ==============
  570. if test -f 'ImageMagick/xtp/README' -a X"$1" != X"-c"; then
  571.     echo 'x - skipping ImageMagick/xtp/README (File already exists)'
  572.     rm -f _shar_wnt_.tmp
  573. else
  574. > _shar_wnt_.tmp
  575. echo 'x - extracting ImageMagick/xtp/README (Text)'
  576. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/xtp/README' &&
  577. XXtp is a utility for retrieving, listing, or printing files from a
  578. remote network site.  Xtp performs most of the same functions as the
  579. ftp program, but does not require any interactive commands.  You simply
  580. specify the file transfer task on the command line and xtp performs the
  581. transfer automatically.
  582. X
  583. To retrieve file contrib/ImageMagick.tar.Z from host export.lcs.mit.edu, 
  584. use:
  585. X
  586. X  xtp -binary -retrieve ImageMagick.tar.Z export.lcs.mit.edu contrib
  587. X
  588. XXtp requires 4.3 BSD compatibilities.  I have successfully executed it on
  589. a SUN, DEC Ultrix, MIPS, IBM RS/6000, and Ardent Titan.
  590. X
  591. The author is cristy@dupont.com.  Comments, suggestions, etc, are
  592. welcome, but be kind.
  593. X
  594. ---
  595. X
  596. Copyright 1990 E. I. Dupont de Nemours & Company
  597. X
  598. Permission to use, copy, modify, distribute, and sell this software and
  599. its documentation for any purpose is hereby granted without fee,
  600. provided that the above copyright notice appear in all copies and that
  601. both that copyright notice and this permission notice appear in
  602. supporting documentation, and that the name of E. I. Dupont de Nemours
  603. & Company not be used in advertising or publicity pertaining to
  604. distribution of the software without specific, written prior
  605. permission.  E. I. Dupont de Nemours & Company makes no representations
  606. about the suitability of this software for any purpose.  It is provided
  607. "as is" without express or implied warranty.
  608. X
  609. E. I. Dupont de Nemours & Company disclaims all warranties with regard
  610. to this software, including all implied warranties of merchantability
  611. and fitness, in no event shall E. I. Dupont de Nemours & Company be
  612. liable for any special, indirect or consequential damages or any
  613. damages whatsoever resulting from loss of use, data or profits, whether
  614. in an action of contract, negligence or other tortious action, arising
  615. out of or in connection with the use or performance of this software.
  616. X
  617. SHAR_EOF
  618. chmod 0644 ImageMagick/xtp/README ||
  619. echo 'restore of ImageMagick/xtp/README failed'
  620. Wc_c="`wc -c < 'ImageMagick/xtp/README'`"
  621. test 1876 -eq "$Wc_c" ||
  622.     echo 'ImageMagick/xtp/README: original size 1876, current size' "$Wc_c"
  623. rm -f _shar_wnt_.tmp
  624. fi
  625. # ============= ImageMagick/xtp/get ==============
  626. if test -f 'ImageMagick/xtp/get' -a X"$1" != X"-c"; then
  627.     echo 'x - skipping ImageMagick/xtp/get (File already exists)'
  628.     rm -f _shar_wnt_.tmp
  629. else
  630. > _shar_wnt_.tmp
  631. echo 'x - extracting ImageMagick/xtp/get (Text)'
  632. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/xtp/get' &&
  633. #!/usr/bin/perl
  634. #
  635. # Retrieve files from remote host using standard 'host: filename' string:
  636. #
  637. #   get export.lcs.mit.edu: contrib/ImageMagick.tar.Z
  638. #
  639. $host=shift || die "Usage: get host/ip-address file\n";
  640. $file=shift || die "Usage: get host/ip-address file\n";
  641. $host=~s/:$//;
  642. $directory=`dirname $file`;
  643. chop($directory);
  644. $file=`basename $file`;
  645. chop($file);
  646. printf("Getting %s in directory %s from host %s...\n",$file,$directory,$host);
  647. $pid=fork;
  648. if ($pid == 0)
  649. X  {
  650. X    exec("xtp -r $file -b $host $directory");
  651. X    exit(0);
  652. X  }
  653. exit(0);
  654. SHAR_EOF
  655. chmod 0644 ImageMagick/xtp/get ||
  656. echo 'restore of ImageMagick/xtp/get failed'
  657. Wc_c="`wc -c < 'ImageMagick/xtp/get'`"
  658. test 541 -eq "$Wc_c" ||
  659.     echo 'ImageMagick/xtp/get: original size 541, current size' "$Wc_c"
  660. rm -f _shar_wnt_.tmp
  661. fi
  662. # ============= ImageMagick/xtp/network.c ==============
  663. if test -f 'ImageMagick/xtp/network.c' -a X"$1" != X"-c"; then
  664.     echo 'x - skipping ImageMagick/xtp/network.c (File already exists)'
  665.     rm -f _shar_wnt_.tmp
  666. else
  667. > _shar_wnt_.tmp
  668. echo 'x - extracting ImageMagick/xtp/network.c (Text)'
  669. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/xtp/network.c' &&
  670. /*
  671. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  672. %                                                                             %
  673. %                                                                             %
  674. %                                                                             %
  675. %             N   N  EEEEE  TTTTT  W   W   OOO   RRRR   K   K                 %
  676. %             NN  N  E        T    W   W  O   O  R   R  K  K                  %
  677. %             N N N  EEE      T    W W W  O   O  RRRR   KKK                   %
  678. %             N  NN  E        T    WW WW  O   O  R R    K  K                  %
  679. %             N   N  EEEEE    T    W   W   OOO   R  R   K   K                 %
  680. %                                                                             %
  681. %                                                                             %
  682. %                          Network Routines.                                  %
  683. %                                                                             %
  684. %                                                                             %
  685. %                           Software Design                                   %
  686. %                             John Cristy                                     %
  687. %                             October 1992                                    %
  688. %                                                                             %
  689. %                                                                             %
  690. %  Copyright 1993 E. I. Dupont de Nemours & Company                           %
  691. %                                                                             %
  692. %  Permission to use, copy, modify, distribute, and sell this software and    %
  693. %  its documentation for any purpose is hereby granted without fee,           %
  694. %  provided that the above Copyright notice appear in all copies and that     %
  695. %  both that Copyright notice and this permission notice appear in            %
  696. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  697. %  & Company not be used in advertising or publicity pertaining to            %
  698. %  distribution of the software without specific, written prior               %
  699. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  700. %  about the suitability of this software for any purpose.  It is provided    %
  701. %  "as is" without express or implied warranty.                               %
  702. %                                                                             %
  703. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  704. %  to this software, including all implied warranties of merchantability      %
  705. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  706. %  liable for any special, indirect or consequential damages or any           %
  707. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  708. %  in an action of contract, negligence or other tortious action, arising     %
  709. %  out of or in connection with the use or performance of this software.      %
  710. %                                                                             %
  711. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  712. %
  713. %
  714. */
  715. X
  716. #include "xtp.h"
  717. #include "regular.h"
  718. #include <unistd.h>
  719. #include <sys/socket.h>
  720. #include <netinet/in.h>
  721. #include <netdb.h>
  722. #include <arpa/inet.h>
  723. X
  724. X
  725. /*
  726. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  727. %                                                                             %
  728. %                                                                             %
  729. %                                                                             %
  730. %   G e t H o s t I n f o                                                     %
  731. %                                                                             %
  732. %                                                                             %
  733. %                                                                             %
  734. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  735. %
  736. %  Function GetHostInfo accepts a host name or address, verifies it is valid,
  737. %  and returns both the host name and address from the network host entry.
  738. %
  739. %  The format of the GetHostInfo routine is:
  740. %
  741. %    info=GetHostInfo(name)
  742. %
  743. %  A description of each parameter follows:
  744. %
  745. %    o info:  Function GetHostInfo returns a pointer to the host name and
  746. %      IP address.  A null pointer is returned if there the host cannot be
  747. %      located.
  748. %
  749. %    o name:  Specifies a pointer to a character array that contains either
  750. %      a name of a host or an IP address.
  751. %
  752. %
  753. */
  754. char *GetHostInfo(name)
  755. char
  756. X  *name;
  757. {
  758. X  char
  759. X    info[2048],
  760. X    *p;
  761. X
  762. X  struct in_addr
  763. X    in;
  764. X
  765. X  struct hostent
  766. X    *hp;
  767. X
  768. X  /*
  769. X    Get host name and address.
  770. X  */
  771. X  if (isascii(*name) && isdigit(*name))
  772. X    in.s_addr=inet_addr(name);
  773. X  else
  774. X    {
  775. X      in.s_addr=(unsigned long) -1;
  776. X      hp=gethostbyname(name);
  777. X      if (hp != (struct hostent *) NULL)
  778. X        in.s_addr=(*(int *) hp->h_addr);
  779. X    }
  780. X  hp=gethostbyaddr((char *) &in,sizeof(struct in_addr),AF_INET);
  781. X  if (hp == (struct hostent *) NULL)
  782. X    {
  783. X      hp=gethostbyname(name);
  784. X      if (hp == (struct hostent *) NULL)
  785. X        return((char *) NULL);
  786. X    }
  787. X  /*
  788. X    Convert hostname to lower-case characters.
  789. X  */
  790. X  p=hp->h_name;
  791. X  while (*p)
  792. X  {
  793. X    if (isupper(*p))
  794. X      *p=tolower(*p);
  795. X    p++;
  796. X  }
  797. X  (void) sprintf(info,"%s [%s]: ",hp->h_name,inet_ntoa(in));
  798. X  return(info);
  799. }
  800. SHAR_EOF
  801. chmod 0644 ImageMagick/xtp/network.c ||
  802. echo 'restore of ImageMagick/xtp/network.c failed'
  803. Wc_c="`wc -c < 'ImageMagick/xtp/network.c'`"
  804. test 5583 -eq "$Wc_c" ||
  805.     echo 'ImageMagick/xtp/network.c: original size 5583, current size' "$Wc_c"
  806. rm -f _shar_wnt_.tmp
  807. fi
  808. # ============= ImageMagick/xtp/regular.c ==============
  809. if test -f 'ImageMagick/xtp/regular.c' -a X"$1" != X"-c"; then
  810.     echo 'x - skipping ImageMagick/xtp/regular.c (File already exists)'
  811.     rm -f _shar_wnt_.tmp
  812. else
  813. > _shar_wnt_.tmp
  814. echo 'x - extracting ImageMagick/xtp/regular.c (Text)'
  815. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/xtp/regular.c' &&
  816. /*
  817. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  818. %                                                                             %
  819. %                                                                             %
  820. %                                                                             %
  821. %             RRRR   EEEEE   GGGG  U   U  L       AAA   RRRR                  %
  822. %             R   R  E      G      U   U  L      A   A  R   R                 %
  823. %             RRRR   EEE    G  GG  U   U  L      AAAAA  RRRR                  %
  824. %             R R    E      G   G  U   U  L      A   A  R R                   %
  825. %             R  R   EEEEE   GGGG   UUU   LLLLL  A   A  R  R                  %
  826. %                                                                             %
  827. %                                                                             %
  828. %                     Regular Expression Interpreter.                         %
  829. %                                                                             %
  830. %                                                                             %
  831. %                                                                             %
  832. %                           Software Design                                   %
  833. %                             John Cristy                                     %
  834. %                              July 1992                                      %
  835. %                                                                             %
  836. %                                                                             %
  837. %  Copyright 1993 E. I. Dupont de Nemours & Company                           %
  838. %                                                                             %
  839. %  Permission to use, copy, modify, distribute, and sell this software and    %
  840. %  its documentation for any purpose is hereby granted without fee,           %
  841. %  provided that the above Copyright notice appear in all copies and that     %
  842. %  both that Copyright notice and this permission notice appear in            %
  843. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  844. %  & Company not be used in advertising or publicity pertaining to            %
  845. %  distribution of the software without specific, written prior               %
  846. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  847. %  about the suitability of this software for any purpose.  It is provided    %
  848. %  "as is" without express or implied warranty.                               %
  849. %                                                                             %
  850. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  851. %  to this software, including all implied warranties of merchantability      %
  852. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  853. %  liable for any special, indirect or consequential damages or any           %
  854. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  855. %  in an action of contract, negligence or other tortious action, arising     %
  856. %  out of or in connection with the use or performance of this software.      %
  857. %                                                                             %
  858. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  859. %
  860. %  CompileRegularExpression returns NULL for a failure, where failures are
  861. %  syntax errors, exceeding implementation limits, or applying `+' or `*'
  862. %  to a possibly-null operand.
  863. %
  864. %  This is essentially the same routine written and copywrited by Henry
  865. %  Spencer, University of Toronto.  I made minor programming changes but
  866. %  major variable name changes to improve readability.
  867. %
  868. %  A regular expression is zero or more branches, separated by `|'. It
  869. %  matches anything that matches one of the branches.
  870. %
  871. %  A branch is zero or more pieces, concatenated. It matches a match for
  872. %  the first, followed by a match for the second, etc.
  873. %
  874. %  A piece is an atom possibly followed by `*', `+', or `?'. An atom
  875. %  followed by `*' matches a sequence of 0 or more matches of the atom. An
  876. %  atom followed by `+' matches a sequence of 1 or more matches of the
  877. %  atom. An atom followed by `?' matches a match of the atom, or the null
  878. %  pattern.
  879. %
  880. %  An atom is a regular expression in parentheses (matching a match for
  881. %  the regular expression), a range (see below), `.' (matching any single
  882. %  character), `^' (matching the null pattern at the beginning of the input
  883. %  pattern), `$' (matching the null pattern at the end of the input pattern),
  884. %  a `\' followed by a single character (matching that character), or a
  885. %  single character with no other significance (matching that character).
  886. %
  887. %  A range is a sequence of characters enclosed in `[]'. It normally
  888. %  matches any single character from the sequence. If the sequence begins
  889. %  with `^', it matches any single character not from the rest of the
  890. %  sequence. If two characters in the sequence are separated by `-', this
  891. %  is shorthand for the full list of ASCII characters between them (e.g.
  892. %  `[0-9]' matches any decimal digit). To include a literal `]' in the
  893. %  sequence, make it the first character (following a possible `^').  To
  894. %  include a literal `-', make it the first or last character.
  895. %
  896. %  If a regular expression could match two different parts of the input
  897. %  pattern, it will match the one which begins earliest. If both begin in
  898. %  the same place but match different lengths, or match the same length
  899. %  in different ways, life gets messier, as follows.
  900. %
  901. %  In general, the possibilities in a list of branches are considered in
  902. %  left-to-right order, the possibilities for `*', `+', and `?' are
  903. %  considered longest-first, nested constructs are considered from the
  904. %  outermost in, and concatenated constructs are considered
  905. %  leftmost-first. The match that will be chosen is the one that uses the
  906. %  earliest possibility in the first choice that has to be made. If there
  907. %  is more than one choice, the next will be made in the same manner
  908. %  (earliest possibility) subject to the decision on the first choice.
  909. %  And so forth.
  910. %
  911. %  For example, `(ab|a)b*c' could match `abc' in one of two ways. The
  912. %  first choice is between `ab' and `a'; since `ab' is earlier, and does
  913. %  lead to a successful overall match, it is chosen. Since the `b' is
  914. %  already spoken for, the `b*' must match its last possibility-the empty
  915. %  pattern-since it must respect the earlier choice.
  916. %
  917. %  In the particular case where no `|'s are present and there is only one
  918. %  `*', `+', or `?', the net effect is that the longest possible match
  919. %  will be chosen. So `ab*', presented with `xabbbby', will match `abbbb'.
  920. %  Note that if `ab*' is tried against `xabyabbbz', it will match `ab'
  921. %  just after `x', due to the begins-earliest rule. (In effect, the deci-
  922. %  sion on where to start the match is the first choice to be made, hence
  923. %  subsequent choices must respect it even if this leads them to
  924. %  less-preferred alternatives.)
  925. %
  926. %
  927. */
  928. X
  929. #include "xtp.h"
  930. #include "regular.h"
  931. X
  932. /*
  933. X  Variable declarations.
  934. */
  935. char
  936. X  *code,
  937. X  **subpattern_end,
  938. X  *p,
  939. X  start_code,
  940. X  *start_pattern,
  941. X  **subpattern;
  942. X
  943. static char
  944. X  *token;
  945. X
  946. static int
  947. X  number_parenthesis;
  948. X
  949. static long
  950. X  code_size;
  951. X
  952. /*
  953. X  Forward declarations.
  954. */
  955. static char
  956. X  *NextToken _Declare((register char *)),
  957. X  *Node _Declare((int)),
  958. X  *Piece _Declare((int *)),
  959. X  *Regular _Declare((int,int *));
  960. X
  961. static int
  962. X  Repeat _Declare((char *));
  963. X
  964. static void
  965. X  EmitCode _Declare((int)),
  966. X  Tail _Declare((char *,char *));
  967. X
  968. /*
  969. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  970. %                                                                             %
  971. %                                                                             %
  972. %                                                                             %
  973. %   A t o m                                                                   %
  974. %                                                                             %
  975. %                                                                             %
  976. %                                                                             %
  977. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  978. %
  979. %
  980. */
  981. static char *Atom(flagp)
  982. int
  983. X  *flagp;
  984. {
  985. X  int
  986. X    flags;
  987. X
  988. X  register char
  989. X    *status;
  990. X
  991. X  *flagp=WorstCase;
  992. X  switch(*token++)
  993. X  {
  994. X    case '^':
  995. X    {
  996. X      status=Node(MatchBeginningOfLine);
  997. X      break;
  998. X    }
  999. X    case '$':
  1000. X    {
  1001. X      status=Node(MatchEndOfProgramOfLine);
  1002. X      break;
  1003. X    }
  1004. X    case '.':
  1005. X    {
  1006. X      status=Node(MatchAnyCharacter);
  1007. X      *flagp|=NonNull | Simple;
  1008. X      break;
  1009. X    }
  1010. X    case '[':
  1011. X    {
  1012. X      register int
  1013. X        class,
  1014. X        class_end;
  1015. X
  1016. X      if (*token != '^')
  1017. X        status=Node(MatchAnyCharacterOf);
  1018. X      else
  1019. X        {
  1020. X          /*
  1021. X            Complement of range.
  1022. X          */
  1023. X          status=Node(MatchAnyCharacterBut);
  1024. X          token++;
  1025. X        }
  1026. X      if ((*token == ']') || (*token == '-'))
  1027. X        EmitCode(*token++);
  1028. X      while ((*token != '\0') && (*token != ']'))
  1029. X      {
  1030. X        if (*token != '-')
  1031. X          EmitCode(*token++);
  1032. X         else
  1033. X          {
  1034. X            token++;
  1035. X            if ((*token == ']') || (*token == '\0'))
  1036. X              EmitCode('-');
  1037. X            else
  1038. X              {
  1039. X                class=((int)*(unsigned char *)(token-2))+1;
  1040. X                class_end=((int)*(unsigned char *)(token));
  1041. X                if (class > class_end+1)
  1042. X                  Fail("invalid [] range");
  1043. X                for(; class <= class_end; class++)
  1044. X                  EmitCode((char) class);
  1045. X                token++;
  1046. X              }
  1047. X          }
  1048. X      }
  1049. X      EmitCode('\0');
  1050. X      if (*token != ']')
  1051. X        Fail("unmatched []");
  1052. X      token++;
  1053. X      *flagp|=NonNull | Simple;
  1054. X      break;
  1055. X    }
  1056. X    case '(':
  1057. X    {
  1058. X      status=Regular(1,&flags);
  1059. X      if (status == NULL)
  1060. X        return(NULL);
  1061. X      *flagp|=flags & (NonNull | SpecialStart);
  1062. X      break;
  1063. X    }
  1064. X    case '\0':
  1065. X    case '|':
  1066. X    case ')':
  1067. X    {
  1068. X      Fail("internal urp");
  1069. X      break;
  1070. X    }
  1071. X    case '?':
  1072. X    case '+':
  1073. X    case '*':
  1074. X    {
  1075. X      Fail("?+* follows nothing");
  1076. X      break;
  1077. X    }
  1078. X    case '\\':
  1079. X    {
  1080. X      if (*token == '\0')
  1081. X        Fail("trailing \\");
  1082. X      status=Node(MatchExactly);
  1083. X      EmitCode(*token++);
  1084. X      EmitCode('\0');
  1085. X      *flagp|=NonNull | Simple;
  1086. X      break;
  1087. X    }
  1088. X    default:
  1089. X    {
  1090. X      register char
  1091. X        ender;
  1092. X
  1093. X      register int
  1094. X        length;
  1095. X
  1096. X      token--;
  1097. X      length=strcspn(token,Meta);
  1098. X      if (length <= 0)
  1099. X        Fail("internal disaster");
  1100. X      ender=(*(token+length));
  1101. X      if (length > 1 && MultipleMatches(ender))
  1102. X        length--;
  1103. X      *flagp|=NonNull;
  1104. X      if (length == 1)
  1105. X        *flagp|=Simple;
  1106. X      status=Node(MatchExactly);
  1107. X      while (length > 0)
  1108. X      {
  1109. X        EmitCode(*token++);
  1110. X        length--;
  1111. X      }
  1112. X      EmitCode('\0');
  1113. X      break;
  1114. X    }
  1115. X  }
  1116. X  return(status);
  1117. }
  1118. X
  1119. /*
  1120. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1121. %                                                                             %
  1122. %                                                                             %
  1123. %                                                                             %
  1124. %   B r a n c h                                                               %
  1125. %                                                                             %
  1126. %                                                                             %
  1127. %                                                                             %
  1128. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1129. %
  1130. %  Function Branch Implements the | operator.
  1131. %
  1132. %
  1133. */
  1134. static char *Branch(flagp)
  1135. int
  1136. X  *flagp;
  1137. {
  1138. X  int
  1139. X    flags;
  1140. X
  1141. X  register char
  1142. X    *chain,
  1143. X    *latest,
  1144. X    *status;
  1145. X
  1146. X  *flagp=WorstCase;
  1147. X  status=Node(MatchThisOrNext);
  1148. X  chain=NULL;
  1149. X  while ((*token != '\0') && (*token != '|') && (*token != ')'))
  1150. X  {
  1151. X    latest=Piece(&flags);
  1152. X    if (latest == NULL)
  1153. X      return(NULL);
  1154. X    *flagp|=flags & NonNull;
  1155. X    if (chain == NULL)
  1156. X      *flagp|=flags & SpecialStart;
  1157. X    else
  1158. X      Tail(chain,latest);
  1159. X    chain=latest;
  1160. X  }
  1161. X  if (chain == NULL)
  1162. X   (void) Node(MatchEmptyString);
  1163. X  return(status);
  1164. }
  1165. X
  1166. /*
  1167. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1168. %                                                                             %
  1169. %                                                                             %
  1170. %                                                                             %
  1171. %   E m i t C o d e                                                           %
  1172. %                                                                             %
  1173. %                                                                             %
  1174. %                                                                             %
  1175. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1176. %
  1177. %
  1178. */
  1179. static void EmitCode(opcode)
  1180. int
  1181. X  opcode;
  1182. {
  1183. X  if (code != &start_code)
  1184. X    *code++=(char) opcode;
  1185. X  else
  1186. X    code_size++;
  1187. }
  1188. X
  1189. /*
  1190. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1191. %                                                                             %
  1192. %                                                                             %
  1193. %                                                                             %
  1194. %   I n s e r t                                                               %
  1195. %                                                                             %
  1196. %                                                                             %
  1197. %                                                                             %
  1198. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1199. %
  1200. %  Function Insert inserts an operator in front of an already-emitted operand.
  1201. %
  1202. */
  1203. static void Insert(opcode,operand)
  1204. char
  1205. X  opcode,
  1206. X  *operand;
  1207. {
  1208. X  register char
  1209. X    *p,
  1210. X    *place,
  1211. X    *q;
  1212. X
  1213. X  if (code == &start_code)
  1214. X    {
  1215. X      code_size+=3;
  1216. X      return;
  1217. X    }
  1218. X  p=code;
  1219. X  code+=3;
  1220. X  q=code;
  1221. X  while (p > operand)
  1222. X    *--q=(*--p);
  1223. X  place=operand;
  1224. X  *place++=opcode;
  1225. X  *place++='\0';
  1226. X  *place++='\0';
  1227. }
  1228. X
  1229. /*
  1230. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1231. %                                                                             %
  1232. %                                                                             %
  1233. %                                                                             %
  1234. %   M a t c h                                                                 %
  1235. %                                                                             %
  1236. %                                                                             %
  1237. %                                                                             %
  1238. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1239. %
  1240. %
  1241. */
  1242. static int Match(regular_expression)
  1243. char
  1244. X  *regular_expression;
  1245. {
  1246. X  char
  1247. X    *next_token;
  1248. X
  1249. X  register char
  1250. X    *scan;
  1251. X
  1252. X  scan=regular_expression;
  1253. X  while (scan != NULL)
  1254. X  {
  1255. X    next_token=NextToken(scan);
  1256. X    switch(OpCode(scan))
  1257. X    {
  1258. X      case MatchBeginningOfLine:
  1259. X      {
  1260. X        if (p != start_pattern)
  1261. X          return(0);
  1262. X        break;
  1263. X      }
  1264. X      case MatchEndOfProgramOfLine:
  1265. X      {
  1266. X        if (*p != '\0')
  1267. X         return(0);
  1268. X        break;
  1269. X      }
  1270. X      case MatchAnyCharacter:
  1271. X      {
  1272. X        if (*p == '\0')
  1273. X          return(0);
  1274. X        p++;
  1275. X        break;
  1276. X      }
  1277. X      case MatchExactly:
  1278. X      {
  1279. X        register char
  1280. X          *operand;
  1281. X
  1282. X        register int
  1283. X          length;
  1284. X
  1285. X        operand=Operand(scan);
  1286. X        /*
  1287. X          Inline the first character for speed.
  1288. X        */
  1289. X        if (*operand != *p)
  1290. X          return(0);
  1291. X        length=strlen(operand);
  1292. X        if ((length > 1) && (strncmp(operand,p,length) != 0))
  1293. X          return(0);
  1294. X        p+=length;
  1295. X        break;
  1296. X      }
  1297. X      case MatchAnyCharacterOf:
  1298. X      {
  1299. X        if ((*p == '\0' || strchr(Operand(scan),*p) == NULL))
  1300. X          return(0);
  1301. X        p++;
  1302. X        break;
  1303. X      }
  1304. X      case MatchAnyCharacterBut:
  1305. X      {
  1306. X        if ((*p == '\0') || (strchr(Operand(scan),*p) != NULL))
  1307. X          return(0);
  1308. X        p++;
  1309. X        break;
  1310. X      }
  1311. X      case MatchEmptyString:
  1312. X        break;
  1313. X      case Back:
  1314. X        break;
  1315. X      case Open+1:
  1316. X      case Open+2:
  1317. X      case Open+3:
  1318. X      case Open+4:
  1319. X      case Open+5:
  1320. X      case Open+6:
  1321. X      case Open+7:
  1322. X      case Open+8:
  1323. X      case Open+9:
  1324. X      {
  1325. X        register char
  1326. X          *save;
  1327. X
  1328. X        register int
  1329. X          no;
  1330. X
  1331. X        no=OpCode(scan)-Open;
  1332. X        save=p;
  1333. X        if (!Match(next_token))
  1334. X          return(0);
  1335. X        else
  1336. X          {
  1337. X            /*
  1338. X              Don't set subpattern if some later invocation of the same
  1339. X              parentheses already has.
  1340. X            */
  1341. X            if (subpattern[no] == NULL)
  1342. X              subpattern[no]=save;
  1343. X            return(1);
  1344. X          }
  1345. X        break;
  1346. X      }
  1347. X      case Close+1:
  1348. X      case Close+2:
  1349. X      case Close+3:
  1350. X      case Close+4:
  1351. X      case Close+5:
  1352. X      case Close+6:
  1353. X      case Close+7:
  1354. X      case Close+8:
  1355. X      case Close+9:
  1356. X      {
  1357. X        register char
  1358. X          *save;
  1359. X
  1360. X        register int
  1361. X          no;
  1362. X
  1363. X        no=OpCode(scan)-Close;
  1364. X        save=p;
  1365. X        if (!Match(next_token))
  1366. X           return(0);
  1367. X        else
  1368. X          {
  1369. X            /*
  1370. X              Don't set subpattern_end if some later invocation of the same
  1371. X              parentheses already has.
  1372. X            */
  1373. X            if (subpattern_end[no] == NULL)
  1374. X              subpattern_end[no]=save;
  1375. X            return(1);
  1376. X          }
  1377. X        break;
  1378. X      }
  1379. X      case MatchThisOrNext:
  1380. X      {
  1381. X        register char
  1382. X          *save;
  1383. X
  1384. X        if (OpCode(next_token) != MatchThisOrNext)
  1385. X          next_token=Operand(scan);
  1386. X        else
  1387. X          {
  1388. X            do
  1389. X            {
  1390. X              save=p;
  1391. X              if (Match(Operand(scan)))
  1392. X                return(1);
  1393. X              p=save;
  1394. X              scan=NextToken(scan);
  1395. X            } while ((scan != NULL) && (OpCode(scan) == MatchThisOrNext));
  1396. X            return(0);
  1397. X          }
  1398. X        break;
  1399. X      }
  1400. X      case MatchZeroOrMore:
  1401. X      case MatchOneOrMore:
  1402. X      {
  1403. X        register char
  1404. X          next_tokench,
  1405. X          *save;
  1406. X
  1407. X        register int
  1408. X          min,
  1409. X          no;
  1410. X
  1411. X        /*
  1412. X          Lookahead to avoid useless match attempts when we know what
  1413. X          character comes next_token.
  1414. X        */
  1415. X        next_tokench='\0';
  1416. X        if (OpCode(next_token) == MatchExactly)
  1417. X          next_tokench=(*Operand(next_token));
  1418. X        min=(OpCode(scan) == MatchZeroOrMore) ? 0 : 1;
  1419. X        save=p;
  1420. X        no=Repeat(Operand(scan));
  1421. X        while (no >= min)
  1422. X        {
  1423. X          /*
  1424. X            If it could work, try it.
  1425. X          */
  1426. X          if ((next_tokench == '\0') || (*p == next_tokench))
  1427. X            if (Match(next_token))
  1428. X              return(1);
  1429. X          /*
  1430. X            Couldn't or didn't -- back up.
  1431. X          */
  1432. X          no--;
  1433. X          p=save+no;
  1434. X        }
  1435. X        return(0);
  1436. X        break;
  1437. X      }
  1438. X      case EndOfProgram:
  1439. X        return(1);
  1440. X        break;
  1441. X      default:
  1442. X        (void) fprintf(stderr,"Regular(3): %s","memory corruption");
  1443. X        return(0);
  1444. X        break;
  1445. X    }
  1446. X    scan=next_token;
  1447. X  }
  1448. X  (void) fprintf(stderr,"Regular(3): %s","corrupted pointers");
  1449. X  return(0);
  1450. }
  1451. X
  1452. /*
  1453. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1454. %                                                                             %
  1455. %                                                                             %
  1456. %                                                                             %
  1457. %   N e x t T o k e n                                                         %
  1458. %                                                                             %
  1459. SHAR_EOF
  1460. true || echo 'restore of ImageMagick/xtp/regular.c failed'
  1461. fi
  1462. echo 'End of ImageMagick part 5'
  1463. echo 'File ImageMagick/xtp/regular.c is continued in part 6'
  1464. echo 6 > _shar_seq_.tmp
  1465. exit 0
  1466.  
  1467. exit 0 # Just in case...
  1468. -- 
  1469.   // chris@Sterling.COM           | Send comp.sources.x submissions to:
  1470. \X/  Amiga - The only way to fly! |    sources-x@sterling.com
  1471.  "It's intuitively obvious to the |
  1472.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  1473.