This program source file was posted to comp.databases.informix.

Apparently, the news posting program that was used wrapped some of the longer
source lines.  I rejoined those that appeared to have this problem until I got
a clean compile.  I also tried to clean up the indentation a little.  I then
made a shell archive of the source file and appended it to the contributor's
posted comments.

I have not run this program since some of the code is specific to a particular
Unix system.  If you don't know enough about C and Unix to tailor this program
for your own environment, then I recommend that you have someone who does look
at this code before you use it.  WH 08/21/1996.

------------------------------------------------------------------------------

Newsgroups: comp.databases.informix
Date: 11 Aug 1996 05:04:16 GMT
From: "Carlos Costa e Silva" <minimal@mail.telepac.pt>
Subject: Re: Looking for a code beautifier for Informix -4gl

Here goes a c program that indents 4gl code.  I haven't looked into it but the
author said that the code is pretty ugly.  No one now has the time to change
it, so for the time being it stays as it is.

It does not recognize all 4gl keywords but it's working in my company.  It's
been used for about a year now and there hasn't been any lost code due to to
the program.

The program indents file.4gl and makes a backup copy in file.iid.

I AND MY COMPANY MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE.  WE SHALL NOT BE LIABLE
FOR ANY DAMAGES SUFFERED BY ANY PERSON OR COMPANY AS A RESULT OF USING,
MODIFYING OR DISTRIBUTING THE SOFTWARE.

Sorry about the legalese, but it's cut and pasted from post by a heavy company.

Anyway here goes.  I hope it's useful and that any useful change return's to
the sender.

#!/bin/sh
#
# This is a shell archive.  To extract its contents,
# execute this file with /bin/sh to create the file(s):
#
# iid.c
#
# This shell archive created: Wed Aug 21 10:07:08 EDT 1996
#
echo "Extracting file iid.c"
sed -e 's/^X//' <<\SHAR_EOF > iid.c
X/*****************************************************************************/
X#include <stdio.h>
X#include <stdlib.h>
X#include <string.h>
X#include <errno.h>
X
X/*****************************************************************************/
X#define BUFSIZE     2048
X#define RESLEFT     6
X#define RESRIGHT    8
X#define RESSPEC     3
X#define RESJUMP     1
X#define MAXWORD     20
X#define NUMSPACES   3
X#define MAXFILENAME 256
X#define EXTFILEIN   ".4gl"
X#define EXTFILEOUT  ".iid"
X
X/*****************************************************************************/
Xchar *jump_spaces(char *, int *);
Xchar *trata_linha(char *, int *, int);
Xchar *put_spaces(char *, int);
Xchar *put_spaces_pos(char *, int, int);
Xchar *get_next_word(char *, char *);
Xchar *first_word(char *, char *, int *, int, int *, int *, int *);
Xchar *next_word(char *, char *, int *, int, int *, int *, int *);
Xchar *jump_word(char *);
Xchar *make_spaces( char *, int);
X
X/*****************************************************************************/
Xchar res_right[RESRIGHT][MAXWORD]={
X    "then\0","while\0","function\0",
X    "main\0","case\0","input\0","report\0","for\0"
X                  };
Xchar res_left[RESLEFT][MAXWORD]={
X    "end\0","endfunction\0","endwhile\0","endfor\0",
X    "endif\0","endcase\0"
X                };
Xchar res_spec[RESSPEC][MAXWORD] ={
X    "else\0","when\0","otherwise\0"
X                };
Xchar res_jump[RESJUMP][MAXWORD] ={
X    "exit\0"
X                };
XFILE *fp_input,*fp_copy,*fp_aux;
X
X/*****************************************************************************/
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X    char buffer[BUFSIZE],*buf,frase[BUFSIZE];
X    int fix_spaces = NUMSPACES, n_keys = 0, js, len, n;
X    char out_name[MAXFILENAME],in_name[MAXFILENAME],wcmd[200];
X
X        if (!teste_args(argc,argv,&fix_spaces)) return(1);
X        get_file_names(argv[1],in_name,out_name);
X        strcpy(wcmd,"ksh /users/wetc/sediid ");
X        strcat(wcmd,in_name);
X        len=strlen(wcmd);
X        wcmd[len-4]='\0';
X        n=system("ll");
X        return 0;
X/*
X        fp_input=fopen(in_name,"r");
X        fp_copy=fopen(out_name,"w");
X        fp_aux=fopen("aux","w");
X        while( fgets(buffer,BUFSIZE,fp_input)!=NULL )
X        {
X            buf=buffer;
X            fputs(buf,fp_copy);
X            if (dont_touch(jump_spaces(buf,&js)))
X        {
X            fputs(buf,fp_aux);
X            continue;
X        }
X        if (is_comment(jump_spaces(buf,&js)))
X        {
X            buf=jump_spaces(buf,&js);
X            buf=put_spaces(buf,n_keys*fix_spaces);
X            fputs(buf,fp_aux);
X            continue;
X        }
X        buf=trata_linha(buf,&n_keys,fix_spaces);
X        fputs(buf,fp_aux);
X    }
X    fclose(fp_input);
X    fclose(fp_copy);
X    fclose(fp_aux);
X    fp_input=fopen(in_name,"w");
X    fp_aux=fopen("aux","r");
X    fseek(fp_aux,0,SEEK_SET);
X        while( fgets(buf,BUFSIZE,fp_aux)!=NULL )
X        {
X            fputs(buf,fp_input);
X    }
X    fclose(fp_input);
X    fclose(fp_aux);
X    strcpy(wcmd,"/users/wetc/sediid1 ");
X    strcat(wcmd,in_name);
X    system(wcmd);
X    unlink("aux");
X*/
X}
X
X/*****************************************************************************/
Xchar *trata_linha(str, keys, f_spaces)
Xchar *str;
Xint *keys, f_spaces;
X{
X    int num_word = 0, cont, jumps, partiu, estado;
X    char aux[BUFSIZE], aux1[BUFSIZE], word[BUFSIZE], *frase, *out=aux1;
X
X    str=jump_spaces(str,&jumps);
X    frase=strcpy(aux,str);
X    cont=1;
X    *str='\0';
X    jumps=0;
X    partiu=0;
X    estado=0;
X    while (!is_return(frase) && cont==1)
X    {
X        *out='\0';
X        if (!partiu)
X        {
X            out=make_spaces(out,jumps);
X            strcat(str,out);
X        }
X        frase=get_next_word(frase,word);
X        num_word++;
X        if (estado)
X        {
X            estado=examina_estado(word,estado);
X            strcat(str,word);
X            frase=jump_spaces(frase,&jumps);
X            continue;
X        }
X        if (!(num_word - 1))
X        {
X
X            out=first_word(frase,word,keys,f_spaces,&cont,&partiu,&estado);
X            strcat(str,out);
X        }
X        else
X        {
X
X            out=next_word(frase,word,keys,f_spaces,&cont,&partiu,&estado);
X            strcat(str,out);
X        }
X        frase=jump_spaces(frase,&jumps);
X    }
X    if (!cont)
X    {
X        out=make_spaces(out,jumps);
X        strcat(str,out);
X        strcat(str,frase);
X    }
X    else
X        strcat(str,"\n");
X    return(str);
X}
X/*****************************************************************************/
Xchar *first_word( str, word, keys, f_spaces, cont, partiu,
Xestado)
Xchar *str,*word;
Xint *keys,f_spaces,*cont,*partiu,*estado;
X{
X    char aux3[BUFSIZE],aux4[BUFSIZE],*aux1,*aux2;
X    int js;
X
X    aux1=strcpy(aux3,word);
X    aux2=strcpy(aux4,str);
X    *estado=0;
X    *partiu=0;
X    if (is_word_jump(word))
X    {
X        aux1=put_spaces(aux1,*keys*f_spaces);
X        *cont=0;
X        return(aux1);
X    }
X    if (is_word_right(word))
X    {
X        aux1=put_spaces(aux1,*keys*f_spaces);
X        (*keys)++;
X        if (!strcmp(word,"function"))
X                    (*keys)=1;
X        *cont=1;
X        if (!strcmp(word,"then") && !is_return(jump_spaces(aux2,&js)))
X        {
X            *partiu=1;
X            *aux2='\0';
X            aux2=put_spaces(aux2,*keys*f_spaces);
X            aux1=strcat(aux1,"\n");
X            aux1=strcat(aux1,aux2);
X        }
X        return(aux1);
X    }
X    if (is_word_left(word))
X    {
X        (*keys)--;
X        aux1=put_spaces(aux1,*keys*f_spaces);
X        *cont=0;
X        return(aux1);
X    }
X    if (is_word_spec(word))
X    {
X            if (!is_return(jump_spaces(aux2,&js)))
X        {
X            *partiu=1;
X            aux1=put_spaces(aux1,(*keys-1)*f_spaces);
X            *aux2='\0';
X            aux2=put_spaces(aux2,*keys*f_spaces);
X            aux1=strcat(aux1,"\n");
X            aux1=strcat(aux1,aux2);
X            return(aux1);
X        }
X        else
X        {
X            aux1=put_spaces(aux1,(*keys-1)*f_spaces);
X            return(aux1);
X        }
X    }
X    aux1=put_spaces(aux1,*keys*f_spaces);
X    *cont=1;
X    *estado=examina_estado(word,*estado);
X    return(aux1);
X}
X/*****************************************************************************/
Xchar *next_word( str, word, keys, f_spaces, cont, partiu, estado)
Xchar *str,*word;
Xint *keys,f_spaces,*cont,*partiu,*estado;
X{
X    char aux3[BUFSIZE],aux4[BUFSIZE],*aux1,*aux2,ret[BUFSIZE]="\n\0";
X
X
X    char word_aux1[BUFSIZE],*word_aux=word_aux1;
X    int js;
X
X    aux1=strcpy(aux3,word);
X    aux2=strcpy(aux4,str);
X    *estado=0;
X    *partiu=0;
X    *cont=1;
X    if (is_word_jump(word))
X    {
X        *cont=0;
X        return(aux1);
X    }
X    if (is_word_right(word))
X    {
X        if (!strcmp(word,"input") || !strcmp(word,"report"))
X        {
X                return(aux1);
X        }
X        (*keys)++;
X        *cont=1;
X        if (!strcmp(word,"then"))
X            if (!is_return(jump_spaces(aux2,&js)))
X            {
X                *partiu=1;
X                *aux2='\0';
X                aux2=put_spaces(aux2,*keys*f_spaces);
X                aux1=strcat(aux1,"\n");
X                aux1=strcat(aux1,aux2);
X                return(aux1);
X            }
X            else
X                return(aux1);
X        aux1=put_spaces(aux1,((*keys)-1)*f_spaces);
X        aux1=strcat(ret,aux1);
X        return(aux1);
X    }
X    if (is_word_left(word))
X    {
X        (*keys)--;
X        aux1=put_spaces(aux1,*keys*f_spaces);
X        aux1=strcat(ret,aux1);
X        *cont=0;
X        return(aux1);
X    }
X    if (is_word_spec(word))
X    {
X            if (!is_return(jump_spaces(aux2,&js)))
X        {
X            *partiu=1;
X            aux1=put_spaces(aux1,(*keys-1)*f_spaces);
X            *aux2='\0';
X            aux2=put_spaces(aux2,*keys*f_spaces);
X            aux1=strcat(aux1,"\n");
X            aux1=strcat(aux1,aux2);
X            aux1=strcat(ret,aux1);
X            return(aux1);
X        }
X        else
X        {
X            aux1=put_spaces(aux1,(*keys-1)*f_spaces);
X            aux1=strcat(ret,aux1);
X            return(aux1);
X        }
X    }
X    *estado=examina_estado(word,*estado);
X    return(aux1);
X}
X/*****************************************************************************/
Xchar *get_next_word(str,word)
Xchar *str,*word;
X{
X    while (*str!='\n' && *str!=' ' && *str!='\t')
X        *word++=*str++;
X    *word='\0';
X    return(str);
X}
X/*****************************************************************************/
Xchar *jump_word(str)
Xchar *str;
X{
X    while (*str!='\n' && *str!=' ' && *str!='\t')
X        str++;
X    return(str);
X}
X/*****************************************************************************/
Xchar *jump_spaces(str,jumps)
Xchar *str;
Xint *jumps;
X{
X    *jumps=0;
X    while (*str==' ' || *str=='\t' )
X    {
X        if (*str==' ')
X            (*jumps)++;
X        else
X            (*jumps)=+8;
X        str++;
X    }
X    return(str);
X}
X/*****************************************************************************/
Xchar *make_spaces(str,spaces)
Xchar *str;
Xint spaces;
X{
X    char *aux;
X    int i;
X
X    aux=str;
X    for (i=1;i<=spaces;i++)
X        *str++=' ';
X    *str='\0';
X    return(aux);
X}
X/*****************************************************************************/
Xchar *put_spaces(str,spaces)
Xchar *str;
Xint spaces;
X{
X    char aux[BUFSIZE],*aux1,*aux2;
X    int i;
X
X    aux1=strcpy(aux,str);
X    aux2=str;
X    for (i=1;i<=spaces;i++)
X        *str++=' ';
X    *str='\0';
X    return(strcat(aux2,aux1));
X}
X/*****************************************************************************/
Xchar *put_spaces_pos(str,spaces,pos)
Xchar *str;
Xint spaces;
Xint pos;
X{
X    char aux1[BUFSIZE],*aux=aux1;
X    int i;
X
X    for (i=1;i<=pos;i++)
X        *aux++=*str++;
X    *aux++='\n';
X    for (i=1;i<=spaces;i++)
X        *aux++=' ';
X    *aux='\0';
X    return(strcat(aux1,str));
X}
X/*****************************************************************************/
Xint is_word_left(str)
Xchar *str;
X{
X    int i;
X
X    for (i=0;i<RESLEFT;i++)
X        if (!strcmp(str,res_left[i]))
X            return(1);
X    return(0);
X}
X/*****************************************************************************/
Xint is_word_right(str)
Xchar *str;
X{
X    int i;
X
X    for (i=0;i<RESRIGHT;i++)
X        if (!strcmp(str,res_right[i]))
X            return(1);
X    return(0);
X}
X/*****************************************************************************/
Xint is_word_jump(str)
Xchar *str;
X{
X    int i;
X
X    for (i=0;i<RESJUMP;i++)
X        if (!strcmp(str,res_jump[i]))
X            return(1);
X    return(0);
X}
X/*****************************************************************************/
Xint is_word_spec(str)
Xchar *str;
X{
X    int i;
X
X    for (i=0;i<RESSPEC;i++)
X        if (!strcmp(str,res_spec[i]))
X            return(1);
X    return(0);
X}
X/*****************************************************************************/
Xint teste_args(nargs,args,spaces)
Xint nargs;
Xchar *args[];
Xint *spaces;
X{
X    char fname[MAXFILENAME],*f_name=fname;
X
X    if ((nargs==1) || (nargs==2 && !strcmp(args[1],"-h")))
X    {
X        printf("iid - indents informix 4gl code\n");
X        printf("    Format : iid file(.4gl) [-i n]\n");
X        printf("              -i : n spaces for each indentacion\n");
X        printf("                   default 3\n");
X        printf("    Input   : file.4gl - file to indent\n");
X        printf("    Output  : file.4gl - indented file\n");
X        printf("              file.iid - backup copy\n");
X        return(0);
X    }
X    if (nargs==4)
X    {
X        if (!strcmp(args[2],"-i"))
X            *spaces=atoi(args[3]);
X    }
X    strcpy(f_name,args[1]);
X    if (fopen(args[1],"r")==(FILE *)NULL )
X    {
X        strcat(args[1],".4gl");
X        if (fopen(args[1],"r")==(FILE *)NULL )
X        {
X            perror(f_name);
X            return(0);
X        }
X    }
X    return(1);
X}
X/*****************************************************************************/
Xint dont_touch(ptr)
Xchar *ptr;
X{
X    return(*ptr=='\'' || *ptr =='\"' || *ptr ==',');
X}
X/*****************************************************************************/
Xint is_comment(ptr)
Xchar *ptr;
X{
X    return(*ptr=='#');
X}
X/*****************************************************************************/
Xint is_return(str)
Xchar *str;
X{
X    return(*str=='\n');
X}
X/*****************************************************************************/
Xint examina_estado(word,estado)
Xchar *word;
Xint estado;
X{
X    int aux,pos;
X
X    aux=estado;
X    pos=0;
X    while (*word!='\0')
X    {
X        pos++;
X        switch(*word)
X        {
X            case '\\':
X                if (*(word+1)!='\0')
X                    word++;
X                break;
X            case '\"':
X                if (aux!=2)
X                    aux=!aux;
X                break;
X            case '\'':
X                if (aux!=1)
X                    aux=(!aux)*2;
X                break;
X        }
X        word++;
X    }
X    return(aux);
X}
X/*****************************************************************************/
Xget_file_names(str,f1,f2)
Xchar *str,*f1,*f2;
X{
X    int find=0;
X
X    while( *str!='\0')
X    {
X        if (*str=='.')
X            find = 1;
X        *f1++=*str;
X        if (!find)
X            *f2++=*str;
X        str++;
X    }
X/*  strcat(f1,EXTFILEIN);*/
X    strcat(f2,EXTFILEOUT);
X}
X/*****************************************************************************/
SHAR_EOF
if [ `wc -c < iid.c` -ne    13270 ]
then
    echo "Lengths do not match -- Bad Copy of iid.c"
fi
echo "Done."
exit 0