[comp.lang.c] Why all this fuss about CTRL

jmsellens@watdragon.UUCP (12/05/86)

There seems to have been a long and somewhat futile discussion about
a macro to give control characters.  Some say use "CTRL(X)", some
say "CTRL('X')".  Here's a wonderful way, that is guaranteed to
be portable, easy to type, and easy to understand.

Make a file called ctrl.h with lines like
#define CTRLA	'\001'
in it (should take mere seconds).  Put #include "ctrl.h" in your
C source files and use CTRLA.  No fuss, no muss.  Easy to understand.
Portable. And it even works.

paul@devon.UUCP (Paul Sutcliffe Jr.) (12/07/86)

In article <1955@watdragon.UUCP>, jmsellens@watdragon.UUCP (John M. Sellens) writes:
> Make a file called ctrl.h with lines like
> #define CTRLA	'\001'
> in it (should take mere seconds).  Put #include "ctrl.h" in your
> C source files and use CTRLA.  No fuss, no muss.  Easy to understand.
> Portable. And it even works.

That's fine if you know ahead of time what control characters you 
want to play with.  However, if I want to find the ``control'' value
of a variable, as in:

	foo()
	{
		char	c, cc;
		...
		cc = CTRL(c);
		...
	}

then your method, although good for what it was intended, won't do.

-Paul

-- 
Paul Sutcliffe, Jr.	 UUCP: {seismo,ihnp4,allegra,rutgers}!cbmvax!devon!paul
Devon Computer Services  COMPUSERVE: 76176,502
Allentown, Penna.	 Sarek: "Any message for your mother, Spock?"
+1 215 398 3776 	 Spock: "Yes. Tell her 'I feel fine.'"

marcus@ihlpl.UUCP (Hall) (12/15/86)

In article <184@devon.UUCP> paul@devon.UUCP (Paul Sutcliffe Jr.) writes:
>In article <1955@watdragon.UUCP>, jmsellens@watdragon.UUCP (John M. Sellens) writes:
>> Make a file called ctrl.h with lines like
>> #define CTRLA	'\001'
>> in it (should take mere seconds).  Put #include "ctrl.h" in your
>> C source files and use CTRLA.  No fuss, no muss.  Easy to understand.
>> Portable. And it even works.
>
>That's fine if you know ahead of time what control characters you 
>want to play with.  However, if I want to find the ``control'' value
>of a variable, as in:
>	..[example]..
>then your method, although good for what it was intended, won't do.

But then, neither did the initial definition for CTRL(X) that started
this whole mess.

Recall the initial definition:

#define CTRL(X) ('X' ^ 0100)

or something very similar that works only for the Reiser cpp (& similar).

John Sellens' idea for cntrl.h leads to yet another possibility that maybe
would implement CTRL for either cpp (Reiser or ANSI) in a way that would
be compatible with the original definition (i.e. no 's required).

Suppose that you have a file ctrl.h that contains:

#define __CTRLA '\001'
#define __CTRLB '\002'
...
#define __CTRLZ '\003'

#define CTRL(X) __CTRL/**/X	/* Reiser cpp */
#define CTRL(X) __CTRL//**/**/X	/* (May need to be this, to preserve X */

#define CTRL(X) __CTRL # X	/* ANSI cpp */

This would still have some problems with things like CTRL(@) and CTRL(?),
but it comes close..

Marcus Hall
..!ihnp4!ihlpl!marcus

arnold@emory.UUCP (12/23/86)

I hope somebody hasn't beat me to this. The whole fuss about

#define CTRL(X)	('X' & 037)	/* ascii! */

in the Reiser cpp not being doable in ANSI C can be answered this way:

#define CTRL(X)	(' ## X ## ' & 037)	/* ascii only */

which uses the token concatenation operator to produce the character
constant. This should also work for things like

	esc = CTRL([);

I am sure someone will correct me if I am wrong. Now, we can hopefully put
this to rest!

( Personally, I think CTRL(X) is bad style;

#define ctrl(x)	((x) & 037)

and using ctrl('x') makes the most sense to me. It is legal in both K&R C and
ANSI C, and allows using the macro on a char or int variable, not just on
a constant. So there. :-)
-- 
Arnold Robbins
CSNET:	arnold@emory	BITNET:	arnold@emoryu1
ARPA:	arnold%emory.csnet@csnet-relay.arpa
UUCP:	{ akgua, decvax, gatech, sb1, sb6, sunatl }!emory!arnold

meissner@dg_rtp.UUCP (Michael Meissner) (12/31/86)

In article <1971@emory.UUCP> arnold@emory.UUCP (Arnold D. Robbins {EUCC}) writes:
> I hope somebody hasn't beat me to this. The whole fuss about
>
> #define CTRL(X)	('X' & 037)	/* ascii! */
>
> in the Reiser cpp not being doable in ANSI C can be answered this way:
>
> #define CTRL(X)	(' ## X ## ' & 037)	/* ascii only */
>
> which uses the token concatenation operator to produce the character
> constant. ....

This will not work with the ANSI preprocessor, because the ANSI preprocessor
only deals with tokens, not atoms (except for string-izing).  The above
would try to create a character constant with 8 characters in it.

-- 
	Michael Meissner, Data General
	...mcnc!rti-sel!dg_rtp!meissner