static void append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value) { const char *val; char *dupval = strdup (value); val = strtok (dupval, ","); while (val != NULL) { DBusMessageIter subiter; dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL, &subiter); append_arg (&subiter, keytype, val); val = strtok (NULL, ","); if (val == NULL) { fprintf (stderr, "%s: Malformed dictionary\n", appname); exit (1); } append_arg (&subiter, valtype, val); dbus_message_iter_close_container (iter, &subiter); val = strtok (NULL, ","); } free (dupval); }
void join_args(char const * const *args_array,MyString *result,int start_arg) { ASSERT(result); if(!args_array) return; for(int i=0;args_array[i];i++) { if(i<start_arg) continue; append_arg(args_array[i],*result); } }
void join_args(SimpleList<MyString> const &args_list,MyString *result,int start_arg) { SimpleListIterator<MyString> it(args_list); ASSERT(result); MyString *arg=NULL; for(int i=0;it.Next(arg);i++) { if(i<start_arg) continue; append_arg(arg->Value(),*result); } }
static void append_array (DBusMessageIter *iter, int type, const char *value) { const char *val; char *dupval = strdup (value); val = strtok (dupval, ","); while (val != NULL) { append_arg (iter, type, val); val = strtok (NULL, ","); } free (dupval); }
int add_arg( char* s, char*** arg, size_t* index, size_t* capacity, int level ) /* purpose: sorts a given full argument string, whether to add or extend * This is the high-level interface to previous functions. * paramtr: s (IN): string to append * arg (OUT): list of arguments as vector * index (IO): index where a new data should be inserted into * capacity (IO): capacity (extend) of vector * level (IN): level of recursion, use 1 * returns: 0 means ok, -1 means error, see errno */ { if ( s[0] == '@' && s[1] != 0 ) { if ( s[1] == '@' ) { return append_arg( s+1, arg, index, capacity ); } else { return expand_arg( s+1, arg, index, capacity, level+1 ); } } else { return append_arg( s, arg, index, capacity ); } }
extern "C" CDECL int rust_run_program(const char* argv[], void* envp, const char* dir, int in_fd, int out_fd, int err_fd) { STARTUPINFO si; ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; HANDLE curproc = GetCurrentProcess(); HANDLE origStdin = (HANDLE)_get_osfhandle(in_fd ? in_fd : 0); if (!DuplicateHandle(curproc, origStdin, curproc, &si.hStdInput, 0, 1, DUPLICATE_SAME_ACCESS)) return -1; HANDLE origStdout = (HANDLE)_get_osfhandle(out_fd ? out_fd : 1); if (!DuplicateHandle(curproc, origStdout, curproc, &si.hStdOutput, 0, 1, DUPLICATE_SAME_ACCESS)) return -1; HANDLE origStderr = (HANDLE)_get_osfhandle(err_fd ? err_fd : 2); if (!DuplicateHandle(curproc, origStderr, curproc, &si.hStdError, 0, 1, DUPLICATE_SAME_ACCESS)) return -1; size_t cmd_len = 0; for (const char** arg = argv; *arg; arg++) { cmd_len += strlen(*arg); cmd_len += 3; // Two quotes plus trailing space or \0 } cmd_len *= 2; // Potentially backslash-escape everything. char* cmd = (char*)malloc(cmd_len); char* pos = cmd; for (const char** arg = argv; *arg; arg++) { append_arg(pos, *arg, *(arg+1) == NULL); } PROCESS_INFORMATION pi; BOOL created = CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, envp, dir, &si, &pi); CloseHandle(si.hStdInput); CloseHandle(si.hStdOutput); CloseHandle(si.hStdError); free(cmd); if (!created) return -1; return (int)pi.hProcess; }
/* Changing optimization levels is a common testing pattern -- we've got a special option that searches for and replaces anything beginning with -O */ void replace_optimization_level (char *new_level) { int i; int optionFound = 0; char *new_opt = malloc(strlen(new_opt)+3); sprintf(new_opt, "-O%s",new_level); for (i=0;i<arg_count;i++) { if (strncmp(arg_array[i],"-O",2) == 0) { replace_arg (new_opt, i); optionFound = 1; break; } } if (optionFound == 0) /* No optimization level? Add it! */ append_arg (new_opt); free (new_opt); }
ComputationNode * v_create_computation(const char * name, ComputationCallback computation, va_list args ) { ComputationNode * computationNode = (ComputationNode *) malloc(sizeof(ComputationNode)); computationNode->name = name; computationNode->computation = computation; computationNode->dependencies = NULL; ComputationNode * dependency; computationNode->numArgs = 0; do { dependency = va_arg (args, ComputationNode * ); if ( dependency->type == END){ break; } append_arg(&(computationNode->dependencies), dependency); ComputationList * dep = computationNode->dependencies; ComputationNode * comp = dep->computation; printf("Append arg of type %s\n", arg_strings[comp->type]); computationNode->numArgs++; } while (true); computationNode->args = (struct ComputationNode **) malloc(sizeof(ComputationNode) * computationNode->numArgs); printf("There are %d args\n", computationNode->numArgs); int i; ComputationList * currentDependency = computationNode->dependencies; int len = computationNode->numArgs; for(i = 0; i < len; i++) { computationNode->args[len - 1 - i] = (currentDependency->computation); currentDependency = currentDependency->next; } computationNode->valid = false; return computationNode; }
void mangle(char *options, struct cpa *a) { char *p, *s, *val; for (s = options; (p = strsep(&s, ",")) != NULL;) if (*p != '\0') { if (strcmp(p, "noauto") == 0) { /* * Do not pass noauto option to nmount(). * or external mount program. noauto is * only used to prevent mounting a filesystem * when 'mount -a' is specified, and is * not a real mount option. */ continue; } else if (strcmp(p, "late") == 0) { /* * "late" is used to prevent certain file * systems from being mounted before late * in the boot cycle; for instance, * loopback NFS mounts can't be mounted * before mountd starts. */ continue; } else if (strcmp(p, "failok") == 0) { /* * "failok" is used to prevent certain file * systems from being causing the system to * drop into single user mode in the boot * cycle, and is not a real mount option. */ continue; } else if (strncmp(p, "mountprog", 9) == 0) { /* * "mountprog" is used to force the use of * userland mount programs. */ val = strchr(p, '='); if (val != NULL) { ++val; if (*val != '\0') mountprog = strdup(val); } if (mountprog == NULL) { errx(1, "Need value for -o mountprog"); } continue; } else if (strcmp(p, "userquota") == 0) { continue; } else if (strncmp(p, userquotaeq, sizeof(userquotaeq) - 1) == 0) { continue; } else if (strcmp(p, "groupquota") == 0) { continue; } else if (strncmp(p, groupquotaeq, sizeof(groupquotaeq) - 1) == 0) { continue; } else if (*p == '-') { append_arg(a, p); p = strchr(p, '='); if (p != NULL) { *p = '\0'; append_arg(a, p + 1); } } else { append_arg(a, strdup("-o")); append_arg(a, p); } } }
/** * Regiser other as a dependent of self */ void register_dependent(ComputationNode * self, ComputationNode * other) { if (! is_dependent(self, other)) { append_arg(&(self->dependents), other); } }
int main(void) { char *cmdline = GetCommandLine(); char *new_cmdline; { char full_path[256]; GetModuleFileName(NULL, full_path, sizeof(full_path)); PathRemoveFileSpec(full_path); append_str(full_path); append_str("\\"); } if(MessageBox(0, "Enable COPY?", "Switcher", MB_YESNO | MB_ICONQUESTION) == IDYES) { int kv = 0; const char *my_client = "..\\spicy\\spicy.exe"; const char *required_args[] = { "-h", "-p", "-s", "-w", NULL }; { char path[256]; GetModuleFileName(NULL, path, sizeof(path)); PathRemoveFileSpec(path); strcat(path, "\\..\\spicy"); SetCurrentDirectory(path); } get_item(&cmdline); // skip program path append_str(my_client); while(get_item(&cmdline)) { int required = 0; const char **p = required_args; for(; *p; p++) if(strcmp(item, *p) == 0) { required = 1; break; } if(required) { append_arg(); get_item(&cmdline); append_arg(); } else { get_item(&cmdline); } } } else { const char *orig_client = "_spicec.exe"; get_item(&cmdline); // skip program path append_str(orig_client); append_str(cmdline); } new_cmdline = finish_args(); { STARTUPINFO si; PROCESS_INFORMATION pi; DWORD flags = 0; memset(&si, 0, sizeof(si)); si.cb = sizeof(STARTUPINFO); /* want to show the console of spicy for debug? */ #if 0 flags = CREATE_NEW_CONSOLE; #endif CreateProcessA(NULL, new_cmdline, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi); } return 0; }
int mountfs(const char *vfstype, const char *spec, const char *name, int flags, const char *options, const char *mntopts) { struct statfs sf; int i, ret; char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX]; static struct cpa mnt_argv; /* resolve the mountpoint with realpath(3) */ if (checkpath(name, mntpath) != 0) { warn("%s", mntpath); return (1); } name = mntpath; if (mntopts == NULL) mntopts = ""; optbuf = catopt(strdup(mntopts), options); if (strcmp(name, "/") == 0) flags |= MNT_UPDATE; if (flags & MNT_FORCE) optbuf = catopt(optbuf, "force"); if (flags & MNT_RDONLY) optbuf = catopt(optbuf, "ro"); /* * XXX * The mount_mfs (newfs) command uses -o to select the * optimization mode. We don't pass the default "-o rw" * for that reason. */ if (flags & MNT_UPDATE) optbuf = catopt(optbuf, "update"); /* Compatibility glue. */ if (strcmp(vfstype, "msdos") == 0) { warnx( "Using \"-t msdosfs\", since \"-t msdos\" is deprecated."); vfstype = "msdosfs"; } /* Construct the name of the appropriate mount command */ (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype); mnt_argv.c = -1; append_arg(&mnt_argv, execname); mangle(optbuf, &mnt_argv); if (mountprog != NULL) strcpy(execname, mountprog); append_arg(&mnt_argv, strdup(spec)); append_arg(&mnt_argv, strdup(name)); append_arg(&mnt_argv, NULL); if (debug) { if (use_mountprog(vfstype)) printf("exec: %s", execname); else printf("mount -t %s", vfstype); for (i = 1; i < mnt_argv.c; i++) (void)printf(" %s", mnt_argv.a[i]); (void)printf("\n"); free(optbuf); free(mountprog); mountprog = NULL; return (0); } if (use_mountprog(vfstype)) { ret = exec_mountprog(name, execname, mnt_argv.a); } else { ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a); } free(optbuf); free(mountprog); mountprog = NULL; if (verbose) { if (statfs(name, &sf) < 0) { warn("statfs %s", name); return (1); } if (fstab_style) putfsent(&sf); else prmount(&sf); } return (ret); }
/* Note: we are trying to avoid duplicating strings during codegen * which is why we have the slightly awkward * CoglPipelineFragendARBfpArg mechanism. */ static void setup_arg (CoglPipeline *pipeline, CoglPipelineLayer *layer, CoglBlendStringChannelMask mask, int arg_index, CoglPipelineCombineSource src, GLint op, CoglPipelineFragendARBfpArg *arg) { CoglPipelineShaderState *shader_state = get_shader_state (pipeline); static const char *tmp_name[3] = { "tmp0", "tmp1", "tmp2" }; switch (src) { case COGL_PIPELINE_COMBINE_SOURCE_TEXTURE: arg->type = COGL_PIPELINE_FRAGEND_ARBFP_ARG_TYPE_TEXTURE; arg->name = "texel%d"; arg->texture_unit = _cogl_pipeline_layer_get_unit_index (layer); setup_texture_source (shader_state, arg->texture_unit, _cogl_pipeline_layer_get_texture_type (layer)); break; case COGL_PIPELINE_COMBINE_SOURCE_CONSTANT: { int unit_index = _cogl_pipeline_layer_get_unit_index (layer); UnitState *unit_state = &shader_state->unit_state[unit_index]; unit_state->constant_id = shader_state->next_constant_id++; unit_state->has_combine_constant = TRUE; unit_state->dirty_combine_constant = TRUE; arg->type = COGL_PIPELINE_FRAGEND_ARBFP_ARG_TYPE_CONSTANT; arg->name = "program.local[%d]"; arg->constant_id = unit_state->constant_id; break; } case COGL_PIPELINE_COMBINE_SOURCE_PRIMARY_COLOR: arg->type = COGL_PIPELINE_FRAGEND_ARBFP_ARG_TYPE_SIMPLE; arg->name = "fragment.color.primary"; break; case COGL_PIPELINE_COMBINE_SOURCE_PREVIOUS: arg->type = COGL_PIPELINE_FRAGEND_ARBFP_ARG_TYPE_SIMPLE; if (_cogl_pipeline_layer_get_unit_index (layer) == 0) arg->name = "fragment.color.primary"; else arg->name = "output"; break; default: /* Sample the texture attached to a specific layer */ { int layer_num = src - COGL_PIPELINE_COMBINE_SOURCE_TEXTURE0; CoglPipelineGetLayerFlags flags = COGL_PIPELINE_GET_LAYER_NO_CREATE; CoglPipelineLayer *other_layer = _cogl_pipeline_get_layer_with_flags (pipeline, layer_num, flags); if (other_layer == NULL) { static CoglBool warning_seen = FALSE; if (!warning_seen) { g_warning ("The application is trying to use a texture " "combine with a layer number that does not exist"); warning_seen = TRUE; } arg->type = COGL_PIPELINE_FRAGEND_ARBFP_ARG_TYPE_SIMPLE; arg->name = "output"; } else { CoglTextureType texture_type; arg->type = COGL_PIPELINE_FRAGEND_ARBFP_ARG_TYPE_TEXTURE; arg->name = "texture[%d]"; arg->texture_unit = _cogl_pipeline_layer_get_unit_index (other_layer); texture_type = _cogl_pipeline_layer_get_texture_type (other_layer); setup_texture_source (shader_state, arg->texture_unit, texture_type); } } break; } arg->swizzle = ""; switch (op) { case COGL_PIPELINE_COMBINE_OP_SRC_COLOR: break; case COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_COLOR: g_string_append_printf (shader_state->source, "SUB tmp%d, one, ", arg_index); append_arg (shader_state->source, arg); g_string_append_printf (shader_state->source, ";\n"); arg->type = COGL_PIPELINE_FRAGEND_ARBFP_ARG_TYPE_SIMPLE; arg->name = tmp_name[arg_index]; arg->swizzle = ""; break; case COGL_PIPELINE_COMBINE_OP_SRC_ALPHA: /* avoid a swizzle if we know RGB are going to be masked * in the end anyway */ if (mask != COGL_BLEND_STRING_CHANNEL_MASK_ALPHA) arg->swizzle = ".a"; break; case COGL_PIPELINE_COMBINE_OP_ONE_MINUS_SRC_ALPHA: g_string_append_printf (shader_state->source, "SUB tmp%d, one, ", arg_index); append_arg (shader_state->source, arg); /* avoid a swizzle if we know RGB are going to be masked * in the end anyway */ if (mask != COGL_BLEND_STRING_CHANNEL_MASK_ALPHA) g_string_append_printf (shader_state->source, ".a;\n"); else g_string_append_printf (shader_state->source, ";\n"); arg->type = COGL_PIPELINE_FRAGEND_ARBFP_ARG_TYPE_SIMPLE; arg->name = tmp_name[arg_index]; break; default: g_error ("Unknown texture combine operator %d", op); break; } }
static void append_function (CoglPipeline *pipeline, CoglBlendStringChannelMask mask, GLint function, CoglPipelineFragendARBfpArg *args, int n_args) { CoglPipelineShaderState *shader_state = get_shader_state (pipeline); const char *mask_name; switch (mask) { case COGL_BLEND_STRING_CHANNEL_MASK_RGB: mask_name = ".rgb"; break; case COGL_BLEND_STRING_CHANNEL_MASK_ALPHA: mask_name = ".a"; break; case COGL_BLEND_STRING_CHANNEL_MASK_RGBA: mask_name = ""; break; default: g_error ("Unknown channel mask %d", mask); mask_name = ""; } switch (function) { case COGL_PIPELINE_COMBINE_FUNC_ADD: g_string_append_printf (shader_state->source, "ADD_SAT output%s, ", mask_name); break; case COGL_PIPELINE_COMBINE_FUNC_MODULATE: /* Note: no need to saturate since we can assume operands * have values in the range [0,1] */ g_string_append_printf (shader_state->source, "MUL output%s, ", mask_name); break; case COGL_PIPELINE_COMBINE_FUNC_REPLACE: /* Note: no need to saturate since we can assume operand * has a value in the range [0,1] */ g_string_append_printf (shader_state->source, "MOV output%s, ", mask_name); break; case COGL_PIPELINE_COMBINE_FUNC_SUBTRACT: g_string_append_printf (shader_state->source, "SUB_SAT output%s, ", mask_name); break; case COGL_PIPELINE_COMBINE_FUNC_ADD_SIGNED: g_string_append_printf (shader_state->source, "ADD tmp3%s, ", mask_name); append_arg (shader_state->source, &args[0]); g_string_append (shader_state->source, ", "); append_arg (shader_state->source, &args[1]); g_string_append (shader_state->source, ";\n"); g_string_append_printf (shader_state->source, "SUB_SAT output%s, tmp3, half", mask_name); n_args = 0; break; case COGL_PIPELINE_COMBINE_FUNC_DOT3_RGB: /* These functions are the same except that GL_DOT3_RGB never * updates the alpha channel. * * NB: GL_DOT3_RGBA is a bit special because it effectively forces * an RGBA mask and we end up ignoring any separate alpha channel * function. */ case COGL_PIPELINE_COMBINE_FUNC_DOT3_RGBA: { const char *tmp4 = "tmp4"; /* The maths for this was taken from Mesa; * apparently: * * tmp3 = 2*src0 - 1 * tmp4 = 2*src1 - 1 * output = DP3 (tmp3, tmp4) * * is the same as: * * output = 4 * DP3 (src0 - 0.5, src1 - 0.5) */ g_string_append (shader_state->source, "MAD tmp3, two, "); append_arg (shader_state->source, &args[0]); g_string_append (shader_state->source, ", minus_one;\n"); if (!fragend_arbfp_args_equal (&args[0], &args[1])) { g_string_append (shader_state->source, "MAD tmp4, two, "); append_arg (shader_state->source, &args[1]); g_string_append (shader_state->source, ", minus_one;\n"); } else tmp4 = "tmp3"; g_string_append_printf (shader_state->source, "DP3_SAT output%s, tmp3, %s", mask_name, tmp4); n_args = 0; } break; case COGL_PIPELINE_COMBINE_FUNC_INTERPOLATE: /* Note: no need to saturate since we can assume operands * have values in the range [0,1] */ /* NB: GL_INTERPOLATE = arg0*arg2 + arg1*(1-arg2) * but LRP dst, a, b, c = b*a + c*(1-a) */ g_string_append_printf (shader_state->source, "LRP output%s, ", mask_name); append_arg (shader_state->source, &args[2]); g_string_append (shader_state->source, ", "); append_arg (shader_state->source, &args[0]); g_string_append (shader_state->source, ", "); append_arg (shader_state->source, &args[1]); n_args = 0; break; default: g_error ("Unknown texture combine function %d", function); g_string_append_printf (shader_state->source, "MUL_SAT output%s, ", mask_name); n_args = 2; break; } if (n_args > 0) append_arg (shader_state->source, &args[0]); if (n_args > 1) { g_string_append (shader_state->source, ", "); append_arg (shader_state->source, &args[1]); } g_string_append (shader_state->source, ";\n"); }
int expand_arg( const char* fn, char*** arg, size_t* index, size_t* capacity, int level ) /* purpose: adds the contents of a file, line by line, to an argument vector * This is a low-level function, use add_arg instead. * paramtr: fn (IN): name of file with contents to append * arg (OUT): list of arguments as vector * index (IO): index where a new data should be inserted into * capacity (IO): capacity (extend) of vector * level (IN): level of recursion * returns: 0 means ok, -1 means error, see errno */ { FILE* f; char line[4096]; size_t len; char* cmd, *save = NULL; unsigned long lineno = 0ul; if ( level >= 32 ) { fprintf( stderr, "ERROR: Nesting too deep (%d levels), " "circuit breaker triggered!\n", level ); errno = EMLINK; return -1; } if ( (f = fopen( fn, "r" )) == NULL ) { /* error while opening file for reading */ return -1; } while ( fgets( line, sizeof(line), f ) ) { ++lineno; /* check for skippable line */ if ( line[0] == 0 || line[0] == '\r' || line[0] == '\n' ) continue; /* check for unterminated line (larger than buffer) */ len = strlen(line); if ( line[len-1] != '\r' && line[len-1] != '\n' ) { /* read buffer was too small, save and append */ char* temp = merge( save, line ); if ( temp == NULL ) { /* error while merging strings */ int saverr = errno; fclose(f); if ( save != NULL ) free((void*) save); errno = saverr; return -1; } if ( save != NULL ) free((void*) save); save = temp; lineno--; continue; } else { /* remove terminating character(s) */ while ( len > 0 && (line[len-1] == '\r' || line[len-1] == '\n') ) { line[len-1] = 0; len--; } } /* final assembly of argument */ if ( save != NULL ) { /* assemble merged line */ cmd = merge( save, line ); free((void*) save); save = NULL; if ( cmd == NULL ) { /* error while merging strings */ int saverr = errno; fclose(f); errno = saverr; return -1; } } else { /* no overlong lines */ cmd = line; } #ifdef DEBUG_ARGV printf( "# %s:%lu: %s\n", fn, lineno, cmd ); #endif /* DEBUG_ARGV */ if ( (len=strlen(cmd)) > 0 ) { int result = #ifdef PERMIT_RECURSION add_arg( cmd, arg, index, capacity, level+1 ) #else /* ! PERMIT_RECURSION */ append_arg( cmd, arg, index, capacity ) #endif /* PERMIT_RECURSION */ ; if ( result == -1 ) { int saverr = errno; fclose(f); if ( cmd != line ) free((void*) cmd); errno = saverr; return -1; } } /* done with this argument */ if ( cmd != line ) free((void*) cmd); } fclose(f); return 0; }
void rewrite_command_line (char *override_options_line, int *argc, char ***argv){ int line_pos = 0; read_args (*argc, *argv); if (override_options_line[0] == '#') { confirm_changes = 0; line_pos++; } if (confirm_changes) fprintf (stderr, "### QA_OVERRIDE_GCC3_OPTIONS: %s\n", override_options_line); /* Loop through all commands in the file */ while (override_options_line[line_pos] != '\0') { char first_char; char *searchStr; char *arg; int search_index; int arg_len; /* Any spaces in between options don't count. */ if (override_options_line[line_pos] == ' ') { line_pos++; continue; } /* The first non-space character is the command. */ first_char = override_options_line[line_pos]; line_pos++; arg_len = strcspn(override_options_line+line_pos, " "); switch (first_char) { case '+': /* Add an argument to the end of the arg list */ arg = arg_string (override_options_line, line_pos, arg_len); append_arg (arg); free (arg); break; case 'x': /* Delete a matching argument */ searchStr = arg_string(override_options_line, line_pos, arg_len); if ((search_index = find_arg(searchStr)) != -1) { delete_arg(search_index); } free (searchStr); break; case 'X': /* Delete a matching argument and the argument following. */ searchStr = arg_string(override_options_line, line_pos, arg_len); if ((search_index = find_arg(searchStr)) != -1) { if (search_index >= arg_count -1) { if (confirm_changes) fprintf(stderr,"Not enough arguments to do X\n"); } else { delete_arg(search_index); /* Delete the matching argument */ delete_arg(search_index); /* Delete the following argument */ } } free (searchStr); break; case 'O': /* Change the optimization level to the specified value, and remove any optimization arguments. This is a separate command because we often want is to substitute our favorite optimization level for whatever the project normally wants. As we probably care about this a lot (for things like testing file sizes at different optimization levels) we make a special rewrite clause. */ arg = arg_string (override_options_line, line_pos, arg_len); replace_optimization_level(arg); free (arg); break; case 's': /* Search for the regexp passed in, and replace a matching argument with the provided replacement string */ searchStr = arg_string (override_options_line, line_pos, arg_len); search_and_replace (searchStr); free (searchStr); break; default: fprintf(stderr,"### QA_OVERRIDE_GCC3_OPTIONS: invalid string (pos %d)\n", line_pos); break; } line_pos += arg_len; } *argc = arg_count; *argv = arg_array; }
int main (int argc, char *argv[]) { DBusConnection *connection; DBusError error; DBusMessage *message; dbus_bool_t print_reply; dbus_bool_t print_reply_literal; int reply_timeout; DBusMessageIter iter; int i; DBusBusType type = DBUS_BUS_SESSION; const char *dest = NULL; const char *name = NULL; const char *path = NULL; int message_type = DBUS_MESSAGE_TYPE_SIGNAL; const char *type_str = NULL; const char *address = NULL; int is_bus = FALSE; int session_or_system = FALSE; appname = argv[0]; if (argc < 3) usage (1); print_reply = FALSE; print_reply_literal = FALSE; reply_timeout = -1; for (i = 1; i < argc && name == NULL; i++) { char *arg = argv[i]; if (strcmp (arg, "--system") == 0) { type = DBUS_BUS_SYSTEM; session_or_system = TRUE; } else if (strcmp (arg, "--session") == 0) { type = DBUS_BUS_SESSION; session_or_system = TRUE; } else if ((strstr (arg, "--bus=") == arg) || (strstr (arg, "--peer=") == arg) || (strstr (arg, "--address=") == arg)) { if (arg[2] == 'b') /* bus */ { is_bus = TRUE; } else if (arg[2] == 'p') /* peer */ { is_bus = FALSE; } else /* address; keeping backwards compatibility */ { is_bus = FALSE; } address = strchr (arg, '=') + 1; if (address[0] == '\0') { fprintf (stderr, "\"--peer=\" and \"--bus=\" require an ADDRESS\n"); usage (1); } } else if (strncmp (arg, "--print-reply", 13) == 0) { print_reply = TRUE; message_type = DBUS_MESSAGE_TYPE_METHOD_CALL; if (strcmp (arg + 13, "=literal") == 0) print_reply_literal = TRUE; else if (*(arg + 13) != '\0') { fprintf (stderr, "invalid value (%s) of \"--print-reply\"\n", arg + 13); usage (1); } } else if (strstr (arg, "--reply-timeout=") == arg) { if (*(strchr (arg, '=') + 1) == '\0') { fprintf (stderr, "\"--reply-timeout=\" requires an MSEC\n"); usage (1); } reply_timeout = strtol (strchr (arg, '=') + 1, NULL, 10); if (reply_timeout <= 0) { fprintf (stderr, "invalid value (%s) of \"--reply-timeout\"\n", strchr (arg, '=') + 1); usage (1); } } else if (strstr (arg, "--dest=") == arg) { if (*(strchr (arg, '=') + 1) == '\0') { fprintf (stderr, "\"--dest=\" requires an NAME\n"); usage (1); } dest = strchr (arg, '=') + 1; } else if (strstr (arg, "--type=") == arg) type_str = strchr (arg, '=') + 1; else if (!strcmp(arg, "--help")) usage (0); else if (arg[0] == '-') usage (1); else if (path == NULL) path = arg; else /* name == NULL guaranteed by the 'while' loop */ name = arg; } if (name == NULL) usage (1); if (session_or_system && (address != NULL)) { fprintf (stderr, "\"--peer\" and \"--bus\" may not be used with \"--system\" or \"--session\"\n"); usage (1); } if (type_str != NULL) { message_type = dbus_message_type_from_string (type_str); if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL || message_type == DBUS_MESSAGE_TYPE_SIGNAL)) { fprintf (stderr, "Message type \"%s\" is not supported\n", type_str); exit (1); } } dbus_error_init (&error); if (dest && !dbus_validate_bus_name (dest, &error)) { fprintf (stderr, "invalid value (%s) of \"--dest\"\n", dest); usage (1); } if (address != NULL) { connection = dbus_connection_open (address, &error); } else { connection = dbus_bus_get (type, &error); } if (connection == NULL) { fprintf (stderr, "Failed to open connection to \"%s\" message bus: %s\n", (address != NULL) ? address : ((type == DBUS_BUS_SYSTEM) ? "system" : "session"), error.message); dbus_error_free (&error); exit (1); } else if ((address != NULL) && is_bus) { if (!dbus_bus_register (connection, &error)) { fprintf (stderr, "Failed to register on connection to \"%s\" message bus: %s\n", address, error.message); dbus_error_free (&error); exit (1); } } if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL) { char *last_dot; last_dot = strrchr (name, '.'); if (last_dot == NULL) { fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n", name); exit (1); } *last_dot = '\0'; message = dbus_message_new_method_call (NULL, path, name, last_dot + 1); dbus_message_set_auto_start (message, TRUE); } else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL) { char *last_dot; last_dot = strrchr (name, '.'); if (last_dot == NULL) { fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n", name); exit (1); } *last_dot = '\0'; message = dbus_message_new_signal (path, name, last_dot + 1); } else { fprintf (stderr, "Internal error, unknown message type\n"); exit (1); } if (message == NULL) { fprintf (stderr, "Couldn't allocate D-Bus message\n"); exit (1); } if (dest && !dbus_message_set_destination (message, dest)) { fprintf (stderr, "Not enough memory\n"); exit (1); } dbus_message_iter_init_append (message, &iter); while (i < argc) { char *arg; char *c; int type; int secondary_type; int container_type; DBusMessageIter *target_iter; DBusMessageIter container_iter; type = DBUS_TYPE_INVALID; arg = argv[i++]; c = strchr (arg, ':'); if (c == NULL) { fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg); exit (1); } *(c++) = 0; container_type = DBUS_TYPE_INVALID; if (strcmp (arg, "variant") == 0) container_type = DBUS_TYPE_VARIANT; else if (strcmp (arg, "array") == 0) container_type = DBUS_TYPE_ARRAY; else if (strcmp (arg, "dict") == 0) container_type = DBUS_TYPE_DICT_ENTRY; if (container_type != DBUS_TYPE_INVALID) { arg = c; c = strchr (arg, ':'); if (c == NULL) { fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg); exit (1); } *(c++) = 0; } if (arg[0] == 0) type = DBUS_TYPE_STRING; else type = type_from_name (arg); if (container_type == DBUS_TYPE_DICT_ENTRY) { char sig[5]; arg = c; c = strchr (c, ':'); if (c == NULL) { fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg); exit (1); } *(c++) = 0; secondary_type = type_from_name (arg); sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR; sig[1] = type; sig[2] = secondary_type; sig[3] = DBUS_DICT_ENTRY_END_CHAR; sig[4] = '\0'; dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, sig, &container_iter); target_iter = &container_iter; } else if (container_type != DBUS_TYPE_INVALID) { char sig[2]; sig[0] = type; sig[1] = '\0'; dbus_message_iter_open_container (&iter, container_type, sig, &container_iter); target_iter = &container_iter; } else target_iter = &iter; if (container_type == DBUS_TYPE_ARRAY) { append_array (target_iter, type, c); } else if (container_type == DBUS_TYPE_DICT_ENTRY) { append_dict (target_iter, type, secondary_type, c); } else append_arg (target_iter, type, c); if (container_type != DBUS_TYPE_INVALID) { dbus_message_iter_close_container (&iter, &container_iter); } } if (print_reply) { DBusMessage *reply; dbus_error_init (&error); reply = dbus_connection_send_with_reply_and_block (connection, message, reply_timeout, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "Error %s: %s\n", error.name, error.message); exit (1); } if (reply) { long sec, usec; _dbus_get_real_time (&sec, &usec); print_message (reply, print_reply_literal, sec, usec); dbus_message_unref (reply); } } else { dbus_connection_send (connection, message, NULL); dbus_connection_flush (connection); } dbus_message_unref (message); dbus_connection_unref (connection); exit (0); }