summaryrefslogtreecommitdiff
path: root/field.c
diff options
context:
space:
mode:
Diffstat (limited to 'field.c')
-rw-r--r--field.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/field.c b/field.c
index 7f20b69c..44c153dc 100644
--- a/field.c
+++ b/field.c
@@ -114,6 +114,15 @@ init_fields()
field0_valid = true;
}
+/* init_csv_fields --- set up to handle --csv */
+
+void
+init_csv_fields(void)
+{
+ if (do_csv)
+ parse_field = comma_parse_field;
+}
+
/* grow_fields --- acquire new fields as needed */
static void
@@ -771,6 +780,7 @@ sc_parse_field(long up_to, /* parse only up to this field number */
* via (*parse_field)(). This variation is for when FS is a comma,
* we do very basic CSV parsing, the same as BWK awk.
*/
+
static long
comma_parse_field(long up_to, /* parse only up to this field number */
char **buf, /* on input: string to parse; on output: point to start next */
@@ -1285,11 +1295,29 @@ do_patsplit(int nargs)
static void
set_parser(parse_field_func_t func)
{
+ /*
+ * Setting FS does nothing if CSV mode, warn in that case,
+ * but don't warn on first call which happens at initialization.
+ */
+ static bool first_time = true;
+ static bool warned = false;
+
+ if (! first_time && do_csv) {
+ if (! warned) {
+ warned = true;
+ warning(_("assignment to FS/FIELDWIDTHS/FPAT has no effect when using --csv"));
+ }
+ return;
+ }
+
normal_parse_field = func;
if (! api_parser_override && parse_field != func) {
parse_field = func;
update_PROCINFO_str("FS", current_field_sep_str());
}
+
+ if (first_time)
+ first_time = false;
}
/* set_FIELDWIDTHS --- handle an assignment to FIELDWIDTHS */
@@ -1503,8 +1531,6 @@ choose_fs_function:
else if (fs->stptr[0] == '\\')
/* same special case */
strcpy(buf, "[\\\\]");
- else if (fs->stptr[0] == ',' && ! do_posix)
- set_parser(comma_parse_field);
else
set_parser(sc_parse_field);
}