cut.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* vi: set sw=8 ts=8: */
  2. /*
  3. * cut.c - minimalist version of cut
  4. *
  5. * Copyright (C) 1999,2000,2001 by Lineo, inc.
  6. * Written by Mark Whitley <markw@codepoet.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <getopt.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <limits.h>
  29. #include "busybox.h"
  30. /* option vars */
  31. static const char optstring[] = "b:c:f:d:sn";
  32. #define OPT_BYTE_FLGS 1
  33. #define OPT_CHAR_FLGS 2
  34. #define OPT_FIELDS_FLGS 4
  35. #define OPT_DELIM_FLGS 8
  36. #define OPT_SUPRESS_FLGS 16
  37. static char part; /* (b)yte, (c)har, (f)ields */
  38. static unsigned int supress_non_delimited_lines;
  39. static char delim = '\t'; /* delimiter, default is tab */
  40. struct cut_list {
  41. int startpos;
  42. int endpos;
  43. };
  44. static const int BOL = 0;
  45. static const int EOL = INT_MAX;
  46. static const int NON_RANGE = -1;
  47. static struct cut_list *cut_lists = NULL; /* growable array holding a series of lists */
  48. static unsigned int nlists = 0; /* number of elements in above list */
  49. static int cmpfunc(const void *a, const void *b)
  50. {
  51. struct cut_list *la = (struct cut_list *)a;
  52. struct cut_list *lb = (struct cut_list *)b;
  53. if (la->startpos > lb->startpos)
  54. return 1;
  55. if (la->startpos < lb->startpos)
  56. return -1;
  57. return 0;
  58. }
  59. /*
  60. * parse_lists() - parses a list and puts values into startpos and endpos.
  61. * valid list formats: N, N-, N-M, -M
  62. * more than one list can be separated by commas
  63. */
  64. static void parse_lists(char *lists)
  65. {
  66. char *ltok = NULL;
  67. char *ntok = NULL;
  68. char *junk;
  69. int s = 0, e = 0;
  70. /* take apart the lists, one by one (they are separated with commas */
  71. while ((ltok = strsep(&lists, ",")) != NULL) {
  72. /* it's actually legal to pass an empty list */
  73. if (strlen(ltok) == 0)
  74. continue;
  75. /* get the start pos */
  76. ntok = strsep(&ltok, "-");
  77. if (ntok == NULL) {
  78. fprintf(stderr, "Help ntok is null for starting position! What do I do?\n");
  79. } else if (strlen(ntok) == 0) {
  80. s = BOL;
  81. } else {
  82. s = strtoul(ntok, &junk, 10);
  83. if(*junk != '\0' || s < 0)
  84. bb_error_msg_and_die("invalid byte or field list");
  85. /* account for the fact that arrays are zero based, while the user
  86. * expects the first char on the line to be char # 1 */
  87. if (s != 0)
  88. s--;
  89. }
  90. /* get the end pos */
  91. ntok = strsep(&ltok, "-");
  92. if (ntok == NULL) {
  93. e = NON_RANGE;
  94. } else if (strlen(ntok) == 0) {
  95. e = EOL;
  96. } else {
  97. e = strtoul(ntok, &junk, 10);
  98. if(*junk != '\0' || e < 0)
  99. bb_error_msg_and_die("invalid byte or field list");
  100. /* if the user specified and end position of 0, that means "til the
  101. * end of the line */
  102. if (e == 0)
  103. e = INT_MAX;
  104. e--; /* again, arrays are zero based, lines are 1 based */
  105. if (e == s)
  106. e = NON_RANGE;
  107. }
  108. /* if there's something left to tokenize, the user past an invalid list */
  109. if (ltok)
  110. bb_error_msg_and_die("invalid byte or field list");
  111. /* add the new list */
  112. cut_lists = xrealloc(cut_lists, sizeof(struct cut_list) * (++nlists));
  113. cut_lists[nlists-1].startpos = s;
  114. cut_lists[nlists-1].endpos = e;
  115. }
  116. /* make sure we got some cut positions out of all that */
  117. if (nlists == 0)
  118. bb_error_msg_and_die("missing list of positions");
  119. /* now that the lists are parsed, we need to sort them to make life easier
  120. * on us when it comes time to print the chars / fields / lines */
  121. qsort(cut_lists, nlists, sizeof(struct cut_list), cmpfunc);
  122. }
  123. static void cut_line_by_chars(const char *line)
  124. {
  125. int c, l;
  126. /* set up a list so we can keep track of what's been printed */
  127. char *printed = xcalloc(strlen(line), sizeof(char));
  128. /* print the chars specified in each cut list */
  129. for (c = 0; c < nlists; c++) {
  130. l = cut_lists[c].startpos;
  131. while (l < strlen(line)) {
  132. if (!printed[l]) {
  133. putchar(line[l]);
  134. printed[l] = 'X';
  135. }
  136. l++;
  137. if (cut_lists[c].endpos == NON_RANGE || l > cut_lists[c].endpos)
  138. break;
  139. }
  140. }
  141. putchar('\n'); /* cuz we were handed a chomped line */
  142. free(printed);
  143. }
  144. static void cut_line_by_fields(char *line)
  145. {
  146. int c, f;
  147. int ndelim = -1; /* zero-based / one-based problem */
  148. int nfields_printed = 0;
  149. char *field = NULL;
  150. char d[2] = { delim, 0 };
  151. char *printed;
  152. /* test the easy case first: does this line contain any delimiters? */
  153. if (strchr(line, delim) == NULL) {
  154. if (!supress_non_delimited_lines)
  155. puts(line);
  156. return;
  157. }
  158. /* set up a list so we can keep track of what's been printed */
  159. printed = xcalloc(strlen(line), sizeof(char));
  160. /* process each list on this line, for as long as we've got a line to process */
  161. for (c = 0; c < nlists && line; c++) {
  162. f = cut_lists[c].startpos;
  163. do {
  164. /* find the field we're looking for */
  165. while (line && ndelim < f) {
  166. field = strsep(&line, d);
  167. ndelim++;
  168. }
  169. /* we found it, and it hasn't been printed yet */
  170. if (field && ndelim == f && !printed[ndelim]) {
  171. /* if this isn't our first time through, we need to print the
  172. * delimiter after the last field that was printed */
  173. if (nfields_printed > 0)
  174. putchar(delim);
  175. fputs(field, stdout);
  176. printed[ndelim] = 'X';
  177. nfields_printed++;
  178. }
  179. f++;
  180. /* keep going as long as we have a line to work with, this is a
  181. * list, and we're not at the end of that list */
  182. } while (line && cut_lists[c].endpos != NON_RANGE && f <= cut_lists[c].endpos);
  183. }
  184. /* if we printed anything at all, we need to finish it with a newline cuz
  185. * we were handed a chomped line */
  186. putchar('\n');
  187. free(printed);
  188. }
  189. static void cut_file_by_lines(const char *line, unsigned int linenum)
  190. {
  191. static int c = 0;
  192. static int l = -1;
  193. /* I can't initialize this above cuz the "initializer isn't
  194. * constant" *sigh* */
  195. if (l == -1)
  196. l = cut_lists[c].startpos;
  197. /* get out if we have no more lists to process or if the lines are lower
  198. * than what we're interested in */
  199. if (c >= nlists || linenum < l)
  200. return;
  201. /* if the line we're looking for is lower than the one we were passed, it
  202. * means we displayed it already, so move on */
  203. while (l < linenum) {
  204. l++;
  205. /* move on to the next list if we're at the end of this one */
  206. if (cut_lists[c].endpos == NON_RANGE || l > cut_lists[c].endpos) {
  207. c++;
  208. /* get out if there's no more lists to process */
  209. if (c >= nlists)
  210. return;
  211. l = cut_lists[c].startpos;
  212. /* get out if the current line is lower than the one we just became
  213. * interested in */
  214. if (linenum < l)
  215. return;
  216. }
  217. }
  218. /* If we made it here, it means we've found the line we're looking for, so print it */
  219. puts(line);
  220. }
  221. /*
  222. * snippy-snip
  223. */
  224. static void cut_file(FILE *file)
  225. {
  226. char *line = NULL;
  227. unsigned int linenum = 0; /* keep these zero-based to be consistent */
  228. /* go through every line in the file */
  229. while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
  230. /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
  231. if ((part & (OPT_CHAR_FLGS | OPT_BYTE_FLGS)))
  232. cut_line_by_chars(line);
  233. /* cut based on fields */
  234. else {
  235. if (delim == '\n')
  236. cut_file_by_lines(line, linenum);
  237. else
  238. cut_line_by_fields(line);
  239. }
  240. linenum++;
  241. free(line);
  242. }
  243. }
  244. extern int cut_main(int argc, char **argv)
  245. {
  246. unsigned long opt;
  247. char *sopt, *sdopt;
  248. bb_opt_complementaly = "b~bcf:c~bcf:f~bcf";
  249. opt = bb_getopt_ulflags(argc, argv, optstring, &sopt, &sopt, &sopt, &sdopt);
  250. part = opt & (OPT_BYTE_FLGS|OPT_CHAR_FLGS|OPT_FIELDS_FLGS);
  251. if(part == 0)
  252. bb_error_msg_and_die("you must specify a list of bytes, characters, or fields");
  253. if(opt & BB_GETOPT_ERROR)
  254. bb_error_msg_and_die("only one type of list may be specified");
  255. parse_lists(sopt);
  256. if((opt & (OPT_DELIM_FLGS))) {
  257. if (strlen(sdopt) > 1) {
  258. bb_error_msg_and_die("the delimiter must be a single character");
  259. }
  260. delim = sdopt[0];
  261. }
  262. supress_non_delimited_lines = opt & OPT_SUPRESS_FLGS;
  263. /* non-field (char or byte) cutting has some special handling */
  264. if (part != OPT_FIELDS_FLGS) {
  265. if (supress_non_delimited_lines) {
  266. bb_error_msg_and_die("suppressing non-delimited lines makes sense"
  267. " only when operating on fields");
  268. }
  269. if (delim != '\t') {
  270. bb_error_msg_and_die("a delimiter may be specified only when operating on fields");
  271. }
  272. }
  273. /* argv[(optind)..(argc-1)] should be names of file to process. If no
  274. * files were specified or '-' was specified, take input from stdin.
  275. * Otherwise, we process all the files specified. */
  276. if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
  277. cut_file(stdin);
  278. }
  279. else {
  280. int i;
  281. FILE *file;
  282. for (i = optind; i < argc; i++) {
  283. file = bb_wfopen(argv[i], "r");
  284. if(file) {
  285. cut_file(file);
  286. fclose(file);
  287. }
  288. }
  289. }
  290. return EXIT_SUCCESS;
  291. }