/* Perform the same operation as bind_array_variable, but with VALUE * being a number, not a string. */ SHELL_VAR * mpibash_bind_array_variable_number (char *name, arrayind_t ind, long value, int flags) { char numstr[25]; /* String version of VALUE */ sprintf (numstr, "%ld", value); return bind_array_variable (name, ind, numstr, flags); }
/* The function implementing the builtin. It uses internal_getopt to parse options. It is the same as getopt(3), but it takes a pointer to a WORD_LIST. If the builtin takes no options, call no_options(list) before doing anything else. If it returns a non-zero value, your builtin should immediately return EX_USAGE. A builtin command returns EXECUTION_SUCCESS for success and EXECUTION_FAILURE to indicate failure. */ int readrec_builtin (WORD_LIST *list) { SHELL_VAR *var; rec_parser_t parser; rec_record_t record; no_options (list); /* Create a librec parser to operate on the standard input and try to read a record. If there is a parse error then report it and fail. */ parser = rec_parser_new (stdin, "stdin"); if (!parser) return EXECUTION_FAILURE; if (!rec_parse_record (parser, &record)) { return EXECUTION_FAILURE; } { size_t record_str_size = 0; char *record_str = NULL; char *record_str_dequoted = NULL; rec_writer_t writer = rec_writer_new_str (&record_str, &record_str_size); if (!writer || !rec_write_record (writer, record)) return EXIT_FAILURE; rec_writer_destroy (writer); /* Set the REPLY_REC environment variable to the read record. */ record_str_dequoted = dequote_string (record_str); var = bind_variable ("REPLY_REC", record_str_dequoted, 0); VUNSETATTR (var, att_invisible); xfree (record_str_dequoted); /* Set the environment variables for the fields. */ { rec_field_t field = NULL; rec_mset_iterator_t iter = rec_mset_iterator (rec_record_mset (record)); // rec_record_reset_marks (record); while (rec_mset_iterator_next (&iter, MSET_FIELD, (const void **) &field, NULL)) { char *var_name = rec_field_name (field); size_t num_fields = rec_record_get_num_fields_by_name (record, var_name); // if (rec_record_field_mark (record, field)) // continue; #if defined ARRAY_VARS if (num_fields > 1) { /* In case several fields share the same field name, create an array variable containing all the values. */ size_t i = 0; for (; i < num_fields; i++) { // rec_record_mark_field (record, field, true); field = rec_record_get_field_by_name (record, var_name, i); var = bind_array_variable (var_name, i, rec_field_value (field), 0); VUNSETATTR (var, att_invisible); } } else { /* Bind a normal variable. */ char *var_value = rec_field_value (field); var = bind_variable (var_name, var_value, 0); VUNSETATTR (var, att_invisible); } #endif /* ARRAY_VARS */ } rec_mset_iterator_free (&iter); } } return EXECUTION_SUCCESS; }