diff options
Diffstat (limited to 'src')
125 files changed, 18069 insertions, 18091 deletions
diff --git a/src/Grammar b/src/Grammar index 574acd6..a6169d1 100644 --- a/src/Grammar +++ b/src/Grammar @@ -1,26 +1,26 @@ # Grammar for Python, version 4 # Changes compared to version 3: -# Removed 'dir' statement. -# Function call argument is a testlist instead of exprlist. +# Removed 'dir' statement. +# Function call argument is a testlist instead of exprlist. # Changes compared to version 2: -# The syntax of Boolean operations is changed to use more -# conventional priorities: or < and < not. +# The syntax of Boolean operations is changed to use more +# conventional priorities: or < and < not. # Changes compared to version 1: -# modules and scripts are unified; -# 'quit' is gone (use ^D); -# empty_stmt is gone, replaced by explicit NEWLINE where appropriate; -# 'import' and 'def' aren't special any more; -# added 'from' NAME option on import clause, and '*' to import all; -# added class definition. +# modules and scripts are unified; +# 'quit' is gone (use ^D); +# empty_stmt is gone, replaced by explicit NEWLINE where appropriate; +# 'import' and 'def' aren't special any more; +# added 'from' NAME option on import clause, and '*' to import all; +# added class definition. # Start symbols for the grammar: -# single_input is a single interactive statement; -# file_input is a module or sequence of commands read from an input file; -# expr_input is the input for the input() function; -# eval_input is the input for the eval() function. +# single_input is a single interactive statement; +# file_input is a module or sequence of commands read from an input file; +# expr_input is the input for the input() function; +# eval_input is the input for the eval() function. # NB: compound_stmt in single_input is followed by extra NEWLINE! single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE @@ -34,7 +34,7 @@ fplist: fpdef (',' fpdef)* fpdef: NAME | '(' fplist ')' stmt: simple_stmt | compound_stmt -simple_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt +simple_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt expr_stmt: (exprlist '=')* exprlist NEWLINE # For assignments, additional restrictions enforced by the interpreter print_stmt: 'print' (test ',')* [test] NEWLINE diff --git a/src/Makefile b/src/Makefile index 3811b77..e07d1c8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,17 +1,17 @@ # /*********************************************************** # Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The # Netherlands. -# -# All Rights Reserved -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose and without fee is hereby granted, +# +# All Rights Reserved +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose and without fee is hereby granted, # provided that the above copyright notice appear in all copies and that -# both that copyright notice and this permission notice appear in +# both that copyright notice and this permission notice appear in # supporting documentation, and that the names of Stichting Mathematisch # Centrum or CWI not be used in advertising or publicity pertaining to # distribution of the software without specific, written prior permission. -# +# # STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND # FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE @@ -19,7 +19,7 @@ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# +# # ******************************************************************/ # Makefile for Python @@ -27,17 +27,17 @@ # # If you are in a hurry, you can just edit this Makefile to choose the # correct settings for SYSV and RANLIB below, and type "make" in this -# directory. If you are using a recent version of SunOS (or Ultrix?) +# directory. If you are using a recent version of SunOS (or Ultrix?) # you don't even have to edit: the Makefile comes pre-configured for # such systems with all configurable options turned off, building the # minimal portable version of the Python interpreter. # # If have more time, read the section on configurable options below. # It may still be wise to begin building the minimal portable Python, -# to see if it works at all, and select options later. You don't have +# to see if it works at all, and select options later. You don't have # to rebuild all objects when you turn on options; all dependencies # are concentrated in the file "config.c" which is rebuilt whenever -# the Makefile changes. (Except if you turn on the GNU Readline option +# the Makefile changes. (Except if you turn on the GNU Readline option # you may have to toss out the tokenizer.o object.) @@ -45,74 +45,72 @@ # =========================================== # Uncomment the following line if you are using a System V derivative. -# This must be used, for instance, on an SGI IRIS. Don't use it for -# SunOS. (This is only needed by posixmodule.c...) +# This must be used, for instance, on an SGI IRIS. Don't use it for +# SunOS. (This is only needed by posixmodule.c...) -#SYSVDEF= -DSYSV +#SYSVDEF= -DSYSV # Choose one of the following two lines depending on whether your system # requires the use of 'ranlib' after creating a library, or not. -#RANLIB = true # For System V -RANLIB = ranlib # For BSD +#RANLIB = true # For System V +RANLIB = ranlib # For BSD # If your system doesn't have symbolic links, uncomment the following # line. -#NOSYMLINKDEF= -DNO_LSTAT +#NOSYMLINKDEF= -DNO_LSTAT # Installation Options # ==================== -# You may want to change DEFPYTHONPATH to reflect where you install the -# Python module library. The default contains "../lib" so running -# the interpreter from the source/build directory as distributed will -# find the library (admittedly a hack). +# You may want to change PYTHONPATH to reflect where you install the +# Python module library. -DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib +PYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python # For "Pure" BSD Systems # ====================== # # "Pure" BSD systems (as opposed to enhanced BSD derivatives like SunOS) -# often miss certain standard library functions. Source for -# these is provided, you just have to turn it on. This may work for +# often miss certain standard library functions. Source for +# these is provided, you just have to turn it on. This may work for # other systems as well, where these things are needed. # If your system does not have a strerror() function in the library, -# uncomment the following two lines to use one I wrote. (Actually, this +# uncomment the following two lines to use one I wrote. (Actually, this # is missing in most systems I have encountered, so it is turned on -# in the Makefile. Turn it off if your system doesn't have sys_errlist.) +# in the Makefile. Turn it off if your system doesn't have sys_errlist.) -# STRERROR_SRC= strerror.c -# STRERROR_OBJ= strerror.o +STRERROR_SRC= strerror.c +STRERROR_OBJ= strerror.o # If your BSD system does not have a fmod() function in the library, # uncomment the following two lines to use one I wrote. -#FMOD_SRC= fmod.c -#FMOD_OBJ= fmod.o +#FMOD_SRC= fmod.c +#FMOD_OBJ= fmod.o # If your BSD system does not have a strtol() function in the library, # uncomment the following two lines to use one I wrote. -#STRTOL_SRC= strtol.c -#STRTOL_OBJ= strtol.o +#STRTOL_SRC= strtol.c +#STRTOL_OBJ= strtol.o # If your BSD system does not have a getcwd() function in the library, # but it does have a getwd() function, uncomment the following two lines -# to use one I wrote. (If you don't have getwd() either, turn on the +# to use one I wrote. (If you don't have getwd() either, turn on the # NO_GETWD #define in getcwd.c.) -#GETCWD_SRC= getcwd.c -#GETCWD_OBJ= getcwd.o +#GETCWD_SRC= getcwd.c +#GETCWD_OBJ= getcwd.o # If your signal() function believes signal handlers return int, # uncomment the following line. -#SIGTYPEDEF= -DSIGTYPE=int +#SIGTYPEDEF= -DSIGTYPE=int # Further porting hints @@ -122,7 +120,7 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # <strings.h>, create a file "string.h" in this directory which contains # the single line "#include <strings.h>", and add "-I." to CFLAGS. # If you don't have the functions strchr and strrchr, add definitions -# "-Dstrchr=index -Dstrrchr=rindex" to CFLAGS. (NB: CFLAGS is not +# "-Dstrchr=index -Dstrrchr=rindex" to CFLAGS. (NB: CFLAGS is not # defined in this Makefile.) @@ -130,14 +128,14 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # ==================== # # Python can be configured to interface to various system libraries that -# are not available on all systems. It is also possible to configure +# are not available on all systems. It is also possible to configure # the input module to use the GNU Readline library for interactive -# input. For each configuration choice you must uncomment the relevant -# section of the Makefile below. Note: you may also have to change a +# input. For each configuration choice you must uncomment the relevant +# section of the Makefile below. Note: you may also have to change a # pathname and/or an architecture identifier that is hardcoded in the # Makefile. # -# Read the comments to determine if you can use the option. (You can +# Read the comments to determine if you can use the option. (You can # always leave all options off and build a minimal portable version of # Python.) @@ -153,7 +151,7 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # # Uncomment the following line to select this option. -#BSDTIMEDEF= -DBSD_TIME +#BSDTIMEDEF= -DBSD_TIME # GNU Readline Option @@ -162,7 +160,7 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # If you have the sources of the GNU Readline library you can have # full interactive command line editing and history in Python. # The GNU Readline library is distributed with the BASH shell -# (I only know of version 1.05). You must build the GNU Readline +# (I only know of version 1.05). You must build the GNU Readline # library and the alloca routine it needs in their own source # directories (which are subdirectories of the basg source directory), # and plant a pointer to the BASH source directory in this Makefile. @@ -172,13 +170,13 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # You may have to fix the definition of LIBTERMCAP; leave the LIBALLOCA # definition commented if alloca() is in your C library. -#BASHDIR= ../../bash-1.05 -#LIBREADLINE= $(BASHDIR)/readline/libreadline.a -#LIBALLOCA= $(BASHDIR)/alloc-files/alloca.o -#LIBTERMCAP= -ltermcap -#RL_USE = -DUSE_READLINE -#RL_LIBS= $(LIBREADLINE) $(LIBALLOCA) $(LIBTERMCAP) -#RL_LIBDEPS= $(LIBREADLINE) $(LIBALLOCA) +#BASHDIR= ../../bash-1.05 +#LIBREADLINE= $(BASHDIR)/readline/libreadline.a +#LIBALLOCA= $(BASHDIR)/alloc-files/alloca.o +#LIBTERMCAP= -ltermcap +#RL_USE = -DUSE_READLINE +#RL_LIBS= $(LIBREADLINE) $(LIBALLOCA) $(LIBTERMCAP) +#RL_LIBDEPS= $(LIBREADLINE) $(LIBALLOCA) # STDWIN Option @@ -190,22 +188,22 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # # Uncomment and edit the following block to use the STDWIN option. # - Edit the STDWINDIR defition to reflect the top of the STDWIN source -# tree. +# tree. # - Edit the ARCH definition to reflect your system's architecture -# (usually the program 'arch' or 'machine' returns this). +# (usually the program 'arch' or 'machine' returns this). # You may have to edit the LIBX11 defition to reflect the location of # the X11 runtime library if it is non-standard. -#STDWINDIR= ../../stdwin -#ARCH= sgi -#LIBSTDWIN= $(STDWINDIR)/Build/$(ARCH)/x11/lib/lib.a -#LIBX11 = -lX11 -#STDW_INCL= -I$(STDWINDIR)/H -#STDW_USE= -DUSE_STDWIN -#STDW_LIBS= $(LIBSTDWIN) $(LIBX11) -#STDW_LIBDEPS= $(LIBSTDWIN) -#STDW_SRC= stdwinmodule.c -#STDW_OBJ= stdwinmodule.o +#STDWINDIR= ../../stdwin +#ARCH= sgi +#LIBSTDWIN= $(STDWINDIR)/Build/$(ARCH)/x11/lib/lib.a +#LIBX11 = -lX11 +#STDW_INCL= -I$(STDWINDIR)/H +#STDW_USE= -DUSE_STDWIN +#STDW_LIBS= $(LIBSTDWIN) $(LIBX11) +#STDW_LIBDEPS= $(LIBSTDWIN) +#STDW_SRC= stdwinmodule.c +#STDW_OBJ= stdwinmodule.o # Amoeba Option @@ -214,31 +212,31 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # If you have the Amoeba 4.0 distribution (Beta or otherwise) you can # configure Python to incorporate the built-in module 'amoeba'. # (Python can also be built for native Amoeba, but it requires more -# work and thought. Contact the author.) +# work and thought. Contact the author.) # # Uncomment and edit the following block to use the Amoeba option. # - Edit the AMOEBADIR defition to reflect the top of the Amoeba source -# tree. +# tree. # - Edit the AM_CONF definition to reflect the machine/operating system -# configuration needed by Amoeba (this is the name of a subdirectory -# of $(AMOEBADIR)/conf/unix, e.g., vax.ultrix). +# configuration needed by Amoeba (this is the name of a subdirectory +# of $(AMOEBADIR)/conf/unix, e.g., vax.ultrix). -#AMOEBADIR= /usr/amoeba -#AM_CONF= mipseb.irix -#LIBAMUNIX= $(AMOEBADIR)/conf/unix/$(AM_CONF)/lib/amunix/libamunix.a -#AM_INCL= -I$(AMOEBADIR)/src/h -#AM_USE = -DUSE_AMOEBA -#AM_LIBDEPS= $(LIBAMUNIX) -#AM_LIBS= $(LIBAMUNIX) -#AM_SRC = amoebamodule.c sc_interpr.c sc_errors.c -#AM_OBJ = amoebamodule.o sc_interpr.o sc_errors.o +#AMOEBADIR= /usr/amoeba +#AM_CONF= mipseb.irix +#LIBAMUNIX= $(AMOEBADIR)/conf/unix/$(AM_CONF)/lib/amunix/libamunix.a +#AM_INCL= -I$(AMOEBADIR)/src/h +#AM_USE = -DUSE_AMOEBA +#AM_LIBDEPS= $(LIBAMUNIX) +#AM_LIBS= $(LIBAMUNIX) +#AM_SRC = amoebamodule.c sc_interpr.c sc_errors.c +#AM_OBJ = amoebamodule.o sc_interpr.o sc_errors.o # Silicon Graphics IRIS Options # ============================= # # The following three options are only relevant if you are using a -# Silicon Graphics IRIS machine. These have been tested with IRIX 3.3.1 +# Silicon Graphics IRIS machine. These have been tested with IRIX 3.3.1 # on a 4D/25. @@ -246,28 +244,22 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # ========= # # This option incorporates the built-in module 'gl', which provides a -# complete interface to the Silicon Graphics GL library. It adds +# complete interface to the Silicon Graphics GL library. It adds # about 70K to the Python text size and about 260K to the unstripped # binary size. # -# NOTE WHEN BUILDING FOR THE FIRST TIME: -# There is a circular dependency in the build process: you need to have -# a working Python interpreter before you can build a Python interpreter -# that incorporates the 'gl' module -- the source file 'glmodule.c' is -# not distributed (it's about 140K!) and a Python script is used to -# create it. Thus, you first have to build python without the the GL -# and Panel options, then edit the Makefile to turn them (or at least GL) -# on and rebuild. You may also have to set PYTHONPATH to point to -# the place where the module library is for the generation script to -# work. +# Note: the file 'glmodule.c' is created by a Python script. If you +# lost the file and have no working Python interpreter, turn off the GL +# and Panel options, rebuild the Python interpreter, use it to create +# glmodule.c, and then turn the options back on. # # Uncomment the following block to use the GL option. -#GL_USE = -DUSE_GL -#GL_LIBDEPS= -#GL_LIBS= -lgl_s -#GL_SRC = glmodule.c cgensupport.c -#GL_OBJ = glmodule.o cgensupport.o +#GL_USE = -DUSE_GL +#GL_LIBDEPS= +#GL_LIBS= -lgl_s +#GL_SRC = glmodule.c cgensupport.c +#GL_OBJ = glmodule.o cgensupport.o # Panel Option @@ -276,25 +268,23 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # If you have source to the NASA Ames Panel Library, you can configure # Python to incorporate the built-in module 'pnl', which is used byu # the standard module 'panel' to provide an interface to most features -# of the Panel Library. This option requires that you also turn on the -# GL option. It adds about 100K to the Python text size and about 160K -# to the unstripped binary size. This requires Panel Library version 9.7 -# (for lower versions you may have to remove some functionality -- send -# me the patches if you bothered to do this). +# of the Panel Library. This option requires that you also turn on the +# GL option. It adds about 100K to the Python text size and about 160K +# to the unstripped binary size. # # Uncomment and edit the following block to use the Panel option. # - Edit the PANELDIR definition to point to the top-level directory -# of the Panel distribution tree. +# of the Panel distribution tree. -#PANELDIR= /usr/people/guido/src/pl -#PANELLIBDIR= $(PANELDIR)/library -#LIBPANEL= $(PANELLIBDIR)/lib/libpanel.a -#PANEL_USE= -DUSE_PANEL -#PANEL_INCL= -I$(PANELLIBDIR)/include -#PANEL_LIBDEPS= $(LIBPANEL) -#PANEL_LIBS= $(LIBPANEL) -#PANEL_SRC= panelmodule.c -#PANEL_OBJ= panelmodule.o +#PANELDIR= /usr/people/guido/src/pl +#PANELLIBDIR= $(PANELDIR)/library +#LIBPANEL= $(PANELLIBDIR)/lib/libpanel.a +#PANEL_USE= -DUSE_PANEL +#PANEL_INCL= -I$(PANELLIBDIR)/include +#PANEL_LIBDEPS= $(LIBPANEL) +#PANEL_LIBS= $(LIBPANEL) +#PANEL_SRC= panelmodule.c +#PANEL_OBJ= panelmodule.o # Audio Option @@ -304,139 +294,140 @@ DEFPYTHONPATH= .:/usr/local/lib/python:/ufs/guido/lib/python:../lib # It incorporates the built-in module 'audio'. # Warning: using the asynchronous I/O facilities of this module can # create a second 'thread', which looks in the listings of 'ps' like a -# forked child. However, it shares its address space with the parent. +# forked child. However, it shares its address space with the parent. # # Uncomment the following block to use the Audio option. -#AUDIO_USE= -DUSE_AUDIO -#AUDIO_SRC= audiomodule.c asa.c -#AUDIO_OBJ= audiomodule.o asa.o +#AUDIO_USE= -DUSE_AUDIO +#AUDIO_SRC= audiomodule.c asa.c +#AUDIO_OBJ= audiomodule.o asa.o # Major Definitions # ================= -STANDARD_OBJ= acceler.o bltinmodule.o ceval.o classobject.o \ - compile.o dictobject.o errors.o fgetsintr.o \ - fileobject.o floatobject.o $(FMOD_OBJ) frameobject.o \ - funcobject.o $(GETCWD_OBJ) \ - graminit.o grammar1.o import.o \ - intobject.o intrcheck.o listnode.o listobject.o \ - mathmodule.o methodobject.o modsupport.o \ - moduleobject.o node.o object.o parser.o \ - parsetok.o posixmodule.o regexp.o regexpmodule.o \ - strdup.o $(STRERROR_OBJ) \ - stringobject.o $(STRTOL_OBJ) structmember.o \ - sysmodule.o timemodule.o tokenizer.o traceback.o \ - tupleobject.o typeobject.o - -STANDARD_SRC= acceler.c bltinmodule.c ceval.c classobject.c \ - compile.c dictobject.c errors.c fgetsintr.c \ - fileobject.c floatobject.c $(FMOD_SRC) frameobject.c \ - funcobject.c $(GETCWD_SRC) \ - graminit.c grammar1.c import.c \ - intobject.c intrcheck.c listnode.c listobject.c \ - mathmodule.c methodobject.c modsupport.c \ - moduleobject.c node.c object.c parser.c \ - parsetok.c posixmodule.c regexp.c regexpmodule.c \ - strdup.c $(STRERROR_SRC) \ - stringobject.c $(STRTOL_SRC) structmember.c \ - sysmodule.c timemodule.c tokenizer.c traceback.c \ - tupleobject.c typeobject.c - -CONFIGDEFS= $(STDW_USE) $(AM_USE) $(AUDIO_USE) $(GL_USE) $(PANEL_USE) \ - '-DPYTHONPATH="$(DEFPYTHONPATH)"' - -CONFIGINCLS= $(STDW_INCL) - -LIBDEPS= libpython.a $(STDW_LIBDEPS) $(AM_LIBDEPS) \ - $(GL_LIBDEPS) $(PANEL_LIBSDEP) $(RL_LIBDEPS) +STANDARD_OBJ= acceler.o bltinmodule.o ceval.o classobject.o \ + compile.o dictobject.o errors.o fgetsintr.o \ + fileobject.o floatobject.o $(FMOD_OBJ) frameobject.o \ + funcobject.o $(GETCWD_OBJ) \ + graminit.o grammar1.o import.o \ + intobject.o intrcheck.o listnode.o listobject.o \ + mathmodule.o methodobject.o modsupport.o \ + moduleobject.o node.o object.o parser.o \ + parsetok.o posixmodule.o regexp.o regexpmodule.o \ + strdup.o $(STRERROR_OBJ) \ + stringobject.o $(STRTOL_OBJ) structmember.o \ + sysmodule.o timemodule.o tokenizer.o traceback.o \ + tupleobject.o typeobject.o + +STANDARD_SRC= acceler.c bltinmodule.c ceval.c classobject.c \ + compile.c dictobject.c errors.c fgetsintr.c \ + fileobject.c floatobject.c $(FMOD_SRC) frameobject.c \ + funcobject.c $(GETCWD_SRC) \ + graminit.c grammar1.c import.c \ + intobject.c intrcheck.c listnode.c listobject.c \ + mathmodule.c methodobject.c modsupport.c \ + moduleobject.c node.c object.c parser.c \ + parsetok.c posixmodule.c regexp.c regexpmodule.c \ + strdup.c $(STRERROR_SRC) \ + stringobject.c $(STRTOL_SRC) structmember.c \ + sysmodule.c timemodule.c tokenizer.c traceback.c \ + tupleobject.c typeobject.c + +CONFIGDEFS= $(STDW_USE) $(AM_USE) $(AUDIO_USE) $(GL_USE) $(PANEL_USE) \ + '-DPYTHONPATH="$(PYTHONPATH)"' + +CONFIGINCLS= $(STDW_INCL) + +LIBDEPS= libpython.a $(STDW_LIBDEPS) $(AM_LIBDEPS) \ + $(GL_LIBDEPS) $(PANEL_LIBSDEP) $(RL_LIBDEPS) # NB: the ordering of items in LIBS is significant! -LIBS= libpython.a $(STDW_LIBS) $(AM_LIBS) \ - $(PANEL_LIBS) $(GL_LIBS) $(RL_LIBS) -lm +LIBS= libpython.a $(STDW_LIBS) $(AM_LIBS) \ + $(PANEL_LIBS) $(GL_LIBS) $(RL_LIBS) -lm -LIBOBJECTS= $(STANDARD_OBJ) $(STDW_OBJ) $(AM_OBJ) $(AUDIO_OBJ) \ - $(GL_OBJ) $(PANEL_OBJ) +LIBOBJECTS= $(STANDARD_OBJ) $(STDW_OBJ) $(AM_OBJ) $(AUDIO_OBJ) \ + $(GL_OBJ) $(PANEL_OBJ) -LIBSOURCES= $(STANDARD_SRC) $(STDW_SRC) $(AM_SRC) $(AUDIO_SRC) \ - $(GL_SRC) $(PANEL_SRC) +LIBSOURCES= $(STANDARD_SRC) $(STDW_SRC) $(AM_SRC) $(AUDIO_SRC) \ + $(GL_SRC) $(PANEL_SRC) -OBJECTS= pythonmain.o config.o +OBJECTS= pythonmain.o config.o -SOURCES= $(LIBSOURCES) pythonmain.c config.c +SOURCES= $(LIBSOURCES) pythonmain.c config.c -GENOBJECTS= acceler.o fgetsintr.o grammar1.o \ - intrcheck.o listnode.o node.o parser.o \ - parsetok.o strdup.o tokenizer.o bitset.o \ - firstsets.o grammar.o metagrammar.o pgen.o \ - pgenmain.o printgrammar.o +GENOBJECTS= acceler.o fgetsintr.o grammar1.o \ + intrcheck.o listnode.o node.o parser.o \ + parsetok.o strdup.o tokenizer.o bitset.o \ + firstsets.o grammar.o metagrammar.o pgen.o \ + pgenmain.o printgrammar.o -GENSOURCES= acceler.c fgetsintr.c grammar1.c \ - intrcheck.c listnode.c node.c parser.c \ - parsetok.c strdup.c tokenizer.c bitset.c \ - firstsets.c grammar.c metagrammar.c pgen.c \ - pgenmain.c printgrammar.c +GENSOURCES= acceler.c fgetsintr.c grammar1.c \ + intrcheck.c listnode.c node.c parser.c \ + parsetok.c strdup.c tokenizer.c bitset.c \ + firstsets.c grammar.c metagrammar.c pgen.c \ + pgenmain.c printgrammar.c # Main Targets # ============ -python: libpython.a $(OBJECTS) $(LIBDEPS) Makefile - $(CC) $(CFLAGS) $(OBJECTS) $(LIBS) -o python +python: libpython.a $(OBJECTS) $(LIBDEPS) Makefile + $(CC) $(CFLAGS) $(OBJECTS) $(LIBS) -o @python + mv @python python -libpython.a: $(LIBOBJECTS) - -rm -f @lib - ar cr @lib $(LIBOBJECTS) - $(RANLIB) @lib - mv @lib libpython.a +libpython.a: $(LIBOBJECTS) + -rm -f @lib + ar cr @lib $(LIBOBJECTS) + $(RANLIB) @lib + mv @lib libpython.a -python_gen: $(GENOBJECTS) $(RL_LIBDEPS) - $(CC) $(CFLAGS) $(GENOBJECTS) $(RL_LIBS) -o python_gen +python_gen: $(GENOBJECTS) $(RL_LIBDEPS) + $(CC) $(CFLAGS) $(GENOBJECTS) $(RL_LIBS) -o python_gen # Utility Targets # =============== -# Don't take the output from lint too seriously. I have not attempted -# to make Python lint-free. But I use function prototypes. +# Don't take the output from lint too seriously. I have not attempted +# to make Python lint-free. But I use function prototypes. -LINTFLAGS= -h +LINTFLAGS= -h -LINTCPPFLAGS= $(CONFIGDEFS) $(CONFIGINCLS) $(SYSVDEF) \ - $(AM_INCL) $(PANEL_INCL) +LINTCPPFLAGS= $(CONFIGDEFS) $(CONFIGINCLS) $(SYSVDEF) \ + $(AM_INCL) $(PANEL_INCL) -LINT= lint +LINT= lint -lint:: $(SOURCES) - $(LINT) $(LINTFLAGS) $(LINTCPPFLAGS) $(SOURCES) +lint:: $(SOURCES) + $(LINT) $(LINTFLAGS) $(LINTCPPFLAGS) $(SOURCES) -lint:: $(GENSOURCES) - $(LINT) $(LINTFLAGS) $(GENSOURCES) +lint:: $(GENSOURCES) + $(LINT) $(LINTFLAGS) $(GENSOURCES) # Generating dependencies is only necessary if you intend to hack Python. # You may change $(MKDEP) to your favorite dependency generator (it should # edit the Makefile in place). -MKDEP= mkdep +MKDEP= mkdep depend:: - $(MKDEP) $(LINTCPPFLAGS) $(SOURCES) $(GENSOURCES) + $(MKDEP) $(LINTCPPFLAGS) $(SOURCES) $(GENSOURCES) # You may change $(CTAGS) to suit your taste... -CTAGS= ctags -t -w +CTAGS= ctags -t -w -HEADERS= *.h +HEADERS= *.h -tags: $(SOURCES) $(GENSOURCES) $(HEADERS) - $(CTAGS) $(SOURCES) $(GENSOURCES) $(HEADERS) +tags: $(SOURCES) $(GENSOURCES) $(HEADERS) + $(CTAGS) $(SOURCES) $(GENSOURCES) $(HEADERS) clean:: - -rm -f *.o core [,#@]* + -rm -f *.o core [,#@]* -clobber:: clean - -rm -f python python_gen libpython.a tags +clobber:: clean + -rm -f python python_gen libpython.a tags # Build Special Objects @@ -444,42 +435,42 @@ clobber:: clean # You may change $(COMPILE) to reflect the default .c.o rule... -COMPILE= $(CC) -c $(CFLAGS) +COMPILE= $(CC) -c $(CFLAGS) -amoebamodule.o: amoebamodule.c - $(COMPILE) $(AM_INCL) $*.c +amoebamodule.o: amoebamodule.c + $(COMPILE) $(AM_INCL) $*.c -config.o: config.c Makefile - $(COMPILE) $(CONFIGDEFS) $(CONFIGINCLS) $*.c +config.o: config.c Makefile + $(COMPILE) $(CONFIGDEFS) $(CONFIGINCLS) $*.c -fgetsintr.o: fgetsintr.c - $(COMPILE) $(SIGTYPEDEF) $*.c +fgetsintr.o: fgetsintr.c + $(COMPILE) $(SIGTYPEDEF) $*.c -intrcheck.o: intrcheck.c - $(COMPILE) $(SIGTYPEDEF) $*.c +intrcheck.o: intrcheck.c + $(COMPILE) $(SIGTYPEDEF) $*.c -panelmodule.o: panelmodule.c - $(COMPILE) $(PANEL_INCL) $*.c +panelmodule.o: panelmodule.c + $(COMPILE) $(PANEL_INCL) $*.c -posixmodule.o: posixmodule.c - $(COMPILE) $(SYSVDEF) $(NOSYMLINKDEF) $*.c +posixmodule.o: posixmodule.c + $(COMPILE) $(SYSVDEF) $(NOSYMLINKDEF) $*.c -sc_interpr.o: sc_interpr.c - $(COMPILE) $(AM_INCL) $*.c +sc_interpr.o: sc_interpr.c + $(COMPILE) $(AM_INCL) $*.c -sc_error.o: sc_error.c - $(COMPILE) $(AM_INCL) $*.c +sc_error.o: sc_error.c + $(COMPILE) $(AM_INCL) $*.c -stdwinmodule.o: stdwinmodule.c - $(COMPILE) $(STDW_INCL) $*.c +stdwinmodule.o: stdwinmodule.c + $(COMPILE) $(STDW_INCL) $*.c -timemodule.o: timemodule.c - $(COMPILE) $(SIGTYPEDEF) $(BSDTIMEDEF) $*.c +timemodule.o: timemodule.c + $(COMPILE) $(SIGTYPEDEF) $(BSDTIMEDEF) $*.c -tokenizer.o: tokenizer.c - $(COMPILE) $(RL_USE) $*.c +tokenizer.o: tokenizer.c + $(COMPILE) $(RL_USE) $*.c -.PRECIOUS: python libpython.a glmodule.c graminit.c graminit.h +.PRECIOUS: python libpython.a glmodule.c graminit.c graminit.h # Generated Sources @@ -489,19 +480,17 @@ tokenizer.o: tokenizer.c # The rules for doing so are given here. # Build "glmodule.c", the GL interface. -# See important note at "GL Option" above. -# You may have to set and export PYTHONPATH for this to work. -# Ignore the messages emitted by the cgen script as long as its exit -# status is zero. +# Ignore the messages emitted by the cgen script. # Also ignore the warnings emitted while compiling glmodule.c; it works. -glmodule.c: cstubs cgen - python cgen <cstubs >@glmodule.c - mv @glmodule.c glmodule.c +glmodule.c: cstubs cgen + python cgen <cstubs >@glmodule.c + mv @glmodule.c glmodule.c # The dependencies for graminit.[ch] are not turned on in the # distributed Makefile because the files themselves are distributed. # Turn them on if you want to hack the grammar. -#graminit.c graminit.h: Grammar python_gen -# python_gen Grammar +#graminit.c graminit.h: Grammar python_gen +# python_gen Grammar + diff --git a/src/PROTO.h b/src/PROTO.h index 88b94fe..7f0e6b9 100644 --- a/src/PROTO.h +++ b/src/PROTO.h @@ -2,12 +2,12 @@ Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. - All Rights Reserved + All Rights Reserved -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in +both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. @@ -25,13 +25,13 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* The macro PROTO(x) is used to put function prototypes in the source. This is defined differently for compilers that support prototypes than -for compilers that don't. It should be used as follows: - int some_function PROTO((int arg1, char *arg2)); +for compilers that don't. It should be used as follows: + int some_function PROTO((int arg1, char *arg2)); A variant FPROTO(x) is used for cases where Standard C allows prototypes but Think C doesn't (mostly function pointers). This file also defines the macro HAVE_PROTOTYPES if and only if -the PROTO() macro expands the prototype. It is also allowed to predefine +the PROTO() macro expands the prototype. It is also allowed to predefine HAVE_PROTOTYPES to force prototypes on. */ @@ -4,7 +4,7 @@ To build the interpreter, edit the Makefile, follow the instructions there, and type "make python". To use the interpreter, you must set the environment variable PYTHONPATH -to point to the directory containing the standard modules. These are +to point to the directory containing the standard modules. These are distributed as a sister directory called 'lib' of this source directory. Try importing the module 'testall' to see if everything works. @@ -5,7 +5,7 @@ - why do reads from stdin fail when I suspend the process? - introduce macros to set/inspect errno for syscalls, to support things - like getoserr() + like getoserr() - fix interrupt handling (interruptable system calls should call - intrcheck() to clear the interrupt status) + intrcheck() to clear the interrupt status) diff --git a/src/acceler.c b/src/acceler.c index 5ed37d3..33f91b6 100644 --- a/src/acceler.c +++ b/src/acceler.c @@ -2,12 +2,12 @@ Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. - All Rights Reserved + All Rights Reserved -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in +both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. @@ -25,13 +25,13 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* Parser accelerator module */ /* The parser as originally conceived had disappointing performance. - This module does some precomputation that speeds up the selection - of a DFA based upon a token, turning a search through an array - into a simple indexing operation. The parser now cannot work - without the accelerators installed. Note that the accelerators - are installed dynamically when the parser is initialized, they - are not part of the static data structure written on graminit.[ch] - by the parser generator. */ + This module does some precomputation that speeds up the selection + of a DFA based upon a token, turning a search through an array + into a simple indexing operation. The parser now cannot work + without the accelerators installed. Note that the accelerators + are installed dynamically when the parser is initialized, they + are not part of the static data structure written on graminit.[ch] + by the parser generator. */ #include "pgenheaders.h" #include "grammar.h" @@ -44,93 +44,93 @@ static void fixstate PROTO((grammar *, dfa *, state *)); void addaccelerators(g) - grammar *g; + grammar *g; { - dfa *d; - int i; + dfa *d; + int i; #ifdef DEBUG - printf("Adding parser accellerators ...\n"); + printf("Adding parser accellerators ...\n"); #endif - d = g->g_dfa; - for (i = g->g_ndfas; --i >= 0; d++) - fixdfa(g, d); - g->g_accel = 1; + d = g->g_dfa; + for (i = g->g_ndfas; --i >= 0; d++) + fixdfa(g, d); + g->g_accel = 1; #ifdef DEBUG - printf("Done.\n"); + printf("Done.\n"); #endif } static void fixdfa(g, d) - grammar *g; - dfa *d; + grammar *g; + dfa *d; { - state *s; - int j; - s = d->d_state; - for (j = 0; j < d->d_nstates; j++, s++) - fixstate(g, d, s); + state *s; + int j; + s = d->d_state; + for (j = 0; j < d->d_nstates; j++, s++) + fixstate(g, d, s); } static void fixstate(g, d, s) - grammar *g; - dfa *d; - state *s; + grammar *g; + dfa *d; + state *s; { - arc *a; - int k; - int *accel; - int nl = g->g_ll.ll_nlabels; - s->s_accept = 0; - accel = NEW(int, nl); - for (k = 0; k < nl; k++) - accel[k] = -1; - a = s->s_arc; - for (k = s->s_narcs; --k >= 0; a++) { - int lbl = a->a_lbl; - label *l = &g->g_ll.ll_label[lbl]; - int type = l->lb_type; - if (a->a_arrow >= (1 << 7)) { - printf("XXX too many states!\n"); - continue; - } - if (ISNONTERMINAL(type)) { - dfa *d1 = finddfa(g, type); - int ibit; - if (type - NT_OFFSET >= (1 << 7)) { - printf("XXX too high nonterminal number!\n"); - continue; - } - for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) { - if (testbit(d1->d_first, ibit)) { - if (accel[ibit] != -1) - printf("XXX ambiguity!\n"); - accel[ibit] = a->a_arrow | (1 << 7) | - ((type - NT_OFFSET) << 8); - } - } - } - else if (lbl == EMPTY) - s->s_accept = 1; - else if (lbl >= 0 && lbl < nl) - accel[lbl] = a->a_arrow; - } - while (nl > 0 && accel[nl-1] == -1) - nl--; - for (k = 0; k < nl && accel[k] == -1;) - k++; - if (k < nl) { - int i; - s->s_accel = NEW(int, nl-k); - if (s->s_accel == NULL) { - fprintf(stderr, "no mem to add parser accelerators\n"); - exit(1); - } - s->s_lower = k; - s->s_upper = nl; - for (i = 0; k < nl; i++, k++) - s->s_accel[i] = accel[k]; - } - DEL(accel); + arc *a; + int k; + int *accel; + int nl = g->g_ll.ll_nlabels; + s->s_accept = 0; + accel = NEW(int, nl); + for (k = 0; k < nl; k++) + accel[k] = -1; + a = s->s_arc; + for (k = s->s_narcs; --k >= 0; a++) { + int lbl = a->a_lbl; + label *l = &g->g_ll.ll_label[lbl]; + int type = l->lb_type; + if (a->a_arrow >= (1 << 7)) { + printf("XXX too many states!\n"); + continue; + } + if (ISNONTERMINAL(type)) { + dfa *d1 = finddfa(g, type); + int ibit; + if (type - NT_OFFSET >= (1 << 7)) { + printf("XXX too high nonterminal number!\n"); + continue; + } + for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) { + if (testbit(d1->d_first, ibit)) { + if (accel[ibit] != -1) + printf("XXX ambiguity!\n"); + accel[ibit] = a->a_arrow | (1 << 7) | + ((type - NT_OFFSET) << 8); + } + } + } + else if (lbl == EMPTY) + s->s_accept = 1; + else if (lbl >= 0 && lbl < nl) + accel[lbl] = a->a_arrow; + } + while (nl > 0 && accel[nl-1] == -1) + nl--; + for (k = 0; k < nl && accel[k] == -1;) + k++; + if (k < nl) { + int i; + s->s_accel = NEW(int, nl-k); + if (s->s_accel == NULL) { + fprintf(stderr, "no mem to add parser accelerators\n"); + exit(1); + } + s->s_lower = k; + s->s_upper = nl; + for (i = 0; k < nl; i++, k++) + s->s_accel[i] = accel[k]; + } + DEL(accel); } diff --git a/src/allobjects.h b/src/allobjects.h index fe46f54..1540adc 100644 --- a/src/allobjects.h +++ b/src/allobjects.h @@ -2,12 +2,12 @@ Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. - All Rights Reserved + All Rights Reserved -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in +both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. @@ -26,7 +26,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include <stdio.h> #include <string.h> -#include <stdlib.h> #include "PROTO.h" diff --git a/src/amoebamodule.c b/src/amoebamodule.c index c9131b7..282677c 100644 --- a/src/amoebamodule.c +++ b/src/amoebamodule.c @@ -2,12 +2,12 @@ Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. - All Rights Reserved + All Rights Reserved -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in +both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. @@ -56,7 +56,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern char *err_why(); extern char *ar_cap(); -#define STUBCODE "+stubcode" +#define STUBCODE "+stubcode" static object *AmoebaError; object *StubcodeError; @@ -70,36 +70,36 @@ extern object *convertcapv(); /* Forward */ static void ins(d, name, v) - object *d; - char *name; - object *v; + object *d; + char *name; + object *v; { - if (v == NULL || dictinsert(d, name, v) != 0) - fatal("can't initialize amoeba module"); + if (v == NULL || dictinsert(d, name, v) != 0) + fatal("can't initialize amoeba module"); } void initamoeba() { - object *m, *d, *v; - - m = initmodule("amoeba", amoeba_methods); - d = getmoduledict(m); - - /* Define capv */ - v = convertcapv(); - ins(d, "capv", v); - DECREF(v); - - /* Set timeout */ - timeout((interval)2000); - - /* Initialize amoeba.error exception */ - AmoebaError = newstringobject("amoeba.error"); - ins(d, "error", AmoebaError); - StubcodeError = newstringobject("amoeba.stubcode_error"); - ins(d, "stubcode_error", StubcodeError); - sc_dict = newdictobject(); + object *m, *d, *v; + + m = initmodule("amoeba", amoeba_methods); + d = getmoduledict(m); + + /* Define capv */ + v = convertcapv(); + ins(d, "capv", v); + DECREF(v); + + /* Set timeout */ + timeout((interval)2000); + + /* Initialize amoeba.error exception */ + AmoebaError = newstringobject("amoeba.error"); + ins(d, "error", AmoebaError); + StubcodeError = newstringobject("amoeba.stubcode_error"); + ins(d, "stubcode_error", StubcodeError); + sc_dict = newdictobject(); } @@ -107,17 +107,17 @@ initamoeba() object * amoeba_error(err) - errstat err; + errstat err; { - object *v = newtupleobject(2); - if (v != NULL) { - settupleitem(v, 0, newintobject((long)err)); - settupleitem(v, 1, newstringobject(err_why(err))); - } - err_setval(AmoebaError, v); - if (v != NULL) - DECREF(v); - return NULL; + object *v = newtupleobject(2); + if (v != NULL) { + settupleitem(v, 0, newintobject((long)err)); + settupleitem(v, 1, newstringobject(err_why(err))); + } + err_setval(AmoebaError, v); + if (v != NULL) + DECREF(v); + return NULL; } @@ -128,62 +128,62 @@ extern typeobject Captype; /* Forward */ #define is_capobject(v) ((v)->ob_type == &Captype) typedef struct { - OB_HEAD - capability ob_cap; + OB_HEAD + capability ob_cap; } capobject; object * newcapobject(cap) - capability *cap; + capability *cap; { - capobject *v = NEWOBJ(capobject, &Captype); - if (v == NULL) - return NULL; - v->ob_cap = *cap; - return (object *)v; + capobject *v = NEWOBJ(capobject, &Captype); + if (v == NULL) + return NULL; + v->ob_cap = *cap; + return (object *)v; } getcapability(v, cap) - object *v; - capability *cap; + object *v; + capability *cap; { - if (!is_capobject(v)) - return err_badarg(); - *cap = ((capobject *)v)->ob_cap; - return 0; + if (!is_capobject(v)) + return err_badarg(); + *cap = ((capobject *)v)->ob_cap; + return 0; } /* - * is_capobj exports the is_capobject macro to the stubcode modules + * is_capobj exports the is_capobject macro to the stubcode modules */ int is_capobj(v) - object *v; + object *v; { - return is_capobject(v); + return is_capobject(v); } /* Methods */ static void capprint(v, fp, flags) - capobject *v; - FILE *fp; - int flags; + capobject *v; + FILE *fp; + int flags; { - /* XXX needs lock when multi-threading */ - fputs(ar_cap(&v->ob_cap), fp); + /* XXX needs lock when multi-threading */ + fputs(ar_cap(&v->ob_cap), fp); } static object * caprepr(v) - capobject *v; + capobject *v; { - /* XXX needs lock when multi-threading */ - return newstringobject(ar_cap(&v->ob_cap)); + /* XXX needs lock when multi-threading */ + return newstringobject(ar_cap(&v->ob_cap)); } extern object *sc_interpreter(); @@ -192,216 +192,216 @@ extern struct methodlist cap_methods[]; /* Forward */ object * sc_makeself(cap, stubcode, name) - object *cap, *stubcode; - char *name; + object *cap, *stubcode; + char *name; { - object *sc_name, *sc_self; - - sc_name = newstringobject(name); - if (sc_name == NULL) - return NULL; - sc_self = newtupleobject(3); - if (sc_self == NULL) { - DECREF(sc_name); - return NULL; - } - if (settupleitem(sc_self, NAME, sc_name) != 0) { - DECREF(sc_self); - return NULL; - } - INCREF(cap); - if (settupleitem(sc_self, CAP, cap) != 0) { - DECREF(sc_self); - return NULL; - } - INCREF(stubcode); - if (settupleitem(sc_self, STUBC, stubcode) != 0) { - DECREF(sc_self); - return NULL; - } - return sc_self; + object *sc_name, *sc_self; + + sc_name = newstringobject(name); + if (sc_name == NULL) + return NULL; + sc_self = newtupleobject(3); + if (sc_self == NULL) { + DECREF(sc_name); + return NULL; + } + if (settupleitem(sc_self, NAME, sc_name) != 0) { + DECREF(sc_self); + return NULL; + } + INCREF(cap); + if (settupleitem(sc_self, CAP, cap) != 0) { + DECREF(sc_self); + return NULL; + } + INCREF(stubcode); + if (settupleitem(sc_self, STUBC, stubcode) != 0) { + DECREF(sc_self); + return NULL; + } + return sc_self; } static void swapcode(code, len) - char *code; - int len; + char *code; + int len; { - int i = sizeof(TscOperand); - TscOpcode opcode; - TscOperand operand; - - while (i < len) { - memcpy(&opcode, &code[i], sizeof(TscOpcode)); - SwapOpcode(opcode); - memcpy(&code[i], &opcode, sizeof(TscOpcode)); - i += sizeof(TscOpcode); - if (opcode & OPERAND) { - memcpy(&operand, &code[i], sizeof(TscOperand)); - SwapOperand(operand); - memcpy(&code[i], &operand, sizeof(TscOperand)); - i += sizeof(TscOperand); - } - } + int i = sizeof(TscOperand); + TscOpcode opcode; + TscOperand operand; + + while (i < len) { + memcpy(&opcode, &code[i], sizeof(TscOpcode)); + SwapOpcode(opcode); + memcpy(&code[i], &opcode, sizeof(TscOpcode)); + i += sizeof(TscOpcode); + if (opcode & OPERAND) { + memcpy(&operand, &code[i], sizeof(TscOperand)); + SwapOperand(operand); + memcpy(&code[i], &operand, sizeof(TscOperand)); + i += sizeof(TscOperand); + } + } } object * sc_findstubcode(v, name) - object *v; - char *name; + object *v; + char *name; { - int fd, fsize; - char *fname, *buffer; - struct stat statbuf; - object *sc_stubcode, *ret; - TscOperand sc_magic; - - /* - * Only look in the current directory for now. - */ - fname = malloc(strlen(name) + 4); - if (fname == NULL) { - return err_nomem(); - } - sprintf(fname, "%s.sc", name); - if ((fd = open(fname, O_RDONLY)) == -1) { - extern int errno; - - if (errno == 2) { - /* - ** errno == 2 is file not found. - */ - err_setstr(NameError, fname); - return NULL; - } - free(fname); - return err_errno(newstringobject(name)); - } - fstat(fd, &statbuf); - fsize = (int)statbuf.st_size; - buffer = malloc(fsize); - if (buffer == NULL) { - free(fname); - close(fd); - return err_nomem(); - } - if (read(fd, buffer, fsize) != fsize) { - close(fd); - free(fname); - return err_errno(newstringobject(name)); - } - close(fd); - free(fname); - memcpy(&sc_magic, buffer, sizeof(TscOperand)); - if (sc_magic != SC_MAGIC) { - SwapOperand(sc_magic); - if (sc_magic != SC_MAGIC) { - free(buffer); - return NULL; - } else { - swapcode(buffer, fsize); - } - } - sc_stubcode = newsizedstringobject( &buffer[sizeof(TscOperand)], - fsize - sizeof(TscOperand)); - free(buffer); - if (sc_stubcode == NULL) { - return NULL; - } - if (dictinsert(sc_dict, name, sc_stubcode) != 0) { - DECREF(sc_stubcode); - return NULL; - } - DECREF(sc_stubcode); /* XXXX */ - sc_stubcode = sc_makeself(v, sc_stubcode, name); - if (sc_stubcode == NULL) { - return NULL; - } - return sc_stubcode; + int fd, fsize; + char *fname, *buffer; + struct stat statbuf; + object *sc_stubcode, *ret; + TscOperand sc_magic; + + /* + * Only look in the current directory for now. + */ + fname = malloc(strlen(name) + 4); + if (fname == NULL) { + return err_nomem(); + } + sprintf(fname, "%s.sc", name); + if ((fd = open(fname, O_RDONLY)) == -1) { + extern int errno; + + if (errno == 2) { + /* + ** errno == 2 is file not found. + */ + err_setstr(NameError, fname); + return NULL; + } + free(fname); + return err_errno(newstringobject(name)); + } + fstat(fd, &statbuf); + fsize = (int)statbuf.st_size; + buffer = malloc(fsize); + if (buffer == NULL) { + free(fname); + close(fd); + return err_nomem(); + } + if (read(fd, buffer, fsize) != fsize) { + close(fd); + free(fname); + return err_errno(newstringobject(name)); + } + close(fd); + free(fname); + memcpy(&sc_magic, buffer, sizeof(TscOperand)); + if (sc_magic != SC_MAGIC) { + SwapOperand(sc_magic); + if (sc_magic != SC_MAGIC) { + free(buffer); + return NULL; + } else { + swapcode(buffer, fsize); + } + } + sc_stubcode = newsizedstringobject( &buffer[sizeof(TscOperand)], + fsize - sizeof(TscOperand)); + free(buffer); + if (sc_stubcode == NULL) { + return NULL; + } + if (dictinsert(sc_dict, name, sc_stubcode) != 0) { + DECREF(sc_stubcode); + return NULL; + } + DECREF(sc_stubcode); /* XXXX */ + sc_stubcode = sc_makeself(v, sc_stubcode, name); + if (sc_stubcode == NULL) { + return NULL; + } + return sc_stubcode; } object * capgetattr(v, name) - capobject *v; - char *name; + capobject *v; + char *name; { - object *sc_method, *sc_stubcodemethod; - - if (sc_dict == NULL) { - /* - ** For some reason the dictionary has not been - ** initialized. Try to find one of the built in - ** methods. - */ - return findmethod(cap_methods, (object *)v, name); - } - sc_stubcodemethod = dictlookup(sc_dict, name); - if (sc_stubcodemethod != NULL) { - /* - ** There is a stubcode method in the dictionary. - ** Execute the stubcode interpreter with the right - ** arguments. - */ - object *self, *ret; - - self = sc_makeself((object *)v, sc_stubcodemethod, name); - if (self == NULL) { - return NULL; - } - ret = findmethod(cap_methods, self, STUBCODE); - DECREF(self); - return ret; - } - err_clear(); - sc_method = findmethod(cap_methods, (object *)v, name); - if (sc_method == NULL) { - /* - ** The method is not built in and not in the - ** dictionary. Try to find it as a stubcode file. - */ - object *self, *ret; - - err_clear(); - self = sc_findstubcode((object *)v, name); - if (self == NULL) { - return NULL; - } - ret = findmethod(cap_methods, self, STUBCODE); - DECREF(self); - return ret; - } - return sc_method; + object *sc_method, *sc_stubcodemethod; + + if (sc_dict == NULL) { + /* + ** For some reason the dictionary has not been + ** initialized. Try to find one of the built in + ** methods. + */ + return findmethod(cap_methods, (object *)v, name); + } + sc_stubcodemethod = dictlookup(sc_dict, name); + if (sc_stubcodemethod != NULL) { + /* + ** There is a stubcode method in the dictionary. + ** Execute the stubcode interpreter with the right + ** arguments. + */ + object *self, *ret; + + self = sc_makeself((object *)v, sc_stubcodemethod, name); + if (self == NULL) { + return NULL; + } + ret = findmethod(cap_methods, self, STUBCODE); + DECREF(self); + return ret; + } + err_clear(); + sc_method = findmethod(cap_methods, (object *)v, name); + if (sc_method == NULL) { + /* + ** The method is not built in and not in the + ** dictionary. Try to find it as a stubcode file. + */ + object *self, *ret; + + err_clear(); + self = sc_findstubcode((object *)v, name); + if (self == NULL) { + return NULL; + } + ret = findmethod(cap_methods, self, STUBCODE); + DECREF(self); + return ret; + } + return sc_method; } int capcompare(v, w) - capobject *v, *w; + capobject *v, *w; { - int cmp = bcmp((char *)&v->ob_cap.cap_port, - (char *)&w->ob_cap, PORTSIZE); - if (cmp != 0) - return cmp; - return prv_number(&v->ob_cap.cap_priv) - - prv_number(&w->ob_cap.cap_priv); + int cmp = bcmp((char *)&v->ob_cap.cap_port, + (char *)&w->ob_cap, PORTSIZE); + if (cmp != 0) + return cmp; + return prv_number(&v->ob_cap.cap_priv) - + prv_number(&w->ob_cap.cap_priv); } static typeobject Captype = { - OB_HEAD_INIT(&Typetype) - 0, - "capability", - sizeof(capobject), - 0, - free, /*tp_dealloc*/ - capprint, /*tp_print*/ - capgetattr, /*tp_getattr*/ - 0, /*tp_setattr*/ - capcompare, /*tp_comp + OB_HEAD_INIT(&Typetype) + 0, + "capability", + sizeof(capobject), + 0, + free, /*tp_dealloc*/ + capprint, /*tp_print*/ + capgetattr, /*tp_getattr*/ + 0, /*tp_setattr*/ + capcompare, /*tp_comp are*/ - caprepr, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ + caprepr, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ }; @@ -412,22 +412,22 @@ extern struct caplist *capv; static object * convertcapv() { - object *d; - struct caplist *c; - d = newdictobject(); - if (d == NULL) - return NULL; - if (capv == NULL) - return d; - for (c = capv; c->cl_name != NULL; c++) { - object *v = newcapobject(c->cl_cap); - if (v == NULL || dictinsert(d, c->cl_name, v) != 0) { - DECREF(d); - return NULL; - } - DECREF(v); - } - return d; + object *d; + struct caplist *c; + d = newdictobject(); + if (d == NULL) + return NULL; + if (capv == NULL) + return d; + for (c = capv; c->cl_name != NULL; c++) { + object *v = newcapobject(c->cl_cap); + if (v == NULL || dictinsert(d, c->cl_name, v) != 0) { + DECREF(d); + return NULL; + } + DECREF(v); + } + return d; } @@ -435,25 +435,25 @@ convertcapv() static int getcaparg(v, a) - object *v; - capability *a; + object *v; + capability *a; { - if (v == NULL || !is_capobject(v)) - return err_badarg(); - *a = ((capobject *)v) -> ob_cap; - return 1; + if (v == NULL || !is_capobject(v)) + return err_badarg(); + *a = ((capobject *)v) -> ob_cap; + return 1; } static int getstrcapargs(v, a, b) - object *v; - object **a; - capability *b; + object *v; + object **a; + capability *b; { - if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) - return err_badarg(); - return getstrarg(gettupleitem(v, 0), a) && - getcaparg(gettupleitem(v, 1), b); + if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) + return err_badarg(); + return getstrarg(gettupleitem(v, 0), a) && + getcaparg(gettupleitem(v, 1), b); } @@ -461,314 +461,314 @@ getstrcapargs(v, a, b) static object * amoeba_name_lookup(self, args) - object *self; - object *args; + object *self; + object *args; { - object *name; - capability cap; - errstat err; - if (!getstrarg(args, &name)) - return NULL; - err = name_lookup(getstringvalue(name), &cap); - if (err != STD_OK) - return amoeba_error(err); - return newcapobject(&cap); + object *name; + capability cap; + errstat err; + if (!getstrarg(args, &name)) + return NULL; + err = name_lookup(getstringvalue(name), &cap); + if (err != STD_OK) + return amoeba_error(err); + return newcapobject(&cap); } static object * amoeba_name_append(self, args) - object *self; - object *args; + object *self; + object *args; { - object *name; - capability cap; - errstat err; - if (!getstrcapargs(args, &name, &cap)) - return NULL; - err = name_append(getstringvalue(name), &cap); - if (err != STD_OK) - return amoeba_error(err); - INCREF(None); - return None; + object *name; + capability cap; + errstat err; + if (!getstrcapargs(args, &name, &cap)) + return NULL; + err = name_append(getstringvalue(name), &cap); + if (err != STD_OK) + return amoeba_error(err); + INCREF(None); + return None; } static object * amoeba_name_replace(self, args) - object *self; - object *args; + object *self; + object *args; { - object *name; - capability cap; - errstat err; - if (!getstrcapargs(args, &name, &cap)) - return NULL; - err = name_replace(getstringvalue(name), &cap); - if (err != STD_OK) - return amoeba_error(err); - INCREF(None); - return None; + object *name; + capability cap; + errstat err; + if (!getstrcapargs(args, &name, &cap)) + return NULL; + err = name_replace(getstringvalue(name), &cap); + if (err != STD_OK) + return amoeba_error(err); + INCREF(None); + return None; } static object * amoeba_name_delete(self, args) - object *self; - object *args; + object *self; + object *args; { - object *name; - errstat err; - if (!getstrarg(args, &name)) - return NULL; - err = name_delete(getstringvalue(name)); - if (err != STD_OK) - return amoeba_error(err); - INCREF(None); - return None; + object *name; + errstat err; + if (!getstrarg(args, &name)) + return NULL; + err = name_delete(getstringvalue(name)); + if (err != STD_OK) + return amoeba_error(err); + INCREF(None); + return None; } static object * amoeba_timeout(self, args) - object *self; - object *args; + object *self; + object *args; { - int i; - object *v; - interval tout; - if (!getintarg(args, &i)) - return NULL; - tout = timeout((interval)i); - v = newintobject((long)tout); - if (v == NULL) - timeout(tout); - return v; + int i; + object *v; + interval tout; + if (!getintarg(args, &i)) + return NULL; + tout = timeout((interval)i); + v = newintobject((long)tout); + if (v == NULL) + timeout(tout); + return v; } static struct methodlist amoeba_methods[] = { - {"name_append", amoeba_name_append}, - {"name_delete", amoeba_name_delete}, - {"name_lookup", amoeba_name_lookup}, - {"name_replace", amoeba_name_replace}, - {"timeout", amoeba_timeout}, - {NULL, NULL} /* Sentinel */ + {"name_append", amoeba_name_append}, + {"name_delete", amoeba_name_delete}, + {"name_lookup", amoeba_name_lookup}, + {"name_replace", amoeba_name_replace}, + {"timeout", amoeba_timeout}, + {NULL, NULL} /* Sentinel */ }; /* Capability methods */ static object * cap_b_size(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - errstat err; - b_fsize size; - if (!getnoarg(args)) - return NULL; - err = b_size(&self->ob_cap, &size); - if (err != STD_OK) - return amoeba_error(err); - return newintobject((long)size); + errstat err; + b_fsize size; + if (!getnoarg(args)) + return NULL; + err = b_size(&self->ob_cap, &size); + if (err != STD_OK) + return amoeba_error(err); + return newintobject((long)size); } static object * cap_b_read(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - errstat err; - char *buf; - object *v; - long offset, size; - b_fsize nread; - if (!getlonglongargs(args, &offset, &size)) - return NULL; - buf = malloc((unsigned int)size); - if (buf == NULL) { - return err_nomem(); - } - err = b_read(&self->ob_cap, (b_fsize)offset, buf, (b_fsize)size, - &nread); - if (err != STD_OK) { - free(buf); - return amoeba_error(err); - } - v = newsizedstringobject(buf, (int)nread); - free(buf); - return v; + errstat err; + char *buf; + object *v; + long offset, size; + b_fsize nread; + if (!getlonglongargs(args, &offset, &size)) + return NULL; + buf = malloc((unsigned int)size); + if (buf == NULL) { + return err_nomem(); + } + err = b_read(&self->ob_cap, (b_fsize)offset, buf, (b_fsize)size, + &nread); + if (err != STD_OK) { + free(buf); + return amoeba_error(err); + } + v = newsizedstringobject(buf, (int)nread); + free(buf); + return v; } static object * cap_dir_lookup(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - object *name; - capability cap; - errstat err; - if (!getstrarg(args, &name)) - return NULL; - err = dir_lookup(&self->ob_cap, getstringvalue(name), &cap); - if (err != STD_OK) - return amoeba_error(err); - return newcapobject(&cap); + object *name; + capability cap; + errstat err; + if (!getstrarg(args, &name)) + return NULL; + err = dir_lookup(&self->ob_cap, getstringvalue(name), &cap); + if (err != STD_OK) + return amoeba_error(err); + return newcapobject(&cap); } static object * cap_dir_append(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - object *name; - capability cap; - errstat err; - if (!getstrcapargs(args, &name, &cap)) - return NULL; - err = dir_append(&self->ob_cap, getstringvalue(name), &cap); - if (err != STD_OK) - return amoeba_error(err); - INCREF(None); - return None; + object *name; + capability cap; + errstat err; + if (!getstrcapargs(args, &name, &cap)) + return NULL; + err = dir_append(&self->ob_cap, getstringvalue(name), &cap); + if (err != STD_OK) + return amoeba_error(err); + INCREF(None); + return None; } static object * cap_dir_delete(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - object *name; - errstat err; - if (!getstrarg(args, &name)) - return NULL; - err = dir_delete(&self->ob_cap, getstringvalue(name)); - if (err != STD_OK) - return amoeba_error(err); - INCREF(None); - return None; + object *name; + errstat err; + if (!getstrarg(args, &name)) + return NULL; + err = dir_delete(&self->ob_cap, getstringvalue(name)); + if (err != STD_OK) + return amoeba_error(err); + INCREF(None); + return None; } static object * cap_dir_replace(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - object *name; - capability cap; - errstat err; - if (!getstrcapargs(args, &name, &cap)) - return NULL; - err = dir_replace(&self->ob_cap, getstringvalue(name), &cap); - if (err != STD_OK) - return amoeba_error(err); - INCREF(None); - return None; + object *name; + capability cap; + errstat err; + if (!getstrcapargs(args, &name, &cap)) + return NULL; + err = dir_replace(&self->ob_cap, getstringvalue(name), &cap); + if (err != STD_OK) + return amoeba_error(err); + INCREF(None); + return None; } static object * cap_dir_list(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - errstat err; - struct dir_open *dd; - object *d; - char *name; - if (!getnoarg(args)) - return NULL; - if ((dd = dir_open(&self->ob_cap)) == NULL) - return amoeba_error(STD_COMBAD); - if ((d = newlistobject(0)) == NULL) { - dir_close(dd); - return NULL; - } - while ((name = dir_next(dd)) != NULL) { - object *v; - v = newstringobject(name); - if (v == NULL) { - DECREF(d); - d = NULL; - break; - } - if (addlistitem(d, v) != 0) { - DECREF(v); - DECREF(d); - d = NULL; - break; - } - DECREF(v); - } - dir_close(dd); - return d; + errstat err; + struct dir_open *dd; + object *d; + char *name; + if (!getnoarg(args)) + return NULL; + if ((dd = dir_open(&self->ob_cap)) == NULL) + return amoeba_error(STD_COMBAD); + if ((d = newlistobject(0)) == NULL) { + dir_close(dd); + return NULL; + } + while ((name = dir_next(dd)) != NULL) { + object *v; + v = newstringobject(name); + if (v == NULL) { + DECREF(d); + d = NULL; + break; + } + if (addlistitem(d, v) != 0) { + DECREF(v); + DECREF(d); + d = NULL; + break; + } + DECREF(v); + } + dir_close(dd); + return d; } object * cap_std_info(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - char buf[256]; - errstat err; - int n; - if (!getnoarg(args)) - return NULL; - err = std_info(&self->ob_cap, buf, sizeof buf, &n); - if (err != STD_OK) - return amoeba_error(err); - return newsizedstringobject(buf, n); + char buf[256]; + errstat err; + int n; + if (!getnoarg(args)) + return NULL; + err = std_info(&self->ob_cap, buf, sizeof buf, &n); + if (err != STD_OK) + return amoeba_error(err); + return newsizedstringobject(buf, n); } object * cap_tod_gettime(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - header h; - errstat err; - bufsize n; - long sec; - int msec, tz, dst; - if (!getnoarg(args)) - return NULL; - h.h_port = self->ob_cap.cap_port; - h.h_priv = self->ob_cap.cap_priv; - h.h_command = TOD_GETTIME; - n = trans(&h, NILBUF, 0, &h, NILBUF, 0); - if (ERR_STATUS(n)) - return amoeba_error(ERR_CONVERT(n)); - tod_decode(&h, &sec, &msec, &tz, &dst); - return newintobject(sec); + header h; + errstat err; + bufsize n; + long sec; + int msec, tz, dst; + if (!getnoarg(args)) + return NULL; + h.h_port = self->ob_cap.cap_port; + h.h_priv = self->ob_cap.cap_priv; + h.h_command = TOD_GETTIME; + n = trans(&h, NILBUF, 0, &h, NILBUF, 0); + if (ERR_STATUS(n)) + return amoeba_error(ERR_CONVERT(n)); + tod_decode(&h, &sec, &msec, &tz, &dst); + return newintobject(sec); } object * cap_tod_settime(self, args) - capobject *self; - object *args; + capobject *self; + object *args; { - header h; - errstat err; - bufsize n; - long sec; - if (!getlongarg(args, &sec)) - return NULL; - h.h_port = self->ob_cap.cap_port; - h.h_priv = self->ob_cap.cap_priv; - h.h_command = TOD_SETTIME; - tod_encode(&h, sec, 0, 0, 0); - n = trans(&h, NILBUF, 0, &h, NILBUF, 0); - if (ERR_STATUS(n)) - return amoeba_error(ERR_CONVERT(n)); - INCREF(None); - return None; + header h; + errstat err; + bufsize n; + long sec; + if (!getlongarg(args, &sec)) + return NULL; + h.h_port = self->ob_cap.cap_port; + h.h_priv = self->ob_cap.cap_priv; + h.h_command = TOD_SETTIME; + tod_encode(&h, sec, 0, 0, 0); + n = trans(&h, NILBUF, 0, &h, NILBUF, 0); + if (ERR_STATUS(n)) + return amoeba_error(ERR_CONVERT(n)); + INCREF(None); + return None; } static struct methodlist cap_methods[] = { - { STUBCODE, sc_interpreter}, - {"b_read", cap_b_read}, - {"b_size", cap_b_size}, - {"dir_append", cap_dir_append}, - {"dir_delete", cap_dir_delete}, - {"dir_list", cap_dir_list}, - {"dir_lookup", cap_dir_lookup}, - {"dir_replace", cap_dir_replace}, - {"std_info", cap_std_info}, - {"tod_gettime", cap_tod_gettime}, - {"tod_settime", cap_tod_settime}, - {NULL, NULL} /* Sentinel */ + { STUBCODE, sc_interpreter}, + {"b_read", cap_b_read}, + {"b_size", cap_b_size}, + {"dir_append", cap_dir_append}, + {"dir_delete", cap_dir_delete}, + {"dir_list", cap_dir_list}, + {"dir_lookup", cap_dir_lookup}, + {"dir_replace", cap_dir_replace}, + {"std_info", cap_std_info}, + {"tod_gettime", cap_tod_gettime}, + {"tod_settime", cap_tod_settime}, + {NULL, NULL} /* Sentinel */ }; @@ -2,12 +2,12 @@ Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. - All Rights Reserved + All Rights Reserved -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in +both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. @@ -23,86 +23,86 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ******************************************************************/ /* Asynchronous audio module for Silicon Graphics 4D/20 under IRIX 3.3 - Copyright 1990 Stichting Mathematisch Centrum, Amsterdam - Author: Guido van Rossum, <[email protected]> - Last modified: [email protected], Oct 14, 1990 - - Callers should #include "asa.h". - - This code is strongly IRIX 3.3 dependent. (Or are sproc() and - friends standard SYSV now?) - - Caution: if you put printf's in the slave for debugging, use "-lmpc" - to get the semaphore version of stdio! - - - This file contains two library layers and a test program: - - - The lower layer implements a simple asynchronous execution facility, - built directly on the system calls sproc() and [un]blockproc(). - - A slave thread sits in an infinite loop waiting for work assigned to - it by the master thread. Work is represented by a function pointer - and an argument of type void*. The function returns a void* pointer - which is transferred back to the master when it submits the next bit - of work. Submitting a NULL function pointer can be used by the - master to wait for completion of the previous work. This lower - layer could be generally useful, but is currently implemented by - static functions, for exclusive by the asynchronous audio layer. - - - The higher layer implements an asynchronous interface to the - /dev/audio device on the Silicon Graphics 4D/20. It defines the - following functions: - - int asa_init() - Required initialization function. The other functions will call - abort() when they are called before asa_init(). It creates the - slave process and returns a file descriptor for the audio - device which can be used to set the sampling rate and output - gain, etc. It prints a message to stderr and returns -1 if the - initialization failed. Calling this function more than onece is - harmless. - - void asa_start_read(char *buf, int len) - Starts an asynchronous read call on the audio device. This - waits for completion of the previous request, if any. - - void asa_start_write(char *buf, int len) - Starts an asynchronous write call on the audio device. This - waits for completion of the previous request, if any. - - int asa_poll() - Polls whether the last asynchronous read or write request is - finished. Returns -1 if no request was queued, 0 if the request - is not yet finished, and 1 if it is finished. - - int asa_wait() - Waits for completion if the last asynchronous read or write - request. It returns the result of the read or write request, - and sets the error code to the error code set by the request if - the result is -1. If no request was queued, this also returns - -1 but leaves the error code unchanged. Note: to get the error - code, don't inspect the global variable errno but call the - function oserror(). - - int asa_cancel() - Cancels the last asynchronous read or write request (by sending - the slave thread a signal for which it has a handler) then - returns its result and error code as asa_wait(). - - void asa_done() - Kills the slave process and closes the audio device. After - this, if further use of the module is required, asa_init() - should be called again. Calling this function when asa_init() - has not been called is harmless. - - - Finally, this file contains a simple test program that is compiled if - MAIN is defined (e.g., compile with cc -DMAIN). It makes a recording - and plays it back. The user must indicate begin and end of recording - and play-back by pressing the Return key. + Copyright 1990 Stichting Mathematisch Centrum, Amsterdam + Author: Guido van Rossum, <[email protected]> + Last modified: [email protected], Oct 14, 1990 + + Callers should #include "asa.h". + + This code is strongly IRIX 3.3 dependent. (Or are sproc() and + friends standard SYSV now?) + + Caution: if you put printf's in the slave for debugging, use "-lmpc" + to get the semaphore version of stdio! + + + This file contains two library layers and a test program: + + + The lower layer implements a simple asynchronous execution facility, + built directly on the system calls sproc() and [un]blockproc(). + + A slave thread sits in an infinite loop waiting for work assigned to + it by the master thread. Work is represented by a function pointer + and an argument of type void*. The function returns a void* pointer + which is transferred back to the master when it submits the next bit + of work. Submitting a NULL function pointer can be used by the + master to wait for completion of the previous work. This lower + layer could be generally useful, but is currently implemented by + static functions, for exclusive by the asynchronous audio layer. + + + The higher layer implements an asynchronous interface to the + /dev/audio device on the Silicon Graphics 4D/20. It defines the + following functions: + + int asa_init() + Required initialization function. The other functions will call + abort() when they are called before asa_init(). It creates the + slave process and returns a file descriptor for the audio + device which can be used to set the sampling rate and output + gain, etc. It prints a message to stderr and returns -1 if the + initialization failed. Calling this function more than onece is + harmless. + + void asa_start_read(char *buf, int len) + Starts an asynchronous read call on the audio device. This + waits for completion of the previous request, if any. + + void asa_start_write(char *buf, int len) + Starts an asynchronous write call on the audio device. This + waits for completion of the previous request, if any. + + int asa_poll() + Polls whether the last asynchronous read or write request is + finished. Returns -1 if no request was queued, 0 if the request + is not yet finished, and 1 if it is finished. + + int asa_wait() + Waits for completion if the last asynchronous read or write + request. It returns the result of the read or write request, + and sets the error code to the error code set by the request if + the result is -1. If no request was queued, this also returns + -1 but leaves the error code unchanged. Note: to get the error + code, don't inspect the global variable errno but call the + function oserror(). + + int asa_cancel() + Cancels the last asynchronous read or write request (by sending + the slave thread a signal for which it has a handler) then + returns its result and error code as asa_wait(). + + void asa_done() + Kills the slave process and closes the audio device. After + this, if further use of the module is required, asa_init() + should be called again. Calling this function when asa_init() + has not been called is harmless. + + + Finally, this file contains a simple test program that is compiled if + MAIN is defined (e.g., compile with cc -DMAIN). It makes a recording + and plays it back. The user must indicate begin and end of recording + and play-back by pressing the Return key. */ @@ -135,20 +135,20 @@ static void *work_result; /*ARGSUSED*/ static void handler(sig) - int sig; + int sig; { - /* Reinstate the handler (non-BSD signal semantics) */ - signal(sig, handler); + /* Reinstate the handler (non-BSD signal semantics) */ + signal(sig, handler); } /* Subroutine to fiddle signals */ static void dosig(sig) - int sig; + int sig; { - if (signal(sig, SIG_IGN) != SIG_IGN) - signal(sig, SIG_DFL); + if (signal(sig, SIG_IGN) != SIG_IGN) + signal(sig, SIG_DFL); } /* Slave control flow */ @@ -156,148 +156,148 @@ dosig(sig) /*ARGSUSED*/ static void slave(arg) - void *arg; + void *arg; { - void * (*func)(); - void *arg; - void *result; - - /* Reset signal handlers that interactive programs often catch. - The assumption is that if the master has a handler for these - signals, it will be a cleanup function. The slave must die - from these. */ - dosig(SIGHUP); - dosig(SIGQUIT); - dosig(SIGTERM); - dosig(SIGPIPE); - - /* Ignore SIGINT if caught or ignored */ - if (signal(SIGINT, SIG_IGN) == SIG_DFL) - signal(SIGINT, SIG_DFL); - - /* Let the handler install itself */ - handler(MYSIG); - - /* Set slave_pid. This is also done in the master thread, but - there is a race condition whereby the slave begins execution - before the master has assigned the result of sproc() to - slave_pid. So we set it here as well -- since this sets the - same value it should be OK. */ - slave_pid = getpid(); - - /* Set the dummy result returned by the first rendezvous */ - result = NULL; - - /* Loop forever, waiting for and executing work */ - for (;;) { - /* First rendezvous: store previous result */ - if (blockproc(slave_pid) < 0) - perror("slave: [result] blockproc(slave_pid)"); - work_result = result; - if (unblockproc(master_pid) < 0) - perror("slave: [result] unblockproc(master_pid)"); - - /* Second rendezvous: fetch work */ - if (blockproc(slave_pid) < 0) - perror("slave: [func,arg] blockproc(slave_pid)"); - func = work_func; - arg = work_arg; - if (unblockproc(master_pid) < 0) - perror("slave: [func,arg] unblockproc(master_pid)"); - - /* Execute work, computing new result */ - if (func == NULL) { - result = arg; - } - else { - result = (*func)(arg); - } - } + void * (*func)(); + void *arg; + void *result; + + /* Reset signal handlers that interactive programs often catch. + The assumption is that if the master has a handler for these + signals, it will be a cleanup function. The slave must die + from these. */ + dosig(SIGHUP); + dosig(SIGQUIT); + dosig(SIGTERM); + dosig(SIGPIPE); + + /* Ignore SIGINT if caught or ignored */ + if (signal(SIGINT, SIG_IGN) == SIG_DFL) + signal(SIGINT, SIG_DFL); + + /* Let the handler install itself */ + handler(MYSIG); + + /* Set slave_pid. This is also done in the master thread, but + there is a race condition whereby the slave begins execution + before the master has assigned the result of sproc() to + slave_pid. So we set it here as well -- since this sets the + same value it should be OK. */ + slave_pid = getpid(); + + /* Set the dummy result returned by the first rendezvous */ + result = NULL; + + /* Loop forever, waiting for and executing work */ + for (;;) { + /* First rendezvous: store previous result */ + if (blockproc(slave_pid) < 0) + perror("slave: [result] blockproc(slave_pid)"); + work_result = result; + if (unblockproc(master_pid) < 0) + perror("slave: [result] unblockproc(master_pid)"); + + /* Second rendezvous: fetch work */ + if (blockproc(slave_pid) < 0) + perror("slave: [func,arg] blockproc(slave_pid)"); + func = work_func; + arg = work_arg; + if (unblockproc(master_pid) < 0) + perror("slave: [func,arg] unblockproc(master_pid)"); + + /* Execute work, computing new result */ + if (func == NULL) { + result = arg; + } + else { + result = (*func)(arg); + } + } } static int slave_init() { - if (slave_pid > 0) - return slave_pid; - master_pid = getpid(); - - /* Reset the queue, in case this is a re-init after asa_done() */ - work_result = NULL; - work_func = NULL; - work_arg = NULL; - - /* Create the slave process, sharing all segments and properties */ - slave_pid = sproc(slave, PR_SALL, (char *)NULL); - if (slave_pid < 0) - perror("slave_init: sproc(slave, PR_SALL, NULL)"); - - /* Set up initial conditions---tricky! - Both the master and the slave start with one credit, since - both the result slot and the work/func slots are initially - free. - Note that we use setblockproccnt() for the master so a - possible indeterminate semaphore value caused by a previous - asa_done() at an unfortunate moment doesn't harm us. - */ - setblockproccnt(master_pid, 1); - unblockproc(slave_pid); - - return slave_pid; + if (slave_pid > 0) + return slave_pid; + master_pid = getpid(); + + /* Reset the queue, in case this is a re-init after asa_done() */ + work_result = NULL; + work_func = NULL; + work_arg = NULL; + + /* Create the slave process, sharing all segments and properties */ + slave_pid = sproc(slave, PR_SALL, (char *)NULL); + if (slave_pid < 0) + perror("slave_init: sproc(slave, PR_SALL, NULL)"); + + /* Set up initial conditions---tricky! + Both the master and the slave start with one credit, since + both the result slot and the work/func slots are initially + free. + Note that we use setblockproccnt() for the master so a + possible indeterminate semaphore value caused by a previous + asa_done() at an unfortunate moment doesn't harm us. + */ + setblockproccnt(master_pid, 1); + unblockproc(slave_pid); + + return slave_pid; } static void slave_done() { - if (slave_pid > 0) { - if (kill(slave_pid, SIGKILL) < 0) - perror("slave_done: kill(slave_pid, SIGKILL)"); - } - slave_pid = -1; + if (slave_pid > 0) { + if (kill(slave_pid, SIGKILL) < 0) + perror("slave_done: kill(slave_pid, SIGKILL)"); + } + slave_pid = -1; } /* Queue new work and return result of previous work */ static void * rendezvous(func, arg) - void * (*func)(); - void *arg; + void * (*func)(); + void *arg; { - void *result; - - if (slave_pid <= 0) - abort(); /* Illegal call: not initialized properly */ - - /* First rendezvous: store new work */ - if (blockproc(master_pid) < 0) - perror("rendezvous: [func,arg] blockproc(master_pid)"); - work_func = func; - work_arg = arg; - if (unblockproc(slave_pid) < 0) - perror("rendezvous: [func,arg] unblockproc(slave_pid)"); - - /* Second rendezvous: fetch previous result */ - if (blockproc(master_pid) < 0) - perror("rendezvous: [result] blockproc(master_pid)"); - result = work_result; - if (unblockproc(slave_pid) < 0) - perror("rendezvous: [result] unblockproc(slave_pid)"); - - return result; + void *result; + + if (slave_pid <= 0) + abort(); /* Illegal call: not initialized properly */ + + /* First rendezvous: store new work */ + if (blockproc(master_pid) < 0) + perror("rendezvous: [func,arg] blockproc(master_pid)"); + work_func = func; + work_arg = arg; + if (unblockproc(slave_pid) < 0) + perror("rendezvous: [func,arg] unblockproc(slave_pid)"); + + /* Second rendezvous: fetch previous result */ + if (blockproc(master_pid) < 0) + perror("rendezvous: [result] blockproc(master_pid)"); + result = work_result; + if (unblockproc(slave_pid) < 0) + perror("rendezvous: [result] unblockproc(slave_pid)"); + + return result; } /* Asynchronous audio interface (higher layer) */ -int audio_fd = -1; /* File descriptor -- not initialized yet */ +int audio_fd = -1; /* File descriptor -- not initialized yet */ static struct queue { - int func; /* 0 = read, 1 = write */ - char *buf; - int len; - int result; - int error; + int func; /* 0 = read, 1 = write */ + char *buf; + int len; + int result; + int error; } queue[2]; static int qindex = 0; @@ -305,135 +305,135 @@ static int qindex = 0; int asa_init() { - int fd; - char *p; - - if (audio_fd >= 0) - return audio_fd; - fd = open("/dev/audio", 2); - if (fd < 0) { - perror("asa_init: Can't open /dev/audio"); - return -1; - } - if (slave_init() < 0) { - perror("asa_init: Can't create slave process"); - close(fd); - return -1; - } - audio_fd = fd; - return fd; + int fd; + char *p; + + if (audio_fd >= 0) + return audio_fd; + fd = open("/dev/audio", 2); + if (fd < 0) { + perror("asa_init: Can't open /dev/audio"); + return -1; + } + if (slave_init() < 0) { + perror("asa_init: Can't create slave process"); + close(fd); + return -1; + } + audio_fd = fd; + return fd; } void asa_done() { - slave_done(); - if (audio_fd >= 0) { - if (close(audio_fd) < 0) - perror("asa_done: close(audio_fd)"); - } - audio_fd = -1; + slave_done(); + if (audio_fd >= 0) { + if (close(audio_fd) < 0) + perror("asa_done: close(audio_fd)"); + } + audio_fd = -1; } static void * runjob(arg) - void *arg; + void *arg; { - extern int errno; - struct queue *q = (struct queue *)arg; - char *buf = q->buf; - int len = q->len; - int n = 0; - - if (q->func == 0) - n = read(audio_fd, buf, len); - else - n = write(audio_fd, buf, len); - if (q->func == 0 && n >= 0) { - while (--len >= n && buf[len] == '\0') - ; - n = len+1; - } - q->result = n; - q->error = oserror(); - return arg; + extern int errno; + struct queue *q = (struct queue *)arg; + char *buf = q->buf; + int len = q->len; + int n = 0; + + if (q->func == 0) + n = read(audio_fd, buf, len); + else + n = write(audio_fd, buf, len); + if (q->func == 0 && n >= 0) { + while (--len >= n && buf[len] == '\0') + ; + n = len+1; + } + q->result = n; + q->error = oserror(); + return arg; } static void startjob(func, buf, len) - int func; - char *buf; - int len; + int func; + char *buf; + int len; { - struct queue *q; - - q = &queue[qindex]; - qindex = (qindex+1) & 1; - q->func = func; - q->buf = buf; - q->len = len; - (void) rendezvous(runjob, (void *)q); + struct queue *q; + + q = &queue[qindex]; + qindex = (qindex+1) & 1; + q->func = func; + q->buf = buf; + q->len = len; + (void) rendezvous(runjob, (void *)q); } void asa_start_read(buf, len) - char *buf; - int len; + char *buf; + int len; { - memset(buf, '\0', len); - startjob(0, buf, len); + memset(buf, '\0', len); + startjob(0, buf, len); } void asa_start_write(buf, len) - char *buf; - int len; + char *buf; + int len; { - startjob(1, buf, len); + startjob(1, buf, len); } int asa_wait() { - struct queue *q; - - q = (struct queue *) rendezvous((void*(*)())NULL, (void *)NULL); - if (q == NULL) { - setoserror(0); - return -1; /* No work was queued */ - } - setoserror(q->error); - return q->result; + struct queue *q; + + q = (struct queue *) rendezvous((void*(*)())NULL, (void *)NULL); + if (q == NULL) { + setoserror(0); + return -1; /* No work was queued */ + } + setoserror(q->error); + return q->result; } int asa_poll() { - int err; - - err = prctl(PR_ISBLOCKED, slave_pid); - if (err < 0) { - perror("prctl(PR_ISBLOCKED, slave_pid)"); - return -1; - } - else if (err == 0) - return 0; - else if (work_result == NULL) { - setoserror(0); - return -1; - } - else - return 1; + int err; + + err = prctl(PR_ISBLOCKED, slave_pid); + if (err < 0) { + perror("prctl(PR_ISBLOCKED, slave_pid)"); + return -1; + } + else if (err == 0) + return 0; + else if (work_result == NULL) { + setoserror(0); + return -1; + } + else + return 1; } int asa_cancel() { - int result; - - kill(slave_pid, MYSIG); - result = asa_wait(); - return result; + int result; + + kill(slave_pid, MYSIG); + result = asa_wait(); + return result; } @@ -445,50 +445,50 @@ asa_cancel() main() { - static char buf[10*16*1024]; /* 10 seconds of sound at 16K/sec */ - int n; - int afd; - - if ((afd = asa_init()) < 0) - exit(1); - ioctl(afd, AUDIOCSETRATE, 3); - ioctl(afd, AUDIOCSETOUTGAIN, 0); - printf("Poll returns %d\n", asa_poll()); - go("Hit enter to start recording:\n"); - asa_start_read(buf, sizeof buf); - go("Hit enter to stop recording:\n"); - /*printf("Poll returns %d\n", asa_poll());*/ - n = asa_cancel(); - if (n < 0) - perror("Read failed"); - else { - printf("Got %d bytes\n", n); - printf("Poll returns %d\n", asa_poll()); - go("Hit enter to start playing:\n"); - ioctl(afd, AUDIOCSETOUTGAIN, 50); - asa_start_write(buf, n); - go("Hit enter to stop playing:\n"); - printf("Poll returns %d\n", asa_poll()); - n = asa_cancel(); - if (n < 0) - perror("Write failed"); - else - printf("Stopped at %d bytes\n", n); - } - ioctl(afd, AUDIOCSETOUTGAIN, 0); - asa_done(); - exit(n < 0 ? 1 : 0); + static char buf[10*16*1024]; /* 10 seconds of sound at 16K/sec */ + int n; + int afd; + + if ((afd = asa_init()) < 0) + exit(1); + ioctl(afd, AUDIOCSETRATE, 3); + ioctl(afd, AUDIOCSETOUTGAIN, 0); + printf("Poll returns %d\n", asa_poll()); + go("Hit enter to start recording:\n"); + asa_start_read(buf, sizeof buf); + go("Hit enter to stop recording:\n"); + /*printf("Poll returns %d\n", asa_poll());*/ + n = asa_cancel(); + if (n < 0) + perror("Read failed"); + else { + printf("Got %d bytes\n", n); + printf("Poll returns %d\n", asa_poll()); + go("Hit enter to start playing:\n"); + ioctl(afd, AUDIOCSETOUTGAIN, 50); + asa_start_write(buf, n); + go("Hit enter to stop playing:\n"); + printf("Poll returns %d\n", asa_poll()); + n = asa_cancel(); + if (n < 0) + perror("Write failed"); + else + printf("Stopped at %d bytes\n", n); + } + ioctl(afd, AUDIOCSETOUTGAIN, 0); + asa_done(); + exit(n < 0 ? 1 : 0); } go(str) - char *str; + char *str; { - char line[100]; - - sleep(1); - fputs(str, stdout); - fflush(stdout); - fgets(line, sizeof line, stdin); + char line[100]; + + sleep(1); + fputs(str, stdout); + fflush(stdout); + fgets(line, sizeof line, stdin); } #endif /* MAIN */ @@ -2,12 +2,12 @@ Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. - All Rights Reserved + All Rights Reserved -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in +both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. diff --git a/src/assert.h b/src/assert.h index f21225e..07d11af 100644 --- a/src/assert.h +++ b/src/assert.h @@ -2,12 +2,12 @@ Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. - All Rights Reserved + All Rights Reserved -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in +both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. diff --git a/src/audiomodule.c b/src/audiomodule.c index cca0102..d4bce1a 100644 --- a/src/audiomodule.c +++ b/src/audiomodule.c @@ -2,12 +2,12 @@ Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. - All Rights Reserved + All Rights Reserved -Permission to use, copy, modify, and distribute this software and its -documentation for any purpose and without fee is hereby granted, +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that -both that copyright notice and this permission notice appear in +both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. @@ -38,12 +38,12 @@ static int audio_fd = -1; static int init() { - if (audio_fd >= 0) - return 1; - if ((audio_fd = asa_init()) >= 0) - return 1; - err_setstr(RuntimeError, "can't initialize async audio"); - return 0; + if (audio_fd >= 0) + return 1; + if ((audio_fd = asa_init()) >= 0) + return 1; + err_setstr(RuntimeError, "can't initialize async audio"); + return 0; } @@ -51,565 +51,565 @@ init() static object * audio_get_ioctl(self, args, code) - object *self; - object *args; - long code; + object *self; + object *args; + long code; { - long x; - if (!getnoarg(args)) - return NULL; - if (!init()) - return NULL; - if ((x = ioctl(audio_fd, code, (char *) NULL)) < 0) { - return NULL; - } - return newintobject(x); + long x; + if (!getnoarg(args)) + return NULL; + if (!init()) + return NULL; + if ((x = ioctl(audio_fd, code, (char *) NULL)) < 0) { + return NULL; + } + return newintobject(x); } static object * audio_set_ioctl(self, args, code) - object *self; - object *args; - long code; + object *self; + object *args; + long code; { - long x; - if (!getlongarg(args, &x)) - return NULL; - if (!init()) - return NULL; - if (ioctl(audio_fd, code, (char *) x) != 0) - return NULL; - INCREF(None); - return None; + long x; + if (!getlongarg(args, &x)) + return NULL; + if (!init()) + return NULL; + if (ioctl(audio_fd, code, (char *) x) != 0) + return NULL; + INCREF(None); + return None; } static object * audio_getingain(self, args) - object *self; - object *args; + object *self; + object *args; { - return audio_get_ioctl(self, args, AUDIOCGETINGAIN); + return audio_get_ioctl(self, args, AUDIOCGETINGAIN); } static object * audio_getoutgain(self, args) - object *self; - object *args; + object *self; + object *args; { - return audio_get_ioctl(self, args, AUDIOCGETOUTGAIN); + return audio_get_ioctl(self, args, AUDIOCGETOUTGAIN); } static object * audio_setingain(self, args) - object *self; - object *args; + object *self; + object *args; { - return audio_set_ioctl(self, args, AUDIOCSETINGAIN); + return audio_set_ioctl(self, args, AUDIOCSETINGAIN); } static object * audio_setoutgain(self, args) - object *self; - object *args; + object *self; + object *args; { - return audio_set_ioctl(self, args, AUDIOCSETOUTGAIN); + return audio_set_ioctl(self, args, AUDIOCSETOUTGAIN); } static object * audio_setrate(self, args) - object *self; - object *args; + object *self; + object *args; { - return audio_set_ioctl(self, args, AUDIOCSETRATE); + return audio_set_ioctl(self, args, AUDIOCSETRATE); } static object * audio_setduration(self, args) - object *self; - object *args; + object *self; + object *args; { - return audio_set_ioctl(self, args, AUDIOCDURATION); + return audio_set_ioctl(self, args, AUDIOCDURATION); } /* Compute average bias, and remove it */ static void unbias(buf, len) - char *buf; - int len; + char *buf; + int len; { - register int i; - register int c; - register long bias; - if (len == 0) - return; - bias = 0; - for (i = 0; i < len; i++) { - c = buf[i]; - if (c > 127) - c -= 256; - bias += c; - } - bias = (bias + len/2) / len; /* Rounded average */ - if (bias != 0) { - for (i = 0; i < len; i++) { - buf[i] -= bias; - } - } + register int i; + register int c; + register long bias; + if (len == 0) + return; + bias = 0; + for (i = 0; i < len; i++) { + c = buf[i]; + if (c > 127) + c -= 256; + bias += c; + } + bias = (bias + len/2) / len; /* Rounded average */ + if (bias != 0) { + for (i = 0; i < len; i++) { + buf[i] -= bias; + } + } } static object * audio_read(self, args) - object *self; - object *args; + object *self; + object *args; { - int c, i, n; - object *v; - char *s; - if (!getintarg(args, &n)) - return NULL; - if (n <= 0) { - err_setstr(RuntimeError, "audio.read: arg <= 0"); - return NULL; - } - if (!init()) - return NULL; - v = newsizedstringobject((char *)NULL, n); - if (v == NULL) - return err_nomem(); - s = getstringvalue(v); - n = read(audio_fd, s, n); - if (intrcheck()) { - DECREF(v); - err_set(KeyboardInterrupt); - return NULL; - } - /* Check for errors */ - if (n < 0) { - DECREF(v); - return NULL; - } - /* But EOF is reported as an empty string */ - - unbias(s, n); - resizestring(&v, n); - return v; + int c, i, n; + object *v; + char *s; + if (!getintarg(args, &n)) + return NULL; + if (n <= 0) { + err_setstr(RuntimeError, "audio.read: arg <= 0"); + return NULL; + } + if (!init()) + return NULL; + v = newsizedstringobject((char *)NULL, n); + if (v == NULL) + return err_nomem(); + s = getstringvalue(v); + n = read(audio_fd, s, n); + if (intrcheck()) { + DECREF(v); + err_set(KeyboardInterrupt); + return NULL; + } + /* Check for errors */ + if (n < 0) { + DECREF(v); + return NULL; + } + /* But EOF is reported as an empty string */ + + unbias(s, n); + resizestring(&v, n); + return v; } static object * audio_write(self, args) - object *self; - object *args; + object *self; + object *args; { - int n, n2; - object *v; - if (!getstrarg(args, &v)) - return NULL; - if (!init()) - return NULL; - errno = 0; - n2 = write(audio_fd, getstringvalue(v), n = getstringsize(v)); - if (intrcheck()) { - err_set(KeyboardInterrupt); - return NULL; - } - /* Check for other errors */ - if (n2 != n) { - if (errno == 0) - errno = EIO; - return NULL; - } - INCREF(None); - return None; + int n, n2; + object *v; + if (!getstrarg(args, &v)) + return NULL; + if (!init()) + return NULL; + errno = 0; + n2 = write(audio_fd, getstringvalue(v), n = getstringsize(v)); + if (intrcheck()) { + err_set(KeyboardInterrupt); + return NULL; + } + /* Check for other errors */ + if (n2 != n) { + if (errno == 0) + errno = EIO; + return NULL; + } + INCREF(None); + return None; } /* audio.amplify(sample, f1, f2). - Amplify a sample by a factor changing from f1/256 to (almost) f2/256. - Negative factors are allowed. Sound values that are to large - to fit in a byte are clipped. */ + Amplify a sample by a factor changing from f1/256 to (almost) f2/256. + Negative factors are allowed. Sound values that are to large + to fit in a byte are clipped. */ static object * audio_amplify(self, args) - object *self; - object *args; + object *self; + object *args; { - object *v; - char *s, *t; - int f1, f2; - int i, n; - int c; - if (!getstrintintarg(args, &v, &f1, &f2)) - return NULL; - n = getstringsize(v); - s = getstringvalue(v); - v = newsizedstringobject((char *)NULL, n); - if (v == NULL) - return err_nomem(); - t = getstringvalue(v); - for (i = 0; i < n; i++) { - c = s[i]; - if (c > 127) c -= 256; /* If chars are unsigned */ - c = c * ( f1*(n-i) + f2*i ) / ( n*256 ); - if (c > 127) c = 127; - else if (c < -128) c = -128; - t[i] = c; - } - return v; + object *v; + char *s, *t; + int f1, f2; + int i, n; + int c; + if (!getstrintintarg(args, &v, &f1, &f2)) + return NULL; + n = getstringsize(v); + s = getstringvalue(v); + v = newsizedstringobject((char *)NULL, n); + if (v == NULL) + return err_nomem(); + t = getstringvalue(v); + for (i = 0; i < n; i++) { + c = s[i]; + if (c > 127) c -= 256; /* If chars are unsigned */ + c = c * ( f1*(n-i) + f2*i ) / ( n*256 ); + if (c > 127) c = 127; + else if (c < -128) c = -128; + t[i] = c; + } + return v; } /* audio.reverse(s): return a sample backwards */ static object * audio_reverse(self, args) - object *self; - object *args; + object *self; + object *args; { - object *v; - char *s, *t; - int i, n; - if (!getstrarg(args, &v)) - return NULL; - n = getstringsize(v); - s = getstringvalue(v); - v = newsizedstringobject((char *)NULL, n); - if (v == NULL) - return err_nomem(); - t = getstringvalue(v); - for (i = 0; i < n; i++) { - t[n-1-i] = s[i]; - } - return v; + object *v; + char *s, *t; + int i, n; + if (!getstrarg(args, &v)) + return NULL; + n = getstringsize(v); + s = getstringvalue(v); + v = newsizedstringobject((char *)NULL, n); + if (v == NULL) + return err_nomem(); + t = getstringvalue(v); + for (i = 0; i < n; i++) { + t[n-1-i] = s[i]; + } + return v; } /* audio.add(a, b): add two samples. - Bytes that exceed the range are clipped. - If one is shorter, the rest of the longer sample is returned unchanged. */ + Bytes that exceed the range are clipped. + If one is shorter, the rest of the longer sample is returned unchanged. */ static object * audio_add(self, args) - object *self; - object *args; + object *self; + object *args; { - object *a, *b, *v; - char *sa, *sb, *t; - int i, n, na, nb, c, ca, cb; - if (!getstrstrarg(args, &a, &b)) - return NULL; - na = getstringsize(a); - sa = getstringvalue(a); - nb = getstringsize(b); - sb = getstringvalue(b); - n = (na > nb) ? na : nb; - v = newsizedstringobject((char *)NULL, n); - if (v == NULL) - return err_nomem(); - t = getstringvalue(v); - for (i = 0; i < n; i++) { - c = 0; - if (i < na) { - ca = sa[i]; - if (ca > 127) ca = ca - 256; - c = c + ca; - } - if (i < nb) { - cb = sb[i]; - if (cb > 127) cb = cb - 256; - c = c + cb; - } - if (c > 127) c = 127; - else if (c < -128) c = -128; - t[i] = c; - } - return v; + object *a, *b, *v; + char *sa, *sb, *t; + int i, n, na, nb, c, ca, cb; + if (!getstrstrarg(args, &a, &b)) + return NULL; + na = getstringsize(a); + sa = getstringvalue(a); + nb = getstringsize(b); + sb = getstringvalue(b); + n = (na > nb) ? na : nb; + v = newsizedstringobject((char *)NULL, n); + if (v == NULL) + return err_nomem(); + t = getstringvalue(v); + for (i = 0; i < n; i++) { + c = 0; + if (i < na) { + ca = sa[i]; + if (ca > 127) ca = ca - 256; + c = c + ca; + } + if (i < nb) { + cb = sb[i]; + if (cb > 127) cb = cb - 256; + c = c + cb; + } + if (c > 127) c = 127; + else if (c < -128) c = -128; + t[i] = c; + } + return v; } /* audio.chr2num(s) returns a list containing the numeric values - of the samples. */ + of the samples. */ static object * audio_chr2num(self, args) - object *self; - object *args; + object *self; + object *args; { - object *v, *w; - char *s; - int c, i, n; - static object *ints[256]; - - /* To avoid filling memory with all those int objects, we create - integer objects for all the desired values and reference these. */ - if (ints[255] == NULL) { - for (i = 0; i < 256; i++) { - if (ints[i] != NULL) - continue; - c = i; - if (c > 127) c -= 256; - ints[i] = newintobject((long)c); - if (ints[i] == NULL) - return NULL; - } - } - - if (!getstrarg(args, &v)) - return NULL; - n = getstringsize(v); - s = getstringvalue(v); - v = newlistobject(n); - if (v == NULL) - return err_nomem(); - for (i = 0; i < n; i++) { - c = s[i] & 0xff; - w = ints[c]; - INCREF(w); - if (setlistitem(v, i, w) != 0) { - DECREF(v); - return NULL; - } - } - return v; + object *v, *w; + char *s; + int c, i, n; + static object *ints[256]; + + /* To avoid filling memory with all those int objects, we create + integer objects for all the desired values and reference these. */ + if (ints[255] == NULL) { + for (i = 0; i < 256; i++) { + if (ints[i] != NULL) + continue; + c = i; + if (c > 127) c -= 256; + ints[i] = newintobject((long)c); + if (ints[i] == NULL) + return NULL; + } + } + + if (!getstrarg(args, &v)) + return NULL; + n = getstringsize(v); + s = getstringvalue(v); + v = newlistobject(n); + if (v == NULL) + return err_nomem(); + for (i = 0; i < n; i++) { + c = s[i] & 0xff; + w = ints[c]; + INCREF(w); + if (setlistitem(v, i, w) != 0) { + DECREF(v); + return NULL; + } + } + return v; } /* audio.num2chr is the inverse of audio.chr2num. - Excess values are clipped. */ + Excess values are clipped. */ static object * audio_num2chr(self, args) - object *self; - object *args; + object *self; + object *args; { - object *v, *w; - char *s; - int c, i, n; - if (!is_listobject(args)) { - err_badarg(); - return NULL; - } - n = getlistsize(args); - v = newsizedstringobject((char *)NULL, n); - if (v == NULL) - return NULL; - s = getstringvalue(v); - for (i = 0; i < n; i++) { - w = getlistitem(args, i); - if (!is_intobject(w)) { - DECREF(v); - err_badarg(); - return NULL; - } - s[i] = getintvalue(w); - } - return v; + object *v, *w; + char *s; + int c, i, n; + if (!is_listobject(args)) { + err_badarg(); + return NULL; + } + n = getlistsize(args); + v = newsizedstringobject((char *)NULL, n); + if (v == NULL) + return NULL; + s = getstringvalue(v); + for (i = 0; i < n; i++) { + w = getlistitem(args, i); + if (!is_intobject(w)) { + DECREF(v); + err_badarg(); + return NULL; + } + s[i] = getintvalue(w); + } + return v; } static object *stdaudio_buffer = NULL; static object * audio_start_recording(self, args) - object *self; - object *args; + object *self; + object *args; { - int n; - object *v; - char *s; - if (!getintarg(args, &n)) - return NULL; - if (stdaudio_buffer != NULL) { - err_setstr(RuntimeError, "audio.start_recording: device busy"); - return NULL; - } - if (n <= 0) { - err_setstr(TypeError, "audio.start_recording: arg <= 0"); - return NULL; - } - if (!init()) - return NULL; - v = newsizedstringobject((char *)NULL, n); - if (v == NULL) - return err_nomem(); - s = getstringvalue(v); - asa_start_read(s, n); - stdaudio_buffer = v; - INCREF(None); - return None; + int n; + object *v; + char *s; + if (!getintarg(args, &n)) + return NULL; + if (stdaudio_buffer != NULL) { + err_setstr(RuntimeError, "audio.start_recording: device busy"); + return NULL; + } + if (n <= 0) { + err_setstr(TypeError, "audio.start_recording: arg <= 0"); + return NULL; + } + if (!init()) + return NULL; + v = newsizedstringobject((char *)NULL, n); + if (v == NULL) + return err_nomem(); + s = getstringvalue(v); + asa_start_read(s, n); + stdaudio_buffer = v; + INCREF(None); + return None; } static object * audio_poll(self, args) - object *self; - object *args; + object *self; + object *args; { - int n; - if (!getnoarg(args)) - return NULL; - if (stdaudio_buffer == NULL) { - err_setstr(RuntimeError, "audio.poll: not busy"); - return NULL; - } - if (!init()) - return NULL; - if ((n = asa_poll()) < 0) - return NULL; - return newintobject(n); + int n; + if (!getnoarg(args)) + return NULL; + if (stdaudio_buffer == NULL) { + err_setstr(RuntimeError, "audio.poll: not busy"); + return NULL; + } + if (!init()) + return NULL; + if ((n = asa_poll()) < 0) + return NULL; + return newintobject(n); } static object * audio_wait_recording(self, args) - object *self; - object *args; + object *self; + object *args; { - object *v; - int n; - if (!getnoarg(args)) - return NULL; - if (stdaudio_buffer == NULL) { - err_setstr(RuntimeError, "audio.wait_recording: not busy"); - return NULL; - } - if (!init()) - return NULL; - if ((n = asa_wait()) < 0) - return NULL; - v = stdaudio_buffer; - stdaudio_buffer = NULL; - unbias(getstringvalue(v), n); - resizestring(&v, n); - return v; + object *v; + int n; + if (!getnoarg(args)) + return NULL; + if (stdaudio_buffer == NULL) { + err_setstr(RuntimeError, "audio.wait_recording: not busy"); + return NULL; + } + if (!init()) + return NULL; + if ((n = asa_wait()) < 0) + return NULL; + v = stdaudio_buffer; + stdaudio_buffer = NULL; + unbias(getstringvalue(v), n); + resizestring(&v, n); + return v; } static object * audio_stop_recording(self, args) - object *self; - object *args; + object *self; + object *args; { - int n; - object *v; - char *s; - if (!getnoarg(args)) - return NULL; - if (stdaudio_buffer == NULL) { - err_setstr(RuntimeError, "audio.stop_recording: not busy"); - return NULL; - } - if ((n = asa_cancel()) < 0) - return NULL; - v = stdaudio_buffer; - stdaudio_buffer = NULL; - s = getstringvalue(v); - unbias(s, n); - resizestring(&v, n); - return v; + int n; + object *v; + char *s; + if (!getnoarg(args)) + return NULL; + if (stdaudio_buffer == NULL) { + err_setstr(RuntimeError, "audio.stop_recording: not busy"); + return NULL; + } + if ((n = asa_cancel()) < 0) + return NULL; + v = stdaudio_buffer; + stdaudio_buffer = NULL; + s = getstringvalue(v); + unbias(s, n); + resizestring(&v, n); + return v; } static object * audio_start_playing(self, args) - object *self; - object *args; + object *self; + object *args; { - object *v; - if (!getstrarg(args, &v)) - return NULL; - if (stdaudio_buffer != NULL) { - err_setstr(RuntimeError, "audio.start_recording: device rbusy"); - return NULL; - } - asa_start_write(getstringvalue(v), (int)getstringsize(v)); - INCREF(v); - stdaudio_buffer = v; - INCREF(None); - return None; + object *v; + if (!getstrarg(args, &v)) + return NULL; + if (stdaudio_buffer != NULL) { + err_setstr(RuntimeError, "audio.start_recording: device rbusy"); + return NULL; + } + asa_start_write(getstringvalue(v), (int)getstringsize(v)); + INCREF(v); + stdaudio_buffer = v; + INCREF(None); + return None; } static object * audio_wait_playing(self, args) - object *self; - object *args; + object *self; + object *args; { - int n; - if (!getnoarg(args)) - return NULL; - if (stdaudio_buffer == NULL) { - err_setstr(RuntimeError, "audio.wait_playing: not busy"); - return NULL; - } - if ((n = asa_wait()) < 0) - return NULL; - DECREF(stdaudio_buffer); - stdaudio_buffer = NULL; - /* XXX return newintobject((long)n); ??? */ - INCREF(None); - return None; + int n; + if (!getnoarg(args)) + return NULL; + if (stdaudio_buffer == NULL) { + err_setstr(RuntimeError, "audio.wait_playing: not busy"); + return NULL; + } + if ((n = asa_wait()) < 0) + return NULL; + DECREF(stdaudio_buffer); + stdaudio_buffer = NULL; + /* XXX return newintobject((long)n); ??? */ + INCREF(None); + return None; } static object * audio_stop_playing(self, args) - object *self; - object *args; + object *self; + object *args; { - int n; - if (!getnoarg(args)) - return NULL; - if (stdaudio_buffer == NULL) { - err_setstr(RuntimeError, "audio.stop_playing: not busy"); - return NULL; - } - if ((n = asa_cancel()) < 0) |
