cut.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. 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. const char delimiter[2] = { delim, 0 };
  100. /* does this line contain any delimiters? */
  101. if (strchr(line, delim) == NULL) {
  102. if (!(option_mask32 & CUT_OPT_SUPPRESS_FLGS))
  103. puts(line);
  104. goto next_line;
  105. }
  106. /* process each list on this line, for as long as we've got
  107. * a line to process */
  108. for (; cl_pos < nlists && line; cl_pos++) {
  109. spos = cut_lists[cl_pos].startpos;
  110. do {
  111. /* find the field we're looking for */
  112. while (line && ndelim < spos) {
  113. field = strsep(&line, delimiter);
  114. ndelim++;
  115. }
  116. /* we found it, and it hasn't been printed yet */
  117. if (field && ndelim == spos && !printed[ndelim]) {
  118. /* if this isn't our first time through, we need to
  119. * print the delimiter after the last field that was
  120. * printed */
  121. if (nfields_printed > 0)
  122. putchar(delim);
  123. fputs(field, stdout);
  124. printed[ndelim] = 'X';
  125. nfields_printed++; /* shouldn't overflow.. */
  126. }
  127. spos++;
  128. /* keep going as long as we have a line to work with,
  129. * this is a list, and we're not at the end of that
  130. * list */
  131. } while (spos <= cut_lists[cl_pos].endpos && line
  132. && cut_lists[cl_pos].endpos != NON_RANGE);
  133. }
  134. }
  135. /* if we printed anything at all, we need to finish it with a
  136. * newline cuz we were handed a chomped line */
  137. putchar('\n');
  138. next_line:
  139. linenum++;
  140. free(printed);
  141. free(orig_line);
  142. }
  143. }
  144. int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  145. int cut_main(int argc UNUSED_PARAM, char **argv)
  146. {
  147. /* growable array holding a series of lists */
  148. struct cut_list *cut_lists = NULL;
  149. unsigned nlists = 0; /* number of elements in above list */
  150. char delim = '\t'; /* delimiter, default is tab */
  151. char *sopt, *ltok;
  152. unsigned opt;
  153. opt_complementary = "b--bcf:c--bcf:f--bcf";
  154. opt = getopt32(argv, optstring, &sopt, &sopt, &sopt, &ltok);
  155. // argc -= optind;
  156. argv += optind;
  157. if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS)))
  158. bb_error_msg_and_die("expected a list of bytes, characters, or fields");
  159. if (opt & CUT_OPT_DELIM_FLGS) {
  160. if (ltok[0] && ltok[1]) { /* more than 1 char? */
  161. bb_error_msg_and_die("the delimiter must be a single character");
  162. }
  163. delim = ltok[0];
  164. }
  165. /* non-field (char or byte) cutting has some special handling */
  166. if (!(opt & CUT_OPT_FIELDS_FLGS)) {
  167. static const char _op_on_field[] ALIGN1 = " only when operating on fields";
  168. if (opt & CUT_OPT_SUPPRESS_FLGS) {
  169. bb_error_msg_and_die
  170. ("suppressing non-delimited lines makes sense%s",
  171. _op_on_field);
  172. }
  173. if (delim != '\t') {
  174. bb_error_msg_and_die
  175. ("a delimiter may be specified%s", _op_on_field);
  176. }
  177. }
  178. /*
  179. * parse list and put values into startpos and endpos.
  180. * valid list formats: N, N-, N-M, -M
  181. * more than one list can be separated by commas
  182. */
  183. {
  184. char *ntok;
  185. int s = 0, e = 0;
  186. /* take apart the lists, one by one (they are separated with commas) */
  187. while ((ltok = strsep(&sopt, ",")) != NULL) {
  188. /* it's actually legal to pass an empty list */
  189. if (!ltok[0])
  190. continue;
  191. /* get the start pos */
  192. ntok = strsep(&ltok, "-");
  193. if (!ntok[0]) {
  194. s = BOL;
  195. } else {
  196. s = xatoi_u(ntok);
  197. /* account for the fact that arrays are zero based, while
  198. * the user expects the first char on the line to be char #1 */
  199. if (s != 0)
  200. s--;
  201. }
  202. /* get the end pos */
  203. if (ltok == NULL) {
  204. e = NON_RANGE;
  205. } else if (!ltok[0]) {
  206. e = EOL;
  207. } else {
  208. e = xatoi_u(ltok);
  209. /* if the user specified and end position of 0,
  210. * that means "til the end of the line" */
  211. if (e == 0)
  212. e = EOL;
  213. e--; /* again, arrays are zero based, lines are 1 based */
  214. if (e == s)
  215. e = NON_RANGE;
  216. }
  217. /* add the new list */
  218. cut_lists = xrealloc_vector(cut_lists, 4, nlists);
  219. /* NB: startpos is always >= 0,
  220. * while endpos may be = NON_RANGE (-1) */
  221. cut_lists[nlists].startpos = s;
  222. cut_lists[nlists].endpos = e;
  223. nlists++;
  224. }
  225. /* make sure we got some cut positions out of all that */
  226. if (nlists == 0)
  227. bb_error_msg_and_die("missing list of positions");
  228. /* now that the lists are parsed, we need to sort them to make life
  229. * easier on us when it comes time to print the chars / fields / lines
  230. */
  231. qsort(cut_lists, nlists, sizeof(struct cut_list), cmpfunc);
  232. }
  233. {
  234. int retval = EXIT_SUCCESS;
  235. if (!*argv)
  236. *--argv = (char *)"-";
  237. do {
  238. FILE *file = fopen_or_warn_stdin(*argv);
  239. if (!file) {
  240. retval = EXIT_FAILURE;
  241. continue;
  242. }
  243. cut_file(file, delim, cut_lists, nlists);
  244. fclose_if_not_stdin(file);
  245. } while (*++argv);
  246. if (ENABLE_FEATURE_CLEAN_UP)
  247. free(cut_lists);
  248. fflush_stdout_and_exit(retval);
  249. }
  250. }