os161-1.99
 All Data Structures
__printf.c
00001 /*
00002  * Copyright (c) 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Guts of printf.
00032  *
00033  * This file is used in both libc and the kernel and needs to work in both
00034  * contexts. This makes a few things a bit awkward.
00035  *
00036  * This is a slightly simplified version of the real-life printf
00037  * originally used in the VINO kernel.
00038  */
00039 
00040 #ifdef _KERNEL
00041 #include <types.h>
00042 #include <lib.h>
00043 #define assert KASSERT
00044 #else
00045 
00046 #include <sys/types.h>
00047 #include <assert.h>
00048 #include <stdint.h>
00049 #include <stdio.h>
00050 #include <string.h>
00051 #endif
00052 
00053 #include <stdarg.h>
00054 
00055 
00056 /* 
00057  * Do we want to support "long long" types with %lld?
00058  *
00059  * Using 64-bit types with gcc causes gcc to emit calls to functions
00060  * like __moddi3 and __divdi3. These need to be provided at link time,
00061  * which can be a hassle; this switch is provided to help avoid
00062  * needing them.
00063  */
00064 #define USE_LONGLONG
00065 
00066 /*
00067  * Define a type that holds the longest signed integer we intend to support.
00068  */
00069 #ifdef USE_LONGLONG
00070 #define INTTYPE  long long
00071 #else
00072 #define INTTYPE  long
00073 #endif
00074 
00075 
00076 /* 
00077  * Space for a long long in base 8, plus a NUL, plus one 
00078  * character extra for slop. 
00079  *
00080  * CHAR_BIT is the number of bits in a char; thus sizeof(long long)*CHAR_BIT
00081  * is the number of bits in a long long. Each octal digit prints 3 bits.
00082  * Values printed in larger bases will be shorter strings.
00083  */
00084 #define NUMBER_BUF_SIZE ((sizeof(INTTYPE) * CHAR_BIT) / 3 + 2)
00085 
00086 /*
00087  * Structure holding the state for printf.
00088  */
00089 typedef struct {
00090         /* Callback for sending printed string data */
00091         void (*sendfunc)(void *clientdata, const char *str, size_t len);
00092         void *clientdata;
00093 
00094         /* The varargs argument pointer */
00095         va_list ap;
00096 
00097         /* Total count of characters printed */
00098         int charcount;
00099 
00100         /* Flag that's true if we are currently looking in a %-format */
00101         int in_pct;
00102 
00103         /* Size of the integer argument to retrieve */
00104         enum { 
00105                 INTSZ, 
00106                 LONGSZ, 
00107 #ifdef USE_LONGLONG
00108                 LLONGSZ,
00109 #endif
00110         } size;
00111 
00112         /* The value of the integer argument retrieved */
00113         unsigned INTTYPE num;
00114 
00115         /* Sign of the integer argument (0 = positive; -1 = negative) */
00116         int sign;
00117 
00118         /* Field width (number of spaces) */
00119         int spacing;
00120 
00121         /* Flag: align to left in field instead of right */
00122         int rightspc;
00123 
00124         /* Character to pad to field size with (space or 0) */
00125         int fillchar;
00126 
00127         /* Number base to print the integer argument in (8, 10, 16) */
00128         int base;
00129 
00130         /* Flag: if set, print 0x before hex and 0 before octal numbers */
00131         int baseprefix;
00132 
00133         /* Flag: alternative output format selected with %#... */
00134         int altformat;
00135 } PF;
00136 
00137 /*
00138  * Send some text onward to the output.
00139  *
00140  * We count the total length we send out so we can return it from __vprintf,
00141  * since that's what most printf-like functions want to return.
00142  */
00143 static
00144 void
00145 __pf_print(PF *pf, const char *txt, size_t len)
00146 {
00147         pf->sendfunc(pf->clientdata, txt, len);
00148         pf->charcount += len;
00149 }
00150 
00151 /*
00152  * Reset the state for the next %-field.
00153  */
00154 static
00155 void
00156 __pf_endfield(PF *pf)
00157 {
00158         pf->in_pct = 0;
00159         pf->size = INTSZ;
00160         pf->num = 0;
00161         pf->sign = 0;
00162         pf->spacing = 0;
00163         pf->rightspc = 0;
00164         pf->fillchar = ' ';
00165         pf->base = 0;
00166         pf->baseprefix = 0;
00167         pf->altformat = 0;
00168 }
00169 
00170 /*
00171  * Process modifier chars (between the % and the type specifier)
00172  *    #           use "alternate display format"
00173  *    -           left align in field instead of right align
00174  *    l           value is long (ll = long long)
00175  *    0-9         field width
00176  *    leading 0   pad with zeros instead of spaces
00177  */
00178 static
00179 void
00180 __pf_modifier(PF *pf, int ch)
00181 {
00182         switch (ch) {
00183         case '#':
00184                 pf->altformat = 1;
00185                 break;
00186         case '-':
00187                 pf->rightspc = 1;
00188                 break;
00189         case 'l': 
00190                 if (pf->size==LONGSZ) {
00191 #ifdef USE_LONGLONG
00192                         pf->size = LLONGSZ;
00193 #endif
00194                 }
00195                 else {
00196                         pf->size = LONGSZ;
00197                 }
00198                 break;
00199         case '0': 
00200                 if (pf->spacing>0) {
00201                         /*
00202                          * Already seen some digits; this is part of the
00203                          * field size.
00204                          */
00205                         pf->spacing = pf->spacing*10;
00206                 }
00207                 else {
00208                         /*
00209                          * Leading zero; set the padding character to 0.
00210                          */
00211                         pf->fillchar = '0';
00212                 }
00213                 break;
00214         default:
00215                 /*
00216                  * Invalid characters should be filtered out by a
00217                  * higher-level function, so if this assert goes off
00218                  * it's our fault.
00219                  */
00220                 assert(ch>'0' && ch<='9');
00221 
00222                 /*
00223                  * Got a digit; accumulate the field size.
00224                  */
00225                 pf->spacing = pf->spacing*10 + (ch-'0');
00226                 break;
00227         }
00228 }
00229 
00230 /*
00231  * Retrieve a numeric argument from the argument list and store it
00232  * in pf->num, according to the size recorded in pf->size and using
00233  * the numeric type specified by ch.
00234  */
00235 static
00236 void
00237 __pf_getnum(PF *pf, int ch)
00238 {
00239         if (ch=='p') {
00240                 /* 
00241                  * Pointer.
00242                  *
00243                  * uintptr_t is a C99 standard type that's an unsigned
00244                  * integer the same size as a pointer.
00245                  */
00246                 pf->num = (uintptr_t) va_arg(pf->ap, void *);
00247         }
00248         else if (ch=='d') {
00249                 /* signed integer */
00250                 INTTYPE signednum=0;
00251                 switch (pf->size) {
00252                 case INTSZ:
00253                         /* %d */
00254                         signednum = va_arg(pf->ap, int);
00255                         break;
00256                 case LONGSZ:
00257                         /* %ld */
00258                         signednum = va_arg(pf->ap, long);
00259                         break;
00260 #ifdef USE_LONGLONG
00261                 case LLONGSZ:
00262                         /* %lld */
00263                         signednum = va_arg(pf->ap, long long);
00264                         break;
00265 #endif
00266                 }
00267 
00268                 /*
00269                  * Check for negative numbers.
00270                  */
00271                 if (signednum < 0) {
00272                         pf->sign = -1;
00273                         pf->num = -signednum;
00274                 }
00275                 else {
00276                         pf->num = signednum;
00277                 }
00278         }
00279         else {
00280                 /* unsigned integer */
00281                 switch (pf->size) {
00282                 case INTSZ:
00283                         /* %u (or %o, %x) */
00284                         pf->num = va_arg(pf->ap, unsigned int);
00285                         break;
00286                 case LONGSZ:
00287                         /* %lu (or %lo, %lx) */
00288                         pf->num = va_arg(pf->ap, unsigned long);
00289                         break;
00290 #ifdef USE_LONGLONG
00291                 case LLONGSZ:
00292                         /* %llu, %llo, %llx */
00293                         pf->num = va_arg(pf->ap, unsigned long long);
00294                         break;
00295 #endif
00296                 }
00297         }
00298 }
00299 
00300 /*
00301  * Set the printing base based on the numeric type specified in ch.
00302  *     %o     octal
00303  *     %d,%u  decimal
00304  *     %x     hex
00305  *     %p     pointer (print as hex)
00306  *
00307  * If the "alternate format" was requested, or always for pointers,
00308  * note to print the C prefix for the type.
00309  */
00310 static
00311 void
00312 __pf_setbase(PF *pf, int ch)
00313 {
00314         switch (ch) {
00315         case 'd':
00316         case 'u':
00317                 pf->base = 10;
00318                 break;
00319         case 'x':
00320         case 'p':
00321                 pf->base = 16;
00322                 break;
00323         case 'o':
00324                 pf->base = 8;
00325                 break;
00326         }
00327         if (pf->altformat || ch=='p') {
00328                 pf->baseprefix = 1;
00329         }
00330 }
00331 
00332 /*
00333  * Function to print "spc" instances of the fill character.
00334  */
00335 static
00336 void
00337 __pf_fill(PF *pf, int spc)
00338 {
00339         char f = pf->fillchar;
00340         int i;
00341         for (i=0; i<spc; i++) {
00342                 __pf_print(pf, &f, 1);
00343         }
00344 }
00345 
00346 /*
00347  * General printing function. Prints the string "stuff".
00348  * The two prefixes (in practice one is a type prefix, such as "0x",
00349  * and the other is the sign) get printed *after* space padding but
00350  * *before* zero padding, if padding is on the left.
00351  */
00352 static
00353 void
00354 __pf_printstuff(PF *pf,
00355                 const char *prefix, const char *prefix2,
00356                 const char *stuff)
00357 {
00358         /* Total length to print. */
00359         int len = strlen(prefix)+strlen(prefix2)+strlen(stuff);
00360 
00361         /* Get field width and compute amount of padding in "spc". */
00362         int spc = pf->spacing;
00363         if (spc > len) {
00364                 spc -= len;
00365         }
00366         else {
00367                 spc = 0;
00368         }
00369 
00370         /* If padding on left and the fill char is not 0, pad first. */
00371         if (spc > 0 && pf->rightspc==0 && pf->fillchar!='0') {
00372                 __pf_fill(pf, spc);
00373         }
00374 
00375         /* Print the prefixes. */
00376         __pf_print(pf, prefix, strlen(prefix));
00377         __pf_print(pf, prefix2, strlen(prefix2));
00378 
00379         /* If padding on left and the fill char *is* 0, pad here. */
00380         if (spc > 0 && pf->rightspc==0 && pf->fillchar=='0') {
00381                 __pf_fill(pf, spc);
00382         }
00383 
00384         /* Print the actual string. */
00385         __pf_print(pf, stuff, strlen(stuff));
00386 
00387         /* If padding on the right, pad afterwards. */
00388         if (spc > 0 && pf->rightspc!=0) {
00389                 __pf_fill(pf, spc);
00390         }
00391 }
00392 
00393 /*
00394  * Function to convert a number to ascii and then print it.
00395  *
00396  * Works from right to left in a buffer of NUMBER_BUF_SIZE bytes.
00397  * NUMBER_BUF_SIZE is set so that the longest number string we can 
00398  * generate (a long long printed in octal) will fit. See above.
00399  */
00400 static
00401 void
00402 __pf_printnum(PF *pf)
00403 {
00404         /* Digits to print with. */
00405         const char *const digits = "0123456789abcdef";
00406 
00407         char buf[NUMBER_BUF_SIZE];   /* Accumulation buffer for string. */
00408         char *x;                     /* Current pointer into buf. */ 
00409         unsigned INTTYPE xnum;       /* Current value to print. */
00410         const char *bprefix;         /* Base prefix (0, 0x, or nothing) */
00411         const char *sprefix;         /* Sign prefix (- or nothing) */
00412 
00413         /* Start in the last slot of the buffer. */
00414         x = buf+sizeof(buf)-1;
00415 
00416         /* Insert null terminator. */
00417         *x-- = 0;
00418 
00419         /* Initialize value. */
00420         xnum = pf->num;
00421 
00422         /* 
00423          * Convert a single digit.
00424          * Do this loop at least once - that way 0 prints as 0 and not "". 
00425          */
00426         do {
00427                 /* 
00428                  * Get the digit character for the least significant
00429                  * part of xnum.
00430                  */
00431                 *x = digits[xnum % pf->base];
00432                 
00433                 /*
00434                  * Back up the pointer to point to the next space to the left.
00435                  */
00436                 x--;
00437 
00438                 /*
00439                  * Drop the value of the digit we just printed from xnum.
00440                  */
00441                 xnum = xnum / pf->base;
00442 
00443                 /*
00444                  * If xnum hits 0 there's no more number left.
00445                  */
00446         } while (xnum > 0);
00447 
00448         /*
00449          * x points to the *next* slot in the buffer to use.
00450          * However, we're done printing the number. So it's pointing
00451          * one slot *before* the start of the actual number text.
00452          * So advance it by one so it actually points at the number.
00453          */
00454         x++;
00455 
00456         /*
00457          * If a base prefix was requested, select it.
00458          */
00459         if (pf->baseprefix && pf->base==16) {
00460                 bprefix = "0x";
00461         }
00462         else if (pf->baseprefix && pf->base==8) {
00463                 bprefix = "0";
00464         }
00465         else {
00466                 bprefix = "";
00467         }
00468 
00469         /*
00470          * Choose the sign prefix.
00471          */
00472         sprefix = pf->sign ? "-" : "";
00473 
00474         /*
00475          * Now actually print the string we just generated.
00476          */
00477         __pf_printstuff(pf, sprefix, bprefix, x);
00478 }
00479 
00480 /*
00481  * Process a single character out of the format string.
00482  */
00483 static
00484 void
00485 __pf_send(PF *pf, int ch)
00486 {
00487         /* Cannot get NULs here. */
00488         assert(ch!=0);
00489 
00490         if (pf->in_pct==0 && ch!='%') {
00491                 /*
00492                  * Not currently in a format, and not a %. Just send
00493                  * the character on through.
00494                  */
00495                 char c = ch;
00496                 __pf_print(pf, &c, 1);
00497         }
00498         else if (pf->in_pct==0) {
00499                 /*
00500                  * Not in a format, but got a %. Start a format.
00501                  */
00502                 pf->in_pct = 1;
00503         }
00504         else if (strchr("#-l0123456789", ch)) {
00505                 /*
00506                  * These are the modifier characters we recognize.
00507                  * (These are the characters between the % and the type.)
00508                  */
00509                 __pf_modifier(pf, ch);
00510         }
00511         else if (strchr("doupx", ch)) {
00512                 /*
00513                  * Integer types.
00514                  * Fetch the number, set the base, print it, then
00515                  * reset for the next format.
00516                  */
00517                 __pf_getnum(pf, ch);
00518                 __pf_setbase(pf, ch);
00519                 __pf_printnum(pf);
00520                 __pf_endfield(pf);
00521         }
00522         else if (ch=='s') {
00523                 /*
00524                  * Print a string.
00525                  */
00526                 const char *str = va_arg(pf->ap, const char *);
00527                 if (str==NULL) {
00528                         str = "(null)";
00529                 }
00530                 __pf_printstuff(pf, "", "", str);
00531                 __pf_endfield(pf);
00532         }
00533         else {
00534                 /* 
00535                  * %%, %c, or illegal character. 
00536                  * Illegal characters are printed like %%.
00537                  * for example, %5k prints "    k".
00538                  */
00539                 char x[2];
00540                 if (ch=='c') {
00541                         x[0] = va_arg(pf->ap, int);
00542                 }
00543                 else {
00544                         x[0] = ch;
00545                 }
00546                 x[1] = 0;
00547                 __pf_printstuff(pf, "", "", x);
00548                 __pf_endfield(pf);
00549         }
00550 }
00551 
00552 /*
00553  * Do a whole printf session.
00554  * Create and initialize a printf state object,
00555  * then send it each character from the format string.
00556  */
00557 int
00558 __vprintf(void (*func)(void *clientdata, const char *str, size_t len), 
00559           void *clientdata, const char *format, va_list ap)
00560 {
00561         PF pf;
00562         int i;
00563 
00564         pf.sendfunc = func;
00565         pf.clientdata = clientdata;
00566         pf.ap = ap;
00567         pf.charcount = 0;
00568         __pf_endfield(&pf);
00569 
00570         for (i=0; format[i]; i++) {
00571                 __pf_send(&pf, format[i]);
00572         }
00573 
00574         return pf.charcount;
00575 }
 All Data Structures