request.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. *
  3. * Things used to handle special requests (eg. manual feed) globally or on a per
  4. * page basis. Requests are passed through to the translator using the -R option.
  5. * The argument to -R can be "request", "request:page", or "request:page:file".
  6. * If page is omitted (as in the first form) or set to 0 request will be applied
  7. * to the global environment. In all other cases it applies only to the selected
  8. * page. If a file is given, page must be supplied, and the lookup is in that file
  9. * rather than *requestfile.
  10. *
  11. */
  12. #include <stdio.h>
  13. #include "gen.h" /* general purpose definitions */
  14. #include "request.h" /* a few special definitions */
  15. #include "path.h" /* for the default request file */
  16. Request request[MAXREQUEST]; /* next page or global request */
  17. int nextreq = 0; /* goes in request[nextreq] */
  18. char *requestfile = REQUESTFILE; /* default lookup file */
  19. /*****************************************************************************/
  20. saverequest(want)
  21. char *want; /* grab code for this stuff */
  22. {
  23. char *page; /* and save it for this page */
  24. char *strtok();
  25. /*
  26. *
  27. * Save the request until we get to appropriate page - don't even bother with
  28. * the lookup right now. Format of *want string is "request", "request:page", or
  29. * "request:page:file", and we assume we can change the string here as needed.
  30. * If page is omitted or given as 0 the request will be done globally. If *want
  31. * includes a file, request and page must also be given, and in that case *file
  32. * will be used for the lookup.
  33. *
  34. */
  35. if ( nextreq < MAXREQUEST ) {
  36. request[nextreq].want = strtok(want, ": ");
  37. if ( (page = strtok(NULL, ": ")) == NULL )
  38. request[nextreq].page = 0;
  39. else request[nextreq].page = atoi(page);
  40. if ( (request[nextreq].file = strtok(NULL, ": ")) == NULL )
  41. request[nextreq].file = requestfile;
  42. nextreq++;
  43. } else error(NON_FATAL, "too many requests - ignoring %s", want);
  44. } /* End of saverequest */
  45. /*****************************************************************************/
  46. writerequest(page, fp_out)
  47. int page; /* write everything for this page */
  48. FILE *fp_out; /* to this file */
  49. {
  50. int i; /* loop index */
  51. /*
  52. *
  53. * Writes out all the requests that have been saved for page. Page 0 refers to
  54. * the global environment and is done during initial setup.
  55. *
  56. */
  57. for ( i = 0; i < nextreq; i++ )
  58. if ( request[i].page == page )
  59. dumprequest(request[i].want, request[i].file, fp_out);
  60. } /* End of writerequest */
  61. /*****************************************************************************/
  62. dumprequest(want, file, fp_out)
  63. char *want; /* look for this string */
  64. char *file; /* in this file */
  65. FILE *fp_out; /* and write the value out here */
  66. {
  67. char buf[100]; /* line buffer for reading *file */
  68. FILE *fp_in;
  69. /*
  70. *
  71. * Looks for *want in the request file and if it's found the associated value
  72. * is copied to the output file. Keywords (ie. the *want strings) begin an @ in
  73. * the first column of file, while the values (ie. the stuff that's copied to
  74. * the output file) starts on the next line and extends to the next keyword or
  75. * to the end of file.
  76. *
  77. */
  78. if ( (fp_in = fopen(file, "r")) != NULL ) {
  79. while ( fgets(buf, sizeof(buf), fp_in) != NULL )
  80. if ( buf[0] == '@' && strncmp(want, &buf[1], strlen(want)) == 0 )
  81. while ( fgets(buf, sizeof(buf), fp_in) != NULL )
  82. if ( buf[0] == '#' || buf[0] == '%' )
  83. continue;
  84. else if ( buf[0] != '@' )
  85. fprintf(fp_out, "%s", buf);
  86. else break;
  87. fclose(fp_in);
  88. } /* End if */
  89. } /* End of dumprequest */
  90. /*****************************************************************************/