subr_prf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /* $OpenBSD: subr_prf.c,v 1.85 2015/03/14 03:38:50 jsg Exp $ */
  2. /* $NetBSD: subr_prf.c,v 1.45 1997/10/24 18:14:25 chuck Exp $ */
  3. /*-
  4. * Copyright (c) 1986, 1988, 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. * (c) UNIX System Laboratories, Inc.
  7. * All or some portions of this file are derived from material licensed
  8. * to the University of California by American Telephone and Telegraph
  9. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  10. * the permission of UNIX System Laboratories, Inc.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
  37. */
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/conf.h>
  41. #include <sys/reboot.h>
  42. #include <sys/msgbuf.h>
  43. #include <sys/proc.h>
  44. #include <sys/ioctl.h>
  45. #include <sys/vnode.h>
  46. #include <sys/file.h>
  47. #include <sys/tty.h>
  48. #include <sys/tprintf.h>
  49. #include <sys/syslog.h>
  50. #include <sys/malloc.h>
  51. #include <sys/pool.h>
  52. #include <sys/mutex.h>
  53. #include <dev/cons.h>
  54. /*
  55. * note that stdarg.h and the ansi style va_start macro is used for both
  56. * ansi and traditional c compilers.
  57. */
  58. #include <sys/stdarg.h>
  59. #ifdef KGDB
  60. #include <sys/kgdb.h>
  61. #endif
  62. #ifdef DDB
  63. #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
  64. #include <ddb/db_var.h> /* db_log, db_radix */
  65. #endif
  66. /*
  67. * defines
  68. */
  69. /* flags for kprintf */
  70. #define TOCONS 0x01 /* to the console */
  71. #define TOTTY 0x02 /* to the process' tty */
  72. #define TOLOG 0x04 /* to the kernel message buffer */
  73. #define TOBUFONLY 0x08 /* to the buffer (only) [for snprintf] */
  74. #define TODDB 0x10 /* to ddb console */
  75. #define TOCOUNT 0x20 /* act like [v]snprintf */
  76. /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
  77. #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
  78. /*
  79. * local prototypes
  80. */
  81. int kprintf(const char *, int, void *, char *, va_list);
  82. void kputchar(int, int, struct tty *);
  83. struct mutex kprintf_mutex = MUTEX_INITIALIZER(IPL_HIGH);
  84. /*
  85. * globals
  86. */
  87. extern struct tty *constty; /* pointer to console "window" tty */
  88. extern int log_open; /* subr_log: is /dev/klog open? */
  89. const char *panicstr; /* arg to first call to panic (used as a flag
  90. to indicate that panic has already been called). */
  91. #ifdef DDB
  92. /*
  93. * Enter ddb on panic.
  94. */
  95. int db_panic = 1;
  96. /*
  97. * db_console controls if we can be able to enter ddb by a special key
  98. * combination (machine dependent).
  99. * If DDB_SAFE_CONSOLE is defined in the kernel configuration it allows
  100. * to break into console during boot. It's _really_ useful when debugging
  101. * some things in the kernel that can cause init(8) to crash.
  102. */
  103. #ifdef DDB_SAFE_CONSOLE
  104. int db_console = 1;
  105. #else
  106. int db_console = 0;
  107. #endif
  108. /*
  109. * flag to indicate if we are currently in ddb (on some processor)
  110. */
  111. int db_is_active;
  112. #endif
  113. /*
  114. * panic on spl assertion failure?
  115. */
  116. int splassert_ctl = 1;
  117. /*
  118. * v_putc: routine to putc on virtual console
  119. *
  120. * the v_putc pointer can be used to redirect the console cnputc elsewhere
  121. * [e.g. to a "virtual console"].
  122. */
  123. void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */
  124. /*
  125. * functions
  126. */
  127. /*
  128. * Partial support (the failure case) of the assertion facility
  129. * commonly found in userland.
  130. */
  131. void
  132. __assert(const char *t, const char *f, int l, const char *e)
  133. {
  134. panic(__KASSERTSTR, t, e, f, l);
  135. }
  136. /*
  137. * tablefull: warn that a system table is full
  138. */
  139. void
  140. tablefull(const char *tab)
  141. {
  142. log(LOG_ERR, "%s: table is full\n", tab);
  143. }
  144. /*
  145. * panic: handle an unresolvable fatal error
  146. *
  147. * prints "panic: <message>" and reboots. if called twice (i.e. recursive
  148. * call) we avoid trying to sync the disk and just reboot (to avoid
  149. * recursive panics).
  150. */
  151. void
  152. panic(const char *fmt, ...)
  153. {
  154. static char panicbuf[512];
  155. int bootopt;
  156. va_list ap;
  157. /* do not trigger assertions, we know that we are inconsistent */
  158. splassert_ctl = 0;
  159. bootopt = RB_AUTOBOOT | RB_DUMP;
  160. va_start(ap, fmt);
  161. if (panicstr)
  162. bootopt |= RB_NOSYNC;
  163. else {
  164. vsnprintf(panicbuf, sizeof panicbuf, fmt, ap);
  165. panicstr = panicbuf;
  166. }
  167. va_end(ap);
  168. printf("panic: ");
  169. va_start(ap, fmt);
  170. vprintf(fmt, ap);
  171. printf("\n");
  172. va_end(ap);
  173. #ifdef KGDB
  174. kgdb_panic();
  175. #endif
  176. #ifdef KADB
  177. if (boothowto & RB_KDB)
  178. kdbpanic();
  179. #endif
  180. #ifdef DDB
  181. if (db_panic)
  182. Debugger();
  183. else
  184. db_stack_dump();
  185. #endif
  186. reboot(bootopt);
  187. /* NOTREACHED */
  188. }
  189. /*
  190. * We print only the function name. The file name is usually very long and
  191. * would eat tons of space in the kernel.
  192. */
  193. void
  194. splassert_fail(int wantipl, int haveipl, const char *func)
  195. {
  196. printf("splassert: %s: want %d have %d\n", func, wantipl, haveipl);
  197. switch (splassert_ctl) {
  198. case 1:
  199. break;
  200. case 2:
  201. #ifdef DDB
  202. db_stack_dump();
  203. #endif
  204. break;
  205. case 3:
  206. #ifdef DDB
  207. db_stack_dump();
  208. Debugger();
  209. #endif
  210. break;
  211. default:
  212. panic("spl assertion failure in %s", func);
  213. }
  214. }
  215. /*
  216. * kernel logging functions: log, logpri, addlog
  217. */
  218. /*
  219. * log: write to the log buffer
  220. *
  221. * => will not sleep [so safe to call from interrupt]
  222. * => will log to console if /dev/klog isn't open
  223. */
  224. void
  225. log(int level, const char *fmt, ...)
  226. {
  227. int s;
  228. va_list ap;
  229. s = splhigh();
  230. logpri(level); /* log the level first */
  231. va_start(ap, fmt);
  232. kprintf(fmt, TOLOG, NULL, NULL, ap);
  233. va_end(ap);
  234. splx(s);
  235. if (!log_open) {
  236. va_start(ap, fmt);
  237. kprintf(fmt, TOCONS, NULL, NULL, ap);
  238. va_end(ap);
  239. }
  240. logwakeup(); /* wake up anyone waiting for log msgs */
  241. }
  242. /*
  243. * logpri: log the priority level to the klog
  244. */
  245. void
  246. logpri(int level)
  247. {
  248. char *p;
  249. char snbuf[KPRINTF_BUFSIZE];
  250. kputchar('<', TOLOG, NULL);
  251. snprintf(snbuf, sizeof snbuf, "%d", level);
  252. for (p = snbuf ; *p ; p++)
  253. kputchar(*p, TOLOG, NULL);
  254. kputchar('>', TOLOG, NULL);
  255. }
  256. /*
  257. * addlog: add info to previous log message
  258. */
  259. int
  260. addlog(const char *fmt, ...)
  261. {
  262. int s;
  263. va_list ap;
  264. s = splhigh();
  265. va_start(ap, fmt);
  266. kprintf(fmt, TOLOG, NULL, NULL, ap);
  267. va_end(ap);
  268. splx(s);
  269. if (!log_open) {
  270. va_start(ap, fmt);
  271. kprintf(fmt, TOCONS, NULL, NULL, ap);
  272. va_end(ap);
  273. }
  274. logwakeup();
  275. return(0);
  276. }
  277. /*
  278. * kputchar: print a single character on console or user terminal.
  279. *
  280. * => if console, then the last MSGBUFS chars are saved in msgbuf
  281. * for inspection later (e.g. dmesg/syslog)
  282. */
  283. void
  284. kputchar(int c, int flags, struct tty *tp)
  285. {
  286. extern int msgbufmapped;
  287. int ddb_active = 0;
  288. #ifdef DDB
  289. ddb_active = db_is_active;
  290. #endif
  291. if (panicstr)
  292. constty = NULL;
  293. if ((flags & TOCONS) && tp == NULL && constty && !ddb_active) {
  294. tp = constty;
  295. flags |= TOTTY;
  296. }
  297. if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
  298. (flags & TOCONS) && tp == constty)
  299. constty = NULL;
  300. if ((flags & TOLOG) &&
  301. c != '\0' && c != '\r' && c != 0177 && msgbufmapped)
  302. msgbuf_putchar(msgbufp, c);
  303. if ((flags & TOCONS) && (constty == NULL || ddb_active) && c != '\0')
  304. (*v_putc)(c);
  305. #ifdef DDB
  306. if (flags & TODDB)
  307. db_putchar(c);
  308. #endif
  309. }
  310. /*
  311. * uprintf: print to the controlling tty of the current process
  312. *
  313. * => we may block if the tty queue is full
  314. * => no message is printed if the queue doesn't clear in a reasonable
  315. * time
  316. */
  317. void
  318. uprintf(const char *fmt, ...)
  319. {
  320. struct process *pr = curproc->p_p;
  321. va_list ap;
  322. if (pr->ps_flags & PS_CONTROLT && pr->ps_session->s_ttyvp) {
  323. va_start(ap, fmt);
  324. kprintf(fmt, TOTTY, pr->ps_session->s_ttyp, NULL, ap);
  325. va_end(ap);
  326. }
  327. }
  328. #if defined(NFSSERVER) || defined(NFSCLIENT)
  329. /*
  330. * tprintf functions: used to send messages to a specific process
  331. *
  332. * usage:
  333. * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
  334. * use the handle when calling "tprintf"
  335. * when done, do a "tprintf_close" to drop the handle
  336. */
  337. /*
  338. * tprintf_open: get a tprintf handle on a process "p"
  339. * XXX change s/proc/process
  340. *
  341. * => returns NULL if process can't be printed to
  342. */
  343. tpr_t
  344. tprintf_open(struct proc *p)
  345. {
  346. struct process *pr = p->p_p;
  347. if (pr->ps_flags & PS_CONTROLT && pr->ps_session->s_ttyvp) {
  348. SESSHOLD(pr->ps_session);
  349. return ((tpr_t)pr->ps_session);
  350. }
  351. return ((tpr_t) NULL);
  352. }
  353. /*
  354. * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
  355. */
  356. void
  357. tprintf_close(tpr_t sess)
  358. {
  359. if (sess)
  360. SESSRELE((struct session *) sess);
  361. }
  362. /*
  363. * tprintf: given tprintf handle to a process [obtained with tprintf_open],
  364. * send a message to the controlling tty for that process.
  365. *
  366. * => also sends message to /dev/klog
  367. */
  368. void
  369. tprintf(tpr_t tpr, const char *fmt, ...)
  370. {
  371. struct session *sess = (struct session *)tpr;
  372. struct tty *tp = NULL;
  373. int flags = TOLOG;
  374. va_list ap;
  375. logpri(LOG_INFO);
  376. if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
  377. flags |= TOTTY;
  378. tp = sess->s_ttyp;
  379. }
  380. va_start(ap, fmt);
  381. kprintf(fmt, flags, tp, NULL, ap);
  382. va_end(ap);
  383. logwakeup();
  384. }
  385. #endif /* NFSSERVER || NFSCLIENT */
  386. /*
  387. * ttyprintf: send a message to a specific tty
  388. *
  389. * => should be used only by tty driver or anything that knows the
  390. * underlying tty will not be revoked(2)'d away. [otherwise,
  391. * use tprintf]
  392. */
  393. void
  394. ttyprintf(struct tty *tp, const char *fmt, ...)
  395. {
  396. va_list ap;
  397. va_start(ap, fmt);
  398. kprintf(fmt, TOTTY, tp, NULL, ap);
  399. va_end(ap);
  400. }
  401. #ifdef DDB
  402. /*
  403. * db_printf: printf for DDB (via db_putchar)
  404. */
  405. int
  406. db_printf(const char *fmt, ...)
  407. {
  408. va_list ap;
  409. int retval;
  410. va_start(ap, fmt);
  411. retval = db_vprintf(fmt, ap);
  412. va_end(ap);
  413. return(retval);
  414. }
  415. int
  416. db_vprintf(const char *fmt, va_list ap)
  417. {
  418. int flags;
  419. flags = TODDB;
  420. if (db_log)
  421. flags |= TOLOG;
  422. return (kprintf(fmt, flags, NULL, NULL, ap));
  423. }
  424. #endif /* DDB */
  425. /*
  426. * normal kernel printf functions: printf, vprintf, snprintf
  427. */
  428. /*
  429. * printf: print a message to the console and the log
  430. */
  431. int
  432. printf(const char *fmt, ...)
  433. {
  434. va_list ap;
  435. int retval;
  436. mtx_enter(&kprintf_mutex);
  437. va_start(ap, fmt);
  438. retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
  439. va_end(ap);
  440. if (!panicstr)
  441. logwakeup();
  442. mtx_leave(&kprintf_mutex);
  443. return(retval);
  444. }
  445. /*
  446. * vprintf: print a message to the console and the log [already have a
  447. * va_list]
  448. */
  449. int
  450. vprintf(const char *fmt, va_list ap)
  451. {
  452. int retval;
  453. mtx_enter(&kprintf_mutex);
  454. retval = kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
  455. if (!panicstr)
  456. logwakeup();
  457. mtx_leave(&kprintf_mutex);
  458. return (retval);
  459. }
  460. /*
  461. * snprintf: print a message to a buffer
  462. */
  463. int
  464. snprintf(char *buf, size_t size, const char *fmt, ...)
  465. {
  466. int retval;
  467. va_list ap;
  468. char *p;
  469. p = buf + size - 1;
  470. if (size < 1)
  471. p = buf;
  472. va_start(ap, fmt);
  473. retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap);
  474. va_end(ap);
  475. if (size > 0)
  476. *(p) = 0; /* null terminate */
  477. return(retval);
  478. }
  479. /*
  480. * vsnprintf: print a message to a buffer [already have va_alist]
  481. */
  482. int
  483. vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
  484. {
  485. int retval;
  486. char *p;
  487. p = buf + size - 1;
  488. if (size < 1)
  489. p = buf;
  490. retval = kprintf(fmt, TOBUFONLY | TOCOUNT, &p, buf, ap);
  491. if (size > 0)
  492. *(p) = 0; /* null terminate */
  493. return(retval);
  494. }
  495. /*
  496. * kprintf: scaled down version of printf(3).
  497. *
  498. * this version based on vfprintf() from libc which was derived from
  499. * software contributed to Berkeley by Chris Torek.
  500. *
  501. * The additional format %b is supported to decode error registers.
  502. * Its usage is:
  503. *
  504. * printf("reg=%b\n", regval, "<base><arg>*");
  505. *
  506. * where <base> is the output base expressed as a control character, e.g.
  507. * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
  508. * the first of which gives the bit number to be inspected (origin 1), and
  509. * the next characters (up to a control character, i.e. a character <= 32),
  510. * give the name of the register. Thus:
  511. *
  512. * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
  513. *
  514. * would produce output:
  515. *
  516. * reg=3<BITTWO,BITONE>
  517. *
  518. * To support larger integers (> 32 bits), %b formatting will also accept
  519. * control characters in the region 0x80 - 0xff. 0x80 refers to bit 0,
  520. * 0x81 refers to bit 1, and so on. The equivalent string to the above is:
  521. *
  522. * kprintf("reg=%b\n", 3, "\10\201BITTWO\200BITONE\n");
  523. *
  524. * and would produce the same output.
  525. *
  526. * Like the rest of printf, %b can be prefixed to handle various size
  527. * modifiers, eg. %b is for "int", %lb is for "long", and %llb supports
  528. * "long long".
  529. *
  530. * This code is large and complicated...
  531. */
  532. /*
  533. * macros for converting digits to letters and vice versa
  534. */
  535. #define to_digit(c) ((c) - '0')
  536. #define is_digit(c) ((unsigned)to_digit(c) <= 9)
  537. #define to_char(n) ((n) + '0')
  538. /*
  539. * flags used during conversion.
  540. */
  541. #define ALT 0x001 /* alternate form */
  542. #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
  543. #define LADJUST 0x004 /* left adjustment */
  544. #define LONGDBL 0x008 /* long double; unimplemented */
  545. #define LONGINT 0x010 /* long integer */
  546. #define QUADINT 0x020 /* quad integer */
  547. #define SHORTINT 0x040 /* short integer */
  548. #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
  549. #define FPT 0x100 /* Floating point number */
  550. #define SIZEINT 0x200 /* (signed) size_t */
  551. /*
  552. * To extend shorts properly, we need both signed and unsigned
  553. * argument extraction methods.
  554. */
  555. #define SARG() \
  556. (flags&QUADINT ? va_arg(ap, quad_t) : \
  557. flags&LONGINT ? va_arg(ap, long) : \
  558. flags&SIZEINT ? va_arg(ap, ssize_t) : \
  559. flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
  560. (long)va_arg(ap, int))
  561. #define UARG() \
  562. (flags&QUADINT ? va_arg(ap, u_quad_t) : \
  563. flags&LONGINT ? va_arg(ap, u_long) : \
  564. flags&SIZEINT ? va_arg(ap, size_t) : \
  565. flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
  566. (u_long)va_arg(ap, u_int))
  567. #define KPRINTF_PUTCHAR(C) do { \
  568. int chr = (C); \
  569. ret += 1; \
  570. if (oflags & TOBUFONLY) { \
  571. if ((vp != NULL) && (sbuf == tailp)) { \
  572. if (!(oflags & TOCOUNT)) \
  573. goto overflow; \
  574. } else \
  575. *sbuf++ = chr; \
  576. } else { \
  577. kputchar(chr, oflags, (struct tty *)vp); \
  578. } \
  579. } while(0)
  580. int
  581. kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
  582. {
  583. char *fmt; /* format string */
  584. int ch; /* character from fmt */
  585. int n; /* handy integer (short term usage) */
  586. char *cp = NULL; /* handy char pointer (short term usage) */
  587. int flags; /* flags as above */
  588. int ret; /* return value accumulator */
  589. int width; /* width from format (%8d), or 0 */
  590. int prec; /* precision from format (%.3d), or -1 */
  591. char sign; /* sign prefix (' ', '+', '-', or \0) */
  592. u_quad_t _uquad; /* integer arguments %[diouxX] */
  593. enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
  594. int dprec; /* a copy of prec if [diouxX], 0 otherwise */
  595. int realsz; /* field size expanded by dprec */
  596. int size = 0; /* size of converted field or string */
  597. char *xdigs = NULL; /* digits for [xX] conversion */
  598. char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
  599. char *tailp = NULL; /* tail pointer for snprintf */
  600. if ((oflags & TOBUFONLY) && (vp != NULL))
  601. tailp = *(char **)vp;
  602. fmt = (char *)fmt0;
  603. ret = 0;
  604. /*
  605. * Scan the format for conversions (`%' character).
  606. */
  607. for (;;) {
  608. while (*fmt != '%' && *fmt) {
  609. KPRINTF_PUTCHAR(*fmt++);
  610. }
  611. if (*fmt == 0)
  612. goto done;
  613. fmt++; /* skip over '%' */
  614. flags = 0;
  615. dprec = 0;
  616. width = 0;
  617. prec = -1;
  618. sign = '\0';
  619. rflag: ch = *fmt++;
  620. reswitch: switch (ch) {
  621. /* XXX: non-standard '%b' format */
  622. case 'b': {
  623. char *b, *z;
  624. int tmp;
  625. _uquad = UARG();
  626. b = va_arg(ap, char *);
  627. if (*b == 8)
  628. snprintf(buf, sizeof buf, "%llo", _uquad);
  629. else if (*b == 10)
  630. snprintf(buf, sizeof buf, "%lld", _uquad);
  631. else if (*b == 16)
  632. snprintf(buf, sizeof buf, "%llx", _uquad);
  633. else
  634. break;
  635. b++;
  636. z = buf;
  637. while (*z) {
  638. KPRINTF_PUTCHAR(*z++);
  639. }
  640. if (_uquad) {
  641. tmp = 0;
  642. while ((n = *b++) != 0) {
  643. if (n & 0x80)
  644. n &= 0x7f;
  645. else if (n <= ' ')
  646. n = n - 1;
  647. if (_uquad & (1LL << n)) {
  648. KPRINTF_PUTCHAR(tmp ? ',':'<');
  649. while (*b > ' ' &&
  650. (*b & 0x80) == 0) {
  651. KPRINTF_PUTCHAR(*b);
  652. b++;
  653. }
  654. tmp = 1;
  655. } else {
  656. while (*b > ' ' &&
  657. (*b & 0x80) == 0)
  658. b++;
  659. }
  660. }
  661. if (tmp) {
  662. KPRINTF_PUTCHAR('>');
  663. }
  664. }
  665. continue; /* no output */
  666. }
  667. case ' ':
  668. /*
  669. * ``If the space and + flags both appear, the space
  670. * flag will be ignored.''
  671. * -- ANSI X3J11
  672. */
  673. if (!sign)
  674. sign = ' ';
  675. goto rflag;
  676. case '#':
  677. flags |= ALT;
  678. goto rflag;
  679. case '*':
  680. /*
  681. * ``A negative field width argument is taken as a
  682. * - flag followed by a positive field width.''
  683. * -- ANSI X3J11
  684. * They don't exclude field widths read from args.
  685. */
  686. if ((width = va_arg(ap, int)) >= 0)
  687. goto rflag;
  688. width = -width;
  689. /* FALLTHROUGH */
  690. case '-':
  691. flags |= LADJUST;
  692. goto rflag;
  693. case '+':
  694. sign = '+';
  695. goto rflag;
  696. case '.':
  697. if ((ch = *fmt++) == '*') {
  698. n = va_arg(ap, int);
  699. prec = n < 0 ? -1 : n;
  700. goto rflag;
  701. }
  702. n = 0;
  703. while (is_digit(ch)) {
  704. n = 10 * n + to_digit(ch);
  705. ch = *fmt++;
  706. }
  707. prec = n < 0 ? -1 : n;
  708. goto reswitch;
  709. case '0':
  710. /*
  711. * ``Note that 0 is taken as a flag, not as the
  712. * beginning of a field width.''
  713. * -- ANSI X3J11
  714. */
  715. flags |= ZEROPAD;
  716. goto rflag;
  717. case '1': case '2': case '3': case '4':
  718. case '5': case '6': case '7': case '8': case '9':
  719. n = 0;
  720. do {
  721. n = 10 * n + to_digit(ch);
  722. ch = *fmt++;
  723. } while (is_digit(ch));
  724. width = n;
  725. goto reswitch;
  726. case 'h':
  727. flags |= SHORTINT;
  728. goto rflag;
  729. case 'l':
  730. if (*fmt == 'l') {
  731. fmt++;
  732. flags |= QUADINT;
  733. } else {
  734. flags |= LONGINT;
  735. }
  736. goto rflag;
  737. case 'q':
  738. flags |= QUADINT;
  739. goto rflag;
  740. case 'z':
  741. flags |= SIZEINT;
  742. goto rflag;
  743. case 'c':
  744. *(cp = buf) = va_arg(ap, int);
  745. size = 1;
  746. sign = '\0';
  747. break;
  748. case 't':
  749. /* ptrdiff_t */
  750. /* FALLTHROUGH */
  751. case 'D':
  752. flags |= LONGINT;
  753. /*FALLTHROUGH*/
  754. case 'd':
  755. case 'i':
  756. _uquad = SARG();
  757. if ((quad_t)_uquad < 0) {
  758. _uquad = -_uquad;
  759. sign = '-';
  760. }
  761. base = DEC;
  762. goto number;
  763. case 'n':
  764. /* %n is unsupported in the kernel; just skip it */
  765. if (flags & QUADINT)
  766. (void)va_arg(ap, quad_t *);
  767. else if (flags & LONGINT)
  768. (void)va_arg(ap, long *);
  769. else if (flags & SHORTINT)
  770. (void)va_arg(ap, short *);
  771. else if (flags & SIZEINT)
  772. (void)va_arg(ap, ssize_t *);
  773. else
  774. (void)va_arg(ap, int *);
  775. continue; /* no output */
  776. case 'O':
  777. flags |= LONGINT;
  778. /*FALLTHROUGH*/
  779. case 'o':
  780. _uquad = UARG();
  781. base = OCT;
  782. goto nosign;
  783. case 'p':
  784. /*
  785. * ``The argument shall be a pointer to void. The
  786. * value of the pointer is converted to a sequence
  787. * of printable characters, in an implementation-
  788. * defined manner.''
  789. * -- ANSI X3J11
  790. */
  791. /* NOSTRICT */
  792. _uquad = (u_long)va_arg(ap, void *);
  793. base = HEX;
  794. xdigs = "0123456789abcdef";
  795. flags |= HEXPREFIX;
  796. ch = 'x';
  797. goto nosign;
  798. case 's':
  799. if ((cp = va_arg(ap, char *)) == NULL)
  800. cp = "(null)";
  801. if (prec >= 0) {
  802. /*
  803. * can't use strlen; can only look for the
  804. * NUL in the first `prec' characters, and
  805. * strlen() will go further.
  806. */
  807. char *p = memchr(cp, 0, prec);
  808. if (p != NULL) {
  809. size = p - cp;
  810. if (size > prec)
  811. size = prec;
  812. } else
  813. size = prec;
  814. } else
  815. size = strlen(cp);
  816. sign = '\0';
  817. break;
  818. case 'U':
  819. flags |= LONGINT;
  820. /*FALLTHROUGH*/
  821. case 'u':
  822. _uquad = UARG();
  823. base = DEC;
  824. goto nosign;
  825. case 'X':
  826. xdigs = "0123456789ABCDEF";
  827. goto hex;
  828. case 'x':
  829. xdigs = "0123456789abcdef";
  830. hex: _uquad = UARG();
  831. base = HEX;
  832. /* leading 0x/X only if non-zero */
  833. if (flags & ALT && _uquad != 0)
  834. flags |= HEXPREFIX;
  835. /* unsigned conversions */
  836. nosign: sign = '\0';
  837. /*
  838. * ``... diouXx conversions ... if a precision is
  839. * specified, the 0 flag will be ignored.''
  840. * -- ANSI X3J11
  841. */
  842. number: if ((dprec = prec) >= 0)
  843. flags &= ~ZEROPAD;
  844. /*
  845. * ``The result of converting a zero value with an
  846. * explicit precision of zero is no characters.''
  847. * -- ANSI X3J11
  848. */
  849. cp = buf + KPRINTF_BUFSIZE;
  850. if (_uquad != 0 || prec != 0) {
  851. /*
  852. * Unsigned mod is hard, and unsigned mod
  853. * by a constant is easier than that by
  854. * a variable; hence this switch.
  855. */
  856. switch (base) {
  857. case OCT:
  858. do {
  859. *--cp = to_char(_uquad & 7);
  860. _uquad >>= 3;
  861. } while (_uquad);
  862. /* handle octal leading 0 */
  863. if (flags & ALT && *cp != '0')
  864. *--cp = '0';
  865. break;
  866. case DEC:
  867. /* many numbers are 1 digit */
  868. while (_uquad >= 10) {
  869. *--cp = to_char(_uquad % 10);
  870. _uquad /= 10;
  871. }
  872. *--cp = to_char(_uquad);
  873. break;
  874. case HEX:
  875. do {
  876. *--cp = xdigs[_uquad & 15];
  877. _uquad >>= 4;
  878. } while (_uquad);
  879. break;
  880. default:
  881. cp = "bug in kprintf: bad base";
  882. size = strlen(cp);
  883. goto skipsize;
  884. }
  885. }
  886. size = buf + KPRINTF_BUFSIZE - cp;
  887. skipsize:
  888. break;
  889. default: /* "%?" prints ?, unless ? is NUL */
  890. if (ch == '\0')
  891. goto done;
  892. /* pretend it was %c with argument ch */
  893. cp = buf;
  894. *cp = ch;
  895. size = 1;
  896. sign = '\0';
  897. break;
  898. }
  899. /*
  900. * All reasonable formats wind up here. At this point, `cp'
  901. * points to a string which (if not flags&LADJUST) should be
  902. * padded out to `width' places. If flags&ZEROPAD, it should
  903. * first be prefixed by any sign or other prefix; otherwise,
  904. * it should be blank padded before the prefix is emitted.
  905. * After any left-hand padding and prefixing, emit zeroes
  906. * required by a decimal [diouxX] precision, then print the
  907. * string proper, then emit zeroes required by any leftover
  908. * floating precision; finally, if LADJUST, pad with blanks.
  909. *
  910. * Compute actual size, so we know how much to pad.
  911. * size excludes decimal prec; realsz includes it.
  912. */
  913. realsz = dprec > size ? dprec : size;
  914. if (sign)
  915. realsz++;
  916. else if (flags & HEXPREFIX)
  917. realsz+= 2;
  918. /* right-adjusting blank padding */
  919. if ((flags & (LADJUST|ZEROPAD)) == 0) {
  920. n = width - realsz;
  921. while (n-- > 0)
  922. KPRINTF_PUTCHAR(' ');
  923. }
  924. /* prefix */
  925. if (sign) {
  926. KPRINTF_PUTCHAR(sign);
  927. } else if (flags & HEXPREFIX) {
  928. KPRINTF_PUTCHAR('0');
  929. KPRINTF_PUTCHAR(ch);
  930. }
  931. /* right-adjusting zero padding */
  932. if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
  933. n = width - realsz;
  934. while (n-- > 0)
  935. KPRINTF_PUTCHAR('0');
  936. }
  937. /* leading zeroes from decimal precision */
  938. n = dprec - size;
  939. while (n-- > 0)
  940. KPRINTF_PUTCHAR('0');
  941. /* the string or number proper */
  942. while (size--)
  943. KPRINTF_PUTCHAR(*cp++);
  944. /* left-adjusting padding (always blank) */
  945. if (flags & LADJUST) {
  946. n = width - realsz;
  947. while (n-- > 0)
  948. KPRINTF_PUTCHAR(' ');
  949. }
  950. }
  951. done:
  952. if ((oflags & TOBUFONLY) && (vp != NULL))
  953. *(char **)vp = sbuf;
  954. overflow:
  955. return (ret);
  956. /* NOTREACHED */
  957. }
  958. #if __GNUC_PREREQ__(2,96)
  959. /*
  960. * XXX - these functions shouldn't be in the kernel, but gcc 3.X feels like
  961. * translating some printf calls to puts and since it doesn't seem
  962. * possible to just turn off parts of those optimizations (some of
  963. * them are really useful), we have to provide a dummy puts and putchar
  964. * that are wrappers around printf.
  965. */
  966. int puts(const char *);
  967. int putchar(int c);
  968. int
  969. puts(const char *str)
  970. {
  971. printf("%s\n", str);
  972. return (0);
  973. }
  974. int
  975. putchar(int c)
  976. {
  977. printf("%c", c);
  978. return (c);
  979. }
  980. #endif