⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookmarks.c

📁 浏览器的源代码,可移植到嵌入式设备.
💻 C
📖 第 1 页 / 共 4 页
字号:
   Bms_sec_add(title);   g_free(title);   /* Write new bookmarks file */   Bms_save();   return 0;}/* * Parse an "add url" request, and update the bm file. * Return code: { 0:OK, 1:Abort } */static int Bmsrv_modify_add_url(int OutFD, char *s_url){   char *p, *q, *title, *url;   int i;   static gint section = 0;   /* bookmarks were loaded before */   if (OutFD == -1) {      /* look for section */      for (q = s_url; (q = strstr(q, "&s")); ++q) {         for (i = 0; isdigit(q[2+i]); ++i);         if (q[2+i] == '=')            section = strtol(q + 2, NULL, 10);      }      return 1;   }   if (!(p = strstr(s_url, "&title=")) ||       !(q = strstr(s_url, "&url=")))      return 1;   title = g_strdup (p + 7);   if ((p = strchr(title, '&')))      *p = 0;   url = g_strdup (q + 5);   if ((p = strchr(url, '&')))      *p = 0;   if (strlen(title) && strlen(url)) {      Unencode_str(title);      Unencode_str(url);      Bms_add(section, url, title);   }   g_free(title);   g_free(url);   section = 0;   /* todo: we should send an "Bookmark added" message, but the      msg-after-HTML functionallity is still pending, not hard though. */   /* Write new bookmarks file */   Bms_save();   return 0;}/* * Check the parameters of a modify request, and return an error message * when it's wrong. * Return code: { 0:OK, 2:Close } */static int Bmsrv_check_modify_request(int OutFD, char *url){   char *p, *msg;   int n_sec, n_url;   /* Count number of marked urls and sections */   Bmsrv_count_urls_and_sections(url, &n_sec, &n_url);   p = strchr(url, '?');   if (strstr(p, "operation=delete&")) {      if (n_url || n_sec)         return 0;      msg = "Delete: you must mark what to delete!";   } else if (strstr(url, "operation=move&")) {      if (n_url && n_sec)         return 0;      else if (n_url)         msg = "Move: you must mark a target section!";      else if (n_sec)         msg = "Move: can not move a section (yet).";      else         msg = "Move: you must mark some urls, and a target section!";   } else if (strstr(url, "operation=modify&")) {      if (n_url || n_sec)         return 0;      msg = "Modify: you must mark what to update!";   } else if (strstr(url, "operation=modify2&")) {      /* nothing to check here */      return 0;   } else if (strstr(url, "operation=add_sec&")) {      /* nothing to check here */      return 0;   } else if (strstr(url, "operation=add_section&")) {      /* nothing to check here */      return 0;   } else if (strstr(url, "operation=add_url&")) {      if (n_sec <= 1)         return 0;      msg = "Add url: only one target section is allowed!";   } else if (strstr(url, "operation=add_url2&")) {      /* nothing to check here */      return 0;   } else if (strstr(url, "operation=none&")) {      msg = "No operation, just do nothing!";   } else {      msg = "Sorry, not implemented yet.";   }   Bmsrv_dpi_send_status_msg(OutFD, msg);   return 2;}/* * Parse a and process a modify request. * Return code: { 0:OK, 1:Abort, 2:Close } */static int Bmsrv_process_modify_request(int OutFD, char *url){   /* check the provided parameters */   if (Bmsrv_check_modify_request(OutFD, url) != 0)      return 2;   if (strstr(url, "operation=delete&")) {      if (Bmsrv_modify_delete(OutFD, url) == 1)         return 1;      if (Bmsrv_send_reload_request(OutFD, "dpi:/bm/modify") == 1)         return 1;   } else if (strstr(url, "operation=move&")) {      if (Bmsrv_modify_move(OutFD, url) == 1)         return 1;      if (Bmsrv_send_reload_request(OutFD, "dpi:/bm/modify") == 1)         return 1;   } else if (strstr(url, "operation=modify&")) {      /* make a local copy of 'url' */      Bmsrv_send_modify_update(-1, url);      MODIFY_PAGE_NUM = 3;      if (Bmsrv_send_reload_request(OutFD, "dpi:/bm/modify") == 1)         return 1;   } else if (strstr(url, "operation=modify2&")) {      if (Bmsrv_modify_update(OutFD, url) == 1)         return 1;      if (Bmsrv_send_reload_request(OutFD, "dpi:/bm/modify") == 1)         return 1;   } else if (strstr(url, "operation=add_sec&")) {      /* this global variable tells which page to send  (--hackish...) */      MODIFY_PAGE_NUM = 2;      if (Bmsrv_send_reload_request(OutFD, "dpi:/bm/modify") == 1)         return 1;   } else if (strstr(url, "operation=add_section&")) {      if (Bmsrv_modify_add_section(OutFD, url) == 1)         return 1;      if (Bmsrv_send_reload_request(OutFD, "dpi:/bm/modify") == 1)         return 1;   } else if (strstr(url, "operation=add_url&")) {      /* this global variable tells which page to send  (--hackish...) */      MODIFY_PAGE_NUM = 4;      /* parse section if present */      Bmsrv_modify_add_url(-1, url);      if (Bmsrv_send_reload_request(OutFD, "dpi:/bm/modify") == 1)         return 1;   } else if (strstr(url, "operation=add_url2&")) {      if (Bmsrv_modify_add_url(OutFD, url) == 1)         return 1;      if (Bmsrv_send_reload_request(OutFD, "dpi:/bm/modify") == 1)         return 1;   }   return 2;}/* -- Bookmarks ------------------------------------------------------------ *//* * Send the current bookmarks page (in HTML) */int send_bm_page(int OutFD){   static GString *gstr = NULL;   gchar *l_title;   GSList *list1, *list2;   BmSec *sec_node;   BmRec *bm_node;   if (!gstr)      gstr = g_string_new("");   if (send_data_string(OutFD, mainpage_header))      return 1;   /* write sections header */   if (send_data_string(OutFD, mainpage_sections_header))      return 1;   /* write sections */   for (list1 = B_secs; list1; list1 = list1->next) {      sec_node = list1->data;      g_string_sprintf(gstr, mainpage_sections_item,                       sec_node->section, sec_node->title);      if (send_data_string(OutFD, gstr->str))         return 1;   }   /* write sections footer */   if (send_data_string(OutFD, mainpage_sections_footer))      return 1;   /* send page middle */   if (send_data_string(OutFD, mainpage_middle1))      return 1;   /* send bookmark cards */   for (list1 = B_secs; list1; list1 = list1->next) {      sec_node = list1->data;      /* send card header */      l_title = make_one_line_str(sec_node->title);      g_string_sprintf(gstr, mainpage_section_card_header,                       sec_node->section, l_title);      g_free(l_title);      if (send_data_string(OutFD, gstr->str))         return 1;      /* send section's bookmarks */      for (list2 = B_bms; list2; list2 = list2->next) {         bm_node = list2->data;         if (bm_node->section == sec_node->section) {            g_string_sprintf(gstr, mainpage_section_card_item,                             bm_node->url, bm_node->title);            if (send_data_string(OutFD, gstr->str))               return 1;         }      }      /* send card footer */      if (send_data_string(OutFD, mainpage_section_card_footer))         return 1;   }   /* finish page */   if (send_data_string(OutFD, mainpage_footer))      return 1;   return 0;}/* -- Dpi parser ----------------------------------------------------------- *//* * Parse a data stream (dpi protocol) * Note: Buf is a zero terminated string * Return code: { 0:OK, 1:Abort, 2:Close } */int Bmsrv_parse_buf(int SockFD, char *Buf, int BufSize){   char *msg1="<dpi cmd='chat' msg='Hi browser'>";   char *msg2="<dpi cmd='chat' msg='Is it worth?'>";   char *msg3="<dpi cmd='chat' msg='Ok, send it'>";   char *p, *cmd, *url, *title, *msg, cmd_buf[16*1024];   int st;   if (!(p = strchr(Buf, '>'))) {      /* Haven't got a full tag */      g_print("Haven't got a full tag!\n");      return 1;   }   cmd = Get_attr_value(Buf, BufSize, "cmd");   //g_print("cmd: %s\n", cmd ? cmd : "NULL");   if (cmd && strcmp(cmd, "chat") == 0) {      g_free(cmd);      msg = Get_attr_value(Buf, BufSize, "msg");      if (*msg == 'H') {         /* "Hi server" */         if (send_data_chunk(SockFD, msg1, strlen(msg1)))            return 1;      } else if (*msg == 'I') {         /* "I want to set abookmark" */         if (send_data_chunk(SockFD, msg2, strlen(msg2)))            return 1;      } else if (*msg == 'S') {         /* "Sure" */         if (send_data_chunk(SockFD, msg3, strlen(msg3)))            return 1;      }      g_free(msg);      return 0;   }   /* sync with the bookmarks file */   Bms_cond_load();   if (cmd && strcmp(cmd, "DpiBye") == 0) {      g_print("bookmarks dpi (pid %d): Got DpiBye.\n", (gint)getpid());      exit(0);   } else if (cmd && strcmp(cmd, "add_bookmark") == 0) {      g_free(cmd);      url = Get_attr_value(Buf, BufSize, "url");      title = Get_attr_value(Buf, BufSize, "title");      if (strlen(title) == 0) {         g_free(title);         title = g_strdup("(Untitled)");      }      if (url && title)         Bmsrv_add_bm(SockFD, url, title);      g_free(url);      g_free(title);      return 2;   } else if (cmd && strcmp(cmd, "open_url") == 0) {      g_free(cmd);      url = Get_attr_value(Buf, BufSize, "url");      if (strcmp(url, "dpi:/bm/modify") == 0) {         st = Bmsrv_send_modify_answer(SockFD, url);         return st;      } else if (strncmp(url, "dpi:/bm/modify?", 15) == 0) {         /* process request */         st = Bmsrv_process_modify_request(SockFD, url);         return st;      }      sprintf(cmd_buf, "<dpi cmd='start_send_page' url='%s'>", url);      //g_print("  cmd_buf=%s\n", cmd_buf);      /* Send dpi command */      if (send(SockFD, cmd_buf, strlen(cmd_buf), 0) == -1) {         perror("[send]");         return 1;      }      /* Send HTTP header */      //g_print("  sending HTTP header...\n");      if (send(SockFD, Header, strlen(Header), 0) == -1) {         perror("[send]");         return 1;      }      //g_print("  sending page body...\n");      st = send_bm_page(SockFD);      if (st != 0) {         char *err =            "<HTML><body> Error on the bookmarks server...</body></html>";         if (send(SockFD, err, strlen(err), 0) == -1) {            perror("[send]");            return 1;         }      }      //g_print("  page body sent.\n");      return 2;   }   return 0;}/* --  Termination handlers ----------------------------------------------- *//* * (was to delete the local namespace socket), *  but this is handled by 'dpid' now. */void cleanup(void){  /* no cleanup required */}/* * Perform any necessary cleanups upon abnormal termination */void termination_handler(int signum){  exit(signum);}/* * -- MAIN ------------------------------------------------------------------- */int main (void) {   struct sockaddr_un spun;   int temp_sock_descriptor;   int address_size;   char buf[16384];   int st, code;   /* Arrange the cleanup function for terminations via exit() */   atexit(cleanup);   /* Arrange the cleanup function for abnormal terminations */   if (signal (SIGINT, termination_handler) == SIG_IGN)     signal (SIGINT, SIG_IGN);   if (signal (SIGHUP, termination_handler) == SIG_IGN)     signal (SIGHUP, SIG_IGN);   if (signal (SIGTERM, termination_handler) == SIG_IGN)     signal (SIGTERM, SIG_IGN);   /* Remove the old socket filename */   cleanup();   BmFile = g_strconcat(g_get_home_dir(), "/", ".dillo/bm.txt", NULL);   g_print("bookmarks.dpi (v.13): accepting connections...\n");   /* some OSes may need this... */   address_size = sizeof(struct sockaddr_un);   while (1) {      temp_sock_descriptor =         accept(STDIN_FILENO, (struct sockaddr *)&spun, &address_size);      if (temp_sock_descriptor == -1) {         perror("[accept]");         exit(1);      }      while (1) {         //sleep(1);         do            st = recv(temp_sock_descriptor, buf, 16384, 0);         while (st < 0 && errno == EINTR);         if (st == -1) {            perror("[recv]");            exit(1);         }         buf[st] = 0;         //g_print("Received from client: %s\n", buf);         /* Let's see what we fished... */         code = Bmsrv_parse_buf(temp_sock_descriptor, buf, st);         if (code == 1)            exit(1);         else if (code == 2)            break;      }      //g_print("Closing temp_sock_descriptor\n");      close(temp_sock_descriptor);   }/*while*/}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -