473,569 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

weird but interesting code - needs help

vib
Hi there,

I am looking at this code for quite awhile, still don't any clue why
one wants to do it this way. It is about display message through UART
or COM port. Here is the code.

The calling code:
[1] MyDbgPrintf(("F ile Write Error\n"));

The called code:
[2] #define MyDbgPrintf(x) UART_Printf x

MOre, the body of UART_Printf:

[3]
void UART_Printf( const char *fmt, ... )
{
v_list ap;
char string[1024];

if( gUart_init == TRUE )
{
v_start( ap, fmt );
vsprintf( string, fmt, ap );
UART_SendString ( string );
v_end( ap );
}
}

And the
[3]
void UART_SendString (char *string )
{
if( gUart_init == TRUE )
{
while( *string )
UART_SendByte( *string++ );
}
}

[4]
typedef int *v_list[1];

[5]
#define v_start(ap, parmN) (void)(*(ap) = __va_start(parm N))
[6]
#define v_end(ap) ((void)(*(ap) = 0))
Any comments?

what are [4], [5] [6] and why?

Thanks in advance.

Nov 15 '05 #1
4 1546
In article <11************ *********@f14g2 000cwb.googlegr oups.com>,
vib <vi*****@gmail. com> wrote:
I am looking at this code for quite awhile, still don't any clue why
one wants to do it this way. It is about display message through UART
or COM port. v_list ap; v_start( ap, fmt );
vsprintf( string, fmt, ap );
UART_SendString ( string );
v_end( ap ); [4]
typedef int *v_list[1]; [5]
#define v_start(ap, parmN) (void)(*(ap) = __va_start(parm N)) [6]
#define v_end(ap) ((void)(*(ap) = 0)) Any comments? what are [4], [5] [6] and why?

I suggest that you read the documentation about the elipsis
(...) prototype and the mechanisms by which one accesses variable
number of arguments. The relevant man page on unix-type systems
is often named stdarg

[4], [5], and [6] are all internal workings of a mechanism that
you should not be poking around in unless you are trying to
implement a C compiler. There are different expansions for them
on different systems.
By the way, the program has a potential buffer overflow problem.
If UART_Printf() is handed a parameter list that expands to more
than 1023 characters, the program will likely corrupt memory.
--
Programming is what happens while you're busy making other plans.
Nov 15 '05 #2
vib
Any benefits of using this method? Just thinking using a simple, easy
understand method, like writting to a circular buffer, and the TX empty
interrupt may read the circular buffer and send it to the UART. Simple
yet easy to implement.

Nov 15 '05 #3
In article <11************ *********@f14g2 000cwb.googlegr oups.com>,
vib <vi*****@gmail. com> wrote:
Any benefits of using this method?
Which method? You did not quote any context, and I've read approximately
118 postings and 1063 email messages since your previous message.
Just thinking using a simple, easy
understand method, like writting to a circular buffer, and the TX empty
interrupt may read the circular buffer and send it to the UART. Simple
yet easy to implement.


UART... TX buffer... lemme rummage through my memory.

The layer you are neglecting is the -formatting- of the data to
write to the buffer. That's what the code you pointed to before
takes care of. It isn't until about 3 layers down that it gets to
the transmission of bytes to the UART, and it does that by way of
the UART_Send routine that we aren't shown the implementation of.
Whether the UART_Send works on a circular buffer or something else
is transparent to this level of code.

You shouldn't be looking at the va_* code as having anything to do
with the complicating of the routine. It -is- a "simple, easy understand"
method at the upper level. Your code just calls the debug
printf layer and passes whatever format string and argument that
it needs, and everything else is automagically taken care of.
You only need to get down to the lower level if you are the person
who has to implement the circular buffer or whatever...

If you -are- the person who needs to implement a circular buffer,
then consider exactly what the user interface would be. If the
circular buffer is partly full at the moment, and I ask to add
something to it, then suppose what I'm adding is too big to fit
into the buffer all at one time. What is your routine going to do then?
Reject the entire string that I want to add? Add what it can
and return the count (or buffer pointer) to the rest? Add what
it can at the moment, sleep a bit and add some more, and so on until
the entire message is inserted? One of the simplest insertion
mechanisms is to check to see if there is room for a byte,
add it if so, and if not then wait for a signal from the writing
routine saying it pulled something out of the buffer (or for
a sanity alarm in case that signal got lost somehow.) And that's
effectively the interface that this code has presented, with it's
preperation of the output string first and then looping sending each
byte in sequence using some unspecified transmission mechanism.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 15 '05 #4
vib
Thanks for the lengthy explanation, really appreciate that.

Walter Roberson wrote:
In article <11************ *********@f14g2 000cwb.googlegr oups.com>,
vib <vi*****@gmail. com> wrote:
Any benefits of using this method?


Which method? You did not quote any context, and I've read approximately
118 postings and 1063 email messages since your previous message.
Just thinking using a simple, easy
understand method, like writting to a circular buffer, and the TX empty
interrupt may read the circular buffer and send it to the UART. Simple
yet easy to implement.


UART... TX buffer... lemme rummage through my memory.

The layer you are neglecting is the -formatting- of the data to
write to the buffer. That's what the code you pointed to before
takes care of. It isn't until about 3 layers down that it gets to
the transmission of bytes to the UART, and it does that by way of
the UART_Send routine that we aren't shown the implementation of.
Whether the UART_Send works on a circular buffer or something else
is transparent to this level of code.

You shouldn't be looking at the va_* code as having anything to do
with the complicating of the routine. It -is- a "simple, easy understand"
method at the upper level. Your code just calls the debug
printf layer and passes whatever format string and argument that
it needs, and everything else is automagically taken care of.
You only need to get down to the lower level if you are the person
who has to implement the circular buffer or whatever...

If you -are- the person who needs to implement a circular buffer,
then consider exactly what the user interface would be. If the
circular buffer is partly full at the moment, and I ask to add
something to it, then suppose what I'm adding is too big to fit
into the buffer all at one time. What is your routine going to do then?
Reject the entire string that I want to add? Add what it can
and return the count (or buffer pointer) to the rest? Add what
it can at the moment, sleep a bit and add some more, and so on until
the entire message is inserted? One of the simplest insertion
mechanisms is to check to see if there is room for a byte,
add it if so, and if not then wait for a signal from the writing
routine saying it pulled something out of the buffer (or for
a sanity alarm in case that signal got lost somehow.) And that's
effectively the interface that this code has presented, with it's
preperation of the output string first and then looping sending each
byte in sequence using some unspecified transmission mechanism.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson


Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1967
by: Jim Dawson | last post by:
I was writing a subroutine to extract fields from lines of text when I ran into an issue. I have reproduced this error on Perl 5.8 on AIX, 5.8 on Linux and 5.6 on Windows. ############### CUT HERE ############### #!/usr/bin/perl -w my @list = ("field1 field2 field3"); sub stripws($)
2
1984
by: jwbeaty | last post by:
Here's a weird one. I'm running SQL Server 7 and when I run a backup something weird happens. When I perform the backup via Enterprise Manager by right clicking on the database I want to backup, I click on OK but no progress blocks show up in the window showing you the status of the backup. The completion window pops up saying that the DB...
0
1437
by: Zwyatt | last post by:
having a really weird little bug w/ time_t...check it out: I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum;
10
1820
by: Don Munroe | last post by:
This one has me stumped. I have three web applications running on two different servers. The first that works fine is hosted by a .Net hosting company. Everyone that uses it has no problems hitting the site. The other two applications are running on my personal server which has Windows 2003, IIS 6, and .Net 1.1 Most of my users can use...
41
2496
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for my purposes. As this code was posted long time ago (November 1998) I would like to ask if the principles used in this code are still valid in the...
4
1486
by: sparks | last post by:
If Not IsNull(DLookup(, "tblDemographic", " = """ & Me.Text9 & """")) Then Cancel = True MsgBox "Duplicate Value is Not Allowed" ActiveControl.Undo DoCmd.RunCommand acCmdUndo End If I try to do this but when you enter a text value such as A you get
1
2149
by: PeaceManGroove | last post by:
It's a long story, but our application needs to run in Access 97. All of the computers in our organisation also run Access 2003. . . C++ code launches our Access reports via a VB 6.0 program. I added code to the VB6 to specify Access.Application.8 object. This was meant to ensure that our reports open in Access 97 and not Access 2003. ...
6
1384
by: Lucas Kanebley Tavares | last post by:
Hello all, I have a templatized class which has an attribute as: "T *data", all constructors initialize it to zero, and then allocate memory for the array (and that IS done correctly, I've checked). What I find it strange, is that at one point I have to reallocate the data, but if I put this in my operator= overload: if (data != 0)...
6
1893
by: Emiurgo | last post by:
Hi there to everyone! I've got a problem which may be interesting to some of you, and I'd be very grateful if there is someone who can give me some advice (or maybe redirect me to some other place). Very brief introduction. Since I'm learning vectorization with SSE (I use gcc 4.1.3 on Ubuntu 7.10) I've built a simple test program in C for...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.