void editMailcap(char *mailcap, struct parsed_tagarg *args) { TextList *t = newTextList(); TextListItem *ti; FILE *f; Str tmp; char *type, *viewer; struct parsed_tagarg *a; bool delete_it; if ((f = fopen(mailcap, "rt")) == NULL) bye("Can't open", mailcap); while (tmp = Strfgets(f), tmp->length > 0) { if (tmp->ptr[0] == '#') continue; Strchop(tmp); extractMailcapEntry(tmp->ptr, &type, &viewer); delete_it = false; for (a = args; a != NULL; a = a->next) { if (!strcmp(a->arg, "delete") && !strcmp(a->value, type)) { delete_it = true; break; } } if (!delete_it) pushText(t, Sprintf("%s;\t%s\n", type, viewer)->ptr); } type = tag_get_value(args, "newtype"); viewer = tag_get_value(args, "newcmd"); if (type != NULL && *type != '\0' && viewer != NULL && *viewer != '\0') pushText(t, Sprintf("%s;\t%s\n", type, viewer)->ptr); fclose(f); if ((f = fopen(mailcap, "w")) == NULL) bye("Can't write to", mailcap); for (ti = t->first; ti != NULL; ti = ti->next) fputs(ti->ptr, f); fclose(f); printf("Content-Type: text/plain\n"); printf("w3m-control: BACK\nw3m-control: BACK\n"); printf("w3m-control: REINIT MAILCAP\n"); }
char * acceptableMimeTypes() { static Str types = NULL; TextList *l; Hash_si *mhash; char *p; int i; if (types != NULL) return types->ptr; /* generate acceptable media types */ l = newTextList(); mhash = newHash_si(16); /* XXX */ /* pushText(l, "text"); */ putHash_si(mhash, "text", 1); pushText(l, "image"); putHash_si(mhash, "image", 1); for (i = 0; i < mailcap_list->nitem; i++) { struct mailcap *mp = UserMailcap[i]; char *mt; if (mp == NULL) continue; for (; mp->type; mp++) { p = strchr(mp->type, '/'); if (p == NULL) continue; mt = allocStr(mp->type, p - mp->type); if (getHash_si(mhash, mt, 0) == 0) { pushText(l, mt); putHash_si(mhash, mt, 1); } } } types = Strnew(); Strcat_charp(types, "text/html, text/*;q=0.5"); while ((p = popText(l)) != NULL) { Strcat_charp(types, ", "); Strcat_charp(types, p); Strcat_charp(types, "/*"); } return types->ptr; }
int main() { // Variables for diagnosis tree NodeT *root = NULL; // Variables for text List TextEntry text; TextList textList = newTextList(); // Variables for driver input control char szInputBuffer[MAX_LINE_SIZE+1]; // input command line int iScanfCnt; // sscanf return char *pszRemaining; // getToken returns this Token szCommand; // Input Command //Variables for reading data input command arguments Element element; // Binary Tree element char szParentId[MAX_ID_SIZE + 1]; // parent Id for NODE command char cYN; // Y or N value for NODE command char szAnswers[MAX_NUMBER_ANSWERS]; // Text for answers // Variables for results char *pszResultId; // Result of searchT or help // Read command lines until EOF while (fgets(szInputBuffer, MAX_LINE_SIZE, stdin) != NULL) { printf("%s", szInputBuffer); // If the line is just a comment, ignore it if (szInputBuffer[0] == '*') continue; // Command is a comment so skip it // get the command pszRemaining = getToken(szInputBuffer, szCommand, MAX_TOKEN_SIZE); // Determine what to invoke based on the command if (strcmp(szCommand, "ROOT") == 0) { // ROOT id - create the root for the tree pszRemaining = getToken(pszRemaining, element.szId, MAX_ID_SIZE); if (pszRemaining == NULL) ErrExit(ERR_DATA, "Invalid data for ROOT command, missing ID"); element.cNodeType = 'Q'; root = allocateNodeT(element); } else if (strcmp(szCommand, "NODE") == 0) { // NODE type id parentId yn iScanfCnt = sscanf(pszRemaining, "%c %s %s %c" , &element.cNodeType, element.szId , szParentId, &cYN); if (iScanfCnt < 4) ErrExit(ERR_DATA, "Invalid data for NODE command: '%s'", pszRemaining); // insert your code to check warning cases and handle the insertion insert(root, szParentId, element, cYN); } else if (strcmp(szCommand, "TEXT") == 0) { // TEXT type id displayText iScanfCnt = sscanf(pszRemaining, "%c %s %79[^\n]", &text.cType, text.szId, text.szText); if (iScanfCnt < 3) ErrExit(ERR_DATA, "Invalid data for TEXT command: '%s'", pszRemaining); addTextEntry(textList, text); } else if (strcmp(szCommand, "PRINT") == 0) { // PRINT using your prettyPrintT routine prettyPrintT(root, 0, textList); printf("\n\n"); } else if (strcmp(szCommand, "HELP") == 0) { // HELP id answers pszRemaining = getToken(pszRemaining, szAnswers, MAX_NUMBER_ANSWERS); if (pszRemaining == NULL) ErrExit(ERR_DATA, "Invalid data for HELP command, missing answers"); pszResultId = help(root, szAnswers, 0, textList); // It is expected that help might return NULL, if the tree is // not defined properly. (not necessarily a student error) if (pszResultId == NULL) printf("\t*** Warning: NULL returned from HELP\n"); else printf("\t%s: %s\n", pszResultId , getText(textList, pszResultId)); } else if (strcmp(szCommand, "DELETE") == 0) { pszRemaining = getToken(pszRemaining, element.szId, MAX_ID_SIZE); if (pszRemaining == NULL) ErrExit(ERR_DATA, "Invalid data for DELETE command, missing ID"); // insert your code for the DELETE command which should remove the // specified ID from its parent node and also free the subtree defined by // the specified ID deleteNode(root, element.szId); } } // Your code to free the tree freeT(root); // Free the textList free(textList); printf("\n"); return 0; }
int insert_bookmark(char *bmark, struct parsed_tagarg *data) { char *url, *title, *section; FILE *f; TextList *tl = newTextList(); int section_found = 0; int bmark_added = 0; Str tmp, section_tmp; url = tag_get_value(data, "url"); title = tag_get_value(data, "title"); section = tag_get_value(data, "newsection"); if (section == NULL || *section == '\0') section = tag_get_value(data, "section"); if (section == NULL || *section == '\0') section = DEFAULT_SECTION; if (url == NULL || *url == '\0' || title == NULL || *title == '\0') { /* Bookmark not added */ return FALSE; } url = html_quote(url); title = html_quote(title); section = html_quote(section); f = fopen(bmark, "r"); if (f == NULL) return create_new_bookmark(bmark, section, title, url, "w"); section_tmp = Sprintf("<h2>%s</h2>\n", section); for (;;) { tmp = Strfgets(f); if (tmp->length == 0) break; if (Strcasecmp(tmp, section_tmp) == 0) section_found = 1; if (section_found && !bmark_added) { Strremovefirstspaces(tmp); if (Strcmp_charp(tmp, end_section) == 0) { pushText(tl, Sprintf("<li><a href=\"%s\">%s</a>\n", url, title)->ptr); bmark_added = 1; } } if (!bmark_added && Strcasecmp_charp(tmp, "</body>\n") == 0) { pushText(tl, Sprintf("<h2>%s</h2>\n<ul>\n", section)->ptr); pushText(tl, Sprintf("<li><a href=\"%s\">%s</a>\n", url, title)->ptr); pushText(tl, end_section); pushText(tl, "</ul>\n"); bmark_added = 1; } pushText(tl, tmp->ptr); } fclose(f); if (!bmark_added) { /* Bookmark not added; perhaps the bookmark file is ill-formed */ /* In this case, a new bookmark is appeneded after the bookmark file */ return create_new_bookmark(bmark, section, title, url, "a"); } f = fopen(bmark, "w"); while (tl->nitem) { fputs(popText(tl), f); } fclose(f); return TRUE; }