annotate toys/posix/cut.c @ 1483:434c4ae19f05 draft

Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
author Rob Landley <rob@landley.net>
date Thu, 18 Sep 2014 18:07:58 -0500
parents 91767d247a50
children 3ac823675413
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* cut.c - Cut from a file.
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com>
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
4 * Copyright 2012 Kyungwan Han <asura321@gmail.com>
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cut.html
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
7
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
8 USE_CUT(NEWTOY(cut, "b:|c:|f:|d:sn[!cbf]", TOYFLAG_BIN))
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
9
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
10 config CUT
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
11 bool "cut"
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
12 default y
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
13 help
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
14 usage: cut OPTION... [FILE]...
1333
fc1bb49e58a9 Help text should have a blank line after usage: lines, and a couple other whitespace tweaks.
Rob Landley <rob@landley.net>
parents: 1134
diff changeset
15
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
16 Print selected parts of lines from each FILE to standard output.
1333
fc1bb49e58a9 Help text should have a blank line after usage: lines, and a couple other whitespace tweaks.
Rob Landley <rob@landley.net>
parents: 1134
diff changeset
17
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
18 -b LIST select only these bytes from LIST.
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
19 -c LIST select only these characters from LIST.
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
20 -f LIST select only these fields.
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
21 -d DELIM use DELIM instead of TAB for field delimiter.
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
22 -s do not print lines not containing delimiters.
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
23 -n don't split multibyte characters (Ignored).
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
24 */
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #define FOR_cut
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #include "toys.h"
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
27
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
28 GLOBALS(
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
29 char *delim;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
30 char *flist;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
31 char *clist;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
32 char *blist;
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
33
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
34 void *slist_head;
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
35 unsigned nelem;
1483
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
36 void (*do_cut)(int fd);
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
37 )
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
38
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
39 struct slist {
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
40 struct slist *next;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
41 int start, end;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
42 };
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
43
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
44 static void add_to_list(int start, int end)
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
45 {
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
46 struct slist *current, *head_ref, *temp1_node;
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
47
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
48 head_ref = TT.slist_head;
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
49 temp1_node = xzalloc(sizeof(struct slist));
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
50 temp1_node->start = start;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
51 temp1_node->end = end;
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
52
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
53 /* Special case for the head end */
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
54 if (!head_ref || head_ref->start >= start) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
55 temp1_node->next = head_ref;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
56 head_ref = temp1_node;
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
57 } else {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
58 /* Locate the node before the point of insertion */
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
59 current = head_ref;
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
60 while (current->next && current->next->start < temp1_node->start)
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
61 current = current->next;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
62 temp1_node->next = current->next;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
63 current->next = temp1_node;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
64 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
65 TT.slist_head = head_ref;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
66 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
67
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
68 // parse list and add to slist.
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
69 static void parse_list(char *list)
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
70 {
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
71 for (;;) {
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
72 char *ctoken = strsep(&list, ","), *dtoken;
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
73 int start = 0, end = INT_MAX;
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
74
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
75 if (!ctoken) break;
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
76 if (!*ctoken) continue;
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
77
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
78 //Get start position.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
79 if (*(dtoken = strsep(&ctoken, "-"))) {
1134
ff22f24c9661 Fix cut.
Rob Landley <rob@landley.net>
parents: 1067
diff changeset
80 start = atolx_range(dtoken, 0, INT_MAX);
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
81 start = (start?(start-1):start);
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
82 }
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
83
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
84 //Get end position.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
85 if (!ctoken) end = -1; //case e.g. 1,2,3
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
86 else if (*ctoken) {//case e.g. N-M
1134
ff22f24c9661 Fix cut.
Rob Landley <rob@landley.net>
parents: 1067
diff changeset
87 end = atolx_range(ctoken, 0, INT_MAX);
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
88 if (!end) end = INT_MAX;
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
89 end--;
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
90 if(end == start) end = -1;
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
91 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
92 add_to_list(start, end);
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
93 TT.nelem++;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
94 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
95 //if list is missing in command line.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
96 if (!TT.nelem) error_exit("missing positions list");
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
97 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
98
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
99 /*
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
100 * retrive data from the file/s.
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
101 */
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
102 static void get_data(void)
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
103 {
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
104 char **argv = toys.optargs; //file name.
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
105 toys.exitval = EXIT_SUCCESS;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
106
1483
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
107 if(!*argv) TT.do_cut(0); //for stdin
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
108 else {
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
109 for(; *argv; ++argv) {
1483
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
110 if(strcmp(*argv, "-") == 0) TT.do_cut(0); //for stdin
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
111 else {
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
112 int fd = open(*argv, O_RDONLY, 0);
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
113 if(fd < 0) {//if file not present then continue with other files.
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
114 perror_msg(*argv);
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
115 continue;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
116 }
1483
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
117 TT.do_cut(fd);
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
118 xclose(fd);
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
119 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
120 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
121 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
122 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
123
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
124 // perform cut operation on the given delimiter.
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
125 static void do_fcut(int fd)
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
126 {
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
127 char *buff, *pfield = 0, *delimiter = TT.delim;
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
128
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
129 for (;;) {
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
130 unsigned cpos = 0;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
131 int start, ndelimiters = -1;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
132 int nprinted_fields = 0;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
133 struct slist *temp_node = TT.slist_head;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
134
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
135 free(pfield);
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
136 pfield = 0;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
137
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
138 if (!(buff = get_line(fd))) break;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
139
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
140 //does line have any delimiter?.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
141 if (strrchr(buff, (int)delimiter[0]) == NULL) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
142 //if not then print whole line and move to next line.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
143 if (!(toys.optflags & FLAG_s)) xputs(buff);
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
144 continue;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
145 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
146
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
147 pfield = xzalloc(strlen(buff) + 1);
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
148
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
149 if (temp_node) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
150 //process list on each line.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
151 while (cpos < TT.nelem && buff) {
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
152 if (!temp_node) break;
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
153 start = temp_node->start;
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
154 do {
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
155 char *field = 0;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
156
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
157 //count number of delimeters per line.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
158 while (buff) {
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
159 if (ndelimiters < start) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
160 ndelimiters++;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
161 field = strsep(&buff, delimiter);
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
162 } else break;
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
163 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
164 //print field (if not yet printed).
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
165 if (!pfield[ndelimiters]) {
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
166 if (ndelimiters == start) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
167 //put delimiter.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
168 if (nprinted_fields++ > 0) xputc(delimiter[0]);
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
169 if (field) fputs(field, stdout);
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
170 //make sure this field won't print again.
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
171 pfield[ndelimiters] = (char) 0x23; //put some char at this position.
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
172 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
173 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
174 start++;
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
175 if ((temp_node->end < 0) || !buff) break;
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
176 } while(start <= temp_node->end);
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
177 temp_node = temp_node->next;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
178 cpos++;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
179 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
180 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
181 xputc('\n');
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
182 }
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
183 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
184
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
185 // perform cut operation char or byte.
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
186 static void do_bccut(int fd)
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
187 {
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
188 char *buff;
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
189
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
190 while ((buff = get_line(fd)) != NULL) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
191 unsigned cpos = 0;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
192 int buffln = strlen(buff);
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
193 char *pfield = xzalloc(buffln + 1);
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
194 struct slist *temp_node = TT.slist_head;
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
195
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
196 if (temp_node != NULL) {
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
197 while (cpos < TT.nelem) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
198 int start;
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
199
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
200 if (!temp_node) break;
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
201 start = temp_node->start;
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
202 while (start < buffln) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
203 //to avoid duplicate field printing.
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
204 if (pfield[start]) {
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
205 if (++start <= temp_node->end) continue;
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
206 temp_node = temp_node->next;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
207 break;
1067
2bfdd63382b4 First pass of cut cleanup, and make test script slightly happier with the concept of 80 character lines.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
208 } else {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
209 //make sure this field won't print again.
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
210 pfield[start] = (char) 0x23; //put some char at this position.
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
211 xputc(buff[start]);
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
212 }
1477
91767d247a50 Cleanup pass on cut, more to do.
Rob Landley <rob@landley.net>
parents: 1333
diff changeset
213 if (++start > temp_node->end) {
706
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
214 temp_node = temp_node->next;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
215 break;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
216 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
217 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
218 cpos++;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
219 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
220 xputc('\n');
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
221 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
222 free(pfield);
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
223 pfield = NULL;
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
224 }
35a9f9c5b53f Commit 698 adding cut should ahve included the actual cut.c file. (Oops.)
Rob Landley <rob@landley.net>
parents:
diff changeset
225 }
1483
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
226
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
227 void cut_main(void)
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
228 {
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
229 char delimiter = '\t'; //default delimiter.
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
230 char *list;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
231
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
232 TT.nelem = 0;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
233 TT.slist_head = NULL;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
234
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
235 //Get list and assign the function.
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
236 if (toys.optflags & FLAG_f) {
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
237 list = TT.flist;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
238 TT.do_cut = do_fcut;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
239 } else if (toys.optflags & FLAG_c) {
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
240 list = TT.clist;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
241 TT.do_cut = do_bccut;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
242 } else {
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
243 list = TT.blist;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
244 TT.do_cut = do_bccut;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
245 }
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
246
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
247 if (toys.optflags & FLAG_d) {
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
248 //delimiter must be 1 char.
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
249 if(TT.delim[0] && TT.delim[1])
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
250 perror_exit("the delimiter must be a single character");
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
251 delimiter = TT.delim[0];
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
252 }
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
253
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
254 if(!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) {
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
255 TT.delim = xzalloc(2);
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
256 TT.delim[0] = delimiter;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
257 }
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
258
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
259 //when field is not specified, cutting has some special handling.
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
260 if (!(toys.optflags & FLAG_f)) {
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
261 if (toys.optflags & FLAG_s)
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
262 perror_exit("suppressing non-delimited lines operating on fields");
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
263 if (delimiter != '\t')
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
264 perror_exit("an input delimiter may be specified only when operating on fields");
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
265 }
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
266
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
267 parse_list(list);
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
268 get_data();
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
269 if (!(toys.optflags & FLAG_d) && (toys.optflags & FLAG_f)) {
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
270 free(TT.delim);
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
271 TT.delim = NULL;
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
272 }
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
273 llist_traverse(TT.slist_head, free);
434c4ae19f05 Reorder functions to get rid of unnecessary prototypes, and move a global into GLOBALS.
Rob Landley <rob@landley.net>
parents: 1477
diff changeset
274 }