Beispiel #1
0
static void
run_tar(const char *target, const char *pack_options,
    const char *unpack_options, const char *flist)
{
	int r;

	assertMakeDir(target, 0775);

	/* Use the tar program to create an archive. */
	r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
	failure("Error invoking %s cf -", testprog, pack_options);
	assertEqualInt(r, 0);

	assertChdir(target);

	/* Verify that nothing went to stderr. */
	assertEmptyFile("pack.err");

	/*
	 * Use tar to unpack the archive into another directory.
	 */
	r = systemf("%s xf archive %s >unpack.out 2>unpack.err",
	    testprog, unpack_options);
	failure("Error invoking %s xf archive %s", testprog, unpack_options);
	assertEqualInt(r, 0);

	/* Verify that nothing went to stderr. */
	assertEmptyFile("unpack.err");
	assertChdir("..");
}
Beispiel #2
0
static void
basic_tar(const char *target, const char *pack_options,
    const char *unpack_options, const char *flist)
{
	int r;

	assertMakeDir(target, 0775);

	/* Use the tar program to create an archive. */
	r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
	failure("Error invoking %s cf -", testprog, pack_options);
	assertEqualInt(r, 0);

	assertChdir(target);

	/* Verify that nothing went to stderr. */
	assertEmptyFile("pack.err");

	/*
	 * Use tar to unpack the archive into another directory.
	 */
	r = systemf("%s xf archive %s >unpack.out 2>unpack.err", testprog, unpack_options);
	failure("Error invoking %s xf archive %s", testprog, unpack_options);
	assertEqualInt(r, 0);

	/* Verify that nothing went to stderr. */
	assertEmptyFile("unpack.err");

	/*
	 * Verify unpacked files.
	 */

	/* Regular file with 2 links. */
	assertIsReg("file", -1);
	assertFileSize("file", 10);
	failure("%s", target);
	assertFileNLinks("file", 2);

	/* Another name for the same file. */
	assertIsReg("linkfile", -1);
	assertFileSize("linkfile", 10);
	assertFileNLinks("linkfile", 2);
	assertIsHardlink("file", "linkfile");

	/* Symlink */
	if (canSymlink())
		assertIsSymlink("symlink", "file");

	/* dir */
	assertIsDir("dir", 0775);
	assertChdir("..");
}
static void
passthrough(const char *target)
{
	int r;

	if (!assertMakeDir(target, 0775))
		return;

	/*
	 * Use cpio passthrough mode to copy files to another directory.
	 */
	r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr",
	    testprog, target, target, target);
	failure("Error invoking %s -p", testprog);
	assertEqualInt(r, 0);

	assertChdir(target);

	/* Verify stderr. */
	failure("Error invoking %s -p in dir %s",
	    testprog, target);
	assertTextFileContents("1 block\n", "stderr");

	verify_files("passthrough");
	assertChdir("..");
}
Beispiel #4
0
int confinstall()
{
	const char *datadir=0;

	datadir=get_install_datadir();

  if (rhythmbox == True || banshee == True || clementine == True)
	{
    COPY("mv %s/conkyPlayer.template %s/templates/conkyPlayer.template", tempdir(), datadir);
  }

	if (cover > 2)
	{
		COPY("mv %s/conkyCover %s/bin/conkyCover; chmod +x %s/bin/conkyCover", tempdir(), datadir, datadir);
  }

	if(set_photo == 1)
	{
		COPY("cp -i %s/bin/conkyPhoto %s/bin/conkyPhoto; chmod +x %s/bin/conkyPhoto", systemdir(), datadir, datadir);
  }
	else if(set_photo == 2)
  {
    COPY("cp -i %s/bin/conkyPhotoRandom %s/bin/conkyPhotoRandom; chmod +x %s/bin/conkyPhotoRandom", systemdir(), datadir, datadir);
  }

	// finish
	systemf("mv %s/conkyrc %s", tempdir(), localdir());
	printf("Your conkyrc was copied to %s\n", localdir());
	printf("Generated configuration files are copied to %s\n", datadir);
	printf("\nTo run conky-colors and conky type: \nconky -c %s/conkyrc\n", localdir());

	return 0;
}
Beispiel #5
0
static bool driver (config conf) {
    bool fail = false;

    compilerCtx comp;
    compilerInit(&comp, &conf.arch, &conf.includeSearchPaths);

    /*Compile each of the inputs to assembly*/
    for (int i = 0; i < conf.inputs.length; i++) {
        compiler(&comp,
                 vectorGet(&conf.inputs, i),
                 vectorGet(&conf.intermediates, i));
    }

    compilerEnd(&comp);

    if (comp.errors != 0 || comp.warnings != 0)
        printf("Compilation complete with %d error%s and %d warning%s\n",
               comp.errors, plural(comp.errors),
               comp.warnings, plural(comp.warnings));

    else if (internalErrors)
        printf("Compilation complete with %d internal error%s\n",
               internalErrors, plural(internalErrors));

    /*Assemble/link*/
    else if (conf.mode != modeNoAssemble) {
        /*Produce a string list of all the intermediates*/
        char* intermediates = strjoinwith((char**) conf.intermediates.buffer, conf.intermediates.length,
                                          " ", malloc);

        if (conf.mode == modeNoLink)
            fail |= systemf("gcc %s -c %s", conf.arch.asflags, intermediates) != 0;

        else {
            fail |= systemf("gcc %s %s -o %s", conf.arch.ldflags, intermediates, conf.output) != 0;

            if (conf.deleteAsm && !fail)
                systemf("rm %s", intermediates);
        }

        free(intermediates);
    }

    return fail || comp.errors != 0 || internalErrors != 0;
}
Beispiel #6
0
static void
copy_ustar(void)
{
	const char *target = "ustar";
	int r;

	/* NOTE: for proper operation on cygwin-1.5 and windows, the
	 * length of the name of the directory below, "ustar", must be
	 * less than or equal to the lengthe of the name of the original
	 * directory, "original"  This restriction derives from the
	 * extremely limited pathname lengths on those platforms.
	 */
	assertMakeDir(target, 0775);
	assertEqualInt(0, chdir(target));

	/*
	 * Use the tar program to create an archive.
	 */
	r = systemf("%s cf archive --format=ustar -C ../original f d l m s >pack.out 2>pack.err",
	    testprog);
	failure("Error invoking \"%s cf archive --format=ustar\"", testprog);
	assertEqualInt(r, 0);

	/* Verify that nothing went to stdout. */
	assertEmptyFile("pack.out");
	/* Stderr is non-empty, since there are a bunch of files
	 * with filenames too long to archive. */

	/*
	 * Use tar to unpack the archive into another directory.
	 */
	r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
	failure("Error invoking %s xf archive", testprog);
	assertEqualInt(r, 0);

	/* Verify that nothing went to stdout or stderr. */
	assertEmptyFile("unpack.err");
	assertEmptyFile("unpack.out");

	verify_tree(LIMIT_USTAR);
	assertEqualInt(0, chdir("../.."));
}
Beispiel #7
0
/*
 * Unpack the archive in a new dir.
 */
static void
unpack(const char *dirname, const char *option)
{
	int r;

	assertMakeDir(dirname, 0755);
	assertChdir(dirname);
	extract_reference_file("test_option_f.cpio");
	r = systemf("%s -i %s < test_option_f.cpio > copy-no-a.out 2>copy-no-a.err", testprog, option);
	assertEqualInt(0, r);
	assertChdir("..");
}
static void
unpack_test(const char *from, const char *options, const char *se)
{
	int r;

	/* Create a work dir named after the file we're unpacking. */
	assertMakeDir(from, 0775);
	assertChdir(from);

	/*
	 * Use cpio to unpack the sample archive
	 */
	extract_reference_file(from);
	r = systemf("%s -i %s < %s >unpack.out 2>unpack.err",
	    testprog, options, from);
	failure("Error invoking %s -i %s < %s",
	    testprog, options, from);
	assertEqualInt(r, 0);

	/* Verify that nothing went to stderr. */
	if (canSymlink()) {
		failure("Error invoking %s -i %s < %s",
		    testprog, options, from);
		assertTextFileContents(se, "unpack.err");
	}

	/*
	 * Verify unpacked files.
	 */

	/* Regular file with 2 links. */
	assertIsReg("file", 0644);
	failure("%s", from);
	assertFileSize("file", 10);
	assertFileSize("linkfile", 10);
	failure("%s", from);
	assertFileNLinks("file", 2);

	/* Another name for the same file. */
	failure("%s", from);
	assertIsHardlink("linkfile", "file");
	assertFileSize("file", 10);
	assertFileSize("linkfile", 10);

	/* Symlink */
	if (canSymlink())
		assertIsSymlink("symlink", "file");

	/* dir */
	assertIsDir("dir", 0775);

	assertChdir("..");
}
Beispiel #9
0
static void
copy_basic(void)
{
	int r;

	/* NOTE: for proper operation on cygwin-1.5 and windows, the
	 * length of the name of the directory below, "plain", must be
	 * less than or equal to the lengthe of the name of the original
	 * directory, "original"  This restriction derives from the
	 * extremely limited pathname lengths on those platforms.
	 */
	assertMakeDir("plain", 0775);
	assertEqualInt(0, chdir("plain"));

	/*
	 * Use the tar program to create an archive.
	 */
	r = systemf("%s cf archive -C ../original f d l m s >pack.out 2>pack.err",
	    testprog);
	failure("Error invoking \"%s cf\"", testprog);
	assertEqualInt(r, 0);

	/* Verify that nothing went to stdout or stderr. */
	assertEmptyFile("pack.err");
	assertEmptyFile("pack.out");

	/*
	 * Use tar to unpack the archive into another directory.
	 */
	r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
	failure("Error invoking %s xf archive", testprog);
	assertEqualInt(r, 0);

	/* Verify that nothing went to stdout or stderr. */
	assertEmptyFile("unpack.err");
	assertEmptyFile("unpack.out");

	verify_tree(LIMIT_NONE);
	assertEqualInt(0, chdir(".."));
}
static void
basic_cpio(const char *target,
    const char *pack_options,
    const char *unpack_options,
    const char *se, const char *se2)
{
	int r;

	if (!assertMakeDir(target, 0775))
	    return;

	/* Use the cpio program to create an archive. */
	r = systemf("%s -o %s < filelist >%s/archive 2>%s/pack.err",
	    testprog, pack_options, target, target);
	failure("Error invoking %s -o %s", testprog, pack_options);
	assertEqualInt(r, 0);

	assertChdir(target);

	/* Verify stderr. */
	failure("Expected: %s, options=%s", se, pack_options);
	assertTextFileContents(se, "pack.err");

	/*
	 * Use cpio to unpack the archive into another directory.
	 */
	r = systemf("%s -i %s< archive >unpack.out 2>unpack.err",
	    testprog, unpack_options);
	failure("Error invoking %s -i %s", testprog, unpack_options);
	assertEqualInt(r, 0);

	/* Verify stderr. */
	failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);
	assertTextFileContents(se2, "unpack.err");

	verify_files(pack_options);

	assertChdir("..");
}
Beispiel #11
0
/*
 * Each test is run in a private work dir.  Those work dirs
 * do have consistent and predictable names, in case a group
 * of tests need to collaborate.  However, there is no provision
 * for requiring that tests run in a certain order.
 */
static int test_run(int i, const char *tmpdir)
{
	int failures_before = failures;

	if (!quiet_flag) {
		printf("%d: %s\n", i, tests[i].name);
		fflush(stdout);
	}

	/*
	 * Always explicitly chdir() in case the last test moved us to
	 * a strange place.
	 */
	if (chdir(tmpdir)) {
		fprintf(stderr,
		    "ERROR: Couldn't chdir to temp dir %s\n",
		    tmpdir);
		exit(1);
	}
	/* Create a temp directory for this specific test. */
	if (mkdir(tests[i].name, 0755)) {
		fprintf(stderr,
		    "ERROR: Couldn't create temp dir ``%s''\n",
		    tests[i].name);
		exit(1);
	}
	/* Chdir() to that work directory. */
	if (chdir(tests[i].name)) {
		fprintf(stderr,
		    "ERROR: Couldn't chdir to temp dir ``%s''\n",
		    tests[i].name);
		exit(1);
	}
	/* Explicitly reset the locale before each test. */
	setlocale(LC_ALL, "C");
	/* Run the actual test. */
	(*tests[i].func)();
	/* Summarize the results of this test. */
	summarize();
	/* If there were no failures, we can remove the work dir. */
	if (failures == failures_before) {
		if (!keep_temp_files && chdir(tmpdir) == 0) {
			systemf("rm -rf %s", tests[i].name);
		}
	}
	/* Return appropriate status. */
	return (failures == failures_before ? 0 : 1);
}
Beispiel #12
0
int initialize_finddir()
{
	char path[FINDDIR_CHAR_LEN];

	info.nofilecheck=0;

	sprintf(info.nullchar, "(null)");

	if(strncpycmd(path, "echo -n ~", FINDDIR_CHAR_LEN) != 0)
		return -1;

	info.datadir[FINDDIR_CUSTOM][0] = '\0';
	snprintf(info.datadir[FINDDIR_LOCAL], FINDDIR_CHAR_LEN, "%s/.conkycolors", path);
	snprintf(info.datadir[FINDDIR_SYSTEM], FINDDIR_CHAR_LEN, "/usr/share/conkycolors");
	snprintf(info.tempdir, FINDDIR_CHAR_LEN, "/tmp/conkycolors");

	if(systemf("if ! test -d %s; then mkdir -p %s; fi", info.tempdir, info.tempdir) != 0)
	{
		printf("Failed to make %s\n", info.tempdir);
		return -1;
	}

	return 0;
}
Beispiel #13
0
int networkConnect()
{
    switch(CurNetwork.mode)
    {
    case NMODE_MANAGED:
        sprintf(cmdSetMode, "iwconfig %s mode managed", CurNetwork.interface);
        break;
    case NMODE_ADHOC:
        sprintf(cmdSetMode, "iwconfig %s mode ad-hoc", CurNetwork.interface);
        break;
    case NMODE_MASTER:
        sprintf(cmdSetMode, "iwconfig %s mode master", CurNetwork.interface);
        break;

    default:
        break;
    }
    sprintf(cmdSetIfaceUp, "ifconfig %s up", CurNetwork.interface);
    sprintf(cmdSetIfaceDown, "ifconfig %s down", CurNetwork.interface);
    sprintf(cmdSetEssid, "iwconfig %s essid \"%s\"", CurNetwork.interface, CurNetwork.essid);
    sprintf(cmdSetDhcp, "udhcpc -i %s -n", CurNetwork.interface);
    switch(CurNetwork.encryption)
    {
    case ENC_WEP:
        sprintf(cmdSetWepKey, "iwconfig %s key s:%s", CurNetwork.interface, CurNetwork.key);
        break;
    case ENC_WEP_NUM:
        sprintf(cmdSetWepKey, "iwconfig %s key %s", CurNetwork.interface, CurNetwork.key);
        break;
    case ENC_WPA:
        sprintf(cmdSetWpaPassphrase, "wpa_passphrase \"%s\" \"%s\" > /tmp/wpa.conf", CurNetwork.essid, CurNetwork.key);
        sprintf(cmdSetWpaSup, "wpa_supplicant -B -Dwext -i%s -c/tmp/wpa.conf", CurNetwork.interface);
        break;

    default:
        break;
    }

    systemf("ifconfig %s down", CurNetwork.interface);

#if defined(DRIVER_WEXT)
    if(systemf("ifconfig %s up", CurNetwork.interface))
    {
        return 1;
    }
    if(system(cmdSetMode))
    {
        return 1;
    }
    if(system(cmdSetEssid))
    {
        return 1;
    }
    switch(CurNetwork.encryption)
    {
    case ENC_NONE:
        sleep(1);
        break;
    case ENC_WEP:
    case ENC_WEP_NUM:
        if(system(cmdSetWepKey))
        {
            return 1;
        }
        sleep(1);
        break;
    case ENC_WPA:
        sleep(1);
        if(system(cmdSetWpaPassphrase))
        {
            return 1;
        }
        if(system(cmdSetWpaSup))
        {
            return 1;
        }
        sleep(1);
        break;

    default:
        break;
    }
#elif defined(DRIVER_NL80211)
    systemf("killall -9 wpa_supplicant");
    if(systemf("ifconfig %s up", CurNetwork.interface))
    {
        return 1;
    }
    sleep(1);
    switch(CurNetwork.encryption)
    {
    case ENC_NONE:
        break;
    case ENC_WEP:
    case ENC_WEP_NUM:
        systemf("echo -e \"network={\\nssid=\\\"%s\\\"\\nscan_ssid=1\\npriority=5\\nkey_mgmt=NONE\\nwep_key0=\\\"%s\\\"\\nwep_tx_keyidx=0\\n}\" > /tmp/wpa.conf", CurNetwork.essid, CurNetwork.key);
        systemf("wpa_supplicant -B -Dnl80211 -i%s -c/tmp/wpa.conf", CurNetwork.interface);
        sleep(1);
        break;
    case ENC_WPA:
        systemf("wpa_passphrase \"%s\" \"%s\" > /tmp/wpa.conf", CurNetwork.essid, CurNetwork.key);
        systemf("wpa_supplicant -B -Dnl80211 -i%s -c/tmp/wpa.conf", CurNetwork.interface);
        sleep(1);
        break;

    default:
        break;
    }
#endif
    if(systemf("udhcpc -i %s -n", CurNetwork.interface))
    {

        return 1;
    }

    updateIpAddress();

    return 0;
}
Beispiel #14
0
static void
unpack_test(const char *from, const char *options, const char *se)
{
	struct stat st, st2;
#if !defined(_WIN32) || defined(__CYGWIN__)
	char buff[128];
#endif
	int r;

	/* Create a work dir named after the file we're unpacking. */
	assertEqualInt(0, mkdir(from, 0775));
	chdir(from);

	/*
	 * Use cpio to unpack the sample archive
	 */
	extract_reference_file(from);
	r = systemf("%s -i %s < %s >unpack.out 2>unpack.err",
	    testprog, options, from);
	failure("Error invoking %s -i %s < %s",
	    testprog, options, from);
	assertEqualInt(r, 0);

	/* Verify that nothing went to stderr. */
	failure("Error invoking %s -i %s < %s", testprog, options, from);
	assertTextFileContents(se, "unpack.err");

	/*
	 * Verify unpacked files.
	 */

	/* Regular file with 2 links. */
	r = lstat("file", &st);
	failure("Failed to stat file %s/file, errno=%d", from, errno);
	assertEqualInt(r, 0);
	if (r == 0) {
		assert(S_ISREG(st.st_mode));
#if defined(_WIN32) && !defined(__CYGWIN__)
		assertEqualInt(0600, st.st_mode & 0700);
#else
		assertEqualInt(0644, st.st_mode & 0777);
#endif
		failure("file %s/file", from);
		assertEqualInt(10, st.st_size);
		failure("file %s/file", from);
		assertEqualInt(2, st.st_nlink);
	}

	/* Another name for the same file. */
	r = lstat("linkfile", &st2);
	failure("Failed to stat file %s/linkfile, errno=%d", from, errno);
	assertEqualInt(r, 0);
	if (r == 0) {
		assert(S_ISREG(st2.st_mode));
#if defined(_WIN32) && !defined(__CYGWIN__)
		assertEqualInt(0600, st2.st_mode & 0700);
#else
		assertEqualInt(0644, st2.st_mode & 0777);
#endif
		failure("file %s/file", from);
		assertEqualInt(10, st2.st_size);
		failure("file %s/file", from);
		assertEqualInt(2, st2.st_nlink);
		failure("file and linkfile should be hardlinked");
		assertEqualInt(st.st_dev, st2.st_dev);
		failure("file %s/file", from);
		assertEqualInt(st.st_ino, st2.st_ino);
	}

	/* Symlink */
	r = lstat("symlink", &st);
	failure("Failed to stat file %s/symlink, errno=%d", from, errno);
	assertEqualInt(r, 0);
#if !defined(_WIN32) || defined(__CYGWIN__)
	if (r == 0) {
		failure("symlink should be a symlink; actual mode is %o",
		    st.st_mode);
		assert(S_ISLNK(st.st_mode));
		if (S_ISLNK(st.st_mode)) {
			r = readlink("symlink", buff, sizeof(buff));
			assertEqualInt(r, 4);
			buff[r] = '\0';
			assertEqualString(buff, "file");
		}
	}
#endif

	/* dir */
	r = lstat("dir", &st);
	if (r == 0) {
		assertEqualInt(r, 0);
		assert(S_ISDIR(st.st_mode));
#if defined(_WIN32) && !defined(__CYGWIN__)
		assertEqualInt(0700, st.st_mode & 0700);
#else
		assertEqualInt(0775, st.st_mode & 0777);
#endif
	}

	chdir("..");
}
Beispiel #15
0
int main(int argc, char **argv)
{
	static const int limit = sizeof(tests) / sizeof(tests[0]);
	int i, tests_run = 0, tests_failed = 0, opt;
	time_t now;
	char *progname, *p;
	char tmpdir[256];
	char tmpdir_timestamp[256];

	/*
	 * Name of this program, used to build root of our temp directory
	 * tree.
	 */
	progname = p = argv[0];
	while (*p != '\0') {
		if (*p == '/')
			progname = p + 1;
		++p;
	}

	/* Get the target program from environment, if available. */
	testprog = getenv("BSDCPIO");

	/* Get the directory holding test files from environment. */
	refdir = getenv("BSDCPIO_TEST_FILES");

	/*
	 * Parse options.
	 */
	while ((opt = getopt(argc, argv, "kp:qr:")) != -1) {
		switch (opt) {
		case 'k':
			dump_on_failure = 0;
			break;
		case 'p':
			testprog = optarg;
			break;
		case 'q':
			quiet_flag++;
			break;
		case 'r':
			refdir = optarg;
			break;
		case '?':
		default:
			usage(progname);
		}
	}
	argc -= optind;
	argv += optind;

	/*
	 * Sanity-check that our options make sense.
	 */
	if (testprog == NULL)
		usage(progname);


	/*
	 * Create a temp directory for the following tests.
	 * Include the time the tests started as part of the name,
	 * to make it easier to track the results of multiple tests.
	 */
	now = time(NULL);
	for (i = 0; i < 1000; i++) {
		strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp),
		    "%Y-%m-%dT%H.%M.%S",
		    localtime(&now));
		sprintf(tmpdir, "/tmp/%s.%s-%03d", progname, tmpdir_timestamp, i);
		if (mkdir(tmpdir,0755) == 0)
			break;
		if (errno == EEXIST)
			continue;
		fprintf(stderr, "ERROR: Unable to create temp directory %s\n",
		    tmpdir);
		exit(1);
	}

	/*
	 * If the user didn't specify a directory for locating
	 * reference files, use the current directory for that.
	 */
	if (refdir == NULL) {
		systemf("/bin/pwd > %s/refdir", tmpdir);
		refdir = slurpfile(NULL, "%s/refdir", tmpdir);
		p = refdir + strlen(refdir);
		while (p[-1] == '\n') {
			--p;
			*p = '\0';
		}
	}

	/*
	 * Banner with basic information.
	 */
	if (!quiet_flag) {
		printf("Running tests in: %s\n", tmpdir);
		printf("Reference files will be read from: %s\n", refdir);
		printf("Running tests on: %s\n", testprog);
	}

	/*
	 * Run some or all of the individual tests.
	 */
	if (argc == 0) {
		/* Default: Run all tests. */
		for (i = 0; i < limit; i++) {
			if (test_run(i, tmpdir))
				tests_failed++;
			tests_run++;
		}
	} else {
		while (*(argv) != NULL) {
			i = atoi(*argv);
			if (**argv < '0' || **argv > '9' || i < 0 || i >= limit) {
				printf("*** INVALID Test %s\n", *argv);
				usage(progname);
			} else {
				if (test_run(i, tmpdir))
					tests_failed++;
				tests_run++;
			}
			argv++;
		}
	}

	/*
	 * Report summary statistics.
	 */
	if (!quiet_flag) {
		printf("\n");
		printf("%d of %d test groups reported failures\n",
		    tests_failed, tests_run);
		printf(" Total of %d individual tests failed.\n", failures);
		printf(" Total of %d individual tests were skipped.\n", skips);
	}
	return (tests_failed);
}
int main(int argn, char *argv[])
{
    int ret;
    char buf[256];
    int numfonts = 0;
    int t;
    char t1searchpath[1024];
    int nup_pos = 0;
    int x,y;
    int one_file_per_page = 0;
    
    initLog(0,-1,0,0,-1,loglevel);

    /* not needed anymore since fonts are embedded
       if(installPath) {
	fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
    }*/

#ifdef HAVE_SRAND48
    srand48(time(0)*getpid());
#else
#ifdef HAVE_SRAND
    srand(time(0)*getpid());
#endif
#endif

    processargs(argn, argv);
    
    driver = gfxsource_pdf_create();
    
    /* pass global parameters to PDF driver*/
    parameter_t*p = device_config;
    while(p) {
	driver->setparameter(driver, p->name, p->value);
	p = p->next;
    }

    if(!filename)
    {
	fprintf(stderr, "Please specify an input file\n");
	exit(1);
    }

    if (!info_only) {
        if(!outputname)
        {
            if(filename) {
                outputname = stripFilename(filename, ".swf");
                msg("<notice> Output filename not given. Writing to %s", outputname);
            } 
        }
            
        if(!outputname)
        {
            fprintf(stderr, "Please use -o to specify an output file\n");
            exit(1);
        }
    }

    // test if the page range is o.k.
    is_in_range(0x7fffffff, pagerange);

    if (!filename) {
	args_callback_usage(argv[0]);
	exit(0);
    }
    
    char fullname[256];
    if(password && *password) {
	sprintf(fullname, "%s|%s", filename, password);
	filename = fullname;
    }
    
    if(pagerange)
	driver->setparameter(driver, "pages", pagerange);

    /* add fonts */
    for(t=0;t<fontpathpos;t++) {
	driver->setparameter(driver, "fontdir", fontpaths[t]);
    }

    if(info_only) {
	show_info(driver, filename);
	return 0;
    }

    char*u = 0;
    if((u = strchr(outputname, '%'))) {
	if(strchr(u+1, '%') || 
	   strchr(outputname, '%')!=u)  {
	    msg("<error> only one %% allowed in filename\n");
	    return 1;
	}
	if(preloader || viewer) {
	    msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
	    return 1;
	}
	msg("<notice> outputting one file per page");
	one_file_per_page = 1;
	char*pattern = (char*)malloc(strlen(outputname)+2);
	/* convert % to %d */
	int l = u-outputname+1;
	memcpy(pattern, outputname, l);
	pattern[l]='d';
	strcpy(pattern+l+1, outputname+l);
	outputname = pattern;
    }

    gfxdocument_t* pdf = driver->open(driver, filename);
    if(!pdf) {
        msg("<error> Couldn't open %s", filename);
        exit(1);
    }
    /* pass global parameters document */
    p = device_config;
    while(p) {
	pdf->setparameter(pdf, p->name, p->value);
	p = p->next;
    }

    struct mypage_t {
	int x;
	int y;
	gfxpage_t*page;
    } pages[4];

    int pagenum = 0;
    int frame = 1;
    int pagenr;
    
    for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++) 
    {
	if(is_in_range(pagenr, pagerange)) {
	    char mapping[80];
	    sprintf(mapping, "%d:%d", pagenr, frame);
	    pdf->setparameter(pdf, "pagemap", mapping);
	    pagenum++;
	}
	if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
	    pagenum = 0;
	    frame++;
	}
    }
    if(pagerange && !pagenum && frame==1) {
	fprintf(stderr, "No pages in range %s", pagerange);
	exit(1);
    }

    pagenum = 0;

    gfxdevice_t*out = create_output_device();;
    pdf->prepare(pdf, out);

    for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++) 
    {
	if(is_in_range(pagenr, pagerange)) {
	    gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
	    pages[pagenum].x = 0;
	    pages[pagenum].y = 0;
	    pages[pagenum].page = page;
	    pagenum++;
	}
	if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {

	    int t;
	    int xmax[xnup], ymax[xnup];
	    int x,y;
	    int width=0, height=0;

	    memset(xmax, 0, xnup*sizeof(int));
	    memset(ymax, 0, ynup*sizeof(int));

	    for(y=0;y<ynup;y++)
	    for(x=0;x<xnup;x++) {
		int t = y*xnup + x;

		if(pages[t].page->width > xmax[x])
		    xmax[x] = (int)pages[t].page->width;
		if(pages[t].page->height > ymax[y])
		    ymax[y] = (int)pages[t].page->height;
	    }
	    for(x=0;x<xnup;x++) {
		width += xmax[x];
		xmax[x] = width;
	    }
	    for(y=0;y<ynup;y++) {
		height += ymax[y];
		ymax[y] = height;
	    }
	    if(custom_clip) {
		out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
	    } else {
		out->startpage(out,width,height);
	    }
	    for(t=0;t<pagenum;t++) {
		int x = t%xnup;
		int y = t/xnup;
		int xpos = x>0?xmax[x-1]:0;
		int ypos = y>0?ymax[y-1]:0;
		msg("<verbose> Render (%d,%d) move:%d/%d\n",
			(int)(pages[t].page->width + xpos),
			(int)(pages[t].page->height + ypos), xpos, ypos);
		pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos, 
			                                   custom_move? move_y : ypos,
							   custom_clip? clip_x1 : 0 + xpos, 
							   custom_clip? clip_y1 : 0 + ypos, 
							   custom_clip? clip_x2 : pages[t].page->width + xpos, 
							   custom_clip? clip_y2 : pages[t].page->height + ypos);
	    }
	    out->endpage(out);
	    for(t=0;t<pagenum;t++)  {
		pages[t].page->destroy(pages[t].page);
	    }
	    pagenum = 0;

	    if(one_file_per_page) {
		gfxresult_t*result = out->finish(out);out=0;
		char buf[1024];
		sprintf(buf, outputname, pagenr);
		if(result->save(result, buf) < 0) {
		    return 1;
		}
		result->destroy(result);result=0;
		out = create_output_device();;
                pdf->prepare(pdf, out);
		msg("<notice> Writing SWF file %s", buf);
	    }
	}
    }
   
    if(one_file_per_page) {
	// remove empty device
	gfxresult_t*result = out->finish(out);out=0;
	result->destroy(result);result=0;
    } else {
	gfxresult_t*result = out->finish(out);
	msg("<notice> Writing SWF file %s", outputname);
	if(result->save(result, outputname) < 0) {
	    exit(1);
	}
	int width = (int)(ptroff_t)result->get(result, "width");
	int height = (int)(ptroff_t)result->get(result, "height");
	result->destroy(result);result=0;

	if(preloader || viewer) {
	    const char*zip = "";
	    if(zlib) {
		zip = "-z";
	    }
	    if(!preloader && viewer) {
		systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
			viewer, outputname, outputname);
		if(!system_quiet)
		    printf("\n");
	    }
	    if(preloader && !viewer) {
		msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
		ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
			SWFDIR, preloader, outputname, outputname);
		if(!system_quiet)
		    printf("\n");
	    }
	    if(preloader && viewer) {
#ifdef HAVE_MKSTEMP
		char tmpname[] = "__swf__XXXXXX";
		mkstemp(tmpname);
#else 
		char*tmpname = "__tmp__.swf";
#endif
		systemf("swfcombine \"%s\" viewport=%s -o %s",
			viewer, outputname, tmpname);
		systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
			getRate(preloader), SWFDIR, preloader, tmpname, outputname);
		systemf("rm %s", tmpname);
	    }
	}
    }

    pdf->destroy(pdf);
    driver->destroy(driver);

   
    /* free global parameters */
    p = device_config;
    while(p) {
	parameter_t*next = p->next;
	if(p->name) free((void*)p->name);p->name = 0;
	if(p->value) free((void*)p->value);p->value =0;
	p->next = 0;free(p);
	p = next;
    }
    if(filters) {
	free(filters);
    }

    return 0;
}
int args_callback_option(char*name,char*val) {
    if (!strcmp(name, "o"))
    {
	outputname = val;
	return 1;
    }
    else if (!strcmp(name, "v"))
    {
	loglevel ++;
        setConsoleLogging(loglevel);
	return 0;
    }
    else if (!strcmp(name, "2"))
    {
        xnup = 2;
        ynup = 1;
	return 0;
    }
    else if (!strcmp(name, "4"))
    {
        xnup = 2;
        ynup = 2;
	return 0;
    }
    else if (!strcmp(name, "9"))
    {
        xnup = 3;
        ynup = 3;
	return 0;
    }
    else if (!strcmp(name, "X"))
    {
        maxwidth = atoi(val);
	return 1;
    }
    else if (!strcmp(name, "Y"))
    {
        maxheight = atoi(val);
	return 1;
    }
    else if (!strcmp(name, "q"))
    {
	loglevel --;
        setConsoleLogging(loglevel);
	system_quiet = 1;
	return 0;
    }
    else if (name[0]=='p')
    {
	/* check whether the page range follows the p directly, like 
	   in -p1,2 */
	do {
	    name++;
	} while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');

	if(*name) {
	    pagerange = name;
	    return 0;
	} 
	pagerange = val;	
	return 1;
    }
    else if (!strcmp(name, "P"))
    {
	password = val;
	return 1;
    }
    else if (!strcmp(name, "c"))
    {
	char*s = strdup(val);
	char*x1 = strtok(s, ":");
	char*y1 = strtok(0, ":");
	char*x2 = strtok(0, ":");
	char*y2 = strtok(0, ":");
	if(!(x1 && y1 && x2 && y2)) {
	    fprintf(stderr, "-c option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
	    exit(1);
	}
	custom_clip = 1;
	clip_x1 = atoi(x1);
	clip_y1 = atoi(y1);
	clip_x2 = atoi(x2);
	clip_y2 = atoi(y2);
	free(s);
	return 1;
    }
    else if (!strcmp(name, "m"))
    {
	char*s = strdup(val);
	char*c = strchr(s, ':');
	if(!c) {
	    fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
	    exit(1);
	}
	*c = 0;
	custom_move = 1;
	move_x = atoi(val);
	move_y = atoi(c+1);
	free(s);
	return 1;
    }
    else if (!strcmp(name, "s"))
    {
	char*s = val;
	char*c = strchr(s, '=');
	if(c && *c && c[1])  {
	    *c = 0;
	    c++;
	    store_parameter(s,c);
	} else if(!strcmp(s,"help")) {
	    printf("PDF Parameters:\n");
	    gfxsource_t*pdf = gfxsource_pdf_create();
	    pdf->setparameter(pdf, "help", "");
	    gfxdevice_t swf;
	    gfxdevice_swf_init(&swf);
	    printf("SWF Parameters:\n");
	    swf.setparameter(&swf, "help", "");
	    exit(0);
	} else {
	    store_parameter(s,"1");
	}
	return 1;
    }
    else if (!strcmp(name, "S"))
    {
	store_parameter("drawonlyshapes", "1");
	return 0;
    }
    else if (!strcmp(name, "i"))
    {
	store_parameter("ignoredraworder", "1");
	return 0;
    }
#ifndef WIN32
    else if (!strcmp(name, "Q"))
    {
	max_time = atoi(val);
	alarm(max_time);
# ifdef HAVE_SIGNAL_H
        signal(SIGALRM, sigalarm);
# endif
	return 1;
    }
#endif
    else if (!strcmp(name, "z"))
    {
	store_parameter("enablezlib", "1");
	zlib = 1;
	return 0;
    }
    else if (!strcmp(name, "n"))
    {
	store_parameter("opennewwindow", "1");
	return 0;
    }
    else if (!strcmp(name, "I"))
    {
	info_only = 1;
	return 0;
    }
    else if (!strcmp(name, "t"))
    {
	store_parameter("insertstop", "1");
	return 0;
    }
    else if (!strcmp(name, "T"))
    {
	if(!strcasecmp(val, "mx"))
	    store_parameter("flashversion", "6");
	else
	    store_parameter("flashversion", val);

	return 1;
    }
    else if (!strcmp(name, "f"))
    {
	store_parameter("storeallcharacters", "1");
	store_parameter("extrafontdata", "1");
	return 0;
    }
    else if (!strcmp(name, "ff"))
    {
	if(filters) {
	    // append this to the current filter expression (we allow more than one --filter)
	    int l = strlen(filters);
	    int new_len = l + strlen(val) + 2;
	    filters = (char*)realloc(filters, new_len);
	    filters[l] = ':';
	    strcpy(filters+l+1, val);
	} else {
	    filters = strdup(val);
	}
	return 1;
    }
    else if (!strcmp(name, "w"))
    {
	store_parameter("linksopennewwindow", "0");
	return 0;
    }
    else if (!strcmp(name, "O"))
    {
	int level = 1;
	int ret=0;
	if(val&& val[0] && val[1]==0 && isdigit(val[0])) {
	    level = atoi(val);
	    ret=1;
	}
	if(level>=1)
	    store_parameter("poly2bitmap", "1");
	if(level>=2)
	    store_parameter("bitmapfonts", "1");
	if(level>=3)
	    store_parameter("ignoredraworder", "1");
	return ret;
    }
    else if (!strcmp(name, "G"))
    {
	//store_parameter("optimize_polygons", "1");
	flatten = 1;
	return 0;
    }
    else if (!strcmp(name, "F"))
    {
	char *s = strdup(val);
	int l = strlen(s);
	while(l && s[l-1]=='/') {
	    s[l-1] = 0;
	    l--;
	}
	fontpaths[fontpathpos++] = s;
	return 1;
    }
    else if (!strcmp(name, "l"))
    {
	char buf[256];
	sprintf(buf, "%s/default_loader.swf", SWFDIR);
	preloader = strdup(buf);
	return 0;
    }
    else if (!strcmp(name, "b"))
    {
	char buf[256];
	sprintf(buf, "%s/default_viewer.swf", SWFDIR);
	viewer = strdup(buf);
	return 0;
    }
    else if (!strcmp(name, "L"))
    {
	if(val)
	{
	    preloader = val;
	}
	else
	{
	    systemf("ls %s/*_loader.swf", SWFDIR);
	    if(!system_quiet)
		printf("\n");
	    exit(1);
	}
	return 1;
    }
    else if (!strcmp(name, "B"))
    {
	if(val)
	{
	    viewer = val;
	}
	else
	{
	    systemf("ls %s/*_viewer.swf", SWFDIR);
	    if(!system_quiet)
		printf("\n");
	    exit(1);
	}
	return 1;
    }
    else if (!strcmp(name, "j"))
    {
	if(name[1]) {
	    store_parameter("jpegquality", &name[1]);
	    return 0;
	} else {
	    store_parameter("jpegquality", val);
	    return 1;
	}
    }
    else if (!strcmp(name, "V"))
    {	
	printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
	exit(0);
    }
    else 
    {
	fprintf(stderr, "Unknown option: -%s\n", name);
	exit(1);
    }
    return 0;
}