This patch from Adam Heath <doogie@debian.org>, makes print_file
[oweals/busybox.git] / cut.c
1 /*
2  * cut.c - minimalist version of cut
3  *
4  * Copyright (C) 1999,2000,2001 by Lineo, inc.
5  * Written by Mark Whitley <markw@lineo.com>, <markw@codepoet.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h> /* getopt */
26 #include <string.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include "busybox.h"
30
31
32 /* globals from other files */
33 extern int optind;
34 extern char *optarg;
35
36
37 /* globals in this file only */
38 static char part = 0; /* (b)yte, (c)har, (f)ields */
39 static int startpos = 1;
40 static int endpos = -1;
41 static char delim = '\t'; /* delimiter, default is tab */
42 static unsigned int supress_non_delimited_lines = 0;
43
44
45 /*
46  * decompose_list() - parses a list and puts values into startpos and endpos.
47  * valid list formats: N, N-, N-M, -M 
48  */
49 static void decompose_list(const char *list)
50 {
51         unsigned int nminus = 0;
52         char *ptr;
53
54         /* the list must contain only digits and no more than one minus sign */
55         for (ptr = (char *)list; *ptr; ptr++) {
56                 if (!isdigit(*ptr) && *ptr != '-') {
57                         error_msg_and_die("invalid byte or field list");
58                 }
59                 if (*ptr == '-') {
60                         nminus++;
61                         if (nminus > 1) {
62                                 error_msg_and_die("invalid byte or field list");
63                         }
64                 }
65         }
66
67         /* handle single value 'N' case */
68         if (nminus == 0) {
69                 startpos = strtol(list, &ptr, 10);
70                 if (startpos == 0) {
71                         error_msg_and_die("missing list of fields");
72                 }
73                 endpos = startpos;
74         }
75         /* handle multi-value cases */
76         else if (nminus == 1) {
77                 /* handle 'N-' case */
78                 if (last_char_is((char *)list,'-')) {
79                         startpos = strtol(list, &ptr, 10);
80                 }
81                 /* handle '-M' case */
82                 else if (list[0] == '-') {
83                         endpos = strtol(&list[1], NULL, 10);
84                 }
85                 /* handle 'N-M' case */
86                 else {
87                         startpos = strtol(list, &ptr, 10);
88                         endpos = strtol(ptr+1, &ptr, 10);
89                 }
90
91                 /* a sanity check */
92                 if (startpos == 0) {
93                         startpos = 1;
94                 }
95         }
96 }
97
98
99 /*
100  * snippy-snip
101  */
102 static void cut_file(FILE *file)
103 {
104         char *line;
105         unsigned int cr_hits = 0;
106
107         /* go through every line in the file */
108         for (line = NULL; (line = get_line_from_file(file)) != NULL; free(line)) {
109                 /* cut based on chars/bytes */
110                 if (part == 'c' || part == 'b') {
111                         chomp(line);
112                         if (0 < endpos && endpos < strlen(line))
113                                 line[endpos] = '\0';
114                         puts(line + startpos - 1);
115                 } 
116                 /* cut based on fields */
117                 else if (part == 'f') {
118                         char *ptr;
119                         char *start = line;
120                         unsigned int delims_hit = 0;
121
122                         if (delim == '\n') {
123                                 cr_hits++;
124                                 if (cr_hits >= startpos && cr_hits <= endpos) {
125                                         while (*start && *start != '\n') {
126                                                 fputc(*start, stdout);
127                                                 start++;
128                                         }
129                                         fputc('\n', stdout);
130                                 }
131                         }
132                         else {
133                                 for (ptr = line; (ptr = strchr(ptr, delim)) != NULL; ptr++) {
134                                         delims_hit++;
135                                         if (delims_hit == (startpos - 1)) {
136                                                 start = ptr+1;
137                                         }
138                                         if (delims_hit == endpos) {
139                                                 break;
140                                         }
141                                 }
142                         
143                                 /* we didn't hit any delimeters */
144                                 if (delims_hit == 0 && !supress_non_delimited_lines) {
145                                         fputs(line, stdout);
146                                 }
147                                 /* we =did= hit some delimiters */
148                                 else if (delims_hit > 0) {
149                                         /* we have a fixed end point */
150                                         if (ptr) {
151                                                 while (start < ptr) {
152                                                         fputc(*start, stdout);
153                                                         start++;
154                                                 }
155                                                 fputc('\n', stdout);
156                                         }
157                                         /* or we're just going til the end of the line */
158                                         else {
159                                                 while (*start) {
160                                                         fputc(*start, stdout);
161                                                         start++;
162                                                 }
163                                         }
164                                 }
165                         }
166                 }
167         }
168 }
169
170 extern int cut_main(int argc, char **argv)
171 {
172         int opt;
173
174         while ((opt = getopt(argc, argv, "b:c:d:f:ns")) > 0) {
175                 switch (opt) {
176                         case 'b':
177                         case 'c':
178                         case 'f':
179                                 /* make sure they didn't ask for two types of lists */
180                                 if (part != 0) {
181                                         error_msg_and_die("only one type of list may be specified");
182                                 }
183                                 part = (char)opt;
184                                 decompose_list(optarg);
185                                 break;
186                         case 'd':
187                                 if (strlen(optarg) > 1) {
188                                         error_msg_and_die("the delimiter must be a single character");
189                                 }
190                                 delim = optarg[0];
191                                 break;
192                         case 'n':
193                                 /* no-op */
194                                 break;
195                         case 's':
196                                 supress_non_delimited_lines++;
197                                 break;
198                 }
199         }
200
201         if (part == 0) {
202                 error_msg_and_die("you must specify a list of bytes, characters, or fields");
203         }
204
205         if (supress_non_delimited_lines && part != 'f') {
206                 error_msg_and_die("suppressing non-delimited lines makes sense"
207                                 " only when operating on fields");
208
209         }
210
211         if (delim != '\t' && part != 'f') {
212                 error_msg_and_die("a delimiter may be specified only when operating on fields");
213         }
214
215         /* argv[(optind)..(argc-1)] should be names of file to process. If no
216          * files were specified or '-' was specified, take input from stdin.
217          * Otherwise, we process all the files specified. */
218         if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
219                 cut_file(stdin);
220         }
221         else {
222                 int i;
223                 FILE *file;
224                 for (i = optind; i < argc; i++) {
225                         file = fopen(argv[i], "r");
226                         if (file == NULL) {
227                                 perror_msg("%s", argv[i]);
228                         } else {
229                                 cut_file(file);
230                                 fclose(file);
231                         }
232                 }
233         }
234
235         return EXIT_SUCCESS;
236 }