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 static void linphonec_new_unknown_subscriber(LinphoneCore *lc,
124 LinphoneFriend *lf, const char *url);
125 static void linphonec_bye_received(LinphoneCore *lc, const char *from);
126 static void linphonec_text_received(LinphoneCore *lc, LinphoneChatRoom *cr,
127 const char *from, const char *msg);
128 static void linphonec_display_status (LinphoneCore * lc, const char *something);
129 static void linphonec_general_state (LinphoneCore * lc, LinphoneGeneralState *gstate);
130 static void linphonec_dtmf_received(LinphoneCore *lc, int dtmf);
131 static void print_prompt(LinphoneCore *opm);
132 /***************************************************************************
136 ***************************************************************************/
138 LinphoneCore *linphonec;
141 static char *histfile_name=NULL;
142 static char last_in_history[256];
144 //auto answer (-a) option
145 static bool_t auto_answer=FALSE;
146 static bool_t answer_call=FALSE;
147 static bool_t vcap_enabled=FALSE;
148 static bool_t display_enabled=FALSE;
149 static bool_t preview_enabled=FALSE;
150 static bool_t show_general_state=FALSE;
151 static bool_t unix_socket=FALSE;
152 static bool_t linphonec_running=TRUE;
153 LPC_AUTH_STACK auth_stack;
154 static int trace_level = 0;
155 static char *logfile_name = NULL;
156 static char configfile_name[PATH_MAX];
157 static char *sipAddr = NULL; /* for autocall */
158 #if !defined(_WIN32_WCE)
159 static ortp_pipe_t client_sock=ORTP_PIPE_INVALID;
160 #endif /*_WIN32_WCE*/
161 char prompt[PROMPT_MAX_LEN];
162 #if !defined(_WIN32_WCE)
163 static ortp_thread_t pipe_reader_th;
164 static bool_t pipe_reader_run=FALSE;
165 #endif /*_WIN32_WCE*/
166 #if !defined(_WIN32_WCE)
167 static ortp_pipe_t server_sock;
168 #endif /*_WIN32_WCE*/
171 LinphoneCoreVTable linphonec_vtable
172 #if !defined (_MSC_VER)
174 .show =(ShowInterfaceCb) stub,
175 .inv_recv = linphonec_call_received,
176 .bye_recv = linphonec_bye_received,
177 .notify_recv = linphonec_notify_received,
178 .new_unknown_subscriber = linphonec_new_unknown_subscriber,
179 .auth_info_requested = linphonec_prompt_for_auth,
180 .display_status = linphonec_display_status,
181 .display_message=linphonec_display_something,
182 #ifdef VINCENT_MAURY_RSVP
183 /* the yes/no dialog box */
184 .display_yes_no= (DisplayMessageCb) stub,
186 .display_warning=linphonec_display_warning,
187 .display_url=linphonec_display_url,
188 .display_question=(DisplayQuestionCb)stub,
189 .text_received=linphonec_text_received,
190 .general_state=linphonec_general_state,
191 .dtmf_received=linphonec_dtmf_received
193 #endif /*_WIN32_WCE*/
198 /***************************************************************************
200 * Linphone core callbacks
202 ***************************************************************************/
205 * Linphone core callback
208 linphonec_display_something (LinphoneCore * lc, const char *something)
210 fprintf (stdout, "%s\n%s", something,prompt);
215 * Linphone core callback
218 linphonec_display_status (LinphoneCore * lc, const char *something)
220 fprintf (stdout, "%s\n%s", something,prompt);
225 * Linphone core callback
228 linphonec_display_warning (LinphoneCore * lc, const char *something)
230 fprintf (stdout, "Warning: %s\n%s", something,prompt);
235 * Linphone core callback
238 linphonec_display_url (LinphoneCore * lc, const char *something, const char *url)
240 fprintf (stdout, "%s : %s\n", something, url);
245 * Linphone core callback
248 linphonec_call_received(LinphoneCore *lc, const char *from)
250 linphonec_set_caller(from);
257 * Linphone core callback
260 linphonec_prompt_for_auth(LinphoneCore *lc, const char *realm, const char *username)
262 /* no prompt possible when using pipes or tcp mode*/
264 linphone_core_abort_authentication(lc,NULL);
266 LinphoneAuthInfo *pending_auth;
268 if ( auth_stack.nitems+1 > MAX_PENDING_AUTH )
271 "Can't accept another authentication request.\n"
272 "Consider incrementing MAX_PENDING_AUTH macro.\n");
276 pending_auth=linphone_auth_info_new(username,NULL,NULL,NULL,realm);
277 auth_stack.elem[auth_stack.nitems++]=pending_auth;
282 * Linphone core callback
285 linphonec_notify_received(LinphoneCore *lc,LinphoneFriend *fid)
287 char *tmp=linphone_address_as_string(linphone_friend_get_address(fid));
288 printf("Friend %s is %s\n", tmp, linphone_online_status_to_string(linphone_friend_get_status(fid)));
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();
522 return readline(prompt);
527 void linphonec_out(const char *fmt,...){
530 va_start (args, fmt);
531 res=ortp_strdup_vprintf(fmt,args);
535 #if !defined(_WIN32_WCE)
536 if (client_sock!=ORTP_PIPE_INVALID){
537 if (ortp_pipe_write(client_sock,(uint8_t*)res,strlen(res))==-1){
538 fprintf(stderr,"Fail to send output via pipe: %s",strerror(errno));
541 #endif /*_WIN32_WCE*/
545 void linphonec_command_finished(void){
546 #if !defined(_WIN32_WCE)
547 if (client_sock!=ORTP_PIPE_INVALID){
548 ortp_server_pipe_close_client(client_sock);
549 client_sock=ORTP_PIPE_INVALID;
551 #endif /*_WIN32_WCE*/
554 void linphonec_set_autoanswer(bool_t enabled){
558 bool_t linphonec_get_autoanswer(){
562 /***************************************************************************/
568 * - char *histfile_name
571 #if defined (_WIN32_WCE)
573 char **convert_args_to_ascii(int argc, _TCHAR **wargv){
575 char **result=malloc(argc*sizeof(char*));
578 wcstombs(argtmp,wargv[i],sizeof(argtmp));
579 result[i]=strdup(argtmp);
584 int _tmain(int argc, _TCHAR* wargv[]) {
585 char **argv=convert_args_to_ascii(argc,wargv);
587 linphonec_vtable.show =(ShowInterfaceCb) stub;
588 linphonec_vtable.inv_recv = linphonec_call_received;
589 linphonec_vtable.bye_recv = linphonec_bye_received;
590 linphonec_vtable.notify_recv = linphonec_notify_received;
591 linphonec_vtable.new_unknown_subscriber = linphonec_new_unknown_subscriber;
592 linphonec_vtable.auth_info_requested = linphonec_prompt_for_auth;
593 linphonec_vtable.display_status = linphonec_display_status;
594 linphonec_vtable.display_message=linphonec_display_something;
595 #ifdef VINCENT_MAURY_RSVP
596 /* the yes/no dialog box */
597 linphonec_vtable.display_yes_no= (DisplayMessageCb) stub;
599 linphonec_vtable.display_warning=linphonec_display_warning;
600 linphonec_vtable.display_url=linphonec_display_url;
601 linphonec_vtable.display_question=(DisplayQuestionCb)stub;
602 linphonec_vtable.text_received=linphonec_text_received;
603 linphonec_vtable.general_state=linphonec_general_state;
604 linphonec_vtable.dtmf_received=linphonec_dtmf_received;
608 main (int argc, char *argv[]) {
612 if (! linphonec_init(argc, argv) ) exit(EXIT_FAILURE);
614 linphonec_main_loop (linphonec, sipAddr);
616 linphonec_finish(EXIT_SUCCESS);
618 exit(EXIT_SUCCESS); /* should never reach here */
622 * Initialize linphonec
625 linphonec_init(int argc, char **argv)
628 //g_mem_set_vtable(&dbgtable);
631 * Set initial values for global variables
637 snprintf(configfile_name, PATH_MAX, "%s/.linphonerc",
639 #elif defined(_WIN32_WCE)
640 strncpy(configfile_name,PACKAGE_DIR "\\linphonerc",PATH_MAX);
641 mylogfile=fopen(PACKAGE_DIR "\\" "linphonec.log","w");
642 printf("Logs are redirected in" PACKAGE_DIR "\\linphonec.log");
644 snprintf(configfile_name, PATH_MAX, "%s/Linphone/linphonerc",
647 /* Handle configuration filename changes */
648 switch (handle_configfile_migration())
650 case -1: /* error during file copies */
652 "Error in configuration file migration\n");
655 case 0: /* nothing done */
656 case 1: /* migrated */
662 if (NULL == bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR))
663 perror ("bindtextdomain failed");
665 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
667 textdomain (GETTEXT_PACKAGE);
669 printf ("NLS disabled.\n");
672 linphonec_parse_cmdline(argc, argv);
676 if (logfile_name != NULL)
677 mylogfile = fopen (logfile_name, "w+");
679 if (mylogfile == NULL)
683 "INFO: no logfile, logging to stdout\n");
685 linphone_core_enable_logs(mylogfile);
689 linphone_core_disable_logs();
692 * Initialize auth stack
697 * Initialize linphone core
699 linphonec=linphone_core_new (&linphonec_vtable, configfile_name, NULL,
701 linphone_core_enable_video(linphonec,vcap_enabled,display_enabled);
702 linphone_core_enable_video_preview(linphonec,preview_enabled);
703 if (!(vcap_enabled || display_enabled)) printf("Warning: video is disabled in linphonec, use -V or -C or -D to enable.\n");
706 * Initialize readline
708 linphonec_initialize_readline();
710 #if !defined(_WIN32_WCE)
712 * Initialize signal handlers
714 signal(SIGTERM, linphonec_finish);
715 signal(SIGINT, linphonec_finish);
716 #endif /*_WIN32_WCE*/
721 void linphonec_main_loop_exit(void){
722 linphonec_running=FALSE;
726 * Close linphonec, cleanly terminating
730 linphonec_finish(int exit_status)
732 printf("Terminating...\n");
734 /* Terminate any pending call */
735 linphonec_parse_command_line(linphonec, "terminate");
736 linphonec_command_finished();
738 linphonec_finish_readline();
740 #if !defined(_WIN32_WCE)
743 #endif /*_WIN32_WCE*/
745 linphone_core_destroy (linphonec);
747 if (mylogfile != NULL && mylogfile != stdout)
757 * This is called from idle_call() whenever
758 * pending_auth != NULL.
760 * It prompts user for a password.
761 * Hitting ^D (EOF) would make this function
763 * Any other input would try to set linphone core
764 * auth_password for the pending_auth, add the auth_info
768 linphonec_prompt_for_auth_final(LinphoneCore *lc)
771 char auth_prompt[256];
773 rl_hook_func_t *old_event_hook;
775 LinphoneAuthInfo *pending_auth=auth_stack.elem[auth_stack.nitems-1];
777 snprintf(auth_prompt, 256, "Password for %s on %s: ",
778 pending_auth->username, pending_auth->realm);
783 * Disable event hook to avoid entering an
784 * infinite loop. This would prevent idle_call
785 * from being called during authentication reads.
786 * Note that it might be undesiderable...
788 old_event_hook=rl_event_hook;
794 input=linphonec_readline(auth_prompt);
797 * If EOF (^D) is sent you probably don't want
798 * to provide an auth password... should give up
799 * the operation, but there's no mechanism to
800 * send this info back to caller currently...
804 printf("Cancel requested, but not implemented.\n");
809 iptr=lpc_strip_blanks(input);
812 * Only blanks, continue asking
820 /* Something typed, let's try */
825 * No check is done here to ensure password is correct.
826 * I guess password will be asked again later.
828 linphone_auth_info_set_passwd(pending_auth, input);
829 linphone_core_add_auth_info(lc, pending_auth);
830 linphone_auth_info_destroy(pending_auth);
831 auth_stack.elem[auth_stack.nitems-1]=0;
832 --(auth_stack.nitems);
835 * Reset line_buffer, to avoid the password
836 * to be used again from outer readline
838 rl_line_buffer[0]='\0';
839 rl_event_hook=old_event_hook;
845 print_usage (int exit_status)
847 fprintf (stdout, "\n\
848 usage: linphonec [-c file] [-s sipaddr] [-a] [-V] [-d level ] [-l logfile]\n\
851 -c file specify path of configuration file.\n\
852 -d level be verbose. 0 is no output. 6 is all output\n\
853 -l logfile specify the log file for your SIP phone\n\
854 -s sipaddress specify the sip call to do at startup\n\
855 -a enable auto answering for incoming calls\n\
856 -V enable video features globally (disabled by default)\n\
857 -C enable video capture only (disabled by default)\n\
858 -D enable video display only (disabled by default)\n\
859 -S show general state messages (disabled by default)\n\
860 -v or --version display version and exits.\n");
868 * Called every second from main read loop.
870 * Will use the following globals:
872 * - LinphoneCore linphonec
873 * - LPC_AUTH_STACK auth_stack;
877 linphonec_idle_call ()
879 LinphoneCore *opm=linphonec;
881 /* Uncomment the following to verify being called */
884 linphone_core_iterate(opm);
886 fprintf (stdout, "-------auto answering to call-------\n" );
887 linphone_core_accept_call(opm,NULL);
891 if ( auth_stack.nitems )
894 * Inhibit command completion
895 * during password prompts
898 rl_inhibit_completion=1;
900 linphonec_prompt_for_auth_final(opm);
902 rl_inhibit_completion=0;
913 * - char *histfile_name (also sets this)
914 * - char *last_in_history (allocates it)
917 linphonec_initialize_readline()
919 /*rl_bind_key('\t', rl_insert);*/
921 /* Allow conditional parsing of ~/.inputrc */
922 rl_readline_name = "linphonec";
924 /* Call idle_call() every second */
925 rl_set_keyboard_input_timeout(LPC_READLINE_TIMEOUT);
926 rl_event_hook=linphonec_idle_call;
928 /* Set history file and read it */
929 histfile_name = ms_strdup_printf ("%s/.linphonec_history",
931 read_history(histfile_name);
933 /* Initialized last_in_history cache*/
934 last_in_history[0] = '\0';
936 /* Register a completion function */
937 rl_attempted_completion_function = linephonec_readline_completion;
939 /* printf("Readline initialized.\n"); */
947 * - char *histfile_name (writes history to file and frees it)
948 * - char *last_in_history (frees it)
952 linphonec_finish_readline()
955 stifle_history(HISTSIZE);
956 write_history(histfile_name);
964 static void print_prompt(LinphoneCore *opm){
965 #ifdef IDENTITY_AS_PROMPT
966 snprintf(prompt, PROMPT_MAX_LEN, "%s> ",
967 linphone_core_get_primary_contact(opm));
969 snprintf(prompt, PROMPT_MAX_LEN, "linphonec> ");
974 linphonec_main_loop (LinphoneCore * opm, char * sipAddr)
976 char buf[LINE_MAX_LEN]; /* auto call handling */
982 /* auto call handling */
983 if (sipAddr != NULL )
985 snprintf (buf, sizeof(buf),"call %s", sipAddr);
986 linphonec_parse_command_line(linphonec, buf);
989 while (linphonec_running && (input=linphonec_readline(prompt)))
991 char *iptr; /* input and input pointer */
995 iptr=lpc_strip_blanks(input);
997 input_len = strlen(iptr);
1000 * Do nothing but release memory
1001 * if only blanks are read
1009 #ifdef HAVE_READLINE
1011 * Only add to history if not already
1012 * last item in it, and only if the command
1013 * doesn't start with a space (to allow for
1016 if ( iptr == input && strcmp(last_in_history, iptr) )
1018 strncpy(last_in_history,iptr,sizeof(last_in_history));
1019 last_in_history[sizeof(last_in_history)-1]='\0';
1024 linphonec_parse_command_line(linphonec, iptr);
1025 linphonec_command_finished();
1033 * Parse command line switches
1038 * - char *logfile_name
1039 * - char *configfile_name
1043 linphonec_parse_cmdline(int argc, char **argv)
1047 while (arg_num < argc)
1049 int old_arg_num = arg_num;
1050 if (strncmp ("-d", argv[arg_num], 2) == 0)
1054 trace_level = atoi (argv[arg_num]);
1058 else if (strncmp ("-l", argv[arg_num], 2) == 0)
1062 logfile_name = argv[arg_num];
1064 else if (strncmp ("-c", argv[arg_num], 2) == 0)
1066 if ( ++arg_num >= argc ) print_usage(EXIT_FAILURE);
1067 #if !defined(_WIN32_WCE)
1068 if (access(argv[arg_num],F_OK)!=0 )
1071 "Cannot open config file %s.\n",
1075 #endif /*_WIN32_WCE*/
1076 snprintf(configfile_name, PATH_MAX, "%s", argv[arg_num]);
1078 else if (strncmp ("-s", argv[arg_num], 2) == 0)
1082 sipAddr = argv[arg_num];
1084 else if (strncmp ("-a", argv[arg_num], 2) == 0)
1088 else if (strncmp ("-C", argv[arg_num], 2) == 0)
1090 vcap_enabled = TRUE;
1092 else if (strncmp ("-D", argv[arg_num], 2) == 0)
1094 display_enabled = TRUE;
1096 else if (strncmp ("-V", argv[arg_num], 2) == 0)
1098 display_enabled = TRUE;
1099 vcap_enabled = TRUE;
1100 preview_enabled=TRUE;
1102 else if ((strncmp ("-v", argv[arg_num], 2) == 0)
1105 ("--version", argv[arg_num],
1106 strlen ("--version")) == 0))
1108 #if !defined(_WIN32_WCE)
1109 printf ("version: " LINPHONE_VERSION "\n");
1111 exit (EXIT_SUCCESS);
1113 else if (strncmp ("-S", argv[arg_num], 2) == 0)
1115 show_general_state = TRUE;
1117 else if (strncmp ("--pipe", argv[arg_num], 6) == 0)
1121 else if (old_arg_num == arg_num)
1123 fprintf (stderr, "ERROR: bad arguments\n");
1124 print_usage (EXIT_FAILURE);
1133 * Up to version 1.2.1 linphone used ~/.linphonec for
1134 * CLI and ~/.gnome2/linphone for GUI as configuration file.
1135 * In newer version both interfaces will use ~/.linphonerc.
1137 * This function helps transparently migrating from one
1138 * to the other layout using the following heuristic:
1140 * IF new_config EXISTS => do nothing
1141 * ELSE IF old_cli_config EXISTS => copy to new_config
1142 * ELSE IF old_gui_config EXISTS => copy to new_config
1145 * 0 if it did nothing
1146 * 1 if it migrated successfully
1150 handle_configfile_migration()
1152 #if !defined(_WIN32_WCE)
1156 #if !defined(_WIN32_WCE)
1157 const char *home = getenv("HOME");
1159 const char *home = ".";
1160 #endif /*_WIN32_WCE*/
1161 new_cfg = ms_strdup_printf("%s/.linphonerc", home);
1164 * If the *NEW* configuration already exists
1167 if (access(new_cfg,F_OK)==0)
1173 old_cfg_cli = ms_strdup_printf("%s/.linphonec", home);
1176 * If the *OLD* CLI configurations exist copy it to
1177 * the new file and make it a symlink.
1179 if (access(old_cfg_cli, F_OK)==0)
1181 if ( ! copy_file(old_cfg_cli, new_cfg) )
1187 printf("%s copied to %s\n", old_cfg_cli, new_cfg);
1194 old_cfg_gui = ms_strdup_printf("%s/.gnome2/linphone", home);
1197 * If the *OLD* GUI configurations exist copy it to
1198 * the new file and make it a symlink.
1200 if (access(old_cfg_gui, F_OK)==0)
1202 if ( ! copy_file(old_cfg_gui, new_cfg) )
1209 printf("%s copied to %s\n", old_cfg_gui, new_cfg);
1217 #endif /*_WIN32_WCE*/
1220 #if !defined(_WIN32_WCE)
1222 * Copy file "from" to file "to".
1223 * Destination file is truncated if existing.
1224 * Return 1 on success, 0 on error (printing an error).
1227 copy_file(const char *from, const char *to)
1234 /* Open "from" file for reading */
1235 in=fopen(from, "r");
1238 snprintf(message, 255, "Can't open %s for reading: %s\n",
1239 from, strerror(errno));
1240 fprintf(stderr, "%s", message);
1244 /* Open "to" file for writing (will truncate existing files) */
1248 snprintf(message, 255, "Can't open %s for writing: %s\n",
1249 to, strerror(errno));
1250 fprintf(stderr, "%s", message);
1254 /* Copy data from "in" to "out" */
1255 while ( (n=fread(buf, 1, sizeof buf, in)) > 0 )
1257 if ( ! fwrite(buf, 1, n, out) )
1268 #endif /*_WIN32_WCE*/
1270 #ifdef HAVE_READLINE
1272 linephonec_readline_completion(const char *text, int start, int end)
1274 char **matches = NULL;
1277 * Prevent readline from falling
1278 * back to filename-completion
1280 rl_attempted_completion_over=1;
1283 * If this is the start of line we complete with commands
1287 return rl_completion_matches(text, linphonec_command_generator);
1291 * Otherwise, we should peek at command name
1292 * or context to implement a smart completion.
1293 * For example: "call .." could return
1294 * friends' sip-uri as matches
1303 * Strip blanks from a string.
1304 * Return a pointer into the provided string.
1305 * Modifies input adding a NULL at first
1306 * of trailing blanks.
1309 lpc_strip_blanks(char *input)
1313 /* Find first non-blank */
1314 while(*input && isspace(*input)) ++input;
1316 /* Find last non-blank */
1317 iptr=input+strlen(input);
1319 while(isspace(*--iptr));
1326 /****************************************************************************
1328 * $Log: linphonec.c,v $
1329 * Revision 1.57 2007/11/14 13:40:27 smorlat
1330 * fix --disable-video build.
1332 * Revision 1.56 2007/09/26 14:07:27 fixkowalski
1333 * - ANSI/C++ compilation issues with non-GCC compilers
1334 * - Faster epm-based packaging
1335 * - Ability to build & run on FC6's eXosip/osip
1337 * Revision 1.55 2007/09/24 16:01:58 smorlat
1340 * Revision 1.54 2007/08/22 14:06:11 smorlat
1341 * authentication bugs fixed.
1343 * Revision 1.53 2007/02/13 21:31:01 smorlat
1344 * added patch for general state.
1345 * new doxygen for oRTP
1348 * Revision 1.52 2007/01/10 14:11:24 smorlat
1349 * add --video to linphonec.
1351 * Revision 1.51 2006/08/21 12:49:59 smorlat
1352 * merged several little patches.
1354 * Revision 1.50 2006/07/26 08:17:28 smorlat
1357 * Revision 1.49 2006/07/17 18:45:00 smorlat
1358 * support for several event queues in ortp.
1359 * glib dependency removed from coreapi/ and console/
1361 * Revision 1.48 2006/04/09 12:45:32 smorlat
1362 * linphonec improvements.
1364 * Revision 1.47 2006/04/04 08:04:34 smorlat
1365 * switched to mediastreamer2, most bugs fixed.
1367 * Revision 1.46 2006/03/16 17:17:40 smorlat
1370 * Revision 1.45 2006/03/12 21:48:31 smorlat
1371 * gcc-2.95 compile error fixed.
1372 * mediastreamer2 in progress
1374 * Revision 1.44 2006/03/04 11:17:10 smorlat
1375 * mediastreamer2 in progress.
1377 * Revision 1.43 2006/02/13 09:50:50 strk
1378 * fixed unused variable warning.
1380 * Revision 1.42 2006/02/02 15:39:18 strk
1381 * - Added 'friend list' and 'friend call' commands
1382 * - Allowed for multiple DTFM send in a single line
1383 * - Added status-specific callback (bare version)
1385 * Revision 1.41 2006/02/02 13:30:05 strk
1386 * - Padded vtable with missing callbacks
1387 * (fixing a segfault on friends subscription)
1388 * - Handled friends notify (bare version)
1389 * - Handled text messages receive (bare version)
1390 * - Printed message on subscription request (bare version)
1392 * Revision 1.40 2006/01/26 09:48:05 strk
1393 * Added limits.h include
1395 * Revision 1.39 2006/01/26 02:11:01 strk
1396 * Removed unused variables, fixed copyright date
1398 * Revision 1.38 2006/01/25 18:33:02 strk
1399 * Removed the -t swich, terminate_on_close made the default behaviour
1401 * Revision 1.37 2006/01/20 14:12:34 strk
1402 * Added linphonec_init() and linphonec_finish() functions.
1403 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1404 * Handling of auto-termination (-t) moved to linphonec_finish().
1405 * Reworked main (input read) loop to not rely on 'terminate'
1406 * and 'run' variable (dropped). configfile_name allocated on stack
1407 * using PATH_MAX limit. Changed print_usage signature to allow
1408 * for an exit_status specification.
1410 * Revision 1.36 2006/01/18 09:25:32 strk
1411 * Command completion inhibited in proxy addition and auth request prompts.
1412 * Avoided use of readline's internal filename completion.
1414 * Revision 1.35 2006/01/14 13:29:32 strk
1415 * Reworked commands interface to use a table structure,
1416 * used by command line parser and help function.
1417 * Implemented first level of completion (commands).
1418 * Added notification of invalid "answer" and "terminate"
1419 * commands (no incoming call, no active call).
1420 * Forbidden "call" intialization when a call is already active.
1421 * Cleaned up all commands, adding more feedback and error checks.
1423 * Revision 1.34 2006/01/13 13:00:29 strk
1424 * Added linphonec.h. Code layout change (added comments, forward decl,
1425 * globals on top, copyright notices and Logs). Handled out-of-memory
1426 * condition on history management. Removed assumption on sizeof(char).
1427 * Fixed bug in authentication prompt (introduced by readline).
1428 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1431 ****************************************************************************/