aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_config/pg_config.c
blob: db3f9f1e086449a861029d35bca526643883254f (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*-------------------------------------------------------------------------
 *
 * pg_config.c
 *
 * This program reports various pieces of information about the
 * installed version of PostgreSQL.  Packages that interface to
 * PostgreSQL can use it to configure their build.
 *
 * This is a C implementation of the previous shell script written by
 * Peter Eisentraut <peter_e@gmx.net>, with adjustments made to
 * accomodate the possibility that the installation has been relocated from
 * the place originally configured.
 *
 * author of C translation: Andrew Dunstan	   mailto:andrew@dunslane.net
 *
 * This code is released under the terms of the PostgreSQL License.
 *
 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
 *
 * $PostgreSQL: pgsql/src/bin/pg_config/pg_config.c,v 1.28 2008/03/27 03:57:33 tgl Exp $
 *
 *-------------------------------------------------------------------------
 */

#include "postgres_fe.h"

#include "port.h"

static const char *progname;
static char mypath[MAXPGPATH];


/*
 * This function cleans up the paths for use with either cmd.exe or Msys
 * on Windows. We need them to use filenames without spaces, for which a
 * short filename is the safest equivalent, eg:
 *		C:/Progra~1/
 */
static void
cleanup_path(char *path)
{
#ifdef WIN32
	char	   *ptr;

	/*
	 * GetShortPathName() will fail if the path does not exist, or short names
	 * are disabled on this file system.  In both cases, we just return the
	 * original path.  This is particularly useful for --sysconfdir, which
	 * might not exist.
	 */
	GetShortPathName(path, path, MAXPGPATH - 1);

	/* Replace '\' with '/' */
	for (ptr = path; *ptr; ptr++)
	{
		if (*ptr == '\\')
			*ptr = '/';
	}
#endif
}


/*
 * For each piece of information known to pg_config, we define a subroutine
 * to print it.  This is probably overkill, but it avoids code duplication
 * and accidentally omitting items from the "all" display.
 */

static void
show_bindir(bool all)
{
	char		path[MAXPGPATH];
	char	   *lastsep;

	if (all)
		printf("BINDIR = ");
	/* assume we are located in the bindir */
	strcpy(path, mypath);
	lastsep = strrchr(path, '/');

	if (lastsep)
		*lastsep = '\0';

	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_docdir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("DOCDIR = ");
	get_doc_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_htmldir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("HTMLDIR = ");
	get_html_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_includedir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("INCLUDEDIR = ");
	get_include_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_pkgincludedir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("PKGINCLUDEDIR = ");
	get_pkginclude_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_includedir_server(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("INCLUDEDIR-SERVER = ");
	get_includeserver_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_libdir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("LIBDIR = ");
	get_lib_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_pkglibdir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("PKGLIBDIR = ");
	get_pkglib_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_localedir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("LOCALEDIR = ");
	get_locale_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_mandir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("MANDIR = ");
	get_man_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_sharedir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("SHAREDIR = ");
	get_share_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_sysconfdir(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("SYSCONFDIR = ");
	get_etc_path(mypath, path);
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_pgxs(bool all)
{
	char		path[MAXPGPATH];

	if (all)
		printf("PGXS = ");
	get_pkglib_path(mypath, path);
	strlcat(path, "/pgxs/src/makefiles/pgxs.mk", sizeof(path));
	cleanup_path(path);
	printf("%s\n", path);
}

static void
show_configure(bool all)
{
#ifdef VAL_CONFIGURE
	if (all)
		printf("CONFIGURE = ");
	printf("%s\n", VAL_CONFIGURE);
#else
	if (!all)
	{
		fprintf(stderr, _("not recorded\n"));
		exit(1);
	}
#endif
}

static void
show_cc(bool all)
{
#ifdef VAL_CC
	if (all)
		printf("CC = ");
	printf("%s\n", VAL_CC);
#else
	if (!all)
	{
		fprintf(stderr, _("not recorded\n"));
		exit(1);
	}
#endif
}

static void
show_cppflags(bool all)
{
#ifdef VAL_CPPFLAGS
	if (all)
		printf("CPPFLAGS = ");
	printf("%s\n", VAL_CPPFLAGS);
#else
	if (!all)
	{
		fprintf(stderr, _("not recorded\n"));
		exit(1);
	}
#endif
}

static void
show_cflags(bool all)
{
#ifdef VAL_CFLAGS
	if (all)
		printf("CFLAGS = ");
	printf("%s\n", VAL_CFLAGS);
#else
	if (!all)
	{
		fprintf(stderr, _("not recorded\n"));
		exit(1);
	}
#endif
}

static void
show_cflags_sl(bool all)
{
#ifdef VAL_CFLAGS_SL
	if (all)
		printf("CFLAGS_SL = ");
	printf("%s\n", VAL_CFLAGS_SL);
#else
	if (!all)
	{
		fprintf(stderr, _("not recorded\n"));
		exit(1);
	}
#endif
}

static void
show_ldflags(bool all)
{
#ifdef VAL_LDFLAGS
	if (all)
		printf("LDFLAGS = ");
	printf("%s\n", VAL_LDFLAGS);
#else
	if (!all)
	{
		fprintf(stderr, _("not recorded\n"));
		exit(1);
	}
#endif
}

static void
show_ldflags_sl(bool all)
{
#ifdef VAL_LDFLAGS_SL
	if (all)
		printf("LDFLAGS_SL = ");
	printf("%s\n", VAL_LDFLAGS_SL);
#else
	if (!all)
	{
		fprintf(stderr, _("not recorded\n"));
		exit(1);
	}
#endif
}

static void
show_libs(bool all)
{
#ifdef VAL_LIBS
	if (all)
		printf("LIBS = ");
	printf("%s\n", VAL_LIBS);
#else
	if (!all)
	{
		fprintf(stderr, _("not recorded\n"));
		exit(1);
	}
#endif
}

static void
show_version(bool all)
{
	if (all)
		printf("VERSION = ");
	printf("PostgreSQL " PG_VERSION "\n");
}


/*
 * Table of known information items
 *
 * Be careful to keep this in sync with the help() display.
 */
typedef struct
{
	const char *switchname;
	void		(*show_func) (bool all);
} InfoItem;

static const InfoItem info_items[] = {
	{"--bindir", show_bindir},
	{"--docdir", show_docdir},
	{"--htmldir", show_htmldir},
	{"--includedir", show_includedir},
	{"--pkgincludedir", show_pkgincludedir},
	{"--includedir-server", show_includedir_server},
	{"--libdir", show_libdir},
	{"--pkglibdir", show_pkglibdir},
	{"--localedir", show_localedir},
	{"--mandir", show_mandir},
	{"--sharedir", show_sharedir},
	{"--sysconfdir", show_sysconfdir},
	{"--pgxs", show_pgxs},
	{"--configure", show_configure},
	{"--cc", show_cc},
	{"--cppflags", show_cppflags},
	{"--cflags", show_cflags},
	{"--cflags_sl", show_cflags_sl},
	{"--ldflags", show_ldflags},
	{"--ldflags_sl", show_ldflags_sl},
	{"--libs", show_libs},
	{"--version", show_version},
	{NULL, NULL}
};


static void
help(void)
{
	printf(_("\n%s provides information about the installed version of PostgreSQL.\n\n"), progname);
	printf(_("Usage:\n"));
	printf(_("  %s [ OPTION ... ]\n\n"), progname);
	printf(_("Options:\n"));
	printf(_("  --bindir              show location of user executables\n"));
	printf(_("  --docdir              show location of documentation files\n"));
	printf(_("  --htmldir             show location of HTML documentation files\n"));
	printf(_("  --includedir          show location of C header files of the client\n"
			 "                        interfaces\n"));
	printf(_("  --pkgincludedir       show location of other C header files\n"));
	printf(_("  --includedir-server   show location of C header files for the server\n"));
	printf(_("  --libdir              show location of object code libraries\n"));
	printf(_("  --pkglibdir           show location of dynamically loadable modules\n"));
	printf(_("  --localedir           show location of locale support files\n"));
	printf(_("  --mandir              show location of manual pages\n"));
	printf(_("  --sharedir            show location of architecture-independent support files\n"));
	printf(_("  --sysconfdir          show location of system-wide configuration files\n"));
	printf(_("  --pgxs                show location of extension makefile\n"));
	printf(_("  --configure           show options given to \"configure\" script when\n"
			 "                        PostgreSQL was built\n"));
	printf(_("  --cc                  show CC value used when PostgreSQL was built\n"));
	printf(_("  --cppflags            show CPPFLAGS value used when PostgreSQL was built\n"));
	printf(_("  --cflags              show CFLAGS value used when PostgreSQL was built\n"));
	printf(_("  --cflags_sl           show CFLAGS_SL value used when PostgreSQL was built\n"));
	printf(_("  --ldflags             show LDFLAGS value used when PostgreSQL was built\n"));
	printf(_("  --ldflags_sl          show LDFLAGS_SL value used when PostgreSQL was built\n"));
	printf(_("  --libs                show LIBS value used when PostgreSQL was built\n"));
	printf(_("  --version             show the PostgreSQL version\n"));
	printf(_("  --help                show this help, then exit\n"));
	printf(_("\nWith no arguments, all known items are shown.\n\n"));
	printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
}

static void
advice(void)
{
	fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
}

static void
show_all(void)
{
	int			i;

	for (i = 0; info_items[i].switchname != NULL; i++)
	{
		(*info_items[i].show_func) (true);
	}
}

int
main(int argc, char **argv)
{
	int			i;
	int			j;
	int			ret;

	set_pglocale_pgservice(argv[0], "pg_config");

	progname = get_progname(argv[0]);

	/* check for --help */
	for (i = 1; i < argc; i++)
	{
		if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-?") == 0)
		{
			help();
			exit(0);
		}
	}

	ret = find_my_exec(argv[0], mypath);

	if (ret)
	{
		fprintf(stderr, _("%s: could not find own program executable\n"), progname);
		exit(1);
	}

	/* no arguments -> print everything */
	if (argc < 2)
	{
		show_all();
		exit(0);
	}

	for (i = 1; i < argc; i++)
	{
		for (j = 0; info_items[j].switchname != NULL; j++)
		{
			if (strcmp(argv[i], info_items[j].switchname) == 0)
			{
				(*info_items[j].show_func) (false);
				break;
			}
		}
		if (info_items[j].switchname == NULL)
		{
			fprintf(stderr, _("%s: invalid argument: %s\n"),
					progname, argv[i]);
			advice();
			exit(1);
		}
	}

	return 0;
}