Commit 81b95e1b authored by cvs2snv's avatar cvs2snv
Browse files

This commit was manufactured by cvs2svn to create tag 'clean-2-2'.

parent 05c6abdf
/*
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
*/
definition module ArgEnv
:: EnvironmentVariable
= EnvironmentVariableUndefined
| EnvironmentVariable !.{#Char}
// get the value of an environment variable
getEnvironmentVariable :: !{#Char} -> *EnvironmentVariable
// get the command line, first element is the command name,
// arguments that are interpreted by the run-time system
// (for example to set the heap size) are excluded
getCommandLine :: {.{#Char}}
/*
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
*/
implementation module ArgEnv
import StdEnv
:: CString :== Int
NULL :== 0
:: EnvironmentVariable
= EnvironmentVariableUndefined
| EnvironmentVariable !.{#Char}
getEnv :: !{#Char} -> (!Int, !CString)
getEnv _
= code inline {
ccall ArgEnvGetEnvironmentVariableC "S-II"
}
getEnvironmentVariable :: !{#Char} -> *EnvironmentVariable
getEnvironmentVariable name
| cString == NULL
= EnvironmentVariableUndefined
| otherwise
= EnvironmentVariable (copy size cString)
where
(size, cString)
= getEnv (name +++ "\0")
copy :: !Int !CString -> {#.Char}
copy length cString
= code inline {
create_array_ CHAR 0 1
push_a 0
ccall ArgEnvCopyCStringToCleanStringC "IS-I"
pop_b 1
.end
}
getCommandLineCount :: Int
getCommandLineCount
= code inline {
ccall ArgEnvGetCommandLineCountC "-I"
}
getCommandLineArgument :: !Int -> (!Int, !Int)
getCommandLineArgument _
= code inline {
ccall ArgEnvGetCommandLineArgumentC "I-II"
}
getArg :: !Int -> {#.Char}
getArg i
= copy size cString
where
(size, cString)
= getCommandLineArgument i
getCommandLine :: {.{#Char}}
getCommandLine
= {getArg i \\ i <- [0 .. getCommandLineCount-1]}
/*
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
*/
# ifdef _WINDOWS_
# include <windows.h>
# else
# include <stdlib.h>
# endif
# define kVariableSize 1
# ifndef NULL
# define NULL ((void *) 0)
# endif
/* defined by the Clean run-time system */
extern int global_argc;
extern char **global_argv;
typedef struct
{
int length;
char characters [kVariableSize];
} *CleanString;
static int
CStringLength (char *s)
{
char *begin;
begin = s;
while (*s != '\0')
s++;
return (s - begin);
} /* CStringLength */
# ifdef _WINDOWS_
/* return size of environment variable's value (including null-terminator)
zero if variable doesn't exist */
int
ArgEnvGetEnvironmentVariableSizeC (CleanString name)
{
char smallBuffer;
/* Assume that name has been null-terminated in Clean */
return (GetEnvironmentVariableA (name->characters, &smallBuffer, 0));
} /* ArgEnvGetEnvironmentVariableSizeC */
/* copy value of environment variable to Clean string
return False if value had to be truncated */
int
ArgEnvGetEnvironmentVariableCharsC (CleanString value, CleanString name)
{
int size, length;
length = value->length;
/* Assume that name has been null-terminated in Clean */
size = GetEnvironmentVariableA (name->characters, value->characters, length);
if (size <= length)
{
size = CStringLength (value->characters);
if (size <= length)
value->length = size;
}
return (size >= value->length);
} /* ArgEnvGetEnvironmentVariableCharsC */
# else /* ifndef _WINDOWS_ */
void
ArgEnvGetEnvironmentVariableC (CleanString name, int *sizeP, char **stringP)
{
char *value;
/* Assume that name has been null-terminated in Clean */
value = getenv (name->characters);
if (value == NULL)
*sizeP = 0;
else
*sizeP = CStringLength (value);
*stringP = value;
} /* ArgEnvGetEnvironmentVariableCharsC */
# endif /* _WINDOWS_ */
int
ArgEnvGetCommandLineCountC (void)
{
return (global_argc);
} /* ArgEnvGetCommandLineCountC */
void
ArgEnvGetCommandLineArgumentC (int i, int *sizeP, char **stringP)
{
char *arg;
/* Assume that i is within bounds */
arg = global_argv [i];
*sizeP = CStringLength (arg);
*stringP = arg;
} /* ArgEnvGetCommandLineArgumentC */
/* copy C string to Clean string
return False if string had to be truncated */
int
ArgEnvCopyCStringToCleanStringC (char *cString, CleanString cleanString)
{
int i, length;
char *to;
length = cleanString->length;
to = cleanString->characters;
i = 0;
while (*cString != '\0' && i < length)
{
i++;
*to++ = *cString++;
}
cleanString->length = i;
return (i <= length);
} /* ArgEnvCopyCStringToCleanStringC */
1.0.3
- removed Clean 1.1 compatibility comments
- change inline directives in abc code blocks to Clean 2.0 syntax
1.0.2
- Added Changes.txt
- No other changes for Unix version
1.0.1
- Initial release
\ No newline at end of file
#
# Version 1.0.3
# Ronny Wichers Schreur
# ronny@cs.kun.nl
#
CLM=clm
CC=gcc
COPTIONS=-Wall -pedantic
printenv : printenv.o ArgEnv.o ArgEnvC.o
$(CLM) -b -nt -l ArgEnvC.o printenv -o printenv
ArgEnvC.o : ArgEnvC.c
$(CC) $(COPTIONS) -c ArgEnvC.c
ArgEnv.o : ArgEnv.icl ArgEnv.dcl
$(CLM) -O ArgEnv
printenv.o : printenv.icl ArgEnv.dcl
$(CLM) -O printenv
cleanup :
$(RM) printenv *.o *.abc
ArgEnv
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
The ArgEnv package provides a Clean interface to the command line
arguments and the environment variables.
This is the README for the Unix version. The Unix version has been
tested on Solaris 5.5 and Clean 1.1 & 1.3, but it should work with all
Unix versions with Clean 1.1 or better. To use the package with Clean
version 1.1 you will have to make a few changes, which are documented
in the source.
FILES
README
This file
CHANGES.txt
Changes history
ArgEnv.dcl
Definition of the interface
ArgEnv.icl
Implementation of the interface
ArgEnvC.c
Implementation of the C side of the interface
Makefile
Makefile for the ArgEnv interface and the example program
printenv.icl
An example program that prints the value of an environment
variable
USAGE
To use the ArgEnv interface you have to link the object module that
is created from the module ArgEnvC.c.
If you use clm you should specify this extra object file with the -l
option:
% clm -l ArgEnvC.o <your-main-module>
In the CleanIDE you can add this extra object file to the Object
Modules section in the Options->Link Options dialogue.
If you forget this, you will get the link error:
Undefined first referenced
symbol in file
ArgEnvCopyCStringToCleanStringC ArgEnv.o
ArgEnvGetEnvironmentVariableC ArgEnv.o
ArgEnvGetCommandLineArgumentC ArgEnv.o
ArgEnvGetCommandLineCountC ArgEnv.o
BUGS
There is no way to stop the Clean run-time system from interpreting
some of the command-line arguments.
/*
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
*/
module printenv
import StdEnv, ArgEnv
Start
| argc == 1
= format (getEnvironmentVariable argv.[1])
| otherwise
= "usage: " +++ argv.[0] +++ " <variable>\n"
where
argc
= size argv - 1
argv
= getCommandLine
format EnvironmentVariableUndefined
= ""
format (EnvironmentVariable value)
= value +++ "\n"
/*
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
*/
definition module ArgEnv
:: EnvironmentVariable
= EnvironmentVariableUndefined
| EnvironmentVariable !.{#Char}
// get the value of an environment variable
getEnvironmentVariable :: !{#Char} -> *EnvironmentVariable
// get the command line, first element is the command name,
// arguments that are interpreted by the run-time system
// (for example to set the heap size) are excluded
getCommandLine :: {.{#Char}}
/*
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
*/
implementation module ArgEnv
import code from "ArgEnvC."
import StdEnv
:: CString :== Int
NULL :== 0
:: EnvironmentVariable
= EnvironmentVariableUndefined
| EnvironmentVariable !.{#Char}
getEnvSize :: !{#Char} -> Int
getEnvSize _
= code inline {
ccall ArgEnvGetEnvironmentVariableSizeC "S-I"
}
copyEnv :: !Int !{#Char} -> {#.Char}
copyEnv _ _
= code inline {
create_array_ CHAR 0 1
push_a 1
push_a 1
ccall ArgEnvGetEnvironmentVariableCharsC "SS-I"
pop_b 1
update_a 0 1
pop_a 1
}
getEnvironmentVariable :: !{#Char} -> *EnvironmentVariable
getEnvironmentVariable name
| size == 0
= EnvironmentVariableUndefined
| otherwise
= EnvironmentVariable (copyEnv size name`)
where
size
= getEnvSize name`
name`
= name +++ "\0"
copy :: !Int !CString -> {#.Char}
copy length cString
= code inline {
create_array_ CHAR 0 1
push_a 0
ccall ArgEnvCopyCStringToCleanStringC "IS-I"
pop_b 1
.end
}
getCommandLineCount :: Int
getCommandLineCount
= code inline {
ccall ArgEnvGetCommandLineCountC "-I"
}
getCommandLineArgument :: !Int -> (!Int, !Int)
getCommandLineArgument _
= code inline {
ccall ArgEnvGetCommandLineArgumentC "I-II"
}
getArg :: !Int -> {#.Char}
getArg i
= copy size cString
where
(size, cString)
= getCommandLineArgument i
getCommandLine :: {.{#Char}}
getCommandLine
= {getArg i \\ i <- [0 .. getCommandLineCount-1]}
/*
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
*/
# ifdef _WINDOWS_
# include <windows.h>
# else
# include <stdlib.h>
# endif
# define kVariableSize 1
# ifndef NULL
# define NULL ((void *) 0)
# endif
/* defined by the Clean run-time system */
extern int global_argc;
extern char **global_argv;
typedef struct
{
size_t length;
char characters [kVariableSize];
} *CleanString;
static int
CStringLength (char *s)
{
char *begin;
begin = s;
while (*s != '\0')
s++;
return (s - begin);
} /* CStringLength */
# ifdef _WINDOWS_
/* return size of environment variable's value (including null-terminator)
zero if variable doesn't exist */
int
ArgEnvGetEnvironmentVariableSizeC (CleanString name)
{
char smallBuffer;
/* Assume that name has been null-terminated in Clean */
return (GetEnvironmentVariableA (name->characters, &smallBuffer, 0));
} /* ArgEnvGetEnvironmentVariableSizeC */
/* copy value of environment variable to Clean string
return False if value had to be truncated */
int
ArgEnvGetEnvironmentVariableCharsC (CleanString value, CleanString name)
{
int size, length;
length = value->length;
/* Assume that name has been null-terminated in Clean */
size = GetEnvironmentVariableA (name->characters, value->characters, length);
if (size <= length)
{
size = CStringLength (value->characters);
if (size <= length)
value->length = size;
}
return (size >= value->length);
} /* ArgEnvGetEnvironmentVariableCharsC */
# else /* ifndef _WINDOWS_ */
void
ArgEnvGetEnvironmentVariableC (CleanString name, int *sizeP, char **stringP)
{
char *value;
/* Assume that name has been null-terminated in Clean */
value = getenv (name->characters);
if (value == NULL)
*sizeP = 0;
else
*sizeP = CStringLength (value);
*stringP = value;
} /* ArgEnvGetEnvironmentVariableCharsC */
# endif /* _WINDOWS_ */
int
ArgEnvGetCommandLineCountC (void)
{
return (global_argc);
} /* ArgEnvGetCommandLineCountC */
void
ArgEnvGetCommandLineArgumentC (int i, int *sizeP, char **stringP)
{
char *arg;
/* Assume that i is within bounds */
arg = global_argv [i];
*sizeP = CStringLength (arg);
*stringP = arg;
} /* ArgEnvGetCommandLineArgumentC */
/* copy C string to Clean string
return False if string had to be truncated */
int
ArgEnvCopyCStringToCleanStringC (char *cString, CleanString cleanString)
{
int i, length;
char *to;
length = cleanString->length;
to = cleanString->characters;
i = 0;
while (*cString != '\0' && i < length)
{
i++;
*to++ = *cString++;
}
cleanString->length = i;
return (i <= length);
} /* ArgEnvCopyCStringToCleanStringC */
1.0.3
- removed Clean 1.1 compatibility comments
- change inline directives in abc code blocks to Clean 2.0 syntax
1.0.2
- Added import of the object file
- Changed README (require Clean 1.3.2, simplified usage instructions)
1.0.1
- Initial release
\ No newline at end of file
ArgEnv
Version 1.0.3
Ronny Wichers Schreur
ronny@cs.kun.nl
The ArgEnv package provides a Clean interface to the command line
arguments and the environment variables.
This is the README for the Windows version. The Windows version has
been tested on Windows XP with Clean 2.1, but it should work with
all Windows versions with Clean 2.0 or better.
FILES
README.txt
This file
CHANGES.txt
Changes history
ArgEnv.dcl
Definition of the interface
ArgEnv.icl
Implementation of the interface
ArgEnvC.c
Implementation of the C side of the interface
Clean System Files/ArgEnvC.obj
Object file from ArgEnvC.c
printenv.icl
An example program that prints the value of an environment
variable
printenv.prj
Project file for the example program