home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 1B
/
DATAFILE_PDCD1B.iso
/
_languages
/
languages
/
cweb
/
c
/
ctangle
< prev
Wrap
Text File
|
1994-02-20
|
22KB
|
851 lines
#define banner "This is CTANGLE (Version 3.x)\n"
#define max_tokmem 150000L
#define max_textmem 2500
#define max_files 16
#define stack_size_max 50
#define variant text
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include "common.h"
#define max_bytes 50000L
#define max_names 3000
#define max_sections 10000
#define hash_size 353
#define buf_size 100
#define longest_name 400
#define long_buf_size (buf_size+longest_name)
#define local static
#define array_size(a)((int)(sizeof(a)/sizeof(a[0])))
#define false (boolean)0
#define true (boolean)1
#define ctangle 0
#define cweave 1
#define and_and 04
#define lt_lt 020
#define gt_gt 021
#define plus_plus 013
#define minus_minus 01
#define minus_gt 031
#define not_eq 032
#define lt_eq 034
#define gt_eq 035
#define eq_eq 036
#define or_or 037
#define find_char (loc<=limit||get_line())
#define including_header_file (saved_include_depth>0)
#define spotless 0
#define harmless_message 1
#define error_message 2
#define fatal_message 3
#define mark_harmless if(history==spotless)history=harmless_message;else
#define mark_error history=error_message
#define overflow(t)fatal("\n! Sorry, %s capacity exceeded",t)
#define confusion(s)fatal("\n! This can't happen: %s",s)
#define show_banner flags['b']
#define show_progress flags['p']
#define show_stats flags['s']
#define show_happiness flags['h']
#define C_plus_plus flags['+']
#define update_terminal fflush(stdout)
#define new_line putchar('\n')
#define term_write(string,leng)printf("%.*s",(int)(leng),string)
#define tok_begin(p)(p)->tok_start
#define tok_end(p)((p)+1)->tok_start
#define text_info_end (&text_info[max_textmem])
#define tok_mem_end (&tok_mem[max_tokmem])
#define macro_flag text_info_end
#define header_flag (text_info_end+1)
#define next_sec(m)((m)->text_link)
#define equiv equiv_or_xref
#define verb_quote 0x2
#define join 0x3
#define cur_state stack[0]
#define cur_byte cur_state.byte_field
#define cur_end cur_state.end_field
#define cur_repl cur_state.repl_field
#define cur_sec cur_state.sec_nr_field
#define stack_end (&stack[stack_size_max])
#define stack_empty (stack_ptr== &stack[0])
#define identifier 0x80
#define section_start 0x81
#define section_end 0x82
#define line_mark 0x83
#define C_printf(format,x)fprintf(C_file,format,x)
#define C_putc(c)putc(c,C_file)
#define no_space 0
#define num_or_id 1
#define operator 2
#define literal 3
#define comp_op(op) \
C_printf(out_state==operator?" %s":"%s",op); \
out_state=operator;break
#define compress(char2,code) \
if(*loc==char2)return++loc,code
#define app_char(c)if(id_loc<mod_text_end)*id_loc++=c;else
#define macro 0
#define section_body 1
#define store_byte(c) \
if(tok_ptr==tok_mem_end)overflow("token");else*tok_ptr++=c
#define report(k,c,m) \
printf("%lu %ss (out of %lu)\n",(unsigned long)(c),k,(unsigned long)(m))
/*2:*//*10:*/
#line 122 "common.inc"
boolean names_match(name_pointer,char*,int,int);
void init_id_name(name_pointer,int);
void init_module_name(name_pointer);/*:10*//*14:*/
#line 134 "w.ctangle"
typedef struct text
{eight_bits*tok_start;
struct text*text_link;
}text,*text_pointer;/*:14*//*26:*/
#line 396 "w.ctangle"
typedef struct
{eight_bits*byte_field;
eight_bits*end_field;
text_pointer repl_field;
sixteen_bits sec_nr_field;
}output_state,*stack_pointer;/*:26*//*41:*/
#line 791 "w.ctangle"
enum
{ignore=0x80,
id_code,constant,
verbatim,
at_sign_image,
join_code,
ord,
control_text,
format,
definition,
header,
begin_C,
module_name,
new_section
};/*:41*//*21:*/
#line 293 "w.ctangle"
void phase_two(void);
void output(text_pointer);
void C_newline(void);
void out_char(eight_bits);/*:21*//*66:*/
#line 1453 "w.ctangle"
boolean mark_line(void);/*:66*//*76:*/
#line 1621 "w.ctangle"
void phase_one(void);/*:76*//*15:*/
#line 155 "w.ctangle"
text text_info[max_textmem+1];
text_pointer text_ptr= &text_info[0];
eight_bits tok_mem[max_tokmem];
eight_bits*tok_ptr= &tok_mem[0];/*:15*//*17:*/
#line 192 "w.ctangle"
text_pointer text_root=NULL;
text_pointer*last_unnamed= &text_root;/*:17*//*23:*/
#line 334 "w.ctangle"
name_pointer output_file[max_files];
int output_file_count=0;/*:23*//*27:*/
#line 425 "w.ctangle"
output_state stack[stack_size_max];
stack_pointer stack_ptr;/*:27*//*30:*/
#line 488 "w.ctangle"
int cur_val;/*:30*//*35:*/
#line 652 "w.ctangle"
eight_bits out_state;
boolean protect;/*:35*//*42:*/
#line 811 "w.ctangle"
eight_bits ccode[UCHAR_MAX+1];/*:42*//*46:*/
#line 921 "w.ctangle"
name_pointer cur_name;/*:46*//*57:*/
#line 1188 "w.ctangle"
text_pointer cur_text;
eight_bits next_control;/*:57*/
#line 77 "w.ctangle"
int main(int ac,char* *av)
{argc=ac;argv=av;
program=ctangle;/*16:*/
#line 164 "w.ctangle"
tok_begin(text_ptr)=tok_ptr;/*:16*//*43:*/
#line 819 "w.ctangle"
{unsigned char c=0;
do ccode[c]=ignore;while(c++ !=UCHAR_MAX);
ccode['v']=ccode['V']='|';
ccode['=']=verbatim;
ccode['@']=at_sign_image;
ccode['&']=join_code;
ccode['\'']=ord;
ccode['^']=ccode['?']=ccode['.']=ccode[':']=ccode['#']=
ccode['t']=ccode['T']=control_text;
ccode['f']=ccode['F']=format;
ccode['d']=ccode['D']=definition;
ccode['h']=ccode['H']=header;
ccode['c']=ccode['C']=ccode['p']=ccode['P']=begin_C;
ccode['<']=ccode['(']=module_name;
ccode[' ']=ccode['\t']=ccode['\n']=ccode['\v']=ccode['\r']=ccode['\f']=
ccode['~']=ccode['*']=new_section;
}/*:43*/
#line 83 "w.ctangle"
common_init();
if(show_banner)print(banner);
phase_one();
phase_two();
wrap_up();
return 0;
}/*:2*//*18:*/
#line 207 "w.ctangle"
boolean names_match(name_pointer p,char*first,int l,int dummy)
{return length(p)==l&&strncmp(first,name_begin(p),l)==0;}
void init_module_name(name_pointer node)
{node->equiv=NULL;}
void init_id_name(name_pointer dummy,int ilk){}/*:18*//*22:*/
#line 305 "w.ctangle"
void phase_two(void)
{phase=2;
if(text_root==NULL&&output_file_count==0)
{print("\n! No program text was specified.");mark_harmless;}
else
{if(show_progress)
{print("\nWriting the output file%s"
,(text_root!=NULL)+output_file_count>1?"s":"");
if(text_root!=NULL)printf(" (%s):",C_file_name);
update_terminal;
}
if(text_root==NULL)C_file=NULL;
else
{open_output_file();cur_line=1;/*33:*/
#line 564 "w.ctangle"
{text_pointer repl,l;
protect=true;
for(repl= &text_info[0];repl<text_ptr;repl++)
if((l=repl->text_link)==macro_flag||l==header_flag)
{cur_byte=tok_begin(repl);cur_end=tok_end(repl);
C_printf("#%se ",l==macro_flag?"defin":"includ");
out_state=no_space;
while(cur_byte<cur_end)
{sixteen_bits a= *cur_byte++;
if(a<0x80)out_char(a);
else if(a>=0xF8)
if(a==0xF8)out_char(*cur_byte++);
else confusion("#line within macro");
else
{a=((a-0x80)<<8)+ *cur_byte++;
if(a<0x2800){cur_val=a;out_char(identifier);}
else confusion("module name within macro");
}
}
C_newline();
}
protect=false;
}/*:33*/
#line 321 "w.ctangle"
output(text_root);
}/*24:*/
#line 21 "ch.ctangle"
{
int i;
char output_file_name[longest_name+1];
for(i=0;i<output_file_count;i++)
{
name_pointer name=output_file[i];
char*dotpos;
strncpy(output_file_name,name_begin(name),length(name));
output_file_name[length(name)]='\0';
dotpos=strchr(output_file_name,'.');
if((dotpos!=NULL)&&((tolower(dotpos[1])=='c')||
(tolower(dotpos[1])=='h'))&&(dotpos[2]=='\0')){
char tmp[longest_name+1];
*dotpos='\0';
sprintf(tmp,"%c.%s",dotpos[1],output_file_name);
strcpy(output_file_name,tmp);
}
if(C_file!=NULL)fclose(C_file);
C_file=fopen(output_file_name,"w");
if(C_file==NULL)
fatal("! Cannot open \"%s\" as output file",output_file_name);
if(show_progress)print("\n(%s):",output_file_name);
cur_line=1;
output(name->equiv);
}
}/*:24*/
#line 324 "w.ctangle"
print_progress("\nDone.\n");
}
}/*:22*//*28:*/
#line 436 "w.ctangle"
void push_level(name_pointer p)
{if(stack_ptr==stack_end)overflow("output stack");
*stack_ptr++=cur_state;
cur_repl=p->equiv;
cur_byte=tok_begin(cur_repl);cur_end=tok_end(cur_repl);
}/*:28*//*29:*/
#line 454 "w.ctangle"
void continue_or_pop_level(void)
{if(cur_repl->text_link!=NULL)
{cur_repl=next_sec(cur_repl);
cur_byte=tok_begin(cur_repl);cur_end=tok_end(cur_repl);
}
else if(--stack_ptr> &stack[0])cur_state= *stack_ptr;
}/*:29*//*31:*/
#line 503 "w.ctangle"
void output(text_pointer repl)
{stack_ptr= &stack[1];
cur_repl=repl;
cur_byte=tok_begin(cur_repl);