1 /****************************************************************************
3 * $Id: linphonec.c,v 1.57 2007/11/14 13:40:27 smorlat Exp $
5 * Copyright (C) 2006 Sandro Santilli <strk@keybit.net>
6 * Copyright (C) 2002 Florian Winterstein <flox@gmx.net>
7 * Copyright (C) 2000 Simon MORLAT <simon.morlat@free.fr>
9 ****************************************************************************
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ****************************************************************************/
29 #include <sys/types.h>
33 #include "private.h" /*coreapi/private.h, needed for LINPHONE_VERSION */
38 #include <linphonecore.h>
40 #include "linphonec.h"
49 #include <sys/socket.h>
55 #if defined(_WIN32_WCE)
57 #if !defined(PATH_MAX)
62 #define strdup _strdup
70 #define _(String) gettext(String)
73 #define _(something) (something)
77 #define PACKAGE_DIR ""
80 /***************************************************************************
84 ***************************************************************************/
87 LinphoneAuthInfo *elem[MAX_PENDING_AUTH];
91 /***************************************************************************
93 * Forward declarations
95 ***************************************************************************/
97 char *lpc_strip_blanks(char *input);
99 static int handle_configfile_migration(void);
100 #if !defined(_WIN32_WCE)
101 static int copy_file(const char *from, const char *to);
102 #endif /*_WIN32_WCE*/
103 static int linphonec_parse_cmdline(int argc, char **argv);
104 static int linphonec_init(int argc, char **argv);
105 static int linphonec_main_loop (LinphoneCore * opm, char * sipAddr);
106 static int linphonec_idle_call (void);
108 static int linphonec_initialize_readline(void);
109 static int linphonec_finish_readline();
110 static char **linephonec_readline_completion(const char *text,
114 /* These are callback for linphone core */
115 static void linphonec_call_received(LinphoneCore *lc, const char *from);
116 static void linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm,
117 const char *username);
118 static void linphonec_display_something (LinphoneCore * lc, const char *something);
119 static void linphonec_display_url (LinphoneCore * lc, const char *something, const char *url);
120 static void linphonec_display_warning (LinphoneCore * lc, const char *something);
121 static void stub () {}
122 static void linphonec_notify_received(LinphoneCore *lc,LinphoneFriend *fid,
123 const char *from, const char *status, const char *img);
124 static void linphonec_new_unknown_subscriber(LinphoneCore *lc,
125 LinphoneFriend *lf, const char *url);
126 static void linphonec_bye_received(LinphoneCore *lc, const char *from);
127 static void linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
128 const char *from, const char *msg);
129 static void linphonec_display_status (LinphoneCore * lc, const char *something);
130 static void linphonec_general_state (LinphoneCore * lc, LinphoneGeneralState *gstate);
131 static void linphonec_dtmf_received(LinphoneCore *lc, int dtmf);
132 static void print_prompt(LinphoneCore *opm);
133 /***************************************************************************
137 ***************************************************************************/
139 LinphoneCore *linphonec;
142 static char *histfile_name=NULL;
143 static char last_in_history[256];
145 //auto answer (-a) option
146 static bool_t auto_answer=FALSE;
147 static bool_t answer_call=FALSE;
148 static bool_t vcap_enabled=FALSE;
149 static bool_t display_enabled=FALSE;
150 static bool_t preview_enabled=FALSE;
151 static bool_t show_general_state=FALSE;
152 static bool_t unix_socket=FALSE;
153 static bool_t linphonec_running=TRUE;
154 LPC_AUTH_STACK auth_stack;
155 static int trace_level = 0;
156 static char *logfile_name = NULL;
157 static char configfile_name[PATH_MAX];
158 static char *sipAddr = NULL; /* for autocall */
159 #if !defined(_WIN32_WCE)
160 static ortp_pipe_t client_sock=ORTP_PIPE_INVALID;
161 #endif /*_WIN32_WCE*/
162 char prompt[PROMPT_MAX_LEN];
163 #if !defined(_WIN32_WCE)
164 static ortp_thread_t pipe_reader_th;
165 static bool_t pipe_reader_run=FALSE;
166 #endif /*_WIN32_WCE*/
167 #if !defined(_WIN32_WCE)
168 static ortp_pipe_t server_sock;
169 #endif /*_WIN32_WCE*/
172 LinphoneCoreVTable linphonec_vtable
173 #if !defined (_MSC_VER)
175 .show =(ShowInterfaceCb) stub,
176 .inv_recv = linphonec_call_received,
177 .bye_recv = linphonec_bye_received,
178 .notify_recv = linphonec_notify_received,
179 .new_unknown_subscriber = linphonec_new_unknown_subscriber,
180 .auth_info_requested = linphonec_prompt_for_auth,
181 .display_status = linphonec_display_status,
182 .display_message=linphonec_display_something,
183 #ifdef VINCENT_MAURY_RSVP
184 /* the yes/no dialog box */
185 .display_yes_no= (DisplayMessageCb) stub,
187 .display_warning=linphonec_display_warning,
188 .display_url=linphonec_display_url,
189 .display_question=(DisplayQuestionCb)stub,
190 .text_received=linphonec_text_received,
191 .general_state=linphonec_general_state,
192 .dtmf_received=linphonec_dtmf_received
194 #endif /*_WIN32_WCE*/
199 /***************************************************************************
201 * Linphone core callbacks
203 ***************************************************************************/
206 * Linphone core callback
209 linphonec_display_something (LinphoneCore * lc, const char *something)
211 fprintf (stdout, "%s\n%s", something,prompt);
216 * Linphone core callback
219 linphonec_display_status (LinphoneCore * lc, const char *something)
221 fprintf (stdout, "%s\n%s", something,prompt);
226 * Linphone core callback
229 linphonec_display_warning (LinphoneCore * lc, const char *something)
231 fprintf (stdout, "Warning: %s\n%s", something,prompt);
236 * Linphone core callback
239 linphonec_display_url (LinphoneCore * lc, const char *something, const char *url)
241 fprintf (stdout, "%s : %s\n", something, url);
246 * Linphone core callback
249 linphonec_call_received(LinphoneCore *lc, const char *from)
251 linphonec_set_caller(from);
258 * Linphone core callback
261 linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm, const char *username)
263 /* no prompt possible when using pipes or tcp mode*/
265 linphone_core_abort_authentication(lc,NULL);
267 LinphoneAuthInfo *pending_auth;
269 if ( auth_stack.nitems+1 > MAX_PENDING_AUTH )
272 "Can't accept another authentication request.\n"
273 "Consider incrementing MAX_PENDING_AUTH macro.\n");
277 pending_auth=linphone_auth_info_new(username,NULL,NULL,NULL,realm);
278 auth_stack.elem[auth_stack.nitems++]=pending_auth;
283 * Linphone core callback
286 linphonec_notify_received(LinphoneCore *lc,LinphoneFriend *fid,
287 const char *from, const char *status, const char *img)
289 printf("Friend %s is %s\n", from, status);
290 // todo: update Friend list state (unimplemented)
294 * Linphone core callback
297 linphonec_new_unknown_subscriber(LinphoneCore *lc, LinphoneFriend *lf,
300 printf("Friend %s requested subscription "
301 "(accept/deny is not implemented yet)\n", url);
302 // This means that this person wishes to be notified
303 // of your presence information (online, busy, away...).
308 * Linphone core callback
311 linphonec_bye_received(LinphoneCore *lc, const char *from)
313 // Should change prompt back to original maybe
315 // printing this is unneeded as we'd get a "Communication ended"
316 // message trough display_status callback anyway
317 //printf("Bye received from %s\n", from);
321 * Linphone core callback
324 linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
325 const char *from, const char *msg)
327 printf("%s: %s\n", from, msg);
328 // TODO: provide mechanism for answering.. ('say' command?)
332 static void linphonec_dtmf_received(LinphoneCore *lc, int dtmf){
333 fprintf(stdout,"Receiving tone %c\n",dtmf);
338 linphonec_general_state (LinphoneCore * lc, LinphoneGeneralState *gstate)
340 if (show_general_state) {
341 switch(gstate->new_state) {
342 case GSTATE_POWER_OFF:
343 printf("GSTATE_POWER_OFF");
345 case GSTATE_POWER_STARTUP:
346 printf("GSTATE_POWER_STARTUP");
348 case GSTATE_POWER_ON:
349 printf("GSTATE_POWER_ON");
351 case GSTATE_POWER_SHUTDOWN:
352 printf("GSTATE_POWER_SHUTDOWN");
354 case GSTATE_REG_NONE:
355 printf("GSTATE_REG_NONE");
358 printf("GSTATE_REG_OK");
360 case GSTATE_REG_FAILED:
361 printf("GSTATE_REG_FAILED");
363 case GSTATE_CALL_IDLE:
364 printf("GSTATE_CALL_IDLE");
366 case GSTATE_CALL_OUT_INVITE:
367 printf("GSTATE_CALL_OUT_INVITE");
369 case GSTATE_CALL_OUT_CONNECTED:
370 printf("GSTATE_CALL_OUT_CONNECTED");
372 case GSTATE_CALL_IN_INVITE:
373 printf("GSTATE_CALL_IN_INVITE");
375 case GSTATE_CALL_IN_CONNECTED:
376 printf("GSTATE_CALL_IN_CONNECTED");
378 case GSTATE_CALL_END:
379 printf("GSTATE_CALL_END");
381 case GSTATE_CALL_ERROR:
382 printf("GSTATE_CALL_ERROR");
385 printf("GSTATE_UNKNOWN_%d",gstate->new_state);
387 if (gstate->message) printf(" %s", gstate->message);
392 static char received_prompt[PROMPT_MAX_LEN];
393 static ms_mutex_t prompt_mutex;
394 static bool_t have_prompt=FALSE;
396 static void *prompt_reader_thread(void *arg){
398 char tmp[PROMPT_MAX_LEN];
399 while ((ret=fgets(tmp,sizeof(tmp),stdin))!=NULL){
400 ms_mutex_lock(&prompt_mutex);
401 strcpy(received_prompt,ret);
403 ms_mutex_unlock(&prompt_mutex);
408 static void start_prompt_reader(void){
410 ms_mutex_init(&prompt_mutex,NULL);
411 ortp_thread_create(&th,NULL,prompt_reader_thread,NULL);
413 #if !defined(_WIN32_WCE)
414 static ortp_pipe_t create_server_socket(void){
417 snprintf(path,sizeof(path)-1,"linphonec-%i",getuid());
421 DWORD size=sizeof(username)-1;
422 GetUserName(username,&size);
423 snprintf(path,sizeof(path)-1,"linphonec-%s",username);
426 return ortp_server_pipe_create(path);
430 static void *pipe_thread(void*p){
432 server_sock=create_server_socket();
433 if (server_sock==ORTP_PIPE_INVALID) return NULL;
434 while(pipe_reader_run){
435 while(client_sock!=ORTP_PIPE_INVALID){ /*sleep until the last command is finished*/
442 client_sock=ortp_server_pipe_accept_client(server_sock);
443 if (client_sock!=ORTP_PIPE_INVALID){
445 /*now read from the client */
446 if ((len=ortp_pipe_read(client_sock,(uint8_t*)tmp,sizeof(tmp)-1))>0){
447 ortp_mutex_lock(&prompt_mutex);
449 strcpy(received_prompt,tmp);
450 printf("Receiving command '%s'\n",received_prompt);fflush(stdout);
452 ortp_mutex_unlock(&prompt_mutex);
454 printf("read nothing\n");fflush(stdout);
455 ortp_server_pipe_close_client(client_sock);
456 client_sock=ORTP_PIPE_INVALID;
460 if (pipe_reader_run) fprintf(stderr,"accept() failed: %s\n",strerror(errno));
463 ms_message("Exiting pipe_reader_thread.");
468 static void start_pipe_reader(void){
469 ms_mutex_init(&prompt_mutex,NULL);
470 pipe_reader_run=TRUE;
471 ortp_thread_create(&pipe_reader_th,NULL,pipe_thread,NULL);
474 static void stop_pipe_reader(void){
475 pipe_reader_run=FALSE;
476 linphonec_command_finished();
477 ortp_server_pipe_close(server_sock);
478 ortp_thread_join(pipe_reader_th,NULL);
480 #endif /*_WIN32_WCE*/
483 #define BOOL_HAVE_READLINE 1
485 #define BOOL_HAVE_READLINE 0
488 char *linphonec_readline(char *prompt){
489 if (unix_socket || !BOOL_HAVE_READLINE ){
490 static bool_t prompt_reader_started=FALSE;
491 static bool_t pipe_reader_started=FALSE;
492 if (!prompt_reader_started){
493 start_prompt_reader();
494 prompt_reader_started=TRUE;
496 if (unix_socket && !pipe_reader_started){
497 #if !defined(_WIN32_WCE)
499 pipe_reader_started=TRUE;
500 #endif /*_WIN32_WCE*/
502 fprintf(stdout,"%s",prompt);
505 ms_mutex_lock(&prompt_mutex);
507 char *ret=strdup(received_prompt);
509 ms_mutex_unlock(&prompt_mutex);
512 ms_mutex_unlock(&prompt_mutex);
513 linphonec_idle_call();
516 /* Following is to get the video window going as it
517 should. Maybe should we only have this on when the option -V
521 if (PeekMessage(&msg, NULL, 0, 0,1)) {
522 TranslateMessage(&msg);
523 DispatchMessage(&msg);
531 return readline(prompt);
536 void linphonec_out(const char *fmt,...){
539 va_start (args, fmt);
540 res=ortp_strdup_vprintf(fmt,args);
544 #if !defined(_WIN32_WCE)
545 if (client_sock!=ORTP_PIPE_INVALID){
546 if (ortp_pipe_write(client_sock,(uint8_t*)res,strlen(res))==-1){
547 fprintf(stderr,"Fail to send output via pipe: %s",strerror(errno));
550 #endif /*_WIN32_WCE*/
554 void linphonec_command_finished(void){
555 #if !defined(_WIN32_WCE)
556 if (client_sock!=ORTP_PIPE_INVALID){
557 ortp_server_pipe_close_client(client_sock);
558 client_sock=ORTP_PIPE_INVALID;
560 #endif /*_WIN32_WCE*/
563 void linphonec_set_autoanswer(bool_t enabled){
567 bool_t linphonec_get_autoanswer(){
571 /***************************************************************************/
577 * - char *histfile_name
580 #if defined (_WIN32_WCE)
582 char **convert_args_to_ascii(int argc, _TCHAR **wargv){
584 char **result=malloc(argc*sizeof(char*));
587 wcstombs(argtmp,wargv[i],sizeof(argtmp));
588 result[i]=strdup(argtmp);
593 int _tmain(int argc, _TCHAR* wargv[]) {
594 char **argv=convert_args_to_ascii(argc,wargv);
596 linphonec_vtable.show =(ShowInterfaceCb) stub;
597 linphonec_vtable.inv_recv = linphonec_call_received;
598 linphonec_vtable.bye_recv = linphonec_bye_received;
599 linphonec_vtable.notify_recv = linphonec_notify_received;
600 linphonec_vtable.new_unknown_subscriber = linphonec_new_unknown_subscriber;
601 linphonec_vtable.auth_info_requested = linphonec_prompt_for_auth;
602 linphonec_vtable.display_status = linphonec_display_status;
603 linphonec_vtable.display_message=linphonec_display_something;
604 #ifdef VINCENT_MAURY_RSVP
605 /* the yes/no dialog box */
606 linphonec_vtable.display_yes_no= (DisplayMessageCb) stub;
608 linphonec_vtable.display_warning=linphonec_display_warning;
609 linphonec_vtable.display_url=linphonec_display_url;
610 linphonec_vtable.display_question=(DisplayQuestionCb)stub;
611 linphonec_vtable.text_received=linphonec_text_received;
612 linphonec_vtable.general_state=linphonec_general_state;
613 linphonec_vtable.dtmf_received=linphonec_dtmf_received;
617 main (int argc, char *argv[]) {
621 if (! linphonec_init(argc, argv) ) exit(EXIT_FAILURE);
623 linphonec_main_loop (linphonec, sipAddr);
625 linphonec_finish(EXIT_SUCCESS);
627 exit(EXIT_SUCCESS); /* should never reach here */
631 * Initialize linphonec
634 linphonec_init(int argc, char **argv)
637 //g_mem_set_vtable(&dbgtable);
640 * Set initial values for global variables
646 snprintf(configfile_name, PATH_MAX, "%s/.linphonerc",
648 #elif defined(_WIN32_WCE)
649 strncpy(configfile_name,PACKAGE_DIR "\\linphonerc",PATH_MAX);
650 mylogfile=fopen(PACKAGE_DIR "\\" "linphonec.log","w");
651 printf("Logs are redirected in" PACKAGE_DIR "\\linphonec.log");
653 snprintf(configfile_name, PATH_MAX, "%s/Linphone/linphonerc",
656 /* Handle configuration filename changes */
657 switch (handle_configfile_migration())
659 case -1: /* error during file copies */
661 "Error in configuration file migration\n");
664 case 0: /* nothing done */
665 case 1: /* migrated */
671 if (NULL == bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR))
672 perror ("bindtextdomain failed");
674 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
676 textdomain (GETTEXT_PACKAGE);
678 printf ("NLS disabled.\n");
681 linphonec_parse_cmdline(argc, argv);
685 if (logfile_name != NULL)
686 mylogfile = fopen (logfile_name, "w+");
688 if (mylogfile == NULL)
692 "INFO: no logfile, logging to stdout\n");
694 linphone_core_enable_logs(mylogfile);
698 linphone_core_disable_logs();
701 * Initialize auth stack
706 * Initialize linphone core
708 linphonec=linphone_core_new (&linphonec_vtable, configfile_name, NULL,
710 linphone_core_enable_video(linphonec,vcap_enabled,display_enabled);
711 linphone_core_enable_video_preview(linphonec,preview_enabled);
712 if (!(vcap_enabled || display_enabled)) printf("Warning: video is disabled in linphonec, use -V or -C or -D to enable.\n");
715 * Initialize readline
717 linphonec_initialize_readline();
719 #if !defined(_WIN32_WCE)
721 * Initialize signal handlers
723 signal(SIGTERM, linphonec_finish);
724 signal(SIGINT, linphonec_finish);
725 #endif /*_WIN32_WCE*/
730 void linphonec_main_loop_exit(void){
731 linphonec_running=FALSE;
735 * Close linphonec, cleanly terminating
739 linphonec_finish(int exit_status)
741 printf("Terminating...\n");
743 /* Terminate any pending call */
744 linphonec_parse_command_line(linphonec, "terminate");
745 linphonec_command_finished();
747 linphonec_finish_readline();
749 #if !defined(_WIN32_WCE)
752 #endif /*_WIN32_WCE*/
754 linphone_core_destroy (linphonec);
756 if (mylogfile != NULL && mylogfile != stdout)
766 * This is called from idle_call() whenever
767 * pending_auth != NULL.
769 * It prompts user for a password.
770 * Hitting ^D (EOF) would make this function
772 * Any other input would try to set linphone core
773 * auth_password for the pending_auth, add the auth_info
777 linphonec_prompt_for_auth_final(LinphoneCore *lc)
780 char auth_prompt[256];
782 rl_hook_func_t *old_event_hook;
784 LinphoneAuthInfo *pending_auth=auth_stack.elem[auth_stack.nitems-1];
786 snprintf(auth_prompt, 256, "Password for %s on %s: ",
787 pending_auth->username, pending_auth->realm);
792 * Disable event hook to avoid entering an
793 * infinite loop. This would prevent idle_call
794 * from being called during authentication reads.
795 * Note that it might be undesiderable...
797 old_event_hook=rl_event_hook;
803 input=linphonec_readline(auth_prompt);
806 * If EOF (^D) is sent you probably don't want
807 * to provide an auth password... should give up
808 * the operation, but there's no mechanism to
809 * send this info back to caller currently...
813 printf("Cancel requested, but not implemented.\n");
818 iptr=lpc_strip_blanks(input);
821 * Only blanks, continue asking
829 /* Something typed, let's try */
834 * No check is done here to ensure password is correct.
835 * I guess password will be asked again later.
837 linphone_auth_info_set_passwd(pending_auth, input);
838 linphone_core_add_auth_info(lc, pending_auth);
839 linphone_auth_info_destroy(pending_auth);
840 auth_stack.elem[auth_stack.nitems-1]=0;
841 --(auth_stack.nitems);
844 * Reset line_buffer, to avoid the password
845 * to be used again from outer readline
847 rl_line_buffer[0]='\0';
848 rl_event_hook=old_event_hook;
854 print_usage (int exit_status)
856 fprintf (stdout, "\n\
857 usage: linphonec [-c file] [-s sipaddr] [-a] [-V] [-d level ] [-l logfile]\n\
860 -c file specify path of configuration file.\n\
861 -d level be verbose. 0 is no output. 6 is all output\n\
862 -l logfile specify the log file for your SIP phone\n\
863 -s sipaddress specify the sip call to do at startup\n\
864 -a enable auto answering for incoming calls\n\
865 -V enable video features globally (disabled by default)\n\
866 -C enable video capture only (disabled by default)\n\
867 -D enable video display only (disabled by default)\n\
868 -S show general state messages (disabled by default)\n\
869 -v or --version display version and exits.\n");
877 * Called every second from main read loop.
879 * Will use the following globals:
881 * - LinphoneCore linphonec
882 * - LPC_AUTH_STACK auth_stack;
886 linphonec_idle_call ()
888 LinphoneCore *opm=linphonec;
890 /* Uncomment the following to verify being called */
893 linphone_core_iterate(opm);
895 fprintf (stdout, "-------auto answering to call-------\n" );
896 linphone_core_accept_call(opm,NULL);
900 if ( auth_stack.nitems )
903 * Inhibit command completion
904 * during password prompts
907 rl_inhibit_completion=1;
909 linphonec_prompt_for_auth_final(opm);
911 rl_inhibit_completion=0;
922 * - char *histfile_name (also sets this)
923 * - char *last_in_history (allocates it)
926 linphonec_initialize_readline()
928 /*rl_bind_key('\t', rl_insert);*/
930 /* Allow conditional parsing of ~/.inputrc */
931 rl_readline_name = "linphonec";
933 /* Call idle_call() every second */
934 rl_set_keyboard_input_timeout(LPC_READLINE_TIMEOUT);
935 rl_event_hook=linphonec_idle_call;
937 /* Set history file and read it */
938 histfile_name = ms_strdup_printf ("%s/.linphonec_history",
940 read_history(histfile_name);
942 /* Initialized last_in_history cache*/
943 last_in_history[0] = '\0';
945 /* Register a completion function */
946 rl_attempted_completion_function = linephonec_readline_completion;
948 /* printf("Readline initialized.\n"); */
956 * - char *histfile_name (writes history to file and frees it)
957 * - char *last_in_history (frees it)
961 linphonec_finish_readline()
964 stifle_history(HISTSIZE);
965 write_history(histfile_name);
973 static void print_prompt(LinphoneCore *opm){
974 #ifdef IDENTITY_AS_PROMPT
975 snprintf(prompt, PROMPT_MAX_LEN, "%s> ",
976 linphone_core_get_primary_contact(opm));
978 snprintf(prompt, PROMPT_MAX_LEN, "linphonec> ");
983 linphonec_main_loop (LinphoneCore * opm, char * sipAddr)
985 char buf[LINE_MAX_LEN]; /* auto call handling */
991 /* auto call handling */
992 if (sipAddr != NULL )
994 snprintf (buf, sizeof(buf),"call %s", sipAddr);
995 linphonec_parse_command_line(linphonec, buf);
998 while (linphonec_running && (input=linphonec_readline(prompt)))
1000 char *iptr; /* input and input pointer */
1004 iptr=lpc_strip_blanks(input);
1006 input_len = strlen(iptr);
1009 * Do nothing but release memory
1010 * if only blanks are read
1018 #ifdef HAVE_READLINE
1020 * Only add to history if not already
1021 * last item in it, and only if the command
1022 * doesn't start with a space (to allow for
1025 if ( iptr == input && strcmp(last_in_history, iptr) )
1027 strncpy(last_in_history,iptr,sizeof(last_in_history));
1028 last_in_history[sizeof(last_in_history)-1]='\0';
1033 linphonec_parse_command_line(linphonec, iptr);
1034 linphonec_command_finished();
1042 * Parse command line switches
1047 * - char *logfile_name
1048 * - char *configfile_name
1052 linphonec_parse_cmdline(int argc, char **argv)
1056 while (arg_num < argc)
1058 int old_arg_num = arg_num;
1059 if (strncmp ("-d", argv[arg_num], 2) == 0)
1063 trace_level = atoi (argv[arg_num]);
1067 else if (strncmp ("-l", argv[arg_num], 2) == 0)
1071 logfile_name = argv[arg_num];
1073 else if (strncmp ("-c", argv[arg_num], 2) == 0)
1075 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1076 #if !defined(_WIN32_WCE)
1077 if (access(argv[arg_num],F_OK)!=0 )
1080 "Cannot open config file %s.\n",
1084 #endif /*_WIN32_WCE*/
1085 snprintf(configfile_name, PATH_MAX, "%s", argv[arg_num]);
1087 else if (strncmp ("-s", argv[arg_num], 2) == 0)
1091 sipAddr = argv[arg_num];
1093 else if (strncmp ("-a", argv[arg_num], 2) == 0)
1097 else if (strncmp ("-C", argv[arg_num], 2) == 0)
1099 vcap_enabled = TRUE;
1101 else if (strncmp ("-D", argv[arg_num], 2) == 0)
1103 display_enabled = TRUE;
1105 else if (strncmp ("-V", argv[arg_num], 2) == 0)
1107 display_enabled = TRUE;
1108 vcap_enabled = TRUE;
1109 preview_enabled=TRUE;
1111 else if ((strncmp ("-v", argv[arg_num], 2) == 0)
1114 ("--version", argv[arg_num],
1115 strlen ("--version")) == 0))
1117 #if !defined(_WIN32_WCE)
1118 printf ("version: " LINPHONE_VERSION "\n");
1120 exit (EXIT_SUCCESS);
1122 else if (strncmp ("-S", argv[arg_num], 2) == 0)
1124 show_general_state = TRUE;
1126 else if (strncmp ("--pipe", argv[arg_num], 6) == 0)
1130 else if (old_arg_num == arg_num)
1132 fprintf (stderr, "ERROR: bad arguments\n");
1133 print_usage (EXIT_FAILURE);
1142 * Up to version 1.2.1 linphone used ~/.linphonec for
1143 * CLI and ~/.gnome2/linphone for GUI as configuration file.
1144 * In newer version both interfaces will use ~/.linphonerc.
1146 * This function helps transparently migrating from one
1147 * to the other layout using the following heuristic:
1149 * IF new_config EXISTS => do nothing
1150 * ELSE IF old_cli_config EXISTS => copy to new_config
1151 * ELSE IF old_gui_config EXISTS => copy to new_config
1154 * 0 if it did nothing
1155 * 1 if it migrated successfully
1159 handle_configfile_migration()
1161 #if !defined(_WIN32_WCE)
1165 #if !defined(_WIN32_WCE)
1166 const char *home = getenv("HOME");
1168 const char *home = ".";
1169 #endif /*_WIN32_WCE*/
1170 new_cfg = ms_strdup_printf("%s/.linphonerc", home);
1173 * If the *NEW* configuration already exists
1176 if (access(new_cfg,F_OK)==0)
1182 old_cfg_cli = ms_strdup_printf("%s/.linphonec", home);
1185 * If the *OLD* CLI configurations exist copy it to
1186 * the new file and make it a symlink.
1188 if (access(old_cfg_cli, F_OK)==0)
1190 if ( ! copy_file(old_cfg_cli, new_cfg) )
1196 printf("%s copied to %s\n", old_cfg_cli, new_cfg);
1203 old_cfg_gui = ms_strdup_printf("%s/.gnome2/linphone", home);
1206 * If the *OLD* GUI configurations exist copy it to
1207 * the new file and make it a symlink.
1209 if (access(old_cfg_gui, F_OK)==0)
1211 if ( ! copy_file(old_cfg_gui, new_cfg) )
1218 printf("%s copied to %s\n", old_cfg_gui, new_cfg);
1226 #endif /*_WIN32_WCE*/
1229 #if !defined(_WIN32_WCE)
1231 * Copy file "from" to file "to".
1232 * Destination file is truncated if existing.
1233 * Return 1 on success, 0 on error (printing an error).
1236 copy_file(const char *from, const char *to)
1243 /* Open "from" file for reading */
1244 in=fopen(from, "r");
1247 snprintf(message, 255, "Can't open %s for reading: %s\n",
1248 from, strerror(errno));
1249 fprintf(stderr, "%s", message);
1253 /* Open "to" file for writing (will truncate existing files) */
1257 snprintf(message, 255, "Can't open %s for writing: %s\n",
1258 to, strerror(errno));
1259 fprintf(stderr, "%s", message);
1263 /* Copy data from "in" to "out" */
1264 while ( (n=fread(buf, 1, sizeof buf, in)) > 0 )
1266 if ( ! fwrite(buf, 1, n, out) )
1277 #endif /*_WIN32_WCE*/
1279 #ifdef HAVE_READLINE
1281 linephonec_readline_completion(const char *text, int start, int end)
1283 char **matches = NULL;
1286 * Prevent readline from falling
1287 * back to filename-completion
1289 rl_attempted_completion_over=1;
1292 * If this is the start of line we complete with commands
1296 return rl_completion_matches(text, linphonec_command_generator);
1300 * Otherwise, we should peek at command name
1301 * or context to implement a smart completion.
1302 * For example: "call .." could return
1303 * friends' sip-uri as matches
1312 * Strip blanks from a string.
1313 * Return a pointer into the provided string.
1314 * Modifies input adding a NULL at first
1315 * of trailing blanks.
1318 lpc_strip_blanks(char *input)
1322 /* Find first non-blank */
1323 while(*input && isspace(*input)) ++input;
1325 /* Find last non-blank */
1326 iptr=input+strlen(input);
1328 while(isspace(*--iptr));
1335 /****************************************************************************
1337 * $Log: linphonec.c,v $
1338 * Revision 1.57 2007/11/14 13:40:27 smorlat
1339 * fix --disable-video build.
1341 * Revision 1.56 2007/09/26 14:07:27 fixkowalski
1342 * - ANSI/C++ compilation issues with non-GCC compilers
1343 * - Faster epm-based packaging
1344 * - Ability to build & run on FC6's eXosip/osip
1346 * Revision 1.55 2007/09/24 16:01:58 smorlat
1349 * Revision 1.54 2007/08/22 14:06:11 smorlat
1350 * authentication bugs fixed.
1352 * Revision 1.53 2007/02/13 21:31:01 smorlat
1353 * added patch for general state.
1354 * new doxygen for oRTP
1357 * Revision 1.52 2007/01/10 14:11:24 smorlat
1358 * add --video to linphonec.
1360 * Revision 1.51 2006/08/21 12:49:59 smorlat
1361 * merged several little patches.
1363 * Revision 1.50 2006/07/26 08:17:28 smorlat
1366 * Revision 1.49 2006/07/17 18:45:00 smorlat
1367 * support for several event queues in ortp.
1368 * glib dependency removed from coreapi/ and console/
1370 * Revision 1.48 2006/04/09 12:45:32 smorlat
1371 * linphonec improvements.
1373 * Revision 1.47 2006/04/04 08:04:34 smorlat
1374 * switched to mediastreamer2, most bugs fixed.
1376 * Revision 1.46 2006/03/16 17:17:40 smorlat
1379 * Revision 1.45 2006/03/12 21:48:31 smorlat
1380 * gcc-2.95 compile error fixed.
1381 * mediastreamer2 in progress
1383 * Revision 1.44 2006/03/04 11:17:10 smorlat
1384 * mediastreamer2 in progress.
1386 * Revision 1.43 2006/02/13 09:50:50 strk
1387 * fixed unused variable warning.
1389 * Revision 1.42 2006/02/02 15:39:18 strk
1390 * - Added 'friend list' and 'friend call' commands
1391 * - Allowed for multiple DTFM send in a single line
1392 * - Added status-specific callback (bare version)
1394 * Revision 1.41 2006/02/02 13:30:05 strk
1395 * - Padded vtable with missing callbacks
1396 * (fixing a segfault on friends subscription)
1397 * - Handled friends notify (bare version)
1398 * - Handled text messages receive (bare version)
1399 * - Printed message on subscription request (bare version)
1401 * Revision 1.40 2006/01/26 09:48:05 strk
1402 * Added limits.h include
1404 * Revision 1.39 2006/01/26 02:11:01 strk
1405 * Removed unused variables, fixed copyright date
1407 * Revision 1.38 2006/01/25 18:33:02 strk
1408 * Removed the -t swich, terminate_on_close made the default behaviour
1410 * Revision 1.37 2006/01/20 14:12:34 strk
1411 * Added linphonec_init() and linphonec_finish() functions.
1412 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1413 * Handling of auto-termination (-t) moved to linphonec_finish().
1414 * Reworked main (input read) loop to not rely on 'terminate'
1415 * and 'run' variable (dropped). configfile_name allocated on stack
1416 * using PATH_MAX limit. Changed print_usage signature to allow
1417 * for an exit_status specification.
1419 * Revision 1.36 2006/01/18 09:25:32 strk
1420 * Command completion inhibited in proxy addition and auth request prompts.
1421 * Avoided use of readline's internal filename completion.
1423 * Revision 1.35 2006/01/14 13:29:32 strk
1424 * Reworked commands interface to use a table structure,
1425 * used by command line parser and help function.
1426 * Implemented first level of completion (commands).
1427 * Added notification of invalid "answer" and "terminate"
1428 * commands (no incoming call, no active call).
1429 * Forbidden "call" intialization when a call is already active.
1430 * Cleaned up all commands, adding more feedback and error checks.
1432 * Revision 1.34 2006/01/13 13:00:29 strk
1433 * Added linphonec.h. Code layout change (added comments, forward decl,
1434 * globals on top, copyright notices and Logs). Handled out-of-memory
1435 * condition on history management. Removed assumption on sizeof(char).
1436 * Fixed bug in authentication prompt (introduced by readline).
1437 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1440 ****************************************************************************/