~fcitx-team/fcitx/autoimport-m17n-master

« back to all changes in this revision

Viewing changes to testmim/getline.c

  • Committer: Cheer Xiao
  • Date: 2012-02-01 12:40:55 UTC
  • Revision ID: git-v1:a9cc1b9887d0ea1724f2f849392d3dcef4c1ecb5
Initial commit for Fcitx-m17n.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "getline.h"
 
2
#include <stdio.h>
 
3
 
 
4
#ifdef WITH_READLINE
 
5
#include <stdlib.h>
 
6
#include <readline/readline.h>
 
7
CFUNC char *mygetline(const char *prompt) {
 
8
        static char *line = NULL;
 
9
        if (line) {
 
10
                free(line);
 
11
        }
 
12
        return line = readline(prompt);
 
13
}
 
14
#else
 
15
#include <string.h>
 
16
enum { NLINE = 1024 };
 
17
CFUNC char *mygetline(const char *prompt) {
 
18
        static char line[NLINE];
 
19
        printf("%s", prompt);
 
20
        if (fgets(line, NLINE, stdin)) {
 
21
                int n = strlen(line);
 
22
                if (line[n-1] == '\n') {
 
23
                        line[n-1] = '\0';
 
24
                }
 
25
                return line;
 
26
        } else {
 
27
                return NULL;
 
28
        }
 
29
}
 
30
#endif