cut.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* vi: set sw=4 ts=4: */
  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. * debloated by Bernhard Fischer
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  10. */
  11. #include "libbb.h"
  12. /* This is a NOEXEC applet. Be very careful! */
  13. /* option vars */
  14. static const char optstring[] ALIGN1 = "b:c:f:d:sn";
  15. #define CUT_OPT_BYTE_FLGS (1<<0)
  16. #define CUT_OPT_CHAR_FLGS (1<<1)
  17. #define CUT_OPT_FIELDS_FLGS (1<<2)
  18. #define CUT_OPT_DELIM_FLGS (1<<3)
  19. #define CUT_OPT_SUPPRESS_FLGS (1<<4)
  20. static char delim = '\t'; /* delimiter, default is tab */
  21. struct cut_list {
  22. int startpos;
  23. int endpos;
  24. };
  25. enum {
  26. BOL = 0,
  27. EOL = INT_MAX,
  28. NON_RANGE = -1
  29. };
  30. /* growable array holding a series of lists */
  31. static struct cut_list *cut_lists;
  32. static unsigned int nlists; /* number of elements in above list */
  33. static int cmpfunc(const void *a, const void *b)
  34. {
  35. return (((struct cut_list *) a)->startpos -
  36. ((struct cut_list *) b)->startpos);
  37. }
  38. static void cut_file(FILE * file)
  39. {
  40. char *line = NULL;
  41. unsigned int linenum = 0; /* keep these zero-based to be consistent */
  42. /* go through every line in the file */
  43. while ((line = xmalloc_getline(file)) != NULL) {
  44. /* set up a list so we can keep track of what's been printed */
  45. char * printed = xzalloc(strlen(line) * sizeof(char));
  46. char * orig_line = line;
  47. unsigned int cl_pos = 0;
  48. int spos;
  49. /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
  50. if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) {
  51. /* print the chars specified in each cut list */
  52. for (; cl_pos < nlists; cl_pos++) {
  53. spos = cut_lists[cl_pos].startpos;
  54. while (spos < strlen(line)) {
  55. if (!printed[spos]) {
  56. printed[spos] = 'X';
  57. putchar(line[spos]);
  58. }
  59. spos++;
  60. if (spos > cut_lists[cl_pos].endpos
  61. || cut_lists[cl_pos].endpos == NON_RANGE)
  62. break;
  63. }
  64. }
  65. } else if (delim == '\n') { /* cut by lines */
  66. spos = cut_lists[cl_pos].startpos;
  67. /* get out if we have no more lists to process or if the lines
  68. * are lower than what we're interested in */
  69. if (linenum < spos || cl_pos >= nlists)
  70. goto next_line;
  71. /* if the line we're looking for is lower than the one we were
  72. * passed, it means we displayed it already, so move on */
  73. while (spos < linenum) {
  74. spos++;
  75. /* go to the next list if we're at the end of this one */
  76. if (spos > cut_lists[cl_pos].endpos
  77. || cut_lists[cl_pos].endpos == NON_RANGE) {
  78. cl_pos++;
  79. /* get out if there's no more lists to process */
  80. if (cl_pos >= nlists)
  81. goto next_line;
  82. spos = cut_lists[cl_pos].startpos;
  83. /* get out if the current line is lower than the one
  84. * we just became interested in */
  85. if (linenum < spos)
  86. goto next_line;
  87. }
  88. }
  89. /* If we made it here, it means we've found the line we're
  90. * looking for, so print it */
  91. puts(line);
  92. goto next_line;
  93. } else { /* cut by fields */
  94. int ndelim = -1; /* zero-based / one-based problem */
  95. int nfields_printed = 0;
  96. char *field = NULL;
  97. const char delimiter[2] = { delim, 0 };
  98. /* does this line contain any delimiters? */
  99. if (strchr(line, delim) == NULL) {
  100. if (!(option_mask32 & CUT_OPT_SUPPRESS_FLGS))
  101. puts(line);
  102. goto next_line;
  103. }
  104. /* process each list on this line, for as long as we've got
  105. * a line to process */
  106. for (; cl_pos < nlists && line; cl_pos++) {
  107. spos = cut_lists[cl_pos].startpos;
  108. do {
  109. /* find the field we're looking for */
  110. while (line && ndelim < spos) {
  111. field = strsep(&line, delimiter);
  112. ndelim++;
  113. }
  114. /* we found it, and it hasn't been printed yet */
  115. if (field && ndelim == spos && !printed[ndelim]) {
  116. /* if this isn't our first time through, we need to
  117. * print the delimiter after the last field that was
  118. * printed */
  119. if (nfields_printed > 0)
  120. putchar(delim);
  121. fputs(field, stdout);
  122. printed[ndelim] = 'X';
  123. nfields_printed++; /* shouldn't overflow.. */
  124. }
  125. spos++;
  126. /* keep going as long as we have a line to work with,
  127. * this is a list, and we're not at the end of that
  128. * list */
  129. } while (spos <= cut_lists[cl_pos].endpos && line
  130. && cut_lists[cl_pos].endpos != NON_RANGE);
  131. }
  132. }
  133. /* if we printed anything at all, we need to finish it with a
  134. * newline cuz we were handed a chomped line */
  135. putchar('\n');
  136. next_line:
  137. linenum++;
  138. free(printed);
  139. free(orig_line);
  140. }
  141. }
  142. static const char _op_on_field[] ALIGN1 = " only when operating on fields";
  143. int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  144. int cut_main(int argc, char **argv)
  145. {
  146. char *sopt, *ltok;
  147. opt_complementary = "b--bcf:c--bcf:f--bcf";
  148. getopt32(argv, optstring, &sopt, &sopt, &sopt, &ltok);
  149. // argc -= optind;
  150. argv += optind;
  151. if (!(option_mask32 & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS)))
  152. bb_error_msg_and_die("expected a list of bytes, characters, or fields");
  153. if (option_mask32 & CUT_OPT_DELIM_FLGS) {
  154. if (strlen(ltok) > 1) {
  155. bb_error_msg_and_die("the delimiter must be a single character");
  156. }
  157. delim = ltok[0];
  158. }
  159. /* non-field (char or byte) cutting has some special handling */
  160. if (!(option_mask32 & CUT_OPT_FIELDS_FLGS)) {
  161. if (option_mask32 & CUT_OPT_SUPPRESS_FLGS) {
  162. bb_error_msg_and_die
  163. ("suppressing non-delimited lines makes sense%s",
  164. _op_on_field);
  165. }
  166. if (delim != '\t') {
  167. bb_error_msg_and_die
  168. ("a delimiter may be specified%s", _op_on_field);
  169. }
  170. }
  171. /*
  172. * parse list and put values into startpos and endpos.
  173. * valid list formats: N, N-, N-M, -M
  174. * more than one list can be separated by commas
  175. */
  176. {
  177. char *ntok;
  178. int s = 0, e = 0;
  179. /* take apart the lists, one by one (they are separated with commas */
  180. while ((ltok = strsep(&sopt, ",")) != NULL) {
  181. /* it's actually legal to pass an empty list */
  182. if (strlen(ltok) == 0)
  183. continue;
  184. /* get the start pos */
  185. ntok = strsep(&ltok, "-");
  186. if (ntok == NULL) {
  187. bb_error_msg
  188. ("internal error: ntok is null for start pos!?\n");
  189. } else if (strlen(ntok) == 0) {
  190. s = BOL;
  191. } else {
  192. s = xatoi_u(ntok);
  193. /* account for the fact that arrays are zero based, while
  194. * the user expects the first char on the line to be char #1 */
  195. if (s != 0)
  196. s--;
  197. }
  198. /* get the end pos */
  199. ntok = strsep(&ltok, "-");
  200. if (ntok == NULL) {
  201. e = NON_RANGE;
  202. } else if (strlen(ntok) == 0) {
  203. e = EOL;
  204. } else {
  205. e = xatoi_u(ntok);
  206. /* if the user specified and end position of 0, that means "til the
  207. * end of the line */
  208. if (e == 0)
  209. e = EOL;
  210. e--; /* again, arrays are zero based, lines are 1 based */
  211. if (e == s)
  212. e = NON_RANGE;
  213. }
  214. /* if there's something left to tokenize, the user passed
  215. * an invalid list */
  216. if (ltok)
  217. bb_error_msg_and_die("invalid byte or field list");
  218. /* add the new list */
  219. cut_lists = xrealloc(cut_lists, sizeof(struct cut_list) * (++nlists));
  220. cut_lists[nlists-1].startpos = s;
  221. cut_lists[nlists-1].endpos = e;
  222. }
  223. /* make sure we got some cut positions out of all that */
  224. if (nlists == 0)
  225. bb_error_msg_and_die("missing list of positions");
  226. /* now that the lists are parsed, we need to sort them to make life
  227. * easier on us when it comes time to print the chars / fields / lines
  228. */
  229. qsort(cut_lists, nlists, sizeof(struct cut_list), cmpfunc);
  230. }
  231. /* argv[0..argc-1] should be names of file to process. If no
  232. * files were specified or '-' was specified, take input from stdin.
  233. * Otherwise, we process all the files specified. */
  234. if (argv[0] == NULL || LONE_DASH(argv[0])) {
  235. cut_file(stdin);
  236. } else {
  237. FILE *file;
  238. do {
  239. file = fopen_or_warn(argv[0], "r");
  240. if (file) {
  241. cut_file(file);
  242. fclose(file);
  243. }
  244. } while (*++argv);
  245. }
  246. if (ENABLE_FEATURE_CLEAN_UP)
  247. free(cut_lists);
  248. return EXIT_SUCCESS;
  249. }