home *** CD-ROM | disk | FTP | other *** search
/ The HTML Web Publisher's Construction Kit / HTMLWPCK.ISO / unix / servers / httpd_14 / source.z / source / httpd_1.4.1 / cgi-src / phf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-18  |  7.0 KB  |  206 lines

  1. #include <stdio.h>
  2. #ifndef NO_STDLIB_H
  3. #include <stdlib.h>
  4. #else
  5. char *getenv();
  6. #endif
  7.  
  8. #define    LF    10
  9. #define HTML_BREAK    printf("<P>%c", LF);
  10. typedef struct {
  11.     char name[128];
  12.     char val[128];
  13. } entry;
  14.  
  15. typedef struct {
  16.     char qfield[256];
  17.     int  qlen;
  18.     char qname[256];
  19. } fields;
  20.  
  21. void getword(char *word, char *line, char stop);
  22. char x2c(char *what);
  23. void unescape_url(char *url);
  24. void plustospace(char *str);
  25. void send_fd(FILE *f, FILE *fd);
  26. void send_doc(int which);
  27.  
  28. static fields idxfields[] = { {"Qalias", 32, "Alias"},
  29.                              {"Qname", 256, "Name" },
  30.                              {"Qemail", 128, "E-mail Address"},
  31.                              {"Qnickname", 120, "Nickname"},
  32.                              {"Qoffice_phone", 60, "Office Phone Number"},
  33.                              {"Qcallsign", 16, "HAM Callsign"},
  34.                              {"Qproxy", 64, "Proxy"},
  35.                              {"Qhigh_school", 30, "High School"},
  36.                              {"Qslip", 256, "SLIP Address"},
  37.                              {NULL, 0, NULL}
  38.                            };
  39.  
  40. static fields othersearchfields[] = { {"Qcurriculum", 64, "Curriculum"},
  41.                              {"Qphone", 64, "Phone Number" },
  42.                              {"Qaddress", 128, "Address"},
  43.                              {"Qoffice_address", 128, "Office Address"},
  44.                              {"Qhome_address", 128, "Home Address"},
  45.                              {"Qpermanent_address", 128, "Permanent Address"},
  46.                              {"Qpermanent_phone", 60, "Permanent Phone"},
  47.                              {"Qdepartment", 64, "Department"},
  48.                              {"Qtitle", 64, "Title"},
  49.                              {"Qproject", 256, "Project"},
  50.                              {"Qother", 256, "Other"},
  51.                              {"Qbirthday", 24, "Birthday"},
  52.                              {"Qcolleges", 120, "Colleges Attended"},
  53.                              {"Qleft_uiuc", 24, "Date/Month Person left UIUC"},
  54.                              {NULL, 0, NULL},
  55.                            };
  56.  
  57. void send_doc(int which) {
  58.     int x;
  59.  
  60.     printf("<TITLE>Form for CSO PH query</TITLE>%c", LF);
  61.     printf("<H1>Form for CSO PH query</H1>%c", LF);
  62.     printf("This form will send a PH query to the specified ph server.%c", LF);
  63.     HTML_BREAK
  64.     printf("<HR>%c", LF);
  65.  
  66.     printf("<FORM ACTION=\"http://%s:%s%s\">%c", getenv("SERVER_NAME"),
  67.            getenv("SERVER_PORT"), getenv("SCRIPT_NAME"), LF);
  68.  
  69.     printf("PH Server:<INPUT TYPE=\"text\" NAME=\"Jserver\" VALUE=\"ns.uiuc.edu\" MAXLENGTH=\"256\">%c", LF);
  70.     HTML_BREAK
  71.  
  72.     printf("<H3>At least one of these fields must be specified:</H3><UL>%c",LF);
  73.     for(x=0; idxfields[x].qlen != 0; x++) 
  74.         printf("<LI><INPUT TYPE=\"text\" NAME=\"%s\" MAXLENGTH=\"%d\">%s%c"
  75.                ,idxfields[x].qfield, idxfields[x].qlen, idxfields[x].qname,LF);
  76.  
  77.     printf("</UL>%c", LF);
  78.  
  79.     if (!(which&0x10)) {
  80.         printf("<A HREF=\"%s?Jform=%d\"><H3>Show additional fields to narrow query</H3></A>%c", getenv("SCRIPT_NAME"), (which | 0x10), LF);
  81.         }
  82.     else {
  83.         printf("<H3>Additional fields to narrow query:</H3><UL>%c",LF);
  84.  
  85.         for(x=0; othersearchfields[x].qlen != 0; x++)
  86.             printf("<LI><INPUT TYPE=\"text\" NAME=\"%s\" MAXLENGTH=\"%d\">%s%c"
  87.                    ,othersearchfields[x].qfield, othersearchfields[x].qlen,
  88.                    othersearchfields[x].qname,LF);
  89.  
  90.         printf("</UL>%c", LF);
  91.  
  92.         printf("<A HREF=\"%s?Jform=%d\">Show fewer query fields</A>%c", getenv("SCRIPT_NAME"), (which & 0x01), LF);
  93.         }
  94.  
  95.     HTML_BREAK
  96.  
  97.     if (!(which & 0x01)) {
  98.         printf("<A HREF=\"%s?Jform=%d\"><H3>Return more than default fields</H3></A>%c", getenv("SCRIPT_NAME"), (which | 0x01), LF);
  99.         }
  100.     else {
  101.         printf("<H3>Fields to return:</H3><UL>%c", LF);
  102.  
  103.         for(x=0; idxfields[x].qlen != 0; x++) 
  104.             printf("<LI><INPUT TYPE=\"checkbox\" NAME=\"return\" VALUE=\"%s\">%s%c", &(idxfields[x].qfield[1]), idxfields[x].qname, LF);
  105.  
  106.         for(x=0; othersearchfields[x].qlen != 0; x++)
  107.             printf("<LI><INPUT TYPE=\"checkbox\" NAME=\"return\" VALUE=\"%s\">%s%c", &(othersearchfields[x].qfield[1]), othersearchfields[x].qname, LF);
  108.  
  109.         printf("</UL>%c", LF);
  110.  
  111.         printf("<A HREF=\"%s?Jform=%d\">Return default fields</A>%c", getenv("SCRIPT_NAME"), (which & 0x10), LF);
  112.         }
  113.  
  114.     HTML_BREAK
  115.     printf("<INPUT TYPE=\"submit\">%c", LF);
  116.     printf("</FORM>%c", LF);
  117.  
  118.     printf("<HR>%c<ADDRESS>", LF);
  119.     printf("Questions, comments to: <a href=\"http://www.ncsa.uiuc.edu/SDG/People/jbrowne/jbrowne.html\">Jim Browne</a>%c", LF);
  120.     printf("</ADDRESS>%c", LF);
  121.         
  122. }
  123.  
  124. main(int argc, char *argv[]) {
  125.     entry entries[64];
  126.     register int x,m=0;
  127.     char *cl;
  128.     char returnstr[1024], typestr[4098], commandstr[8192], serverstr[256];
  129.     int atleastonereturn = 0, atleastonequery = 0, which = 0;
  130.     FILE *phfp;
  131.  
  132.     printf("Content-type: text/html%c%c",LF,LF);
  133.  
  134.     strcpy(returnstr, "return ");
  135.     strcpy(typestr, " ");
  136.  
  137.     cl = getenv("QUERY_STRING");
  138.  
  139.     if((!cl) || (!cl[0])) {
  140.         send_doc(0);
  141.         exit(1);
  142.     }
  143.  
  144.     for(x=0;cl[0] != '\0';x++) {
  145.         m=x;
  146.         getword(entries[x].val,cl,'&');
  147.         plustospace(entries[x].val);
  148.         unescape_url(entries[x].val);
  149.         getword(entries[x].name,entries[x].val,'=');
  150.     }
  151.  
  152.     for(x=0; x <= m; x++) {
  153. /*      printf("%s = %s %c", entries[x].name, entries[x].val, LF); */
  154.  
  155.         if (!strcmp(entries[x].name, "return")) {
  156.             strcat(returnstr, entries[x].val);
  157.             strcat(returnstr, " ");
  158.             atleastonereturn = 1;
  159.             }
  160.         else if ((entries[x].name[0] == 'Q') && strlen(entries[x].val)) {
  161.             strcat(typestr, &(entries[x].name[1]));
  162.             strcat(typestr, "=");
  163.             strcat(typestr, entries[x].val);
  164.             strcat(typestr, " ");
  165.             atleastonequery = 1;
  166.             }
  167.         else if (!strcmp(entries[x].name, "Jserver")) 
  168.             strcpy(serverstr, entries[x].val);
  169.         else if (!strcmp(entries[x].name, "Jform")) 
  170.             if (sscanf(entries[x].val, "%d", &which)) {
  171.                 send_doc(which);
  172.                 exit(1);
  173.                 }
  174.             else exit(1);
  175.         }       
  176.  
  177.     printf("<H1>Query Results</H1>%c", LF);
  178.     HTML_BREAK
  179.  
  180.     if (!atleastonequery) printf("<B>You did not enter a query!</B>%c",LF);
  181.     else {
  182.         strcpy(commandstr, "/usr/local/bin/ph -m ");
  183.         if (strlen(serverstr)) {
  184.            strcat(commandstr, " -s ");
  185.            /* RM 2/22/94 oops */
  186.            escape_shell_cmd(serverstr);
  187.            strcat(commandstr, serverstr);
  188.            strcat(commandstr, " ");
  189.            }
  190.         escape_shell_cmd(typestr);
  191.         strcat(commandstr, typestr);
  192.         if (atleastonereturn) {
  193.            escape_shell_cmd(returnstr);
  194.            strcat(commandstr, returnstr);
  195.         }
  196.  
  197.         printf("%s%c", commandstr, LF);
  198.         printf("<PRE>%c", LF);
  199.  
  200.         phfp = popen(commandstr,"r");
  201.         send_fd(phfp, stdout);
  202.  
  203.         printf("</PRE>%c", LF);
  204.         }
  205. }
  206.