OSDN Git Service

e4401eedc23363ff5d9807883f30b4b23b4a0181
[ntch/develop.git] / src / ui / disp_editor.c
1 /* Copyright 2013 Akira Ohta (akohta001@gmail.com)
2     This file is part of ntch.
3
4     The ntch is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8
9     The ntch is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with ntch.  If not, see <http://www.gnu.org/licenses/>.
16     
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <signal.h>
25
26 #include "env.h"
27 #include "error.h"
28 #include "nt_string.h"
29 #include "_2ch/_2ch.h"
30 #include "utils/nt_std_t.h"
31 #include "utils/text.h"
32 #include "utils/file.h"
33 #include "ui/disp.h"
34
35
36 static BOOL read_editor(FILE *fp, nt_write_data_handle h_write_data,
37                 char *buf_reuse, size_t buf_len);
38
39 BOOL disp_editor2(const char *file_name)
40 {
41         char buf[1024*2];
42         int ret;
43         
44         sprintf(buf, "%s %s", EDITOR_CMD, file_name);
45         ret = system(buf);
46         if (WIFSIGNALED(ret) &&
47                 (WTERMSIG(ret) == SIGINT || 
48                          WTERMSIG(ret) == SIGQUIT))
49                 return FALSE;
50         return TRUE;
51 }
52
53 BOOL nt_link_cmp(nt_link_tp l, nt_link_tp r, nt_compare_fn cmp_func)
54 {
55         nt_link_tp lp, rp;
56         lp = l;
57         rp = r;
58         do{
59                 if(0 != cmp_func(lp->data, rp->data))
60                         return FALSE;
61                 lp = lp->next;
62                 rp = rp->next;
63         }while(l != lp || r != rp);
64         return (l == lp && r == rp);
65 }
66
67 nt_link_tp disp_editor3(nt_link_tp text_list)
68 {
69         FILE *tmp_fp;
70         pid_t pid;
71         char fname[512];
72         char buf[128];
73         int ret;
74         nt_link_tp out_linkp;
75         pid = getpid();
76         
77         sprintf(fname, "%s/txt%d.tmp", LOG_PATH, pid);
78         if(!text_list){
79                 tmp_fp = fopen(fname, "w");
80                 if(!tmp_fp)
81                         return FALSE;
82                 fclose(tmp_fp);
83         }else{
84                 if(!nt_write_text_file(fname, text_list))
85                         return NULL;
86         }
87         
88         sprintf(buf, "%s %s", EDITOR_CMD, fname);
89         ret = system(buf);
90         if (WIFSIGNALED(ret) &&
91                 (WTERMSIG(ret) == SIGINT || 
92                          WTERMSIG(ret) == SIGQUIT))
93                 return NULL;
94         
95         out_linkp =  nt_read_text_file(fname);
96         if(!out_linkp)
97                 return NULL;
98         
99         if(nt_link_cmp(out_linkp, text_list, nt_link_wcscmp_fnc)){
100                 nt_all_link_free(out_linkp, free);
101                 return NULL;
102         }
103         return out_linkp;
104 }
105
106
107 BOOL disp_editor(nt_write_data_handle h_write_data)
108 {
109         FILE *tmp_fp;
110         pid_t pid;
111         char fname[512];
112         char buf[1024*2];
113         int len, ret;
114         const char *name, *mail;
115         
116         assert(h_write_data);
117
118         name = nt_write_data_get_name(h_write_data);
119         mail = nt_write_data_get_mail(h_write_data);
120
121         pid = getpid();
122
123         sprintf(fname, "%s/msg%d.tmp", LOG_PATH, pid);
124         tmp_fp = fopen(fname, "w");
125         if(!tmp_fp)
126                 return FALSE;
127         
128         strcpy(buf, "name: ");
129         if(name)
130                 strcat(buf, name);
131         strcat(buf, "\n");
132         len = strlen(buf);      
133         if(len > fwrite(buf, sizeof(char), len, tmp_fp))
134                 goto ERROR_TRAP1;
135
136         sprintf(buf, "mail: %s\n\n", 
137                         (mail) ? mail : "sage");
138         len = strlen(buf);      
139         if(len > fwrite(buf, sizeof(char), len, tmp_fp))
140                 goto ERROR_TRAP1;
141         
142         fprintf(tmp_fp, "Please write your message here\n");
143         fclose(tmp_fp);
144
145         sprintf(buf, "%s %s", EDITOR_CMD, fname);
146         ret = system(buf);
147         if (WIFSIGNALED(ret) &&
148                 (WTERMSIG(ret) == SIGINT || 
149                          WTERMSIG(ret) == SIGQUIT))
150                 goto ERROR_TRAP2;
151
152         tmp_fp = fopen(fname, "r");
153         if(!tmp_fp)
154                 goto ERROR_TRAP2;
155
156         if(!read_editor(tmp_fp, h_write_data, buf, sizeof(buf)))
157                 goto ERROR_TRAP1;
158         
159
160         nt_write_data_set_status_msg(h_write_data, NT_INFO_WRITE_MSG_SUCCESS);
161         fclose(tmp_fp);
162         unlink(fname);
163         return TRUE;
164
165 ERROR_TRAP1:
166         fclose(tmp_fp);
167 ERROR_TRAP2:
168         unlink(fname);
169         return FALSE;
170 }
171
172 BOOL read_editor(FILE *fp, nt_write_data_handle h_write_data, 
173                 char *buf_reuse, size_t buf_len)
174 {
175         char *cptr, *cptr2;
176         char buf2[64];
177         int len;
178         int offset, nread;
179
180         /* Read headers. */
181         while(fgets(buf_reuse, buf_len, fp)){
182                 if(buf_reuse[0] == '\n' ||
183                                 buf_reuse[0] == '\0')
184                         break;
185                 cptr = strchr(buf_reuse, ':');
186                 if(!cptr)
187                         break;
188
189                 *cptr = '\0';
190                 cptr2 = nt_trim2(buf_reuse, buf2, sizeof(buf2));
191                 if(!cptr2)
192                         continue;
193
194                 if(0 == strcmp(cptr2, "name")){
195                         nt_write_data_set_name(h_write_data,cptr+1);
196                         //if(writep->name)
197                         //      free(writep->name);
198                         //writep->name = nt_trim(cptr+1);
199                 }else if(0 == strcmp(cptr2, "mail")){
200                         nt_write_data_set_mail(h_write_data,cptr+1);
201                         //if(writep->mail)
202                         //      free(writep->mail);
203                         //writep->mail = nt_trim(cptr+1);
204                 }
205         }
206         /* Read message to send */
207         buf_reuse[0] = '\0';
208         offset = 0;
209         while(fgets(buf_reuse+offset, buf_len-offset, fp)){
210                 nread = strlen(buf_reuse+offset);
211                 offset += nread;
212         }
213         len = strlen(buf_reuse);
214         if(len == 0){
215                 nt_write_data_set_status_msg(h_write_data, NT_ERR_MSG_WRITE_MSG_LENGTH);
216                 return FALSE;
217         }
218         if(!strcmp(buf_reuse, "Please write your message here\n")){
219                 nt_write_data_set_status_msg(h_write_data, NT_ERR_MSG_WRITE_MSG_ABORTED);
220                 return FALSE;
221         }
222         //cptr = malloc(len+1);
223         //strcpy(cptr, );
224         //writep->msg = cptr;
225         nt_write_data_set_msg(h_write_data, buf_reuse);
226         return TRUE;
227 }
228