#define DATASTR_MODE 1 /* Pas de vérif des arguments + verrou sytématique */ #define MSG_MODE 1 /* Pas de vérif des arguments */ #include #include #include #include #include #include #include #include VER_INFO_EXPORT (msgadmin, "$Revision: 1.1 $", "$Name: $", __FILE__, "$Author: smas $") #define USAGE "Usage : %s [ --help | --version [-v] | --create | --destroy ]\n" #define QUIT 0 #define BASE_INIT 1 #define BASE_OPEN 2 #define BASE_INFO 3 #define BASE_CLOSE 4 #define BASE_END 5 #define SHOW_PORT_LIST 6 #define PORT_CREATE 7 #define PORT_DUMP 8 #define PORT_FLUSH 9 #define PORT_DELETE 10 char menu [1000]; char tmp [100]; void init_menu (void); int print_menu (void); int main (int argc, char ** argv) { int choice; MSGT_Port * Port; MSGT_Message * Msg; char Answer[10]; /* Lancement de commande d'administration */ if (argc >= 2) { if (!strcmp (argv[1], "--help")) { fprintf (stderr, USAGE, argv[0]); return -1; } else if (!strcmp (argv[1], "--version")) { if (argc >= 3 && !strcmp (argv[2], "-v")) return VER_Object_Print (stdout, VERD_VERBOSE); else return VER_Object_Print (stdout, VERD_MINIMAL); } else if (!strcmp (argv[1], "--create")) { if (MSG_Library_Open (0, NULL, MSGD_CREATE | MSGD_DEBUG_ALL) != MSGS_OK) { fprintf (stderr, "=> Impossible de créer l'instance de la librairie LIBMSG\n"); return -1; } return 1; } else if (!strcmp (argv[1], "--destroy")) { if (MSG_Library_Open (0, NULL, MSGD_OPEN | MSGD_DEBUG_ALL) != MSGS_OK || MSG_Library_Close (MSGD_DESTROY) != MSGS_OK) { fprintf (stderr, "=> Impossible de détruire l'instance de la librairie LIBMSG\n"); return -1; } return 0; } else { fprintf (stderr, USAGE, argv[0]); return -1; } } /* Lancement du menu intercatif */ init_menu (); choice = print_menu (); while (choice != QUIT) { strcpy (Answer, "yes"); switch (choice) { case BASE_INIT: fprintf (stdout, "\nReturn code = %s\n", \ MSG_Library_Open (0, NULL, MSGD_CREATE | MSGD_DEBUG_ALL) == MSGS_OK ? "OK" : "NOK" ); break; case BASE_OPEN: fprintf (stdout, "\nReturn code = %s\n", \ MSG_Library_Open (0, NULL, MSGD_OPEN | MSGD_DEBUG_ALL) == MSGS_OK ? "OK" : "NOK" ); break; case BASE_END: fprintf (stdout, "\nReturn code = %s\n", \ MSG_Library_Close (MSGD_DESTROY) == MSGS_OK ? "OK" : "NOK" ); break; case BASE_CLOSE: fprintf (stdout, "\nReturn code = %s\n", \ MSG_Library_Close (MSGD_CLOSE) == MSGS_OK ? "OK" : "NOK" ); break; case BASE_INFO: if (!MSG_Base) fprintf (stdout, "\nLIBMSG base must be opened first\n"); else DS_DataStruct_Info_Print (MSG_Base->Port_List, stdout); break; case SHOW_PORT_LIST: if (!MSG_Base) fprintf (stdout, "\nLIBMSG base must be opened first\n"); else DS_DataStruct_Print (MSG_Base->Port_List, stdout); break; case PORT_CREATE: if (!MSG_Base) fprintf (stdout, "\nLIBMSG base must be opened first\n"); else { fprintf (stdout, "\nPort name ? "); gets (tmp); if (MSG_Port_Open (tmp, &Port, MSGD_CREATE) != MSGS_OK) fprintf (stdout, "Unable to create port \"%s\"\n", tmp); } break; case PORT_DUMP: if (!MSG_Base) fprintf (stdout, "\nLIBMSG base must be opened first\n"); else { fprintf (stdout, "\nPort name ? "); gets (tmp); if (MSG_Port_Open (tmp, &Port, MSGD_OPEN) != MSGS_OK) fprintf (stdout, "Unable to open port \"%s\"\n", tmp); else { ND_Manager_Exec (MSG_Base->Port_List->Manager, NDD_CMD_PRINT_VALUE, Port, stdout); if (fprintf (stdout, "\n\nShow message queue ? (y/n) ") && gets (tmp) && *tmp == 'y') DS_DataStruct_Print (Port->MsgQueue, stdout); if (fprintf (stdout, "\nShow semaphore list ? (y/n) ") && gets (tmp) && *tmp == 'y') DS_DataStruct_Print (Port->SemList, stdout); } } break; case PORT_FLUSH: if (!MSG_Base) fprintf (stdout, "\nLIBMSG base must be opened first\n"); else { fprintf (stdout, "\nPort name ? "); gets (tmp); if (MSG_Port_Open (tmp, &Port, MSGD_OPEN) != MSGS_OK) fprintf (stdout, "Unable to open port \"%s\"\n", tmp); /* Retrait de tous les messages du port */ while (MSG_Message_Receive (Port, MSGD_NO_TYPE, &Msg, MSGD_NO_WAIT) == MSGS_OK) MSG_Message_Free (Msg); } break; case PORT_DELETE: if (!MSG_Base) fprintf (stdout, "\nLIBMSG base must be opened first\n"); else { fprintf (stdout, "\nPort name ? "); gets (tmp); if (MSG_Port_Exist (tmp, &Port) != MSGS_OK) { fprintf (stdout, "Port \"%s\" does not exist\n", tmp); break; } if (MSG_Port_Close (Port, MSGD_DESTROY) != MSGS_OK) fprintf (stdout, "Unable to delete port \"%s\"\n", tmp); } break; case QUIT: fprintf (stdout, "\nExiting program ...\n"); break; default: fprintf (stdout, "\nUndefined choice %d\n", choice); } choice = print_menu (); } return 0; } void init_menu (void) { sprintf (menu, "Menu :\n"); sprintf (tmp, " - %2d) %-25s", QUIT, "Quit"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s\n", BASE_INIT, "Init library"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s", BASE_OPEN, "Open library"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s\n", BASE_INFO, "Info library"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s", BASE_CLOSE, "Close library"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s\n", BASE_END, "End library"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s", SHOW_PORT_LIST, "Show message ports"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s\n", PORT_CREATE, "Create a message port"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s", PORT_DUMP, "Dump a message port"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s\n", PORT_FLUSH, "Flush all message from a port"); strcat (menu, tmp); sprintf (tmp, " - %2d) %-25s\n", PORT_DELETE, "Delete a message port"); strcat (menu, tmp); } int print_menu (void) { fprintf (stdout, "---------------------------------------------------------------------------\n%s", menu); tmp[0] = '\0'; while (tmp[0] == '\0') { printf ("\nChoice ? "); gets (tmp); } return atoi (tmp); }