printf.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* vi: set sw=4 ts=4: */
  2. /* printf - format and print data
  3. Copyright 1999 Dave Cinege
  4. Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
  5. Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  6. */
  7. /* Usage: printf format [argument...]
  8. A front end to the printf function that lets it be used from the shell.
  9. Backslash escapes:
  10. \" = double quote
  11. \\ = backslash
  12. \a = alert (bell)
  13. \b = backspace
  14. \c = produce no further output
  15. \f = form feed
  16. \n = new line
  17. \r = carriage return
  18. \t = horizontal tab
  19. \v = vertical tab
  20. \0ooo = octal number (ooo is 0 to 3 digits)
  21. \xhhh = hexadecimal number (hhh is 1 to 3 digits)
  22. Additional directive:
  23. %b = print an argument string, interpreting backslash escapes
  24. The 'format' argument is re-used as many times as necessary
  25. to convert all of the given arguments.
  26. David MacKenzie <djm@gnu.ai.mit.edu> */
  27. // 19990508 Busy Boxed! Dave Cinege
  28. #include "busybox.h"
  29. static int print_formatted(char *format, int argc, char **argv);
  30. static void print_direc(char *start, size_t length,
  31. int field_width, int precision, char *argument);
  32. typedef void (*converter)(char *arg, void *result);
  33. static void multiconvert(char *arg, void *result, converter convert)
  34. {
  35. char s[16];
  36. if (*arg == '"' || *arg == '\'') {
  37. sprintf(s, "%d", (unsigned char)arg[1]);
  38. arg = s;
  39. }
  40. convert(arg, result);
  41. if (errno) /* Huh, looks strange... bug? */
  42. fputs(arg, stderr);
  43. }
  44. static void conv_strtoul(char *arg, void *result)
  45. {
  46. *(unsigned long*)result = bb_strtoul(arg, NULL, 10);
  47. }
  48. static void conv_strtol(char *arg, void *result)
  49. {
  50. *(long*)result = bb_strtol(arg, NULL, 10);
  51. }
  52. static void conv_strtod(char *arg, void *result)
  53. {
  54. char *end;
  55. /* Well, this one allows leading whitespace... so what */
  56. /* What I like much less is that "-" is accepted too! :( */
  57. *(double*)result = strtod(arg, &end);
  58. if (end[0]) errno = ERANGE;
  59. }
  60. static unsigned long my_xstrtoul(char *arg)
  61. {
  62. unsigned long result;
  63. multiconvert(arg, &result, conv_strtoul);
  64. return result;
  65. }
  66. static long my_xstrtol(char *arg)
  67. {
  68. long result;
  69. multiconvert(arg, &result, conv_strtol);
  70. return result;
  71. }
  72. static double my_xstrtod(char *arg)
  73. {
  74. double result;
  75. multiconvert(arg, &result, conv_strtod);
  76. return result;
  77. }
  78. static void print_esc_string(char *str)
  79. {
  80. for (; *str; str++) {
  81. if (*str == '\\') {
  82. str++;
  83. putchar(bb_process_escape_sequence((const char **)&str));
  84. } else {
  85. putchar(*str);
  86. }
  87. }
  88. }
  89. int printf_main(int argc, char **argv)
  90. {
  91. char *format;
  92. int args_used;
  93. if (argc <= 1 || argv[1][0] == '-') {
  94. bb_show_usage();
  95. }
  96. format = argv[1];
  97. argc -= 2;
  98. argv += 2;
  99. do {
  100. args_used = print_formatted(format, argc, argv);
  101. argc -= args_used;
  102. argv += args_used;
  103. }
  104. while (args_used > 0 && argc > 0);
  105. /* if (argc > 0)
  106. fprintf(stderr, "excess args ignored");
  107. */
  108. return EXIT_SUCCESS;
  109. }
  110. /* Print the text in FORMAT, using ARGV (with ARGC elements) for
  111. arguments to any '%' directives.
  112. Return the number of elements of ARGV used. */
  113. static int print_formatted(char *format, int argc, char **argv)
  114. {
  115. int save_argc = argc; /* Preserve original value. */
  116. char *f; /* Pointer into 'format'. */
  117. char *direc_start; /* Start of % directive. */
  118. size_t direc_length; /* Length of % directive. */
  119. int field_width; /* Arg to first '*', or -1 if none. */
  120. int precision; /* Arg to second '*', or -1 if none. */
  121. for (f = format; *f; ++f) {
  122. switch (*f) {
  123. case '%':
  124. direc_start = f++;
  125. direc_length = 1;
  126. field_width = precision = -1;
  127. if (*f == '%') {
  128. putchar('%');
  129. break;
  130. }
  131. if (*f == 'b') {
  132. if (argc > 0) {
  133. print_esc_string(*argv);
  134. ++argv;
  135. --argc;
  136. }
  137. break;
  138. }
  139. if (strchr("-+ #", *f)) {
  140. ++f;
  141. ++direc_length;
  142. }
  143. if (*f == '*') {
  144. ++f;
  145. ++direc_length;
  146. if (argc > 0) {
  147. field_width = my_xstrtoul(*argv);
  148. ++argv;
  149. --argc;
  150. } else
  151. field_width = 0;
  152. } else
  153. while (isdigit(*f)) {
  154. ++f;
  155. ++direc_length;
  156. }
  157. if (*f == '.') {
  158. ++f;
  159. ++direc_length;
  160. if (*f == '*') {
  161. ++f;
  162. ++direc_length;
  163. if (argc > 0) {
  164. precision = my_xstrtoul(*argv);
  165. ++argv;
  166. --argc;
  167. } else
  168. precision = 0;
  169. } else
  170. while (isdigit(*f)) {
  171. ++f;
  172. ++direc_length;
  173. }
  174. }
  175. if (*f == 'l' || *f == 'L' || *f == 'h') {
  176. ++f;
  177. ++direc_length;
  178. }
  179. /*
  180. if (!strchr ("diouxXfeEgGcs", *f))
  181. fprintf(stderr, "%%%c: invalid directive", *f);
  182. */
  183. ++direc_length;
  184. if (argc > 0) {
  185. print_direc(direc_start, direc_length, field_width,
  186. precision, *argv);
  187. ++argv;
  188. --argc;
  189. } else
  190. print_direc(direc_start, direc_length, field_width,
  191. precision, "");
  192. break;
  193. case '\\':
  194. if (*++f == 'c')
  195. exit(0);
  196. putchar(bb_process_escape_sequence((const char **)&f));
  197. f--;
  198. break;
  199. default:
  200. putchar(*f);
  201. }
  202. }
  203. return save_argc - argc;
  204. }
  205. static void
  206. print_direc(char *start, size_t length, int field_width, int precision,
  207. char *argument)
  208. {
  209. char *p; /* Null-terminated copy of % directive. */
  210. p = xmalloc((unsigned) (length + 1));
  211. strncpy(p, start, length);
  212. p[length] = 0;
  213. switch (p[length - 1]) {
  214. case 'd':
  215. case 'i':
  216. if (field_width < 0) {
  217. if (precision < 0)
  218. printf(p, my_xstrtol(argument));
  219. else
  220. printf(p, precision, my_xstrtol(argument));
  221. } else {
  222. if (precision < 0)
  223. printf(p, field_width, my_xstrtol(argument));
  224. else
  225. printf(p, field_width, precision, my_xstrtol(argument));
  226. }
  227. break;
  228. case 'o':
  229. case 'u':
  230. case 'x':
  231. case 'X':
  232. if (field_width < 0) {
  233. if (precision < 0)
  234. printf(p, my_xstrtoul(argument));
  235. else
  236. printf(p, precision, my_xstrtoul(argument));
  237. } else {
  238. if (precision < 0)
  239. printf(p, field_width, my_xstrtoul(argument));
  240. else
  241. printf(p, field_width, precision, my_xstrtoul(argument));
  242. }
  243. break;
  244. case 'f':
  245. case 'e':
  246. case 'E':
  247. case 'g':
  248. case 'G':
  249. if (field_width < 0) {
  250. if (precision < 0)
  251. printf(p, my_xstrtod(argument));
  252. else
  253. printf(p, precision, my_xstrtod(argument));
  254. } else {
  255. if (precision < 0)
  256. printf(p, field_width, my_xstrtod(argument));
  257. else
  258. printf(p, field_width, precision, my_xstrtod(argument));
  259. }
  260. break;
  261. case 'c':
  262. printf(p, *argument);
  263. break;
  264. case 's':
  265. if (field_width < 0) {
  266. if (precision < 0)
  267. printf(p, argument);
  268. else
  269. printf(p, precision, argument);
  270. } else {
  271. if (precision < 0)
  272. printf(p, field_width, argument);
  273. else
  274. printf(p, field_width, precision, argument);
  275. }
  276. break;
  277. }
  278. free(p);
  279. }