home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / apple2 / 30 < prev    next >
Encoding:
Text File  |  1990-11-26  |  13.0 KB  |  554 lines

  1. Path: wuarchive!mit-eddie!rutgers!aramis.rutgers.edu!paul.rutgers.edu!jac
  2. From: jac@paul.rutgers.edu (Jonathan A. Chandross)
  3. Newsgroups: comp.sources.apple2
  4. Subject: v001SRC008:  AAF Tools (REPOST)
  5. Message-ID: <Nov.19.20.11.02.1990.19381@paul.rutgers.edu>
  6. Date: 20 Nov 90 01:11:02 GMT
  7. Organization: Rutgers Univ., New Brunswick, N.J.
  8. Lines: 543
  9. Approved: jac@paul.rutgers.edu
  10.  
  11.  
  12. Submitted-by: jac
  13. Posting-number: Volume 1, Source:8
  14. Archive-name: archive/aaf_jac
  15. Architecture: ANY_2
  16. Version-number: 1.01
  17.  
  18. As I mentioned in another posting, the first version of the AAF tools
  19. I posted had a serious flaw.  That has been fixed in this release.
  20.  
  21. Please discard the earlier version of this package.
  22.  
  23. ********
  24.  
  25. =Read.Me
  26. -
  27. -Apple Archive Format
  28. -Version 1.01
  29. -
  30. -COPYRIGHT 1990 by Jonathan A. Chandross.
  31. -All Rights Reserved.
  32. -
  33. -The AAF is a simple archive format designed for shipping around Apple
  34. -source code.  An archive consists of a series of lines.  Each line is
  35. -interpreted according to the first character of the line.  The
  36. -interpretations are:
  37. -
  38. -    =       the name of the unpacked file starting here
  39. -    -       a source line to be unpacked into the current file
  40. -    +       end of archive.
  41. -
  42. -Lines beginning with any other character are simply output to the console.
  43. -
  44. -A simple example of this format:
  45. -    From: random@foobar.com
  46. -    To: freeworld
  47. -    Subject: First program.
  48. -            R.
  49. -    =helloworld.c
  50. -    -main()
  51. -    -{
  52. -    -       printf("Hello World\n");
  53. -    -}
  54. -    =Read.Me
  55. -    -Test out a C compiler; just compile and execute.
  56. -    + All done.
  57. -    J. Random Hacker
  58. -    random@foobar.com
  59. -
  60. -This file would create the files "helloworld.c" and "Read.Me".  When
  61. -the archive was unpacked, all of the non-source and non-file-name
  62. -lines would be output to the console until the '+' was encountered.
  63. -
  64. -Using '=' to specify a file name and '-' to specify a line of source
  65. -allows a standard USENET or email file to be unpacked without removing
  66. -any preamble or trailing information.
  67. -
  68. -The '+' at the end of the file indicates the end of an archive.  Lines
  69. -occurring past this point will not be processed at all.
  70. -
  71. -The '=' and "+" sentinels are due to Doug Gwyn.
  72. -
  73. -
  74. -Jonathan A. Chandross
  75. -jac@paul.rutgers.edu
  76. -November 1990
  77. -
  78. =Makefile
  79. -
  80. -FLAGS =        -g
  81. -DEFINES =    -D SHELL
  82. -
  83. -all: paaf upaaf
  84. -
  85. -paaf: paaf.c
  86. -    cc ${DEFINES} ${FLAGS} -o paaf paaf.c
  87. -
  88. -upaaf: upaaf.c
  89. -    cc ${DEFINES} ${FLAGS} -o upaaf upaaf.c
  90. -
  91. -lint:
  92. -    lint paaf.c
  93. -    lint upaaf.c
  94. -
  95. -clean:
  96. -    rm -f paaf upaaf
  97. -
  98. =paaf.c
  99. -
  100. -/*
  101. - *******************************************************************************
  102. - *
  103. - * paaf.c
  104. - *
  105. - * Pack an archive in Apple Archive Format.
  106. - *
  107. - * Another quality product handcrafted with loving care by:
  108. - *    Jonathan A. Chandross
  109. - *    jac@paul.rutgers.edu
  110. - *    November 1990
  111. - *
  112. - * COPYRIGHT 1990 by Jonathan A. Chandross
  113. - * All rights reserved.
  114. - *
  115. - * Use "cc -D SHELL" to compile for use with a shell.
  116. - *
  117. - * Version 1.01
  118. - *
  119. - *******************************************************************************
  120. - */
  121. -
  122. -#include <stdio.h>
  123. -
  124. -/*
  125. - *******************************************************************************
  126. - *
  127. - * Defines
  128. - *
  129. - * EXPORT        function is visible outside this module
  130. - * IMPORT        function is defined outside this module
  131. - * STATIC        function is invisible outside this module
  132. - *
  133. - * LENGTH        length of strings
  134. - *
  135. - *******************************************************************************
  136. - */
  137. -#define EXPORT
  138. -#define IMPORT    extern
  139. -#define LOCAL    static
  140. -
  141. -#define LENGTH    255
  142. -
  143. -/*
  144. - *******************************************************************************
  145. - *
  146. - * main
  147. - *
  148. - * Packs a series of files in Apple Archive Format
  149. - * 
  150. - * Parameters:
  151. - *    argc            number of command line arguments if compiled
  152. - *                for a shell
  153. - *    argv            command line argument vector
  154. - * Locals:
  155. - *    archive_name        name of archive to pack if not using a shell
  156. - * Returns:
  157. - *
  158. - * If this program is compiled for use with a shell, the first command line
  159. - * argument is considered to contain a list of file names to archive.  If
  160. - * this program is compiled stand-alone, i.e. not for use with a shell, it
  161. - * prompts for the files to pack.
  162. - *
  163. - * The SHELL define is an unsightly way to do things, but there is little
  164. - * choice if this program is to be used on an Apple lacking some sort of
  165. - * shell.
  166. - *
  167. - *******************************************************************************
  168. - */
  169. -#ifdef SHELL
  170. -EXPORT int main(argc, argv)
  171. -int        argc;
  172. -char        *argv[];
  173. -#else
  174. -EXPORT int main()
  175. -#endif
  176. -{
  177. -IMPORT    void    pack();
  178. -IMPORT    int    fclose();
  179. -#ifdef SHELL
  180. -IMPORT    void    usage();
  181. -#else
  182. -char        archive_file_name[LENGTH];
  183. -FILE        *archive_file;
  184. -#endif
  185. -
  186. -#ifdef SHELL
  187. -    /*
  188. -     * If we are using a shell, then argv[] will contain the names of the
  189. -     * files to pack into an archive.
  190. -     */
  191. -    if(argc == 1) {
  192. -        usage(argv[0]);
  193. -        exit(1);
  194. -        }
  195. -    else {
  196. -        /*
  197. -         * argc is always 1 more than the number of arguments since
  198. -         * argv[0] contains the name of the program.  This means we
  199. -         * have to correct for the array basing whenever we use an
  200. -         * argument.
  201. -         */
  202. -        int        arg_num;
  203. -
  204. -        for(arg_num = 2; arg_num <= argc; arg_num++) {
  205. -            (void) fprintf(stderr,
  206. -                    "Packing file: '%s'\n",
  207. -                    argv[arg_num - 1]);
  208. -            pack(argv[arg_num - 1], stdout);
  209. -            }
  210. -        (void) fputs("+ END OF ARCHIVE\n\n", stdout);
  211. -        }
  212. -#else
  213. -    (void) fputs("Archive name:", stdout);
  214. -    if(scanf("%s", archive_file_name) == EOF)
  215. -        exit(1);
  216. -
  217. -    if((archive_file = fopen(archive_file_name, "w")) == (FILE *)NULL) {
  218. -        (void) fprintf(stderr,
  219. -            "Error: could not open archive file '%s'\n",
  220. -            archive_file_name);
  221. -        exit(1);
  222. -        }
  223. -
  224. -    for(;;) {
  225. -        char        buffer[LENGTH];
  226. -
  227. -        (void) fputs("File name to archive:", stdout);
  228. -        if(scanf("%s", buffer) == EOF)
  229. -            break;
  230. -        else
  231. -            pack(buffer, archive_file);
  232. -        }
  233. -    (void) fputs("+ END OF ARCHIVE\n\n", archive_file);
  234. -    (void) fclose(archive_file);
  235. -#endif
  236. -
  237. -    return(0);
  238. -}
  239. -
  240. -/*
  241. - *******************************************************************************
  242. - *
  243. - * usage
  244. - *
  245. - * Print out a message on how to use this program
  246. - * 
  247. - * Parameters:
  248. - *    object_name        name of object file storing this program
  249. - * Locals:
  250. - * Returns:
  251. - *
  252. - *******************************************************************************
  253. - */
  254. -#ifdef SHELL
  255. -LOCAL void usage(object_name)
  256. -char        *object_name;
  257. -{
  258. -    (void) fprintf(stderr,
  259. -            "Usage: %s file_1 [file_2] [file_3] [...]\n",
  260. -            object_name);
  261. -}
  262. -#endif
  263. -
  264. -/*
  265. - *******************************************************************************
  266. - *
  267. - * pack
  268. - *
  269. - * pack an archive
  270. - * 
  271. - * Parameters:
  272. - *    file_name        name of file to add to archive
  273. - *    output_file        archive file (appended to)
  274. - * Locals:
  275. - *    buffer            holds each line as it is read out of archive
  276. - *    file            file to archive
  277. - * Returns:
  278. - *
  279. - *******************************************************************************
  280. - */
  281. -LOCAL void pack(file_name, output_file)
  282. -char        *file_name;
  283. -FILE        *output_file;
  284. -{
  285. -IMPORT    char    *fgets();
  286. -IMPORT    int    fputs();
  287. -IMPORT    int    fclose();
  288. -IMPORT    FILE    *fopen();
  289. -char        buffer[LENGTH];
  290. -FILE        *file;
  291. -
  292. -    /*
  293. -     * Open the file to archive and make sure it exists.
  294. -     */
  295. -    if((file = fopen(file_name, "r")) == (FILE *)NULL) {
  296. -        (void) fprintf(stderr,
  297. -                   "Error: could not open file '%s'\n",
  298. -                   file_name);
  299. -        exit(1);
  300. -        }
  301. -
  302. -    /*
  303. -     * Put the file name in the output
  304. -     */
  305. -    (void) fprintf(output_file, "=%s\n", file_name);
  306. -    
  307. -    /*
  308. -     * As long as the file is not empty, process lines.  Each line
  309. -     * to be output has a leading '-'.
  310. -     */
  311. -    buffer[0] = '-';
  312. -    while(fgets(&buffer[1], LENGTH, file) != (char *)NULL) {
  313. -        /*
  314. -         * Add the line to the output file with a leading '-'.
  315. -         */
  316. -        (void) fputs(buffer, output_file);
  317. -        }
  318. -
  319. -    /*
  320. -     * Done with this file.
  321. -     */
  322. -    (void) fclose(file);
  323. -}
  324. -
  325. =upaaf.c
  326. -
  327. -/*
  328. - *******************************************************************************
  329. - *
  330. - * upaaf.c
  331. - *
  332. - * Unpack an archive in apple format.
  333. - *
  334. - * Another quality product handcrafted with loving care by:
  335. - *    Jonathan A. Chandross
  336. - *    jac@paul.rutgers.edu
  337. - *    November 1990
  338. - *
  339. - * COPYRIGHT 1990 by Jonathan A. Chandross
  340. - * All rights reserved.
  341. - *
  342. - * Use "cc -D SHELL" to compile for use with a shell.
  343. - *
  344. - * Version 1.01
  345. - *
  346. - *******************************************************************************
  347. - */
  348. -
  349. -#include <stdio.h>
  350. -
  351. -/*
  352. - *******************************************************************************
  353. - *
  354. - * Defines
  355. - *
  356. - * EXPORT        function is visible outside this module
  357. - * IMPORT        function is defined outside this module
  358. - * STATIC        function is invisible outside this module
  359. - *
  360. - * LENGTH        length of strings
  361. - *
  362. - *******************************************************************************
  363. - */
  364. -#define EXPORT
  365. -#define IMPORT    extern
  366. -#define LOCAL    static
  367. -
  368. -#define LENGTH    255
  369. -
  370. -/*
  371. - *******************************************************************************
  372. - *
  373. - * main
  374. - *
  375. - * Unpacks a file in Apple Archive Format
  376. - * 
  377. - * Parameters:
  378. - *    argc            number of command line arguments if compiled
  379. - *                for a shell
  380. - *    argv            command line argument vector
  381. - * Locals:
  382. - *    archive_name        name of archive to unpack if not using a
  383. - *                shell
  384. - * Returns:
  385. - *
  386. - * If this program is compiled for use with a shell, the first command line
  387. - * argument is considered to contain the name of the archive to unpack.  If
  388. - * this program is compiled stand-alone, i.e. not for use with a shell, it
  389. - * prompts for the archive to unpack.
  390. - *
  391. - * The SHELL define is an unsightly way to do things, but there is little
  392. - * choice if this program is to be used on an Apple lacking some sort of
  393. - * shell.
  394. - *
  395. - *******************************************************************************
  396. - */
  397. -#ifdef SHELL
  398. -EXPORT int main(argc, argv)
  399. -int        argc;
  400. -char        *argv[];
  401. -#else
  402. -EXPORT int main()
  403. -#endif
  404. -{
  405. -IMPORT    void    unpack();
  406. -#ifdef SHELL
  407. -IMPORT    void    usage();
  408. -#else
  409. -char        archive_name[LENGTH];
  410. -#endif
  411. -
  412. -#ifdef SHELL
  413. -    /*
  414. -     * If we are using a shell, then argv[1] will be the name of the
  415. -     * archive to unpack.
  416. -     */
  417. -    if(argc == 2)
  418. -        unpack(argv[1]);
  419. -    else {
  420. -        usage(argv[0]);
  421. -        exit(1);
  422. -        }
  423. -#else
  424. -    (void) fputs("Enter name of archive to unpack:", stdout);
  425. -    (void) scanf("%s", archive_name);
  426. -    unpack(archive_name);
  427. -#endif
  428. -
  429. -    return(0);
  430. -}
  431. -
  432. -/*
  433. - *******************************************************************************
  434. - *
  435. - * usage
  436. - *
  437. - * Print out a message on how to use this program
  438. - * 
  439. - * Parameters:
  440. - *    object_name        name of object file storing this program
  441. - * Locals:
  442. - * Returns:
  443. - *
  444. - *******************************************************************************
  445. - */
  446. -#ifdef SHELL
  447. -LOCAL void usage(object_name)
  448. -char        *object_name;
  449. -{
  450. -    (void) fprintf(stderr, "Usage: %s archive-name\n", object_name);
  451. -}
  452. -#endif
  453. -
  454. -/*
  455. - *******************************************************************************
  456. - *
  457. - * unpack
  458. - *
  459. - * Unpack an archive
  460. - * 
  461. - * Parameters:
  462. - *    archive_file_name    name of archive file to unpack
  463. - * Locals:
  464. - *    buffer            holds each line as it is read out of archive
  465. - *    archive_file        archive file
  466. - *    output_file        current file being unpacked from archive
  467. - * Returns:
  468. - *
  469. - *******************************************************************************
  470. - */
  471. -LOCAL void unpack(archive_file_name)
  472. -char        *archive_file_name;
  473. -{
  474. -IMPORT    char    *fgets();
  475. -IMPORT    int    fput();
  476. -IMPORT    int    strlen();
  477. -IMPORT    int    fclose();
  478. -IMPORT    FILE    *fopen();
  479. -char        buffer[LENGTH];
  480. -FILE        *archive_file;
  481. -FILE        *output_file = (FILE *)NULL;
  482. -
  483. -    /*
  484. -     * Open the archive file and make sure it exists.
  485. -     */
  486. -    if((archive_file = fopen(archive_file_name, "r")) == (FILE *)NULL) {
  487. -        (void) fprintf(stderr, "Error: could not open archive '%s'\n",
  488. -            archive_file_name);
  489. -        exit(1);
  490. -        }
  491. -    
  492. -    /*
  493. -     * As long as the file is not empty, process lines
  494. -     */
  495. -    while(fgets(buffer, LENGTH, archive_file) != (char *)NULL) {
  496. -        /*
  497. -         * What kind of line do we have? 
  498. -         */
  499. -        switch(buffer[0]) {
  500. -             case '=':
  501. -            /*
  502. -             * Have a file name.  Remove the trailing newline
  503. -             * from the file-name (left by fgets) by overwriting
  504. -             * with a Null.  Then open the file and verify that
  505. -             * we can write to it.  Skip over the first character
  506. -             * in the file-name (it is an '=') 
  507. -             */
  508. -            buffer[strlen(buffer) - 1] = '\0';
  509. -            (void) fprintf(stdout, "Unpacking '%s'\n", &buffer[1]);
  510. -
  511. -            /*
  512. -             * If we have an open output file, close it.
  513. -             */
  514. -            if(output_file != (FILE *)NULL)
  515. -                (void) fclose(output_file);
  516. -
  517. -            if((output_file = fopen(&buffer[1], "w")) ==
  518. -               (FILE *)NULL) {
  519. -                (void) fprintf(stderr,
  520. -                    "Error: could not open '%s'\n", buffer);
  521. -                exit(1);
  522. -                }
  523. -            break;
  524. -             case '-':
  525. -            /*
  526. -             * Have a line of source.  Add the line to the output
  527. -             * file without the leading "-".
  528. -             */
  529. -            if(output_file == (FILE *)NULL) {
  530. -                fputs("Error: no file specified.\n", stderr);
  531. -                exit(1);
  532. -                }
  533. -            else
  534. -                (void) fputs(&buffer[1], output_file);
  535. -            break;
  536. -             case '+':
  537. -            /*
  538. -             * End of archive.  Exit.
  539. -             */
  540. -            exit(0);
  541. -            break;
  542. -             default:
  543. -            /*
  544. -             * Have some other type of line (mail header,
  545. -             * signature, comment, etc.)  Output it.
  546. -             */
  547. -            (void) fputs(&buffer[0], stdout);
  548. -            break;
  549. -             }
  550. -        }
  551. -}
  552. -
  553. + END OF ARCHIVE
  554.