home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lynx2.8.1dev.10.tar.gz / lynx2.8.1dev.10.tar / lynx2-8 / src / LYList.c < prev    next >
C/C++ Source or Header  |  1998-04-23  |  11KB  |  361 lines

  1. /*            Lynx Document Reference List Support          LYList.c
  2. **            ====================================
  3. **
  4. **    Author: FM    Foteos Macrides (macrides@sci.wfbr.edu)
  5. **
  6. */
  7.  
  8. #include <HTUtils.h>
  9. #include <tcp.h>
  10. #include <LYUtils.h>
  11. #include <GridText.h>
  12. #include <LYList.h>
  13. #include <LYSignal.h>
  14. #include <LYGlobalDefs.h>
  15. #include <LYCharUtils.h>
  16.  
  17. #ifdef DIRED_SUPPORT
  18. #include <LYUpload.h>
  19. #include <LYLocal.h>
  20. #endif /* DIRED_SUPPORT */
  21.  
  22. #include <LYexit.h>
  23. #include <LYLeaks.h>
  24.  
  25. #define FREE(x) if (x) {free(x); x = NULL;}
  26.  
  27. /*    showlist - F.Macrides (macrides@sci.wfeb.edu)
  28. **    --------
  29. **    Create a temporary text/html file with a list of links to
  30. **    HyperText References in the current document.
  31. **
  32. **  On entry
  33. **    titles        Set:    if we want titles where available
  34. **            Clear:    we only get addresses.
  35. */
  36.  
  37. static char list_filename[256] = "\0";
  38.  
  39. /*
  40.  *  Returns the name of the file used for the List Page, if one has
  41.  *  been created, as a full URL; otherwise, returns an empty string.
  42.  * - kw
  43.  */
  44. PUBLIC char * LYlist_temp_url NOARGS
  45. {
  46.     return list_filename;
  47. }
  48.  
  49. PUBLIC int showlist ARGS2(
  50.     document *,    newdoc,
  51.     BOOLEAN,    titles)
  52. {
  53.     int cnt;
  54.     int refs, hidden_links;
  55.     static char tempfile[256];
  56.     static BOOLEAN first = TRUE;
  57.     FILE *fp0;
  58.     char *Address = NULL, *Title = NULL, *cp = NULL;
  59.     BOOLEAN intern_w_post = FALSE;
  60.     char *desc = "unknown field or link";
  61.  
  62.     refs = HText_sourceAnchors(HTMainText);
  63.     hidden_links = HText_HiddenLinkCount(HTMainText);
  64.     if (refs <= 0 && hidden_links > 0 &&
  65.     LYHiddenLinks != HIDDENLINKS_SEPARATE) {
  66.     _statusline(NO_VISIBLE_REFS_FROM_DOC);
  67.     sleep(MessageSecs);
  68.     return(-1);
  69.     }
  70.     if (refs <= 0 && hidden_links <= 0) {
  71.     _statusline(NO_REFS_FROM_DOC);
  72.     sleep(MessageSecs);
  73.     return(-1);
  74.     }
  75.  
  76.     if (first) {
  77.     tempname(tempfile, NEW_FILE);
  78.     /*
  79.      *  Make the file a URL now.
  80.      */
  81. #if defined (VMS) || defined (DOSPATH)    || defined (__EMX__)
  82.     sprintf(list_filename, "file://localhost/%s", tempfile);
  83. #else
  84.     sprintf(list_filename, "file://localhost%s", tempfile);
  85. #endif /* VMS */
  86.     first = FALSE;
  87. #ifdef VMS
  88.     } else {
  89.     remove(tempfile);  /* Remove duplicates on VMS. */
  90. #endif /* VMS */
  91.     }
  92.  
  93.     if ((fp0 = LYNewTxtFile(tempfile)) == NULL) {
  94.     _statusline(CANNOT_OPEN_TEMP);
  95.     sleep(MessageSecs);
  96.     return(-1);
  97.     }
  98.  
  99.     StrAllocCopy(newdoc->address, list_filename);
  100.     LYforce_HTML_mode = TRUE;    /* force this file to be HTML */
  101.     LYforce_no_cache = TRUE;    /* force this file to be new */
  102.  
  103.  
  104.     fprintf(fp0, "<head>\n");
  105.     LYAddMETAcharsetToFD(fp0, -1);
  106.     if (strchr(HTLoadedDocumentURL(), '"') == NULL) {
  107.     /*
  108.      *  Insert a BASE tag so there is some way to relate the List Page
  109.      *  file to its underlying document after we are done.    It won't
  110.      *  be actually used for resolving relative URLs. - kw
  111.      */
  112.     StrAllocCopy(Address, HTLoadedDocumentURL());
  113.     LYEntify(&Address, FALSE);
  114.     fprintf(fp0, "<base href=\"%s\">\n", Address);
  115.     FREE(Address);
  116.     }
  117.     fprintf(fp0, "<title>%s</title>\n</head>\n<body>\n",
  118.          LIST_PAGE_TITLE);
  119.     fprintf(fp0, "<h1>You have reached the List Page</h1>\n");
  120.     fprintf(fp0, "<h2>%s Version %s</h2>\n", LYNX_NAME, LYNX_VERSION);
  121.     StrAllocCopy(Address, HTLoadedDocumentURL());
  122.     LYEntify(&Address, FALSE);
  123.     fprintf(fp0,
  124.         "  References in %s<p>\n",
  125.         ((Address != NULL && *Address != '\0') ?
  126.                        Address : "this document:"));
  127.     FREE(Address);
  128.     if (refs > 0) {
  129.     fprintf(fp0, "<%s compact>\n", ((keypad_mode == NUMBERS_AS_ARROWS) ?
  130.                        "ol" : "ul"));
  131.     if (hidden_links > 0)
  132.         fprintf(fp0, "<lh><em>Visible links:</em>\n");
  133.     }
  134.     if (hidden_links > 0) {
  135.     if (LYHiddenLinks == HIDDENLINKS_IGNORE)
  136.         hidden_links = 0;
  137.     }
  138.     for (cnt = 1; cnt <= refs; cnt++) {
  139.     HTChildAnchor *child = HText_childNumber(cnt);
  140.     HTAnchor *dest_intl = NULL;
  141.     HTAnchor *dest;
  142.     HTParentAnchor *parent;
  143.     char *address;
  144.     CONST char *title;
  145.  
  146.     if (child == 0) {
  147.         /*
  148.          *    child should not be 0 unless form field numbering is on
  149.          *    and cnt is the number of a form input field.
  150.          *    HText_FormDescNumber() will set desc to a description
  151.          *    of what type of input field this is.  We'll list it to
  152.          *    ensure that the link numbers on the list page match the
  153.          *    numbering in the original document, but won't create a
  154.          *    forward link to the form. - FM && LE
  155.          *
  156.          *    Changed to create a fake hidden link, to get the numbering
  157.          *    right in connection with always treating this file as
  158.          *    HIDDENLINKS_MERGE in GridText.c - kw
  159.          */
  160.         if (keypad_mode == LINKS_AND_FORM_FIELDS_ARE_NUMBERED) {
  161.         HText_FormDescNumber(cnt, (char **)&desc);
  162.         fprintf(fp0,
  163.         "<li><a id=%d href=\"#%d\">form field</a> = <em>%s</em>\n",
  164.             cnt, cnt, desc);
  165.         }
  166.         continue;
  167.     }
  168. #ifndef DONT_TRACK_INTERNAL_LINKS
  169.     dest_intl = HTAnchor_followTypedLink((HTAnchor *)child,
  170.                                LINK_INTERNAL);
  171. #endif
  172.     dest = dest_intl ?
  173.         dest_intl : HTAnchor_followMainLink((HTAnchor *)child);
  174.     parent = HTAnchor_parent(dest);
  175.     if (!intern_w_post && dest_intl &&
  176.         HTMainAnchor && HTMainAnchor->post_data &&
  177.         parent->post_data &&
  178.         !strcmp(HTMainAnchor->post_data, parent->post_data)) {
  179.         /*
  180.          *    Set flag to note that we had at least one internal link,
  181.          *    if the document from which we are generating the list
  182.          *    has assosiated POST data; after an extra check that the
  183.          *    link destination really has hthe same POST data so that
  184.          *    we can believe it is an internal link.
  185.          */
  186.         intern_w_post = TRUE;
  187.     }
  188.     address =  HTAnchor_address(dest);
  189.     title = titles ? HTAnchor_title(parent) : NULL;
  190.     StrAllocCopy(Address, address);
  191.     FREE(address);
  192.     LYEntify(&Address, TRUE);
  193.     if (title && *title) {
  194.         StrAllocCopy(Title, title);
  195.         LYEntify(&Title, TRUE);
  196.         if (*Title) {
  197.         cp = strchr(Address, '#');
  198.         } else {
  199.         FREE(Title);
  200.         }
  201.     }
  202.  
  203.     fprintf(fp0, "<li><a href=\"%s\"%s>%s%s%s%s%s</a>\n", Address,
  204.             dest_intl ? " TYPE=\"internal link\"" : "",
  205.             dest_intl ? "(internal) " : "",
  206.             ((HTAnchor*)parent != dest) && Title ? "in " : "",
  207.             (char *)(Title ? Title : Address),
  208.             (Title && cp) ? " - " : "",
  209.             (Title && cp) ? (cp+1) : "");
  210.  
  211.     FREE(Address);
  212.     FREE(Title);
  213.     }
  214.  
  215.     if (hidden_links > 0) {
  216.     if (refs > 0)
  217.         fprintf(fp0, "\n</%s>\n\n<p>\n",
  218.              ((keypad_mode == NUMBERS_AS_ARROWS) ?
  219.                             "ol" : "ul"));
  220.     fprintf(fp0, "<%s compact>\n", ((keypad_mode == NUMBERS_AS_ARROWS) ?
  221.                     "ol continue" : "ul"));
  222.     fprintf(fp0, "<lh><em>Hidden links:</em>\n");
  223.     }
  224.  
  225.     for (cnt = 0; cnt < hidden_links; cnt++) {
  226.     StrAllocCopy(Address, HText_HiddenLinkAt(HTMainText, cnt));
  227.     LYEntify(&Address, FALSE);
  228.     if (!(Address && *Address)) {
  229.         FREE(Address);
  230.         continue;
  231.     }
  232.     fprintf(fp0, "<li><a href=\"%s\">%s</a>\n", Address, Address);
  233.  
  234.     FREE(Address);
  235.     }
  236.  
  237.     fprintf(fp0,"\n</%s>\n</body>\n", ((keypad_mode == NUMBERS_AS_ARROWS) ?
  238.                        "ol" : "ul"));
  239.  
  240.     /*
  241.      *    Make necessary changes to newdoc before returning to caller.
  242.      *    If the intern_w_post flag is set, we keep the POST data in
  243.      *    newdoc that have been passed in.  They should be the same as
  244.      *    in the loaded locument for which we generated the list.
  245.      *    In that case the file we have written will be associated with
  246.      *    the same POST data when it is loaded after we are done here,
  247.      *    so that following one of the links we have marked as "internal
  248.      *    link" can lead back to the underlying document with the right
  249.      *    address+post_data combination. - kw
  250.      */
  251.     if (intern_w_post) {
  252.     newdoc->internal_link = TRUE;
  253.     } else {
  254.     FREE(newdoc->post_data);
  255.     FREE(newdoc->post_content_type);
  256.     newdoc->internal_link = FALSE;
  257.     }
  258.     newdoc->isHEAD = FALSE;
  259.     newdoc->safe = FALSE;
  260.     fclose(fp0);
  261.     return(0);
  262. }
  263.  
  264. /*    printlist - F.Macrides (macrides@sci.wfeb.edu)
  265. **    ---------
  266. **    Print a text/plain list of HyperText References
  267. **    in the current document.
  268. **
  269. **  On entry
  270. **    titles        Set:    if we want titles where available
  271. **            Clear:    we only get addresses.
  272. */
  273. PUBLIC void printlist ARGS2(
  274.     FILE *,     fp,
  275.     BOOLEAN,    titles)
  276. {
  277. #ifdef VMS
  278.     extern BOOLEAN HadVMSInterrupt;
  279. #endif /* VMS */
  280.     int cnt;
  281.     int refs, hidden_links;
  282.     char *address = NULL;
  283.     char *desc = "unknown field or link";
  284.  
  285.     refs = HText_sourceAnchors(HTMainText);
  286.     if (refs <= 0 && LYHiddenLinks != HIDDENLINKS_SEPARATE)
  287.     return;
  288.     hidden_links = HText_HiddenLinkCount(HTMainText);
  289.     if (refs <= 0 && hidden_links <= 0) {
  290.     return;
  291.     } else {
  292.     fprintf(fp, "\n%s\n\n", "References");
  293.     if (hidden_links > 0) {
  294.         fprintf(fp, "   Visible links:\n");
  295.         if (LYHiddenLinks == HIDDENLINKS_IGNORE)
  296.         hidden_links = 0;
  297.     }
  298.     for (cnt = 1; cnt <= refs; cnt++) {
  299.         HTChildAnchor *child = HText_childNumber(cnt);
  300.         HTAnchor *dest;
  301.         HTParentAnchor *parent;
  302.         CONST char *title;
  303.  
  304.         if (child == 0) {
  305.         /*
  306.          *  child should not be 0 unless form field numbering is on
  307.          *  and cnt is the number of a form intput field.
  308.          *  HText_FormDescNumber() will set desc to a description
  309.          *  of what type of input field this is.  We'll create a
  310.          *  within-document link to ensure that the link numbers on
  311.          *  the list page match the numbering in the original document,
  312.          *  but won't create a forward link to the form. - FM && LE
  313.          */
  314.         if (keypad_mode == LINKS_AND_FORM_FIELDS_ARE_NUMBERED) {
  315.             HText_FormDescNumber(cnt, (char **)&desc);
  316.             fprintf(fp, "%4d. form field = %s\n", cnt, desc);
  317.         }
  318.         continue;
  319.         }
  320.         dest = HTAnchor_followMainLink((HTAnchor *)child);
  321.         /*
  322.          *    Ignore if child anchor points to itself, i.e. we had
  323.          *    something like <A NAME=xyz HREF="#xyz"> and it is not
  324.          *    treated as a hidden link.  Useful if someone 'P'rints
  325.          *    the List Page (which isn't a very useful action to do,
  326.          *    but anyway...) - kw
  327.          */
  328.         if (dest == (HTAnchor *)child)
  329.         continue;
  330.         parent = HTAnchor_parent(dest);
  331.         title = titles ? HTAnchor_title(parent) : NULL;
  332.         address =  HTAnchor_address(dest);
  333.         fprintf(fp, "%4d. %s%s\n", cnt,
  334.             ((HTAnchor*)parent != dest) && title ? "in " : "",
  335.             (title ? title : address));
  336.         FREE(address);
  337. #ifdef VMS
  338.         if (HadVMSInterrupt)
  339.         break;
  340. #endif /* VMS */
  341.     }
  342.  
  343.     if (hidden_links > 0)
  344.         fprintf(fp, "%s   Hidden links:\n", ((refs > 0) ? "\n" : ""));
  345.     for (cnt = 0; cnt < hidden_links; cnt++) {
  346.         StrAllocCopy(address, HText_HiddenLinkAt(HTMainText, cnt));
  347.         if (!(address && *address)) {
  348.         FREE(address);
  349.         continue;
  350.         }
  351.         fprintf(fp, "%4d. %s\n", ((cnt + 1) + refs), address);
  352.         FREE(address);
  353. #ifdef VMS
  354.         if (HadVMSInterrupt)
  355.         break;
  356. #endif /* VMS */
  357.     }
  358.     }
  359.     return;
  360. }
  361.