Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / libcaptive / kd / kdebug.c
1 /* $Id$
2  * reactos spinlock emulation of libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "reactos/internal/kd.h"        /* self */
23 #include "captive/unicode.h"    /* for captive_validate_AnsiString() */
24 #include <glib/gtypes.h>
25 #include <glib/gmessages.h>
26 #include <glib/galloca.h>
27 #include <glib/gstring.h>
28 #include <glib/gmem.h>
29
30
31 /**
32  * KdDebuggerEnabled:
33  *
34  * #BOOLEAN variable defined to %FALSE by libcaptive.
35  * Currently just enables some debug output in
36  * reactos/ntoskrnl/ldr/loader.c
37  */
38 BOOLEAN /* __declspec(dllexport) */ KdDebuggerEnabled=FALSE;
39
40 /**
41  * KdDebugState:
42  *
43  * Defined to %KD_DEBUG_DISABLED by default in reactos.
44  * Currently just enables some debug output in
45  * reactos/ntoskrnl/ldr/loader.c
46  */
47 ULONG KdDebugState=KD_DEBUG_GDB;
48
49
50 /* storage for the trailing string part without newline */
51 static GString *KdpPrintString_buffer;
52
53 /**
54  * KdpPrintString:
55  * @String: String text to print. Buffer is flushed only when newline seen.
56  *
57  * Prints the specified text string to the console. libcaptive
58  * accepts any newline characters as valid and autonewlines the text
59  * if needed.
60  */
61 ULONG KdpPrintString(PANSI_STRING String)
62 {
63 gchar *out_base,*out;
64 const CHAR *in;
65
66         g_return_val_if_fail(captive_validate_AnsiString(String),0);
67
68         out_base=g_alloca(((size_t)String->Length)+1);
69         out=out_base;
70         for (in=String->Buffer;*in;in++) {
71                 if (*in=='\r' && in[1]=='\n')
72                         in++;
73                 if (*in=='\r' || *in=='\n')
74                         *out++='\n';
75                 else
76                         *out++=*in;
77                 }
78         *out='\0';
79         
80         /* Print all completed lines */
81         if ((out=strrchr(out_base,'\n'))) {
82 gchar *previous;
83
84                 *out='\0';      /* cut off the rest of string */
85
86                 if (!KdpPrintString_buffer)
87                         previous=NULL;
88                 else {
89                         previous=g_string_free(KdpPrintString_buffer,
90                                         FALSE);  /* free_segment */
91                         KdpPrintString_buffer=NULL;
92                         }
93                 g_message("KdpPrintString: %s%s",       /* newline is appended by g_message() */
94                                 (!previous ? "" : previous),
95                                 out_base);
96                 if (previous)
97                         g_free(previous);
98
99                 out_base=out+1; /* leave the rest of string (if any) */
100                 }
101
102         /* store/append the remaining rest of string w/o newline */
103         if (*out_base)
104                 KdpPrintString_buffer=(!KdpPrintString_buffer ? g_string_new(out_base)
105                                 : g_string_append(KdpPrintString_buffer,out_base));
106
107         return (ULONG)String->Length;
108 }