cut.c 7.7 KB

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