home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fax-3.2.1 / cmd / faxps / faxps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  5.7 KB  |  252 lines

  1. /*
  2.   This file is part of the NetFax system.
  3.  
  4.   (c) Copyright 1989 by David M. Siegel. 
  5.       All rights reserved.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation.
  10.  
  11.     This program is distributed in the hope that it will be useful, 
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <signal.h>
  24. #include <pwd.h>
  25. #include <sys/param.h>
  26.  
  27. #include "../../lib/libutil/list.h"
  28. #include "../../lib/libfax/libfax.h"
  29.  
  30. /*
  31.  * Some flags that we share with the signal handlers.
  32.  */
  33. static int pages = -1;
  34. static int coversheet = FALSE;
  35.  
  36. /*
  37.  * Base filenames for the PPM files that Ghostscript generates.
  38.  */
  39. static char out_base[MAXPATHLEN];
  40. static char coversheet_base[MAXPATHLEN];
  41.  
  42. /*
  43.  * This is the Postscript file that is being spooled.
  44.  */
  45. static int delete_ps_file = FALSE;
  46. static char *ps_file;
  47.  
  48. /*
  49.  * Delete generated G3 files.
  50.  */
  51. static void cleanup_g3_files(base, pages)
  52.      char *base;
  53.      int pages;
  54. {
  55.     int page;
  56.  
  57.     for (page = 1; page <= pages; page++) {
  58.     char file[MAXPATHLEN];
  59.     sprintf(file, "%s.g3.%d", base, page);
  60.     unlink(file);
  61.     }
  62. }
  63.  
  64. /*
  65.  * Cleanup temporary files.
  66.  */
  67. static void cleanup()
  68. {
  69.     if (delete_ps_file)
  70.       unlink(ps_file);
  71.     if (pages > 0 && strlen(out_base) > 0)
  72.       cleanup_g3_files(out_base, pages);
  73.     if (coversheet && strlen(coversheet_base) > 0)
  74.       cleanup_g3_files(coversheet_base, 1);
  75. }
  76.  
  77. /*
  78.  * Called when we get a variety of signals.
  79.  */
  80. static void signal_handler()
  81. {
  82.     fprintf(stderr, "transmission aborted\n");
  83.     cleanup();
  84.     exit(1);
  85. }
  86.  
  87. /*
  88.  * Install the signals that we want to trap for cleanup.
  89.  */
  90. static void install_signal_handlers()
  91. {
  92.     signal(SIGHUP, signal_handler);
  93.     signal(SIGINT, signal_handler);
  94.     signal(SIGQUIT, signal_handler);
  95. }
  96.  
  97. main(argc,argv)
  98.      int argc;
  99.      char *argv[];
  100. {
  101.     int c;
  102.     int errflg = 0;
  103.     extern char *optarg;
  104.     extern int optind;
  105.  
  106.     int pid = getpid();
  107.     int now = time(0);
  108.  
  109.     int got_recip = 0;
  110.     int rc;
  111.  
  112.     char fax_cmd[2048];
  113.     char recips[2048];
  114.     char *recipient = "";
  115.     char *sender = "";
  116.     char *sender_fax = "";
  117.     int mail_notification = FALSE;
  118.     char *host = NULL;
  119.     char *user_name = NULL;
  120.  
  121.     strcpy(recips, "");
  122.     strcpy(out_base, "");
  123.     strcpy(coversheet_base, "");
  124.  
  125.     while ((c = getopt(argc, argv, "cp:r:s:S:mu:h:")) != -1)
  126.       switch (c) {
  127.     case 'c':
  128.       coversheet = TRUE;
  129.       break;
  130.     case 'p':
  131.       got_recip++;
  132.       strcat(recips, " ");
  133.       strcat(recips, optarg);
  134.       break;
  135.     case 'r':
  136.       recipient = optarg;
  137.       break;
  138.     case 's':
  139.       sender = optarg;
  140.       break;
  141.     case 'S':
  142.       sender_fax = optarg;
  143.       break;
  144.     case 'm':
  145.       mail_notification = TRUE;
  146.       break;
  147.     case 'u':
  148.       user_name = optarg;
  149.       break;
  150.     case 'h':
  151.       host = optarg;
  152.       break;
  153.     case '?':
  154.       errflg++;;
  155.       break;
  156.       }
  157.  
  158.     if (errflg || got_recip == 0) {
  159.     fprintf(stderr, "usage: %s -p phone [filename]\n", argv[0]);
  160.     fprintf(stderr, "\tone or more -p argument may be given\n");
  161.     fprintf(stderr, "\tif no filename is given, input is from stdin\n");
  162.     fprintf(stderr, "optional args:\n");
  163.     fprintf(stderr, "\t-m will send email notification on delivery\n");
  164.     fprintf(stderr, "\t-h <host> uses the spooler on the given host\n");
  165.     fprintf(stderr, "\t-u <user> spools job with the given user name\n");
  166.     fprintf(stderr, "\t-c generates a coverpage\n");
  167.     fprintf(stderr, "\t-s <name> specifies the coverpage sender\n");
  168.     fprintf(stderr, "\t-S <phone> specifies the coverpage return fax\n");
  169.     fprintf(stderr, "\t-r <name> specifies the coverpage recipient\n");
  170.     exit(1);
  171.     }
  172.  
  173.     /*
  174.      * Catch some signals so we can do a proper cleanup.
  175.      */
  176.     install_signal_handlers();
  177.  
  178.     /*
  179.      * Do we get input from stdin?
  180.      */
  181.     if (optind == argc) {
  182.     char *mktemp();
  183.     char buf[BUFSIZ];
  184.     int bytes;
  185.     int fd;
  186.     
  187.     ps_file = mktemp("/tmp/faxdXXXXXX");
  188.     delete_ps_file = TRUE;
  189.     
  190.     if ((fd = open(ps_file, O_WRONLY|O_CREAT, 0666)) < 0) {
  191.         perror("temp file");
  192.         exit(2);
  193.     }
  194.  
  195.     while ((bytes = read(0, buf, sizeof(buf))) > 0)
  196.       write(fd, buf, bytes);
  197.     
  198.     close(fd);
  199.     } else
  200.       ps_file = argv[optind];
  201.  
  202.     /*
  203.      * Make the G3 files.
  204.      */
  205.     sprintf(out_base, "/tmp/fax%d%d", pid, now);
  206.     if ((pages = cvt_postscript_to_g3(ps_file, out_base)) <= 0) {
  207.     fprintf(stderr, "can't convert postscript file\n");
  208.     exit(3);
  209.     }
  210.  
  211.     /*
  212.      * Make the coversheet.
  213.      */
  214.     if (coversheet) {
  215.     sprintf(coversheet_base, "/tmp/faxc%d%d", pid, now);
  216.     if (cvt_coversheet_to_g3(coversheet_base, recipient, sender, 
  217.                  sender_fax, pages) < 0) {
  218.         fprintf(stderr, "can't convert coversheet\n");
  219.         cleanup_g3_files(out_base, pages);
  220.         exit(3);
  221.     }
  222.     }
  223.  
  224.     /*
  225.      * Build the fax command.
  226.      */
  227.     sprintf(fax_cmd, "%s", FAX_ENQ_PROG);
  228.     if (user_name != NULL)
  229.       sprintf(&fax_cmd[strlen(fax_cmd)], " -u %s", user_name);
  230.     if (host != NULL)
  231.       sprintf(&fax_cmd[strlen(fax_cmd)], " -h %s", host);
  232.     if (mail_notification)
  233.       strcat(fax_cmd, " -m");
  234.     strcat(fax_cmd, recips);
  235.     strcat(fax_cmd, " ---");
  236.     if (coversheet)
  237.       sprintf(&fax_cmd[strlen(fax_cmd)], " %s.g3.1", coversheet_base);
  238.     sprintf(&fax_cmd[strlen(fax_cmd)], " %s.g3:%d", out_base, pages);
  239.  
  240.     /*
  241.      * Run the fax command.
  242.      */
  243.     rc = system(fax_cmd);
  244.  
  245.     /*
  246.      * All done!  Cleanup and exit.
  247.      */
  248.     cleanup();
  249.  
  250.     exit(rc);
  251. }
  252.