source: rtems/c/src/lib/libc/printk.c @ 1bce637

4.104.114.84.95
Last change on this file since 1bce637 was 1bce637, checked in by Joel Sherrill <joel.sherrill@…>, on Jun 12, 2000 at 4:37:04 PM

Added printk as a generic, shared routine.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*-------------------------------------------------------------------------+
2| printk.c v1.1 - PC386 BSP - 1997/08/07
3+--------------------------------------------------------------------------+
4| (C) Copyright 1997 -
5| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
6|
7| http://pandora.ist.utl.pt
8|
9| Instituto Superior Tecnico * Lisboa * PORTUGAL
10+--------------------------------------------------------------------------+
11| Disclaimer:
12|
13| This file is provided "AS IS" without warranty of any kind, either
14| expressed or implied.
15+--------------------------------------------------------------------------+
16| This code is based on code by: Jose Rufino - IST
17|
18|  $Id$
19+--------------------------------------------------------------------------*/
20
21
22#include <stdarg.h>
23#include <stdio.h>
24#include <bspIo.h>
25/* #include <libcpu/cpu.h> */
26
27/*-------------------------------------------------------------------------+
28|         Function: printNum
29|      Description: print number in a given base.
30| Global Variables: None.
31|        Arguments: num - number to print, base - base used to print the number.
32|          Returns: Nothing.
33+--------------------------------------------------------------------------*/
34static void
35printNum(long unsigned int num, int base, int sign, int maxwidth, int lead)
36{
37  long unsigned int n;
38  int count;
39  char toPrint[20];
40
41  if ( (sign == 1) && ((long)num <  0) ) {
42    BSP_output_char('-');
43    num = -num;
44    maxwidth--;
45  }
46 
47  count = 0;
48  while ((n = num / base) > 0) {
49    toPrint[count++] = (num - (n*base));
50    num = n ;
51  }
52  toPrint[count++] = num;
53
54  if (maxwidth) {
55    for (n=maxwidth-count ; n ; n-- )
56      BSP_output_char(lead);
57  }
58
59  for (n = 0; n < count; n++){
60    BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]);
61  }
62} /* printNum */
63
64
65/*-------------------------------------------------------------------------+
66|         Function: printk
67|      Description: a simplified version of printf intended for use when the
68                    console is not yet initialized or in ISR's.
69| Global Variables: None.
70|        Arguments: as in printf: fmt - format string, ... - unnamed arguments.
71|          Returns: Nothing.
72+--------------------------------------------------------------------------*/
73void
74printk(char *fmt, ...)
75{
76  va_list  ap;      /* points to each unnamed argument in turn */
77  char     c, *str;
78  int      lflag, base, sign, width, lead;
79  /* unsigned int level; */
80
81  /* _CPU_ISR_Disable(level); */
82 
83  va_start(ap, fmt); /* make ap point to 1st unnamed arg */
84  for (; *fmt != '\0'; fmt++)
85  {
86    lflag = 0;
87    base  = 0;
88    sign = 0;
89    width = 0;
90    lead = ' ';
91    if (*fmt == '%')
92    {
93      fmt++;
94      if (*fmt == '0' ) {
95        lead = '0';
96        fmt++;
97      }
98      while (*fmt >= '0' && *fmt <= '9' ) {
99        width *= 10;
100        width += (*fmt - '0');
101        fmt++;
102      }
103
104      if ((c = *fmt) == 'l')
105      {
106        lflag = 1;
107        c = *++fmt;
108      }
109      switch (c)
110      {
111        case 'o': case 'O': base = 8; sign = 0; break;
112        case 'd': case 'D': base = 10; sign = 1; break;
113        case 'u': case 'U': base = 10; sign = 0; break;
114        case 'x': case 'X': base = 16; sign = 0; break;
115        case 's':
116          for (str = va_arg(ap, char *); *str; str++) 
117            BSP_output_char(*str);
118          break;
119        case 'c':
120#if defined(_TMS320C3x) || defined(_TMS320C4x)
121          BSP_output_char(va_arg(ap, int));
122#else
123          BSP_output_char(va_arg(ap, char));
124#endif
125          break;
126        default:
127          BSP_output_char(c);
128          break;
129      } /* switch*/
130
131      if (base)
132        printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int),
133                 base, sign, width, lead);
134    }
135    else
136    {
137      BSP_output_char(*fmt);
138    }
139  }
140  va_end(ap); /* clean up when done */
141  /* _CPU_ISR_Enable(level); */
142
143} /* printk */
144
Note: See TracBrowser for help on using the repository browser.