static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { static SERVICE_TABLE_ENTRY service_table[] = { {server_name, (LPSERVICE_MAIN_FUNCTION) ServiceMain}, {NULL, NULL} }; int service_installed; char buf[200], *service_argv[] = {__argv[0], NULL}; POINT pt; HMENU hMenu; switch (msg) { case WM_CREATE: if (__argv[1] != NULL && !strcmp(__argv[1], service_magic_argument)) { start_mongoose(1, service_argv); StartServiceCtrlDispatcher(service_table); exit(EXIT_SUCCESS); } else { start_mongoose(__argc, __argv); } break; case WM_COMMAND: switch (LOWORD(wParam)) { case ID_QUIT: mg_stop(ctx); Shell_NotifyIcon(NIM_DELETE, &TrayIcon); PostQuitMessage(0); break; case ID_EDIT_CONFIG: edit_config_file(); break; case ID_INSTALL_SERVICE: case ID_REMOVE_SERVICE: manage_service(LOWORD(wParam)); break; } break; case WM_USER: switch (lParam) { case WM_RBUTTONUP: case WM_LBUTTONUP: case WM_LBUTTONDBLCLK: hMenu = CreatePopupMenu(); AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, server_name); AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, ""); service_installed = manage_service(0); snprintf(buf, sizeof(buf), "NT service: %s installed", service_installed ? "" : "not"); AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf); AppendMenu(hMenu, MF_STRING | (service_installed ? MF_GRAYED : 0), ID_INSTALL_SERVICE, "Install service"); AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0), ID_REMOVE_SERVICE, "Deinstall service"); AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, ""); AppendMenu(hMenu, MF_STRING, ID_EDIT_CONFIG, "Edit config file"); AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit"); GetCursorPos(&pt); SetForegroundWindow(hWnd); TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL); PostMessage(hWnd, WM_NULL, 0, 0); DestroyMenu(hMenu); break; } break; case WM_CLOSE: mg_stop(ctx); Shell_NotifyIcon(NIM_DELETE, &TrayIcon); PostQuitMessage(0); return 0; // We've just sent our own quit message, with proper hwnd. } return DefWindowProc(hWnd, msg, wParam, lParam); }
static BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lP) { FILE *fp; int i; const char *name, *value, **options = mg_get_valid_option_names(); switch (msg) { case WM_CLOSE: DestroyWindow(hDlg); break; case WM_COMMAND: switch (LOWORD(wParam)) { case ID_SAVE: EnableWindow(GetDlgItem(hDlg, ID_SAVE), FALSE); if ((fp = fopen(config_file, "w+")) != NULL) { save_config(hDlg, fp); fclose(fp); mg_stop(ctx); start_mongoose(__argc, __argv); } EnableWindow(GetDlgItem(hDlg, ID_SAVE), TRUE); break; case ID_RESET_DEFAULTS: for (i = 0; options[i] != NULL; i += 3) { name = options[i + 1]; value = options[i + 2] == NULL ? "" : options[i + 2]; if (is_boolean_option(name)) { CheckDlgButton(hDlg, ID_CONTROLS + i / 3, !strcmp(value, "yes") ? BST_CHECKED : BST_UNCHECKED); } else { SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i / 3), value); } } break; } for (i = 0; options[i] != NULL; i += 3) { name = options[i + 1]; if ((is_filename_option(name) || is_directory_option(name)) && LOWORD(wParam) == ID_CONTROLS + i / 3 + ID_FILE_BUTTONS_DELTA) { OPENFILENAME of; BROWSEINFO bi; char path[PATH_MAX] = ""; memset(&of, 0, sizeof(of)); of.lStructSize = sizeof(of); of.hwndOwner = (HWND) hDlg; of.lpstrFile = path; of.nMaxFile = sizeof(path); of.lpstrInitialDir = mg_get_option(ctx, "document_root"); of.Flags = OFN_CREATEPROMPT | OFN_NOCHANGEDIR; memset(&bi, 0, sizeof(bi)); bi.hwndOwner = (HWND) hDlg; bi.lpszTitle = "Choose WWW root directory:"; bi.ulFlags = BIF_RETURNONLYFSDIRS; if (is_directory_option(name)) { SHGetPathFromIDList(SHBrowseForFolder(&bi), path); } else { GetOpenFileName(&of); } if (path[0] != '\0') { SetWindowText(GetDlgItem(hDlg, ID_CONTROLS + i / 3), path); } } } break; case WM_INITDIALOG: SendMessage(hDlg, WM_SETICON,(WPARAM) ICON_SMALL, (LPARAM) hIcon); SendMessage(hDlg, WM_SETICON,(WPARAM) ICON_BIG, (LPARAM) hIcon); SetWindowText(hDlg, "Mongoose settings"); SetFocus(GetDlgItem(hDlg, ID_SAVE)); for (i = 0; options[i] != NULL; i += 3) { name = options[i + 1]; value = mg_get_option(ctx, name); if (is_boolean_option(name)) { CheckDlgButton(hDlg, ID_CONTROLS + i / 3, !strcmp(value, "yes") ? BST_CHECKED : BST_UNCHECKED); } else { SetDlgItemText(hDlg, ID_CONTROLS + i / 3, value == NULL ? "" : value); } } break; default: break; } return FALSE; }
int main(int argc, char **argv) { int c; char fname[MAX_IMAGE_FNAME]; const char *software_select = NULL; int opt_i = 0; int opt_e = 0; int opt_s = 0; int opt_w = 0; struct hw_type hwrev; char image_url[MAX_URL]; int opt_d = 0; RECOVERY_STATUS result; #ifdef CONFIG_WEBSERVER char weboptions[1024]; char **av = NULL; int ac = 0; #endif memset(&flashdesc, 0, sizeof(flashdesc)); memset(main_options, 0, sizeof(main_options)); strcpy(main_options, "vhi:se:"); #ifdef CONFIG_MTD strcat(main_options, "b:"); #endif #ifdef CONFIG_DOWNLOAD strcat(main_options, "d:"); #endif #ifdef CONFIG_WEBSERVER strcat(main_options, "w:"); #endif memset(fname, 0, sizeof(fname)); printf("%s\n", BANNER); printf("Licensed under GPLv2. See source distribution for detailed " "copyright notices.\n\n"); /* Process options with getopt */ while ((c = getopt_long(argc, argv, main_options, long_options, NULL)) != EOF) { switch (c) { case 'v': verbose++; break; #ifdef CONFIG_MTD case 'b': mtd_set_ubiblacklist(optarg); break; #endif case 'i': strncpy(fname, optarg, sizeof(fname)); opt_i = 1; break; case 'e': software_select = optarg; opt_e = 1; break; case 'h': usage(argv[0]); exit(0); break; #ifdef CONFIG_DOWNLOAD case 'd': strncpy(image_url, optarg, sizeof(image_url)); opt_d = 1; break; #endif case 's': /* run as server */ opt_s = 1; break; #ifdef CONFIG_WEBSERVER case 'w': snprintf(weboptions, sizeof(weboptions), "%s %s", argv[0], optarg); av = splitargs(weboptions, &ac); opt_w = 1; break; #endif default: usage(argv[0]); exit(1); break; } } swupdate_init(&swcfg); lua_handlers_init(); if(!get_hw_revision(&hwrev)) printf("Running on %s Revision %s\n", hwrev.boardname, hwrev.revision); print_registered_handlers(); notify_init(); if (opt_e) { if (parse_image_selector(software_select, &swcfg)) { fprintf(stderr, "Incorrect select option format\n"); exit(1); } fprintf(stderr, "software set: %s mode: %s\n", swcfg.software_set, swcfg.running_mode); } network_daemon = start_thread(network_initializer, &swcfg); if (opt_i) { install_from_file(fname); cleanup_files(&swcfg); notify(SUCCESS, 0, 0); } if (opt_d) { result =download_from_url(image_url); if (result == SUCCESS) exit(0); else exit(1); } /* Start embedded web server */ #if defined(CONFIG_MONGOOSE) if (opt_w) start_mongoose(ac, av); #endif if (opt_w || opt_s) pthread_join(network_daemon, NULL); }
std::string MongooseNDK::MGstart(const std::string& arg) { int res = 0; char *options[MAX_OPTIONS]; Json::Reader reader; Json::FastWriter writer; Json::Value root; Json::Value rval; m_pParent->getLog()->debug("MgStartRunning"); char ptemp[MAXPATHLEN]; // Build default document root in case missing getcwd(ptemp, MAXPATHLEN); strcat(ptemp,"/data"); bool parse = reader.parse(arg, root); for(unsigned int i=0; i<MAX_OPTIONS; i++) { options[i] = NULL; } if(is_running) { rval["status"] = false; rval["error"] = "Already Running"; } else { if (parse) { int xops = 0; if(!root.isMember("document_root")) { options[xops++] = sdup("document_root"); options[xops++] = sdup(ptemp); } if(!root.isMember("enable_directory_listing")) { options[xops++] = sdup("enable_directory_listing"); options[xops++] = sdup("no"); } Json::Value::Members memberNames = root.getMemberNames(); int ecount = 0; for(unsigned int i=0; i<memberNames.size(); i++) { std::string memberName = memberNames[i]; options[xops++] = sdup(memberName.c_str()); options[xops++] = urldecode(root[memberName].asCString()); } m_pParent->getLog()->info("Starting Mongoose"); ecount = start_mongoose(options); for (int idx = 0; options[idx] != NULL; idx++) { free(options[idx]); } if(ecount >= 0) { rval["status"] = true; rval["error"] = false; rval["command_errors"] = ecount; rval["document_root"] = mongoose_get_option("document_root"); rval["listening_ports"] = mongoose_get_option("listening_ports"); is_running = true; } else { ecount = 0 - (ecount + 1); rval["status"] = false; rval["error"] = "Unable to start server"; rval["command_errors"] = ecount; } } else { rval["status"] = false; rval["error"] = "Unable to parse JSON"; } } return writer.write(rval); }
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { static SERVICE_TABLE_ENTRY service_table[] = { {server_name, (LPSERVICE_MAIN_FUNCTION) ServiceMain}, {NULL, NULL} }; int service_installed; char buf[200], *service_argv[] = {__argv[0], NULL}; POINT pt; HMENU hMenu; static UINT s_uTaskbarRestart; // for taskbar creation switch (msg) { case WM_CREATE: if (__argv[1] != NULL && !strcmp(__argv[1], service_magic_argument)) { start_mongoose(1, service_argv); StartServiceCtrlDispatcher(service_table); exit(EXIT_SUCCESS); } else { start_mongoose(__argc, __argv); s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); } break; case WM_COMMAND: switch (LOWORD(wParam)) { case ID_QUIT: mg_stop(ctx); Shell_NotifyIcon(NIM_DELETE, &TrayIcon); PostQuitMessage(0); return 0; case ID_SETTINGS: show_settings_dialog(); break; case ID_INSTALL_SERVICE: case ID_REMOVE_SERVICE: manage_service(LOWORD(wParam)); break; case ID_CONNECT: printf("[%s]\n", get_url_to_first_open_port(ctx)); ShellExecute(NULL, "open", get_url_to_first_open_port(ctx), NULL, NULL, SW_SHOW); break; } break; case WM_USER: switch (lParam) { case WM_RBUTTONUP: case WM_LBUTTONUP: case WM_LBUTTONDBLCLK: hMenu = CreatePopupMenu(); AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, server_name); AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, ""); service_installed = manage_service(0); snprintf(buf, sizeof(buf), "NT service: %s installed", service_installed ? "" : "not"); AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf); AppendMenu(hMenu, MF_STRING | (service_installed ? MF_GRAYED : 0), ID_INSTALL_SERVICE, "Install service"); AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0), ID_REMOVE_SERVICE, "Deinstall service"); AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, ""); AppendMenu(hMenu, MF_STRING, ID_CONNECT, "Start browser"); AppendMenu(hMenu, MF_STRING, ID_SETTINGS, "Edit Settings"); AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, ""); AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit"); GetCursorPos(&pt); SetForegroundWindow(hWnd); TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL); PostMessage(hWnd, WM_NULL, 0, 0); DestroyMenu(hMenu); break; } break; case WM_CLOSE: mg_stop(ctx); Shell_NotifyIcon(NIM_DELETE, &TrayIcon); PostQuitMessage(0); return 0; // We've just sent our own quit message, with proper hwnd. default: if (msg==s_uTaskbarRestart) Shell_NotifyIcon(NIM_ADD, &TrayIcon); } return DefWindowProc(hWnd, msg, wParam, lParam); }