Re: Looking for a code beautifier for Informix -4gl
Posted in 1996
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. -------------- BEGIN C CODE -------------------------- /*********************************************************** ******************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> /*********************************************************** ******************/ #define BUFSIZE 2048 #define RESLEFT 6 #define RESRIGHT 8 #define RESSPEC 3 #define RESJUMP 1 #define MAXWORD 20 #define NUMSPACES 3 #define MAXFILENAME 256 #define EXTFILEIN ".4gl" #define EXTFILEOUT ".iid" /*********************************************************** ******************/ char *jump_spaces(char *, int *); char *trata_linha(char *, int *, int); char *put_spaces(char *, int); char *put_spaces_pos(char *, int, int); char *get_next_word(char *, char *); char *first_word(char *, char *, int *, int, int *, int *, int *); char *next_word(char *, char *, int *, int, int *, int *, int *); char *jump_word(char *); char *make_spaces( char *, int); /*********************************************************** ******************/ char res_right[RESRIGHT][MAXWORD]={ "then\\0","while\\0","function\\0", "main\\0","case\\0","input\\0","report\\0","for\\0" }; char res_left[RESLEFT][MAXWORD]={ "end\\0","endfunction\\0","endwhile\\0","endfor\\0", "endif\\0","endcase\\0" }; char res_spec[RESSPEC][MAXWORD] ={ "else\\0","when\\0","otherwise\\0" }; char res_jump[RESJUMP][MAXWORD] ={ "exit\\0" }; FILE *fp_input,*fp_copy,*fp_aux; /*********************************************************** ******************/ main(argc,argv) int argc; char *argv[]; { char buffer[BUFSIZE],*buf,frase[BUFSIZE]; int fix_spaces = NUMSPACES, n_keys = 0, js, len, n; char out_name[MAXFILENAME],in_name[MAXFILENAME],wcmd[200]; if (!teste_args(argc,argv,&fix_spaces)) return(1); get_file_names(argv[1],in_name,out_name); strcpy(wcmd,"ksh /users/wetc/sediid "); strcat(wcmd,in_name); len=strlen(wcmd); wcmd[len-4]='\\0'; n=system("ll"); return 0; /* fp_input=fopen(in_name,"r"); fp_copy=fopen(out_name,"w"); fp_aux=fopen("aux","w"); while( fgets(buffer,BUFSIZE,fp_input)!=NULL ) { buf=buffer; fputs(buf,fp_copy); if (dont_touch(jump_spaces(buf,&js))) { fputs(buf,fp_aux); continue; } if (is_comment(jump_spaces(buf,&js))) { buf=jump_spaces(buf,&js); buf=put_spaces(buf,n_keys*fix_spaces); fputs(buf,fp_aux); continue; } buf=trata_linha(buf,&n_keys,fix_spaces); fputs(buf,fp_aux); } fclose(fp_input); fclose(fp_copy); fclose(fp_aux); fp_input=fopen(in_name,"w"); fp_aux=fopen("aux","r"); fseek(fp_aux,0,SEEK_SET); while( fgets(buf,BUFSIZE,fp_aux)!=NULL ) { fputs(buf,fp_input); } fclose(fp_input); fclose(fp_aux); strcpy(wcmd,"/users/wetc/sediid1 "); strcat(wcmd,in_name); system(wcmd); unlink("aux"); */ } /*********************************************************** ******************/ char *trata_linha(str, keys, f_spaces) char *str; int *keys, f_spaces; { int num_word = 0, cont, jumps, partiu, estado; char aux[BUFSIZE], aux1[BUFSIZE], word[BUFSIZE], *frase, *out=aux1; str=jump_spaces(str,&jumps); frase=strcpy(aux,str); cont=1; *str='\\0'; jumps=0; partiu=0; estado=0; while (!is_return(frase) && cont==1) { *out='\\0'; if (!partiu) { out=make_spaces(out,jumps); strcat(str,out); } frase=get_next_word(frase,word); num_word++; if (estado) { estado=examina_estado(word,estado); strcat(str,word); frase=jump_spaces(frase,&jumps); continue; } if (!(num_word - 1)) { out=first_word(frase,word,keys,f_spaces,&cont,&partiu,&estad o); strcat(str,out); } else { out=next_word(frase,word,keys,f_spaces,&cont,&partiu,&estado ); strcat(str,out); } frase=jump_spaces(frase,&jumps); } if (!cont) { out=make_spaces(out,jumps); strcat(str,out); strcat(str,frase); } else strcat(str,"\\n"); return(str); } /*********************************************************** ******************/ char *first_word( str, word, keys, f_spaces, cont, partiu, estado) char *str,*word; int *keys,f_spaces,*cont,*partiu,*estado; { char aux3[BUFSIZE],aux4[BUFSIZE],*aux1,*aux2; int js; aux1=strcpy(aux3,word); aux2=strcpy(aux4,str); *estado=0; *partiu=0; if (is_word_jump(word)) { aux1=put_spaces(aux1,*keys*f_spaces); *cont=0; return(aux1); } if (is_word_right(word)) { aux1=put_spaces(aux1,*keys*f_spaces); (*keys)++; if (!strcmp(word,"function")) (*keys)=1; *cont=1; if (!strcmp(word,"then") && !is_return(jump_spaces(aux2,&js))) { *partiu=1; *aux2='\\0'; aux2=put_spaces(aux2,*keys*f_spaces); aux1=strcat(aux1,"\\n"); aux1=strcat(aux1,aux2); } return(aux1); } if (is_word_left(word)) { (*keys)--; aux1=put_spaces(aux1,*keys*f_spaces); *cont=0; return(aux1); } if (is_word_spec(word)) { if (!is_return(jump_spaces(aux2,&js))) { *partiu=1; aux1=put_spaces(aux1,(*keys-1)*f_spaces); *aux2='\\0'; aux2=put_spaces(aux2,*keys*f_spaces); aux1=strcat(aux1,"\\n"); aux1=strcat(aux1,aux2); return(aux1); } else { aux1=put_spaces(aux1,(*keys-1)*f_spaces);