Subversion Repositories HelenOS

Rev

Rev 2377 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 1987,1997, Prentice Hall
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use of the MINIX operating system in source and
  6.  * binary forms, with or without modification, are permitted provided
  7.  * that the following conditions are met:
  8.  
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  
  12.  * - Redistributions in binary form must reproduce the above
  13.  *   copyright notice, this list of conditions and the following
  14.  *   disclaimer in the documentation and/or other materials provided
  15.  *   with the distribution.
  16.  
  17.  * - Neither the name of Prentice Hall nor the names of the software
  18.  *   authors or contributors may be used to endorse or promote
  19.  *   products derived from this software without specific prior
  20.  *   written permission.
  21.  
  22.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
  23.  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26.  * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
  27.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  32.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  33.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.  */
  35.  
  36. /** @addtogroup FileSystemImpl
  37. * @{
  38. */
  39.  
  40. /**
  41.  * @file    printing.c
  42.  * @brief   Functions for printing on console.
  43.  */
  44.  
  45.  
  46. #include <string.h>
  47. #include <ipc/ipc.h>
  48. #include <ipc/services.h>
  49. #include <async.h>
  50. #include "fs.h"
  51. #include "../console/console.h"
  52.  
  53. #define TOTAL_CONVERSIONS   5
  54. #define BUFF_SIZE       10
  55.  
  56.  
  57. /**
  58.  * Table of printing conversions for: d, i, u, c, %.
  59.  */
  60. char conversions[TOTAL_CONVERSIONS];
  61. char format_string[3] = "%";
  62.  
  63. static int validate_string(char conv);
  64.  
  65. /**
  66.  * Connects to SERVICE_CONSOLE and initializes used data structures
  67.  */
  68. int init_printing()
  69. {
  70.    
  71.     /* Attempt to connect to SERVICE_CONSOLE fail. */
  72.     if (!connect_to_con(&con_phone, CON_CONN_ATTEMPTS))
  73.         return FALSE;
  74.  
  75.     conversions[0] = 'd';
  76.     conversions[1] = 'i';
  77.     conversions[2] = 'u';
  78.     conversions[3] = 'c';
  79.     conversions[4] = '%';
  80.    
  81. }
  82.  
  83. /**
  84.  * Check whether the conversion character matches the conversions table
  85.  */
  86. int validate_string(char conv)
  87. {
  88.    
  89.     int i;
  90.    
  91.     /* Check if the conversion character matches conversions table. */
  92.     for (i = 0; i < TOTAL_CONVERSIONS; i++){
  93.         if (conv == conversions[i]) {
  94.             return TRUE;   
  95.         }
  96.     }
  97.  
  98.     return FALSE;  
  99. }
  100.  
  101. /**
  102.  * Prints char* on console
  103.  */
  104. int print_console(char *str)
  105. {
  106.    
  107.     int i, len;
  108.     char c;
  109.    
  110.     /* Must be connected to the CONSOLE_SERVICE. */
  111.     if (con_phone < 0)
  112.         return FALSE;
  113.    
  114.     len = strlen(str);
  115.     if (len == 0)
  116.         return TRUE;
  117.    
  118.     for (i = 0; i < len; i++) {
  119.         c = *str++;
  120.         async_req_2(con_phone, CONSOLE_PUTCHAR, c, 0, NULL, NULL);  /* Do the work. */     
  121.     }
  122.  
  123.     return TRUE;
  124. }
  125.  
  126. /**
  127.  * Prints int argument with specified conversion inside input string
  128.  */
  129. int print_console_int(char *str, int arg)
  130. {
  131.    
  132.     int i, retval, len, front_side, offset;
  133.     char conversion;
  134.     char buffer[BUFF_SIZE];
  135.     char *front, *back;
  136.  
  137.     len = strlen(str);
  138.  
  139.     if (len == 0)
  140.         return TRUE;
  141.    
  142.     front = malloc((len+1) * sizeof(char));
  143.     back = malloc((len+1) * sizeof(char));
  144.    
  145.     front_side = TRUE;
  146.    
  147.     /* Copy front side of source string. */
  148.     for (i = 0; i < len; i++) {
  149.         if (str[i] == '%') {
  150.             front_side = FALSE;
  151.             break;
  152.         }
  153.         front[i] = str[i];
  154.     }
  155.    
  156.     /* Character '%' was not found. Print it in a normal way, without int argument. */
  157.     if (front_side) {
  158.         return (print_console(str));   
  159.     }
  160.    
  161.     /* Character '%' was the last in the input string - error. */
  162.     if (i == (len-1)) {
  163.         return FALSE;
  164.     }
  165.    
  166.     front[i] = '\0';
  167.     conversion = str[++i];
  168.     retval = validate_string(conversion);
  169.    
  170.     if (!retval) {
  171.            return FALSE;   
  172.     }
  173.    
  174.     offset = ++i;
  175.    
  176.     /* Copy back side of source string. */
  177.     for (;i < len; i++) {
  178.         back[i-offset] = str[i];
  179.     }
  180.  
  181.     back[i] = '\0';
  182.  
  183.     /* Two characters '%' in source string. */
  184.     if (conversion == '%') {
  185.         print_console(front);
  186.         print_console(back);
  187.         return TRUE;
  188.     }
  189.    
  190.     /* Other 'normal' case. */
  191.     print_console(front);
  192.     format_string[1] = conversion;
  193.     sprintf(buffer, format_string, arg);
  194.     print_console(buffer);
  195.     print_console(back);
  196.  
  197.     return TRUE;
  198. }
  199.  
  200. /**
  201.  * }
  202.  */
  203.  
  204.