utprint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /******************************************************************************
  2. *
  3. * Module Name: utprint - Formatted printing routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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. /*******************************************************************************
  63. *
  64. * FUNCTION: acpi_ut_bound_string_length
  65. *
  66. * PARAMETERS: string - String with boundary
  67. * count - Boundary of the string
  68. *
  69. * RETURN: Length of the string. Less than or equal to Count.
  70. *
  71. * DESCRIPTION: Calculate the length of a string with boundary.
  72. *
  73. ******************************************************************************/
  74. static acpi_size
  75. acpi_ut_bound_string_length(const char *string, acpi_size count)
  76. {
  77. u32 length = 0;
  78. while (*string && count) {
  79. length++;
  80. string++;
  81. count--;
  82. }
  83. return (length);
  84. }
  85. /*******************************************************************************
  86. *
  87. * FUNCTION: acpi_ut_bound_string_output
  88. *
  89. * PARAMETERS: string - String with boundary
  90. * end - Boundary of the string
  91. * c - Character to be output to the string
  92. *
  93. * RETURN: Updated position for next valid character
  94. *
  95. * DESCRIPTION: Output a character into a string with boundary check.
  96. *
  97. ******************************************************************************/
  98. static char *acpi_ut_bound_string_output(char *string, const char *end, char c)
  99. {
  100. if (string < end) {
  101. *string = c;
  102. }
  103. ++string;
  104. return (string);
  105. }
  106. /*******************************************************************************
  107. *
  108. * FUNCTION: acpi_ut_put_number
  109. *
  110. * PARAMETERS: string - Buffer to hold reverse-ordered string
  111. * number - Integer to be converted
  112. * base - Base of the integer
  113. * upper - Whether or not using upper cased digits
  114. *
  115. * RETURN: Updated position for next valid character
  116. *
  117. * DESCRIPTION: Convert an integer into a string, note that, the string holds a
  118. * reversed ordered number without the trailing zero.
  119. *
  120. ******************************************************************************/
  121. static char *acpi_ut_put_number(char *string, u64 number, u8 base, u8 upper)
  122. {
  123. const char *digits;
  124. u64 digit_index;
  125. char *pos;
  126. pos = string;
  127. digits = upper ? acpi_gbl_upper_hex_digits : acpi_gbl_lower_hex_digits;
  128. if (number == 0) {
  129. *(pos++) = '0';
  130. } else {
  131. while (number) {
  132. (void)acpi_ut_divide(number, base, &number,
  133. &digit_index);
  134. *(pos++) = digits[digit_index];
  135. }
  136. }
  137. /* *(Pos++) = '0'; */
  138. return (pos);
  139. }
  140. /*******************************************************************************
  141. *
  142. * FUNCTION: acpi_ut_scan_number
  143. *
  144. * PARAMETERS: string - String buffer
  145. * number_ptr - Where the number is returned
  146. *
  147. * RETURN: Updated position for next valid character
  148. *
  149. * DESCRIPTION: Scan a string for a decimal integer.
  150. *
  151. ******************************************************************************/
  152. const char *acpi_ut_scan_number(const char *string, u64 *number_ptr)
  153. {
  154. u64 number = 0;
  155. while (isdigit((int)*string)) {
  156. number *= 10;
  157. number += *(string++) - '0';
  158. }
  159. *number_ptr = number;
  160. return (string);
  161. }
  162. /*******************************************************************************
  163. *
  164. * FUNCTION: acpi_ut_print_number
  165. *
  166. * PARAMETERS: string - String buffer
  167. * number - The number to be converted
  168. *
  169. * RETURN: Updated position for next valid character
  170. *
  171. * DESCRIPTION: Print a decimal integer into a string.
  172. *
  173. ******************************************************************************/
  174. const char *acpi_ut_print_number(char *string, u64 number)
  175. {
  176. char ascii_string[20];
  177. const char *pos1;
  178. char *pos2;
  179. pos1 = acpi_ut_put_number(ascii_string, number, 10, FALSE);
  180. pos2 = string;
  181. while (pos1 != ascii_string) {
  182. *(pos2++) = *(--pos1);
  183. }
  184. *pos2 = 0;
  185. return (string);
  186. }
  187. /*******************************************************************************
  188. *
  189. * FUNCTION: acpi_ut_format_number
  190. *
  191. * PARAMETERS: string - String buffer with boundary
  192. * end - Boundary of the string
  193. * number - The number to be converted
  194. * base - Base of the integer
  195. * width - Field width
  196. * precision - Precision of the integer
  197. * type - Special printing flags
  198. *
  199. * RETURN: Updated position for next valid character
  200. *
  201. * DESCRIPTION: Print an integer into a string with any base and any precision.
  202. *
  203. ******************************************************************************/
  204. static char *acpi_ut_format_number(char *string,
  205. char *end,
  206. u64 number,
  207. u8 base, s32 width, s32 precision, u8 type)
  208. {
  209. char *pos;
  210. char sign;
  211. char zero;
  212. u8 need_prefix;
  213. u8 upper;
  214. s32 i;
  215. char reversed_string[66];
  216. /* Parameter validation */
  217. if (base < 2 || base > 16) {
  218. return (NULL);
  219. }
  220. if (type & ACPI_FORMAT_LEFT) {
  221. type &= ~ACPI_FORMAT_ZERO;
  222. }
  223. need_prefix = ((type & ACPI_FORMAT_PREFIX)
  224. && base != 10) ? TRUE : FALSE;
  225. upper = (type & ACPI_FORMAT_UPPER) ? TRUE : FALSE;
  226. zero = (type & ACPI_FORMAT_ZERO) ? '0' : ' ';
  227. /* Calculate size according to sign and prefix */
  228. sign = '\0';
  229. if (type & ACPI_FORMAT_SIGN) {
  230. if ((s64)number < 0) {
  231. sign = '-';
  232. number = -(s64)number;
  233. width--;
  234. } else if (type & ACPI_FORMAT_SIGN_PLUS) {
  235. sign = '+';
  236. width--;
  237. } else if (type & ACPI_FORMAT_SIGN_PLUS_SPACE) {
  238. sign = ' ';
  239. width--;
  240. }
  241. }
  242. if (need_prefix) {
  243. width--;
  244. if (base == 16) {
  245. width--;
  246. }
  247. }
  248. /* Generate full string in reverse order */
  249. pos = acpi_ut_put_number(reversed_string, number, base, upper);
  250. i = ACPI_PTR_DIFF(pos, reversed_string);
  251. /* Printing 100 using %2d gives "100", not "00" */
  252. if (i > precision) {
  253. precision = i;
  254. }
  255. width -= precision;
  256. /* Output the string */
  257. if (!(type & (ACPI_FORMAT_ZERO | ACPI_FORMAT_LEFT))) {
  258. while (--width >= 0) {
  259. string = acpi_ut_bound_string_output(string, end, ' ');
  260. }
  261. }
  262. if (sign) {
  263. string = acpi_ut_bound_string_output(string, end, sign);
  264. }
  265. if (need_prefix) {
  266. string = acpi_ut_bound_string_output(string, end, '0');
  267. if (base == 16) {
  268. string =
  269. acpi_ut_bound_string_output(string, end,
  270. upper ? 'X' : 'x');
  271. }
  272. }
  273. if (!(type & ACPI_FORMAT_LEFT)) {
  274. while (--width >= 0) {
  275. string = acpi_ut_bound_string_output(string, end, zero);
  276. }
  277. }
  278. while (i <= --precision) {
  279. string = acpi_ut_bound_string_output(string, end, '0');
  280. }
  281. while (--i >= 0) {
  282. string = acpi_ut_bound_string_output(string, end,
  283. reversed_string[i]);
  284. }
  285. while (--width >= 0) {
  286. string = acpi_ut_bound_string_output(string, end, ' ');
  287. }
  288. return (string);
  289. }
  290. /*******************************************************************************
  291. *
  292. * FUNCTION: acpi_ut_vsnprintf
  293. *
  294. * PARAMETERS: string - String with boundary
  295. * size - Boundary of the string
  296. * format - Standard printf format
  297. * args - Argument list
  298. *
  299. * RETURN: Number of bytes actually written.
  300. *
  301. * DESCRIPTION: Formatted output to a string using argument list pointer.
  302. *
  303. ******************************************************************************/
  304. int
  305. acpi_ut_vsnprintf(char *string,
  306. acpi_size size, const char *format, va_list args)
  307. {
  308. u8 base;
  309. u8 type;
  310. s32 width;
  311. s32 precision;
  312. char qualifier;
  313. u64 number;
  314. char *pos;
  315. char *end;
  316. char c;
  317. const char *s;
  318. const void *p;
  319. s32 length;
  320. int i;
  321. pos = string;
  322. end = string + size;
  323. for (; *format; ++format) {
  324. if (*format != '%') {
  325. pos = acpi_ut_bound_string_output(pos, end, *format);
  326. continue;
  327. }
  328. type = 0;
  329. base = 10;
  330. /* Process sign */
  331. do {
  332. ++format;
  333. if (*format == '#') {
  334. type |= ACPI_FORMAT_PREFIX;
  335. } else if (*format == '0') {
  336. type |= ACPI_FORMAT_ZERO;
  337. } else if (*format == '+') {
  338. type |= ACPI_FORMAT_SIGN_PLUS;
  339. } else if (*format == ' ') {
  340. type |= ACPI_FORMAT_SIGN_PLUS_SPACE;
  341. } else if (*format == '-') {
  342. type |= ACPI_FORMAT_LEFT;
  343. } else {
  344. break;
  345. }
  346. } while (1);
  347. /* Process width */
  348. width = -1;
  349. if (isdigit((int)*format)) {
  350. format = acpi_ut_scan_number(format, &number);
  351. width = (s32)number;
  352. } else if (*format == '*') {
  353. ++format;
  354. width = va_arg(args, int);
  355. if (width < 0) {
  356. width = -width;
  357. type |= ACPI_FORMAT_LEFT;
  358. }
  359. }
  360. /* Process precision */
  361. precision = -1;
  362. if (*format == '.') {
  363. ++format;
  364. if (isdigit((int)*format)) {
  365. format = acpi_ut_scan_number(format, &number);
  366. precision = (s32)number;
  367. } else if (*format == '*') {
  368. ++format;
  369. precision = va_arg(args, int);
  370. }
  371. if (precision < 0) {
  372. precision = 0;
  373. }
  374. }
  375. /* Process qualifier */
  376. qualifier = -1;
  377. if (*format == 'h' || *format == 'l' || *format == 'L') {
  378. qualifier = *format;
  379. ++format;
  380. if (qualifier == 'l' && *format == 'l') {
  381. qualifier = 'L';
  382. ++format;
  383. }
  384. }
  385. switch (*format) {
  386. case '%':
  387. pos = acpi_ut_bound_string_output(pos, end, '%');
  388. continue;
  389. case 'c':
  390. if (!(type & ACPI_FORMAT_LEFT)) {
  391. while (--width > 0) {
  392. pos =
  393. acpi_ut_bound_string_output(pos,
  394. end,
  395. ' ');
  396. }
  397. }
  398. c = (char)va_arg(args, int);
  399. pos = acpi_ut_bound_string_output(pos, end, c);
  400. while (--width > 0) {
  401. pos =
  402. acpi_ut_bound_string_output(pos, end, ' ');
  403. }
  404. continue;
  405. case 's':
  406. s = va_arg(args, char *);
  407. if (!s) {
  408. s = "<NULL>";
  409. }
  410. length = acpi_ut_bound_string_length(s, precision);
  411. if (!(type & ACPI_FORMAT_LEFT)) {
  412. while (length < width--) {
  413. pos =
  414. acpi_ut_bound_string_output(pos,
  415. end,
  416. ' ');
  417. }
  418. }
  419. for (i = 0; i < length; ++i) {
  420. pos = acpi_ut_bound_string_output(pos, end, *s);
  421. ++s;
  422. }
  423. while (length < width--) {
  424. pos =
  425. acpi_ut_bound_string_output(pos, end, ' ');
  426. }
  427. continue;
  428. case 'o':
  429. base = 8;
  430. break;
  431. case 'X':
  432. type |= ACPI_FORMAT_UPPER;
  433. case 'x':
  434. base = 16;
  435. break;
  436. case 'd':
  437. case 'i':
  438. type |= ACPI_FORMAT_SIGN;
  439. case 'u':
  440. break;
  441. case 'p':
  442. if (width == -1) {
  443. width = 2 * sizeof(void *);
  444. type |= ACPI_FORMAT_ZERO;
  445. }
  446. p = va_arg(args, void *);
  447. pos =
  448. acpi_ut_format_number(pos, end, ACPI_TO_INTEGER(p),
  449. 16, width, precision, type);
  450. continue;
  451. default:
  452. pos = acpi_ut_bound_string_output(pos, end, '%');
  453. if (*format) {
  454. pos =
  455. acpi_ut_bound_string_output(pos, end,
  456. *format);
  457. } else {
  458. --format;
  459. }
  460. continue;
  461. }
  462. if (qualifier == 'L') {
  463. number = va_arg(args, u64);
  464. if (type & ACPI_FORMAT_SIGN) {
  465. number = (s64)number;
  466. }
  467. } else if (qualifier == 'l') {
  468. number = va_arg(args, unsigned long);
  469. if (type & ACPI_FORMAT_SIGN) {
  470. number = (s32)number;
  471. }
  472. } else if (qualifier == 'h') {
  473. number = (u16)va_arg(args, int);
  474. if (type & ACPI_FORMAT_SIGN) {
  475. number = (s16)number;
  476. }
  477. } else {
  478. number = va_arg(args, unsigned int);
  479. if (type & ACPI_FORMAT_SIGN) {
  480. number = (signed int)number;
  481. }
  482. }
  483. pos = acpi_ut_format_number(pos, end, number, base,
  484. width, precision, type);
  485. }
  486. if (size > 0) {
  487. if (pos < end) {
  488. *pos = '\0';
  489. } else {
  490. end[-1] = '\0';
  491. }
  492. }
  493. return (ACPI_PTR_DIFF(pos, string));
  494. }
  495. /*******************************************************************************
  496. *
  497. * FUNCTION: acpi_ut_snprintf
  498. *
  499. * PARAMETERS: string - String with boundary
  500. * size - Boundary of the string
  501. * Format, ... - Standard printf format
  502. *
  503. * RETURN: Number of bytes actually written.
  504. *
  505. * DESCRIPTION: Formatted output to a string.
  506. *
  507. ******************************************************************************/
  508. int acpi_ut_snprintf(char *string, acpi_size size, const char *format, ...)
  509. {
  510. va_list args;
  511. int length;
  512. va_start(args, format);
  513. length = acpi_ut_vsnprintf(string, size, format, args);
  514. va_end(args);
  515. return (length);
  516. }
  517. #ifdef ACPI_APPLICATION
  518. /*******************************************************************************
  519. *
  520. * FUNCTION: acpi_ut_file_vprintf
  521. *
  522. * PARAMETERS: file - File descriptor
  523. * format - Standard printf format
  524. * args - Argument list
  525. *
  526. * RETURN: Number of bytes actually written.
  527. *
  528. * DESCRIPTION: Formatted output to a file using argument list pointer.
  529. *
  530. ******************************************************************************/
  531. int acpi_ut_file_vprintf(ACPI_FILE file, const char *format, va_list args)
  532. {
  533. acpi_cpu_flags flags;
  534. int length;
  535. flags = acpi_os_acquire_lock(acpi_gbl_print_lock);
  536. length = acpi_ut_vsnprintf(acpi_gbl_print_buffer,
  537. sizeof(acpi_gbl_print_buffer), format, args);
  538. (void)acpi_os_write_file(file, acpi_gbl_print_buffer, length, 1);
  539. acpi_os_release_lock(acpi_gbl_print_lock, flags);
  540. return (length);
  541. }
  542. /*******************************************************************************
  543. *
  544. * FUNCTION: acpi_ut_file_printf
  545. *
  546. * PARAMETERS: file - File descriptor
  547. * Format, ... - Standard printf format
  548. *
  549. * RETURN: Number of bytes actually written.
  550. *
  551. * DESCRIPTION: Formatted output to a file.
  552. *
  553. ******************************************************************************/
  554. int acpi_ut_file_printf(ACPI_FILE file, const char *format, ...)
  555. {
  556. va_list args;
  557. int length;
  558. va_start(args, format);
  559. length = acpi_ut_file_vprintf(file, format, args);
  560. va_end(args);
  561. return (length);
  562. }
  563. #endif