Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

ODPrntf.c 5.64 KB
Newer Older
rswindell's avatar
rswindell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* OpenDoors Online Software Programming Toolkit
 * (C) Copyright 1991 - 1999 by Brian Pirie.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
Deucе's avatar
Deucе committed
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
rswindell's avatar
rswindell committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 *
 *        File: ODPrntf.c
 *
 * Description: Implements the od_printf() function.
 *
 *   Revisions: Date          Ver   Who  Change
 *              ---------------------------------------------------------------
 *              Oct 13, 1994  6.00  BP   New file header format.
 *              Dec 09, 1994  6.00  BP   Standardized coding style.
 *              Nov 11, 1995  6.00  BP   Removed register keyword.
 *              Nov 14, 1995  6.00  BP   32-bit portability.
 *              Nov 16, 1995  6.00  BP   Removed oddoor.h, added odcore.h.
 *              Dec 12, 1995  6.00  BP   Added entry, exit and kernel macros.
 *              Dec 30, 1995  6.00  BP   Added ODCALL for calling convention.
 *              Jan 03, 1996  6.00  BP   Use ODVCALL instead of ODCALL.
 *              Jan 04, 1996  6.00  BP   Add missing OD_API_EXIT() at end.
 *              Feb 19, 1996  6.00  BP   Changed version number to 6.00.
 *              Mar 03, 1996  6.10  BP   Begin version 6.10.
36
 *              Aug 10, 2003  6.23  SH   *nix support
rswindell's avatar
rswindell committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 */

#define BUILDING_OPENDOORS

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>

#include "OpenDoor.h"
#include "ODCore.h"
#include "ODGen.h"
#include "ODKrnl.h"


/* Size of od_printf() working buffer. Adjust this upwards if you are    */
/* encountering difficulties when calling od_printf() with long strings. */
#define WORK_BUFFER_SIZE   512


/* ----------------------------------------------------------------------------
 * od_printf()
 *
 * The OpenDoors equivalent of the C printf() function, this function performs
 * formatted string output to both the local and remote screens.
 *
 * Parameters: pszFormat - Format string, in the same format as the printf()
 *                         format string.
 *
 *             The semantics of any further parameters are dicated by the
 *             contents of the format string.
 *
 *     Return: void
 */
deuce's avatar
deuce committed
71
ODAPIDEF void ODVCALL od_printf(const char *pszFormat,...)
rswindell's avatar
rswindell committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
{
   va_list pArgumentList;
   static char *pszWorkBuffer = NULL;
   char *pchCurrent;
   char *pchStart;
   BOOL bNotFound;
   INT nCharCount;

   /* Log function entry if running in trace mode. */
   TRACE(TRACE_API, "od_printf()");

   /* Initialize OpenDoors if it hasn't already been done. */
   if(!bODInitialized) od_init();

   OD_API_ENTRY();

   /* Allocate work buffer if none has been allocated yet. */
   if(pszWorkBuffer == NULL &&
      (pszWorkBuffer = malloc(WORK_BUFFER_SIZE)) == NULL)
   {
      /* If we are unable to allocate a buffer, return with a memory error. */
      od_control.od_error = ERR_MEMORY;
      OD_API_EXIT();
      return;
   }

   /* Copy the arguments after the format string. */
   va_start(pArgumentList, pszFormat);

   /* Perform a string printf to the working buffer. */
   vsprintf(pszWorkBuffer, pszFormat, pArgumentList);

   va_end(pArgumentList);

   /* If no color characters are defined, then just display the entire */
   /* buffer in one shot.                                              */
   if(!od_control.od_color_char && !od_control.od_color_delimiter)
       goto quick_print;

   chColorCheck = od_control.od_color_delimiter;

   bNotFound = TRUE;
   pchCurrent = (char *)pszWorkBuffer;
   pchStart = (char *)pszWorkBuffer;
   nCharCount = 0;
   while(*pchCurrent)
   {
      if(*pchCurrent == od_control.od_color_delimiter)
      {
         bNotFound = FALSE;

         if(nCharCount != 0)
         {
            od_disp(pchStart, nCharCount, TRUE);
         }

         if(!*(++pchCurrent))
         {
            chColorCheck = 0;
            OD_API_EXIT();
            return;
         }
         od_set_attrib(od_color_config(pchCurrent));
         if(!*(pchCurrent = (char *)pchColorEndPos))
         {
            chColorCheck = 0;
            OD_API_EXIT();
            return;
         }

         if(!*(++pchCurrent))
         {
            OD_API_EXIT();
            return;
         }
         pchStart = (char *)pchCurrent;
         nCharCount = 0;
      }

      else if(*pchCurrent == od_control.od_color_char)
      {
         bNotFound = FALSE;

         if(nCharCount != 0)
         {
            od_disp(pchStart, nCharCount, TRUE);
         }

         if(!*(++pchCurrent))
         {
            OD_API_EXIT();
            return;
         }
         od_set_attrib(*pchCurrent);

         if(!*(++pchCurrent))
         {
            OD_API_EXIT();
            return;
         }
         pchStart = (char *)pchCurrent;
         nCharCount = 0;
      }
      else
      {
         ++nCharCount;
         ++pchCurrent;
      }
   }

   chColorCheck = 0;

   if(bNotFound)
   {
quick_print:
      /* Display the entire string in one shot. */
      od_disp_str(pszWorkBuffer);
   }
   else if(nCharCount != 0)
   {
      /* If there are remaining characters in the string, then display them. */
      od_disp(pchStart, nCharCount, TRUE);
   }

   OD_API_EXIT();
}