Changeset 1352fc1 in mainline
- Timestamp:
- 2009-11-30T15:00:23Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1df977c, ba26129
- Parents:
- 272f88f0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/edit/edit.c
r272f88f0 r1352fc1 36 36 37 37 #include <stdio.h> 38 #include <stdlib.h> 38 39 #include <sys/types.h> 39 40 #include <vfs/vfs.h> … … 104 105 static void key_handle_ctrl(console_event_t const *ev); 105 106 static int file_save(char const *fname); 107 static void file_save_as(void); 106 108 static int file_insert(char *fname); 107 109 static int file_save_range(char const *fname, spt_t const *spos, 108 110 spt_t const *epos); 111 static char *filename_prompt(char const *prompt, char const *init_value); 109 112 static void pane_text_display(void); 110 113 static void pane_row_display(void); … … 150 153 151 154 if (argc == 2) { 152 doc.file_name = argv[1];155 doc.file_name = str_dup(argv[1]); 153 156 } else if (argc > 1) { 154 157 printf("Invalid arguments.\n"); 155 158 return -2; 156 159 } else { 157 doc.file_name = "/edit.txt";160 doc.file_name = NULL; 158 161 } 159 162 160 163 new_file = false; 161 164 162 if ( file_insert(doc.file_name) != EOK)165 if (doc.file_name == NULL || file_insert(doc.file_name) != EOK) 163 166 new_file = true; 164 167 … … 170 173 pane_text_display(); 171 174 pane_status_display(); 172 if (new_file )173 status_display("File not found. Createdempty file.");175 if (new_file && doc.file_name != NULL) 176 status_display("File not found. Starting empty file."); 174 177 pane_caret_display(); 175 178 … … 266 269 break; 267 270 case KC_S: 268 (void) file_save(doc.file_name); 271 if (doc.file_name != NULL) 272 file_save(doc.file_name); 273 else 274 file_save_as(); 275 break; 276 case KC_E: 277 file_save_as(); 269 278 break; 270 279 default: … … 272 281 } 273 282 } 274 275 283 276 284 /** Save the document. */ … … 285 293 286 294 rc = file_save_range(fname, &sp, &ep); 287 status_display("File saved."); 295 296 switch (rc) { 297 case EINVAL: 298 status_display("Error opening file!"); 299 break; 300 case EIO: 301 status_display("Error writing data!"); 302 break; 303 default: 304 status_display("File saved."); 305 break; 306 } 288 307 289 308 return rc; 309 } 310 311 /** Change document name and save. */ 312 static void file_save_as(void) 313 { 314 char *old_fname, *fname; 315 int rc; 316 317 old_fname = (doc.file_name != NULL) ? doc.file_name : ""; 318 fname = filename_prompt("Save As", old_fname); 319 if (fname == NULL) { 320 status_display("Save cancelled."); 321 return; 322 } 323 324 rc = file_save(fname); 325 if (rc != EOK) 326 return; 327 328 if (doc.file_name != NULL) 329 free(doc.file_name); 330 doc.file_name = fname; 331 } 332 333 #define INPUT_MAX_LEN 64 334 335 /** Ask for a file name. */ 336 static char *filename_prompt(char const *prompt, char const *init_value) 337 { 338 console_event_t ev; 339 char *str; 340 wchar_t buffer[INPUT_MAX_LEN + 1]; 341 int nc; 342 bool done; 343 344 asprintf(&str, "%s: %s", prompt, init_value); 345 status_display(str); 346 console_goto(con, 1 + str_length(str), scr_rows - 1); 347 free(str); 348 349 console_set_color(con, COLOR_WHITE, COLOR_BLACK, 0); 350 351 str_to_wstr(buffer, INPUT_MAX_LEN + 1, init_value); 352 nc = wstr_length(buffer); 353 done = false; 354 355 while (!done) { 356 console_get_event(con, &ev); 357 358 if (ev.type == KEY_PRESS) { 359 /* Handle key press. */ 360 if (((ev.mods & KM_ALT) == 0) && 361 (ev.mods & KM_CTRL) != 0) { 362 ; 363 } else if ((ev.mods & (KM_CTRL | KM_ALT)) == 0) { 364 switch (ev.key) { 365 case KC_ESCAPE: 366 return NULL; 367 case KC_BACKSPACE: 368 if (nc > 0) { 369 putchar('\b'); 370 fflush(stdout); 371 --nc; 372 } 373 break; 374 case KC_ENTER: 375 done = true; 376 break; 377 default: 378 if (ev.c >= 32 && nc < INPUT_MAX_LEN) { 379 putchar(ev.c); 380 fflush(stdout); 381 buffer[nc++] = ev.c; 382 } 383 break; 384 } 385 } 386 } 387 } 388 389 buffer[nc] = '\0'; 390 391 str = malloc(STR_BOUNDS(wstr_length(buffer)) + 1); 392 if (str == NULL) 393 return NULL; 394 395 wstr_nstr(str, buffer, STR_BOUNDS(wstr_length(buffer)) + 1); 396 397 console_set_color(con, COLOR_BLACK, COLOR_WHITE, 0); 398 399 return str; 290 400 } 291 401 … … 359 469 } while (!spt_equal(&bep, epos)); 360 470 361 fclose(f); 471 if (fclose(f) != EOK) 472 return EIO; 362 473 363 474 return EOK; … … 473 584 spt_t caret_pt; 474 585 coord_t coord; 586 char *fname; 475 587 int n; 476 588 … … 478 590 spt_get_coord(&caret_pt, &coord); 479 591 592 fname = (doc.file_name != NULL) ? doc.file_name : "<unnamed>"; 593 480 594 console_goto(con, 0, scr_rows - 1); 481 595 console_set_color(con, COLOR_WHITE, COLOR_BLACK, 0); 482 n = printf(" %d, %d: File '%s'. Ctrl- S Save Ctrl-Q Quit",483 coord.row, coord.column, doc.file_name);596 n = printf(" %d, %d: File '%s'. Ctrl-Q Quit Ctrl-S Save " 597 "Ctrl-E Save As", coord.row, coord.column, fname); 484 598 printf("%*s", scr_columns - 1 - n, ""); 485 599 fflush(stdout);
Note:
See TracChangeset
for help on using the changeset viewer.