/*
* Copyright (c) 1987,1997, Prentice Hall
* All rights reserved.
*
* Redistribution and use of the MINIX operating system in source and
* binary forms, with or without modification, are permitted provided
* that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* - Neither the name of Prentice Hall nor the names of the software
* authors or contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup FileSystemImpl
* @{
*/
/**
* @file printing.c
* @brief Functions for printing on console.
*/
#include <string.h>
#include <ipc/ipc.h>
#include <ipc/services.h>
#include <async.h>
#include "fs.h"
#include "../console/console.h"
#define TOTAL_CONVERSIONS 5
#define BUFF_SIZE 10
/**
* Table of printing conversions for: d, i, u, c, %.
*/
char conversions[TOTAL_CONVERSIONS];
char format_string[3] = "%";
static int validate_string(char conv);
/**
* Connects to SERVICE_CONSOLE and initializes used data structures
*/
int init_printing()
{
/* Attempt to connect to SERVICE_CONSOLE fail. */
if (!connect_to_con(&con_phone, CON_CONN_ATTEMPTS))
return FALSE;
conversions[0] = 'd';
conversions[1] = 'i';
conversions[2] = 'u';
conversions[3] = 'c';
conversions[4] = '%';
}
/**
* Check whether the conversion character matches the conversions table
*/
int validate_string(char conv)
{
int i;
/* Check if the conversion character matches conversions table. */
for (i = 0; i < TOTAL_CONVERSIONS; i++){
if (conv == conversions[i]) {
return TRUE;
}
}
return FALSE;
}
/**
* Prints char* on console
*/
int print_console(char *str)
{
int i, len;
char c;
/* Must be connected to the CONSOLE_SERVICE. */
if (con_phone < 0)
return FALSE;
if (len == 0)
return TRUE;
for (i = 0; i < len; i++) {
c = *str++;
async_req_2(con_phone, CONSOLE_PUTCHAR, c, 0, NULL, NULL); /* Do the work. */
}
return TRUE;
}
/**
* Prints int argument with specified conversion inside input string
*/
int print_console_int(char *str, int arg)
{
int i, retval, len, front_side, offset;
char conversion;
char buffer[BUFF_SIZE];
char *front, *back;
if (len == 0)
return TRUE;
front
= malloc((len
+1) * sizeof(char));
back
= malloc((len
+1) * sizeof(char));
front_side = TRUE;
/* Copy front side of source string. */
for (i = 0; i < len; i++) {
if (str[i] == '%') {
front_side = FALSE;
break;
}
front[i] = str[i];
}
/* Character '%' was not found. Print it in a normal way, without int argument. */
if (front_side) {
return (print_console(str));
}
/* Character '%' was the last in the input string - error. */
if (i == (len-1)) {
return FALSE;
}
front[i] = '\0';
conversion = str[++i];
retval = validate_string(conversion);
if (!retval) {
return FALSE;
}
offset = ++i;
/* Copy back side of source string. */
for (;i < len; i++) {
back[i-offset] = str[i];
}
back[i] = '\0';
/* Two characters '%' in source string. */
if (conversion == '%') {
print_console(front);
print_console(back);
return TRUE;
}
/* Other 'normal' case. */
print_console(front);
format_string[1] = conversion;
sprintf(buffer
, format_string
, arg
);
print_console(buffer);
print_console(back);
return TRUE;
}
/**
* }
*/