- /* handle multi-value cases */
- else if (nminus == 1) {
- /* handle 'N-' case */
- if (list[strlen(list) - 1] == '-') {
- startpos = strtol(list, &ptr, 10);
- }
- /* handle '-M' case */
- else if (list[0] == '-') {
- endpos = strtol(&list[1], NULL, 10);
- }
- /* handle 'N-M' case */
- else {
- startpos = strtol(list, &ptr, 10);
- endpos = strtol(ptr+1, &ptr, 10);
- }
+ putchar('\n'); /* cuz we were handed a chomped line */
+ free(printed);
+}
+
+
+static void cut_line_by_fields(char *line)
+{
+ int c, f;
+ int ndelim = -1; /* zero-based / one-based problem */
+ int nfields_printed = 0;
+ char *field = NULL;
+ char d[2] = { delim, 0 };
+ char *printed;
+
+ /* test the easy case first: does this line contain any delimiters? */
+ if (strchr(line, delim) == NULL) {
+ if (!supress_non_delimited_lines)
+ puts(line);
+ return;
+ }
+
+ /* set up a list so we can keep track of what's been printed */
+ printed = xcalloc(strlen(line), sizeof(char));
+
+ /* process each list on this line, for as long as we've got a line to process */
+ for (c = 0; c < nlists && line; c++) {
+ f = cut_lists[c].startpos;
+ do {
+
+ /* find the field we're looking for */
+ while (line && ndelim < f) {
+ field = strsep(&line, d);
+ ndelim++;
+ }
+
+ /* we found it, and it hasn't been printed yet */
+ if (field && ndelim == f && !printed[ndelim]) {
+ /* if this isn't our first time through, we need to print the
+ * delimiter after the last field that was printed */
+ if (nfields_printed > 0)
+ putchar(delim);
+ fputs(field, stdout);
+ printed[ndelim] = 'X';
+ nfields_printed++;
+ }
+
+ f++;
+
+ /* keep going as long as we have a line to work with, this is a
+ * list, and we're not at the end of that list */
+ } while (line && cut_lists[c].endpos != NON_RANGE && f <= cut_lists[c].endpos);
+ }
+
+ /* if we printed anything at all, we need to finish it with a newline cuz
+ * we were handed a chomped line */
+ putchar('\n');
+
+ free(printed);
+}