librazor/test-lua.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Oct 04 18:12:58 2014 +0100 (2014-10-04)
changeset 454 56ff755c268c
parent 378 ee438b1e94c0
child 455 df914f383f5c
permissions -rw-r--r--
Only export symbols starting with razor_ in dynamic library.

Apart from being good practice to avoid clashes with higher-level
libraries and the application, this also fixes an obscure bug: The
gnulib library is used both by librazor (the dynamic library) and
by razor (the executable). In doing so, we want to have two separate
copies of the library despite the code duplication this involves.
Without the explicit limit to export only razor_ symbols, the razor
executable under mingw64 was picking up the getopt_long function
from librazor and the optind variable from libgnu which meant that
it did not see optind changing. Hiding librazor's copy of getopt
causes the linker to find libgnu's copy and everything works.

Note that under mingw librazor-#.dll still contains undocumented
(private) razor_ symbols but these will do no harm as long as nobody
tries to use them.
ali@352
     1
/*
ali@352
     2
 * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
ali@352
     3
 *
ali@352
     4
 * This program is free software; you can redistribute it and/or modify
ali@352
     5
 * it under the terms of the GNU General Public License as published by
ali@352
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@352
     7
 * (at your option) any later version.
ali@352
     8
 *
ali@352
     9
 * This program is distributed in the hope that it will be useful,
ali@352
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@352
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@352
    12
 * GNU General Public License for more details.
ali@352
    13
 *
ali@352
    14
 * You should have received a copy of the GNU General Public License along
ali@352
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@352
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@352
    17
 */
ali@352
    18
ali@352
    19
#include "config.h"
ali@352
    20
ali@352
    21
#include <stdlib.h>
ali@352
    22
#include <stdio.h>
ali@352
    23
#include <string.h>
ali@378
    24
#include <errno.h>
ali@352
    25
#include <sys/types.h>
ali@352
    26
#include <sys/stat.h>
ali@352
    27
#include <dirent.h>
ali@352
    28
#include <unistd.h>
ali@352
    29
#include <math.h>
ali@352
    30
#include "razor-internal.h"
ali@352
    31
ali@352
    32
/*
ali@352
    33
 * This kludge is to work around an apparent bug in Fedora 10's lua package
ali@352
    34
 * (it appears to require libm but not to include a DT_NEEDED).
ali@352
    35
 */
ali@352
    36
ali@352
    37
void (*kludge)() = (void (*)())sin;
ali@352
    38
ali@352
    39
static void recursive_remove(const char *directory)
ali@352
    40
{
ali@352
    41
	DIR *dp;
ali@352
    42
	struct dirent *dirp;
ali@352
    43
	char *buf;
ali@352
    44
ali@352
    45
	dp = opendir(directory);
ali@352
    46
	while((dirp = readdir(dp))) {
ali@352
    47
		if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
ali@352
    48
			buf = malloc(strlen(directory) + strlen(dirp->d_name)
ali@352
    49
				     + 2);
ali@352
    50
			sprintf(buf, "%s/%s", directory, dirp->d_name);
ali@352
    51
			if (remove(buf) < 0)
ali@352
    52
				recursive_remove(buf);
ali@352
    53
			free(buf);
ali@352
    54
		}
ali@352
    55
	}
ali@352
    56
ali@352
    57
	rmdir(directory);
ali@352
    58
}
ali@352
    59
ali@352
    60
int main(int argc, char *argv[])
ali@352
    61
{
ali@352
    62
	char root[] = "/tmp/razor.XXXXXX";
ali@352
    63
	int r;
ali@352
    64
	void *script;
ali@352
    65
	size_t len;
ali@378
    66
	char *s, *test_file, *srcdir;
ali@352
    67
	FILE *fp;
ali@424
    68
	struct razor_error *error = NULL;
ali@352
    69
ali@352
    70
	if (argc > 2) {
ali@352
    71
		fprintf(stderr, "usage: %s [TESTS-FILE]\n", argv[0]);
ali@352
    72
		exit(1);
ali@352
    73
	}
ali@352
    74
	if (argc == 2)
ali@352
    75
		test_file = argv[1];
ali@352
    76
	else
ali@352
    77
		test_file = "test.lua";
ali@352
    78
ali@352
    79
	if (!mkdtemp(root)) {
ali@352
    80
		perror(root);
ali@352
    81
		exit(1);
ali@352
    82
	}
ali@352
    83
ali@352
    84
	s = malloc(strlen(root) + strlen("/testfile") + 1);
ali@352
    85
	strcpy(s, root);
ali@352
    86
	strcat(s, "/testfile");
ali@352
    87
	fp = fopen(s, "w");
ali@352
    88
	if (!fp) {
ali@352
    89
		perror(s);
ali@352
    90
		exit(1);
ali@352
    91
	}
ali@352
    92
	fprintf(fp, "#!" LUA_BINARY "\n"
ali@352
    93
	  "print('Abracadabra!')\n");
ali@352
    94
	fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IXUSR);
ali@352
    95
	fclose(fp);
ali@352
    96
	free(s);
ali@352
    97
ali@424
    98
	script = razor_file_get_contents(test_file, &len, 0, &error);
ali@378
    99
	if (!script) {
ali@378
   100
		srcdir = getenv("srcdir");
ali@378
   101
		if (srcdir && errno == ENOENT && *test_file != '/') {
ali@424
   102
			razor_error_free(error);
ali@378
   103
			s = malloc(strlen(srcdir) + strlen(test_file) + 2);
ali@378
   104
			strcpy(s, srcdir);
ali@378
   105
			strcat(s, "/");
ali@378
   106
			strcat(s, test_file);
ali@424
   107
			script = razor_file_get_contents(s, &len, 0, &error);
ali@378
   108
			if (!script) {
ali@424
   109
				fprintf(stderr, "%s\n",
ali@424
   110
					razor_error_get_msg(error));
ali@424
   111
				razor_error_free(error);
ali@378
   112
				exit(1);
ali@378
   113
			}
ali@378
   114
			free(s);
ali@378
   115
		} else {
ali@424
   116
			fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
   117
			razor_error_free(error);
ali@378
   118
			exit(1);
ali@378
   119
		}
ali@378
   120
	}
ali@376
   121
	r = run_lua_script(root, test_file, script, len, -1);
ali@352
   122
	razor_file_free_contents(script, len);
ali@352
   123
ali@352
   124
	recursive_remove(root);
ali@352
   125
	exit(r ? 1 : 0);
ali@352
   126
}