utprint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /******************************************************************************
  2. *
  3. * Module Name: utprint - Formatted printing routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utprint")
  46. #define ACPI_FORMAT_SIGN 0x01
  47. #define ACPI_FORMAT_SIGN_PLUS 0x02
  48. #define ACPI_FORMAT_SIGN_PLUS_SPACE 0x04
  49. #define ACPI_FORMAT_ZERO 0x08
  50. #define ACPI_FORMAT_LEFT 0x10
  51. #define ACPI_FORMAT_UPPER 0x20
  52. #define ACPI_FORMAT_PREFIX 0x40
  53. /* Local prototypes */
  54. static acpi_size
  55. acpi_ut_bound_string_length(const char *string, acpi_size count);
  56. static char *acpi_ut_bound_string_output(char *string, const char *end, char c);
  57. static char *acpi_ut_format_number(char *string,
  58. char *end,
  59. u64 number,
  60. u8 base, s32 width, s32 precision, u8 type);
  61. static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper);
  62. /* Module globals */
  63. static const char acpi_gbl_lower_hex_digits[] = "0123456789abcdef";
  64. static const char acpi_gbl_upper_hex_digits[] = "0123456789ABCDEF";
  65. /*******************************************************************************
  66. *
  67. * FUNCTION: acpi_ut_bound_string_length
  68. *
  69. * PARAMETERS: string - String with boundary
  70. * count - Boundary of the string
  71. *
  72. * RETURN: Length of the string. Less than or equal to Count.
  73. *
  74. * DESCRIPTION: Calculate the length of a string with boundary.
  75. *
  76. ******************************************************************************/
  77. static acpi_size
  78. acpi_ut_bound_string_length(const char *string, acpi_size count)
  79. {
  80. u32 length = 0;
  81. while (*string && count) {
  82. length++;
  83. string++;
  84. count--;
  85. }
  86. return (length);
  87. }
  88. /*******************************************************************************
  89. *
  90. * FUNCTION: acpi_ut_bound_string_output
  91. *
  92. * PARAMETERS: string - String with boundary
  93. * end - Boundary of the string
  94. * c - Character to be output to the string
  95. *
  96. * RETURN: Updated position for next valid character
  97. *
  98. * DESCRIPTION: Output a character into a string with boundary check.
  99. *
  100. ******************************************************************************/
  101. static char *acpi_ut_bound_string_output(char *string, const char *end, char c)
  102. {
  103. if (string < end) {
  104. *string = c;
  105. }
  106. ++string;
  107. return (string);
  108. }
  109. /*******************************************************************************
  110. *
  111. * FUNCTION: acpi_ut_put_number
  112. *
  113. * PARAMETERS: string - Buffer to hold reverse-ordered string
  114. * number - Integer to be converted
  115. * base - Base of the integer
  116. * upper - Whether or not using upper cased digits
  117. *
  118. * RETURN: Updated position for next valid character
  119. *
  120. * DESCRIPTION: Convert an integer into a string, note that, the string holds a
  121. * reversed ordered number without the trailing zero.
  122. *
  123. ******************************************************************************/
  124. static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper)
  125. {
  126. const char *digits;
  127. u64 digit_index;
  128. char *pos;
  129. pos = string;
  130. digits = upper ? acpi_gbl_upper_hex_digits : acpi_gbl_lower_hex_digits;
  131. if (number == 0) {
  132. *(pos++) = '0';
  133. } else {
  134. while (number) {
  135. (void)acpi_ut_divide(number, base, &number,
  136. &digit_index);
  137. *(pos++) = digits[digit_index];
  138. }
  139. }
  140. /* *(Pos++) = '0'; */
  141. return (pos);
  142. }
  143. /*******************************************************************************
  144. *
  145. * FUNCTION: acpi_ut_scan_number
  146. *
  147. * PARAMETERS: string - String buffer
  148. * number_ptr - Where the number is returned
  149. *
  150. * RETURN: Updated position for next valid character
  151. *
  152. * DESCRIPTION: Scan a string for a decimal integer.
  153. *
  154. ******************************************************************************/
  155. const char *acpi_ut_scan_number(const char *string, u64 *number_ptr)
  156. {
  157. u64 number = 0;
  158. while (isdigit((int)*string)) {
  159. number *= 10;
  160. number += *(string++) - '0';
  161. }
  162. *number_ptr = number;
  163. return (string);
  164. }
  165. /*******************************************************************************
  166. *
  167. * FUNCTION: acpi_ut_print_number
  168. *
  169. * PARAMETERS: string - String buffer
  170. * number - The number to be converted
  171. *
  172. * RETURN: Updated position for next valid character
  173. *
  174. * DESCRIPTION: Print a decimal integer into a string.
  175. *
  176. ******************************************************************************/
  177. const char *acpi_ut_print_number(char *string, u64 number)
  178. {
  179. char ascii_string[20];
  180. const char *pos1;
  181. char *pos2;
  182. pos1 = acpi_ut_put_number(ascii_string, number, 10, FALSE);
  183. pos2 = string;
  184. while (pos1 != ascii_string) {
  185. *(pos2++) = *(--pos1);
  186. }
  187. *pos2 = 0;
  188. return (string);
  189. }
  190. /*******************************************************************************
  191. *
  192. * FUNCTION: acpi_ut_format_number
  193. *
  194. * PARAMETERS: string - String buffer with boundary
  195. * end - Boundary of the string
  196. * number - The number to be converted
  197. * base - Base of the integer
  198. * width - Field width
  199. * precision - Precision of the integer
  200. * type - Special printing flags
  201. *
  202. * RETURN: Updated position for next valid character
  203. *
  204. * DESCRIPTION: Print an integer into a string with any base and any precision.
  205. *
  206. ******************************************************************************/
  207. static char *acpi_ut_format_number(char *string,
  208. char *end,
  209. u64 number,
  210. u8 base, s32 width, s32 precision, u8 type)
  211. {
  212. char *pos;
  213. char sign;
  214. char zero;
  215. u8 need_prefix;
  216. u8 upper;
  217. s32 i;
  218. char reversed_string[66];
  219. /* Parameter validation */
  220. if (base < 2 || base > 16) {
  221. return (NULL);
  222. }
  223. if (type & ACPI_FORMAT_LEFT) {
  224. type &= ~ACPI_FORMAT_ZERO;
  225. }
  226. need_prefix = ((type & ACPI_FORMAT_PREFIX)
  227. && base != 10) ? TRUE : FALSE;
  228. upper = (type & ACPI_FORMAT_UPPER) ? TRUE : FALSE;
  229. zero = (type & ACPI_FORMAT_ZERO) ? '0' : ' ';
  230. /* Calculate size according to sign and prefix */
  231. sign = '\0';
  232. if (type & ACPI_FORMAT_SIGN) {
  233. if ((s64) number < 0) {
  234. sign = '-';
  235. number = -(s64) number;
  236. width--;
  237. } else if (type & ACPI_FORMAT_SIGN_PLUS) {
  238. sign = '+';
  239. width--;
  240. } else if (type & ACPI_FORMAT_SIGN_PLUS_SPACE) {
  241. sign = ' ';
  242. width--;
  243. }
  244. }
  245. if (need_prefix) {
  246. width--;
  247. if (base == 16) {
  248. width--;
  249. }
  250. }
  251. /* Generate full string in reverse order */
  252. pos = acpi_ut_put_number(reversed_string, number, base, upper);
  253. i = ACPI_PTR_DIFF(pos, reversed_string);
  254. /* Printing 100 using %2d gives "100", not "00" */
  255. if (i > precision) {
  256. precision = i;
  257. }
  258. width -= precision;
  259. /* Output the string */
  260. if (!(type & (ACPI_FORMAT_ZERO | ACPI_FORMAT_LEFT))) {
  261. while (--width >= 0) {
  262. string = acpi_ut_bound_string_output(string, end, ' ');
  263. }
  264. }
  265. if (sign) {
  266. string = acpi_ut_bound_string_output(string, end, sign);
  267. }
  268. if (need_prefix) {
  269. string = acpi_ut_bound_string_output(string, end, '0');
  270. if (base == 16) {
  271. string = acpi_ut_bound_string_output(string, end,
  272. upper ? 'X' : 'x');
  273. }
  274. }
  275. if (!(type & ACPI_FORMAT_LEFT)) {
  276. while (--width >= 0) {
  277. string = acpi_ut_bound_string_output(string, end, zero);
  278. }
  279. }
  280. while (i <= --precision) {
  281. string = acpi_ut_bound_string_output(string, end, '0');
  282. }
  283. while (--i >= 0) {
  284. string = acpi_ut_bound_string_output(string, end,
  285. reversed_string[i]);
  286. }
  287. while (--width >= 0) {
  288. string = acpi_ut_bound_string_output(string, end, ' ');
  289. }
  290. return (string);
  291. }
  292. /*******************************************************************************
  293. *
  294. * FUNCTION: acpi_ut_vsnprintf
  295. *
  296. * PARAMETERS: string - String with boundary
  297. * size - Boundary of the string
  298. * format - Standard printf format
  299. * args - Argument list
  300. *
  301. * RETURN: Number of bytes actually written.
  302. *
  303. * DESCRIPTION: Formatted output to a string using argument list pointer.
  304. *
  305. ******************************************************************************/
  306. int
  307. acpi_ut_vsnprintf(char *string,
  308. acpi_size size, const char *format, va_list args)
  309. {
  310. u8 base;
  311. u8 type;
  312. s32 width;
  313. s32 precision;
  314. char qualifier;
  315. u64 number;
  316. char *pos;
  317. char *end;
  318. char c;
  319. const char *s;
  320. const void *p;
  321. s32 length;
  322. int i;
  323. pos = string;
  324. end = string + size;
  325. for (; *format; ++format) {
  326. if (*format != '%') {
  327. pos = acpi_ut_bound_string_output(pos, end, *format);
  328. continue;
  329. }
  330. type = 0;
  331. base = 10;
  332. /* Process sign */
  333. do {
  334. ++format;
  335. if (*format == '#') {
  336. type |= ACPI_FORMAT_PREFIX;
  337. } else if (*format == '0') {
  338. type |= ACPI_FORMAT_ZERO;
  339. } else if (*format == '+') {
  340. type |= ACPI_FORMAT_SIGN_PLUS;
  341. } else if (*format == ' ') {
  342. type |= ACPI_FORMAT_SIGN_PLUS_SPACE;
  343. } else if (*format == '-') {
  344. type |= ACPI_FORMAT_LEFT;
  345. } else {
  346. break;
  347. }
  348. } while (1);
  349. /* Process width */
  350. width = -1;
  351. if (isdigit((int)*format)) {
  352. format = acpi_ut_scan_number(format, &number);
  353. width = (s32) number;
  354. } else if (*format == '*') {
  355. ++format;
  356. width = va_arg(args, int);
  357. if (width < 0) {
  358. width = -width;
  359. type |= ACPI_FORMAT_LEFT;
  360. }
  361. }
  362. /* Process precision */
  363. precision = -1;
  364. if (*format == '.') {
  365. ++format;
  366. if (isdigit((int)*format)) {
  367. format = acpi_ut_scan_number(format, &number);
  368. precision = (s32) number;
  369. } else if (*format == '*') {
  370. ++format;
  371. precision = va_arg(args, int);
  372. }
  373. if (precision < 0) {
  374. precision = 0;
  375. }
  376. }
  377. /* Process qualifier */
  378. qualifier = -1;
  379. if (*format == 'h' || *format == 'l' || *format == 'L') {
  380. qualifier = *format;
  381. ++format;
  382. if (qualifier == 'l' && *format == 'l') {
  383. qualifier = 'L';
  384. ++format;
  385. }
  386. }
  387. switch (*format) {
  388. case '%':
  389. pos = acpi_ut_bound_string_output(pos, end, '%');
  390. continue;
  391. case 'c':
  392. if (!(type & ACPI_FORMAT_LEFT)) {
  393. while (--width > 0) {
  394. pos =
  395. acpi_ut_bound_string_output(pos,
  396. end,
  397. ' ');
  398. }
  399. }
  400. c = (char)va_arg(args, int);
  401. pos = acpi_ut_bound_string_output(pos, end, c);
  402. while (--width > 0) {
  403. pos =
  404. acpi_ut_bound_string_output(pos, end, ' ');
  405. }
  406. continue;
  407. case 's':
  408. s = va_arg(args, char *);
  409. if (!s) {
  410. s = "<NULL>";
  411. }
  412. length = acpi_ut_bound_string_length(s, precision);
  413. if (!(type & ACPI_FORMAT_LEFT)) {
  414. while (length < width--) {
  415. pos =
  416. acpi_ut_bound_string_output(pos,
  417. end,
  418. ' ');
  419. }
  420. }
  421. for (i = 0; i < length; ++i) {
  422. pos = acpi_ut_bound_string_output(pos, end, *s);
  423. ++s;
  424. }
  425. while (length < width--) {
  426. pos =
  427. acpi_ut_bound_string_output(pos, end, ' ');
  428. }
  429. continue;
  430. case 'o':
  431. base = 8;
  432. break;
  433. case 'X':
  434. type |= ACPI_FORMAT_UPPER;
  435. case 'x':
  436. base = 16;
  437. break;
  438. case 'd':
  439. case 'i':
  440. type |= ACPI_FORMAT_SIGN;
  441. case 'u':
  442. break;
  443. case 'p':
  444. if (width == -1) {
  445. width = 2 * sizeof(void *);
  446. type |= ACPI_FORMAT_ZERO;
  447. }
  448. p = va_arg(args, void *);
  449. pos = acpi_ut_format_number(pos, end,
  450. ACPI_TO_INTEGER(p), 16,
  451. width, precision, type);
  452. continue;
  453. default:
  454. pos = acpi_ut_bound_string_output(pos, end, '%');
  455. if (*format) {
  456. pos =
  457. acpi_ut_bound_string_output(pos, end,
  458. *format);
  459. } else {
  460. --format;
  461. }
  462. continue;
  463. }
  464. if (qualifier == 'L') {
  465. number = va_arg(args, u64);
  466. if (type & ACPI_FORMAT_SIGN) {
  467. number = (s64) number;
  468. }
  469. } else if (qualifier == 'l') {
  470. number = va_arg(args, unsigned long);
  471. if (type & ACPI_FORMAT_SIGN) {
  472. number = (s32) number;
  473. }
  474. } else if (qualifier == 'h') {
  475. number = (u16)va_arg(args, int);
  476. if (type & ACPI_FORMAT_SIGN) {
  477. number = (s16) number;
  478. }
  479. } else {
  480. number = va_arg(args, unsigned int);
  481. if (type & ACPI_FORMAT_SIGN) {
  482. number = (signed int)number;
  483. }
  484. }
  485. pos = acpi_ut_format_number(pos, end, number, base,
  486. width, precision, type);
  487. }
  488. if (size > 0) {
  489. if (pos < end) {
  490. *pos = '\0';
  491. } else {
  492. end[-1] = '\0';
  493. }
  494. }
  495. return (ACPI_PTR_DIFF(pos, string));
  496. }
  497. /*******************************************************************************
  498. *
  499. * FUNCTION: acpi_ut_snprintf
  500. *
  501. * PARAMETERS: string - String with boundary
  502. * size - Boundary of the string
  503. * Format, ... - Standard printf format
  504. *
  505. * RETURN: Number of bytes actually written.
  506. *
  507. * DESCRIPTION: Formatted output to a string.
  508. *
  509. ******************************************************************************/
  510. int acpi_ut_snprintf(char *string, acpi_size size, const char *format, ...)
  511. {
  512. va_list args;
  513. int length;
  514. va_start(args, format);
  515. length = acpi_ut_vsnprintf(string, size, format, args);
  516. va_end(args);
  517. return (length);
  518. }
  519. #ifdef ACPI_APPLICATION
  520. /*******************************************************************************
  521. *
  522. * FUNCTION: acpi_ut_file_vprintf
  523. *
  524. * PARAMETERS: file - File descriptor
  525. * format - Standard printf format
  526. * args - Argument list
  527. *
  528. * RETURN: Number of bytes actually written.
  529. *
  530. * DESCRIPTION: Formatted output to a file using argument list pointer.
  531. *
  532. ******************************************************************************/
  533. int acpi_ut_file_vprintf(ACPI_FILE file, const char *format, va_list args)
  534. {
  535. acpi_cpu_flags flags;
  536. int length;
  537. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  538. length = acpi_ut_vsnprintf(acpi_gbl_print_buffer,
  539. sizeof(acpi_gbl_print_buffer), format, args);
  540. (void)acpi_os_write_file(file, acpi_gbl_print_buffer, length, 1);
  541. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  542. return (length);
  543. }
  544. /*******************************************************************************
  545. *
  546. * FUNCTION: acpi_ut_file_printf
  547. *
  548. * PARAMETERS: file - File descriptor
  549. * Format, ... - Standard printf format
  550. *
  551. * RETURN: Number of bytes actually written.
  552. *
  553. * DESCRIPTION: Formatted output to a file.
  554. *
  555. ******************************************************************************/
  556. int acpi_ut_file_printf(ACPI_FILE file, const char *format, ...)
  557. {
  558. va_list args;
  559. int length;
  560. va_start(args, format);
  561. length = acpi_ut_file_vprintf(file, format, args);
  562. va_end(args);
  563. return (length);
  564. }
  565. #endif