summaryrefslogtreecommitdiffstats
path: root/ghostprobe.c
blob: af71dda365fa9866c3327fd7852d31e293194da4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 1996-1998 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * ghostprobe - attempt to figure out what our version of Ghostscript
 *              can do.
 *
 * This program would have been written in Perl, but I didn't want to make
 * the existence of Perl a condition to build magicfilter.
 *
 */

#include <stdio.h>
#include <string.h>
#include "config.h"

#define WHITESPACE "\b\t\n\v\f\r "

#ifndef HAVE_GHOSTSCRIPT

main()
{
  exit(0);			/* No Ghostscript to probe! */
}

#else /* HAVE_GHOSTSCRIPT */

main(int argc, char *argv[])
{
  FILE *gs;
  char line[2048], *p;
  enum { st_devs, st_lang, st_ignore } state;
  static char *prefix[] = { "GS_DEV_", "GS_LANG_" };
  int lang_info = 0;

  gs = popen(PATH_GHOSTSCRIPT " -help", "r");
  if ( !gs )
    exit(1);

  printf("/*\n"
	 " * Ghostscript configuration summary.  Automatically generated\n"
	 " * by ghostprobe.\n"
	 " */\n"
	 "\n");

  state = st_ignore;		/* Parser state */

  while ( fgets(line, 2048, gs) != NULL )
    {
      if ( strchr(WHITESPACE, line[0]) )
	{
	  switch(state)
	    {
	    case st_ignore:
	      break;		/* Do nothing */
	    case st_devs:
	    case st_lang:
	      p = strtok(line, WHITESPACE);
	      while ( p )
		{
		  printf("#define %s%s 1\n", prefix[state], p);
		  p = strtok(NULL, WHITESPACE);
		}
	      break;
	    }
	}
      else if ( !strcmp("Available devices:\n", line) )
	state = st_devs;
      else if ( !strcmp("Language interpreters:\n", line) )
	{
	  state = st_lang;
	  lang_info = 1;	/* We have a language info section */
	}
      else if ( !strncmp("Input formats: ", line, 15) )
	{
	  state = st_lang;
	  lang_info = 1;
	  /* The rest of the line needs to be interpreted */
	  p = strtok(line+15, WHITESPACE);
	  while ( p )
	    {
	      printf("#define %s%s 1\n", prefix[state], p);
	      p = strtok(NULL, WHITESPACE);
	    }
	}
      else
	state = st_ignore;
    }

  pclose(gs);

  if ( !lang_info )
    {
      /* Older versions of Ghostscript lacked the Language Interpreters
	 information; just define PostScript without being specific */
      printf("#define GS_LANG_PostScript 1\n");
    }

  exit(0);
}

#endif /* HAVE_GHOSTSCRIPT */