home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-05-18 | 37.8 KB | 1,401 lines |
- Newsgroups: comp.sources.unix
- From: lab@techno.sth.cgl.se (Lars Berntzon)
- Subject: v26i057: cforms - forms management front end for curses(3), Part02/03
- Sender: unix-sources-moderator@pa.dec.com
- Approved: vixie@pa.dec.com
-
- Submitted-By: lab@techno.sth.cgl.se (Lars Berntzon)
- Posting-Number: Volume 26, Issue 57
- Archive-Name: cforms/part02
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 2 (of 3)."
- # Contents: doc/cforms.tex src/get_field.c src/output.c src/token.c
- # Wrapped by vixie@cognition.pa.dec.com on Tue May 19 19:14:29 1992
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'doc/cforms.tex' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'doc/cforms.tex'\"
- else
- echo shar: Extracting \"'doc/cforms.tex'\" \(13964 characters\)
- sed "s/^X//" >'doc/cforms.tex' <<'END_OF_FILE'
- X% LATEX Documentation for CForms
- X% By Lars Berntzon
- X% $Log: cforms.tex,v $
- X% Revision 1.2 92/01/25 18:08:54 lasse
- X% Adde how to compile
- X%
- X% Revision 1.1 92/01/19 12:41:08 lasse
- X% Initial revision
- X%
- X%
- X\newcommand{\synopsys}[1]{\verb!\newline
- XSynopsys: {#1}!
- X}
- X
- X\documentstyle{article}
- X\title{CForms v 1.0}
- X\author{Lars Berntzon, E-Mail: lab@cgl.se}
- X\begin{document}
- X\maketitle
- X
- X\section{Introduction}
- X
- X CForms is a formular manager for building applications to be used for
- X many types of terminals though it uses the {\em curses} library.
- X CForms is built up by a language that consists of the objects: modules,
- X pictures, fields, literals and events.
- X
- X An application is built by one or more modules that contains one or more
- X pictures that handles the various functions in the application.
- X Each picture may contain any number of fields and text literal
- X that describes the appearance of the picture.
- X
- X Fields are defined by their name and may be specified with any type,
- X size, special attributes and event handling functions.
- X
- X CForms may be intermixed with C-code modules in any way.
- X
- X
- X\section{Language description}
- X
- X Currently you can only have one module (file) with CForms language,
- X but i plan to make a CForms 'linker' and thus make it possible to
- X have multiple files.
- X
- X The language is not case sensitive exept thoose parts
- X that are pure C-code (in events- and ccode statements).
- X
- X
- X\subsection{Viewports}
- X
- X To create a picture you first need to create a viewport, wich
- X describes the size and position on the real screen where the picture
- X should appear. A viewport is defined by its name, wich later should
- X be referenced in the picture. Several pictures may share the same
- X viewport.
- X
- X Syntax for a viewport is:
- X \begin{verbatim}
- X VIEWPORT <name> {
- X POS <column>, <row>;
- X SIZE <width>, <height>;
- X }
- X \end{verbatim}
- X
- X Example:
- X \begin{verbatim}
- X Viewport stdscreen {
- X Pos 1,1;
- X Size 80, 24;
- X }
- X \end{verbatim}
- X
- X
- X\subsection{CCode}
- X The CCode statement introduces a C-code block that may contain any
- X code such as global variables, functions, preprocessor statements.
- X This means that there is no need to have separated form-files and
- X C-files with support functions. Ccodes may only be used in outer
- X scopes, i.e. not inside pictures, literals or fields.
- X
- X Syntax for Ccode is:
- X \begin{verbatim}
- X CCODE {
- X <Any C code>
- X }
- X \end{verbatim}
- X
- X Example:
- X \begin{verbatim}
- X CCode {
- X cleanup()
- X {
- X free_all_mem();
- X cforms_end();
- X exit();
- X }
- X }
- X \end{verbatim}
- X
- X
- X\subsection{Pictures}
- X
- X A picture is what shows up on the terminal when the application is
- X running. Pictures contains fields - which are places for input or
- X output, literals - which are static texts and events -
- X events doesn't show but defines C-code functions to be called when
- X certain things happens. Events specified in pictures are actually
- X used by fields, but they are automatically specified for all fields
- X in that pictures (unless specifically turned off), in other worlds a
- X default event for all fields.
- X
- X A pictures is defined by its name, and the first picture to be started
- X in the application is determined in the main routine (or a descendant)
- X through the function {\em pic\_call}.
- X
- X
- X Syntax for a picture is:
- X \begin{verbatim}
- X PICTURE <picture-name> VIEWPORT <viewport-name> {
- X <literals> \
- X <fields> > in any order
- X <events> /
- X }
- X \end{verbatim}
- X
- X Example:
- X \begin{verbatim}
- X Picture main Viewport stdscreen {
- X Literal 10, 10, "Welcome, enter your name: ";
- X Field Name {
- X Pos +0, +1;
- X Type Char(10);
- X }
- X }
- X \end{verbatim}
- X
- X
- X\subsection{Fields}
- X
- X Fields are placesholders in pictures that may be used to input from
- X operator and/or as output from the application. A field may be of
- X many kind of types and sizes. Fields can also contain events that
- X will be called when the specified event occurs. It is also possible
- X to specify literal text to appear immediately before and immediately
- X after the actual field, this makes it possible to have text belonging
- X to fields that are not depending of the fields position.
- X
- X The position of a field may be specified in absolute- or relative
- X coordinates. For relative coordinates the column- and row number is
- X prepended by a minus or a plus sign to indikate positive or negative
- X relative position.
- X Finally it is possible to specify a couple of modifiers for the field that
- X modifies adjustment, visibility, protection e.t.c.
- X Valid types for a field is:
- X \begin{itemize}
- X \item{INT} Field may only contain digits.
- X \item{STR} Field may only contain alphanumeric characters.
- X \item{CHAR} Field may contain any printable character.
- X \end{itemize}
- X The size of a field is by default 1, but may be altered as a number
- X within brackets after the type.
- X
- X Valid modifiers are:
- X \begin{itemize}
- X \item{PROTECTED} - Field may not be altered.
- X \item{UPPERCASE} - All alpha characters are in uppercase.
- X \end{itemize}
- X
- X Syntax for a field is:
- X \begin{verbatim}
- X FIELD <name> {
- X <type> [(<size>)];
- X POS [+-]<column>, [+-]<row>;
- X LVALUE "<left value>";
- X RVALUE "<right value>";
- X [UPPERCASE;]
- X [PROTECTED;]
- X }
- X \end{verbatim}
- X Example:
- X \begin{verbatim}
- X Field adress {
- X Pos 20, +1;
- X Type Char(20);
- X LValue "Adress: ";
- X Uppercase;
- X }
- X \end{verbatim}
- X
- X\subsection{Literals}
- X Literals is static text to be visualized in the picture, literals
- X is below fields if a collission should occur. Syntax of a literal
- X is:
- X \begin{verbatim}
- X Literal [+-]<column>, [+-]<row>, "<text>";
- X \end{verbatim}
- X Example:
- X \begin{verbatim}
- X Literal +0, +1, "List of persons";
- X \end{verbatim}
- X\subsection{Events}
- X
- X Events are used to define functions keys and other special cases for eg.
- X refresh display and so on. An event is specified by its event type
- X and either a block of C-code to execute when the event occurres, or the
- X word 'forget' wich means that the specified event should be disabled for
- X this field.
- X
- X Type of events is:
- X \begin{itemize}
- X \item[KEY] A key has been pressed. An event of the type KEY must
- X have one of the following modifiers:
- X \begin{itemize}
- X \item[F0 - F20 -] Function keys
- X \item[UP -] Up arrow
- X \item[DOWN -] Down arrow
- X \item[LEFT -] Left arrow
- X \item[RIGHT -] Right arrow
- X \item[CR -] Carriage return
- X \item[BS -] Backspace
- X \item[TAB -] Horizontal tab
- X \item[FIND -] Search
- X \item[INSERT -] Insert
- X \end{itemize}
- X \item[REFRESH] Refresh the picture
- X \item[DRAW] Only available from pictures, is called
- X when a pictures is first drawn.
- X \item[LEFT] When trying to move to the left of the field
- X \item[RIGHT] When trying to move to the left of the field
- X \item[ENTRY] When field is entered
- X \item[EXIT] When field is left
- X \end{itemize}
- X
- X
- X Syntax for an event is:
- X \begin{verbatim}
- X EVENT <type> [<type modifier>] {
- X <C-code statements>
- X }
- X \end{verbatim}
- X or
- X \begin{verbatim}
- X EVENT <type> [<type modifier>] FORGET;
- X \end{verbatim}
- X Example:
- X \begin{verbatim}
- X Event Entry {
- X fld_set(current.field, "X");
- X }
- X
- X Event Key DOWN {
- X fld_move(fld_down(current.field));
- X }
- X \end{verbatim}
- X
- X\section{EVENT and CCODE programing}
- X
- X
- X For all event- and ccode blocks the developer can use a set
- X library functions that comes with cforms. Most functions returning
- X integers returns ether OK or FAIL exept for those like fld\_len that
- X returns the value expected. Those returning pointers return NULL
- X uppon failure.
- X This is a list and a description of all functions:
- X \subsection{Start/stop functions}
- X \subsubsection{cforms\_init} Initiate CForms, this must be done
- X before any cforms functions can be called.
- X \synopsys{int cforms\_init(void)}
- X
- X \subsubsection{cforms\_end} Stop CForms.
- X \synopsys{int cforms\_end(void)}
- X
- X \subsection{Picture functions}
- X \subsubsection{pic\_call} Call picture.
- X \synopsys{int pic\_call(struct picture *, field *)}\\
- X Where picture is a pointer to the picture to call and field
- X is where the cursor lands on, NULL means the first field in
- X picture.
- X
- X \subsubsection{pic\_clear} Clear all fields for picture.
- X \synopsys{int pic\_clear(struct picture *)}\\
- X If picture is NULL current.picture is cleared.
- X
- X \subsubsection{pic\_leave} Leave current picture after current
- X event has finished.
- X \synopsys{int pic\_leave(void)}
- X
- X \subsubsection{picture} Get picture with name.
- X \synopsys{struct picture *picture(char *fmt, ...)}\\
- X Returns a pointer to a picture with the name generated
- X with 'fmt' and its arguments the same way that printf works.
- X
- X \subsection{Field functions}
- X \subsubsection{field} Get field with name
- X \synopsys{struct field *field(char *fmt, ...)}\\
- X Returns a pointer to a field with the name generated
- X with 'fmt' and its arguments the same way that printf works.
- X
- X If the field name is prepended by the picture name and a
- X colon, i.e. "picture:field", a field can be found in another
- X picture than current.picture.
- X
- X \subsubsection{fld\_isempty} Check if field is empty, i.e.
- X full of spaces, tabs or null.
- X \synopsys{int fld\_isempty(struct field *)}\\
- X Returns TRUE or FALSE condition.
- X
- X \subsubsection{fld\_first} Find first field of picture.
- X \synopsys{struct field *fld\_first(void)}
- X
- X \subsubsection{fld\_last} Find last field of picture.
- X \synopsys{struct field *fld\_last(void)}
- X
- X \subsubsection{fld\_next} Find next field for picture.
- X \synopsys{struct field *fld\_next(struct field *)}\\
- X Returns the field after field given by argument (or
- X current.field if NULL).
- X
- X \subsubsection{fld\_previous} Find previous field for picture.
- X \synopsys{struct field *fld\_previous(struct field *)}\\
- X Returns the field berfore field given by argument (or
- X current.field if NULL).
- X
- X \subsubsection{fld\_left} Find left field.
- X \synopsys{struct field *fld\_left(struct field *)}\\
- X Returns the field to the left of the field given by argument
- X (or current.field if NULL).
- X
- X \subsubsection{fld\_right} Find right field.
- X \synopsys{struct field *fld\_right(struct field *)}\\
- X Returns the field to the right of the field given by argument
- X (or current.field if NULL).
- X
- X \subsubsection{fld\_up} Find field above current.
- X \synopsys{struct field *fld\_up(struct field *)}\\
- X Returns the field above the field given by argument
- X (or cur\-rent.\-field if\- NULL).
- X
- X \subsubsection{fld\_down} Find field below current.
- X \synopsys{struct field *fld\_down(struct field *)}\\
- X Returns the field below the field given by argument
- X (or current.\-field if\- NULL).
- X
- X \subsubsection{fld\_set} Set value for field.
- X \synopsys{int fld\_set(struct field *, char *)}\\
- X If field is NULL set value for current.field.
- X
- X \subsubsection{fld\_nset} Set value for field but max n chars.
- X \synopsys{int fld\_nset(struct field *, char *)}\\
- X If field is NULL set value for current.field.
- X
- X \subsubsection{fld\_get} Get value of field.
- X \synopsys{char *fld\_get(struct field *)}\\
- X If field is NULL get value for current.field.
- X
- X \subsubsection{fld\_len} Return length of field.
- X \synopsys{int fld\_len(struct field *)}\\
- X If field is NULL return length for current.field.
- X
- X \subsubsection{fld\_ismodified} Return true if field has been
- X modified since last fld\_set or fld\_nset.
- X \synopsys{int fld\_ismodified(struct field *)}\\
- X If field is NULL return modified for current.field.
- X
- X \subsubsection{fld\_touch} Make field not modified.
- X \synopsys{int fld\_touch(struct field *)}\\
- X If field is NULL touch current.field.
- X
- X \subsection{General functions}
- X \subsubsection{message} Give message (line 24).
- X \synopsys{int message(char *fmt, ...)}
- X
- X \subsubsection{strequ} Compare two strings, but case insensitive.
- X \synopsys{int strequ(char *s1, char *s2)}
- X
- X \subsection{Global variables}
- X \subsubsection{current.picture} Allways points to current picture
- X (can be NULL).
- X \synopsys{struct picture *current.picture}
- X
- X \subsubsection{current.field} Allways points to current field
- X (can be NULL).
- X \synopsys{struct field *current.field}
- X
- X\section{How to compile and install CForms}
- X
- XWhen you have unpacked the CForms archive you just do cd to the directory
- Xwhere you unpacked it and change the DEST-variable in the Makefile to
- Xwhere you want CForms installed.
- XAfter that you should do a 'make' followed by an 'make install'.
- X
- XYou can also compile the example application in the example subdirectory
- Xby doing 'make example', look it up to see what an application might look
- Xlike.
- X
- X\section{How to compile CForms applications}
- X
- XLets say you just installed CForms with DEST set to {\em /usr/local},
- Xthat means that the CForms compiler resides in /usr/local/bin, the
- Xcforms include file resides in /usr/local/include and the library
- Xlibcforms.a in /usr/local/lib, then this is how to compile a CForms
- Xapplication:
- X
- XFirst you run the CForms compiler, {\em cfc}, on your inputfile:
- X \begin{verbatim}
- X cfc <yourfile>
- X \end{verbatim}
- XThis will generate the compiler output file, {\em cforms.c}. That file
- Xshould be compiled with you ordinary c-compiler by doing something like:
- X \begin{verbatim}
- X cc -o <yourapp> -I/usr/local/include -L/usr/local/lib \
- X cforms.c -lcforms -lcurses
- X \end{verbatim}
- XObserve that the -L flag must be there beccause the cforms library is in
- X/usr/\-local/\-lib.
- X
- XNow you should be able to run you application.
- X
- X\newpage
- X\tableofcontents
- X
- X\end{document}
- END_OF_FILE
- if test 13964 -ne `wc -c <'doc/cforms.tex'`; then
- echo shar: \"'doc/cforms.tex'\" unpacked with wrong size!
- fi
- # end of 'doc/cforms.tex'
- fi
- if test -f 'src/get_field.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'src/get_field.c'\"
- else
- echo shar: Extracting \"'src/get_field.c'\" \(6000 characters\)
- sed "s/^X//" >'src/get_field.c' <<'END_OF_FILE'
- X/*******************************************************************************
- X *
- X * G E T _ F I E L D . C
- X * ---------------------
- X *
- X * Description:
- X * Compiles a field statement.
- X *
- X * Included functions:
- X * get_field - Does the job.
- X *
- X * Revision:
- X * Ver Date By Reason
- X * --- ---- -- ------
- X * 1.00 900625 Lars Berntzon Created
- X *
- X ******************************************************************************/
- X
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <string.h>
- X
- X#include "token.h"
- X#include "comp.h"
- X
- X#define isnormal(ch) (isalnum(ch) || (ch) == '_')
- X
- Xstatic int found_pos();
- Xstatic int found_event();
- Xstatic int found_lvalue();
- Xstatic int found_rvalue();
- Xstatic int found_type();
- Xstatic int found_protected();
- Xstatic int found_uppercase();
- X
- Xstatic struct lookup_s lookup[] = {
- X "LVALUE", found_lvalue,
- X "RVALUE", found_rvalue,
- X "POS", found_pos,
- X "EVENT", found_event,
- X "TYPE", found_type,
- X "PROTECTED", found_protected,
- X "UPPERCASE", found_uppercase
- X};
- X
- Xstruct field *get_field()
- X{
- X char token[TOKENSIZE];
- X struct field *fp = NULL;
- X int i;
- X
- X if (GetTokNC(token) == NULL || !isnormal(token[0])) {
- X error("expected name of field");
- X return NULL;
- X }
- X
- X fp = memalloc(sizeof *fp);
- X link_name(&fp->link, token);
- X
- X if (GetTokNC(token) == NULL || strcmp(token, "{")) {
- X error("expected '{'");
- X unget_field(fp);
- X return NULL;
- X }
- X
- X while(GetTokNC(token) != NULL) {
- X if(strcmp(token, "}") == 0) break;
- X else if (strcmp(token, "{") == 0) {
- X UnGetTok("{");
- X skip_stmt();
- X continue;
- X }
- X
- X for(i = 0; i < N_CMDS; i++) {
- X if (strequ(token, lookup[i].cmd) == 0) break;
- X }
- X if (i < N_CMDS) {
- X if ((*lookup[i].func)(fp) != OK) {
- X unget_field(fp);
- X return NULL;
- X }
- X }
- X else {
- X error("unknown token for field");
- X }
- X }
- X if (fp->lvalue && fp->pos.x - (int) strlen(fp->lvalue) < 1) {
- X error("field has position to far left");
- X }
- X if (fp->rvalue && fp->pos.x + fp->len + (int) strlen(fp->rvalue) >= 80) {
- X error("field has position to far left");
- X }
- X if (fp->type == NULL) {
- X error("undefined type for field");
- X unget_field(fp);
- X return NULL;
- X }
- X return fp;
- X}
- X
- Xstruct field *unget_field(struct field *fp)
- X{
- X struct field *next;
- X struct event *ep;
- X
- X if (fp == NULL) return NULL;
- X next = (struct field *)fp->link.next;
- X
- X for(ep = fp->event; ep != NULL; ep = unget_event(ep))
- X ;
- X if (fp->link.name) free(fp->link.name);
- X if (fp->lvalue) free(fp->lvalue);
- X if (fp->rvalue) free(fp->rvalue);
- X free(fp);
- X
- X return next;
- X}
- X
- Xstatic int found_lvalue(fp)
- X struct field *fp;
- X{
- X char token[TOKENSIZE];
- X
- X if (GetTokNC(token) == NULL || token[0] != '"') {
- X error("expected lvalue");
- X return FAIL;
- X }
- X token[strlen(token) - 1] = 0;
- X
- X fp->lvalue = memalloc(strlen(token));
- X strcpy(fp->lvalue, token + 1);
- X if (GetTokNC(token) == NULL || strcmp(token, ";")) {
- X error("expected ';'");
- X return FAIL;
- X }
- X
- X return OK;
- X}
- X
- Xstatic int found_rvalue(fp)
- X struct field *fp;
- X{
- X char token[TOKENSIZE];
- X
- X if (GetTokNC(token) == NULL || token[0] != '"') {
- X error("expected rvalue");
- X return FAIL;
- X }
- X token[strlen(token) - 1] = 0;
- X
- X fp->rvalue = memalloc(strlen(token));
- X strcpy(fp->rvalue, token + 1);
- X if (GetTokNC(token) == NULL || strcmp(token, ";")) {
- X error("expected ';'");
- X return FAIL;
- X }
- X
- X return OK;
- X}
- X
- Xstatic int found_pos(fp)
- X struct field *fp;
- X{
- X char token[TOKENSIZE];
- X
- X if (GetPos(&fp->pos.x, & fp->pos.y) != OK) return FAIL;
- X
- X if (GetTokNC(token) == NULL || strcmp(token, ";")) {
- X error("expected ';'");
- X return FAIL;
- X }
- X return OK;
- X}
- X
- Xstatic int found_event(fp)
- X struct field *fp;
- X{
- X struct event *ep;
- X
- X if ((ep = get_event()) == NULL) return FAIL;
- X
- X link((struct link **)&fp->event, &ep->link, TYPE_EVENT);
- X
- X return OK;
- X}
- X
- Xstatic int found_type(fp)
- X struct field *fp;
- X{
- X char token[TOKENSIZE];
- X int i;
- X
- X static struct {
- X char *name;
- X char *code;
- X } type[] = {
- X "INT", "FLD_INT",
- X "CHAR", "FLD_STR",
- X "ALNUM", "FLD_ALNUM",
- X NULL, NULL
- X };
- X
- X if (GetTokNC(token) == NULL) {
- X error("unexpected end of file for field");
- X return FAIL;
- X }
- X
- X for(i = 0; type[i].name != NULL; i++) {
- X if (strequ(token, type[i].name) == 0) break;
- X }
- X
- X if (type[i].name == NULL) {
- X error("unknown type for field");
- X return FAIL;
- X }
- X
- X fp->type = type[i].code;
- X fp->len = 1;
- X
- X if (GetTokNC(token) == NULL) {
- X error("unexpected end of file for field");
- X return FAIL;
- X }
- X
- X if (strcmp(token, ";") == 0) return OK;
- X
- X if (strcmp(token, "(") != 0) {
- X error("expected '(' or ';' after field type");
- X return FAIL;
- X }
- X
- X if (GetTokNC(token) == NULL || !isdigit(token[0])) {
- X error("field size must be integer");
- X return FAIL;
- X }
- X
- X fp->len = atoi(token);
- X
- X if (GetTokNC(token) == NULL || strcmp(token, ")")) {
- X error("expected ')' after field");
- X return FAIL;
- X }
- X
- X if (GetTokNC(token) == NULL || strcmp(token, ";")) {
- X error("expected ';' after field");
- X return FAIL;
- X }
- X
- X return OK;
- X}
- X
- Xstatic int found_protected(fp)
- X struct field *fp;
- X{
- X char token[TOKENSIZE];
- X
- X fp->flags |= FLD_PROTECTED;
- X
- X if (GetTokNC(token) == NULL) {
- X error("unexpected end of file for field");
- X return FAIL;
- X }
- X if (strcmp(token, ";") != 0) {
- X error("expetced ';'");
- X return FAIL;
- X }
- X
- X return OK;
- X}
- X
- Xstatic int found_uppercase(fp)
- X struct field *fp;
- X{
- X char token[TOKENSIZE];
- X
- X fp->flags |= FLD_UPPERCASE;
- X
- X if (GetTokNC(token) == NULL) {
- X error("unexpected end of file for field");
- X return FAIL;
- X }
- X if (strcmp(token, ";") != 0) {
- X error("expetced ';'");
- X return FAIL;
- X }
- X
- X return OK;
- X}
- X
- END_OF_FILE
- if test 6000 -ne `wc -c <'src/get_field.c'`; then
- echo shar: \"'src/get_field.c'\" unpacked with wrong size!
- fi
- # end of 'src/get_field.c'
- fi
- if test -f 'src/output.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'src/output.c'\"
- else
- echo shar: Extracting \"'src/output.c'\" \(8094 characters\)
- sed "s/^X//" >'src/output.c' <<'END_OF_FILE'
- X/*******************************************************************************
- X *
- X * O U T P U T . C
- X * ---------------
- X *
- X * Description:
- X * Generates the C-code for CForms.
- X *
- X * Included functions:
- X * output - Does the job
- X *
- X * Revision:
- X * Ver Date By Reason
- X * --- ---- -- ------
- X * 1.00 900627 Lars Berntzon Created
- X *
- X ******************************************************************************/
- X
- X#include "config.h"
- X
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <string.h>
- X#include <assert.h>
- X
- X#include "token.h"
- X#include "comp.h"
- X
- Xvoid output()
- X{
- X struct viewport *vp;
- X struct picture *pp;
- X struct field *fp;
- X struct event *ep;
- X struct stmt *sp;
- X struct literal *lp;
- X struct ccode *cp;
- X int n_fields, n_events, n_literals, n_pictures;
- X FILE *out;
- X
- X if ((out = fopen("cforms.c", "w")) == NULL) {
- X fprintf(stderr, "failed to open output file.\n");
- X cleanup(1);
- X }
- X
- X /*
- X * G e n e r a t e H e a d e r.
- X */
- X
- X fprintf(out, "/* Generated by C-Forms version %s */\n\n", version);
- X fprintf(out, "#include <stdio.h>\n\n");
- X fprintf(out, "#include <curses.h>\n");
- X fprintf(out, "#include \"cforms.h\"\n\n");
- X
- X#if 0
- X /*
- X * C r e a t e v i e w p o r t s.
- X */
- X if (list.viewport) {
- X fprintf(out, " /* V i e w p o r t s */\n\n");
- X for(vp = list.viewport; vp != NULL; vp = NEXT_VIEWPORT(vp))
- X {
- X fprintf(out, "WINDOW *win_%s;\n", vp->link.name);
- X }
- X fprintf(out, "\n");
- X }
- X#endif
- X
- X /*
- X * C r e a t e C - c o d e s.
- X */
- X if (list.ccode) fprintf(out, " /* C - c o d e s */\n\n");
- X for(cp = list.ccode; cp != NULL; cp = NEXT_CCODE(cp))
- X {
- X for(sp = cp->stmt; sp != NULL; sp = sp->next) {
- X fprintf(out, "%s\n", sp->txt);
- X }
- X fprintf(out, "\n");
- X }
- X
- X /*
- X * C r e a t e p i c t u r e s.
- X */
- X if (list.picture) fprintf(out, " /* P i c t u r e s */\n\n");
- X for(pp = list.picture; pp != NULL; pp = NEXT_PICTURE(pp))
- X {
- X /*
- X * E v e n t s f o r a l l f i e l d s.
- X */
- X n_fields = 0;
- X n_literals = 0;
- X
- X for(fp = pp->field; fp != NULL; fp = NEXT_FIELD(fp))
- X {
- X /* Event routines for field */
- X
- X for(ep = fp->event; ep != NULL; ep = NEXT_EVENT(ep))
- X {
- X if (ep->stmt) {
- X fprintf(out, "static void event_%s_%s_%s()\n{\n",
- X pp->link.name, fp->link.name, ep->link.name);
- X for(sp = ep->stmt; sp != NULL; sp = sp->next)
- X {
- X fprintf(out, " %s\n", sp->txt);
- X }
- X fprintf(out, "}\n");
- X }
- X }
- X
- X /* Event table for field */
- X
- X if (fp->event) {
- X fprintf(out, "static struct event etab_%s_%s[] = {\n",
- X pp->link.name, fp->link.name);
- X for(ep = fp->event; ep != NULL; ep = NEXT_EVENT(ep))
- X {
- X fprintf(out, " %s, %s, ", ep->type, ep->code);
- X
- X if (ep->stmt) {
- X fprintf(out, "event_%s_%s_%s",
- X pp->link.name, fp->link.name, ep->link.name);
- X }
- X else {
- X fprintf(out, "NULL");
- X }
- X fprintf(out, "%s\n", NEXT_EVENT(ep) ? "," : "");
- X }
- X fprintf(out, "};\n\n");
- X }
- X
- X /*
- X * F i e l d d a t a a r r a y.
- X */
- X fprintf(out, "static char field_%s_%s[%d];\n\n",
- X pp->link.name, fp->link.name, fp->len + 1);
- X fprintf(out, "/***************************************************/\n");
- X }
- X
- X /*
- X * T a b l e o f a l l f i e l d s.
- X */
- X if (pp->field) {
- X fprintf(out, "static struct field ftab_%s[] = {\n", pp->link.name);
- X
- X for(fp = pp->field; fp != NULL; n_fields++, fp = NEXT_FIELD(fp))
- X {
- X fprintf(out, " {\"%s\", ", fp->link.name);
- X fprintf(out, "%s, %d, ", fp->type, fp->len);
- X fprintf(out, "field_%s_%s, ", pp->link.name, fp->link.name);
- X fprintf(out, "%d, %d, \"%s\", \"%s\", ", fp->pos.x, fp->pos.y,
- X fp->lvalue ? fp->lvalue : "", fp->rvalue ? fp->rvalue : "");
- X if (fp->event) {
- X fprintf(out, "etab_%s_%s", pp->link.name, fp->link.name);
- X }
- X else {
- X fprintf(out, "NULL");
- X }
- X for(n_events = 0, ep = fp->event; ep != NULL;
- X n_events++, ep = NEXT_EVENT(ep))
- X ;
- X fprintf(out, ", %d, %d", n_events, fp->flags);
- X fprintf(out, ", 0"); /* Modified flag */
- X fprintf(out, ", NULL"); /* Modified flag */
- X fprintf(out, "}%s", NEXT_FIELD(fp) ? ",\n" : "\n");
- X }
- X fprintf(out, "};\n\n");
- X }
- X
- X /*
- X * T a b l e o f l i t e r a l s.
- X */
- X if (pp->literal) {
- X fprintf(out, "static struct literal ltab_%s[] = {\n", pp->link.name);
- X for(lp = pp->literal; lp != NULL; n_literals++, lp = NEXT_LITERAL(lp))
- X {
- X fprintf(out, " %d, %d, \"%s\", %d%s\n",
- X lp->pos.x, lp->pos.y,
- X lp->link.name, lp->display_flags,
- X NEXT_LITERAL(lp) ? "," : "");
- X }
- X fprintf(out, "};\n\n");
- X }
- X
- X /*
- X * E v e n t s f o r p i c t u r e.
- X */
- X
- X for(ep = pp->event; ep != NULL; ep = NEXT_EVENT(ep))
- X {
- X if (ep->stmt) {
- X fprintf(out, "static void event_%s_%s()\n{\n",
- X pp->link.name, ep->link.name);
- X for(sp = ep->stmt; sp != NULL; sp = sp->next)
- X {
- X fprintf(out, " %s\n", sp->txt);
- X }
- X fprintf(out, "}\n\n");
- X }
- X }
- X
- X n_events = 0;
- X if(pp->event) {
- X fprintf(out, "static struct event etab_%s[] = {\n",
- X pp->link.name);
- X for(ep = pp->event; ep != NULL; n_events++, ep = NEXT_EVENT(ep))
- X {
- X fprintf(out, " %s, %s, ", ep->type, ep->code);
- X
- X if (ep->stmt) {
- X fprintf(out, "event_%s_%s", pp->link.name, ep->link.name);
- X }
- X else {
- X fprintf(out, "NULL");
- X }
- X fprintf(out, "%s\n", NEXT_EVENT(ep) ? "," : "");
- X }
- X fprintf(out, "};\n\n");
- X }
- X }
- X
- X /*
- X * T a b l e o f p i c t u r e s.
- X */
- X if(list.picture) {
- X fprintf(out, "static struct picture ptab[] = {\n");
- X }
- X n_pictures = 0;
- X for(pp = list.picture; pp != NULL; n_pictures++, pp = NEXT_PICTURE(pp))
- X {
- X fprintf(out, " {\"%s\", ", pp->link.name);
- X /* Fields */
- X for(n_fields = 0, fp = pp->field; fp; fp = NEXT_FIELD(fp)) {
- X n_fields++;
- X }
- X
- X if (pp->field) {
- X fprintf(out, "ftab_%s, %d, ", pp->link.name, n_fields);
- X }
- X else fprintf(out, "NULL, 0, ");
- X
- X /* Literals */
- X for(n_literals = 0, lp = pp->literal; lp; lp = NEXT_LITERAL(lp)) {
- X n_literals++;
- X }
- X if (pp->literal) {
- X fprintf(out, "ltab_%s, %d, ", pp->link.name, n_literals);
- X }
- X else fprintf(out, "NULL, 0, ");
- X
- X /* Events */
- X for(n_events = 0, ep = pp->event; ep; ep = NEXT_EVENT(ep)) {
- X n_events++;
- X }
- X if (pp->event) {
- X fprintf(out, "etab_%s, %d", pp->link.name, n_events);
- X }
- X else fprintf(out, "NULL, 0");
- X
- X fprintf(out, ", %d,%d, %d,%d}", pp->viewport->pos.x, pp->viewport->pos.y,
- X pp->viewport->size.x, pp->viewport->size.y);
- X if (NEXT_PICTURE(pp)) {
- X fprintf(out, ",");
- X }
- X fprintf(out, "\n");
- X }
- X if(list.picture) {
- X fprintf(out, "};\n\n");
- X }
- X
- X /*
- X * Event functions for module.
- X */
- X for(ep = list.event; ep != NULL; ep = NEXT_EVENT(ep))
- X {
- X if (ep->stmt) {
- X fprintf(out, "static void event_%s()\n{\n", ep->link.name);
- X for(sp = ep->stmt; sp != NULL; sp = sp->next)
- X {
- X fprintf(out, " %s\n", sp->txt);
- X }
- X fprintf(out, "}\n\n");
- X }
- X }
- X n_events = 0;
- X if(list.event) {
- X fprintf(out, "static struct event etab[] = {\n");
- X for(ep = list.event; ep != NULL; n_events++, ep = NEXT_EVENT(ep))
- X {
- X fprintf(out, " %s, %s, ", ep->type, ep->code);
- X
- X if (ep->stmt) {
- X fprintf(out, "event_%s", ep->link.name);
- X }
- X else {
- X fprintf(out, "NULL");
- X }
- X fprintf(out, "%s\n", NEXT_EVENT(ep) ? "," : "");
- X }
- X fprintf(out, "};\n\n");
- X }
- X
- X fprintf(out, "struct module _module = { ");
- X if (list.picture) fprintf(out, "ptab, %d, ", n_pictures);
- X else fprintf(out, "NULL, 0, ");
- X if (list.event) fprintf(out, "etab, %d", n_events);
- X else fprintf(out, "NULL, 0");
- X fprintf(out, "};\n");
- X}
- END_OF_FILE
- if test 8094 -ne `wc -c <'src/output.c'`; then
- echo shar: \"'src/output.c'\" unpacked with wrong size!
- fi
- # end of 'src/output.c'
- fi
- if test -f 'src/token.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'src/token.c'\"
- else
- echo shar: Extracting \"'src/token.c'\" \(6503 characters\)
- sed "s/^X//" >'src/token.c' <<'END_OF_FILE'
- X/*******************************************************************************
- X*
- X* T O K E N . C
- X* -------------
- X*
- X* Description:
- X* Reads tokens from input file. Tokens are the C-stype tokens. It is
- X* allso possible to unget tokens for later recall by GetTok().
- X*
- X* Included functions:
- X* GetTok() - Get token
- X* GetTokNC() - Get token but skip comment
- X* UnGetTok() - Unget token
- X* OpenTok() - Open file to read tokens from
- X*
- X*
- X* Revision:
- X* Ver Date By Reason
- X* --- ---- -- ------
- X* 1.00 900619 Lars Berntzon Created
- X*
- X******************************************************************************/
- X
- X#include "config.h"
- X
- X#include <stdio.h>
- X#include <ctype.h>
- X#include <string.h>
- X#ifdef STDLIB_H
- X#include <stdlib.h>
- X#endif
- X#ifdef MALLOC_H
- X#include <malloc.h>
- X#endif
- X
- X#include "token.h"
- X
- X#define isnormal(c) (isalnum(c) || (c) == '_')
- X
- X
- X /* G l o b a l v a r i a b l e s */
- X
- Xint newline = 1; /* Number of newlines before this token */
- Xint line = 1; /* Current line number */
- XFILE *in; /* The input file pointer */
- X
- X /* L o c a l v a r i a b l e */
- X
- Xstatic struct saved { /* Queue of ungotten tokens */
- X struct saved *next;
- X char token[TOKENSIZE];
- X} *saved = NULL;
- Xtypedef void *(*state_t)(char *);
- X
- X/*******************************************************************************
- X *
- X * G E T O K
- X * ---------
- X *
- X * Description:
- X * Reads one token from input file (or from ungotten token).
- X *
- X * Output:
- X * str - String where data wil be put (if not NULL).
- X *
- X * Return:
- X * String containing token, NULL if end of file.
- X *
- X ******************************************************************************/
- Xchar *GetTok(char *str)
- X{
- X static char token[TOKENSIZE];
- X static int ch = ' ';
- X struct saved *p;
- X int pos;
- X enum { gt_init, gt_got_delim, gt_normal,
- X gt_searching_quote, gt_got_backslash } state = gt_init;
- X
- X if (p = saved) {
- X strncpy(token, saved->token, TOKENSIZE);
- X saved = p->next;
- X free(p);
- X pos = 1;
- X goto found;
- X }
- X
- X token[0] = 0;
- X
- X newline = 0;
- X
- X if (ch == EOF) return NULL;
- X
- X for(pos = 0; ch != EOF; ch = getc(in)) {
- X if (ch == '\n') {
- X line++;
- X newline++;
- X }
- X switch(state)
- X {
- X case gt_init:
- X if (isspace(ch)) {
- X break;
- X }
- X else if (ch == '"') {
- X token[pos++] = ch;
- X state = gt_searching_quote;
- X }
- X else if (!isnormal(ch)) {
- X token[pos++] = ch;
- X token[pos++] = 0;
- X ch = ' ';
- X goto found;
- X }
- X else if (isnormal(ch)) {
- X token[pos++] = ch;
- X state = gt_normal;
- X }
- X break;
- X
- X case gt_normal:
- X if (isnormal(ch)) {
- X token[pos++] = ch;
- X }
- X else {
- X token[pos++] = 0;
- X goto found;
- X }
- X break;
- X
- X case gt_searching_quote:
- X token[pos++] = ch;
- X if (ch == '\\') {
- X state = gt_got_backslash;
- X }
- X else {
- X if (ch == '"') {
- X token[pos++] = 0;
- X ch = ' ';
- X goto found;
- X }
- X }
- X break;
- X
- X case gt_got_backslash:
- X token[pos++] = ch;
- X state = gt_searching_quote;
- X break;
- X }
- X }
- X
- Xfound:
- X if (pos == 0) return NULL;
- X
- X if (str) return strcpy(str, token);
- X
- X return token;
- X}
- X
- X/*******************************************************************************
- X *
- X * G E T T O K N C
- X * ---------------
- X *
- X * Description:
- X * Reads one token from input file (or from ungotten token) and skips
- X * C-style comments.
- X *
- X * Output:
- X * str - String where data will be put (if not NULL).
- X *
- X * Return:
- X * String containing token, NULL if EOF.
- X *
- X ******************************************************************************/
- Xstatic void *nc_init(char *);
- Xstatic void *nc_found_leading_slash(char *);
- Xstatic void *nc_searching_star(char *);
- Xstatic void *nc_expect_slash(char *);
- X
- Xchar *GetTokNC(char *token)
- X{
- X char *ret;
- X state_t state = nc_init;
- X
- X while((ret = GetTok(token)) != NULL &&
- X (state = (state_t) (*state)(ret)) != NULL) {
- X }
- X return ret;
- X}
- Xstatic void *nc_init(char *token)
- X{
- X if(strcmp(token, "/") != 0) return NULL;
- X return (void *)nc_found_leading_slash;
- X}
- Xstatic void *nc_found_leading_slash(char *token)
- X{
- X if(strcmp(token, "*") == 0) return (void *)nc_searching_star;
- X UnGetTok(token);
- X strcpy(token, "/");
- X return NULL;
- X}
- Xstatic void *nc_searching_star(char *token)
- X{
- X if(strcmp(token, "*") == 0) return (void *)nc_expect_slash;
- X return (void *)nc_searching_star;
- X}
- Xstatic void *nc_expect_slash(char *token)
- X{
- X if (strcmp(token, "/") == 0) return (void *)nc_init;
- X UnGetTok(token);
- X return (void *)nc_searching_star;
- X}
- X
- X/*******************************************************************************
- X *
- X * U N G E T T O K
- X * ---------------
- X *
- X * Description:
- X * Returns a token to be read later by GetTok().
- X *
- X * Input:
- X * str - String to be put back.
- X *
- X ******************************************************************************/
- Xvoid
- XUnGetTok(char *str)
- X{
- X struct saved *p = NULL;
- X
- X if ((p = (struct saved *)malloc(sizeof *p)) == NULL) {
- X fprintf(stderr, "Out of memory");
- X return;
- X }
- X
- X p->next = saved;
- X saved = p;
- X strncpy(saved->token, str, TOKENSIZE);
- X}
- X
- X/*******************************************************************************
- X *
- X * O P E N T O K
- X * -------------
- X *
- X * Description:
- X * Open a file to read tokens from.
- X *
- X * Input:
- X * name - Name of file.
- X *
- X * Return:
- X * Opened files pointer, of NULL if failure.
- X *
- X ******************************************************************************/
- XFILE *OpenTok(char *name)
- X{
- X if (in != NULL) {
- X fclose(in);
- X }
- X return in = fopen(name, "r");
- X}
- X
- X#ifdef STANDALONE
- X/*******************************************************************************
- X *
- X * M A I N
- X * -------
- X *
- X * Description:
- X * Main routine when compiled as standalone.
- X * This is only used for test.
- X *
- X ******************************************************************************/
- X
- Xmain()
- X{
- X char filename[100];
- X char *p;
- X
- X printf("Enter filename of 'stdin': ");
- X scanf("%s", filename);
- X if (strcmp(filename, "stdin") == 0) {
- X printf("Using stdin\n");
- X in = stdin;
- X }
- X else if (OpenTok(filename) == NULL) {
- X fprintf(stderr, "Can't open file '%s'\n", filename);
- X exit(1);
- X }
- X
- X while(p = GetTokNC(NULL)) printf("'%s'\n", p);
- X UnGetTok("ungotten 1");
- X while(p = GetTokNC(NULL)) printf("'%s'\n", p);
- X UnGetTok("ungotten 2");
- X UnGetTok("ungotten 3");
- X while(p = GetTokNC(NULL)) printf("'%s'\n", p);
- X printf("END\n");
- X}
- X#endif /* STANDALONE */
- END_OF_FILE
- if test 6503 -ne `wc -c <'src/token.c'`; then
- echo shar: \"'src/token.c'\" unpacked with wrong size!
- fi
- # end of 'src/token.c'
- fi
- echo shar: End of archive 2 \(of 3\).
- cp /dev/null ark2isdone
- MISSING=""
- for I in 1 2 3 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have unpacked all 3 archives.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-