Exemplo n.º 1
0
static void build(struct options* opts)
{
    static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
    strarray *lib_dirs, *files;
    strarray *spec_args, *link_args;
    char *output_file;
    const char *spec_o_name;
    const char *output_name, *spec_file, *lang;
    const char* winebuild = getenv("WINEBUILD");
    int generate_app_loader = 1;
    unsigned int j;

    /* NOTE: for the files array we'll use the following convention:
     *    -axxx:  xxx is an archive (.a)
     *    -dxxx:  xxx is a DLL (.def)
     *    -lxxx:  xxx is an unsorted library
     *    -oxxx:  xxx is an object (.o)
     *    -rxxx:  xxx is a resource (.res)
     *    -sxxx:  xxx is a shared lib (.so)
     *    -xlll:  lll is the language (c, c++, etc.)
     */

    if (!winebuild) winebuild = "winebuild";

    output_file = strdup( opts->output_name ? opts->output_name : "a.out" );

    /* 'winegcc -o app xxx.exe.so' only creates the load script */
    if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so"))
    {
	create_file(output_file, 0755, app_loader_template, opts->files->base[0]);
	return;
    }

    /* generate app loader only for .exe */
    if (opts->shared || strendswith(output_file, ".exe.so"))
	generate_app_loader = 0;

    /* normalize the filename a bit: strip .so, ensure it has proper ext */
    if (strendswith(output_file, ".so")) 
	output_file[strlen(output_file) - 3] = 0;
    if (opts->shared)
    {
	if ((output_name = strrchr(output_file, '/'))) output_name++;
	else output_name = output_file;
	if (!strchr(output_name, '.'))
	    output_file = strmake("%s.dll", output_file);
    }
    else if (!strendswith(output_file, ".exe"))
	output_file = strmake("%s.exe", output_file);

    /* get the filename from the path */
    if ((output_name = strrchr(output_file, '/'))) output_name++;
    else output_name = output_file;

    /* prepare the linking path */
    if (!opts->wine_objdir)
    {
        lib_dirs = strarray_dup(opts->lib_dirs);
	for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ )
	    strarray_add(lib_dirs, stdlibpath[j]);
    }
    else
    {
        lib_dirs = strarray_alloc();
        strarray_add(lib_dirs, strmake("%s/dlls", opts->wine_objdir));
        strarray_add(lib_dirs, strmake("%s/libs/wine", opts->wine_objdir));
        strarray_addall(lib_dirs, opts->lib_dirs);
    }

    /* mark the files with their appropriate type */
    spec_file = lang = 0;
    files = strarray_alloc();
    for ( j = 0; j < opts->files->size; j++ )
    {
	const char* file = opts->files->base[j];
	if (file[0] != '-')
	{
	    switch(get_file_type(file))
	    {
		case file_def:
		case file_spec:
		    if (spec_file)
			error("Only one spec file can be specified\n");
		    spec_file = file;
		    break;
		case file_rc:
		    /* FIXME: invoke wrc to build it */
		    error("Can't compile .rc file at the moment: %s\n", file);
	            break;
	    	case file_res:
		    strarray_add(files, strmake("-r%s", file));
		    break;
		case file_obj:
		    strarray_add(files, strmake("-o%s", file));
		    break;
		case file_arh:
		    strarray_add(files, strmake("-a%s", file));
		    break;
		case file_so:
		    strarray_add(files, strmake("-s%s", file));
		    break;
	    	case file_na:
		    error("File does not exist: %s\n", file);
		    break;
	        default:
		    file = compile_to_object(opts, file, lang);
		    strarray_add(files, strmake("-o%s", file));
		    break;
	    }
	}
	else if (file[1] == 'l')
            add_library( lib_dirs, files, file + 2 );
	else if (file[1] == 'x')
	    lang = file;
    }
    if (opts->shared && !spec_file)
	error("A spec file is currently needed in shared mode\n");

    /* add the default libraries, if needed */
    if (!opts->nostdlib && opts->use_msvcrt) add_library(lib_dirs, files, "msvcrt");

    if (!opts->wine_objdir && !opts->nodefaultlibs) 
    {
        if (opts->gui_app) 
	{
	    add_library(lib_dirs, files, "shell32");
	    add_library(lib_dirs, files, "comdlg32");
	    add_library(lib_dirs, files, "gdi32");
	}
        add_library(lib_dirs, files, "advapi32");
        add_library(lib_dirs, files, "user32");
        add_library(lib_dirs, files, "kernel32");
    }

    if (!opts->nostartfiles) add_library(lib_dirs, files, "winecrt0");
    if (!opts->nostdlib) add_library(lib_dirs, files, "wine");

    /* run winebuild to generate the .spec.o file */
    spec_args = strarray_alloc();
    spec_o_name = get_temp_file(output_name, ".spec.o");
    strarray_add(spec_args, winebuild);
    if (verbose) strarray_add(spec_args, "-v");
    if (keep_generated) strarray_add(spec_args, "--save-temps");
    strarray_add(spec_args, "--as-cmd");
    strarray_add(spec_args, AS);
    strarray_add(spec_args, "--ld-cmd");
    strarray_add(spec_args, LD);
    strarray_addall(spec_args, strarray_fromstring(DLLFLAGS, " "));
    strarray_add(spec_args, opts->shared ? "--dll" : "--exe");
    strarray_add(spec_args, "-o");
    strarray_add(spec_args, spec_o_name);
    if (spec_file)
    {
        strarray_add(spec_args, "-E");
        strarray_add(spec_args, spec_file);
    }

    if (!opts->shared)
    {
        strarray_add(spec_args, "-F");
        strarray_add(spec_args, output_name);
        strarray_add(spec_args, "--subsystem");
        strarray_add(spec_args, opts->gui_app ? "windows" : "console");
        if (opts->unicode_app)
        {
            strarray_add(spec_args, "--entry");
            strarray_add(spec_args, "__wine_spec_exe_wentry");
        }
    }

    for ( j = 0; j < lib_dirs->size; j++ )
	strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));

    for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
        strarray_add(spec_args, opts->winebuild_args->base[j]);

    for ( j = 0; j < files->size; j++ )
    {
	const char* name = files->base[j] + 2;
	switch(files->base[j][1])
	{
	    case 'r':
		strarray_add(spec_args, files->base[j]);
		break;
	    case 'd':
	    case 'a':
	    case 'o':
		strarray_add(spec_args, name);
		break;
	}
    }

    spawn(opts->prefix, spec_args, 0);

    /* link everything together now */
    link_args = strarray_alloc();
    strarray_addall(link_args, get_translator(opts->processor));
    strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " "));

    strarray_add(link_args, "-o");
    strarray_add(link_args, strmake("%s.so", output_file));

    for ( j = 0 ; j < opts->linker_args->size ; j++ ) 
        strarray_add(link_args, opts->linker_args->base[j]);

#ifdef __APPLE__
    if (opts->image_base)
    {
        strarray_add(link_args, "-image_base");
        strarray_add(link_args, opts->image_base);
    }
#endif

    for ( j = 0; j < lib_dirs->size; j++ )
	strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));

    strarray_add(link_args, spec_o_name);

    for ( j = 0; j < files->size; j++ )
    {
	const char* name = files->base[j] + 2;
	switch(files->base[j][1])
	{
	    case 'l':
	    case 's':
		strarray_add(link_args, strmake("-l%s", name));
		break;
	    case 'a':
	    case 'o':
		strarray_add(link_args, name);
		break;
	}
    }

    if (!opts->nostdlib) 
    {
	strarray_add(link_args, "-lm");
	strarray_add(link_args, "-lc");
    }

    spawn(opts->prefix, link_args, 0);

    /* set the base address */
    if (opts->image_base)
    {
        const char *prelink = PRELINK;
        if (prelink[0] && strcmp(prelink,"false"))
        {
            strarray *prelink_args = strarray_alloc();
            strarray_add(prelink_args, prelink);
            strarray_add(prelink_args, "--reloc-only");
            strarray_add(prelink_args, opts->image_base);
            strarray_add(prelink_args, strmake("%s.so", output_file));
            spawn(opts->prefix, prelink_args, 1);
            strarray_free(prelink_args);
        }
    }

    /* create the loader script */
    if (generate_app_loader)
    {
        if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0;
        create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name));
    }
}
Exemplo n.º 2
0
static int riemann_event_add_tag (Event *event, char const *tag) /* {{{ */
{
	return (strarray_add (&event->tags, &event->n_tags, tag));
} /* }}} int riemann_event_add_tag */
Exemplo n.º 3
0
static void compile(struct options* opts, const char* lang)
{
    strarray* comp_args = strarray_alloc();
    unsigned int j;
    int gcc_defs = 0;

    switch(opts->processor)
    {
	case proc_cpp:  gcc_defs = 1; break;
#ifdef __GNUC__
	/* Note: if the C compiler is gcc we assume the C++ compiler is too */
	/* mixing different C and C++ compilers isn't supported in configure anyway */
	case proc_cc:  gcc_defs = 1; break;
	case proc_cxx: gcc_defs = 1; break;
#else
	case proc_cc:  gcc_defs = 0; break;
	case proc_cxx: gcc_defs = 0; break;
#endif
	case proc_as:  gcc_defs = 0; break;
    }
    strarray_addall(comp_args, get_translator(opts->processor));

    if (opts->processor != proc_cpp)
    {
#ifdef CC_FLAG_SHORT_WCHAR
	if (!opts->wine_objdir && !opts->noshortwchar)
	{
            strarray_add(comp_args, CC_FLAG_SHORT_WCHAR);
            strarray_add(comp_args, "-DWINE_UNICODE_NATIVE");
	}
#endif
        strarray_addall(comp_args, strarray_fromstring(DLLFLAGS, " "));
    }

#ifdef _WIN64
    strarray_add(comp_args, "-DWIN64");
    strarray_add(comp_args, "-D_WIN64");
    strarray_add(comp_args, "-D__WIN64");
    strarray_add(comp_args, "-D__WIN64__");
#else
    strarray_add(comp_args, "-DWIN32");
    strarray_add(comp_args, "-D_WIN32");
    strarray_add(comp_args, "-D__WIN32");
    strarray_add(comp_args, "-D__WIN32__");
#endif
    strarray_add(comp_args, "-D__WINNT");
    strarray_add(comp_args, "-D__WINNT__");

    if (gcc_defs)
    {
#ifdef __APPLE__ /* Mac OS X uses a 16-byte aligned stack and not a 4-byte one */
	strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
	strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
	strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
	strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
#else
	strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__))");
	strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__))");
	strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__))");
	strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__))");
#endif

	strarray_add(comp_args, "-D__fastcall=__attribute__((__fastcall__))");
	strarray_add(comp_args, "-D_fastcall=__attribute__((__fastcall__))");
	strarray_add(comp_args, "-D__declspec(x)=__declspec_##x");
	strarray_add(comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
	strarray_add(comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
	strarray_add(comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
	strarray_add(comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
	strarray_add(comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
	strarray_add(comp_args, "-D__declspec_naked=__attribute__((naked))");
	strarray_add(comp_args, "-D__declspec_noinline=__attribute__((noinline))");
	strarray_add(comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
	strarray_add(comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
	strarray_add(comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
	strarray_add(comp_args, "-D__declspec_selectany=__attribute__((weak))");
	strarray_add(comp_args, "-D__declspec_thread=__thread");
    }

    /* Wine specific defines */
    strarray_add(comp_args, "-D__WINE__");
    strarray_add(comp_args, "-D__int8=char");
    strarray_add(comp_args, "-D__int16=short");
    /* FIXME: what about 64-bit platforms? */
    strarray_add(comp_args, "-D__int32=int");
#ifdef HAVE_LONG_LONG
    strarray_add(comp_args, "-D__int64=long long");
#endif

    /* options we handle explicitly */
    if (opts->compile_only)
	strarray_add(comp_args, "-c");
    if (opts->output_name)
    {
	strarray_add(comp_args, "-o");
	strarray_add(comp_args, opts->output_name);
    }

    /* the rest of the pass-through parameters */
    for ( j = 0 ; j < opts->compiler_args->size ; j++ ) 
        strarray_add(comp_args, opts->compiler_args->base[j]);

    /* the language option, if any */
    if (lang && strcmp(lang, "-xnone"))
	strarray_add(comp_args, lang);

    /* last, but not least, the files */
    for ( j = 0; j < opts->files->size; j++ )
    {
	if (opts->files->base[j][0] != '-')
	    strarray_add(comp_args, opts->files->base[j]);
    }

    /* standard includes come last in the include search path */
#ifdef __GNUC__
#define SYS_INCLUDE "-isystem"
#else
#define SYS_INCLUDE "-I"
#endif
    if (!opts->wine_objdir && !opts->nostdinc)
    {
        if (opts->use_msvcrt)
        {
            strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/msvcrt");
            strarray_add(comp_args, "-D__MSVCRT__");
        }
        strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/windows");
    }
#undef SYS_INCLUDE

    spawn(opts->prefix, comp_args, 0);
}
Exemplo n.º 4
0
static int statsd_read (void) /* {{{ */
{
  c_avl_iterator_t *iter;
  char *name;
  statsd_metric_t *metric;

  char **to_be_deleted = NULL;
  size_t to_be_deleted_num = 0;
  size_t i;

  pthread_mutex_lock (&metrics_lock);

  if (metrics_tree == NULL)
  {
    pthread_mutex_unlock (&metrics_lock);
    return (0);
  }

  iter = c_avl_get_iterator (metrics_tree);
  while (c_avl_iterator_next (iter, (void *) &name, (void *) &metric) == 0)
  {
    if ((metric->updates_num == 0)
        && ((conf_delete_counters && (metric->type == STATSD_COUNTER))
          || (conf_delete_timers && (metric->type == STATSD_TIMER))
          || (conf_delete_gauges && (metric->type == STATSD_GAUGE))
          || (conf_delete_sets && (metric->type == STATSD_SET))))
    {
      DEBUG ("statsd plugin: Deleting metric \"%s\".", name);
      strarray_add (&to_be_deleted, &to_be_deleted_num, name);
      continue;
    }

    /* Names have a prefix, e.g. "c:", which determines the (statsd) type.
     * Remove this here. */
    statsd_metric_submit_unsafe (name + 2, metric);

    /* Reset the metric. */
    metric->updates_num = 0;
    if (metric->type == STATSD_SET)
      statsd_metric_clear_set_unsafe (metric);
  }
  c_avl_iterator_destroy (iter);

  for (i = 0; i < to_be_deleted_num; i++)
  {
    int status;

    status = c_avl_remove (metrics_tree, to_be_deleted[i],
        (void *) &name, (void *) &metric);
    if (status != 0)
    {
      ERROR ("stats plugin: c_avl_remove (\"%s\") failed with status %i.",
          to_be_deleted[i], status);
      continue;
    }

    sfree (name);
    statsd_metric_free (metric);
  }

  pthread_mutex_unlock (&metrics_lock);

  strarray_free (to_be_deleted, to_be_deleted_num);

  return (0);
} /* }}} int statsd_read */
Exemplo n.º 5
0
Arquivo: res32.c Projeto: r6144/wine
/* output the resources into a .o file */
void output_res_o_file( DLLSPEC *spec )
{
    unsigned int i;
    char *res_file = NULL;
    int fd;

    if (!spec->nb_resources) fatal_error( "--resources mode needs at least one resource file as input\n" );
    if (!output_file_name) fatal_error( "No output file name specified\n" );

    byte_swapped = 0;
    init_output_buffer();

    put_dword( 0 );      /* ResSize */
    put_dword( 32 );     /* HeaderSize */
    put_word( 0xffff );  /* ResType */
    put_word( 0x0000 );
    put_word( 0xffff );  /* ResName */
    put_word( 0x0000 );
    put_dword( 0 );      /* DataVersion */
    put_word( 0 );       /* Memory options */
    put_word( 0 );       /* Language */
    put_dword( 0 );      /* Version */
    put_dword( 0 );      /* Characteristics */

    for (i = 0; i < spec->nb_resources; i++)
    {
        unsigned int header_size = get_resource_header_size( &spec->resources[i] );

        put_dword( spec->resources[i].data_size );
        put_dword( (header_size + 3) & ~3 );
        put_string( &spec->resources[i].type );
        put_string( &spec->resources[i].name );
        align_output( 4 );
        put_dword( 0 );
        put_word( spec->resources[i].mem_options );
        put_word( spec->resources[i].lang );
        put_dword( 0 );
        put_dword( 0 );
        put_data( spec->resources[i].data, spec->resources[i].data_size );
        align_output( 4 );
    }

    /* if the output file name is a .res too, don't run the results through windres */
    if (strendswith( output_file_name, ".res"))
    {
        flush_output_buffer();
        return;
    }

    res_file = get_temp_file_name( output_file_name, ".res" );
    if ((fd = open( res_file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600 )) == -1)
        fatal_error( "Cannot create %s\n", res_file );
    if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
        fatal_error( "Error writing to %s\n", res_file );
    close( fd );
    free( output_buffer );

    if (res_file)
    {
        struct strarray *args = strarray_init();
        strarray_add( args, find_tool( "windres", NULL ), "-i", res_file, "-o", output_file_name, NULL );
        spawn( args );
        strarray_free( args );
    }
    output_file_name = NULL;  /* so we don't try to assemble it */
}
Exemplo n.º 6
0
int handle_flush (FILE *fh, char *buffer)
{
	int success = 0;
	int error   = 0;

	double timeout = 0.0;
	char **plugins = NULL;
	size_t plugins_num = 0;
	char **identifiers = NULL;
	size_t identifiers_num = 0;

#define PRINT_TO_SOCK(fh, ...) \
	do { \
		if (fprintf (fh, __VA_ARGS__) < 0) { \
			char errbuf[1024]; \
			WARNING ("handle_flush: failed to write to socket #%i: %s", \
					fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
			strarray_free (plugins, plugins_num); \
			strarray_free (identifiers, identifiers_num); \
			return -1; \
		} \
		fflush(fh); \
	} while (0)

	if ((fh == NULL) || (buffer == NULL))
		return (-1);

	DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
			(void *) fh, buffer);

	if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
	{
		PRINT_TO_SOCK (fh, "-1 Cannot parse command.\n");
		return (-1);
	}
	buffer += strlen ("FLUSH");

	while (*buffer != 0)
	{
		char *opt_key;
		char *opt_value;
		int status;

		opt_key = NULL;
		opt_value = NULL;
		status = parse_option (&buffer, &opt_key, &opt_value);
		if (status != 0)
		{
			PRINT_TO_SOCK (fh, "-1 Parsing options failed.\n");
			strarray_free (plugins, plugins_num);
			strarray_free (identifiers, identifiers_num);
			return (-1);
		}

		if (strcasecmp ("plugin", opt_key) == 0)
			strarray_add (&plugins, &plugins_num, opt_value);
		else if (strcasecmp ("identifier", opt_key) == 0)
			strarray_add (&identifiers, &identifiers_num, opt_value);
		else if (strcasecmp ("timeout", opt_key) == 0)
		{
			char *endptr;

			errno = 0;
			endptr = NULL;
			timeout = strtod (opt_value, &endptr);

			if ((endptr == opt_value) || (errno != 0) || (!isfinite (timeout)))
			{
				PRINT_TO_SOCK (fh, "-1 Invalid value for option `timeout': "
						"%s\n", opt_value);
				strarray_free (plugins, plugins_num);
				strarray_free (identifiers, identifiers_num);
				return (-1);
			}
			else if (timeout < 0.0)
			{
				timeout = 0.0;
			}
		}
		else
		{
			PRINT_TO_SOCK (fh, "-1 Cannot parse option %s\n", opt_key);
			strarray_free (plugins, plugins_num);
			strarray_free (identifiers, identifiers_num);
			return (-1);
		}
	} /* while (*buffer != 0) */

	for (size_t i = 0; (i == 0) || (i < plugins_num); i++)
	{
		char *plugin = NULL;

		if (plugins_num != 0)
			plugin = plugins[i];

		for (size_t j = 0; (j == 0) || (j < identifiers_num); j++)
		{
			char *identifier = NULL;
			int status;

			if (identifiers_num != 0)
				identifier = identifiers[j];

			status = plugin_flush (plugin,
					DOUBLE_TO_CDTIME_T (timeout),
					identifier);
			if (status == 0)
				success++;
			else
				error++;
		}
	}

	PRINT_TO_SOCK (fh, "0 Done: %i successful, %i errors\n",
			success, error);

	strarray_free (plugins, plugins_num);
	strarray_free (identifiers, identifiers_num);
	return (0);
#undef PRINT_TO_SOCK
} /* int handle_flush */
Exemplo n.º 7
0
int main(int argc, char **argv)
{
    save_argv0(argv[0]);

    struct cyrbu_cmd_options options = {0};
    enum cyrbu_mode mode = CYRBU_MODE_UNSPECIFIED;
    enum cyrbu_cmd cmd = CYRBU_CMD_UNSPECIFIED;
    const char *alt_config = NULL;
    const char *backup_name = NULL;
    const char *command = NULL;
    const char *subcommand = NULL;
    struct backup *backup = NULL;
    mbname_t *mbname = NULL;
    int i, opt, r = 0;

    while ((opt = getopt(argc, argv, "C:fmuv")) != EOF) {
        switch (opt) {
        case 'C':
            alt_config = optarg;
            break;
        case 'f':
            if (mode != CYRBU_MODE_UNSPECIFIED) usage();
            mode = CYRBU_MODE_FILENAME;
            break;
        case 'm':
            if (mode != CYRBU_MODE_UNSPECIFIED) usage();
            mode = CYRBU_MODE_MBOXNAME;
            break;
        case 'u':
            if (mode != CYRBU_MODE_UNSPECIFIED) usage();
            mode = CYRBU_MODE_USERNAME;
            break;
        case 'v':
            options.verbose++;
            break;
        default:
            usage();
            break;
        }
    }

    /* default mode is username */
    if (mode == CYRBU_MODE_UNSPECIFIED)
        mode = CYRBU_MODE_USERNAME;

    /* get the backup name */
    if (optind == argc) usage();
    backup_name = argv[optind++];

    /* get the command */
    if (optind == argc) usage();
    command = argv[optind++];

    /* get the subcommand */
    if (optind == argc) usage();
    subcommand = argv[optind++];

    /* parse the command and subcommand */
    cmd = parse_cmd_string(command, subcommand);

    /* check remaining arguments based on command */
    switch (cmd) {
    case CYRBU_CMD_LIST_ALL:
    case CYRBU_CMD_LIST_CHUNKS:
    case CYRBU_CMD_LIST_MAILBOXES:
    case CYRBU_CMD_LIST_MESSAGES:
        /* these want no more arguments */
        if (optind != argc) usage();
        break;
    case CYRBU_CMD_SHOW_CHUNKS:
    case CYRBU_CMD_SHOW_MAILBOXES:
    case CYRBU_CMD_SHOW_MESSAGES:
        /* these need at least one more argument */
        if (optind == argc) usage();
        break;
    case CYRBU_CMD_DUMP_CHUNK:
    case CYRBU_CMD_DUMP_MAILBOX:
    case CYRBU_CMD_DUMP_MESSAGE:
        /* these need exactly one more argument */
        if (argc - optind != 1) usage();
        break;
    default:
        usage();
        break;
    }

    /* build a nice args list */
    options.argv = strarray_new();
    for (i = optind; i < argc; i++) {
        strarray_add(options.argv, argv[i]);
    }

    // FIXME finish parsing options

    cyrus_init(alt_config, "cyr_backup", 0, 0);

    /* open backup */
    switch (mode) {
    case CYRBU_MODE_FILENAME:
        r = backup_open_paths(&backup, backup_name, NULL,
                              BACKUP_OPEN_NONBLOCK, BACKUP_OPEN_NOCREATE);
        break;
    case CYRBU_MODE_MBOXNAME:
        mbname = mbname_from_intname(backup_name);
        if (!mbname) usage();
        r = backup_open(&backup, mbname,
                        BACKUP_OPEN_NONBLOCK, BACKUP_OPEN_NOCREATE);
        break;
    case CYRBU_MODE_USERNAME:
        mbname = mbname_from_userid(backup_name);
        if (!mbname) usage();
        r = backup_open(&backup, mbname,
                        BACKUP_OPEN_NONBLOCK, BACKUP_OPEN_NOCREATE);
        break;
    default:
        usage();
        break;
    }

    /* run command */
    if (!r && cmd_func[cmd])
        r = cmd_func[cmd](backup, &options);

    if (r)
        fprintf(stderr, "%s: %s\n", backup_name, error_message(r));

    /* close backup */
    if (backup)
        backup_close(&backup);

    /* clean up and exit */
    backup_cleanup_staging_path();
    cyrus_done();

    strarray_free(options.argv);
    exit(r ? EC_TEMPFAIL : EC_OK);
}
Exemplo n.º 8
0
int main(int argc, char** argv)
{
	char line[1024], point_a[1024], point_b[1024];
	struct hashed_strarray connpt_names;
	FILE* fp = 0;
	int i, rc, point_a_idx, point_b_idx, existing_net_idx, new_net_idx;
	int net_data_idx;
	uint32_t** nets;

	if (argc < 2) {
		fprintf(stderr,
			"\n"
			"pair2net - finds all pairs connected to the same net\n"
			"Usage: %s <data_file> | '-' for stdin\n", argv[0]);
		goto xout;
	}
	if (strarray_init(&connpt_names, STRIDX_1M)) {
		fprintf(stderr, "Out of memory in %s:%i\n", __FILE__, __LINE__);
		goto xout;
	}

	nets = calloc(STRIDX_1M, sizeof(*nets));
	if (!nets) {
		fprintf(stderr, "Out of memory in %s:%i\n", __FILE__, __LINE__);
		goto xout;
	}
	if (!strcmp(argv[1], "-"))
		fp = stdin;
	else {
		fp = fopen(argv[1], "r");
		if (!fp) {
			fprintf(stderr, "Error opening %s.\n", argv[1]);
			goto xout;
		}
	}
	while (fgets(line, sizeof(line), fp)) {
		i = sscanf(line, "%s%s", point_a, point_b);
		if (i != 2) continue;

		i = strlen(point_b);
		if (i && point_b[i-1] == '\n')
			point_b[i-1] = 0;
		rc = strarray_add(&connpt_names, point_a, &point_a_idx);
		if (rc) {
			fprintf(stderr, "Out of memory in %s:%i\n",
				__FILE__, __LINE__);
			goto xout;
		}
		rc = strarray_add(&connpt_names, point_b, &point_b_idx);
		if (rc) {
			fprintf(stderr, "Out of memory in %s:%i\n",
				__FILE__, __LINE__);
			goto xout;
		}
		if (nets[point_a_idx] && nets[point_b_idx]) {
			continue;}
		if (nets[point_a_idx] || nets[point_b_idx]) {
			if (nets[point_a_idx]) {
				existing_net_idx = point_a_idx;
				new_net_idx = point_b_idx;
			} else { // point_b_idx exists
				existing_net_idx = point_b_idx;
				new_net_idx = point_a_idx;
			}
			if ((uint64_t) nets[existing_net_idx] & 1)
				net_data_idx = (uint64_t) nets[existing_net_idx] >> 32;
			else
				net_data_idx = existing_net_idx;
			// add new_net_idx to net data
			rc = add_entry(&nets[net_data_idx], new_net_idx);
			if (rc) goto xout;
			// point to net data from new_net_idx
			nets[new_net_idx] = (uint32_t*) (((uint64_t) net_data_idx << 32) | 1);
		} else {