home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume13 / opqcp / part01 next >
Text File  |  1990-06-24  |  46KB  |  1,293 lines

  1. Newsgroups: comp.sources.misc
  2. keywords: C, obfuscator, opacifier, shrouder
  3. organization: University of Utah CS Dept
  4. subject: v13i068: C obfuscator
  5. from: fish%kzin@cs.utah.edu (Russ Fish)
  6. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  7.  
  8. Posting-number: Volume 13, Issue 68
  9. Submitted-by: fish%kzin@cs.utah.edu (Russ Fish)
  10. Archive-name: opqcp/part01
  11.  
  12. A friend mentioned somewhere on the net that I had once written a C
  13. obfuscator, and I've gotten numerous requests for it.  So I'll post it.
  14. [This is actually a second attempt to post, since I've gotten even more
  15. numerous messages, telling me that it hadn't gotten there the first time...]
  16.  
  17. I make no claims for it, other than the fact that it once worked sufficently
  18. to package up a portable "opaque source" distribution of a couple hundred
  19. thousand lines of our geometric modeling system.  
  20.  
  21. The idea was to throw away all the information not absolutely needed by the
  22. compiler or linker, or squish it into unreadable id's.  To support separate
  23. compilation of large systems, it supports an optional dictionary of global
  24. identifiers and their translations, which I generated simply by doing `nm' on
  25. the .o files and .a libraries, and bashed into the dictionary format using
  26. the usual morass of `grep', `sort', `sed', `awk', etc.
  27.  
  28. Everybody seems to agree that the results are pretty unreadable.  :-)
  29.  
  30. That was over five years ago, and I haven't used it since.  (We make binary
  31. distribution tapes for various plaforms now...)  Feel free to use the code in
  32. any way that seems useful.  (Pound nails with it, etc.  :-)
  33.  
  34. Files included below:
  35.     opqcp.c            "Opaque Copy".  The obfuscator.
  36.     opqcp.opq       The result of hitting opqcp.c with opqcp.
  37.  
  38.     makefile        
  39.  
  40.     misc.h            A few support files, chopped down to subsets.
  41.     list.h
  42.     symtab.h
  43.     new.c
  44.     symtab.c
  45.  
  46. -Russ Fish            fish@cs.utah.edu        (801) 581-5884
  47.  
  48.  
  49. ps. Here's the germ of a slight improvement: The C keywords and punctuation
  50. characters could be squished out of the obfuscated file as well, if cpp were
  51. used to substitute them back in at compile time.  Then the source files would
  52. all be COMPLETELY unreadable.  (But of course, the key .h file of
  53. substitutions would have to be sent along, so it really doesn't gain that
  54. much.)
  55.  
  56. Extract the rest of this message into a file, cd to an empty directory, and
  57. feed the file into a c-shell(....)
  58. [GAAAAAAACK! ! ! ! !  I repacked this as a Bourne shell shar.  ++bsa]
  59.  
  60. #--------------------------------CUT HERE-------------------------------------
  61. #! /bin/sh
  62. #
  63. # This is a shell archive.  Save this into a file, edit it
  64. # and delete all lines above this comment.  Then give this
  65. # file to sh by executing the command "sh file".  The files
  66. # will be extracted into the current directory owned by
  67. # you with default permissions.
  68. #
  69. # The files contained herein are:
  70. #
  71. # -rw-r--r--  1 allbery      3334 Jun 24 19:41 list.h
  72. # -rw-r--r--  1 allbery       184 Jun 24 19:41 makefile
  73. # -rw-r--r--  1 allbery      2171 Jun 24 19:41 misc.h
  74. # -rw-r--r--  1 allbery      1728 Jun 24 19:41 new.c
  75. # -rw-r--r--  1 allbery     17010 Jun 24 19:41 opqcp.c
  76. # -rw-r--r--  1 allbery      8944 Jun 24 19:41 opqcp.opq
  77. # -rw-r--r--  1 allbery      3411 Jun 24 19:41 symtab.c
  78. # -rw-r--r--  1 allbery      2547 Jun 24 19:41 symtab.h
  79. #
  80. echo 'x - list.h'
  81. if test -f list.h; then echo 'shar: not overwriting list.h'; else
  82. sed 's/^X//' << '________This_Is_The_END________' > list.h
  83. X/*
  84. X * list.h - Definitions for list datastructures.
  85. X *
  86. X * Author:      Russ Fish
  87. X *              Computer Science Dept.
  88. X *              University of Utah
  89. X * Date:        1 August 1980
  90. X */
  91. X
  92. X#ifndef    LISTLINKS                         /* only once */
  93. X
  94. X/*****************************************************************/
  95. X/* TAG( list LISTHDR TLISTHDR NULLPTR P N  T_P T_N ) */
  96. X/* TAG( LISTLINKS TLISTLINKS ) */
  97. Xtypedef                                 /* Generalized list header. */
  98. X    struct listhdr
  99. X    {
  100. X    int t_tag;            /* object type tag */
  101. X        struct listhdr *next,*prev;     /* Backward & forward pointers */
  102. X    }
  103. X    list;
  104. X
  105. X/* LISTLINKS = a type tag, followed by a LISTHDR */
  106. X#define LISTLINKS    int t_tag; struct tlist * next, * prev
  107. X#define    TLISTLINKS(type)    int t_tag; type * next, * prev
  108. X
  109. X/* Access for List pointer manipulations. (Previous and Next.) */
  110. X#define P(node) ((node)->prev)
  111. X#define N(node) ((node)->next)
  112. X/*   #define P(node) (((list *)(node))->prev)
  113. X *   #define N(node) (((list *)(node))->next)
  114. X */
  115. X
  116. X/* Versions of P and N with cast on result to set list subtype properly. */
  117. X#define T_P(type,node) ((type *)(node)->prev)
  118. X#define T_N(type,node) ((type *)(node)->next)
  119. X
  120. X/* *****************************************************************
  121. X * TAG( list_list LLIST NEW_LIST_LIST )
  122. X * 
  123. X * list of lists data structure.
  124. X */
  125. X
  126. Xtypedef struct list_list
  127. X{
  128. X        TLISTLINKS(struct list_list);
  129. X        list * l_list;
  130. X} list_list;
  131. X
  132. X#define    LLIST(type,ll)    (type *)ll->l_list    /* get the list pointed to */
  133. X
  134. X/*****************************************************************/
  135. X
  136. X/* TAG( ADD INSERT REMOVE ) */
  137. X/* ADD links new members into a list, given a pointer to the new member, and
  138. X * a pointer to the first (left) element of the list.
  139. X */
  140. X#define ADD(new,first) ( P(new) = NULL ,N(new) = (first),\
  141. X             (  ((first)!=NULL) ? (P(first)=(new), 0) :0 ),\
  142. X            first = (new) )
  143. X
  144. X/* INSERT links members into the middle of a list, given a pointer to the new
  145. X * member, and a pointer to the list element to insert after.
  146. X */
  147. X#define INSERT(new,after) ( P(new) = (after),N(new) = N(after),\
  148. X    ( (N(after)!=NULL) ? (P(N(after))=(new), 0) :0),\
  149. X    N(after) = (new) )
  150. X
  151. X/* REMOVE unlinks a list element from a list, given a pointer to the element.
  152. X */
  153. X#define REMOVE(elt) (  ( (P(elt)!=NULL) ? (N(P(elt))=N(elt), 0) :0 ),\
  154. X                ( (N(elt)!=NULL) ? (P(N(elt))=P(elt), 0) :0)  )
  155. X
  156. X
  157. X/****************************************************************
  158. X * TAG( TRACE FREE_LIST FIRSTP ENDP )
  159. X *
  160. X *    TRACE - Traces a linear list.
  161. X *      FREE_LIST - Walks a list, freeing the elements.
  162. X *    FIRSTP - Tests an element, TRUE if it is the first.
  163. X *    ENDP - Tests an element, TRUE if it is the end.
  164. X */
  165. X#define TRACE(t_var,ini)   for((t_var)=(ini);(t_var)!=NULL;(t_var)=N(t_var))
  166. X#define FREE_LIST(lst)  {while(N(lst)){lst=N(lst);free(P(lst));} free(lst);}
  167. X#define FIRSTP(ptr)   (P(ptr) == NULL)
  168. X#define ENDP(ptr)     (N(ptr) == NULL)
  169. X
  170. X/****************************************************************
  171. X * TAGS( DEL )
  172. X *      del   deletes an element from a list, updating the base 
  173. X *          pointer of the list if necesary.
  174. X */
  175. X#define DEL(elt,base) (  ( (P(elt)!=NULL) ? (N(P(elt)) = N(elt)) :0 ),\
  176. X                   ( (N(elt)!=NULL) ? (P(N(elt)) = P(elt)) :0) ,\
  177. X                          ( ((elt)==(base)) ? ((base)= N(base) ) :0)    )
  178. X
  179. X#endif    LISTLINKS
  180. ________This_Is_The_END________
  181. if test `wc -c < list.h` -ne 3334; then
  182.     echo 'shar: list.h was damaged during transit (should have been 3334 bytes)'
  183. fi
  184. fi        ; : end of overwriting check
  185. echo 'x - makefile'
  186. if test -f makefile; then echo 'shar: not overwriting makefile'; else
  187. sed 's/^X//' << '________This_Is_The_END________' > makefile
  188. X# Makefile for opqcp.
  189. X
  190. XCFLAGS = -g
  191. X
  192. XOFILES = opqcp.o symtab.o new.o
  193. Xopqcp: $(OFILES)
  194. X    cc -o opqcp $(CFLAGS) $(OFILES)
  195. X
  196. Xopqcp.o: misc.h symtab.h
  197. Xsymtab.o: misc.h symtab.h
  198. Xnew.o: misc.h
  199. ________This_Is_The_END________
  200. if test `wc -c < makefile` -ne 184; then
  201.     echo 'shar: makefile was damaged during transit (should have been 184 bytes)'
  202. fi
  203. fi        ; : end of overwriting check
  204. echo 'x - misc.h'
  205. if test -f misc.h; then echo 'shar: not overwriting misc.h'; else
  206. sed 's/^X//' << '________This_Is_The_END________' > misc.h
  207. X/*
  208. X * misc.h   - Just the subset of defs needed for opqcp.
  209. X *
  210. X * Author:      Russ Fish
  211. X *              Computer Science Dept.
  212. X *              University of Utah
  213. X * Date:        1 August 1980
  214. X */
  215. X
  216. X#ifndef MISCH               /* Only once. */
  217. X#define MISCH
  218. X
  219. X#include <stdio.h>           /* Defines NULL. */
  220. X
  221. X/*****************************************************************
  222. X * TAGS( string boolean fn byte address )
  223. X *
  224. X *     Commonly used types.
  225. X *
  226. X */
  227. Xtypedef char * string;             /* Character Strings. */
  228. Xtypedef int boolean;               /* Logical vars or values. */
  229. X
  230. Xtypedef char byte;                 /* Byte is the unit of "sizeof". */
  231. X
  232. Xtypedef char * address;
  233. X
  234. X/*****************************************************************
  235. X * TAGS( TRUE FALSE )
  236. X */
  237. X
  238. X#define TRUE  1                         /* Logical constants. */
  239. X#define FALSE 0
  240. X
  241. X/*****************************************************************
  242. X * TAGS( NEW NEW_1 NEW_N new )
  243. X *
  244. X *     NEW(type,size)
  245. X * Generalized allocation function, given an object type and size (in
  246. X * bytes) to be allocated.
  247. X * 
  248. X * Variants:
  249. X *     NEW_1(type)   - Allocate a single object of the given type.
  250. X *     NEW_N(type,n) - Allocate an array of N objects of the given type.
  251. X */
  252. Xextern address
  253. Xnew();              /* In-core allocator. */
  254. X/* ( size )
  255. X * int size;
  256. X */
  257. X#define NEW(type,size) (type *)new(size)        /* Macro with type cast. */
  258. X#define NEW_1(type) (type *)new(sizeof(type))    /* Single arg, fixed size. */
  259. X#define NEW_N(type,n) (type *)new((n)*sizeof(type))  /* Array of base type. */
  260. X
  261. X/*****************************************************************
  262. X * TAGS( FREE free dispose )
  263. X * 
  264. X * Macros to cast the argument to dispose().
  265. X */
  266. X#define FREE( ptr )    dispose( (address)ptr )
  267. X#define free( ptr )    dispose( (address)ptr )
  268. X
  269. X/*****************************************************************
  270. X * TAG( SIZE SIZE_N msize )
  271. X *
  272. X * SIZE returns the size (in bytes) of an allocated object.
  273. X *
  274. X * SIZE_N returns the number of objects of TYPE in ARRAY.
  275. X */
  276. Xextern long
  277. Xmsize();
  278. X/* ( obj )
  279. X * address obj;
  280. X */
  281. X#define SIZE(obj) msize( (address)obj )
  282. X#define SIZE_N(type, array) (msize( (address)array ) / sizeof (type))
  283. X
  284. X#endif
  285. ________This_Is_The_END________
  286. if test `wc -c < misc.h` -ne 2171; then
  287.     echo 'shar: misc.h was damaged during transit (should have been 2171 bytes)'
  288. fi
  289. fi        ; : end of overwriting check
  290. echo 'x - new.c'
  291. if test -f new.c; then echo 'shar: not overwriting new.c'; else
  292. sed 's/^X//' << '________This_Is_The_END________' > new.c
  293. X/*
  294. X * new.c - Generalized allocation function.
  295. X *
  296. X * Author:      Russ Fish
  297. X *              Computer Science Dept.
  298. X *              University of Utah
  299. X * Date:        1 August 1980
  300. X */
  301. X#include "misc.h"
  302. X
  303. Xtypedef union
  304. X{
  305. X    long mem_size;           /* Someplace to stash the actual block size. */
  306. X
  307. X    /* Avoid messing up the double alignment of the block we are wrapping. */
  308. X    double dummy;
  309. X} mem_hdr;
  310. Xextern char * malloc();            /* Use the Std I/O Mem Alloc. */
  311. X
  312. X/*****************************************************************
  313. X * TAG( new )
  314. X * 
  315. X *     Allocate a block of storage.
  316. X * 
  317. X */
  318. Xaddress 
  319. Xnew(size)
  320. Xint size;
  321. X{
  322. X    mem_hdr * retval;
  323. X
  324. X    if ( (retval = (mem_hdr *)
  325. X      malloc( (unsigned) (size + sizeof(mem_hdr)) )) <= 0 )
  326. X    {
  327. X        fprintf( stderr, "\nNo Space in heap! %d bytes requested.\n", size);
  328. X        exit( -1 );
  329. X    }
  330. X
  331. X    /* Fill in the specified block size in a header word.  The returned
  332. X     * pointer points just AFTER the mem_hdr.  Only "new", "dispose",
  333. X     * "msize", and "expand" need to know about the mem_hdr.
  334. X     */
  335. X    retval->mem_size = (long) size;
  336. X
  337. X    return (address)( retval + 1 );
  338. X}
  339. X
  340. X/*****************************************************************
  341. X * TAG( msize )
  342. X * 
  343. X *     Report the size of a block of storage.
  344. X */
  345. Xlong
  346. Xmsize( obj )
  347. Xaddress obj;
  348. X{
  349. X    return ( (mem_hdr *)obj - 1 )->mem_size;
  350. X}
  351. X
  352. X/*****************************************************************
  353. X * TAG( dispose )
  354. X * 
  355. X *     Free a block of storage.
  356. X */
  357. Xdispose( obj )
  358. Xaddress obj;
  359. X{
  360. X    /* Get the "free" compatibility macro out of the way.  This is the
  361. X     * only place in the system where the real "free" function is visible.
  362. X     */
  363. X#   undef free
  364. X    if ( obj )
  365. X    free( (char *) ((mem_hdr *)obj - 1) );
  366. X}
  367. ________This_Is_The_END________
  368. if test `wc -c < new.c` -ne 1728; then
  369.     echo 'shar: new.c was damaged during transit (should have been 1728 bytes)'
  370. fi
  371. fi        ; : end of overwriting check
  372. echo 'x - opqcp.c'
  373. if test -f opqcp.c; then echo 'shar: not overwriting opqcp.c'; else
  374. sed 's/^X//' << '________This_Is_The_END________' > opqcp.c
  375. X/* opqcp - Makes opaque copies of a group of sources to support a flavor
  376. X * of distribution with the security of an object-only version but the ability
  377. X * to recompile on a variety of machines like a source distribution.
  378. X * 
  379. X * The idea is that there is a lot of information in good sources which is
  380. X * not needed by the compiler or linker, but which conveys the meaning of the
  381. X * program to a programmer.  We try to write the most understandable programs
  382. X * possible, opqcp aspires to translate them into the least understandable.
  383. X * 
  384. X *   Comments, #includes, #define'ed constants and macros - stripped or 
  385. X *     expanded by pouring the source through the C preprocessor.
  386. X * 
  387. X *   Global symbols - translated into unreadable equivalents or preserved
  388. X *     if needed to link to the program or function.  A global dictionary
  389. X *     must have previously been derived from the .o file symbol tables.
  390. X *     (All C reserved keywords are preserved automatically.)
  391. X * 
  392. X *   Local identifiers - Translated into unreadable equivalents within
  393. X *     a single source file.  This applies to everything not in the global
  394. X *     table (variables, typedefs, struct fields, etc.)
  395. X * 
  396. X *   Indentation/Whitespace - The tokens are packed on screen-width lines with
  397. X *     all whitespace removed.
  398. X * 
  399. X * Arguments (dash args precede file and destdir args.):
  400. X *   -d dictfile - (Optional, may be repeated.) Contains a wordlist that is
  401. X *     entered into a symbol table of global names.  Lines with just a name
  402. X *      mark that identifier as a clear global to preserve in the output.  
  403. X *    (All C reserved words are automatically preserved in this way.)
  404. X *     Lines with a name and its translation separated by a blank character
  405. X *     denote an opaque global which is translated the same everywhere.
  406. X *     All other symbols will be locally translated and may be different in
  407. X *     each source file.
  408. X * 
  409. X *   -Idir - (Optional, may be repeated.) Include directory arguments passed
  410. X *     on to the C preprocessor.
  411. X * 
  412. X *   -f - Filter mode, just opacifies stdin to stdout.  File and
  413. X *     directory arguments are ignored and the input must already have
  414. X *     been run through "cc -E" if preprocessing is desired.
  415. X * 
  416. X *   -t - Token trace (for testing).  Forces filter mode, prints token
  417. X *    types and values.
  418. X * 
  419. X *   filenames.c - (Required except in filter mode, may be repeated.) 
  420. X *     Source files to obfuscate.
  421. X * 
  422. X *   destdir - (One required as the last argument except in filter mode.)
  423. X *      Where to put the opaque copies of the source files.
  424. X *     The copies have the same names as the originals, but opqcp
  425. X *     refuses to overwrite a file with itself.
  426. X */
  427. X
  428. X#include "misc.h"
  429. X#include "symtab.h"
  430. X#include "sys/types.h"
  431. X#include "sys/stat.h"
  432. X#include "ctype.h"
  433. X
  434. Xstring c_keywords[] =        /* From K&R, appendix A, section 2.3. */
  435. X{
  436. X    "int", "char", "float", "double", "struct", "union", "enum", "long",
  437. X    "short", "unsigned", "auto", "extern", "register", "typedef", "static",
  438. X    "goto", "return", "sizeof", "break", "continue", "if", "else", "for",
  439. X    "do", "while", "switch", "case", "default", "entry", "fortran", "asm",
  440. X    "main",                /* Reserved for main program functions. */
  441. X    "void"                /* Not in K&R, added since then. */
  442. X};
  443. Xint n_keywords = sizeof c_keywords / sizeof( string );
  444. X
  445. Xmain( argc, argv )
  446. Xint argc;
  447. Xstring argv[];
  448. X{
  449. X    boolean filter_mode = FALSE, token_trace = FALSE;
  450. X    int i, j, k, l, arg_num;
  451. X    hash_table * globals, * locals;
  452. X    id * symbol;
  453. X    string *name_ptr, *arg_ptr, arg;
  454. X    static char I_args[BUFSIZ] = { '\0' };
  455. X    char buffer[BUFSIZ], buffer2[BUFSIZ];
  456. X    struct stat stat_buffer;
  457. X    FILE * dict_in, * input, * output, * popen();
  458. X    dev_t input_dev;
  459. X    ino_t input_inode;
  460. X    string directory, name, translation, index();
  461. X
  462. X    char chr, * chr_ptr , token[BUFSIZ], * token_end, string_type;
  463. X    int token_length, line_length;
  464. X    enum { NONE, SYMBOL, STRING, NUMBER, OTHER }
  465. X    token_type, prev_token_type;
  466. X    static string token_type_strings[] =
  467. X     { "none", "symbol", "string", "number", "other" };
  468. X    boolean whitespace;
  469. X    /* These characters will not be separated if they occur together. */
  470. X    string op_diphthong_chars = "=!<>&|+-*/%&^";
  471. X
  472. X    /* Some simple macros to stream characters through the token recognizer. */
  473. X#   define NEW_TOKEN ( token_end = token, *token_end = '\0' )
  474. X#   define NEXT ( OUT, IN )
  475. X#   define OUT ( *token_end++ = chr, *token_end = '\0' )
  476. X#   define IN ( chr = getc( input ) )
  477. X#   define NEW_LINE ( line_length = 0 )
  478. X
  479. X    int local_counter = 0;
  480. X#   define NLOCALS 5000
  481. X    static char local_names[NLOCALS][6];
  482. X
  483. X    /* Save a lot of hassle by putting the local name strings in a static
  484. X     * array where they can be re-used.  The var_value cells of the ids in
  485. X     * a hashed symbol table are second class citizens in that they are not
  486. X     * freed when the hash table is freed, although the var_name strings are.
  487. X     */
  488. X    for ( i=0; i < NLOCALS/1000; i++ ) /* Initialize the local name strings. */
  489. X    for ( j=0; j<=9; j++ )
  490. X        for ( k=0; k<=9; k++ )
  491. X        for ( l=0; l<=9; l++ )
  492. X        {
  493. X            chr_ptr = local_names[local_counter++];
  494. X            *chr_ptr++ = 'l';    /* Leading letter. */
  495. X            if ( i ) *chr_ptr++ = '0' + i;    /* Thousands. */
  496. X            if ( i || j ) *chr_ptr++ = '0' + j;    /* Hundreds. */
  497. X            if ( i || j || k ) *chr_ptr++ = '0' + k;    /* Tens. */
  498. X            *chr_ptr++ = '0' + l;    /* Ones. */
  499. X            *chr_ptr = '\0';
  500. X        }
  501. X    
  502. X    /* Start out the global dictionary with the C keywords. */
  503. X    globals = new_hash_table( 1000 );
  504. X    for ( i=0, name_ptr = c_keywords; i < n_keywords; i++, name_ptr++ )
  505. X    new_symbol( *name_ptr, globals );
  506. X
  507. X    /* Process dash arguments. */
  508. X    for ( arg_num=1, arg_ptr = &argv[1], arg = *arg_ptr;
  509. X      arg_num < argc;
  510. X      arg_num++, arg_ptr++, arg = *arg_ptr )
  511. X    {
  512. X    if ( arg[0] != '-' )
  513. X        break;    /* Only do dash args here. */
  514. X
  515. X    /* Process -d (global dictionary) args. */
  516. X    if ( strcmp( arg, "-d" ) == 0 )
  517. X    {
  518. X        if ( ++arg_num >= argc-1 ) break;    /* Ignore -d at the end. */
  519. X        arg = *++arg_ptr;
  520. X
  521. X        if ( (dict_in = fopen( arg, "r" )) == NULL )
  522. X        {
  523. X        sprintf( buffer, "opqcp: Can't open dictionary %s\n", arg );
  524. X        perror( buffer );
  525. X        exit( 1 );
  526. X        }
  527. X
  528. X        while( fgets( buffer, BUFSIZ, dict_in ) != NULL )
  529. X        {
  530. X        /* Trash the newline fgets puts in the buffer. */
  531. X        buffer[ strlen(buffer)-1 ] = '\0';
  532. X         
  533. X        /* Optional translation is separated from name by a blank. */
  534. X        if ( (translation = index( buffer, ' ' )) != NULL )
  535. X            *translation++ = '\0';    /* Terminate the name string. */
  536. X
  537. X        /* Link in a copy of the name string. */
  538. X        name = NEW( char, strlen(buffer)+1 );
  539. X        strcpy( name, buffer );
  540. X        symbol = new_symbol( name, globals );
  541. X
  542. X        /* Translation will be NULL if symbol is to be left alone. */
  543. X        if ( translation != NULL )
  544. X        {
  545. X            /* Link in a copy of the translation string. */
  546. X            symbol->var_value = NEW( char, strlen(translation)+1 );
  547. X            strcpy( symbol->var_value, translation );
  548. X        }
  549. X        }
  550. X        fclose( dict_in );
  551. X    }
  552. X    /* Process -Idir (cpp include directory) argument(s). */
  553. X    else if ( strncmp( arg, "-I", 2 ) == 0 )
  554. X    {
  555. X        strcat( I_args, " " );
  556. X        strcat( I_args, arg );
  557. X    }
  558. X    /* Process a -t (token trace) argument. */
  559. X    else if ( strcmp( arg, "-t" ) == 0 )
  560. X    {
  561. X        token_trace = TRUE;        /* For testing. */
  562. X        goto filter;        /* Forces filter mode. */
  563. X    }
  564. X    /* Process a -f (filter mode) argument. */
  565. X    else if ( strcmp( arg, "-f" ) == 0 )
  566. X    {
  567. X    filter:
  568. X        filter_mode = TRUE;        /* No-ops all of the file handling. */
  569. X        input = stdin;
  570. X        output = stdout;
  571. X    }
  572. X    else
  573. X        break;             /* Out if invalid dash arg. */
  574. X    }
  575. X
  576. X    /* Usage message if bad dash flag or no file and dir args . */
  577. X    if ( arg[0] == '-'  || !filter_mode && argc-arg_num < 2 )
  578. X    {
  579. X    fprintf( stderr, "%s\n%s\n",
  580. X        "usage: opqcp [-d dict]* [-Idir]* [srcfile]+ destdir",
  581. X        "  or   opqcp [-d dict]* -f" );
  582. X    exit( 2 );
  583. X    }
  584. X
  585. X    /* Check that the last argument is a directory. */
  586. X    if ( !filter_mode )
  587. X    {
  588. X    if ( stat( directory = argv[argc-1], &stat_buffer ) == -1 )
  589. X    {
  590. X        sprintf( buffer, "opqcp: Can't stat directory %s\n", directory );
  591. X        perror( buffer );
  592. X        exit( 1 );
  593. X    }
  594. X    if ( ! (stat_buffer.st_mode & S_IFDIR) )
  595. X    {
  596. X        fprintf( stderr, "opqcp: %s is not a directory.\n", directory );
  597. X        exit( 1 );
  598. X    }
  599. X    }
  600. X
  601. X    /* Loop through the file arguments. */
  602. X    for ( ; filter_mode || arg_num < argc-1; arg_num++, arg_ptr++ )
  603. X    {
  604. X    if ( filter_mode )
  605. X        arg = "(stdin)";
  606. X    else
  607. X    {
  608. X        arg = *arg_ptr;
  609. X
  610. X        /* Before opening the output file, check that it isn't the same
  611. X         * as the input file.  (This would result in clearing the file
  612. X         * before it was read!)
  613. X         */
  614. X        if ( stat( arg, &stat_buffer ) == -1 )
  615. X        {
  616. X        sprintf( buffer, "opqcp: Can't stat input file %s\n", arg );
  617. X        perror( buffer );
  618. X        continue;        /* Go do next file arg. */
  619. X        }
  620. X        /* Might as well check that it's a plain file while we're here. */
  621. X        if ( ! (stat_buffer.st_mode & S_IFREG) )
  622. X        {
  623. X        fprintf( stderr, "opqcp: %s is not a plain file.\n", arg );
  624. X        continue;        /* Go do next file arg. */
  625. X        }
  626. X        input_dev = stat_buffer.st_dev;
  627. X        input_inode = stat_buffer.st_ino;
  628. X
  629. X        sprintf( buffer, "%s/%s", directory, arg ); /* Output file path. */
  630. X        if ( stat( buffer, &stat_buffer ) == 0 )
  631. X        {           /* We can only stat an already existing output file. */
  632. X        /* Check for inode collision to guard against overwriting. */
  633. X        if ( stat_buffer.st_dev == input_dev &&
  634. X             stat_buffer.st_ino == input_inode )
  635. X        {
  636. X            fprintf( stderr, "opqcp: Can't copy file %s to itself.\n",
  637. X                 arg );
  638. X            continue;        /* Go on to next file arg. */
  639. X        }
  640. X        /* Check that it's a plain file while we're here. */
  641. X        if ( ! (stat_buffer.st_mode & S_IFREG) )
  642. X        {
  643. X            fprintf( stderr, "opqcp: %s is not a plain file.\n",
  644. X                 buffer );
  645. X            continue;        /* Go on to next file arg. */
  646. X        }
  647. X        }
  648. X
  649. X        /* Open the opaque source file on the destination directory. */
  650. X        if ( (output = fopen( buffer, "w" )) == NULL )
  651. X        {
  652. X        sprintf( buffer, "opqcp: Can't write file %s",arg );
  653. X        perror( buffer );
  654. X        continue;        /* Go on to the next file. */
  655. X        }
  656. X
  657. X        /* Copyright notice. */
  658. X        fputs( "/* Licensed material, Copyright ", output );
  659. X        fputs( "(c) 1985, University of Utah. */\n", output );
  660. X
  661. X        /* Read the source file through the C preprocessor. */
  662. X        sprintf( buffer2, "cc -E %s %s", I_args, arg );
  663. X        if ( (input = popen( buffer2, "r" )) == NULL )
  664. X        {
  665. X        sprintf( buffer2, "opqcp: Couldn't start cpp on %s.",arg );
  666. X        perror( buffer2 );
  667. X        fclose( output );    /* It was already opened. */
  668. X        unlink( buffer );    /* Wipe out the empty output file. */
  669. X        continue;        /* Go on to the next file. */
  670. X        }
  671. X    }
  672. X
  673. X    /* Local dictionary lasts only through a single source file. */
  674. X    locals = new_hash_table( 1000 );
  675. X    local_counter = 0;
  676. X
  677. X    /* Transfer tokens from the cpp stream to a packed output file. */
  678. X    chr = '\n'; /* In effect, the end of the line before the first line. */
  679. X    NEW_LINE;            /* Start first line of output. */
  680. X    prev_token_type = NONE;
  681. X    /* Back here at the beginning of each token. */
  682. X    while( chr != EOF )
  683. X    {
  684. X        NEW_TOKEN;        /* Set up to collect a token. */
  685. X        token_type = NONE;
  686. X        whitespace = FALSE;
  687. X
  688. X        /* Flush whitespace before a token. */
  689. X        while( isspace( chr ) )
  690. X        {
  691. X        whitespace = TRUE;
  692. X
  693. X        if ( chr != '\n' )
  694. X            IN;        /* Character after blank or tab. */
  695. X        else
  696. X        {
  697. X            IN;        /* First character on a line. */
  698. X            if ( chr == '#' )
  699. X            {
  700. X            /* Flush C preprocessor linenumber statements. */
  701. X            while ( chr != '\n' && chr != EOF )
  702. X                IN;        /* Go until the newline is found. */
  703. X            }
  704. X        }
  705. X        }
  706. X
  707. X        /* Identifiers, keywords, etc.  Underscore is a letter. */
  708. X        if ( isalpha( chr ) || chr == '_' )
  709. X        {
  710. X        token_type = SYMBOL;
  711. X
  712. X        /* Grab the alpha and then alphanumeric characters. */
  713. X        NEXT;
  714. X        while ( isalnum( chr ) || chr == '_' ) NEXT;
  715. X
  716. X        /* Look the symbol up in the global dictionary. */
  717. X        if ( (symbol = find_symbol( token, globals )) != NULL )
  718. X        {
  719. X            /* Substitute a global translation if there is one. */
  720. X            if ( symbol->var_value != NULL )
  721. X            {
  722. X            strcpy( token, symbol->var_value );
  723. X            }
  724. X        }
  725. X        else
  726. X        {
  727. X            /* Look the symbol up in the local dictionary. */
  728. X            if ( (symbol = find_symbol( token, locals )) == NULL )
  729. X            {
  730. X            /* New symbol, assign a local translation. */
  731. X            name = NEW( char, strlen(token)+1 );
  732. X            strcpy( name, token );
  733. X            symbol = new_symbol( name, locals );
  734. X            symbol->var_value = local_names[++local_counter];
  735. X            }
  736. X            /* Translate symbol to the local equivalent. */
  737. X            strcpy( token, symbol->var_value );
  738. X        }
  739. X        }
  740. X        /* Character constants and strings. */
  741. X        else if ( chr == '"' || chr == '\'' )
  742. X        {
  743. X        token_type = STRING;
  744. X        string_type = chr;    /* Remember which kind of quote. */
  745. X        NEXT;      /* Stash quote, get first char of string. */
  746. X        while ( chr != string_type )
  747. X        {
  748. X            if ( chr != '\\' )
  749. X            NEXT;    /* Stash non-backslash char, go ahead. */
  750. X            else
  751. X            /* Handle backslash escapes. */
  752. X            {
  753. X            NEXT;   /* Stash backslash, go to escaped char. */
  754. X            if ( string_type != '"' || chr != '\n' )
  755. X                NEXT;    /* Stash escaped char, go ahead. */
  756. X            else
  757. X            {
  758. X                /* Weird special case.  Strings can be
  759. X                 * continued onto the next line if they
  760. X                 * precede the newline with a backslash.
  761. X                 */
  762. X                NEXT;    /* Stash escaped newline, go ahead. */
  763. X                if ( strlen(token) + line_length > 77 )
  764. X                fputs( "\n", output ); /* Break before. */
  765. X                /* Put out the string with escaped newline. */
  766. X                fputs( token, output );
  767. X                /* Start a new line and a new token. */
  768. X                NEW_LINE; NEW_TOKEN;
  769. X            }
  770. X            }
  771. X        }
  772. X        NEXT;        /* Stash closing quote. */
  773. X        }
  774. X        /* Numbers.  (This can be a bit simplified over a real
  775. X         * lexical analyzer, since it only has to correctly
  776. X         * recognize all valid number forms, not detect subtle
  777. X         * syntax errors.)
  778. X         */
  779. X        else if ( isdigit( chr ) )
  780. X        {
  781. X        number:
  782. X        token_type = NUMBER;
  783. X
  784. X        /* Initial numeric part. */
  785. X        if ( chr == '0' )
  786. X        {  /* Could be an octal or hex constant, or just a "0". */
  787. X            NEXT;
  788. X            if ( chr == 'x' || chr == 'X' )
  789. X            {
  790. X            NEXT;    /* Get first char of hex constant. */
  791. X            while( isdigit( chr ) || chr >= 'a' && chr <= 'f'
  792. X                          || chr >= 'A' && chr <= 'F' )
  793. X                NEXT;    /* Read through hex constant. */
  794. X            }
  795. X            else        /* Octal integer constant. */
  796. X            while ( isdigit( chr ) ) NEXT;
  797. X        }
  798. X        else
  799. X            /* Decimal integer, or part of a floating pt number. */
  800. X            while ( isdigit( chr ) ) NEXT;
  801. X
  802. X        /* Optional integer "l" suffix, fraction, or exponent. */
  803. X        if ( chr == 'l' || chr == 'L' )
  804. X            NEXT;
  805. X        else
  806. X        {
  807. X            /* Optional fractional part on floats. */
  808. X            if ( chr == '.' )
  809. X            {
  810. X            NEXT;    /* Get first char of fraction. */
  811. X            while ( isdigit( chr ) ) NEXT;
  812. X            }
  813. X
  814. X            /* Optional exponent. */
  815. X            if ( chr == 'e' || chr == 'E' )
  816. X            {
  817. X            NEXT;    /* Get first char of exponent. */
  818. X            /* Optional sign on exponent. */
  819. X            if ( chr == '+' || chr == '-' ) NEXT;
  820. X            while ( isdigit( chr ) ) NEXT;    /* Exponent. */
  821. X            }
  822. X        }
  823. X        }
  824. X        /* Operators and single-character tokens for punctuation. */
  825. X        else if( chr != EOF )
  826. X        {
  827. X        token_type = OTHER;
  828. X
  829. X        /* Dot may be either a leading decimal point on a number,
  830. X         * or a structure access operator. We have a number if the
  831. X         * following character is a digit.
  832. X         */
  833. X        if ( chr == '.' )
  834. X        {
  835. X            NEXT;
  836. X            if ( isdigit( chr ) )
  837. X            goto number;    /* Yep, leading decimal point. */
  838. X        }
  839. X        else            /* Everything other than dots. */
  840. X        {
  841. X            /* Keep operator diphthongs contiguous. */
  842. X            if ( index( op_diphthong_chars, chr ) != NULL )
  843. X            while ( index( op_diphthong_chars, chr ) != NULL )
  844. X                NEXT;    /* Collect diphthong characters. */
  845. X            else
  846. X            NEXT;        /* Single-character token. */
  847. X        }
  848. X        }
  849. X
  850. X        if ( token_trace )
  851. X        fprintf( output, "type = %s, token = `",
  852. X            token_type_strings[ (int)token_type ] );
  853. X
  854. X        /* Got a token in the buffer, pack it onto an output line. */
  855. X        token_length = strlen( token );
  856. X        if ( token_length + line_length > 77 )  /* Time for line break? */
  857. X        {
  858. X        fputs( "\n", output );    /* Line break. */
  859. X        NEW_LINE;        /* Start next line. */
  860. X        }
  861. X        /* Need to preserve blanks between symbols or numbers, and
  862. X         * between operators next to = signs so they don't run together.
  863. X         */
  864. X        else if ( (prev_token_type==SYMBOL || prev_token_type==NUMBER) &&
  865. X               (token_type==SYMBOL || token_type==NUMBER)   ||
  866. X              token[0] == '=' && whitespace )    /* Space before =. */
  867. X        {
  868. X        fputs( " ", output );
  869. X        line_length++;
  870. X        }
  871. X
  872. X        /* Add token to the output stream. */
  873. X        fputs( token, output );
  874. X        line_length += token_length;
  875. X
  876. X        if ( token[token_length-1] == '=' && isspace( chr ) )
  877. X        {
  878. X        fputs( " ", output );    /* Space after = sign. */
  879. X        line_length++;
  880. X        }
  881. X
  882. X        if ( token_trace )
  883. X        fprintf( output, "'\n" );
  884. X
  885. X        prev_token_type = token_type;
  886. X    }
  887. X    /* EOF. */
  888. X    fputs( "\n", output );        /* End of line at end of file. */
  889. X
  890. X    if ( filter_mode )
  891. X        exit( 0 );            /* Done. */
  892. X    else
  893. X    {
  894. X        pclose( input );
  895. X        fclose( output );
  896. X        fr_hash_table( locals );
  897. X    }
  898. X    }                    /* Next file. */
  899. X}
  900. ________This_Is_The_END________
  901. if test `wc -c < opqcp.c` -ne 17010; then
  902.     echo 'shar: opqcp.c was damaged during transit (should have been 17010 bytes)'
  903. fi
  904. fi        ; : end of overwriting check
  905. echo 'x - opqcp.opq'
  906. if test -f opqcp.opq; then echo 'shar: not overwriting opqcp.opq'; else
  907. sed 's/^X//' << '________This_Is_The_END________' > opqcp.opq
  908. X/* Licensed material, Copyright (c) 1985, University of Utah. */
  909. Xextern struct l1{int l2;char*l3;char*l4;int l5;short l6;char l7;}l8[];struct
  910. Xl1*l9();struct l1*l10();struct l1*l11();struct l1*l12();long l13();char*l14()
  911. X;char*l15();char*l16();typedef char*l17;typedef int l18;typedef char l19;
  912. Xtypedef char*l20;extern l20 l21();extern long l22();typedef struct l23{int l24
  913. X;struct l23*l25,*l26;}l27;typedef struct l28{int l24;struct l28*l25,*l26;l27*
  914. Xl29;}l28;typedef struct l30 l31;struct l30{int l24;l31*l25,*l26;l17 l32;l20
  915. Xl33;};extern l31*l34();extern l31*l35();typedef struct{int l36;l31*l37[1];}
  916. Xl38;extern l38*l39();extern l31**l40();extern l41();extern l42();typedef
  917. Xunsigned char l43;typedef unsigned short l44;typedef unsigned int l45;typedef
  918. Xunsigned long l46;typedef unsigned short l47;typedef struct l48{short l49[1];
  919. X}*l50;typedef struct l51{int l52[15];}l51;typedef struct l53{long l52[2];}l54
  920. X;typedef long l55;typedef char*l56;typedef long*l57;typedef l46 l58;typedef
  921. Xlong l59;typedef long l60;typedef long l61;typedef short l62;typedef long l63
  922. X;typedef l44 l64;typedef l44 l65;typedef l46 l66;typedef long l67;typedef
  923. Xstruct l68{l67 l69[(((256)+(((sizeof(l67)*8))-1))/((sizeof(l67)*8)))];}l68;
  924. Xstruct l70{l62 l71;l58 l72;unsigned short l73;short l74;l64 l75;l65 l76;l62
  925. Xl77;l63 l78;l61 l79;int l80;l61 l81;int l82;l61 l83;int l84;long l85;long l86
  926. X;long l87[2];};extern char l88[];l17 l89[] = {"int","char","float","double",
  927. X"struct","union","enum","long","short","unsigned","auto","extern","register",
  928. X"typedef","static","goto","return","sizeof","break","continue","if","else",
  929. X"for","do","while","switch","case","default","entry","fortran","asm","main",
  930. X"void"};int l90 = sizeof l89/sizeof(l17);main(l91,l92)int l91;l17 l92[];{l18
  931. Xl93 = 0,l94 = 0;int l95,l96,l97,l98,l99;l38*l100,*l101;l31*l102;l17*l103,*
  932. Xl104,l105;static char l106[1024] = {'\0'};char l107[1024],l108[1024];struct
  933. Xl70 l109;struct l1*l110,*l111,*l112,*l12();l62 l113;l58 l114;l17 l115,l116,
  934. Xl117,l118();char l119,*l120,l121[1024],*l122,l123;int l124,l125;enum{l126,
  935. Xl127,l128,l129,l130}l131,l132;static l17 l133[] = {"none","symbol","string",
  936. X"number","other"};l18 l134;l17 l135 = "=!<>&|+-*/%&^";int l136 = 0;static char
  937. Xl137[5000][6];for(l95=0;l95<5000/1000;l95++)for(l96=0;l96<=9;l96++)for(l97=0;
  938. Xl97<=9;l97++)for(l98=0;l98<=9;l98++){l120 = l137[l136++];*l120++ = 'l';if(l95
  939. X)*l120++ = '0'+l95;if(l95||l96)*l120++ = '0'+l96;if(l95||l96||l97)*l120++ = 
  940. X'0'+l97;*l120++ = '0'+l98;*l120 = '\0';}l100 = l39(1000);for(l95=0,l103 = l89
  941. X;l95<l90;l95++,l103++)l34(*l103,l100);for(l99=1,l104 = &l92[1],l105 = *l104;
  942. Xl99<l91;l99++,l104++,l105 = *l104){if(l105[0]!= '-')break;if(l138(l105,"-d")
  943. X== 0){if(++l99>= l91-1)break;l105 = *++l104;if((l110 = l9(l105,"r")) == 0){
  944. Xl16(l107,"opqcp: Can't open dictionary %s\n",l105);l139(l107);l140(1);}while(
  945. Xl14(l107,1024,l110)!= 0){l107[l141(l107)-1] = '\0';if((l117 = l118(l107,' '))
  946. X!= 0)*l117++ = '\0';l116 = (char*)l21(l141(l107)+1);l142(l116,l107);l102 = 
  947. Xl34(l116,l100);if(l117!= 0){l102->l33 = (char*)l21(l141(l117)+1);l142(l102->
  948. Xl33,l117);}}l143(l110);}else if(l144(l105,"-I",2) == 0){l145(l106," ");l145(
  949. Xl106,l105);}else if(l138(l105,"-t") == 0){l94 = 1;goto l146;}else if(l138(
  950. Xl105,"-f") == 0){l146:l93 = 1;l111 = (&l8[0]);l112 = (&l8[1]);}else break;}if
  951. X(l105[0] == '-'||!l93&&l91-l99<2){l147((&l8[2]),"%s\n%s\n",
  952. X"usage: opqcp [-d dict]* [-Idir]* [srcfile]+ destdir",
  953. X"  or   opqcp [-d dict]* -f");l140(2);}if(!l93){if(l70(l115 = l92[l91-1],&
  954. Xl109) == -1){l16(l107,"opqcp: Can't stat directory %s\n",l115);l139(l107);
  955. Xl140(1);}if(!(l109.l73&0040000)){l147((&l8[2]),
  956. X"opqcp: %s is not a directory.\n",l115);l140(1);}}for(;l93||l99<l91-1;l99++,
  957. Xl104++){if(l93)l105 = "(stdin)";else{l105 = *l104;if(l70(l105,&l109) == -1){
  958. Xl16(l107,"opqcp: Can't stat input file %s\n",l105);l139(l107);continue;}if(!(
  959. Xl109.l73&0100000)){l147((&l8[2]),"opqcp: %s is not a plain file.\n",l105);
  960. Xcontinue;}l113 = l109.l71;l114 = l109.l72;l16(l107,"%s/%s",l115,l105);if(l70(
  961. Xl107,&l109) == 0){if(l109.l71 == l113&&l109.l72 == l114){l147((&l8[2]),
  962. X"opqcp: Can't copy file %s to itself.\n",l105);continue;}if(!(l109.l73&
  963. X0100000)){l147((&l8[2]),"opqcp: %s is not a plain file.\n",l107);continue;}}
  964. Xif((l112 = l9(l107,"w")) == 0){l16(l107,"opqcp: Can't write file %s",l105);
  965. Xl139(l107);continue;}l148("/* Licensed material, Copyright ",l112);l148(
  966. X"(c) 1985, University of Utah. */\n",l112);l16(l108,"cc -E %s %s",l106,l105);
  967. Xif((l111 = l12(l108,"r")) == 0){l16(l108,"opqcp: Couldn't start cpp on %s.",
  968. Xl105);l139(l108);l143(l112);l149(l107);continue;}}l101 = l39(1000);l136 = 0;
  969. Xl119 = '\n';(l125 = 0);l132 = l126;while(l119!= (-1)){(l122 = l121,*l122 = 
  970. X'\0');l131 = l126;l134 = 0;while(((l88+1)[l119]&010)){l134 = 1;if(l119!= '\n'
  971. X)(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111)));
  972. Xelse{(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111)
  973. X));if(l119 == '#'){while(l119!= '\n'&&l119!= (-1))(l119 = (--(l111)->l2>=0?(
  974. Xint)(*(unsigned char*)(l111)->l3++):l150(l111)));}}}if(((l88+1)[l119]&(01|02)
  975. X)||l119 == '_'){l131 = l127;((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)
  976. X->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));while(((l88+1)[
  977. Xl119]&(01|02|04))||l119 == '_')((*l122++ = l119,*l122 = '\0'),(l119 = (--(
  978. Xl111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));if((l102 = 
  979. Xl35(l121,l100))!= 0){if(l102->l33!= 0){l142(l121,l102->l33);}}else{if((l102 = 
  980. Xl35(l121,l101)) == 0){l116 = (char*)l21(l141(l121)+1);l142(l116,l121);l102 = 
  981. Xl34(l116,l101);l102->l33 = l137[++l136];}l142(l121,l102->l33);}}else if(l119
  982. X== '"'||l119 == '\''){l131 = l128;l123 = l119;((*l122++ = l119,*l122 = '\0'),
  983. X(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));
  984. Xwhile(l119!= l123){if(l119!= '\\')((*l122++ = l119,*l122 = '\0'),(l119 = (--(
  985. Xl111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));else{((*l122
  986. X++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111
  987. X)->l3++):l150(l111))));if(l123!= '"'||l119!= '\n')((*l122++ = l119,*l122 = 
  988. X'\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111
  989. X))));else{((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(
  990. Xunsigned char*)(l111)->l3++):l150(l111))));if(l141(l121)+l125>77)l148("\n",
  991. Xl112);l148(l121,l112);(l125 = 0);(l122 = l121,*l122 = '\0');}}}((*l122++ = 
  992. Xl119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3
  993. X++):l150(l111))));}else if(((l88+1)[l119]&04)){l151:l131 = l129;if(l119 == 
  994. X'0'){((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned
  995. Xchar*)(l111)->l3++):l150(l111))));if(l119 == 'x'||l119 == 'X'){((*l122++ = 
  996. Xl119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3
  997. X++):l150(l111))));while(((l88+1)[l119]&04)||l119>= 'a'&&l119<= 'f'||l119>= 
  998. X'A'&&l119<= 'F')((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)
  999. X(*(unsigned char*)(l111)->l3++):l150(l111))));}else while(((l88+1)[l119]&04))
  1000. X((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char
  1001. X*)(l111)->l3++):l150(l111))));}else while(((l88+1)[l119]&04))((*l122++ = l119
  1002. X,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):
  1003. Xl150(l111))));if(l119 == 'l'||l119 == 'L')((*l122++ = l119,*l122 = '\0'),(
  1004. Xl119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));
  1005. Xelse{if(l119 == '.'){((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?
  1006. X(int)(*(unsigned char*)(l111)->l3++):l150(l111))));while(((l88+1)[l119]&04))(
  1007. X(*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*
  1008. X)(l111)->l3++):l150(l111))));}if(l119 == 'e'||l119 == 'E'){((*l122++ = l119,*
  1009. Xl122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):
  1010. Xl150(l111))));if(l119 == '+'||l119 == '-')((*l122++ = l119,*l122 = '\0'),(
  1011. Xl119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));
  1012. Xwhile(((l88+1)[l119]&04))((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2
  1013. X>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));}}}else if(l119!= (-1)
  1014. X){l131 = l130;if(l119 == '.'){((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111
  1015. X)->l2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));if(((l88+1)[l119]
  1016. X&04))goto l151;}else{if(l118(l135,l119)!= 0)while(l118(l135,l119)!= 0)((*l122
  1017. X++ = l119,*l122 = '\0'),(l119 = (--(l111)->l2>=0?(int)(*(unsigned char*)(l111
  1018. X)->l3++):l150(l111))));else((*l122++ = l119,*l122 = '\0'),(l119 = (--(l111)->
  1019. Xl2>=0?(int)(*(unsigned char*)(l111)->l3++):l150(l111))));}}if(l94)l147(l112,
  1020. X"type = %s, token = `",l133[(int)l131]);l124 = l141(l121);if(l124+l125>77){
  1021. Xl148("\n",l112);(l125 = 0);}else if((l132==l127||l132==l129)&&(l131==l127||
  1022. Xl131==l129)||l121[0] == '='&&l134){l148(" ",l112);l125++;}l148(l121,l112);
  1023. Xl125+= l124;if(l121[l124-1] == '='&&((l88+1)[l119]&010)){l148(" ",l112);l125
  1024. X++;}if(l94)l147(l112,"'\n");l132 = l131;}l148("\n",l112);if(l93)l140(0);else{
  1025. Xl152(l111);l143(l112);l42(l101);}}}
  1026. ________This_Is_The_END________
  1027. if test `wc -c < opqcp.opq` -ne 8944; then
  1028.     echo 'shar: opqcp.opq was damaged during transit (should have been 8944 bytes)'
  1029. fi
  1030. fi        ; : end of overwriting check
  1031. echo 'x - symtab.c'
  1032. if test -f symtab.c; then echo 'shar: not overwriting symtab.c'; else
  1033. sed 's/^X//' << '________This_Is_The_END________' > symtab.c
  1034. X/* 
  1035. X * symtab.c - Symbol Table package routines.
  1036. X *          ( Descriptions in file symtab.h .)
  1037. X * 
  1038. X * Author:    Russ Fish
  1039. X *         Computer Science Dept.
  1040. X *         University of Utah
  1041. X * Date:    3 September 1981
  1042. X */
  1043. X
  1044. X#include "misc.h"
  1045. X#include "symtab.h"
  1046. X
  1047. X/*****************************************************************
  1048. X * TAG( new_symbol )
  1049. X */
  1050. Xid *                /* Constructor. */
  1051. Xnew_symbol( sym_name, table )
  1052. Xstring sym_name;
  1053. Xhash_table * table;
  1054. X{
  1055. X    id  * ret,  * * slot;
  1056. X
  1057. X    /* Allocate id node and link into symbol table at hashed location. */
  1058. X    ret = NEW_1( id );
  1059. X    slot = hash( sym_name, table );
  1060. X    ADD( ret, *slot );
  1061. X
  1062. X    ret->var_name = sym_name;    /* Link to name string from id. */
  1063. X    ret->var_value = NULL;    /* No value cell or fn ptr yet. */
  1064. X
  1065. X    return( ret );
  1066. X}
  1067. X
  1068. X/*****************************************************************
  1069. X * TAG( find_symbol )
  1070. X */
  1071. Xid *                /* Locator, NULL if not found. */
  1072. Xfind_symbol( sym_name, table )
  1073. Xstring sym_name;
  1074. Xhash_table * table;
  1075. X{
  1076. X    register id * sym;            /* Symbol list traversal ptr. */
  1077. X
  1078. X    /* Search list to which symbol hashes. */
  1079. X    TRACE( sym, *hash( sym_name, table ) )
  1080. X    {
  1081. X    /* Break out of search loop when matching name is found. */
  1082. X    if ( strcmp( sym_name, sym->var_name ) == 0 ) break;
  1083. X    }                /* sym is NULL if trace loop exits. */
  1084. X
  1085. X    return( sym );
  1086. X}
  1087. X
  1088. X/*****************************************************************
  1089. X *  TAG( new_hash_table )
  1090. X */
  1091. Xhash_table *            /* Constructor. */
  1092. Xnew_hash_table( n_entries )
  1093. Xint n_entries;
  1094. X{
  1095. X    hash_table * ret;
  1096. X    id ** ent;            /* Entries of table are bases of id lists. */
  1097. X    int i;
  1098. X
  1099. X    ret = NEW( hash_table, sizeof( hash_table ) + n_entries * sizeof( id * ) );
  1100. X
  1101. X    ret->hash_size = n_entries;        /* Remember the size, for hash comp. */
  1102. X
  1103. X    for ( i = 0, ent = & ret->hash_id[0];    /* Clear the entries. */
  1104. X      i < n_entries;
  1105. X      i++ )
  1106. X          *ent++ = NULL;    /* Empty list pointers. */
  1107. X
  1108. X    return( ret );
  1109. X}
  1110. X
  1111. X/*****************************************************************
  1112. X *  TAG( hash )
  1113. X */
  1114. Xid * *       /* Hashing algorithm, returns ptr to base of an id list in table. */
  1115. Xhash( sym_name, table )
  1116. Xstring sym_name;
  1117. Xhash_table * table;
  1118. X{
  1119. X    register char * c;
  1120. X    register int i, n, hash_val;
  1121. X
  1122. X    /* Hash is the total of the character codes, modulo number of entries. */
  1123. X    for ( i = 0, n = strlen( sym_name ), c = (char *)sym_name, hash_val = 0;
  1124. X      i < n;
  1125. X      i++ )
  1126. X          hash_val += *c++;
  1127. X
  1128. X    return( & table->hash_id[ hash_val % table->hash_size ] );
  1129. X}
  1130. X
  1131. X/*****************************************************************
  1132. X * TAG( ld_table )
  1133. X * 
  1134. X * Links a vector of already initialiazed id's into a hash table.
  1135. X * NULL var_name terminates the vector.
  1136. X */
  1137. Xld_table( id_vec, table )
  1138. Xid id_vec[];
  1139. Xhash_table * table;
  1140. X{
  1141. X    id * sym;            /* Vector traversal ptr. */
  1142. X    id  * * slot;
  1143. X
  1144. X    for ( sym = & id_vec[0]; sym->var_name != NULL; sym++ )
  1145. X    {
  1146. X    /* Link id node into symbol table at hashed location. */
  1147. X        slot = hash( sym->var_name, table );
  1148. X    ADD( sym, *slot );
  1149. X    }
  1150. X}
  1151. X
  1152. X/*****************************************************************
  1153. X * TAG( fr_hash_table )
  1154. X * 
  1155. X * Dispose of a hash table.
  1156. X */
  1157. Xfr_hash_table( table )
  1158. Xhash_table * table;
  1159. X{
  1160. X    id ** ent;            /* Entries of table are bases of id lists. */
  1161. X    id * ids;
  1162. X    int i;
  1163. X
  1164. X    for ( i = 0, ent = & table->hash_id[0];    /* Clear the entries. */
  1165. X      i < table->hash_size;
  1166. X      i++, ent++ )
  1167. X    if ( ids = *ent )
  1168. X        FREE_LIST( ids );
  1169. X
  1170. X    FREE( table );
  1171. X}
  1172. X
  1173. ________This_Is_The_END________
  1174. if test `wc -c < symtab.c` -ne 3411; then
  1175.     echo 'shar: symtab.c was damaged during transit (should have been 3411 bytes)'
  1176. fi
  1177. fi        ; : end of overwriting check
  1178. echo 'x - symtab.h'
  1179. if test -f symtab.h; then echo 'shar: not overwriting symtab.h'; else
  1180. sed 's/^X//' << '________This_Is_The_END________' > symtab.h
  1181. X/* 
  1182. X * symtab.h - Declarations for using Symbol Table package.
  1183. X * 
  1184. X * Author:    Russ Fish
  1185. X *         Computer Science Dept.
  1186. X *         University of Utah
  1187. X * Date:    28 August 1981
  1188. X */
  1189. X
  1190. X/*****************************************************************
  1191. X * TAG( symtab )
  1192. X * 
  1193. X * Simple symbol table package.  Hash on name string gives buckets within
  1194. X * hash table, which are bases of lists of id's, rather than reprobing
  1195. X * within table.  Multiple dictionaries are supported.
  1196. X */
  1197. X#ifndef _SYM_TAB                /* Only once. */
  1198. X#define _SYM_TAB
  1199. X
  1200. X#include "list.h"            /* Needs list package. */
  1201. X
  1202. X/*****************************************************************
  1203. X * TAG( id )
  1204. X *
  1205. X * id - Identifier datatype.
  1206. X */
  1207. Xtypedef struct _id id;
  1208. Xstruct _id
  1209. X{
  1210. X    TLISTLINKS(id);            /* Linked lists within hash buckets. */
  1211. X    string var_name;        /* Character string name of id. */
  1212. X    address var_value;        /* Ptr to value of variable id. */
  1213. X};
  1214. X
  1215. X/*****************************************************************
  1216. X * TAG( new_symbol find_symbol )
  1217. X */
  1218. Xextern id *
  1219. Xnew_symbol();                /* Constructor. */
  1220. X/* ( sym_name, table )
  1221. X * string sym_name;
  1222. X * hash_table * table;
  1223. X */
  1224. X
  1225. Xextern id *
  1226. Xfind_symbol();                /* Locator, NULL if not found. */
  1227. X/* ( sym_name, table )
  1228. X * string sym_name;
  1229. X * hash_table * table;
  1230. X */
  1231. X
  1232. X/*****************************************************************
  1233. X *  TAG( hash_table new_hash_table hash )
  1234. X *
  1235. X * Datatype for hash dictionaries.
  1236. X */
  1237. Xtypedef struct
  1238. X{
  1239. X    int hash_size;            /* Number of entries in table. */
  1240. X    id * hash_id[1];        /* Base of id list on entry. */
  1241. X}
  1242. Xhash_table;
  1243. X
  1244. Xextern hash_table *
  1245. Xnew_hash_table();              /* Constructor, returns cleared table. */
  1246. X/* ( n_entries )
  1247. X * int n_entries;
  1248. X */
  1249. X
  1250. Xextern id * *
  1251. Xhash();       /* Hashing algorithm, returns ptr to base of an id list in table. */
  1252. X/* ( sym_name, table )
  1253. X * string sym_name;
  1254. X * hash_table * table;
  1255. X */
  1256. X
  1257. X/*****************************************************************
  1258. X * TAG( ld_table )
  1259. X * 
  1260. X * Links a vector of already initialiazed id's into a hash table.
  1261. X * NULL var_name terminates the vector.
  1262. X */
  1263. Xextern 
  1264. Xld_table();
  1265. X/* ( id_vec, table )
  1266. X * id id_vec[];
  1267. X * hash_table * table;
  1268. X */
  1269. X
  1270. X/*****************************************************************
  1271. X * TAG( fr_hash_table )
  1272. X * 
  1273. X * Dispose of a hash table.
  1274. X */
  1275. Xextern 
  1276. Xfr_hash_table();
  1277. X/* ( table )
  1278. X * hash_table * table;
  1279. X */
  1280. X
  1281. X/* Macros to aid in initializing symbol vectors. */
  1282. X#define NULLS  NULL,NULL
  1283. X#define VAR_SYM(name_string,var_name) { NULLS, name_string,&var_name, NULL }
  1284. X#define FN_SYM(name_string,fn_name) { NULLS, name_string, NULL, fn_name }
  1285. X
  1286. X#endif _SYM_TAB
  1287. ________This_Is_The_END________
  1288. if test `wc -c < symtab.h` -ne 2547; then
  1289.     echo 'shar: symtab.h was damaged during transit (should have been 2547 bytes)'
  1290. fi
  1291. fi        ; : end of overwriting check
  1292. exit 0
  1293.